luat_lib_libgnss.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. @module libgnss
  3. @summary NMEA数据处理
  4. @version 1.0
  5. @data 2020.07.03
  6. */
  7. #include "luat_base.h"
  8. #include "luat_msgbus.h"
  9. #include "luat_malloc.h"
  10. #define LUAT_LOG_TAG "luat.gnss"
  11. #include "luat_log.h"
  12. #include "minmea.h"
  13. struct minmea_sentence_rmc frame_rmc = {0};
  14. struct minmea_sentence_gga frame_gga = {0};
  15. struct minmea_sentence_gsv frame_gsv = {0};
  16. static int parse_nmea(const char* line) {
  17. // $GNRMC,080313.00,A,2324.40756,N,11313.86184,E,0.284,,010720,,,A*68
  18. // LLOGD(">> %s", line);
  19. switch (minmea_sentence_id(line, false)) {
  20. case MINMEA_SENTENCE_RMC: {
  21. //struct minmea_sentence_rmc frame;
  22. if (minmea_parse_rmc(&frame_rmc, line)) {
  23. // LLOGD("$RMC: raw coordinates and speed: (%d/%d,%d/%d) %d/%d",
  24. // frame_rmc.latitude.value, frame_rmc.latitude.scale,
  25. // frame_rmc.longitude.value, frame_rmc.longitude.scale,
  26. // frame_rmc.speed.value, frame_rmc.speed.scale);
  27. // LLOGD("$RMC fixed-point coordinates and speed scaled to three decimal places: (%d,%d) %d",
  28. // minmea_rescale(&frame_rmc.latitude, 1000),
  29. // minmea_rescale(&frame_rmc.longitude, 1000),
  30. // minmea_rescale(&frame_rmc.speed, 1000));
  31. // LLOGD("$RMC floating point degree coordinates and speed: (%f,%f) %f",
  32. // minmea_tocoord(&frame_rmc.latitude),
  33. // minmea_tocoord(&frame_rmc.longitude),
  34. // minmea_tofloat(&frame_rmc.speed));
  35. }
  36. } break;
  37. case MINMEA_SENTENCE_GGA: {
  38. //struct minmea_sentence_gga frame;
  39. if (minmea_parse_gga(&frame_gga, line)) {
  40. //LLOGD("$GGA: fix quality: %d", frame_gga.fix_quality);
  41. }
  42. } break;
  43. case MINMEA_SENTENCE_GSV: {
  44. if (minmea_parse_gsv(&frame_gsv, line)) {
  45. // LLOGD("$GSV: message %d of %d", frame_gsv.msg_nr, frame_gsv.total_msgs);
  46. // LLOGD("$GSV: sattelites in view: %d", frame_gsv.total_sats);
  47. // for (int i = 0; i < 4; i++)
  48. // LLOGD("$GSV: sat nr %d, elevation: %d, azimuth: %d, snr: %d dbm",
  49. // frame_gsv.sats[i].nr,
  50. // frame_gsv.sats[i].elevation,
  51. // frame_gsv.sats[i].azimuth,
  52. // frame_gsv.sats[i].snr);
  53. }
  54. } break;
  55. }
  56. return 0;
  57. }
  58. static int l_libgnss_parse(lua_State *L) {
  59. size_t len = 0;
  60. const char* str = luaL_checklstring(L, 1, &len);
  61. if (len == 0) {
  62. return 0;
  63. }
  64. char buff[85] = {0}; // nmea 最大长度82,含换行符
  65. char *ptr = (char*)str;
  66. size_t prev = 0;
  67. for (size_t i = 1; i < len; i++)
  68. {
  69. if (*(ptr + i) == 0x0A) {
  70. if (i - prev > 10 && i - prev < 82) {
  71. memcpy(buff, ptr + prev, i - prev - 1);
  72. if (buff[0] == '$') {
  73. buff[i - prev - 1] = 0; // 确保结束符存在
  74. parse_nmea((const char*)buff);
  75. }
  76. }
  77. i ++;
  78. prev = i;
  79. }
  80. }
  81. return 0;
  82. }
  83. static int l_libgnss_is_fix(lua_State *L) {
  84. lua_pushboolean(L, frame_rmc.valid);
  85. return 1;
  86. }
  87. static int l_libgnss_get_int_location(lua_State *L) {
  88. if (frame_rmc.valid) {
  89. lua_pushinteger(L, frame_rmc.latitude.value);
  90. lua_pushinteger(L, frame_rmc.longitude.value);
  91. lua_pushinteger(L, frame_rmc.speed.value);
  92. } else {
  93. lua_pushinteger(L, 0);
  94. lua_pushinteger(L, 0);
  95. lua_pushinteger(L, 0);
  96. }
  97. return 3;
  98. }
  99. static int l_libgnss_get_rmc(lua_State *L) {
  100. lua_createtable(L, 0, 12);
  101. lua_pushliteral(L, "valid");
  102. lua_pushboolean(L, frame_rmc.valid);
  103. lua_settable(L, -3);
  104. lua_pushliteral(L, "lat");
  105. lua_pushinteger(L, frame_rmc.latitude.value);
  106. lua_settable(L, -3);
  107. lua_pushliteral(L, "lng");
  108. lua_pushinteger(L, frame_rmc.longitude.value);
  109. lua_settable(L, -3);
  110. lua_pushliteral(L, "speed");
  111. lua_pushinteger(L, frame_rmc.speed.value);
  112. lua_settable(L, -3);
  113. lua_pushliteral(L, "course");
  114. lua_pushinteger(L, frame_rmc.course.value);
  115. lua_settable(L, -3);
  116. lua_pushliteral(L, "variation");
  117. lua_pushinteger(L, frame_rmc.variation.value);
  118. lua_settable(L, -3);
  119. lua_pushliteral(L, "year");
  120. lua_pushinteger(L, frame_rmc.date.year + 2000);
  121. lua_settable(L, -3);
  122. lua_pushliteral(L, "month");
  123. lua_pushinteger(L, frame_rmc.date.month);
  124. lua_settable(L, -3);
  125. lua_pushliteral(L, "day");
  126. lua_pushinteger(L, frame_rmc.date.day);
  127. lua_settable(L, -3);
  128. lua_pushliteral(L, "hour");
  129. lua_pushinteger(L, frame_rmc.time.hours);
  130. lua_settable(L, -3);
  131. lua_pushliteral(L, "min");
  132. lua_pushinteger(L, frame_rmc.time.minutes);
  133. lua_settable(L, -3);
  134. lua_pushliteral(L, "sec");
  135. lua_pushinteger(L, frame_rmc.time.seconds);
  136. lua_settable(L, -3);
  137. return 1;
  138. }
  139. #include "rotable.h"
  140. static const rotable_Reg reg_libgnss[] =
  141. {
  142. { "parse", l_libgnss_parse, 0},
  143. { "isFix", l_libgnss_is_fix, 0},
  144. { "getIntLocation", l_libgnss_get_int_location, 0},
  145. { "getRmc", l_libgnss_get_rmc, 0},
  146. { NULL, NULL , 0}
  147. };
  148. LUAMOD_API int luaopen_libgnss( lua_State *L ) {
  149. rotable_newlib(L, reg_libgnss);
  150. return 1;
  151. }