1+ #include " Lap.hpp"
2+ #include < iomanip>
3+ #include < sstream>
4+ #include < cmath>
5+
6+ Lap::Lap (std::shared_ptr<OdometryNode> pose3d) : pose3d_(pose3d) {
7+ reset ();
8+ }
9+
10+ std::string Lap::check_threshold () {
11+ std::lock_guard<std::mutex> lock (lap_mutex_);
12+
13+ if (!pause_condition_) {
14+ if (start_time_.time_since_epoch ().count () != 0 && !lap_rest_) {
15+ auto now = std::chrono::system_clock::now ();
16+ if (lap_time_.count () == 0 ) {
17+ lap_time_ = now - start_time_;
18+ } else {
19+ lap_time_ += now - start_time_;
20+ }
21+ start_time_ = now;
22+ }
23+
24+ if (start_time_.time_since_epoch ().count () == 0 && lap_rest_) {
25+ start_time_ = std::chrono::system_clock::now ();
26+ lap_rest_ = false ;
27+ }
28+ }
29+
30+ if (lap_time_.count () == 0 ) {
31+ return " 0" ;
32+ }
33+
34+ double total_seconds = lap_time_.count ();
35+ int hours = static_cast <int >(total_seconds / 3600 );
36+ int minutes = static_cast <int >((total_seconds - hours * 3600 ) / 60 );
37+ double seconds = total_seconds - hours * 3600 - minutes * 60 ;
38+
39+ std::stringstream ss;
40+ ss << hours << " :"
41+ << std::setfill (' 0' ) << std::setw (2 ) << minutes << " :"
42+ << std::setfill (' 0' ) << std::setw (2 ) << static_cast <int >(seconds) << " ."
43+ << std::setfill (' 0' ) << std::setw (6 ) << static_cast <int >(std::round ((seconds - std::floor (seconds)) * 1000000 ));
44+
45+ return ss.str ();
46+ }
47+
48+ std::string Lap::return_lap_time () {
49+ std::lock_guard<std::mutex> lock (lap_mutex_);
50+ return std::to_string (lap_time_.count ());
51+ }
52+
53+ void Lap::reset () {
54+ std::lock_guard<std::mutex> lock (lap_mutex_);
55+ start_time_ = std::chrono::system_clock::time_point ();
56+ lap_time_ = std::chrono::duration<double >::zero ();
57+ lap_rest_ = true ;
58+ buffer_ = false ;
59+ pause_condition_ = false ;
60+ }
61+
62+ void Lap::pause () {
63+ std::lock_guard<std::mutex> lock (lap_mutex_);
64+ pause_condition_ = true ;
65+ }
66+
67+ void Lap::unpause () {
68+ std::lock_guard<std::mutex> lock (lap_mutex_);
69+ if (pause_condition_) {
70+ start_time_ = std::chrono::system_clock::now ();
71+ }
72+ pause_condition_ = false ;
73+ }
0 commit comments