-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGPS.ino
More file actions
384 lines (324 loc) · 9.82 KB
/
Copy pathGPS.ino
File metadata and controls
384 lines (324 loc) · 9.82 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <TinyGPS++.h>
// The TinyGPS++ object
TinyGPSPlus gps;
//============================================================================
// Check the GPS
//============================================================================
void CheckGPS() {
processGPSData();
}
//============================================================================
// Check the communication with the GPS
//============================================================================
static void GPSCommTest(unsigned long ms)
{
#if defined(ALLOWDEBUG)
unsigned long start = millis();
toSerialConsole("---START GPS COMMUNICATION CHECK---\n");
toSerialConsole("-----------------------------------\n");
do
{
if (SerialGPS.available())
Serial.write(SerialGPS.read());
} while (millis() - start < ms);
toSerialConsole("-----------------------------------\n");
toSerialConsole("----END GPS COMMUNICATION CHECK----\n\n");
#endif
}
//============================================================================
// This custom version of delay() ensures that the gps object is being "fed".
//============================================================================
static void smartDelay(unsigned long ms) {
unsigned long start = millis();
do {
while (SerialGPS.available())
gps.encode(SerialGPS.read());
} while (millis() - start < ms);
}
//============================================================================
// Check all the GPS values and add to the UGPS object
//============================================================================
static void processGPSData() {
// Number of Satellitese
if (gps.satellites.isValid())
UGPS.Satellites = gps.satellites.value();
else
UGPS.Satellites = 0;
// Time
if (gps.time.isValid()) {
UGPS.Hours = gps.time.hour();
UGPS.Minutes = gps.time.minute();
UGPS.Seconds = gps.time.second();
UGPS.Day = gps.date.day();
UGPS.Month = gps.date.month();
UGPS.Year = gps.date.year();
} else {
UGPS.Hours = 0;
UGPS.Minutes = 0;
UGPS.Seconds = 0;
UGPS.Day = 0;
UGPS.Day = 0;
UGPS.Month = 0;
UGPS.Year = 0;
}
// Position
if (gps.location.isValid()) {
UGPS.validPosition = true;
UGPS.Longitude = gps.location.lng();
UGPS.Latitude = gps.location.lat();
UGPS.Heading = gps.course.deg();
} else {
UGPS.validPosition = false;
UGPS.Longitude = 0;
UGPS.Latitude = 0;
UGPS.Heading = 0;
}
// Altitude
if (gps.altitude.isValid())
UGPS.Altitude = gps.altitude.meters();
else
UGPS.Altitude = 0;
if (UGPS.Altitude < 0)
UGPS.Altitude = 0;
// Speed
if (gps.speed.isValid())
UGPS.Speed = gps.speed.kmph();
else
UGPS.Speed = 0;
}
//============================================================================
// Generate a random string
//============================================================================
String generateRandomString(int length) {
// De mogelijke karakters waar de string uit kan bestaan
const char charset[] = "abcdefghijklmnopqrstuvwxyz0123456789";
String result = "";
for (int i = 0; i < length; i++) {
// Kies een willekeurige index uit de charset
int index = random(0, sizeof(charset) - 1);
result += charset[index];
}
return result;
}
//============================================================================
// Create a foldername giving the year, month, day of the GPS
//============================================================================
String getSDFolderName()
{
String result = "";
// Date: YYYYMMDD
result += String(UGPS.Year);
if (UGPS.Month < 10) result += "0";
result += String(UGPS.Month);
if (UGPS.Day < 10) result += "0";
result += String(UGPS.Day);
return result;
}
//============================================================================
// Create a Time / Location stamp that can be used as a filename for example
//============================================================================
String getTimeLocationString()
{
String result = "";
// Date: YYYYMMDD
result += String(UGPS.Year);
if (UGPS.Month < 10) result += "0";
result += String(UGPS.Month);
if (UGPS.Day < 10) result += "0";
result += String(UGPS.Day);
result += " ";
// Time: HHMMSS
if (UGPS.Hours < 10) result += "0";
result += String(UGPS.Hours);
if (UGPS.Minutes < 10) result += "0";
result += String(UGPS.Minutes);
if (UGPS.Seconds < 10) result += "0";
result += String(UGPS.Seconds);
result += 'z';
result += " ";
// Coördinates and Altitude
result += String(UGPS.Latitude, 4);
result += " ";
result += String(UGPS.Longitude, 4);
result += " ";
result += String(UGPS.Altitude);
return result;
}
//============================================================================
// Create a string with coordinates and time for the JPEG low res image
//============================================================================
String getJPGOverlayString()
{
String result = "";
// Date: YYYY-MM-DD
result += String(UGPS.Year);
result += "-";
if (UGPS.Month < 10) result += "0";
result += String(UGPS.Month);
result += "-";
if (UGPS.Day < 10) result += "0";
result += String(UGPS.Day);
result += " ";
// Time: HH:MM:SS
if (UGPS.Hours < 10) result += "0";
result += String(UGPS.Hours);
result += ":";
if (UGPS.Minutes < 10) result += "0";
result += String(UGPS.Minutes);
result += ":";
if (UGPS.Seconds < 10) result += "0";
result += String(UGPS.Seconds);
result += 'z';
result += " ";
// Coördinates and Altitude
result += "lat: ";
result += String(UGPS.Latitude, 4);
result += " ";
result += "lon: ";
result += String(UGPS.Longitude, 4);
result += " ";
result += "alt: ";
result += String(UGPS.Altitude);
result += "m";
return result;
}
//============================================================================
// Print all GPS data for debugging and conveniance
//============================================================================
void printGPSData() {
String str;
toSerialConsole(" Time: ");
toSerialConsole(UGPS.Hours);
toSerialConsole(":");
toSerialConsole(UGPS.Minutes);
toSerialConsole(":");
toSerialConsole(UGPS.Seconds); toSerialConsole("\n");
toSerialConsole(" Latitude: ");
toSerialConsole(UGPS.Latitude, 6);
str = getAPRSlat(UGPS.Latitude);
toSerialConsole(" (");
toSerialConsole(str);
toSerialConsole(")\n");
toSerialConsole(" Longitude: ");
toSerialConsole(UGPS.Longitude, 6);
str = getAPRSlon(UGPS.Longitude);
toSerialConsole(" (");
toSerialConsole(str);
toSerialConsole(")\n");
toSerialConsole(" Altitude: ");
toSerialConsole(UGPS.Altitude);
str = getAPRSAlt(UGPS.Altitude);
toSerialConsole(" (");
toSerialConsole(str);
toSerialConsole(")\n");
toSerialConsole(" Satellites: ");
toSerialConsole(UGPS.Satellites); toSerialConsole("\n");
}
//============================================================================
// Translate the decimal GPS longitude from the GPS to APRS
// compatible coordinates
//============================================================================
String getAPRSlon(float lon_dd) {
float lon_d1;
int lon_d;
float lon_m1;
int lon_m;
float lon_s;
String lon_d_str;
String lon_m_str;
char lon_s_str[6];
String lon;
String SouthN;
// West or East
if (lon_dd < 0)
SouthN = "W";
else
SouthN = "E";
// Calculate the string for the degrees
lon_d1 = abs(lon_dd);
lon_d = lon_d1;
lon_d_str = String(lon_d);
if (abs(lon_d) < 10) {
lon_d_str = "00" + lon_d_str;
} else if (abs(lon_d) < 100) {
lon_d_str = "0" + lon_d_str;
}
// Calculate the string for the minutes
lon_m1 = (lon_d1 - lon_d) * 60;
lon_m = lon_m1;
lon_m_str = String(lon_m);
if (abs(lon_m) < 10) {
lon_m_str = "0" + lon_m_str;
}
// Calculate the string for the seconds
lon_s = (lon_m1 - lon_m);
dtostrf(lon_s, 4, 2, lon_s_str);
lon = lon_d_str + lon_m_str + &lon_s_str[1] + SouthN;
return lon;
}
//============================================================================
// Create an APRS compatible timestamp from the GPS time
//============================================================================
String getAPRStimestamp() {
char buf[10];
sprintf(buf, "%02d%02d%02dh", UGPS.Hours, UGPS.Minutes, UGPS.Seconds);
return buf;
}
//============================================================================
// Translate the decimal GPS latitude from the GPS to APRS
// compatible coordinates
//============================================================================
String getAPRSlat(float lat_dd) {
float lat_d1;
int lat_d;
float lat_m1;
int lat_m;
float lat_s;
String lat_d_str;
String lat_m_str;
char lat_s_str[6];
String lat;
String lat_SouthN;
// South or North
if (lat_dd < 0)
lat_SouthN = "S";
else
lat_SouthN = "N";
// Calculate the string for the degrees
lat_d1 = abs(lat_dd);
lat_d = lat_d1;
lat_d_str = String(lat_d);
if (abs(lat_d) < 10) {
lat_d_str = "0" + lat_d_str;
}
// Calculate the string for the minutes
lat_m1 = (lat_d1 - lat_d) * 60;
lat_m = lat_m1;
lat_m_str = String(lat_m);
if (abs(lat_m) < 10) {
lat_m_str = "0" + lat_m_str;
}
// Calculate the string for the seconds
lat_s = (lat_m1 - lat_m);
dtostrf(lat_s, 4, 2, lat_s_str);
lat = lat_d_str + lat_m_str + &lat_s_str[1] + lat_SouthN;
return lat;
}
//============================================================================
// Translate the GPS altitude from the GPS to APRS
// compatible altitude
//============================================================================
String getAPRSAlt(long Alt) {
long Feet;
char alt_str[15];
String res = "";
// altitudein feet
Feet = Alt * 3.28;
ltoa(Feet, alt_str, 10);
res = alt_str;
for (int i = (6 - res.length()); i > 0; i--) {
res = "0" + res;
}
res = "/A=" + res;
return res;
}