AppTaskGps.c 7.4 KB

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