AppTaskGps.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /****************************************************************************
  2. *
  3. * Copy right: Qx.Chen jie
  4. * File name: AppTaskGps.c
  5. * Description: Gps处理任务和三轴数据处理及获取
  6. * History: 2021-03-07 2021-07-15
  7. * Version: V3.0
  8. ****************************************************************************/
  9. #include "AppTaskGps.h"
  10. static QueueHandle_t norGpsHandle = NULL;
  11. static osThreadId_t gpsTaskHandle = NULL;
  12. static StaticTask_t gpsTask = NULL;
  13. static UINT8 gpsTaskStack[GPS_TASK_STACK_SIZE];
  14. extern void GsensorInit(void);
  15. extern void GsensorI2CHandler(ARM_I2C_SignalEvent_t cb_event);
  16. extern void GsensorI2CCallback(UINT32 event);
  17. static process_gps gProcess_Gps_Task = PROCESS_GPS_STATE_IDLE;
  18. #define PROC_GPS_STATE_SWITCH(a) (gProcess_Gps_Task = a)
  19. UINT32 location_handle(char *in1);
  20. void strdel(char * str,char c);
  21. static void GpsTask(void* arg)
  22. {
  23. /*三轴加速度初始化*/
  24. GsensorI2CHandler(GsensorI2CCallback);
  25. GsensorInit();
  26. if(GpsRecvHandle == NULL)
  27. {
  28. GpsRecvHandle = osMessageQueueNew(1,sizeof(GPSInfo*), NULL);
  29. }
  30. gpsReqMsg msg;
  31. GPSInfo *GpsInfoData;
  32. char *p=NULL;
  33. CHAR *p2=NULL;
  34. const char *delim = "\n";
  35. char *databuffer[20];
  36. UINT32 speedtemp;
  37. UINT32 latitude;
  38. UINT32 longitude;
  39. UINT16 direction;
  40. UINT16 GpsNoDataCounter = 0;
  41. UINT8 index=0;
  42. PROC_GPS_STATE_SWITCH(PROCESS_GPS_STATE_INIT);
  43. while(1)
  44. {
  45. switch(gProcess_Gps_Task)
  46. {
  47. case PROCESS_GPS_STATE_INIT:
  48. {
  49. osDelay(100);
  50. posGGAServiceStart(norGpsHandle);
  51. posGGAReset();
  52. PROC_GPS_STATE_SWITCH(PROCESS_GPS_STATE_WORK);
  53. break;
  54. }
  55. case PROCESS_GPS_STATE_IDLE:
  56. {
  57. osDelay(100);
  58. if(gProcess_app==WORK)
  59. {
  60. PROC_GPS_STATE_SWITCH(PROCESS_GPS_STATE_WORK);
  61. break;
  62. }
  63. else
  64. {
  65. PROC_GPS_STATE_SWITCH(PROCESS_GPS_STATE_SLEEP);
  66. break;
  67. }
  68. break;
  69. }
  70. case PROCESS_GPS_STATE_WORK:
  71. {
  72. if(GpsNoDataCounter>=60*10)
  73. {
  74. GpsNoDataCounter = 0;
  75. osDelay(100);
  76. posGGAServiceStart(norGpsHandle);
  77. posGGAReset();
  78. }
  79. osStatus_t ret = osMessageQueueGet(norGpsHandle, &msg, 0, 1000);
  80. if(ret==0)
  81. {
  82. if (msg.dataPtr)
  83. {
  84. //char temp[] = "$GNRMC,082626.000,A,2939.91801,N,10637.09500,E,0.543,30.254,261120,,,A,V*17";
  85. GpsInfoData = malloc(sizeof(GPSInfo));
  86. memset(GpsInfoData,0x00,sizeof(GPSInfo));
  87. p = strtok(msg.dataPtr,delim);//将信息进行分割
  88. p2 = strtok(NULL,delim);
  89. #ifdef USING_PRINTF1
  90. printf("\nP msgptr data:%s\r\n",p);
  91. #endif
  92. p = strtok(p,",");//只取第1行的信息RMC
  93. //p = strtok(temp,",");//模拟测试
  94. if (strcmp(p,"$GNRMC")==0)
  95. {
  96. index = 0;
  97. while (p)
  98. {
  99. databuffer[index]=p;
  100. p = strtok(NULL,",");
  101. index++;
  102. }
  103. if (index>5&&strcmp(databuffer[2],"A")==0)
  104. {
  105. GpsNoDataCounter = 0;
  106. GpsInfoData->locateMark = 0x01;//有效,东经,北纬写定
  107. GpsFlag = 3;
  108. strdel(databuffer[3],'.');
  109. strdel(databuffer[5],'.');
  110. strdel(databuffer[7],'.');
  111. speedtemp = atol(databuffer[7])*1852/1e5;//节换算单位,1节=1.852km每小时
  112. GpsInfoData->speed[0] = (speedtemp>>8)&0xFF;
  113. GpsInfoData->speed[1]= speedtemp&0xFF;
  114. latitude =location_handle(databuffer[3]);
  115. GpsInfoData->latitude[0] = latitude>> 24;
  116. GpsInfoData->latitude[1] = latitude>> 16;
  117. GpsInfoData->latitude[2] = latitude>> 8;
  118. GpsInfoData->latitude[3] = latitude;
  119. longitude = location_handle(databuffer[5]);
  120. GpsInfoData->longitude[0] = longitude>>24;
  121. GpsInfoData->longitude[1] = longitude>>16;
  122. GpsInfoData->longitude[2] = longitude>>8;
  123. GpsInfoData->longitude[3] = longitude;
  124. if(speedtemp>=50)//大于5km/h才输出方位
  125. {
  126. direction = atol(databuffer[8]);
  127. GpsInfoData->direction[0] = direction>>8;
  128. GpsInfoData->direction[1] = direction;
  129. }
  130. else
  131. {
  132. GpsInfoData->direction[0] = 0xff;
  133. GpsInfoData->direction[1] = 0xff;
  134. }
  135. if(speedtemp>=30 && speedtemp<=1500 && BattWorkStateDelay==0x01)
  136. {
  137. AppDataInfo.appDataModify = true;
  138. AppDataInfo.AccMileage = speedtemp/36 + AppDataInfo.AccMileage;
  139. if(AppDataInfo.AccMileage>=0xfffffffe)
  140. {
  141. AppDataInfo.AccMileage = 0;
  142. }
  143. }
  144. }
  145. GpsInfoData->AccMileage[0] = AppDataInfo.AccMileage>>24;
  146. GpsInfoData->AccMileage[1] = AppDataInfo.AccMileage>>16;
  147. GpsInfoData->AccMileage[2] = AppDataInfo.AccMileage>>8;
  148. GpsInfoData->AccMileage[3] = AppDataInfo.AccMileage;
  149. }
  150. p2 = strtok(p2,",");//只取第2行的信息GGA
  151. memset(databuffer,0x30,20);
  152. if(strcmp(p2,"$GNGGA")==0)
  153. {
  154. index = 0;
  155. while (p2)
  156. {
  157. databuffer[index]=p2;
  158. p2 = strtok(NULL,",");
  159. index++;
  160. }
  161. if(index>9&&(strcmp(databuffer[6],"1")==0||strcmp(databuffer[6],"2")==0))
  162. {
  163. GpsInfoData->satelliteNum = atol(databuffer[7]);//卫星数目写入
  164. GpsFlag = GpsInfoData->satelliteNum;
  165. strdel(databuffer[9],'.');
  166. UINT16 alt = 0;
  167. alt = atol(databuffer[9])/10 + 1000;
  168. GpsInfoData->altitude[0] = alt>>8;//海拔
  169. GpsInfoData->altitude[1] = alt;
  170. strdel(databuffer[2],'.');
  171. strdel(databuffer[4],'.');
  172. latitude =location_handle(databuffer[2]);
  173. GpsInfoData->latitude[0] = latitude>> 24;
  174. GpsInfoData->latitude[1] = latitude>> 16;
  175. GpsInfoData->latitude[2] = latitude>> 8;
  176. GpsInfoData->latitude[3] = latitude;
  177. longitude = location_handle(databuffer[4]);
  178. GpsInfoData->longitude[0] = longitude>>24;
  179. GpsInfoData->longitude[1] = longitude>>16;
  180. GpsInfoData->longitude[2] = longitude>>8;
  181. GpsInfoData->longitude[3] = longitude;
  182. }
  183. else
  184. {
  185. GpsFlag = 0;
  186. memset(GpsInfoData,0x00,sizeof(GPSInfo));
  187. GpsNoDataCounter++;
  188. }
  189. }
  190. osMessageQueueReset(GpsRecvHandle);
  191. osMessageQueuePut(GpsRecvHandle,&GpsInfoData,0,1000);
  192. }
  193. if(msg.dataPtr)
  194. free(msg.dataPtr);
  195. msg.dataPtr=NULL;
  196. }
  197. PROC_GPS_STATE_SWITCH(PROCESS_GPS_STATE_IDLE);
  198. break;
  199. }
  200. case PROCESS_GPS_STATE_SLEEP:
  201. {
  202. posGGAServiceStop();
  203. while(TRUE)
  204. {
  205. osDelay(100);
  206. if(gProcess_app==WORK)
  207. {
  208. PROC_GPS_STATE_SWITCH(PROCESS_GPS_STATE_INIT);
  209. break;
  210. }
  211. }
  212. break;
  213. }
  214. }
  215. }
  216. }
  217. void AppTaskGpsInit(void *arg)
  218. {
  219. if(norGpsHandle == NULL)
  220. {
  221. norGpsHandle = osMessageQueueNew(1,sizeof(gpsReqMsg), NULL);
  222. if(norGpsHandle == NULL)
  223. return;
  224. }
  225. if(gpsTaskHandle == NULL)
  226. {
  227. osThreadAttr_t task_attr;
  228. memset(&task_attr , 0 , sizeof(task_attr));
  229. task_attr.name = "GPS";
  230. task_attr.priority = osPriorityBelowNormal6;
  231. task_attr.cb_mem = &gpsTask;
  232. task_attr.cb_size = sizeof(StaticTask_t);
  233. task_attr.stack_mem = gpsTaskStack;
  234. task_attr.stack_size =GPS_TASK_STACK_SIZE;
  235. memset(& gpsTaskStack, 0xa5, GPS_TASK_STACK_SIZE);
  236. gpsTaskHandle = osThreadNew(GpsTask , NULL,&task_attr);
  237. if(gpsTaskHandle == NULL)
  238. return;
  239. }
  240. }
  241. void AppTaskGpsDeInit(void *arg)
  242. {
  243. osThreadTerminate(gpsTaskHandle);
  244. gpsTaskHandle = NULL;
  245. }
  246. UINT32 location_handle(char *in1)
  247. {
  248. UINT32 location_temp;
  249. UINT32 location_degree;
  250. UINT32 location_min;
  251. location_temp = atol(in1);
  252. location_degree = location_temp/(1e7);
  253. location_degree = location_degree*(1e6);
  254. location_min = location_temp-location_degree*10;
  255. location_min = location_min/6;
  256. location_temp = location_degree+location_min;
  257. return location_temp;
  258. }
  259. void strdel(char * str,char c)
  260. {
  261. char *p = str;
  262. while(*str)
  263. {
  264. if(*str!=c)
  265. *p++ = *str;
  266. str++;
  267. }
  268. *p = '\0';
  269. }