22
33#include " LapCounter.hpp"
44
5+ #define DEG_TO_RAD ((float ) M_PI / 180 .0f )
6+
57#define EARTH_RADIUS 6378137 // earths radius in meters, used for conversion to local coordinate plane
68#define X_TOLERANCE 8 .0f // check lap completion if x is within +- X_TOLERANCE meters of home x coord
79#define Y_TOLERANCE 8 .0f // check lap completion if y is within +- Y_TOLERANCE meters of home y coord
8- #define HEADING_TOLERANCE 30 .0f // check lap completion if heading is within +- HEADING_TOLERANCE degrees of home
10+ #define HEADING_TOLERANCE 80 .0f // check lap completion if heading is within +- HEADING_TOLERANCE degrees of home
911#define MIN_LAP_TIME 10 // minimum lap time in seconds. used to ensure same lap isn't counted twice
1012
13+ using namespace std ;
14+
1115LapCounter::LapCounter (VehicleState state) {
1216 resetLapCounter (state);
1317}
@@ -38,7 +42,10 @@ void LapCounter::resetLapCounter(VehicleState state) {
3842}
3943
4044void LapCounter::updateLapCounter (VehicleState state) {
41- // Weighted avg for prev to average out noise
45+ /* *
46+ * UPDATE POSITION
47+ */
48+ // Get prev data, weighted avg to average out noise
4249 data.lap_prev_latitude_f = 0 .1f * data.lap_prev_latitude_f + 0 .9f * data.lap_curr_latitude_f ;
4350 data.lap_prev_longitude_f = 0 .1f * data.lap_prev_longitude_f + 0 .9f * data.lap_curr_longitude_f ;
4451 data.lap_prev_x_f = 0 .1f * data.lap_prev_x_f + 0 .9f * data.lap_curr_x_f ;
@@ -48,37 +55,40 @@ void LapCounter::updateLapCounter(VehicleState state) {
4855 // Get curr data, transform to local coordinate plane
4956 data.lap_curr_latitude_f = state.vdmGpsLatLong .LATITUDE ;
5057 data.lap_curr_longitude_f = state.vdmGpsLatLong .LONGITUDE ;
51- data.lap_curr_x_f = EARTH_RADIUS * (data.lap_curr_longitude_f -
52- data.lap_home_longitude_f ) * cos (M_PI * data.lap_home_latitude_f /
53- 180 );
54- data.lap_curr_y_f = EARTH_RADIUS * (data.lap_curr_latitude_f -
55- data.lap_home_latitude_f );
58+ data.lap_curr_x_f = EARTH_RADIUS * DEG_TO_RAD * (data.lap_curr_longitude_f - data.lap_home_longitude_f ) * cosf (DEG_TO_RAD * data.lap_home_latitude_f ); // lat, long is degrees -> translate to rad
59+ data.lap_curr_y_f = EARTH_RADIUS * DEG_TO_RAD * (data.lap_curr_latitude_f - data.lap_home_latitude_f );
60+ data.lap_curr_heading_f = (float ) (state.vdmGpsData .TRUE_COURSE % 36000 ) * 0 .01f ; // scale to 0-359.99
61+
5662 // TRANSFORM: Apply rotation matrix, home heading is x axis.
57- double home_heading_rad_f = M_PI * data.lap_home_heading_f / 180 ; // convert to rad for trig funcs
58- double temp_x_f = data.lap_curr_x_f * cos (home_heading_rad_f) + data.lap_curr_y_f * sin (home_heading_rad_f);
59- double temp_y_f = data.lap_curr_x_f * -sin (home_heading_rad_f) + data.lap_curr_y_f * cos (home_heading_rad_f);
63+ float home_heading_rad_f = DEG_TO_RAD * data.lap_home_heading_f ; // convert to rad for trig funcs
64+ float temp_x_f = data.lap_curr_x_f * cosf (home_heading_rad_f) + data.lap_curr_y_f * sinf (home_heading_rad_f);
65+ float temp_y_f = data.lap_curr_x_f * -sinf (home_heading_rad_f) + data.lap_curr_y_f * cosf (home_heading_rad_f);
6066 data.lap_curr_x_f = temp_x_f;
6167 data.lap_curr_y_f = temp_y_f;
6268
63- data.lap_curr_heading_f = (float ) (state.vdmGpsData .TRUE_COURSE % 36000 ) * 0 .01f ; // scale to 0-359.99
64-
69+ /* *
70+ * CHECK IF LAP COMPLETED
71+ */
6572 // If nowhere near home coords/heading, return immediately
6673 if (abs (data.lap_curr_x_f - data.lap_home_x_f ) > X_TOLERANCE) return ;
6774 if (abs (data.lap_curr_y_f - data.lap_home_y_f ) > Y_TOLERANCE) return ;
6875 if (abs (data.lap_curr_heading_f - data.lap_home_heading_f ) > HEADING_TOLERANCE) return ;
69- if (timer.elapsed_time ().count () < MIN_LAP_TIME) return ;
70-
71- // If close enough and crossed home line, increment counter
72- if (data.lap_curr_x_f > data.lap_home_x_f && data.lap_prev_x_f < data.lap_home_x_f ) {
73- data.lap_counter ++;
74- data.lap_time = timer.elapsed_time ();
75-
76+ if (timer.elapsed_time ().count () < MIN_LAP_TIME) {
7677 timer.reset ();
7778 timer.start ();
79+
7880 return ;
7981 }
82+
83+ // Lap has been completed, all conditions pass
84+ data.lap_counter ++;
85+ data.lap_time = std::chrono::duration<float >{timer.elapsed_time ()}.count ();
86+
87+ timer.reset ();
88+ timer.start ();
89+ return ;
8090}
8191
82- std::chrono::microseconds LapCounter::getTime () {
83- return timer.elapsed_time ();
92+ float LapCounter::getTime () {
93+ return std::chrono::duration< float >{ timer.elapsed_time ()}. count ();
8494}
0 commit comments