AppTaskGps.c 6.2 KB

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