AppTaskGps.c 7.0 KB

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