AppTaskGps.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * @Author: chenjie
  3. * @Date: 2022-06-06
  4. * @LastEditTime: 2022-10-27
  5. * @LastEditors: chenjie
  6. * @Description:
  7. * @FilePath: \undefinedd:\1_WorkFiles\1_Device\2_4G_G1\2_SW\workspace\S32K146_4G\code\app\AppTaskGps.c
  8. * Copyright (c) 2022 by chenjie, All Rights Reserved.
  9. */
  10. #include "AppTaskGps.h"
  11. void strdel(char *str, char c);
  12. uint32 location_handle(char *in1);
  13. void GpsDataDecode(uint8 *msg);
  14. uint8 GpsRecvPtr[1024];
  15. uint32 timerCounterGetdata = 0;
  16. uint32 ledCounterTmp = 0;
  17. uint32 triaxiCounterTmp = 0;
  18. static void vtimerCallback(TimerHandle_t pxTimer)
  19. {
  20. uint32 ulTimerID;
  21. ulTimerID = (uint32)pvTimerGetTimerID(pxTimer);
  22. if (ulTimerID == 0)
  23. {
  24. timerCounterGetdata++;
  25. }
  26. }
  27. void GpsTask(void *pvParameters)
  28. {
  29. (void)pvParameters;
  30. GpsDataQueueHandle = xQueueCreate(1, sizeof(GPSInfo));//长度为1才可以允许覆写
  31. Dio_WriteChannel(DioConf_DioChannel_PTD1_GPIO_OUT_MCU_GPS_POW_EN, STD_ON);//GPS开机
  32. uint16 pReadLen = 0;
  33. TimerHandle_t monitorTimer1ms;
  34. monitorTimer1ms = xTimerCreate("monitor1ms", 1, pdTRUE, (void *)0, vtimerCallback);
  35. xTimerStart(monitorTimer1ms, 0);
  36. real_T S0[SIZE_FFT];
  37. real_T S1[SIZE_FFT];
  38. real_T S2[SIZE_FFT];
  39. uint8 fftIdx = 0;
  40. uint16 GpsErrCounter = 0;
  41. uint16 GpsRestartCounter = 0;
  42. bool GpsPwrDownFlg = false;
  43. while(1)
  44. {
  45. memset(GpsRecvPtr,0,sizeof(GpsRecvPtr));
  46. UART_Receive_Data(UART_LPUART2,GpsRecvPtr,&pReadLen,1);
  47. if(pReadLen>0)
  48. {
  49. GpsDataDecode(GpsRecvPtr);
  50. }
  51. if(TimerCounter%1000==0)
  52. {
  53. if(DeviceSpeed>0)//GPS定位成功
  54. {
  55. GpsErrCounter = 0;
  56. }
  57. else
  58. {
  59. if(GpsErrCounter>60*10)
  60. {
  61. Dio_WriteChannel(DioConf_DioChannel_PTD1_GPIO_OUT_MCU_GPS_POW_EN, STD_OFF);//GPS关机
  62. GpsErrCounter = 0;
  63. GpsPwrDownFlg = true;
  64. GpsRestartCounter = 0;
  65. }
  66. if(GpsPwrDownFlg)
  67. {
  68. GpsRestartCounter+=1;
  69. }
  70. GpsErrCounter+=1;
  71. }
  72. if(GpsRestartCounter>10)
  73. {
  74. GpsRestartCounter = 0;
  75. GpsPwrDownFlg = false;
  76. Dio_WriteChannel(DioConf_DioChannel_PTD1_GPIO_OUT_MCU_GPS_POW_EN, STD_ON);//GPS开机
  77. }
  78. }
  79. /*LED控制*/
  80. if((timerCounterGetdata - ledCounterTmp) > 200)
  81. {
  82. Dio_WriteChannel(LED_INDEX2, STD_OFF);
  83. ledCounterTmp = timerCounterGetdata;
  84. if(SocketId>=0)//网络连接成功
  85. {
  86. Dio_FlipChannel(LED_INDEX3);
  87. }
  88. else
  89. {
  90. Dio_WriteChannel(LED_INDEX3,STD_OFF);
  91. }
  92. if(DeviceSpeed>0)//GPS定位成功
  93. {
  94. Dio_FlipChannel(LED_INDEX4);
  95. GpsErrCounter = 0;
  96. }
  97. else
  98. {
  99. Dio_WriteChannel(LED_INDEX4,STD_OFF);
  100. }
  101. }
  102. /*三轴计算*/
  103. if ((timerCounterGetdata - triaxiCounterTmp)> 10)
  104. {
  105. triaxiCounterTmp = timerCounterGetdata;
  106. SL_SC7A20_Read_XYZ_Data(xyzData);
  107. S0[fftIdx] = (real_T)xyzData[0];
  108. S1[fftIdx] = (real_T)xyzData[1];
  109. S2[fftIdx] = (real_T)xyzData[2];
  110. fftIdx++;
  111. }
  112. if (fftIdx >= SIZE_FFT)
  113. {
  114. fftIdx = 0;
  115. fft(S0, sizeof(S0) / sizeof(real_T), 100, returnFreq[0], returnP[0]); // 50 max
  116. fft(S1, sizeof(S1) / sizeof(real_T), 100, returnFreq[1], returnP[1]);
  117. fft(S2, sizeof(S2) / sizeof(real_T), 100, returnFreq[2], returnP[2]);
  118. }
  119. }
  120. }
  121. void GpsDataDecode(uint8 *msg)
  122. {
  123. // char temp[] = "$GNRMC,082626.000,A,2939.91801,N,10637.09500,E,5.543,30.254,261120,,,A,V*17";
  124. char *p = NULL;
  125. char *p2 = NULL;
  126. const char *delim = "\n";
  127. GPSInfo GpsInfoData;
  128. uint8 index = 0;
  129. char *databuffer[20];
  130. uint32 speedtemp;
  131. uint32 latitude;
  132. uint32 longitude;
  133. uint16 direction;
  134. memset((uint8 *)&GpsInfoData, 0x00, sizeof(GPSInfo));
  135. p = strtok(msg, delim); //将信息进行分割
  136. p2 = strtok(NULL, delim);
  137. p = strtok(p, ","); //只取第1行的信息RMC
  138. // p = strtok(temp,",");//模拟测试
  139. if (strcmp(p, "$GNRMC") == 0)
  140. {
  141. index = 0;
  142. while (p)
  143. {
  144. databuffer[index] = p;
  145. p = strtok(NULL, ",");
  146. index++;
  147. }
  148. if (index > 5 && strcmp(databuffer[2], "A") == 0)
  149. {
  150. GpsInfoData.locateMark = 0x01; //有效,东经,北纬写定
  151. strdel(databuffer[3], '.');
  152. strdel(databuffer[5], '.');
  153. strdel(databuffer[7], '.');
  154. speedtemp = atol(databuffer[7]) * 1852 / 1e5; //节换算单位,1节=1.852km每小时
  155. DeviceSpeed = speedtemp+1;
  156. latitude = location_handle(databuffer[3]);
  157. GpsInfoData.latitude[0] = latitude >> 24;
  158. GpsInfoData.latitude[1] = latitude >> 16;
  159. GpsInfoData.latitude[2] = latitude >> 8;
  160. GpsInfoData.latitude[3] = latitude;
  161. longitude = location_handle(databuffer[5]);
  162. GpsInfoData.longitude[0] = longitude >> 24;
  163. GpsInfoData.longitude[1] = longitude >> 16;
  164. GpsInfoData.longitude[2] = longitude >> 8;
  165. GpsInfoData.longitude[3] = longitude;
  166. if (speedtemp >= 50) //大于5km/h才输出方位
  167. {
  168. direction = atol(databuffer[8]);
  169. GpsInfoData.direction[0] = direction >> 8;
  170. GpsInfoData.direction[1] = direction;
  171. GpsInfoData.speed[0] = (speedtemp >> 8) & 0xFF;
  172. GpsInfoData.speed[1] = speedtemp & 0xFF;
  173. }
  174. else
  175. {
  176. GpsInfoData.direction[0] = 0;
  177. GpsInfoData.direction[1] = 0;
  178. GpsInfoData.speed[0] = 0;
  179. GpsInfoData.speed[1] = 0;
  180. }
  181. if (speedtemp >= 50 && speedtemp <= 1500)
  182. {
  183. AppConfigInfo.appSaveFlg = true;
  184. AppConfigInfo.AppDataInfo.AccMileage = speedtemp / 36 + AppConfigInfo.AppDataInfo.AccMileage;
  185. if (AppConfigInfo.AppDataInfo.AccMileage >= 0xfffffffe)
  186. {
  187. AppConfigInfo.AppDataInfo.AccMileage = 0;
  188. }
  189. }//累计里程的累加
  190. }
  191. else
  192. {
  193. DeviceSpeed = 0;
  194. }
  195. GpsInfoData.AccMileage[0] = AppConfigInfo.AppDataInfo.AccMileage >> 24;
  196. GpsInfoData.AccMileage[1] = AppConfigInfo.AppDataInfo.AccMileage >> 16;
  197. GpsInfoData.AccMileage[2] = AppConfigInfo.AppDataInfo.AccMileage >> 8;
  198. GpsInfoData.AccMileage[3] = AppConfigInfo.AppDataInfo.AccMileage;
  199. }
  200. p2 = strtok(p2, ","); //只取第2行的信息GGA
  201. memset(databuffer, 0x30, 20);
  202. if (strcmp(p2, "$GNGGA") == 0)
  203. {
  204. index = 0;
  205. while (p2)
  206. {
  207. databuffer[index] = p2;
  208. p2 = strtok(NULL, ",");
  209. index++;
  210. }
  211. if (index > 9 && (strcmp(databuffer[6], "1") == 0 || strcmp(databuffer[6], "2") == 0))
  212. {
  213. GpsInfoData.satelliteNum = atol(databuffer[7]); //卫星数目写入
  214. strdel(databuffer[9], '.');
  215. uint16 alt = 0;
  216. alt = atol(databuffer[9]) / 10 + 1000;
  217. GpsInfoData.altitude[0] = alt >> 8; //海拔
  218. GpsInfoData.altitude[1] = alt;
  219. strdel(databuffer[2], '.');
  220. strdel(databuffer[4], '.');
  221. latitude = location_handle(databuffer[2]);
  222. GpsInfoData.latitude[0] = latitude >> 24;
  223. GpsInfoData.latitude[1] = latitude >> 16;
  224. GpsInfoData.latitude[2] = latitude >> 8;
  225. GpsInfoData.latitude[3] = latitude;
  226. longitude = location_handle(databuffer[4]);
  227. GpsInfoData.longitude[0] = longitude >> 24;
  228. GpsInfoData.longitude[1] = longitude >> 16;
  229. GpsInfoData.longitude[2] = longitude >> 8;
  230. GpsInfoData.longitude[3] = longitude;
  231. }
  232. }
  233. xQueueOverwrite(GpsDataQueueHandle,(uint8 *)&GpsInfoData);
  234. }
  235. void strdel(char *str, char c)
  236. {
  237. char *p = str;
  238. while (*str)
  239. {
  240. if (*str != c)
  241. *p++ = *str;
  242. str++;
  243. }
  244. *p = '\0';
  245. }
  246. uint32 location_handle(char *in1)
  247. {
  248. uint32 location_temp;
  249. uint32 location_degree;
  250. uint32 location_min;
  251. location_temp = atol(in1);
  252. location_degree = location_temp / (1e7);
  253. location_degree = location_degree * (1e6);
  254. location_min = location_temp - location_degree * 10;
  255. location_min = location_min / 6;
  256. location_temp = location_degree + location_min;
  257. return location_temp;
  258. }