nmea.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. #ifdef PC_BUILD
  2. #include "../gps-test-tool/main.h"
  3. #else
  4. #include "main.h"
  5. #endif
  6. /*----------------------------------------------------*/
  7. /* Get a line received from GPS module */
  8. /*----------------------------------------------------*/
  9. uint16_t get_line ( /* 0:line incomplete or timed out, >0: Number of bytes received. */
  10. char *buff,
  11. uint16_t sz_buf
  12. )
  13. {
  14. char c;
  15. static uint16_t i = 0;
  16. uint16_t ret_len;
  17. set_timer(recv_timeout, 1000);
  18. for (;;) {
  19. wdt_reset();
  20. if (FLAGS & (F_LVD | F_POWEROFF))
  21. return 0; /* A brownout is detected */
  22. if (timer_expired(recv_timeout))
  23. return 0; /* timeout; continue the main loop */
  24. if (System.keypress) /* process user keypress */
  25. return 0;
  26. if (!uart0_test()) {
  27. sleep();
  28. continue;
  29. }
  30. c = (char)uart0_get();
  31. if (i == 0 && c != '$' )
  32. continue; /* Find start of line */
  33. if (c == '\n' || c == '\r') {
  34. buff[i++] = '\r';
  35. buff[i++] = '\n';
  36. buff[i++] = '\0'; /* add null termination for string */
  37. break; /* EOL */
  38. }
  39. buff[i++] = c;
  40. if (System.conf.uart_log_mode != UART_LOG_MODE_SYSTEM)
  41. uart1_put(c);
  42. if (i >= sz_buf - 3) /* keep 3 bytes for terminating character */
  43. i = 0; /* Buffer overflow (abort this line) */
  44. }
  45. ret_len = i;
  46. i = 0;
  47. if (ret_len > 0 && System.conf.uart_log_mode != UART_LOG_MODE_SYSTEM) {
  48. uart1_put('\r');
  49. uart1_put('\n');
  50. }
  51. return ret_len;
  52. }
  53. /* Compare sentence header string */
  54. BYTE gp_comp (const char *str1, __flash const char *str2)
  55. {
  56. char c;
  57. do {
  58. c = pgm_read_byte(str2++);
  59. } while (c && c == *str1++);
  60. return c;
  61. }
  62. /* Per-epoch GGA/GSA/logged tracking for nmea_epoch_complete() - partial parse
  63. * state, not global "fully cooked" data, so kept local to this file rather
  64. * than on System. */
  65. static struct {
  66. unsigned char gga_ok : 1;
  67. unsigned char gsa_ok : 1;
  68. unsigned char logged : 1;
  69. } epoch;
  70. #define FIELD_BUF_LEN 32
  71. /* Get a column item */
  72. static
  73. const char* gp_col ( /* Returns pointer to the item (returns a NULL when not found) */
  74. const char* buf, /* Pointer to the sentence */
  75. BYTE col /* Column number (0 is the 1st item) */
  76. ) {
  77. BYTE c;
  78. static char field_buf[FIELD_BUF_LEN];
  79. unsigned char length = 0;
  80. while (col) {
  81. do {
  82. c = *buf++;
  83. if (c <= ' ') return NULL;
  84. } while (c != ',');
  85. col--;
  86. }
  87. while (*buf && *buf != ',' && length < FIELD_BUF_LEN-1) {
  88. field_buf[length++] = *buf++;
  89. }
  90. field_buf[length] = '\0';
  91. return field_buf;
  92. }
  93. unsigned int gp_val(const char *db, unsigned char count) {
  94. unsigned int out = 0;
  95. while (count--) {
  96. unsigned char n;
  97. n = *db - '0';
  98. if (n >= 10)
  99. return 0;
  100. out *= 10;
  101. out += n;
  102. db++;
  103. }
  104. return out;
  105. }
  106. #define SPEED_SAMPLE_CAP_MULT 3 /* cap any single sample at this multiple of the running average... */
  107. #define SPEED_SAMPLE_CAP_MIN_SAMPLES 60 /* ...but only once the average actually means something, ... */
  108. #define SPEED_SAMPLE_CAP_FLOOR_X100 2000 /* ...and never cap below this (20 km/h): the cap is a backstop against
  109. * a single wild outlier, not a speed limit - persistence (below) is what
  110. * lets a genuinely sustained fast segment (car/train) through. */
  111. #define SPEED_SAMPLE_HIGH_HISTORY_LEN 7 /* consider this many of the most recent samples... */
  112. #define SPEED_SAMPLE_HIGH_HISTORY_MASK ((1U << SPEED_SAMPLE_HIGH_HISTORY_LEN) - 1)
  113. #define SPEED_SAMPLE_HIGH_MIN_COUNT 5 /* ...and once this many of them were above the cap, stop clamping:
  114. * a single spike (e.g. the 200 km/h one-second glitch seen on a real
  115. * hike) never reaches this, but a sustained fast segment does within a
  116. * few seconds. */
  117. /* Bit history of "was this raw sample above the cap" for the last
  118. * SPEED_SAMPLE_HIGH_HISTORY_LEN samples (bit 0: most recent). Static file
  119. * scope, not part of System - it's working state for the cap logic only,
  120. * nothing else needs it. */
  121. static unsigned char speed_sample_high_history;
  122. static void speed_sample_process(double speed_kmh) {
  123. unsigned char paused = tracking_is_paused();
  124. unsigned char fix_trustworthy = gps_fix_trustworthy();
  125. System.speed = speed_kmh+0.5;
  126. /* Average speed: fixed-point accumulation of instantaneous samples,
  127. * excluding time spent paused, instead of dividing distance by time. */
  128. if (!paused && fix_trustworthy) {
  129. unsigned long int sample_x100 = (unsigned long int)(speed_kmh*100.0 + 0.5);
  130. if (System.speed_sample_count >= SPEED_SAMPLE_CAP_MIN_SAMPLES) {
  131. unsigned long int avg_x100 = System.speed_accum_x100 / System.speed_sample_count;
  132. unsigned long int cap_x100 = avg_x100 * SPEED_SAMPLE_CAP_MULT;
  133. unsigned char is_high;
  134. if (cap_x100 < SPEED_SAMPLE_CAP_FLOOR_X100)
  135. cap_x100 = SPEED_SAMPLE_CAP_FLOOR_X100;
  136. is_high = sample_x100 > cap_x100;
  137. speed_sample_high_history = (speed_sample_high_history << 1) | is_high;
  138. if (is_high && __builtin_popcount(speed_sample_high_history & SPEED_SAMPLE_HIGH_HISTORY_MASK) < SPEED_SAMPLE_HIGH_MIN_COUNT)
  139. sample_x100 = cap_x100; /* not (yet) persistent: clamp as a backstop against a lone spike */
  140. }
  141. System.speed_accum_x100 += sample_x100;
  142. System.speed_sample_count++;
  143. }
  144. }
  145. static time_t gp_rmc_parse(const char *str) {
  146. const char *p;
  147. struct tm tmc;
  148. double speed_knots;
  149. epoch.gga_ok = 0;
  150. epoch.gsa_ok = 0;
  151. epoch.logged = 0;
  152. p = gp_col(str, 1); /* Get h:m:s */
  153. if (!p)
  154. return 0;
  155. tmc.tm_hour = gp_val(p, 2);
  156. tmc.tm_min = gp_val(p+2, 2);
  157. tmc.tm_sec = gp_val(p+4, 2);
  158. p = gp_col(str, 9); /* Get y:m:d */
  159. if (!p)
  160. return 0;
  161. tmc.tm_mday = gp_val(p, 2);
  162. tmc.tm_mon = gp_val(p+2, 2) - 1;
  163. tmc.tm_year = gp_val(p+4, 2) + 100;
  164. utc = mktime(&tmc); /* Check time validity */
  165. if (utc == -1)
  166. return 0;
  167. p = gp_col(str, 2); /* Get status */
  168. if (!p || *p != 'A') {
  169. System.location_valid = LOC_INVALID;
  170. FLAGS &= ~F_GPSOK;
  171. return 0; /* Return 0 even is time is valid (comes from module's internal RTC) */
  172. }
  173. FLAGS |= F_GPSOK;
  174. /* Speed over ground, from this same sentence (and the same epoch whose
  175. * status field was just checked above), in knots - converted to km/h.
  176. * Not VTG: some receivers omit VTG or send a shorter form of it, and RMC
  177. * is the one sentence essentially every NMEA GNSS module emits. */
  178. p = gp_col(str, 7);
  179. if (p && *p) {
  180. xatof(&p, &speed_knots);
  181. speed_sample_process(speed_knots * 1.852);
  182. }
  183. return utc;
  184. }
  185. /* A fix is only trusted (for acceptance into gpx_process_point(), the speed
  186. * average, and the auto-pause speed-based unpause check) when it is valid,
  187. * of a known type, and reasonably accurate: a single sample from a poor fix
  188. * (e.g. HDOP 16 with only a few satellites) would otherwise distort the
  189. * whole-session average, corrupt the track with a wild position/altitude
  190. * outlier, or - worse - talk the auto-pause logic into resuming a stationary
  191. * logger. */
  192. #define FIX_QUALITY_HDOP_MAX_X100 500 /* a fix is untrustworthy once HDOP > 5.00 */
  193. static void gp_gga_parse(const char *str) {
  194. const char *p;
  195. double tmp, hdop;
  196. unsigned int hdop_x100;
  197. p = gp_col(str, 7); /* satellites used */
  198. if (!p)
  199. return;
  200. System.satellites_used = atoi(p);
  201. epoch.gga_ok = 1;
  202. if (System.satellites_used >= System.conf.min_sats) {
  203. System.sat_count_low = 0;
  204. } else {
  205. System.sat_count_low = 1;
  206. }
  207. /* HDOP, from this same sentence: GSA (which also reports HDOP, into
  208. * System.hdop_x100) is parsed after GGA within the epoch, so its value
  209. * is still last epoch's here - read GGA's own HDOP field instead. */
  210. p = gp_col(str, 8);
  211. if (!p)
  212. return;
  213. xatof(&p, &hdop);
  214. hdop_x100 = (unsigned int)(hdop*100.0 + 0.5);
  215. /* check validity */
  216. p = gp_col(str, 6);
  217. if (!p)
  218. return;
  219. if (*p == '0') {
  220. System.location_valid = LOC_INVALID;
  221. return;
  222. }
  223. /* Reject (rather than just not-promote) on a low satellite count, poor
  224. * HDOP, or anything short of a 3D fix, so a bad fix can't coast on
  225. * LOC_VALID left over from the last good epoch - otherwise a
  226. * low-quality point (wrong position and/or altitude) would still be
  227. * accepted downstream. A 2D fix in particular can't solve for altitude
  228. * at all (only 3 satellites: 2 horizontal DOF + time, no vertical) and
  229. * reports a held-over or drifting one instead - seen after a real
  230. * reacquisition, where a 2D fix reported an altitude a few hundred
  231. * meters off before enough satellites came in for a real 3D fix.
  232. * System.fix_type is set by GSA, parsed after GGA within the epoch, so
  233. * it's last epoch's value here - same staleness as HDOP above, but a
  234. * bad 2D fix reliably spans several consecutive epochs, so a one-epoch
  235. * lag isn't a practical gap here (unlike a true point fix). */
  236. if (!System.sat_count_low && hdop_x100 <= FIX_QUALITY_HDOP_MAX_X100
  237. && System.fix_type == FIX_TYPE_3D)
  238. System.location_valid = LOC_VALID_NEW;
  239. else
  240. System.location_valid = LOC_INVALID;
  241. /* parse location */
  242. p = gp_col(str, 2); /* latitude */
  243. if (!p)
  244. return;
  245. location.lat = gp_val(p, 2); /* degrees */
  246. p += 2;
  247. xatof(&p, &tmp); /* minutes */
  248. tmp /= 60; /* convert minutes to degrees */
  249. location.lat += tmp;
  250. p = gp_col(str, 3); /* N/S */
  251. if (!p)
  252. return;
  253. if (*p != 'N')
  254. location.lat = -location.lat;
  255. p = gp_col(str, 4); /* longitude */
  256. if (!p)
  257. return;
  258. location.lon = gp_val(p, 3); /* degrees */
  259. p += 3;
  260. xatof(&p, &tmp); /* minutes */
  261. tmp /= 60; /* convert minutes to degrees */
  262. location.lon += tmp;
  263. p = gp_col(str, 5); /* E/W */
  264. if (!p)
  265. return;
  266. if (*p != 'E')
  267. location.lon = -location.lon;
  268. p = gp_col(str, 6); /* fix type */
  269. if (!p)
  270. return;
  271. if (*p == '2')
  272. System.dgps = 1;
  273. else
  274. System.dgps = 0;
  275. p = gp_col(str, 9); /* MSL altitude */
  276. if (!p)
  277. return;
  278. xatof(&p, &tmp);
  279. location.alt = tmp;
  280. location.time = utc; /* parsed from RMC */
  281. }
  282. unsigned char gps_fix_trustworthy(void) {
  283. return (FLAGS & F_GPSOK) && System.fix_type != FIX_TYPE_UNKNOWN
  284. && System.hdop_x100 <= FIX_QUALITY_HDOP_MAX_X100;
  285. }
  286. static void gp_gsa_parse(const char *str) {
  287. const char *p;
  288. double hdop;
  289. p = gp_col(str, 2); /* fix type: 1 no fix, 2 2D, 3 3D */
  290. if (p && *p == '2')
  291. System.fix_type = FIX_TYPE_2D;
  292. else if (p && *p == '3')
  293. System.fix_type = FIX_TYPE_3D;
  294. else
  295. System.fix_type = FIX_TYPE_UNKNOWN;
  296. p = gp_col(str, 16); /* HDOP */
  297. if (p && *p) {
  298. xatof(&p, &hdop);
  299. System.hdop_x100 = (unsigned int)(hdop*100.0 + 0.5);
  300. }
  301. epoch.gsa_ok = 1;
  302. }
  303. /* Returns 1 exactly once per epoch (the moment both GGA and GSA have been
  304. * parsed for it, whichever arrives second), so callers that need this
  305. * epoch's satellite count and HDOP together (e.g. logging a fix event) never
  306. * see a stale value left over from a previous epoch or an earlier line -
  307. * regardless of which order the receiver sends GGA/GSA in. */
  308. unsigned char nmea_epoch_complete(void) {
  309. if (epoch.gga_ok && epoch.gsa_ok && !epoch.logged) {
  310. epoch.logged = 1;
  311. return 1;
  312. }
  313. return 0;
  314. }
  315. /*$PMTK355*31<CR><LF>
  316. Return $PMTK001,355,3,1,0,0*2E “$PMTK001,355,3,GLON_Enable,BEIDOU_Enable,GALILEO_Enable”
  317. The GLONASS search mode is enabled. */
  318. static void pmtk001_parse(const char *str) {
  319. const char *p;
  320. /* check validity */
  321. p = gp_col(str, 1);
  322. if (strcmp_P(p, PSTR("355"))) /* not the PMTK355 reply */
  323. return;
  324. p = gp_col(str, 2);
  325. if (*p == '0' || *p == '1') { /* invalid / unsupported */
  326. System.gps_only = 1;
  327. xputs_P(PSTR("GPS only\r\n"));
  328. } else {
  329. System.gps_only = 0;
  330. xputs_P(PSTR("Multi GNSS\r\n"));
  331. }
  332. gps_initialize();
  333. }
  334. unsigned char nmea_checksum(const char *str) {
  335. unsigned char cs = 0;
  336. while (*str && *str != '*') {
  337. cs ^= *str++; /* NMEA checksum is quite primitive, but still should catch simple bitstream errors or truncated lines */
  338. }
  339. return cs;
  340. }
  341. time_t gps_parse(const char *str) { /* Get all required data from NMEA sentences */
  342. signed int len = strlen(str)-2; /* remove final \r\n */
  343. const char *checksum;
  344. unsigned char calc_checksum, inc_checksum;
  345. signed int i;
  346. char c;
  347. if (len < 4)
  348. return 0; /* each message must contain $.*xx where . is one or more actual data characters and xx is the checksum */
  349. for (i = len-1; i && i >= (len-2); i--) { /* find checksum */
  350. if (str[i] == '*')
  351. break;
  352. }
  353. checksum = str+i+1; /* skip * character */
  354. inc_checksum = 0; /* parse hex string */
  355. while (*checksum && isalnum(*checksum)) {
  356. inc_checksum *= 16;
  357. c = *checksum++;
  358. if (c >= '0' && c <= '9') {
  359. inc_checksum += c - '0';
  360. } else if (c >= 'a' && c <= 'f') {
  361. inc_checksum += c - 'a' + 10;
  362. } else if (c >= 'A' && c <= 'F') {
  363. inc_checksum += c - 'A' + 10;
  364. } else {
  365. xputs_P(PSTR("Invalid NMEA: malformed checksum\r\n"));
  366. return 0; /* invalid checksum character */
  367. }
  368. }
  369. str++; /* drop initial $ */
  370. calc_checksum = nmea_checksum(str);
  371. if (inc_checksum != calc_checksum) {
  372. xputs_P(PSTR("Invalid NMEA checksum received\r\n"));
  373. return 0;
  374. }
  375. if (!gp_comp(str, PSTR("GPRMC")) || !gp_comp(str, PSTR("GNRMC")) || !gp_comp(str, PSTR("BDRMC")) || !gp_comp(str, PSTR("GARMC"))) {
  376. return gp_rmc_parse(str);
  377. }
  378. if (!gp_comp(str, PSTR("GPGGA")) || !gp_comp(str, PSTR("GNGGA")) || !gp_comp(str, PSTR("BDGGA")) || !gp_comp(str, PSTR("GAGGA"))) {
  379. gp_gga_parse(str);
  380. return 0;
  381. }
  382. if (!gp_comp(str, PSTR("GPGSA")) || !gp_comp(str, PSTR("GNGSA")) || !gp_comp(str, PSTR("BDGSA")) || !gp_comp(str, PSTR("GAGSA"))) {
  383. gp_gsa_parse(str);
  384. return 0;
  385. }
  386. if (!System.gps_initialized && !gp_comp(str, PSTR("PMTK011"))) {
  387. gps_initialize();
  388. return 0;
  389. }
  390. if (!gp_comp(str, PSTR("PMTK001"))) {
  391. pmtk001_parse(str);
  392. return 0;
  393. }
  394. if (!gp_comp(str, PSTR("PMTK"))) {
  395. /* Any other PMTK reply (e.g. PMTK705, the firmware release query
  396. * response) - log it verbatim, no need for a dedicated parser. */
  397. log_prefix();
  398. xputs_P(PSTR("RX "));
  399. xputs(str);
  400. return 0;
  401. }
  402. return 0;
  403. }
  404. void uart0_put_wrap(int c) {
  405. uart0_put((char)c);
  406. }
  407. /* Sends the SBAS-enable/disable and GNSS-mode commands for the currently
  408. * configured settings. Used both as the second step of gps_initialize()'s
  409. * boot sequence, and directly as the settings menu's .changed callback for
  410. * the SBAS/GNSS-mode entries - the latter must apply the just-changed
  411. * setting immediately, regardless of System.gps_initialized, rather than
  412. * going through gps_initialize()'s boot state machine (which would instead
  413. * re-send the GNSS-mode query if called before the module's PMTK011 boot
  414. * line has been seen). */
  415. void gps_settings_changed(void) {
  416. /*
  417. * PMTK353: set gnss search mode (GPS/Galileo/Glonass/Beidou)
  418. * PMTK313: enable SBAS
  419. */
  420. if (get_flag(CONFFLAG_ENABLE_SBAS)) {
  421. xfprintf(uart0_put_wrap, PSTR("$PMTK313,1*2E\r\n"));
  422. xputs_P(PSTR("SBAS enable sent\r\n"));
  423. } else {
  424. xfprintf(uart0_put_wrap, PSTR("$PMTK313,0*2F\r\n"));
  425. xputs_P(PSTR("SBAS disable sent\r\n"));
  426. }
  427. if (!System.gps_only) {
  428. switch (System.conf.gnss_mode) {
  429. default:
  430. case GNSS_MODE_GPS_GLONASS_GALILEO:
  431. xfprintf(uart0_put_wrap, PSTR("$PMTK353,1,1,1,0,0*2A\r\n"));
  432. break;
  433. case GNSS_MODE_GPS:
  434. xfprintf(uart0_put_wrap, PSTR("$PMTK353,1,0,0,0,0*2A\r\n"));
  435. break;
  436. case GNSS_MODE_GPS_GALILEO:
  437. xfprintf(uart0_put_wrap, PSTR("$PMTK353,1,0,1,0,0*2B\r\n"));
  438. break;
  439. case GNSS_MODE_GALILEO:
  440. xfprintf(uart0_put_wrap, PSTR("$PMTK353,0,0,1,0,0*2A\r\n"));
  441. break;
  442. case GNSS_MODE_GPS_BEIDOU:
  443. xfprintf(uart0_put_wrap, PSTR("$PMTK353,1,0,0,0,1*2B\r\n"));
  444. break;
  445. case GNSS_MODE_BEIDOU:
  446. xfprintf(uart0_put_wrap, PSTR("$PMTK353,0,0,0,0,1*2A\r\n"));
  447. break;
  448. }
  449. xputs_P(PSTR("GNSS mode setting sent\r\n"));
  450. } else {
  451. System.conf.gnss_mode = GNSS_MODE_GPS;
  452. }
  453. }
  454. void gps_initialize(void) {
  455. /*
  456. * PMTK355: query gnss search mode (will fail if only GPS is supported)
  457. * PMTK605: query firmware release; the reply (PMTK705) is logged
  458. * verbatim by the generic PMTK passthrough in gps_parse()
  459. */
  460. switch (System.gps_initialized) {
  461. case GPS_INIT_NOT_INITIALIZED:
  462. xfprintf(uart0_put_wrap, PSTR("$PMTK355*31\r\n"));
  463. xputs_P(PSTR("GNSS mode query sent\r\n"));
  464. xfprintf(uart0_put_wrap, PSTR("$PMTK605*31\r\n"));
  465. System.gps_initialized = GPS_INIT_QUERY_SENT;
  466. break;
  467. case GPS_INIT_QUERY_SENT:
  468. gps_settings_changed();
  469. break;
  470. default:
  471. break;
  472. }
  473. }
  474. void check_min_sat_limit(void) {
  475. if (System.conf.min_sats > 6 && (System.conf.gnss_mode == GNSS_MODE_GPS || System.conf.gnss_mode == GNSS_MODE_GALILEO || System.conf.gnss_mode == GNSS_MODE_BEIDOU)) {
  476. /* by geometry, max visible number is 6..12 */
  477. System.conf.min_sats = 6;
  478. }
  479. }