-
Notifications
You must be signed in to change notification settings - Fork 2
/
GPS.cpp
executable file
·362 lines (337 loc) · 10.5 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
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
/********************************************************************
* GPS.cpp - Libary for using the GPS module - LS20126 from Sparkfun *
*********************************************************************/
#if defined(ARDUINO) && ARDUINO >= 100
// Choose Arduino.h for IDE 1.0
#include "Arduino.h"
#else
// Choose WProgram.h if IDE is older than 1.0
#include "WProgram.h"
#endif
#include "RR_GPS.h"
#include <math.h>
char byteGPS = -1;
char buffer[300] = "";
int bufferIndex = 0;
RR_GPS::RR_GPS(void)
#if USING_GPS_SOFTWARESERIAL
:gps_SoftSerial(GPS_RX_PIN,GPS_TX_PIN)
#endif
{
//_baud = baud;
pinMode(GPS_ENABLE_PIN,OUTPUT);
_new_term=false;
On=false;
}
void RR_GPS::Enable()
{
setTarget(TARGET_LAT, TARGET_LON);
digitalWrite(GPS_ENABLE_PIN,HIGH);
Serial.println(GGA_OFF);
delay(200);
//Serial.println(INIT);
//delay(200);
Serial.println(GLL_OFF);
delay(200);
Serial.println(GSA_OFF);
delay(200);
Serial.println(GSV_OFF);
delay(200);
Serial.println(VTG_OFF);
delay(200);
Serial.println(PLSR_ON_5HZ);
delay(200);
//Serial.println(BAUD57600);
//delay(200);
//Serial.begin(57600);
char temp = '\0';
while(temp != '$')
{
if(Serial.available()){
temp = Serial.read();
}
}
On=true;
}
void RR_GPS::Disable()
{
digitalWrite(GPS_ENABLE_PIN,LOW);
}
uint8_t RR_GPS::readMessage(boolean readRMC)
{
char RMCBuffer[300] = "", PLSRBuffer[300] = "";
boolean RMC_completed = false, PLSR_completed = false;
while(1)
{
if(Serial.available())
{
byteGPS = Serial.read(); // Read a byte of the serial port
//if(DBUG) Serial.print(byteGPS);
if(byteGPS == -1)
{ // Wait 100ms if Port empty
if(DBUG) Serial.println(F("Empty Port"));
return 0;
}
else
{
if(byteGPS=='$')
{
_new_term=true;
_term_type=_TERM_OTHER;
// Serial.println("New Term");
}
else if(_new_term) // $ sign not carried to buffer
{
switch(_term_type)
{
case _TERM_OTHER: //New Term. Store enough characters to determine if of interest.
buffer[bufferIndex] = byteGPS; // Store valid Serial data in Buffer
bufferIndex++;
if(bufferIndex==10)
{
if(strncmp(buffer,_PLSR_COMPASS1_ID,10)==0&&!PLSR_completed)
{
strcpy(PLSRBuffer,buffer);
buffer[0] = '\0';
_term_type=_TERM_PLSR;
}
else if(strncmp(buffer,_RMC_ID,5)==0&&!RMC_completed)
{
if(readRMC){
strcpy(RMCBuffer,buffer);
buffer[0] = '\0';
_term_type=_TERM_RMC;
}
else
{
_new_term=false;
_term_type=_TERM_OTHER;
bufferIndex = 0;
buffer[0] = '\0';
}
}
else // Message not of interest
{
// Serial.println("Message Not Interesting");
_new_term=false;
_term_type=_TERM_OTHER;
bufferIndex = 0;
buffer[0] = '\0';
}
}
break;
case _TERM_RMC:
RMCBuffer[bufferIndex]=byteGPS;
bufferIndex++;
//Serial.write(byteGPS);
if(byteGPS == 10)
{
//Serial.write(RMCBuffer);
RMCBuffer[bufferIndex]='\0';
bufferIndex=0;
_new_term=false;
if(DBUG) Serial.println(F("RMC Reading completed"));
RMC_completed = true;
}
break;
case _TERM_PLSR:
PLSRBuffer[bufferIndex]=byteGPS;
bufferIndex++;
if(byteGPS==10)
{
// Serial.write(PLSRBuffer);
PLSRBuffer[bufferIndex]='\0';
bufferIndex=0;
_new_term=false;
// Serial.println("PLSR Reading completed");
PLSR_completed = true;
}
break;
}
}
}
}
if((PLSR_completed&&!readRMC)||RMC_completed&&PLSR_completed)
{
if(readRMC){
extractData(RMCBuffer,RMC);
extractData(PLSRBuffer,PLSR);
if(status[0]!='A'){
if(gps_fix)
{
gps_fix=false;
}
RMC_completed = false;
Serial.println(F("Invalid RMC data. Reading again!"));
break;
}
else
{
if(!gps_fix)
{
gps_fix=true;
}
Serial.println(F("UpdateTarget"));
updateTargetDistance();
updateTargetHeading();
break;
}
}
else
{
extractData(PLSRBuffer,PLSR);
if(gps_fix)
{
updateTargetHeading();
}
break;
}
}
}
return 1;
}
void RR_GPS::storeRMCData(char value[], int segment)
{
switch(segment)
{
case TIME:
strcpy(time,value);
break;
case STATUS:
strcpy(status,value);
break;
case LATITUDE:
latitude = atof(value);
break;
case NS:
if(value[0] == 'S')
latitude *= -1;
break;
case LONGITUDE:
longitude = atof(value);
break;
case EW:
if(value[0] == 'W')
longitude *= -1;
break;
}
}
void RR_GPS::storePLSRData(char value[], int segment)
{
switch(segment)
{
case DIR:
dir = atof(value);
break;
case AX:
ax = atof(value);
break;
case AY:
ay = atof(value);
break;
case AZ:
az = atof(value);
break;
}
}
void RR_GPS::extractData(char line[], int type)
{
int index = 0, segment = 0, indexTemp = 0;
char segmentData[15] = "";
while(line[index]){
if(line[index] == ','){
segmentData[indexTemp] = '\0';
if(type == RMC){
storeRMCData(segmentData,segment);
}else if(type == PLSR){
storePLSRData(segmentData,segment);
}
segment++;
indexTemp = 0;
}else{
segmentData[indexTemp] = line[index];
indexTemp++;
if(line[index] == '*'){
// Checksum is being ignored here.
return;
}
}
index++;
}
}
/*
void RR_GPS::displayData(int type)
{
if(type == RMC)
{
Serial.println(F(" Printing GPRMC Report"));
Serial.print(F(" Time: "));
Serial.println(time);
Serial.print(F(" Status: "));
Serial.println(status[0]);
Serial.print(F(" Latitude: "));
Serial.println(latitude,4);
Serial.print(F(" Longitude: "));
Serial.println(longitude,4);
Serial.print(F(" Speed: "));
Serial.println(spd);
Serial.print(F(" Course: "));
Serial.println(course);
Serial.print(F(" Date: "));
Serial.println(date);
}
else if(type == PLSR)
{
Serial.println(F(" Printing PLSR Report"));
Serial.print(F(" Direction: "));
Serial.println(dir);
Serial.print(F(" Calib Status: "));
Serial.println(cstatus);
Serial.print(F(" Field Intensity: "));
Serial.println(fint);
Serial.print(F(" Ax: "));
Serial.println(ax);
Serial.print(F(" Ay: "));
Serial.println(ay);
Serial.print(F(" Az: "));
Serial.println(az);
Serial.print(F(" Temp: "));
Serial.println(temperature);
Serial.print(F(" Mode: "));
Serial.println(mm);
Serial.print(F(" CCD Status: "));
Serial.println(ccds);
}
}
*/
void RR_GPS::setTarget(float latitude, float longitude)
{
target_latitude = latitude;
target_longitude = longitude;
}
void RR_GPS::updateTargetDistance()
{
// Using Haversine formula for calculating Over-the-surface distance
// Source: http://www.movable-type.co.uk/scripts/latlong.html
int R = 6371; // Mean Earth radius
float lat1 = toRad(latitude);
float lat2 = toRad(target_latitude);
float dlat = lat2-lat1, dlong = toRad(target_longitude)-toRad(longitude);
float a = sin(dlat/2)*sin(dlat/2)+sin(dlong/2)*sin(dlong/2)*cos(lat1)*cos(lat2);
float c = 2*atan2(sqrt(a),sqrt(1-a));
distance_rem = R*c;
// Using the formula for bearing to calculate the required current heading.
float y = sin(dlong)*cos(lat2);
float x = cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(dlong);
_bearing = fmod((atan2(y,x)*57.295779513+360),360); // Multiplied by constant to convert to decimal degrees.
}
void RR_GPS::updateTargetHeading()
{
delta_heading_reqd = _bearing - dir;
}
float RR_GPS::toRad(float value)
{
float min = fmod(value, 100.0)/60.0;
float deg = float(int(value/100));
return (deg+min)*PI/180.0;
}