AppTaskGps.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. while(1)
  41. {
  42. memset(GpsRecvPtr,0,sizeof(GpsRecvPtr));
  43. UART_Receive_Data(UART_LPUART2,GpsRecvPtr,&pReadLen,1);
  44. if(pReadLen>0)
  45. {
  46. GpsDataDecode(GpsRecvPtr);
  47. }
  48. /*LED控制*/
  49. if((timerCounterGetdata - ledCounterTmp) > 200)
  50. {
  51. Dio_WriteChannel(DioConf_DioChannel_PTE0_GPIO_OUT_MCU_LED1, STD_OFF);
  52. ledCounterTmp = timerCounterGetdata;
  53. if(SocketId>=0)//网络连接成功
  54. {
  55. Dio_FlipChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2);
  56. }
  57. else
  58. {
  59. Dio_WriteChannel(DioConf_DioChannel_PTE1_GPIO_OUT_MCU_LED2,STD_OFF);
  60. }
  61. if(DeviceSpeed>0)//GPS定位成功
  62. {
  63. Dio_FlipChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_LED3);
  64. }
  65. else
  66. {
  67. Dio_WriteChannel(DioConf_DioChannel_PTE7_GPIO_OUT_MCU_LED3,STD_OFF);
  68. }
  69. }
  70. /*三轴计算*/
  71. if ((timerCounterGetdata - triaxiCounterTmp)> 10)
  72. {
  73. triaxiCounterTmp = timerCounterGetdata;
  74. SL_SC7A20_Read_XYZ_Data(xyzData);
  75. S0[fftIdx] = (real_T)xyzData[0];
  76. S1[fftIdx] = (real_T)xyzData[1];
  77. S2[fftIdx] = (real_T)xyzData[2];
  78. fftIdx++;
  79. }
  80. if (fftIdx >= SIZE_FFT)
  81. {
  82. fftIdx = 0;
  83. fft(S0, sizeof(S0) / sizeof(real_T), 100, returnFreq[0], returnP[0]); // 50 max
  84. fft(S1, sizeof(S1) / sizeof(real_T), 100, returnFreq[1], returnP[1]);
  85. fft(S2, sizeof(S2) / sizeof(real_T), 100, returnFreq[2], returnP[2]);
  86. }
  87. }
  88. }
  89. void GpsDataDecode(uint8 *msg)
  90. {
  91. // char temp[] = "$GNRMC,082626.000,A,2939.91801,N,10637.09500,E,5.543,30.254,261120,,,A,V*17";
  92. char *p = NULL;
  93. char *p2 = NULL;
  94. const char *delim = "\n";
  95. GPSInfo GpsInfoData;
  96. uint8 index = 0;
  97. char *databuffer[20];
  98. uint32 speedtemp;
  99. uint32 latitude;
  100. uint32 longitude;
  101. uint16 direction;
  102. memset((uint8 *)&GpsInfoData, 0x00, sizeof(GPSInfo));
  103. p = strtok(msg, delim); //将信息进行分割
  104. p2 = strtok(NULL, delim);
  105. p = strtok(p, ","); //只取第1行的信息RMC
  106. // p = strtok(temp,",");//模拟测试
  107. if (strcmp(p, "$GNRMC") == 0)
  108. {
  109. index = 0;
  110. while (p)
  111. {
  112. databuffer[index] = p;
  113. p = strtok(NULL, ",");
  114. index++;
  115. }
  116. if (index > 5 && strcmp(databuffer[2], "A") == 0)
  117. {
  118. GpsInfoData.locateMark = 0x01; //有效,东经,北纬写定
  119. strdel(databuffer[3], '.');
  120. strdel(databuffer[5], '.');
  121. strdel(databuffer[7], '.');
  122. speedtemp = atol(databuffer[7]) * 1852 / 1e5; //节换算单位,1节=1.852km每小时
  123. DeviceSpeed = speedtemp+1;
  124. latitude = location_handle(databuffer[3]);
  125. GpsInfoData.latitude[0] = latitude >> 24;
  126. GpsInfoData.latitude[1] = latitude >> 16;
  127. GpsInfoData.latitude[2] = latitude >> 8;
  128. GpsInfoData.latitude[3] = latitude;
  129. longitude = location_handle(databuffer[5]);
  130. GpsInfoData.longitude[0] = longitude >> 24;
  131. GpsInfoData.longitude[1] = longitude >> 16;
  132. GpsInfoData.longitude[2] = longitude >> 8;
  133. GpsInfoData.longitude[3] = longitude;
  134. if (speedtemp >= 50) //大于5km/h才输出方位
  135. {
  136. direction = atol(databuffer[8]);
  137. GpsInfoData.direction[0] = direction >> 8;
  138. GpsInfoData.direction[1] = direction;
  139. GpsInfoData.speed[0] = (speedtemp >> 8) & 0xFF;
  140. GpsInfoData.speed[1] = speedtemp & 0xFF;
  141. }
  142. else
  143. {
  144. GpsInfoData.direction[0] = 0;
  145. GpsInfoData.direction[1] = 0;
  146. GpsInfoData.speed[0] = 0;
  147. GpsInfoData.speed[1] = 0;
  148. }
  149. if (speedtemp >= 50 && speedtemp <= 1500)
  150. {
  151. AppConfigInfo.appSaveFlg = true;
  152. AppConfigInfo.AppDataInfo.AccMileage = speedtemp / 36 + AppConfigInfo.AppDataInfo.AccMileage;
  153. if (AppConfigInfo.AppDataInfo.AccMileage >= 0xfffffffe)
  154. {
  155. AppConfigInfo.AppDataInfo.AccMileage = 0;
  156. }
  157. }//累计里程的累加
  158. }
  159. else
  160. {
  161. DeviceSpeed = 0;
  162. }
  163. GpsInfoData.AccMileage[0] = AppConfigInfo.AppDataInfo.AccMileage >> 24;
  164. GpsInfoData.AccMileage[1] = AppConfigInfo.AppDataInfo.AccMileage >> 16;
  165. GpsInfoData.AccMileage[2] = AppConfigInfo.AppDataInfo.AccMileage >> 8;
  166. GpsInfoData.AccMileage[3] = AppConfigInfo.AppDataInfo.AccMileage;
  167. }
  168. p2 = strtok(p2, ","); //只取第2行的信息GGA
  169. memset(databuffer, 0x30, 20);
  170. if (strcmp(p2, "$GNGGA") == 0)
  171. {
  172. index = 0;
  173. while (p2)
  174. {
  175. databuffer[index] = p2;
  176. p2 = strtok(NULL, ",");
  177. index++;
  178. }
  179. if (index > 9 && (strcmp(databuffer[6], "1") == 0 || strcmp(databuffer[6], "2") == 0))
  180. {
  181. GpsInfoData.satelliteNum = atol(databuffer[7]); //卫星数目写入
  182. strdel(databuffer[9], '.');
  183. uint16 alt = 0;
  184. alt = atol(databuffer[9]) / 10 + 1000;
  185. GpsInfoData.altitude[0] = alt >> 8; //海拔
  186. GpsInfoData.altitude[1] = alt;
  187. strdel(databuffer[2], '.');
  188. strdel(databuffer[4], '.');
  189. latitude = location_handle(databuffer[2]);
  190. GpsInfoData.latitude[0] = latitude >> 24;
  191. GpsInfoData.latitude[1] = latitude >> 16;
  192. GpsInfoData.latitude[2] = latitude >> 8;
  193. GpsInfoData.latitude[3] = latitude;
  194. longitude = location_handle(databuffer[4]);
  195. GpsInfoData.longitude[0] = longitude >> 24;
  196. GpsInfoData.longitude[1] = longitude >> 16;
  197. GpsInfoData.longitude[2] = longitude >> 8;
  198. GpsInfoData.longitude[3] = longitude;
  199. }
  200. }
  201. xQueueOverwrite(GpsDataQueueHandle,(uint8 *)&GpsInfoData);
  202. }
  203. void strdel(char *str, char c)
  204. {
  205. char *p = str;
  206. while (*str)
  207. {
  208. if (*str != c)
  209. *p++ = *str;
  210. str++;
  211. }
  212. *p = '\0';
  213. }
  214. uint32 location_handle(char *in1)
  215. {
  216. uint32 location_temp;
  217. uint32 location_degree;
  218. uint32 location_min;
  219. location_temp = atol(in1);
  220. location_degree = location_temp / (1e7);
  221. location_degree = location_degree * (1e6);
  222. location_min = location_temp - location_degree * 10;
  223. location_min = location_min / 6;
  224. location_temp = location_degree + location_min;
  225. return location_temp;
  226. }