GpsTask.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /****************************************************************************
  2. *
  3. * Copy right: Qx.Chen jie
  4. * File name: GpsTask.c
  5. * Description: Gps处理任务
  6. * History: 2021-03-07
  7. *
  8. ****************************************************************************/
  9. #include "bsp.h"
  10. #include "bsp_custom.h"
  11. #include "osasys.h"
  12. #include "ostask.h"
  13. #include "queue.h"
  14. #include "ps_event_callback.h"
  15. #include "app.h"
  16. #include "cmisim.h"
  17. #include "cmimm.h"
  18. #include "cmips.h"
  19. #include "sockets.h"
  20. #include "psifevent.h"
  21. #include "ps_lib_api.h"
  22. #include "lwip/netdb.h"
  23. //#include <cis_def.h>
  24. #include "debug_log.h"
  25. #include "slpman_ec616.h"
  26. #include "plat_config.h"
  27. #include "ec_tcpip_api.h"
  28. #include "app.h"
  29. #include "MainTask.h"
  30. #include "GpsTask.h"
  31. #include "TcpTask.h"
  32. //全局变量区输入
  33. extern volatile BOOL Sleep_flag;
  34. extern AppDataBody AppDataInfo;
  35. extern UINT8 WorkFlag;
  36. //全局变量区输出
  37. UINT8 GpsData[20];
  38. osMutexId_t GpsMutex = NULL;
  39. //线程定义区
  40. #define GPS_TASK_STACK_SIZE (512)
  41. static QueueHandle_t norGpsHandle = NULL;
  42. static osThreadId_t gpsTaskHandle = NULL;
  43. static StaticTask_t gpsTask = NULL;
  44. static UINT8 gpsTaskStack[GPS_TASK_STACK_SIZE];
  45. //函数声明区
  46. void strdel(char * str,char c);
  47. UINT32 location_handle(char *in1);
  48. static void GpsTask(void* arg)
  49. {
  50. gpsReqMsg msg;
  51. char *p=NULL;
  52. const char *delim = "\n";
  53. char *databuffer[14];
  54. UINT32 speedtemp;
  55. UINT32 latitude;
  56. UINT32 longitude;
  57. UINT16 direction;
  58. posGGAServiceStart(norGpsHandle);
  59. posGGAReset();
  60. if(GpsMutex == NULL)
  61. {
  62. GpsMutex = osMutexNew(NULL);
  63. }
  64. while(1)
  65. {
  66. //char temp[] = "$GNRMC,082626.000,A,2939.91801,N,10637.09500,E,0.543,30.254,261120,,,A,V*17";
  67. osStatus_t ret = osMessageQueueGet(norGpsHandle, &msg, 0, osWaitForever);
  68. if(ret==0)
  69. {
  70. if (msg.dataPtr)
  71. {
  72. osStatus_t result = osMutexAcquire(GpsMutex, osWaitForever);//Gps数据锁
  73. p = strtok(msg.dataPtr,delim);//将信息进行分割
  74. #ifdef USING_PRINTF1
  75. printf("\nP msgptr data:%s\r\n",p);
  76. #endif
  77. p = strtok(p,",");//只取第1行的信息RMC
  78. //p = strtok(temp,",");//模拟测试
  79. if (strcmp(p,"$GNRMC")==0)
  80. {
  81. UINT8 i=0;
  82. while (p)
  83. {
  84. databuffer[i]=p;
  85. p = strtok(NULL,",");
  86. i++;;
  87. }
  88. if (strcmp(databuffer[1],"V")==0|strcmp(databuffer[2],"V")==0)
  89. {
  90. memset(GpsData,0x00,sizeof(GpsData));
  91. }
  92. else if (strcmp(databuffer[2],"A")==0)
  93. {
  94. GpsData[0] = 0x01;//有效,东经,北纬写定
  95. GpsData[1] = 03;//卫星数目写入1
  96. strdel(databuffer[3],'.');
  97. strdel(databuffer[5],'.');
  98. strdel(databuffer[7],'.');
  99. speedtemp = atol(databuffer[7])*1852/1e5;//节换算单位,1节=1.852km每小时
  100. GpsData[4] = (speedtemp>>8)&0xFF;
  101. GpsData[5] = speedtemp&0xFF;
  102. latitude =location_handle(databuffer[3]);
  103. GpsData[8] = latitude>> 24;
  104. GpsData[9] = latitude>> 16;
  105. GpsData[10] = latitude>> 8;
  106. GpsData[11] = latitude;
  107. longitude = location_handle(databuffer[5]);
  108. GpsData[12] = longitude>>24;
  109. GpsData[13] = longitude>>16;
  110. GpsData[14] = longitude>>8;
  111. GpsData[15] = longitude;
  112. GpsData[6] = 0x03;
  113. GpsData[7] = 0xE8;
  114. if(speedtemp>=50)//大于5km/h才输出方位
  115. {
  116. direction = atol(databuffer[8]);
  117. GpsData[2] = direction>>8;
  118. GpsData[3] = direction;
  119. }
  120. else
  121. {
  122. GpsData[2] = 0xff;
  123. GpsData[3] = 0xfe;
  124. }
  125. if(speedtemp>=30 && speedtemp<=1500 && WorkFlag==0x01)
  126. {
  127. AppDataInfo.appDataModify = true;
  128. AppDataInfo.AccMileage = speedtemp/36 + AppDataInfo.AccMileage;
  129. if(AppDataInfo.AccMileage>=0xfffffffe)
  130. {
  131. AppDataInfo.AccMileage = 0;
  132. }
  133. }
  134. GpsData[16] = AppDataInfo.AccMileage>>24;
  135. GpsData[17] = AppDataInfo.AccMileage>>16;
  136. GpsData[18] = AppDataInfo.AccMileage>>8;
  137. GpsData[19] = AppDataInfo.AccMileage;
  138. }
  139. }
  140. osMutexRelease(GpsMutex);
  141. }
  142. if(msg.dataPtr)
  143. free(msg.dataPtr);
  144. msg.dataPtr=NULL;
  145. }
  146. if (Sleep_flag)
  147. {
  148. posGGAServiceStop();
  149. osThreadExit();
  150. break;
  151. }
  152. }
  153. }
  154. INT32 GpsTaskInit(void)
  155. {
  156. if(norGpsHandle == NULL)
  157. {
  158. norGpsHandle = osMessageQueueNew(1,sizeof(gpsReqMsg), NULL);
  159. if(norGpsHandle == NULL)
  160. return 1;
  161. }
  162. if(gpsTaskHandle == NULL)
  163. {
  164. osThreadAttr_t task_attr;
  165. memset(&task_attr , 0 , sizeof(task_attr));
  166. task_attr.name = "GPS";
  167. task_attr.priority = osPriorityBelowNormal6;
  168. task_attr.cb_mem = &gpsTask;
  169. task_attr.cb_size = sizeof(StaticTask_t);
  170. task_attr.stack_mem = gpsTaskStack;
  171. task_attr.stack_size =GPS_TASK_STACK_SIZE;
  172. memset(& gpsTaskStack, 0xa5, GPS_TASK_STACK_SIZE);
  173. gpsTaskHandle = osThreadNew(GpsTask , NULL,&task_attr);
  174. if(gpsTaskHandle == NULL)
  175. return 1;
  176. }
  177. return 0;
  178. }
  179. /*----------------------------------------*/
  180. // 字符串删除函数
  181. void strdel(char * str,char c)
  182. {
  183. char *p = str;
  184. while(*str)
  185. {
  186. if(*str!=c)
  187. *p++ = *str;
  188. str++;
  189. }
  190. *p = '\0';
  191. }
  192. UINT32 location_handle(char *in1)
  193. {
  194. UINT32 location_temp;
  195. UINT32 location_degree;
  196. UINT32 location_min;
  197. location_temp = atol(in1);
  198. location_degree = location_temp/(1e7);
  199. location_degree = location_degree*(1e6);
  200. location_min = location_temp-location_degree*10;
  201. location_min = location_min/6;
  202. location_temp = location_degree+location_min;
  203. return location_temp;
  204. }