/
gps.c
373 lines (283 loc) · 9.79 KB
/
gps.c
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
/*
* $Id: gps.c,v 1.20 2009-06-03 07:38:36 la7eca Exp $
* NMEA data
*/
#include "defines.h"
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <inttypes.h>
#include <string.h>
#include <stdio.h>
#include "kernel/timer.h"
#include "kernel/stream.h"
#include "hdlc.h"
#include "fbuf.h"
#include "ax25.h"
#include "config.h"
#include "gps.h"
#include <math.h>
#define NMEA_BUFSIZE 80
#define NMEA_MAXTOKENS 16
/* Defined in commands.c */
uint8_t tokenize(char*, char*[], uint8_t, char*, bool);
/* Defined in uart.c */
Stream* uart_tx_init(uint16_t);
Stream* uart_rx_init(uint16_t, bool);
/* Current position */
posdata_t current_pos;
/* Local handlers */
static void do_rmc (uint8_t, char**, Stream*);
static void do_gga (uint8_t, char**, Stream*);
static void nmeaListener (void);
void notify_fix (bool);
static char buf[NMEA_BUFSIZE];
static bool monitor_pos, monitor_raw;
static Stream *in, *out;
static bool is_fixed = true;
extern uint8_t blink_length, blink_interval;
static Cond wait_gps;
void gps_init(Stream *outstr)
{
cond_init(&wait_gps);
monitor_pos = monitor_raw = false;
uint16_t baud;
GET_PARAM(GPS_BAUD, &baud);
in = uart_rx_init(baud, FALSE);
out = outstr;
THREAD_START(nmeaListener, STACK_GPSLISTENER);
make_output(GPSON);
set_port(GPSON);
}
void gps_on()
{
clear_port(GPSON);
notify_fix(false);
}
void gps_off()
{
set_port(GPSON);
BLINK_NORMAL
}
/*************************************************************************
* Compute distance (in meters) between two gps positions
* compute bearing based on two gps positions
*************************************************************************/
/* The usual PI/180 constant */
static const double DEG_TO_RAD = 0.017453292519943295769236907684886;
/* Earth's quatratic mean radius for WGS-84 */
static const double EARTH_RADIUS_IN_METERS = 6372797.560856;
/*
* Computes the arc, in radians, between two WGS-84 positions.
* Use the Haversine formula.
* http://en.wikipedia.org/wiki/Law_of_haversines
*/
static double arcInRadians(posdata_t *from, posdata_t *to)
{
double latitudeArc = (from->latitude - to->latitude) * DEG_TO_RAD;
double longitudeArc = (from->longitude - to->longitude) * DEG_TO_RAD;
double latitudeH = sin(latitudeArc * 0.5);
latitudeH *= latitudeH;
double lontitudeH = sin(longitudeArc * 0.5);
lontitudeH *= lontitudeH;
double tmp = cos(from->latitude * DEG_TO_RAD) * cos(to->latitude * DEG_TO_RAD);
return 2.0 * asin(sqrt(latitudeH + tmp * lontitudeH));
}
uint32_t gps_distance(posdata_t *from, posdata_t *to)
{
return (uint32_t) round(EARTH_RADIUS_IN_METERS * arcInRadians(from, to));
}
uint16_t gps_bearing(posdata_t *from, posdata_t *to)
{
double dLon = (from->longitude - to->longitude) * DEG_TO_RAD;
double toLat = to->latitude * DEG_TO_RAD;
double fromLat = from->latitude * DEG_TO_RAD;
if (dLon == 0 && toLat==fromLat)
return -1;
double y = sin(dLon) * cos(from->latitude * DEG_TO_RAD);
double x = cos(toLat) * sin(fromLat) - sin(toLat) * cos(fromLat) * cos(dLon);
uint16_t brng = (uint16_t) round(atan2(y, x) / DEG_TO_RAD);
return (brng + 180) % 360;
}
/**************************************************************************
* Read and process NMEA sentences.
* Not thread safe. Use in at most one thread, use lock, or allocate
* buf on the stack instead.
**************************************************************************/
bool nmea_ok = false;
static void nmeaListener()
{
char* argv[16];
uint8_t argc;
while (1) {
uint8_t checksum = 0;
int c_checksum;
getstr(in, buf, NMEA_BUFSIZE, '\n');
if (buf[0] != '$')
continue;
/* If requested, show raw NMEA packet on screen */
if (monitor_raw) {
putstr(out, buf);
putstr(out, "\n");
}
/* Checksum (optional) */
uint8_t i;
for (i=1; i<NMEA_BUFSIZE && buf[i] !='*' && buf[i] != 0 ; i++)
checksum ^= buf[i];
if (buf[i] == '*') {
buf[i] = 0;
sscanf(buf+i+1, "%X", &c_checksum);
if (c_checksum != checksum)
continue;
}
nmea_ok = true;
/* Split input line into tokens */
argc = tokenize(buf, argv, NMEA_MAXTOKENS, ",", false);
/* Select command handler */
if (strncmp("RMC", argv[0]+3, 3) == 0)
do_rmc(argc, argv, out);
else if (strncmp("GGA", argv[0]+3, 3) == 0)
do_gga(argc, argv, out);
}
}
/****************************************************************
* Monitoring control
* nmea_mon_pos - valid GPRMC position reports
* nmea_mon_raw - NMEA packets
* nmea_mon_off - turn it all off
****************************************************************/
void gps_mon_pos(void)
{ monitor_pos = true; }
void gps_mon_raw(void)
{ monitor_raw = true; }
void gps_mon_off(void)
{ monitor_pos = monitor_raw = false; }
/****************************************************************
* Convert position NMEA fields to float (degrees)
****************************************************************/
static void str2coord(const uint8_t ndeg, const char* str, float* coord)
{
float minutes;
char dstring[ndeg+1];
/* Format [ddmm.mmmmm] */
strncpy(dstring, str, ndeg);
dstring[ndeg] = 0;
sscanf(dstring, "%f", coord); /* Degrees */
sscanf(str+ndeg, "%f", &minutes); /* Minutes */
*coord += (minutes / 60);
}
/*****************************************************************
* Convert date/time NMEA fields to 32 bit integer (timestamp)
*****************************************************************/
static void nmea2time( timestamp_t* t, const char* timestr, const char* datestr)
{
uint16_t hour, min, sec;
uint16_t date, month, year;
sscanf(timestr, "%2u%2u%2u", &hour, &min, &sec);
sscanf(datestr, "%2u%2u%2u", &date, &month, &year);
*t = (uint32_t)
((uint32_t) date-1) * 86400 + ((uint32_t)hour) * 3600 + ((uint32_t)min) * 60 + sec;
}
char* time2str(char* buf, timestamp_t time)
{
sprintf(buf, "%2u:%2u:%2u",
(uint8_t) ((time / 3600) % 24), (uint8_t) ((time / 60) % 60), (uint8_t) (time % 60) );
return buf;
}
/****************************************************************
* handle changes in GPS lock - mainly change LED blinking
****************************************************************/
void notify_fix(bool lock)
{
if (!lock)
BLINK_GPS_SEARCHING
else {
if (!is_fixed) {
notifyAll(&wait_gps);
}
BLINK_NORMAL
}
is_fixed = lock;
}
bool gps_is_fixed()
{ return is_fixed && GET_BYTE_PARAM(TRACKER_ON); }
/* Return true if we waited */
bool gps_wait_fix(uint16_t timeout)
{
Timer ttimer;
if (is_fixed)
return false;
if (timeout > 0) {
timer_set(&ttimer, timeout);
timer_callback(&ttimer, (CBfunc) notifyAll, &wait_gps);
}
wait(&wait_gps);
timer_cancel(&ttimer);
return true;
}
bool gps_hasWaiters()
{ return hasWaiters(&wait_gps); }
uint16_t course_count = 0;
float altitude = -1;
/****************************************************************
* Handle RMC line
****************************************************************/
static void do_rmc(uint8_t argc, char** argv, Stream *out)
{
static uint8_t lock_cnt = 4;
char buf[95], tbuf[9];
if (argc != 13) /* Ignore if wrong format */
return;
if (*argv[2] != 'A') {
notify_fix(false); /* Ignore if receiver not in lock */
lock_cnt = 4;
return;
}
else
if (lock_cnt > 0) {
lock_cnt--;
return;
}
lock_cnt = 1;
notify_fix(true);
/* get timestamp */
nmea2time(¤t_pos.timestamp, argv[1], argv[9]);
/* get latitude [ddmm.mmmmm] */
str2coord(2, argv[3], ¤t_pos.latitude);
if (*argv[4] == 'S')
current_pos.latitude = -current_pos.latitude;
/* get longitude [dddmm.mmmmm] */
str2coord(3, argv[5], ¤t_pos.longitude);
if (*argv[6] == 'W')
current_pos.longitude = -current_pos.longitude;
/* get speed [nnn.nn] */
if (*argv[7] != '\0')
sscanf(argv[7], "%f", ¤t_pos.speed);
else
current_pos.speed = 0;
/* get course [nnn.nn] */
if (*argv[8] != '\0') {
float x;
sscanf(argv[8], "%f", &x);
current_pos.course = (uint16_t) x+0.5;
}
else
current_pos.course = 0;
current_pos.altitude = altitude;
/* If requested, show position on screen */
if (monitor_pos) {
sprintf_P(buf, PSTR("TIME: %s, POS: lat=%f, long=%f, SPEED: %f km/h, COURSE: %u deg\r\n"),
time2str(tbuf, current_pos.timestamp), current_pos.latitude, current_pos.longitude,
current_pos.speed*KNOTS2KMH, current_pos.course);
putstr(out, buf);
}
}
/*******************************************
* Get altitude from GGA line
*******************************************/
static void do_gga(uint8_t argc, char** argv, Stream *out)
{
if (argc == 15 && *argv[6] > '0')
sscanf(argv[9], "%f", &altitude);
else
altitude = -1;
}