-
Notifications
You must be signed in to change notification settings - Fork 2
/
real_track_data_debug.ino
182 lines (154 loc) · 5.47 KB
/
real_track_data_debug.ino
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* Real debug using adafruit GPS library without a GPS connected
*
* Will probably be cleaned up... eventually... to replace current unit-test suite
*
* 3 different data-sets available
*/
#include <stdlib.h>
#include <string.h>
// reminder: this example pauses code until terminal connected
#define HAS_DEBUG
#define DEBUG_SERIAL Serial
#ifdef HAS_DEBUG
#define debugln DEBUG_SERIAL.println
#define debug DEBUG_SERIAL.print
#else
void dummy_debug(...) {}
#define debug dummy_debug
#define debugln dummy_debug
#endif
// were not actually using the GPS, we just need the library
#define GPS_SERIAL Serial1
#include <Adafruit_GPS.h>
Adafruit_GPS* gps = NULL;
#include <DovesLapTimer.h>
// probably dont change unless you know what you're doing
// might be better off making track dependant??
double crossingThresholdMeters = 7.0;
DovesLapTimer lapTimer(crossingThresholdMeters, &DEBUG_SERIAL);
// DovesLapTimer lapTimer(crossingThresholdMeters);
// orlando kart center
// Width of your crossing line is somewhat important
// Do not make the width of your crossing line too much larger than the track
// a tolerence is already included, more testing to be done tho
// these two are closer to turn 10 and make it better for testing the detection
const double crossingPointALat = 28.41270817056385;
const double crossingPointALng = -81.37973266418031;
const double crossingPointBLat = 28.41273038679321;
const double crossingPointBLng = -81.37957048753776;
// Actual data samples from a trip to the track
// #include "gps_race_data_lap.h" // normal track
#include "gps_race_data_2laps.h" // normal track
// #include "gps_race_data_long_lap.h" // long track
const int num_gps_logs = sizeof(gps_logs) / sizeof(gps_logs[0]);
// Define a static variable to keep track of the last processed line
static int last_processed_line = -1;
/**
* Returns the next NMEA data string from a pre-defined array of GPS logs.
* The function stores the logs in a global array and keeps track of the
* last processed line using a static variable. Each time the function is
* called, it returns the next line in the array until all the lines have
* been processed. If there are no more lines to process, the function
* returns NULL.
*
* @return A pointer to a dynamically allocated string containing the next
* NMEA data string from the array, or NULL if there are no more
* lines to process. The caller is responsible for freeing the memory
* allocated by this function.
*/
char *gpsLastFakeNMEA() {
// Check if we've processed all the lines
if (last_processed_line >= num_gps_logs - 1) {
return NULL;
}
// Update the last processed line
last_processed_line++;
// Allocate a new string to hold the NMEA data
char* nmea_data = (char*) malloc(strlen(gps_logs[last_processed_line]) + 1);
// Copy the NMEA data into the new string
strcpy(nmea_data, gps_logs[last_processed_line]);
// Return the new string
return nmea_data;
}
/**
* @brief Returns the GPS time since midnight in milliseconds
*
* @return unsigned long The time since midnight in milliseconds
*/
unsigned long getGpsTimeInMilliseconds() {
unsigned long timeInMillis = 0;
timeInMillis += gps->hour * 3600000ULL; // Convert hours to milliseconds
timeInMillis += gps->minute * 60000ULL; // Convert minutes to milliseconds
timeInMillis += gps->seconds * 1000ULL; // Convert seconds to milliseconds
timeInMillis += gps->milliseconds; // Add the milliseconds part
return timeInMillis;
}
void gpsLoop() {
if (gps->parse(gpsLastFakeNMEA())) {
// update the timer loop everytime we have fixed data
if (gps->fix) {
// Update current time
lapTimer.updateCurrentTime(getGpsTimeInMilliseconds());
// Update current posistional data
float altitudeMeters = gps->altitude;
float speedKnots = gps->speed;
lapTimer.loop(gps->latitudeDegrees, gps->longitudeDegrees, altitudeMeters, speedKnots);
}
}
}
void setup() {
#ifdef HAS_DEBUG
Serial.begin(9600);
while (!Serial);
#endif
debugln("Started");
gps = new Adafruit_GPS(&GPS_SERIAL);
// initialize laptimer class
lapTimer.setStartFinishLine(crossingPointALat, crossingPointALng, crossingPointBLat, crossingPointBLng);
lapTimer.forceLinearInterpolation();
// lapTimer.forceCatmullRomInterpolation();
// reset everything back to zero
lapTimer.reset();
debugln(F("GPS Lap Timer Started"));
}
bool done = false;
unsigned long lastLapTime = -1;
void loop() {
if (last_processed_line >= num_gps_logs - 1 ) {
if (!done) {
done = true;
debugln();
debugln("~~~~~~~~Finished~~~~~~~~");
}
return;
}
// will process one loop at a time
gpsLoop();
if (last_processed_line > 0) {
debug(".");
if (lastLapTime != lapTimer.getLastLapTime()) {
lastLapTime = lapTimer.getLastLapTime();
debugln();
debug("LastLapTime: ");
debug(lastLapTime / 1000);
debug(".");
debugln(lastLapTime % 1000);
}
// if (lapTimer.getRaceStarted()) {
// debugln();
// debug("getCurrentLapTime: ");
// debug(lapTimer.getCurrentLapTime() / 1000);
// debug(".");
// debugln(lapTimer.getCurrentLapTime() % 1000);
// }
// for multilap datasets
// if (lapTimer.getRaceStarted()) {
// debugln();
// debug("getPaceDifference: ");
// debug(lapTimer.getPaceDifference());
// }
// emulate realtime
// delay(1000 / 25);
}
}