app_gps.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include "app_gps.h"
  2. #include "FreeRTOS.h"
  3. #include "queue.h"
  4. #include "stdlib.h"
  5. #include "app_protocol.h"
  6. extern gps_info_t gps_info;
  7. QueueHandle_t gps_data_queue;
  8. uint16_t usart3_rx_status;
  9. uint8_t usart3_rx[1500];
  10. void strdel(char *str, char c);
  11. uint32_t location_handle(char *in1);
  12. void GpsDataDecode(uint8_t *msg);
  13. void GpsTask(void *pvParameters)
  14. {
  15. (void)pvParameters;
  16. gps_data_queue = xQueueCreate(1, sizeof(gps_info_t));//长度为1才可以允许覆写
  17. while(1)
  18. {
  19. if(usart3_rx_status&0x8000)//串口3收到数据
  20. {
  21. //printf("recieve gps data:%s\r\n",usart3_rx);
  22. GpsDataDecode(usart3_rx);
  23. usart3_rx_status = 0;
  24. memset(usart3_rx,0,sizeof(usart3_rx));
  25. }
  26. vTaskDelay(pdMS_TO_TICKS(100));
  27. }
  28. }
  29. void GpsDataDecode(uint8_t *msg)
  30. {
  31. /*
  32. $GNRMC,021732.00,A,2939.88641,N,10637.05509,E,0.424,,290822,,,A,V*18
  33. $GNGGA,021732.00,2939.88641,N,10637.05509,E,1,14,2.91,245.6,M,,M,,*5B
  34. $GNGSA,A,3,05,11,12,20,02,25,,,,,,,4.41,2.91,3.31,1*0A
  35. $GNGSA,A,3,13,29,08,05,35,,,,,,,,4.41,2.91,3.31,4*0C
  36. $GNGSA,A,3,11,24,25,,,,,,,,,,4.41,2.91,3.31,3*08
  37. $GPGSV,3,1,11,02,43,316,41,05,46,239,28,11,56,006,11,12,40,255,41,0*63
  38. $GPGSV,3,2,11,20,75,313,33,25,23,295,36,19,,,26,42,39,126,,0*5D
  39. $GPGSV,3,3,11,50,35,121,,40,24,248,36,41,46,221,29,0*50
  40. $GBGSV,2,1,08,05,28,245,29,08,59,335,35,13,58,310,35,29,62,346,36,0*7C
  41. $GBGSV,2,2,08,35,16,312,33,09,01,178,,10,54,201,,30,46,099,,0*7A
  42. $GAGSV,1,1,04,11,38,313,32,24,30,227,14,25,25,288,22,10,,,32,0*4E
  43. $GNVTG,,T,,M,0.424,N,0.785,K,A*35
  44. */
  45. //char temp[] = "$GNRMC,082626.000,A,2939.91801,N,10637.09500,E,0.543,30.254,261120,,,A,V*17";
  46. char temp[] = "$GNRMC,021732.00,A,2939.88641,N,10637.05509,E,0.424,,290822,,,A,V*18";
  47. char temp1[] ="$GNGGA,021732.00,2939.88641,N,10637.05509,E,1,14,2.91,245.6,M,,M,,*5B";
  48. char *p = NULL;
  49. char *p2 = NULL;
  50. const char *delim = "\n";
  51. gps_info_t gps_info_data;
  52. uint8_t index = 0;
  53. char *databuffer[20];
  54. uint32_t speedtemp;
  55. uint32_t latitude;
  56. uint32_t longitude;
  57. uint32_t direction;
  58. memset((uint8_t *)&gps_info_data, 0x00, sizeof(gps_info_t));
  59. p = strtok((char*)msg, delim); //将信息进行分割
  60. p2 = strtok(NULL, delim);
  61. p = strtok(p, ","); //只取第1行的信息RMC
  62. //memcpy(p,temp,strlen(temp));测试
  63. if (strstr(p,"$GNRMC") != 0)
  64. {
  65. index = 0;
  66. while (p)
  67. {
  68. databuffer[index] = p;
  69. p = strtok(NULL, ",");
  70. index++;
  71. }
  72. if (index > 5 && strcmp(databuffer[2], "A") == 0)
  73. {
  74. gps_info_data.locate_mark = 0x01; //有效,东经,北纬写定
  75. strdel(databuffer[3], '.');
  76. strdel(databuffer[5], '.');
  77. //printf("databuffer[7]=%s\r\n",databuffer[7]);
  78. strdel(databuffer[7], '.');
  79. //printf("databuffer[7]=%s\r\n",databuffer[7]);
  80. speedtemp = atol(databuffer[7]) * 1852 / 1e5; //节换算单位,1节=1.852km每小时
  81. //printf("speedtemp=%d\r\n",speedtemp);
  82. gps_info_data.speed[0] = (speedtemp >> 8) & 0xFF;
  83. gps_info_data.speed[1] = speedtemp & 0xFF;
  84. latitude = location_handle(databuffer[3]);
  85. gps_info_data.latitude[0] = latitude >> 24;
  86. gps_info_data.latitude[1] = latitude >> 16;
  87. gps_info_data.latitude[2] = latitude >> 8;
  88. gps_info_data.latitude[3] = latitude;
  89. longitude = location_handle(databuffer[5]);
  90. gps_info_data.longitude[0] = longitude >> 24;
  91. gps_info_data.longitude[1] = longitude >> 16;
  92. gps_info_data.longitude[2] = longitude >> 8;
  93. gps_info_data.longitude[3] = longitude;
  94. if (speedtemp >= 50) //大于5km/h才输出方位
  95. {
  96. direction = atol(databuffer[8]);
  97. gps_info_data.direction[0] = direction >> 8;
  98. gps_info_data.direction[1] = direction;
  99. }
  100. else
  101. {
  102. gps_info_data.direction[0] = 0xff;
  103. gps_info_data.direction[1] = 0xff;
  104. }
  105. // if (speedtemp >= 30 && speedtemp <= 1500 && BattWorkStateDelay == 0x01)
  106. // {
  107. // AppDataInfo.appDataModify = true;
  108. // AppDataInfo.AccMileage = speedtemp / 36 + AppDataInfo.AccMileage;
  109. // if (AppDataInfo.AccMileage >= 0xfffffffe)
  110. // {
  111. // AppDataInfo.AccMileage = 0;
  112. // }
  113. // }累计里程的累加
  114. }
  115. // GpsInfoData.AccMileage[0] = AppDataInfo.AccMileage >> 24;
  116. // GpsInfoData.AccMileage[1] = AppDataInfo.AccMileage >> 16;
  117. // GpsInfoData.AccMileage[2] = AppDataInfo.AccMileage >> 8;
  118. // GpsInfoData.AccMileage[3] = AppDataInfo.AccMileage;
  119. }
  120. p2 = strtok(p2, ","); //只取第2行的信息GGA
  121. memcpy(p2,temp,strlen(temp));///////////////////////////////
  122. memset(databuffer, 0x30, 20);
  123. if (strstr(p2, "$GNGGA") != 0)
  124. {
  125. index = 0;
  126. while (p2)
  127. {
  128. databuffer[index] = p2;
  129. p2 = strtok(NULL, ",");
  130. index++;
  131. }
  132. if (index > 9 && (strcmp(databuffer[6], "1") == 0 || strcmp(databuffer[6], "2") == 0))
  133. {
  134. gps_info_data.satellite_num = atol(databuffer[7]); //卫星数目写入
  135. strdel(databuffer[9], '.');
  136. uint16_t alt = 0;
  137. alt = atol(databuffer[9]) / 10 + 1000;
  138. gps_info_data.altitude[0] = alt >> 8; //海拔
  139. gps_info_data.altitude[1] = alt;
  140. strdel(databuffer[2], '.');
  141. strdel(databuffer[4], '.');
  142. latitude = location_handle(databuffer[2]);
  143. gps_info_data.latitude[0] = latitude >> 24;
  144. gps_info_data.latitude[1] = latitude >> 16;
  145. gps_info_data.latitude[2] = latitude >> 8;
  146. gps_info_data.latitude[3] = latitude;
  147. longitude = location_handle(databuffer[4]);
  148. gps_info_data.longitude[0] = longitude >> 24;
  149. gps_info_data.longitude[1] = longitude >> 16;
  150. gps_info_data.longitude[2] = longitude >> 8;
  151. gps_info_data.longitude[3] = longitude;
  152. }
  153. }
  154. xQueueOverwrite(gps_data_queue,(uint8_t *)&gps_info_data);
  155. }
  156. void strdel(char *str, char c)
  157. {
  158. char *p = str;
  159. while (*str)
  160. {
  161. if (*str != c)
  162. *p++ = *str;
  163. str++;
  164. }
  165. *p = '\0';
  166. }
  167. uint32_t location_handle(char *in1)
  168. {
  169. uint32_t location_temp;
  170. uint32_t location_degree;
  171. uint32_t location_min;
  172. location_temp = atol(in1);
  173. location_degree = location_temp / (1e7);
  174. location_degree = location_degree * (1e6);
  175. location_min = location_temp - location_degree * 10;
  176. location_min = location_min / 6;
  177. location_temp = location_degree + location_min;
  178. return location_temp;
  179. }