-
Notifications
You must be signed in to change notification settings - Fork 9
/
gps.cpp
192 lines (161 loc) · 4.7 KB
/
gps.cpp
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
#include "gps.h"
// Pointer to the incoming serial message buffer
String* GPS::message;
// Current status: true if we have reception
boolean GPS::reception;
// We keep the last valid state from gps here
GPSSTATUS GPS::last;
// Reference to the eventManager
EventManager* GPS::eventManager;
/**
* Parse incoming messages. This gets called once a complete
* message is ready in the message propertu
*/
void GPS::handleMessage(int eventCode, int eventParam) {
if (!calculateChecksum()) {
#ifdef DEBUG
Serial.print("X ");
Serial.print(*message);
Serial.println("Checksum error");
#endif
return;
}
if (message->startsWith("$GPRMC,")) {
#ifdef DEBUG
Serial.print("> ");
Serial.println(*message);
#endif
parseRMC(message);
}
}
/**
* Handle GPRMC messages
*/
void GPS::parseRMC(String* msg) {
byte offset = 7;
byte fieldNr = 0;
String current;
boolean old;
float rawCoord, rawSpd, rawHdg;
char rawIndicator;
while (fieldNr < 9) {
current = msg->substring(offset);
switch (fieldNr) {
// UTC Time
case 0:
last.time = current.substring(0,2).toInt() * 60 + current.substring(2,4).toInt();
break;
// Status
case 1:
old = reception;
if (current.charAt(0) == 'A') {
reception = true;
last.timestamp = millis();
} else if (current.charAt(0) == 'V') {
reception = false;
}
if (old != reception) {
eventManager->queueEvent(GPS_STATUS_CHANGED, reception);
}
break;
case 2: // Latitude
case 4: // Longitude
rawCoord = current.toFloat();
break;
case 3: // N/S Indicator
case 5: // E/W Indicator
if (!rawCoord) break;
rawIndicator = current.charAt(0);
if (fieldNr == 3) {
last.lat = toCoordinates(rawCoord, rawIndicator);
} else {
last.lng = toCoordinates(rawCoord, rawIndicator);
}
break;
// Speed Over Ground
case 6:
if (current.indexOf(',') == 0) {
last.spd = -1;
break;
}
last.spd = current.toFloat() * 1.852;
break;
// Course Over Ground
case 7:
if (current.indexOf(',') == 0) {
last.hdg = -1;
break;
}
last.hdg = current.toFloat();
// Date
case 8:
last.day = current.substring(0, 2).toInt() + (current.substring(2, 4).toInt() - 1) * 30;
break;
}
offset = msg->indexOf(',', offset) + 1;
fieldNr++;
}
if (reception) {
eventManager->queueEvent(GPS_UPDATED, 0);
}
#ifdef DEBUG
Serial.print("Lat: ");
Serial.println(last.lat,6);
Serial.print("Lng: ");
Serial.println(last.lng,6);
Serial.print("SPD: ");
Serial.println(last.spd);
Serial.print("HDG: ");
Serial.println(last.hdg);
Serial.print("Is night: ");
Serial.println(isNight());
#endif
}
/**
* Returns true if we're after sunset
* @return boolean true when it's night at the moment
*/
boolean GPS::isNight() {
SUNINFO today;
if (last.day < 0 || last.day > 359) return false;
memcpy_P(&today, &sunrisemap[last.day], sizeof(SUNINFO));
return last.time < today.sunrise || today.sunset < last.time;
}
/**
* Convert NMEA coordinates to defimal representation
* @param float the coordinate in ddmm.mmmm format
* @param char the N/s/E/W indicator
* @return float the converted value
*/
float GPS::toCoordinates(float coord, char indicator) {
byte deg;
float min, dec;
deg = coord / 100;
min = coord - (deg * 100);
dec = deg + min/60;
if (indicator == 'W' || indicator == 'S') {
dec = - dec;
}
return dec;
}
/**
* Calculate the checksum for the current NMEA sequence
* @returns 1 on success 0 else
*/
byte GPS::calculateChecksum() {
byte i, last;
uint16_t checksum;
String strChecksum;
last = message->lastIndexOf('*');
if (last < 0) return 0;
checksum = 0;
for (i=1; i<last; i++) {
checksum = checksum ^ message->charAt(i);
}
strChecksum = String(checksum, HEX);
strChecksum.toUpperCase();
if (checksum < 16) {
strChecksum = "0" + strChecksum;
}
return message->substring(last + 1, last + 3) == strChecksum;
}