minmea.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Copyright © 2014 Kosma Moczek <kosma@cloudyourcar.com>
  3. * This program is free software. It comes without any warranty, to the extent
  4. * permitted by applicable law. You can redistribute it and/or modify it under
  5. * the terms of the Do What The Fuck You Want To Public License, Version 2, as
  6. * published by Sam Hocevar. See the COPYING file for more details.
  7. */
  8. #ifndef MINMEA_H
  9. #define MINMEA_H
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. #include <ctype.h>
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <time.h>
  17. #include <math.h>
  18. #ifdef MINMEA_INCLUDE_COMPAT
  19. #include <minmea_compat.h>
  20. #endif
  21. #ifndef MINMEA_MAX_SENTENCE_LENGTH
  22. #define MINMEA_MAX_SENTENCE_LENGTH 80
  23. #endif
  24. #define FRAME_TXT_MAX_LEN (79)
  25. enum minmea_sentence_id {
  26. MINMEA_INVALID = -1,
  27. MINMEA_UNKNOWN = 0,
  28. MINMEA_SENTENCE_GBS,
  29. MINMEA_SENTENCE_GGA,
  30. MINMEA_SENTENCE_GLL,
  31. MINMEA_SENTENCE_GSA,
  32. MINMEA_SENTENCE_GST,
  33. MINMEA_SENTENCE_GSV,
  34. MINMEA_SENTENCE_RMC,
  35. MINMEA_SENTENCE_VTG,
  36. MINMEA_SENTENCE_ZDA,
  37. MINMEA_SENTENCE_TXT,
  38. MINMEA_SENTENCE_MAX_ID
  39. };
  40. struct minmea_float {
  41. int_least32_t value;
  42. int_least32_t scale;
  43. };
  44. struct minmea_date {
  45. int day;
  46. int month;
  47. int year;
  48. };
  49. struct minmea_time {
  50. int hours;
  51. int minutes;
  52. int seconds;
  53. int microseconds;
  54. };
  55. struct minmea_sentence_gbs {
  56. struct minmea_time time;
  57. struct minmea_float err_latitude;
  58. struct minmea_float err_longitude;
  59. struct minmea_float err_altitude;
  60. int svid;
  61. struct minmea_float prob;
  62. struct minmea_float bias;
  63. struct minmea_float stddev;
  64. };
  65. struct minmea_sentence_rmc {
  66. struct minmea_time time;
  67. bool valid;
  68. struct minmea_float latitude;
  69. struct minmea_float longitude;
  70. struct minmea_float speed;
  71. struct minmea_float course;
  72. struct minmea_date date;
  73. struct minmea_float variation;
  74. };
  75. struct minmea_sentence_gga {
  76. struct minmea_time time;
  77. struct minmea_float latitude;
  78. struct minmea_float longitude;
  79. int fix_quality;
  80. int satellites_tracked;
  81. struct minmea_float hdop;
  82. struct minmea_float altitude; char altitude_units;
  83. struct minmea_float height; char height_units;
  84. struct minmea_float dgps_age;
  85. };
  86. enum minmea_gll_status {
  87. MINMEA_GLL_STATUS_DATA_VALID = 'A',
  88. MINMEA_GLL_STATUS_DATA_NOT_VALID = 'V',
  89. };
  90. // FAA mode added to some fields in NMEA 2.3.
  91. enum minmea_faa_mode {
  92. MINMEA_FAA_MODE_AUTONOMOUS = 'A',
  93. MINMEA_FAA_MODE_DIFFERENTIAL = 'D',
  94. MINMEA_FAA_MODE_ESTIMATED = 'E',
  95. MINMEA_FAA_MODE_MANUAL = 'M',
  96. MINMEA_FAA_MODE_SIMULATED = 'S',
  97. MINMEA_FAA_MODE_NOT_VALID = 'N',
  98. MINMEA_FAA_MODE_PRECISE = 'P',
  99. };
  100. struct minmea_sentence_gll {
  101. struct minmea_float latitude;
  102. struct minmea_float longitude;
  103. struct minmea_time time;
  104. char status;
  105. char mode;
  106. };
  107. struct minmea_sentence_gst {
  108. struct minmea_time time;
  109. struct minmea_float rms_deviation;
  110. struct minmea_float semi_major_deviation;
  111. struct minmea_float semi_minor_deviation;
  112. struct minmea_float semi_major_orientation;
  113. struct minmea_float latitude_error_deviation;
  114. struct minmea_float longitude_error_deviation;
  115. struct minmea_float altitude_error_deviation;
  116. };
  117. enum minmea_gsa_mode {
  118. MINMEA_GPGSA_MODE_AUTO = 'A',
  119. MINMEA_GPGSA_MODE_FORCED = 'M',
  120. };
  121. enum minmea_gsa_fix_type {
  122. MINMEA_GPGSA_FIX_NONE = 1,
  123. MINMEA_GPGSA_FIX_2D = 2,
  124. MINMEA_GPGSA_FIX_3D = 3,
  125. };
  126. struct minmea_sentence_gsa {
  127. char mode;
  128. int fix_type;
  129. int sats[12];
  130. struct minmea_float pdop;
  131. struct minmea_float hdop;
  132. struct minmea_float vdop;
  133. };
  134. struct minmea_sat_info {
  135. int nr;
  136. int elevation;
  137. int azimuth;
  138. int snr;
  139. };
  140. struct minmea_sentence_gsv {
  141. int total_msgs;
  142. int msg_nr;
  143. int total_sats;
  144. struct minmea_sat_info sats[4];
  145. };
  146. struct minmea_sentence_vtg {
  147. struct minmea_float true_track_degrees;
  148. struct minmea_float magnetic_track_degrees;
  149. struct minmea_float speed_knots;
  150. struct minmea_float speed_kph;
  151. enum minmea_faa_mode faa_mode;
  152. };
  153. struct minmea_sentence_zda {
  154. struct minmea_time time;
  155. struct minmea_date date;
  156. int hour_offset;
  157. int minute_offset;
  158. };
  159. struct minmea_sentence_txt {
  160. int count;
  161. int index;
  162. char txt[FRAME_TXT_MAX_LEN + 1];
  163. };
  164. /**
  165. * Calculate raw sentence checksum. Does not check sentence integrity.
  166. */
  167. uint8_t minmea_checksum(const char *sentence);
  168. /**
  169. * Check sentence validity and checksum. Returns true for valid sentences.
  170. */
  171. bool minmea_check(const char *sentence, bool strict);
  172. /**
  173. * Determine talker identifier.
  174. */
  175. bool minmea_talker_id(char talker[3], const char *sentence);
  176. /**
  177. * Determine sentence identifier.
  178. */
  179. enum minmea_sentence_id minmea_sentence_id(const char *sentence, bool strict);
  180. /**
  181. * Scanf-like processor for NMEA sentences. Supports the following formats:
  182. * c - single character (char *)
  183. * d - direction, returned as 1/-1, default 0 (int *)
  184. * f - fractional, returned as value + scale (struct minmea_float *)
  185. * i - decimal, default zero (int *)
  186. * s - string (char *)
  187. * t - talker identifier and type (char *)
  188. * D - date (struct minmea_date *)
  189. * T - time stamp (struct minmea_time *)
  190. * _ - ignore this field
  191. * ; - following fields are optional
  192. * Returns true on success. See library source code for details.
  193. */
  194. bool minmea_scan(const char *sentence, const char *format, ...);
  195. /*
  196. * Parse a specific type of sentence. Return true on success.
  197. */
  198. bool minmea_parse_gbs(struct minmea_sentence_gbs *frame, const char *sentence);
  199. bool minmea_parse_rmc(struct minmea_sentence_rmc *frame, const char *sentence);
  200. bool minmea_parse_gga(struct minmea_sentence_gga *frame, const char *sentence);
  201. bool minmea_parse_gsa(struct minmea_sentence_gsa *frame, const char *sentence);
  202. bool minmea_parse_gll(struct minmea_sentence_gll *frame, const char *sentence);
  203. bool minmea_parse_gst(struct minmea_sentence_gst *frame, const char *sentence);
  204. bool minmea_parse_gsv(struct minmea_sentence_gsv *frame, const char *sentence);
  205. bool minmea_parse_vtg(struct minmea_sentence_vtg *frame, const char *sentence);
  206. bool minmea_parse_zda(struct minmea_sentence_zda *frame, const char *sentence);
  207. bool minmea_parse_txt(struct minmea_sentence_txt *frame, const char *sentence);
  208. /**
  209. * Convert GPS UTC date/time representation to a UNIX calendar time.
  210. */
  211. int minmea_getdatetime(struct tm *tm, const struct minmea_date *date, const struct minmea_time *time_);
  212. /**
  213. * Convert GPS UTC date/time representation to a UNIX timestamp.
  214. */
  215. int minmea_gettime(struct timespec *ts, const struct minmea_date *date, const struct minmea_time *time_);
  216. /**
  217. * Rescale a fixed-point value to a different scale. Rounds towards zero.
  218. */
  219. static inline int_least32_t minmea_rescale(const struct minmea_float *f, int_least32_t new_scale)
  220. {
  221. if (f->scale == 0)
  222. return 0;
  223. if (f->scale == new_scale)
  224. return f->value;
  225. if (f->scale > new_scale)
  226. return (f->value + ((f->value > 0) - (f->value < 0)) * f->scale/new_scale/2) / (f->scale/new_scale);
  227. else
  228. return f->value * (new_scale/f->scale);
  229. }
  230. /**
  231. * Convert a fixed-point value to a floating-point value.
  232. * Returns NaN for "unknown" values.
  233. */
  234. static inline float minmea_tofloat(const struct minmea_float *f)
  235. {
  236. if (f->scale == 0)
  237. return 0;
  238. return (float) f->value / (float) f->scale;
  239. }
  240. /**
  241. * Convert a raw coordinate to a floating point DD.DDD... value.
  242. * Returns NaN for "unknown" values.
  243. */
  244. static inline float minmea_tocoord(const struct minmea_float *f)
  245. {
  246. if (f->scale == 0)
  247. return 0;
  248. if (f->scale > (INT_LEAST32_MAX / 100))
  249. return 0;
  250. if (f->scale < (INT_LEAST32_MIN / 100))
  251. return 0;
  252. int_least32_t degrees = f->value / (f->scale * 100);
  253. int_least32_t minutes = f->value % (f->scale * 100);
  254. return (float) degrees + (float) minutes / (60 * f->scale);
  255. }
  256. /**
  257. * Convert a raw coordinate to a floating point DD.DDD... value.
  258. * Returns NaN for "unknown" values.
  259. */
  260. static inline uint32_t minmea_tocoord2(const struct minmea_float *f)
  261. {
  262. if (f->scale == 0)
  263. return 0;
  264. if (f->scale > (INT_LEAST32_MAX / 100))
  265. return 0;
  266. if (f->scale < (INT_LEAST32_MIN / 100))
  267. return 0;
  268. int_least32_t degrees = f->value / (f->scale * 100);
  269. int_least32_t minutes = f->value % (f->scale * 100);
  270. uint32_t ret = degrees * 10000000 + (uint32_t)(((float) minutes / (60 * f->scale)) * 10000000);
  271. return ret;
  272. }
  273. /**
  274. * Check whether a character belongs to the set of characters allowed in a
  275. * sentence data field.
  276. */
  277. static inline bool minmea_isfield(char c) {
  278. return isprint((unsigned char) c) && c != ',' && c != '*';
  279. }
  280. // 扩展
  281. #define RECV_BUFF_SIZE (2048)
  282. #define FRAME_GSA_MAX (6)
  283. #define FRAME_GSV_MAX (5)
  284. #define FRAME_TXT_MAX (80)
  285. int luat_libgnss_init(int clear);
  286. int luat_libgnss_parse_data(const char* data, size_t len);
  287. int luat_libgnss_parse_nmea(const char* line);
  288. void luat_libgnss_uart_recv_cb(int uart_id, uint32_t data_len);
  289. int luat_libgnss_state_onchanged(int state);
  290. int luat_libgnss_on_rawdata(const char* data, size_t len);
  291. enum GNSS_STATE {
  292. GNSS_STATE_INIT = 0,
  293. GNSS_STATE_FIXED,
  294. GNSS_STATE_LOSE,
  295. GNSS_STATE_OPEN,
  296. GNSS_STATE_CLOSE
  297. };
  298. typedef struct luat_libgnss
  299. {
  300. uint32_t fix_at_ticks;
  301. // int lua_ref;
  302. struct minmea_sentence_rmc frame_rmc;
  303. char gga[MINMEA_MAX_SENTENCE_LENGTH + 4];
  304. char gll[MINMEA_MAX_SENTENCE_LENGTH + 4];
  305. char gst[MINMEA_MAX_SENTENCE_LENGTH + 4];
  306. char vtg[MINMEA_MAX_SENTENCE_LENGTH + 4];
  307. char zda[MINMEA_MAX_SENTENCE_LENGTH + 4];
  308. // struct minmea_sentence_gga frame_gga;
  309. // struct minmea_sentence_gll frame_gll;
  310. // struct minmea_sentence_gst frame_gst;
  311. struct minmea_sentence_gsv frame_gsv_gp[FRAME_GSV_MAX];
  312. struct minmea_sentence_gsv frame_gsv_gb[FRAME_GSV_MAX];
  313. // struct minmea_sentence_gsv frame_gsv_gl[FRAME_GSV_MAX];
  314. // struct minmea_sentence_gsv frame_gsv_ga[FRAME_GSV_MAX];
  315. // struct minmea_sentence_vtg frame_vtg;
  316. struct minmea_sentence_gsa frame_gsa[FRAME_GSA_MAX];
  317. // struct minmea_sentence_zda frame_zda;
  318. struct minmea_sentence_txt txt;
  319. uint8_t debug;
  320. uint8_t rtc_auto;
  321. } luat_libgnss_t;
  322. typedef struct luat_libgnss_tmp
  323. {
  324. struct minmea_sentence_rmc frame_rmc;
  325. // struct minmea_sentence_gga frame_gga;
  326. // struct minmea_sentence_gll frame_gll;
  327. // struct minmea_sentence_gst frame_gst;
  328. struct minmea_sentence_gsv frame_gsv;
  329. // struct minmea_sentence_vtg frame_vtg;
  330. struct minmea_sentence_gsa frame_gsa;
  331. // struct minmea_sentence_zda frame_zda;
  332. } luat_libgnss_tmp_t;
  333. #ifdef __cplusplus
  334. }
  335. #endif
  336. #endif /* MINMEA_H */
  337. /* vim: set ts=4 sw=4 et: */