AppTaskGps.c 6.1 KB

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