You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 lines
7.8 KiB

3 years ago
  1. #include "GPSAnalyse.h"
  2. GPSAnalyse::GPSAnalyse()
  3. {
  4. _GPS_Str.clear();
  5. }
  6. void GPSAnalyse::setSerialPtr(HardwareSerial &serial)
  7. {
  8. _serial = &serial;
  9. _xSemaphore = xSemaphoreCreateMutex();
  10. }
  11. GPSAnalyse::~GPSAnalyse()
  12. {
  13. }
  14. void GPSAnalyse::run(void *data)
  15. {
  16. GPSReadBuff = (char*)calloc(1024,sizeof(char));
  17. Serial.println("GPS Task");
  18. while (1)
  19. {
  20. if (_serial->available())
  21. {
  22. memset(GPSReadBuff, 1024, sizeof(char));
  23. _serial->readBytes(GPSReadBuff, _serial->available());
  24. _GPS_Str.concat(GPSReadBuff);
  25. xSemaphoreTake(_xSemaphore, portMAX_DELAY);
  26. Analyse();
  27. xSemaphoreGive(_xSemaphore);
  28. delay(1);
  29. }
  30. else
  31. {
  32. delay(5);
  33. }
  34. }
  35. }
  36. void GPSAnalyse::upDate()
  37. {
  38. xSemaphoreTake(_xSemaphore, portMAX_DELAY);
  39. memcpy(&s_GNRMC,&_s_GNRMC,sizeof(GNRMC_t));
  40. memcpy(&s_GNGAS,&_s_GNGAS,sizeof(GNGSA_t));
  41. memcpy(&s_GPGSV,&_s_GPGSV,sizeof(GPGSV_t));
  42. xSemaphoreGive(_xSemaphore);
  43. }
  44. void GPSAnalyse::AnaGPRMC(String str)
  45. {
  46. str = str.substring(str.indexOf("GNRMC"), str.length());
  47. if (str.indexOf('*') != -1)
  48. {
  49. int indexsub = str.indexOf('*');
  50. String sumstr = str.substring(indexsub + 1, str.length());
  51. str = str.substring(0, indexsub);
  52. //Serial.println(sumstr);
  53. }
  54. int index = 0, last_index = 0, pamindex = 1;
  55. while (str.indexOf(',', index) != -1)
  56. {
  57. index = str.indexOf(',', index + 1);
  58. last_index = str.indexOf(',', index + 1);
  59. //Serial.printf("index:%d,%d\n", index, last_index);
  60. if (index != -1)
  61. {
  62. last_index = (last_index == -1) ? str.length() : last_index;
  63. if ((last_index - index) > 1)
  64. {
  65. String pamstr = str.substring(index + 1, last_index);
  66. #ifdef DEBUG_GPS
  67. Serial.printf("%d:", pamindex);
  68. Serial.println(pamstr);
  69. #endif
  70. _s_GNRMC.pamstr[pamindex] = pamstr;
  71. }
  72. else
  73. {
  74. _s_GNRMC.pamstr[pamindex].clear();
  75. }
  76. pamindex++;
  77. }
  78. }
  79. _s_GNRMC.Utc = _s_GNRMC.pamstr[1];
  80. if (_s_GNRMC.pamstr[2].length() != 0)
  81. {
  82. _s_GNRMC.State = _s_GNRMC.pamstr[2].charAt(0);
  83. }
  84. {
  85. _s_GNRMC.Latitude = _s_GNRMC.pamstr[3].substring(0, 2).toDouble() + _s_GNRMC.pamstr[3].substring(2, 4).toDouble() / 60 + _s_GNRMC.pamstr[3].substring(5, 9).toDouble() / 600000;
  86. }
  87. //_s_GNRMC.Latitude = _s_GNRMC.pamstr[3].toDouble();
  88. _s_GNRMC.LatitudeMark = _s_GNRMC.pamstr[4].charAt(0);
  89. {
  90. _s_GNRMC.Longitude = _s_GNRMC.pamstr[5].substring(0, 3).toDouble() + _s_GNRMC.pamstr[5].substring(3, 5).toDouble() / 60 + _s_GNRMC.pamstr[5].substring(6, 10).toDouble() / 600000;
  91. }
  92. //_s_GNRMC.Longitude = _s_GNRMC.pamstr[5].toDouble();
  93. _s_GNRMC.LongitudeMark = _s_GNRMC.pamstr[6].charAt(0);
  94. _s_GNRMC.TrackSpeed = _s_GNRMC.pamstr[7].toFloat();
  95. _s_GNRMC.TrackAngle = _s_GNRMC.pamstr[8].toFloat();
  96. _s_GNRMC.Date = _s_GNRMC.pamstr[9];
  97. _s_GNRMC.Magnetic = _s_GNRMC.pamstr[10].toFloat();
  98. _s_GNRMC.Declination = _s_GNRMC.pamstr[11].charAt(0);
  99. _s_GNRMC.mode = _s_GNRMC.pamstr[12].charAt(0);
  100. }
  101. void GPSAnalyse::AnaGNGAS(String str)
  102. {
  103. str = str.substring(str.indexOf("GNGSA"), str.length());
  104. if (str.indexOf('*') != -1)
  105. {
  106. int indexsub = str.indexOf('*');
  107. String sumstr = str.substring(indexsub + 1, str.length());
  108. str = str.substring(0, indexsub);
  109. //Serial.println(sumstr);
  110. }
  111. int index = 0, last_index = 0, pamindex = 1;
  112. while (str.indexOf(',', index) != -1)
  113. {
  114. index = str.indexOf(',', index + 1);
  115. last_index = str.indexOf(',', index + 1);
  116. if (index != -1)
  117. {
  118. last_index = (last_index == -1) ? str.length() : last_index;
  119. if ((last_index - index) > 1)
  120. {
  121. String pamstr = str.substring(index + 1, last_index);
  122. #ifdef DEBUG_GPS
  123. Serial.printf("%d:", pamindex);
  124. Serial.println(pamstr);
  125. #endif
  126. _s_GNGAS.pamstr[pamindex] = pamstr;
  127. }
  128. else
  129. {
  130. _s_GNGAS.pamstr[pamindex].clear();
  131. }
  132. pamindex++;
  133. }
  134. }
  135. _s_GNGAS.mode2 = _s_GNGAS.pamstr[1].charAt(0);
  136. _s_GNGAS.mode1 = _s_GNGAS.pamstr[2].toInt();
  137. for (size_t i = 0; i < 12; i++)
  138. {
  139. _s_GNGAS.PINMap[i] = _s_GNGAS.pamstr[3 + i].toInt();
  140. }
  141. _s_GNGAS.PDOP = _s_GNGAS.pamstr[15].toFloat();
  142. _s_GNGAS.HDOP = _s_GNGAS.pamstr[16].toFloat();
  143. _s_GNGAS.VDOP = _s_GNGAS.pamstr[17].toFloat();
  144. }
  145. void GPSAnalyse::AnaGPGSV(String str)
  146. {
  147. //Serial.println(str);
  148. str = str.substring(str.indexOf("GPGSV"), str.length());
  149. if (str.indexOf('*') != -1)
  150. {
  151. int indexsub = str.indexOf('*');
  152. String sumstr = str.substring(indexsub + 1, str.length());
  153. str = str.substring(0, indexsub);
  154. //Serial.println(sumstr);
  155. }
  156. int index = 0, last_index = 0, pamindex = 1;
  157. while (str.indexOf(',', index) != -1)
  158. {
  159. index = str.indexOf(',', index + 1);
  160. last_index = str.indexOf(',', index + 1);
  161. if (index != -1)
  162. {
  163. last_index = (last_index == -1) ? str.length() : last_index;
  164. if ((last_index - index) > 1)
  165. {
  166. String pamstr = str.substring(index + 1, last_index);
  167. #ifdef DEBUG_GPS
  168. Serial.printf("%d:", pamindex);
  169. Serial.println(pamstr);
  170. #endif
  171. _s_GPGSV.pamstr[pamindex] = pamstr;
  172. }
  173. else
  174. {
  175. _s_GPGSV.pamstr[pamindex].clear();
  176. }
  177. pamindex++;
  178. }
  179. }
  180. int SatelliteSize = (pamindex - 4) / 4;
  181. //Serial.printf("Number%d\n", SatelliteSize);
  182. _s_GPGSV.size = _s_GPGSV.pamstr[1].toInt();
  183. _s_GPGSV.Number = _s_GPGSV.pamstr[2].toInt();
  184. _s_GPGSV.SatelliteSize = _s_GPGSV.pamstr[3].toInt();
  185. if (_s_GPGSV.Number == 1)
  186. {
  187. for (size_t i = 0; i < 32; i++)
  188. {
  189. _s_GPGSV.Satellite[i].flag = false;
  190. }
  191. }
  192. for (size_t i = 0; i < SatelliteSize; i++)
  193. {
  194. int id = _s_GPGSV.pamstr[4 + (i * 4) + 0].toInt();
  195. if( id >= 32 ) continue;
  196. if(( 7 + (i * 4)) > 50 )
  197. {
  198. break;
  199. }
  200. _s_GPGSV.Satellite[id].elevation = _s_GPGSV.pamstr[4 + (i * 4) + 1].toInt();
  201. _s_GPGSV.Satellite[id].Azimuth = _s_GPGSV.pamstr[4 + (i * 4) + 2].toInt();
  202. _s_GPGSV.Satellite[id].SNR = (_s_GPGSV.pamstr[4 + (i * 4) + 3].length() == 0) ? -1 : _s_GPGSV.pamstr[4 + (i * 4) + 3].toInt();
  203. _s_GPGSV.Satellite[id].flag = true;
  204. }
  205. if (_s_GPGSV.Number == _s_GPGSV.size)
  206. {
  207. for (size_t i = 0; i < 32; i++)
  208. {
  209. if (_s_GPGSV.Satellite[i].flag == true)
  210. {
  211. #ifdef DEBUG_GPS
  212. Serial.printf("ID %d:%d,%d,%d\n", i, _s_GPGSV.Satellite[i].elevation, _s_GPGSV.Satellite[i].Azimuth, _s_GPGSV.Satellite[i].SNR);
  213. #endif
  214. }
  215. }
  216. }
  217. }
  218. void GPSAnalyse::Analyse()
  219. {
  220. while (_GPS_Str.indexOf('\r') != -1)
  221. {
  222. int index = _GPS_Str.indexOf('\r');
  223. String str = _GPS_Str.substring(0, index);
  224. _GPS_Str = _GPS_Str.substring(index + 3, _GPS_Str.length());
  225. str.trim();
  226. //Serial.println(str);
  227. if (str.indexOf("GNRMC") != -1)
  228. {
  229. AnaGPRMC(str);
  230. }
  231. else if (str.indexOf("GPVTG") != -1)
  232. {
  233. }
  234. else if (str.indexOf("GPGGA") != -1)
  235. {
  236. }
  237. else if (str.indexOf("GNGSA") != -1)
  238. {
  239. //Serial.print("GNGSA:");
  240. //AnaGNGAS(str);
  241. }
  242. else if (str.indexOf("GPGSV") != -1)
  243. {
  244. //Serial.print("GPGSV:");
  245. AnaGPGSV(str);
  246. }
  247. else if (str.indexOf("GPGLL") != -1)
  248. {
  249. }
  250. }
  251. }