AppTaskGps.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. char *p = NULL;
  32. CHAR *p2 = NULL;
  33. const char *delim = "\n";
  34. char *databuffer[20];
  35. UINT32 speedtemp;
  36. UINT32 latitude;
  37. UINT32 longitude;
  38. UINT16 direction;
  39. UINT16 GpsNoDataCounter = 0;
  40. UINT8 index = 0;
  41. UINT8 xyzCounter = 0;
  42. INT16 xData[3] = {0};
  43. INT16 yData[3] = {0};
  44. INT16 zData[3] = {0};
  45. INT16 xyzDataBuffer[3] = {0};
  46. GPSInfo GpsInfoData;
  47. PROC_GPS_STATE_SWITCH(PROCESS_GPS_STATE_INIT);
  48. while (1)
  49. {
  50. switch (gProcess_Gps_Task)
  51. {
  52. case PROCESS_GPS_STATE_INIT:
  53. {
  54. osDelay(100);
  55. posGGAServiceStart(norGpsHandle);
  56. posGGAReset();
  57. PROC_GPS_STATE_SWITCH(PROCESS_GPS_STATE_WORK);
  58. break;
  59. }
  60. case PROCESS_GPS_STATE_IDLE:
  61. {
  62. osDelay(100);
  63. if (gProcess_app == WORK)
  64. {
  65. PROC_GPS_STATE_SWITCH(PROCESS_GPS_STATE_WORK);
  66. break;
  67. }
  68. else
  69. {
  70. PROC_GPS_STATE_SWITCH(PROCESS_GPS_STATE_SLEEP);
  71. break;
  72. }
  73. break;
  74. }
  75. case PROCESS_GPS_STATE_WORK:
  76. {
  77. if (GpsNoDataCounter >= 60 * 10)
  78. {
  79. memset((UINT8 *)&GpsInfoData, 0x00, sizeof(GPSInfo));
  80. GpsNoDataCounter = 0;
  81. osDelay(100);
  82. posGGAServiceStart(norGpsHandle);
  83. posGGAReset();
  84. }
  85. osStatus_t ret = osMessageQueueGet(norGpsHandle, &msg, 0, 1000);
  86. if (ret == 0)
  87. {
  88. if (msg.dataPtr)
  89. {
  90. // char temp[] = "$GNRMC,082626.000,A,2939.91801,N,10637.09500,E,0.543,30.254,261120,,,A,V*17";
  91. memset((UINT8 *)&GpsInfoData, 0x00, sizeof(GPSInfo));
  92. p = strtok(msg.dataPtr, delim); //将信息进行分割
  93. p2 = strtok(NULL, delim);
  94. #ifdef USING_PRINTF1
  95. printf("\nP msgptr data:%s\r\n", p);
  96. #endif
  97. p = strtok(p, ","); //只取第1行的信息RMC
  98. // p = strtok(temp,",");//模拟测试
  99. if (strcmp(p, "$GNRMC") == 0)
  100. {
  101. index = 0;
  102. while (p)
  103. {
  104. databuffer[index] = p;
  105. p = strtok(NULL, ",");
  106. index++;
  107. }
  108. if (index > 5 && strcmp(databuffer[2], "A") == 0)
  109. {
  110. GpsNoDataCounter = 0;
  111. GpsInfoData.locateMark = 0x01; //有效,东经,北纬写定
  112. GpsFlag = 3;
  113. strdel(databuffer[3], '.');
  114. strdel(databuffer[5], '.');
  115. strdel(databuffer[7], '.');
  116. speedtemp = atol(databuffer[7]) * 1852 / 1e5; //节换算单位,1节=1.852km每小时
  117. GpsInfoData.speed[0] = (speedtemp >> 8) & 0xFF;
  118. GpsInfoData.speed[1] = speedtemp & 0xFF;
  119. latitude = location_handle(databuffer[3]);
  120. GpsInfoData.latitude[0] = latitude >> 24;
  121. GpsInfoData.latitude[1] = latitude >> 16;
  122. GpsInfoData.latitude[2] = latitude >> 8;
  123. GpsInfoData.latitude[3] = latitude;
  124. longitude = location_handle(databuffer[5]);
  125. GpsInfoData.longitude[0] = longitude >> 24;
  126. GpsInfoData.longitude[1] = longitude >> 16;
  127. GpsInfoData.longitude[2] = longitude >> 8;
  128. GpsInfoData.longitude[3] = longitude;
  129. if (speedtemp >= 50) //大于5km/h才输出方位
  130. {
  131. direction = atol(databuffer[8]);
  132. GpsInfoData.direction[0] = direction >> 8;
  133. GpsInfoData.direction[1] = direction;
  134. }
  135. else
  136. {
  137. GpsInfoData.direction[0] = 0xff;
  138. GpsInfoData.direction[1] = 0xff;
  139. }
  140. if (speedtemp >= 30 && speedtemp <= 1500 && BattWorkStateDelay == 0x01)
  141. {
  142. AppDataInfo.appDataModify = true;
  143. AppDataInfo.AccMileage = speedtemp / 36 + AppDataInfo.AccMileage;
  144. if (AppDataInfo.AccMileage >= 0xfffffffe)
  145. {
  146. AppDataInfo.AccMileage = 0;
  147. }
  148. }
  149. }
  150. GpsInfoData.AccMileage[0] = AppDataInfo.AccMileage >> 24;
  151. GpsInfoData.AccMileage[1] = AppDataInfo.AccMileage >> 16;
  152. GpsInfoData.AccMileage[2] = AppDataInfo.AccMileage >> 8;
  153. GpsInfoData.AccMileage[3] = AppDataInfo.AccMileage;
  154. }
  155. p2 = strtok(p2, ","); //只取第2行的信息GGA
  156. memset(databuffer, 0x30, 20);
  157. if (strcmp(p2, "$GNGGA") == 0)
  158. {
  159. index = 0;
  160. while (p2)
  161. {
  162. databuffer[index] = p2;
  163. p2 = strtok(NULL, ",");
  164. index++;
  165. }
  166. if (index > 9 && (strcmp(databuffer[6], "1") == 0 || strcmp(databuffer[6], "2") == 0))
  167. {
  168. GpsInfoData.satelliteNum = atol(databuffer[7]); //卫星数目写入
  169. GpsFlag = GpsInfoData.satelliteNum;
  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. else
  189. {
  190. GpsFlag = 0;
  191. GpsNoDataCounter++;
  192. }
  193. }
  194. osMessageQueueReset(GpsRecvHandle);
  195. osMessageQueuePut(GpsRecvHandle, (UINT8 *)&GpsInfoData, 0, 1000);
  196. }
  197. if (msg.dataPtr)
  198. free(msg.dataPtr);
  199. msg.dataPtr = NULL;
  200. }
  201. if (getbit(PadInterrupt, 3) == 1 || getbit(PadInterrupt, 4) == 1 || TimeCounter % 50 == 0)
  202. {
  203. //SL_SC7A20_Reg_read_all();
  204. UINT8 ret = SL_SC7A20_Read_XYZ_Data(xyzDataBuffer);
  205. xData[xyzCounter] = xyzDataBuffer[0];
  206. yData[xyzCounter] = xyzDataBuffer[1];
  207. zData[xyzCounter] = xyzDataBuffer[2];
  208. xyzCounter++;
  209. if (xyzCounter > 2)
  210. {
  211. xyzCounter = 0;
  212. }
  213. xyzData[0] = (xData[0] + xData[1] + xData[2]) / 3;
  214. xyzData[1] = (yData[0] + yData[1] + yData[2]) / 3;
  215. xyzData[2] = (zData[0] + zData[1] + zData[2]) / 3;
  216. #ifdef USING_PRINTF
  217. printf("\n%d %d %d \r\n", xyzData[0], xyzData[1], xyzData[2]);
  218. #endif
  219. }
  220. PROC_GPS_STATE_SWITCH(PROCESS_GPS_STATE_IDLE);
  221. break;
  222. }
  223. case PROCESS_GPS_STATE_SLEEP:
  224. {
  225. posGGAServiceStop();
  226. while (TRUE)
  227. {
  228. osDelay(100);
  229. if (gProcess_app == WORK)
  230. {
  231. PROC_GPS_STATE_SWITCH(PROCESS_GPS_STATE_INIT);
  232. break;
  233. }
  234. }
  235. break;
  236. }
  237. }
  238. }
  239. }
  240. void AppTaskGpsInit(void *arg)
  241. {
  242. if (norGpsHandle == NULL)
  243. {
  244. norGpsHandle = osMessageQueueNew(1, sizeof(gpsReqMsg), NULL);
  245. if (norGpsHandle == NULL)
  246. return;
  247. }
  248. if (gpsTaskHandle == NULL)
  249. {
  250. osThreadAttr_t task_attr;
  251. memset(&task_attr, 0, sizeof(task_attr));
  252. task_attr.name = "GPSTask";
  253. task_attr.priority = osPriorityBelowNormal6;
  254. task_attr.cb_mem = &gpsTask;
  255. task_attr.cb_size = sizeof(StaticTask_t);
  256. task_attr.stack_mem = gpsTaskStack;
  257. task_attr.stack_size = GPS_TASK_STACK_SIZE;
  258. memset(&gpsTaskStack, 0xa5, GPS_TASK_STACK_SIZE);
  259. gpsTaskHandle = osThreadNew(GpsTask, NULL, &task_attr);
  260. if (gpsTaskHandle == NULL)
  261. return;
  262. }
  263. }
  264. void AppTaskGpsDeInit(void *arg)
  265. {
  266. osThreadTerminate(gpsTaskHandle);
  267. gpsTaskHandle = NULL;
  268. }
  269. UINT32 location_handle(char *in1)
  270. {
  271. UINT32 location_temp;
  272. UINT32 location_degree;
  273. UINT32 location_min;
  274. location_temp = atol(in1);
  275. location_degree = location_temp / (1e7);
  276. location_degree = location_degree * (1e6);
  277. location_min = location_temp - location_degree * 10;
  278. location_min = location_min / 6;
  279. location_temp = location_degree + location_min;
  280. return location_temp;
  281. }
  282. void strdel(char *str, char c)
  283. {
  284. char *p = str;
  285. while (*str)
  286. {
  287. if (*str != c)
  288. *p++ = *str;
  289. str++;
  290. }
  291. *p = '\0';
  292. }