-
Notifications
You must be signed in to change notification settings - Fork 349
Expand file tree
/
Copy pathLap.cpp
More file actions
67 lines (56 loc) · 1.84 KB
/
Lap.cpp
File metadata and controls
67 lines (56 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "Lap.hpp"
#include <iomanip>
#include <sstream>
#include <cmath>
Lap::Lap(std::shared_ptr<OdometryNode> pose3d) : pose3d_(pose3d) {
reset();
}
std::string Lap::check_threshold() {
if (!pause_condition_) {
if (start_time_.time_since_epoch().count() != 0 && !lap_rest_) {
auto now = std::chrono::system_clock::now();
if (lap_time_.count() == 0) {
lap_time_ = now - start_time_;
} else {
lap_time_ += now - start_time_;
}
start_time_ = now;
}
if (start_time_.time_since_epoch().count() == 0 && lap_rest_) {
start_time_ = std::chrono::system_clock::now();
lap_rest_ = false;
}
}
if (lap_time_.count() == 0) {
return "0";
}
double total_seconds = lap_time_.count();
int hours = static_cast<int>(total_seconds / 3600);
int minutes = static_cast<int>((total_seconds - hours * 3600) / 60);
double seconds = total_seconds - hours * 3600 - minutes * 60;
std::stringstream ss;
ss << hours << ":"
<< std::setfill('0') << std::setw(2) << minutes << ":"
<< std::setfill('0') << std::setw(2) << static_cast<int>(seconds) << "."
<< std::setfill('0') << std::setw(6) << static_cast<int>(std::round((seconds - std::floor(seconds)) * 1000000));
return ss.str();
}
std::string Lap::return_lap_time() {
return std::to_string(lap_time_.count());
}
void Lap::reset() {
start_time_ = std::chrono::system_clock::time_point();
lap_time_ = std::chrono::duration<double>::zero();
lap_rest_ = true;
buffer_ = false;
pause_condition_ = false;
}
void Lap::pause() {
pause_condition_ = true;
}
void Lap::unpause() {
if (pause_condition_) {
start_time_ = std::chrono::system_clock::now();
}
pause_condition_ = false;
}