-
Notifications
You must be signed in to change notification settings - Fork 3
/
GPS_Air530.cpp
432 lines (387 loc) · 9.14 KB
/
GPS_Air530.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include "Arduino.h"
#include "GPS_Air530.h"
HardwareSerial GPSSerial(UART_NUM_1);
double gps_x_pi = 3.14159265358979324 * 3000.0 / 180.0;
double gps_pi = 3.14159265358979324;
double gps_a = 6378245.0;
double gps_ee = 0.00669342162296594323;
int str_chop(char *s, int buff_size, char separator, uint8_t *idx_ary, int max_idx);
double transformLon(double x, double y);
double transformLat(double x, double y);
double outOfChina(double lat, double lon);
double transformLon(double x, double y) {
double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x));
ret += (20.0 * sin(6.0 * x * gps_pi) + 20.0 * sin(2.0 * x * gps_pi)) * 2.0 / 3.0;
ret += (20.0 * sin(x * gps_pi) + 40.0 * sin(x / 3.0 * gps_pi)) * 2.0 / 3.0;
ret += (150.0 * sin(x / 12.0 * gps_pi) + 300.0 * sin(x / 30.0 * gps_pi)) * 2.0 / 3.0;
return ret;
};
double transformLat(double x, double y) {
double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x));
ret += (20.0 * sin(6.0 * x * gps_pi) + 20.0 * sin(2.0 * x * gps_pi)) * 2.0 / 3.0;
ret += (20.0 * sin(y * gps_pi) + 40.0 * sin(y / 3.0 * gps_pi)) * 2.0 / 3.0;
ret += (160.0 * sin(y / 12.0 * gps_pi) + 320 * sin(y * gps_pi / 30.0)) * 2.0 / 3.0;
return ret;
}
double outOfChina(double lat, double lon) {
if (lon < 72.004 || lon > 137.8347)
return true;
if (lat < 0.8293 || lat > 55.8271)
return true;
return false;
}
String calchecksum(String cmd)
{
uint8_t checksum=cmd[1];
char temp[5];
for(int i=2;i<cmd.length();i++)
{
checksum^=cmd[i];
}
memset(temp,0,5);
sprintf(temp, "*%02X\r\n", checksum);
cmd += temp;
return cmd;
}
Air530Class::Air530Class(int8_t powerCtl)
:_powerCtl(powerCtl)
{}
void Air530Class::begin()
{
pinMode(_powerCtl,OUTPUT);
digitalWrite(_powerCtl, LOW);
GPSSerial.begin(9600); // was 9600
delay(1000);
}
void Air530Class::setmode(GPSMODE mode)
{
String cmd="";
switch(mode)
{
case MODE_GPS:
cmd += "$PGKC115,1,0,0,0*2B\r\n";
break;
case MODE_GPS_BEIDOU:
cmd += "$PGKC115,1,0,1,0*2A\r\n";
break;
case MODE_GPS_GLONASS:
cmd += "$PGKC115,1,1,0,0*2A\r\n";
break;
case MODE_GPS_GALILEO: // added section for Galileo
cmd += "$PGKC115,1,0,0,1*2A\r\n";
break;
case MODE_GPS_MULTI: // added section for Multi-GPS
cmd += "$PGKC115,1,1,1,0*2B\r\n";
break;
default:
break;
}
sendcmd(cmd);
}
void Air530Class::setNMEA(uint8_t nmeamode)
{
String cmd = "$PGKC242,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0";
if(nmeamode & NMEA_GLL)
{
cmd[9] = '1';
}
if(nmeamode & NMEA_RMC)
{
cmd[11] = '1';
}
if(nmeamode & NMEA_VTG)
{
cmd[13] = '1';
}
if(nmeamode & NMEA_GGA)
{
cmd[15] = '1';
}
if(nmeamode & NMEA_GSA)
{
cmd[17] = '1';
}
if(nmeamode & NMEA_GSV)
{
cmd[19] = '1';
}
if(nmeamode & NMEA_GRS)
{
cmd[21] = '1';
}
if(nmeamode & NMEA_GST)
{
cmd[23] = '1';
}
cmd = calchecksum(cmd);
sendcmd(cmd);
}
void Air530Class::reset()
{
String cmd = "$PGKC030,3,1*2E\r\n";
sendcmd(cmd);
}
int Air530Class::available(void)
{
return GPSSerial.available();
}
int Air530Class::read(void)
{
return GPSSerial.read();
}
void Air530Class::clear()
{
String cmd = "$PGKC040*2B\r\n";
sendcmd(cmd);
}
void Air530Class::setPPS(uint8_t mode, uint16_t pulse_width)
{
String cmd = "$PGKC161,";
char temp[15];
if(pulse_width>=999)
{
pulse_width = 998;
}
memset(temp,0,15);
sprintf(temp, "%d,%d,%d", mode,pulse_width,1000);
cmd += temp;
cmd = calchecksum(cmd);
sendcmd(cmd);
}
void Air530Class::end()
{
digitalWrite(_powerCtl, HIGH);
GPSSerial.end();
}
void Air530Class::sendcmd(String cmd)
{
while(GPSSerial.available())//wait for gps serial idle
{
GPSSerial.readStringUntil('\n');
}
GPSSerial.print(cmd);
}
String Air530Class::getNMEA()
{
uint32_t starttime = millis();
String nmea = "";
while(millis() - starttime <1000)
{
if(GPSSerial.available())
{
char c = GPSSerial.read();
if(c=='$')
{
nmea += c;
nmea += GPSSerial.readStringUntil('\n');
return nmea;
}
}
}
return "0";
}
String Air530Class::getRMC()
{
String nmea = "";
uint32_t starttime = millis();
while(millis() - starttime <1000)
{
if ( GPSSerial.available())
{
char c = GPSSerial.read();
if(c=='$')
{
nmea = GPSSerial.readStringUntil('\n');
if(nmea[2] == 'R' && nmea[3] == 'M' && nmea[4] == 'C')
{
nmea = c + nmea;
return nmea;
}
}
}
}
return "0";
}
String Air530Class::getGGA()
{
String nmea = "";
uint32_t starttime = millis();
while(millis() - starttime <1000)
{
if ( GPSSerial.available())
{
char c = GPSSerial.read();
if(c=='$')
{
nmea = GPSSerial.readStringUntil('\n');
if(nmea[2] == 'G' && nmea[3] == 'G' && nmea[4] == 'A')
{
nmea = c + nmea;
return nmea;
}
}
}
}
return "0";
}
String Air530Class::getVTG()
{
String nmea = "";
uint32_t starttime = millis();
while(millis() - starttime <1000)
{
if ( GPSSerial.available())
{
char c = GPSSerial.read();
if(c=='$')
{
nmea = GPSSerial.readStringUntil('\n');
if(nmea[2] == 'V' && nmea[3] == 'T' && nmea[4] == 'G')
{
nmea = c + nmea;
return nmea;
}
}
}
}
return "0";
}
String Air530Class::getGSA()
{
String nmea = "";
uint32_t starttime = millis();
while(millis() - starttime <1000)
{
if ( GPSSerial.available())
{
char c = GPSSerial.read();
if(c=='$')
{
nmea = GPSSerial.readStringUntil('\n');
if(nmea[2] == 'G' && nmea[3] == 'S' && nmea[4] == 'A')
{
nmea = c + nmea;
return nmea;
}
}
}
}
return "0";
}
String Air530Class::getGSV()
{
String nmea = "";
uint32_t starttime = millis();
while(millis() - starttime <1000)
{
if ( GPSSerial.available())
{
char c = GPSSerial.read();
if(c=='$')
{
nmea = GPSSerial.readStringUntil('\n');
if(nmea[2] == 'G' && nmea[3] == 'S' && nmea[4] == 'V')
{
nmea = c + nmea;
return nmea;
}
}
}
}
return "0";
}
String Air530Class::getGLL()
{
String nmea = "";
uint32_t starttime = millis();
while(millis() - starttime <1000)
{
if ( GPSSerial.available())
{
char c = GPSSerial.read();
if(c=='$')
{
nmea = GPSSerial.readStringUntil('\n');
if(nmea[2] == 'G' && nmea[3] == 'L' && nmea[4] == 'L')
{
nmea = c + nmea;
return nmea;
}
}
}
}
return "0";
}
/*
* WGS-84: international standard GPS coordinate£¨Google Earth¡¢ GPS module¡¢Tian Map£©
* GCJ-02: China coordinate migration standard, Google Map¡¢Gaode Map¡¢Tencent map
* BD-09: Baidu coordinate offset standard, Baidu Map
*/
//WGS-84 to GCJ-02
/*
gps_status_t Air530Class::WGSToGCJ(gps_status_t status) {
if (outOfChina(status.latitude, status.longitude)) {
return status;
}
double dLat = transformLat(status.longitude - 105.0, status.latitude - 35.0);
double dLon = transformLon(status.longitude - 105.0, status.latitude - 35.0);
double radLat = status.latitude / 180.0 * gps_pi;
double magic = sin(radLat);
magic = 1 - gps_ee * magic * magic;
double sqrtMagic = sqrt(magic);
dLat = (dLat * 180.0) / ((gps_a * (1 - gps_ee)) / (magic * sqrtMagic) * gps_pi);
dLon = (dLon * 180.0) / (gps_a / sqrtMagic * cos(radLat) * gps_pi);
double lat = status.latitude + dLat;
double lon = status.longitude + dLon;
status.latitude = lat;
status.longitude = lon;
return status;
};
gps_status_t Air530Class::GCJToBD(gps_status_t status){
double x = status.longitude;
double y = status.latitude;
double z = sqrt(x * x + y * y) + 0.00002 * sin(y * gps_x_pi);
double theta = atan2(y, x) + 0.000003 * cos(x * gps_x_pi);
double bd_lon = z * cos(theta) + 0.0065;
double bd_lat = z * sin(theta) + 0.006;
status.latitude=bd_lat;
status.longitude=bd_lon;
return status;
};
gps_status_t Air530Class::WGSToBD(gps_status_t status)
{
status = WGSToGCJ(status);
status = GCJToBD(status);
return status;
}
*/
int str_chop(char *s, int buff_size, char separator, uint8_t *idx_ary, int max_idx) {
int i = 0; /* index in the string */
int j = 0; /* index in the result array */
if ((s == NULL) || (buff_size < 0) || (separator == 0) || (idx_ary == NULL) || (max_idx < 0)) {
/* unsafe to do anything */
return -1;
}
if ((buff_size == 0) || (max_idx == 0)) {
/* nothing to do */
return 0;
}
s[buff_size - 1] = 0; /* add string terminator at the end of the buffer, just to be sure */
idx_ary[j] = 0;
j += 1;
/* loop until string terminator is reached */
while (s[i] != 0) {
if (s[i] == separator) {
s[i] = 0; /* replace separator by string terminator */
if (j >= max_idx) { /* no more room in the index array */
return j;
}
idx_ary[j] = i+1; /* next token start after replaced separator */
++j;
}
++i;
}
return j;
}
//Air530Class GPS(GPIO14);
Air530Class Air530(GPIO14);