-
Notifications
You must be signed in to change notification settings - Fork 2
/
gpslogger.ino
358 lines (307 loc) · 8.9 KB
/
gpslogger.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/**
* Fetch GPS data over serial and log it to an SD card. See README for more.
*/
#include <SoftwareSerial.h>
#include <SdFat.h>
#include <TinyGPS.h>
#define SAMPLE_INTERVAL_MS 1000
#define PIN_STATUS_LED 13
#define PIN_RX_FROM_GPS 2
#define PIN_TX_TO_GPS 4
#define PIN_SD_CHIP_SELECT 10
#define PIN_SPI_CHIP_SELECT_REQUIRED 10
// The SD library requires, for SPI communication:
// 11 MOSI (master/Arduino output, slave/SD input)
// 12 MISO (master input, slave output)
// 13 CLK (clock)
#define DEFAULT_GPS_BAUD 57600
#define GPS_BAUD 14400
// Seek to fileSize + this position before writing track points.
#define SEEK_TRKPT_BACKWARDS -24
#define GPX_EPILOGUE "\t</trkseg></trk>\n</gpx>\n"
#define LATLON_PREC 6
#define PIN_BATTERY_DIVIDED_VOLTAGE A3
#define VOLTAGE_ANALOG_CONSTANT 53.71
#define CUTOFF_DV -0.1
#define CUTOFF_VOLTAGE 3.4
TinyGPS gps;
SoftwareSerial nss(PIN_RX_FROM_GPS, PIN_TX_TO_GPS);
SdFat sd;
SdFile gpxFile;
SdFile voltageFile;
// General-purpose text buffer used in formatting.
char buf[32];
struct GpsSample {
float lat_deg,
lon_deg;
float altitude_m;
int satellites;
int hdop_hundredths;
// How many ms, according to millis(), since the last position data was read
// from the GPS.
unsigned long fix_age_ms;
float speed_mps;
float course_deg;
// How many ms, according to millis(), since the last datetime data was read
// from the GPS.
unsigned long datetime_fix_age_ms;
int year;
byte month,
day,
hour,
minute,
second,
hundredths;
};
// The latest sample read from the GPS.
struct GpsSample sample;
// The previously sampled / recorded voltages.
float lastVoltage;
float lastRecordedVoltage;
byte lastRecordedVoltageMinute;
void setup() {
pinMode(PIN_STATUS_LED, OUTPUT);
digitalWrite(PIN_STATUS_LED, HIGH);
lastVoltage = 0;
lastRecordedVoltage = 0;
lastRecordedVoltageMinute = 61;
Serial.begin(115200);
setUpSd();
resetGpsConfig();
getFirstGpsSample();
startFilesOnSdNoSync();
digitalWrite(PIN_STATUS_LED, LOW);
}
void loop() {
readFromGpsUntilSampleTime();
fillGpsSample(gps);
if (sample.fix_age_ms <= SAMPLE_INTERVAL_MS) {
// TODO: Write whenever there is new data (trust GPS is set at 1Hz).
writeGpxSampleToSd();
}
recordVoltage();
}
void setUpSd() {
if (PIN_SD_CHIP_SELECT != PIN_SPI_CHIP_SELECT_REQUIRED) {
pinMode(PIN_SPI_CHIP_SELECT_REQUIRED, OUTPUT);
}
if (!sd.begin(PIN_SD_CHIP_SELECT, SPI_QUARTER_SPEED)) {
sd.initErrorHalt();
}
}
/**
* Redoes GPS configuration, assuming it has factory-default settings.
*
* Although this should only have to be done once, the GPS module sometimes
* drops these customizations (possibly due to power brown-out).
*/
void resetGpsConfig() {
digitalWrite(PIN_STATUS_LED, HIGH);
nss.begin(DEFAULT_GPS_BAUD);
nss.println("$PMTK314,0,5,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0*28");
nss.println("$PMTK251,14400*29");
nss.flush();
nss.end();
digitalWrite(PIN_STATUS_LED, LOW);
nss.begin(GPS_BAUD);
}
void getFirstGpsSample() {
nss.begin(GPS_BAUD);
while (true) {
readFromGpsUntilSampleTime();
fillGpsSample(gps);
if (sample.fix_age_ms == TinyGPS::GPS_INVALID_AGE) {
Serial.println(F("Waiting for location fix."));
} else if (sample.fix_age_ms == TinyGPS::GPS_INVALID_AGE) {
Serial.println(F("Waiting for datetime fix."));
} else {
Serial.println(F("Got GPS fix."));
break;
}
}
}
static void readFromGpsUntilSampleTime() {
unsigned long start = millis();
// Process a sample from the GPS every second.
while (millis() - start < SAMPLE_INTERVAL_MS) {
readFromGpsSerial();
getVoltageMaybeExit();
}
}
static bool readFromGpsSerial() {
while (nss.available()) {
gps.encode(nss.read());
}
}
static void startFilesOnSdNoSync() {
// directory
sprintf(
buf,
"%02d%02d%02d",
sample.year,
sample.month,
sample.day);
if (!sd.exists(buf)) {
if (!sd.mkdir(buf)) {
sd.errorHalt("Creating log directory for today failed.");
}
}
// SdFat will silently die if given a filename longer than "8.3"
// (8 characters, a dot, and 3 file-extension characters).
// GPX log
openTimestampedFile(".gpx", gpxFile);
gpxFile.print(F(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<gpx version=\"1.0\">\n"
"\t<trk><trkseg>\n"));
gpxFile.print(F(GPX_EPILOGUE));
// input (battery) voltage log
openTimestampedFile("v.csv", voltageFile);
voltageFile.print(F("datetime,voltage\n"));
}
static void openTimestampedFile(const char *shortSuffix, SdFile &file) {
sprintf(
buf,
"%02d%02d%02d/%02d%02d%02d%s",
sample.year,
sample.month,
sample.day,
sample.hour,
sample.minute,
sample.second,
shortSuffix);
Serial.print(F("Starting file "));
Serial.println(buf);
if (sd.exists(buf)) {
Serial.println(F("warning: already exists, overwriting."));
}
if (!file.open(buf, O_CREAT | O_WRITE)) {
sd.errorHalt();
}
}
static void writeFloat(float v, SdFile &file, int precision) {
obufstream ob(buf, sizeof(buf));
ob << setprecision(precision) << v;
file.print(buf);
}
static void writeFormattedSampleDatetime(SdFile &file) {
sprintf(
buf,
"%04d-%02d-%02dT%02d:%02d:%02d.%03dZ",
sample.year,
sample.month,
sample.day,
sample.hour,
sample.minute,
sample.second,
sample.hundredths);
file.print(buf);
}
static void writeGpxSampleToSd() {
gpxFile.seekSet(gpxFile.fileSize() + SEEK_TRKPT_BACKWARDS);
gpxFile.print(F("\t\t<trkpt "));
gpxFile.print(F("lat=\""));
writeFloat(sample.lat_deg, gpxFile, LATLON_PREC);
gpxFile.print(F("\" lon=\""));
writeFloat(sample.lon_deg, gpxFile, LATLON_PREC);
gpxFile.print(F("\">"));
getVoltageMaybeExit();
gpxFile.print(F("<time>"));
writeFormattedSampleDatetime(gpxFile);
gpxFile.print(F("</time>"));
if (sample.altitude_m != TinyGPS::GPS_INVALID_F_ALTITUDE) {
gpxFile.print(F("<ele>")); // meters
writeFloat(sample.altitude_m, gpxFile, 2 /* centimeter precision */);
gpxFile.print(F("</ele>"));
}
getVoltageMaybeExit();
if (sample.speed_mps != TinyGPS::GPS_INVALID_F_SPEED) {
gpxFile.print(F("<speed>"));
writeFloat(sample.speed_mps, gpxFile, 1);
gpxFile.print(F("</speed>"));
}
if (sample.course_deg != TinyGPS::GPS_INVALID_F_ANGLE) {
gpxFile.print(F("<course>"));
writeFloat(sample.course_deg, gpxFile, 1);
gpxFile.print(F("</course>"));
}
getVoltageMaybeExit();
if (sample.satellites != TinyGPS::GPS_INVALID_SATELLITES) {
gpxFile.print(F("<sat>"));
gpxFile.print(sample.satellites);
gpxFile.print(F("</sat>"));
}
if (sample.hdop_hundredths != TinyGPS::GPS_INVALID_HDOP) {
gpxFile.print(F("<hdop>"));
writeFloat(sample.hdop_hundredths / 100.0, gpxFile, 2);
gpxFile.print(F("</hdop>"));
}
getVoltageMaybeExit();
gpxFile.print(F("</trkpt>\n"));
gpxFile.print(F(GPX_EPILOGUE));
getVoltageMaybeExit();
digitalWrite(PIN_STATUS_LED, HIGH);
if (!gpxFile.sync() || gpxFile.getWriteError()) {
Serial.println(F("SD sync/write error."));
}
digitalWrite(PIN_STATUS_LED, LOW);
}
static void fillGpsSample(TinyGPS &gps) {
gps.f_get_position(
&sample.lat_deg,
&sample.lon_deg,
&sample.fix_age_ms);
sample.altitude_m = gps.f_altitude();
sample.satellites = gps.satellites();
sample.hdop_hundredths = gps.hdop();
sample.course_deg = gps.f_course();
sample.speed_mps = gps.f_speed_mps();
gps.crack_datetime(
&sample.year,
&sample.month,
&sample.day,
&sample.hour,
&sample.minute,
&sample.second,
&sample.hundredths,
&sample.datetime_fix_age_ms);
}
static float getVoltageMaybeExit() {
// TODO: Do safety calculation in int to be faster?
float voltage =
analogRead(PIN_BATTERY_DIVIDED_VOLTAGE) / VOLTAGE_ANALOG_CONSTANT;
float dv = voltage - lastVoltage;
// Regulated (USB) supply voltage is approximately 0 on the battery vin,
// otherwise abort if vin is low or falling.
if (voltage > 2.0
&& (voltage < CUTOFF_VOLTAGE || dv < CUTOFF_DV)) {
gpxFile.close();
voltageFile.print(F("x"));
voltageFile.close();
digitalWrite(PIN_STATUS_LED, HIGH);
Serial.print(F("Exiting, final voltage was "));
Serial.println(voltage);
Serial.flush();
exit(0);
// TODO: Resume in case of a false alarm.
}
lastVoltage = voltage;
return voltage;
}
static bool recordVoltage() {
float voltage = getVoltageMaybeExit();
if (voltage != lastRecordedVoltage
|| lastRecordedVoltageMinute != sample.minute) {
writeFormattedSampleDatetime(voltageFile);
voltageFile.print(F(","));
writeFloat(voltage, voltageFile, 2);
voltageFile.print(F("\n"));
lastRecordedVoltageMinute = sample.minute;
lastRecordedVoltage = voltage;
digitalWrite(PIN_STATUS_LED, HIGH);
if (!voltageFile.sync() || voltageFile.getWriteError()) {
Serial.println(F("SD sync/write error."));
}
digitalWrite(PIN_STATUS_LED, LOW);
}
}