Skip to content

Commit 1617041

Browse files
working, adjust X,Y tolerances as needed. refactor main.cpp for this
1 parent bbb0fe3 commit 1617041

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

Telemetry-Main/LapCounter.cpp

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
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+
1115
LapCounter::LapCounter(VehicleState state) {
1216
resetLapCounter(state);
1317
}
@@ -38,7 +42,10 @@ void LapCounter::resetLapCounter(VehicleState state) {
3842
}
3943

4044
void 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
}

Telemetry-Main/LapCounter.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Made to separate lap counting from VSM. Uses VSM GPS and HEADING/TRUE_COURSE
66
* data. Make sure to call updateLapCounter a lot.
77
*
8-
* Note: "local coordinate plane" is defined as: nose towards positive x
8+
* Note: "local coordinate plane" is defined as: nose towards positive x, positive y is left i think
99
*/
1010

1111
#ifndef LAP_COUNTER_HPP
@@ -35,7 +35,7 @@ struct LapCounterData {
3535
float lap_curr_heading_f;
3636

3737
uint8_t lap_counter; // MARK: surely we don't do more than 255 laps(?)
38-
std::chrono::microseconds lap_time;
38+
float lap_time;
3939
};
4040

4141
class LapCounter {
@@ -56,9 +56,9 @@ class LapCounter {
5656
*/
5757
void updateLapCounter(VehicleState state);
5858
/**
59-
* Gets the lap time in microseconds.
59+
* Gets the lap time in seconds.
6060
*/
61-
std::chrono::microseconds getTime();
61+
float getTime();
6262
};
6363

6464
#endif

0 commit comments

Comments
 (0)