AppTaskGps.c 5.6 KB

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