GpsData.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #include "includes.h"
  2. SUT_GPS_INF sutGpsInfo;
  3. SUT_MESS sutMess;
  4. const unsigned short GPS_TimeTable[GPS_TABLE_NUM]={0,5,10,15,30,60,120,300};
  5. static unsigned char gpsInit=0;
  6. void GPSRestart(void){
  7. gpsInit=1;
  8. }
  9. void GPSStop(void){
  10. gpsInit=10;
  11. }
  12. void GPSInit(void){
  13. static unsigned int tick;
  14. GPIO_InitTypeDef GPIO_InitStructure;
  15. if(gpsInit==0) return;
  16. else if(gpsInit==1){
  17. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  18. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  19. GPIO_InitStructure.GPIO_Pin = GPS_PWREN_PIN;
  20. GPIO_Init(GPS_PWREN_PORT, &GPIO_InitStructure);
  21. SlwTrace(INF,"GPS Down...",1);
  22. GPS_PWREN_LOW;
  23. sutGpsInfo.isGpsWork=0;
  24. sutGpsInfo.isGpsValid=0;
  25. sutGpsInfo.GpsInactiveCt=0;
  26. uTimerStart(&tick, 50);
  27. gpsInit=2;
  28. }else if(gpsInit==2){
  29. if(uTimerExpired(&tick)){
  30. GPS_PWREN_HIGH;
  31. SlwTrace(INF,"GPS Start Done",1);
  32. gpsInit=0;
  33. }
  34. }else if(gpsInit==10){
  35. GPS_PWREN_LOW;
  36. SlwTrace(INF,"GPS Down",1);
  37. sutGpsInfo.isGpsWork=0;
  38. sutGpsInfo.isGpsValid=0;
  39. gpsInit=0;
  40. }
  41. }
  42. static void GpsDataProcess(void);
  43. PT_THREAD (ptGpsTask(struct pt *pt)){
  44. static timer_t ptTimer;
  45. static short GpsSendCt=0;
  46. char buf[70];
  47. if(g_ucModemTaskEn==0)return 1;//w
  48. //GPSInit();
  49. PT_BEGIN(pt);
  50. while(1){
  51. //GpsDataProcess();
  52. GpsInternalChange();
  53. if(sutGpsInfo.isGpsValid && g_uiGpsStat&&GpsSendCt>GPS_TimeTable[newPara.gpsTimeIndex]&&hgs_poc_type==POC_BND){
  54. //AT+POC=loc,22.23211,103.123121,1,23.4,12.5,4,2016/12/29-13:28:15
  55. GpsSendCt=0;
  56. snprintf(buf,sizeof(buf),"AT+POC=loc,%d.%d,%d.%d,%d,23.4,12.5,4,%d/%d/%d-%d:%d:%d\r\n",sutGpsInfo.latitue/100000,sutGpsInfo.latitue-((sutGpsInfo.latitue/100000)*100000),sutGpsInfo.longitue/100000,sutGpsInfo.longitue-((sutGpsInfo.longitue/100000)*100000),\
  57. 1,sutGpsInfo.year,sutGpsInfo.month,sutGpsInfo.day,sutGpsInfo.hour,sutGpsInfo.minu,sutGpsInfo.sec);
  58. ModemSendAt(buf);
  59. printf("GPS SEND=%s\r\n",buf);
  60. }
  61. PTTimerStart(&pt_timerPool, &ptTimer, 100);//GPS数据1秒才出一条RMC,0.5秒处理一次足够了 //50
  62. PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer));
  63. }
  64. PT_END(pt);
  65. }
  66. void GpsInternalChange()
  67. {
  68. static unsigned char cnt=2;
  69. static unsigned char last_uiGpsStat=2;
  70. if(last_uiGpsStat!=g_uiGpsStat){
  71. cnt=0;
  72. last_uiGpsStat=g_uiGpsStat;
  73. }
  74. switch(cnt)
  75. {
  76. case 0:
  77. if(g_uiGpsStat){
  78. ModemSendAt("AT+GPS=\"ON\"\r\n");
  79. ModemSendAt("AT+VIBCTRL=\"POWER\",\"ON\"\r\n");
  80. printf("GPS open--change--r\n");
  81. }
  82. else {
  83. ModemSendAt("AT+GPS=\"OFF\"\r\n");
  84. ModemSendAt("AT+VIBCTRL=\"POWER\",\"OFF\"\r\n");
  85. }
  86. cnt=1;
  87. break;
  88. case 1:
  89. if(g_uiGpsStat)ModemSendAt("AT+GPSANT=1\r\n");
  90. else ModemSendAt("AT+GPSANT=0\r\n");
  91. cnt=2;
  92. break;
  93. }
  94. }
  95. static void GpsDataProcess(void)
  96. {
  97. char *nema=(char *)RxBuffer3;
  98. short len;
  99. if(g_usUart3RecvLen){
  100. len=strlen(&nema[7]);
  101. if(len>(UART3_RX_BUFFER_SIZE-10)){
  102. SlwTrace(INF,"UART2RECV OVER!",1);
  103. return;
  104. }
  105. process_gps_data(&nema[7],len);
  106. sutGpsInfo.isGpsWork=1;
  107. sutGpsInfo.GpsInactiveCt=0;
  108. ////////////////////////////////////////////////
  109. g_usUart3RecvLen=0;
  110. }
  111. }
  112. static unsigned char translate_digitAscii_to_bcd(char *ascii_number, unsigned char len, char *bcd_number)
  113. {
  114. int i, j;
  115. char temp[10] = ""; /* extra + and NULL */
  116. unsigned char bcd_index = 0; /* Index into output bcd_number */
  117. int num_len = len;
  118. /* Translate or skip over invalid characters */
  119. for (i = 0, j = 0; i < num_len; i++)
  120. {
  121. temp[j++] = ascii_number[i] & 0x0F;
  122. }
  123. ascii_number[len] = 0;
  124. /* Now that temp has the bcd codes in natural order... Squish them together
  125. * and reverse the order per bcd coding.
  126. */
  127. for (i = 0; i < j; i+=2)
  128. bcd_number[bcd_index++] = ((char)(temp[i] << 4)) | temp[i+1];
  129. bcd_number[len] = 0;
  130. return 0;
  131. }
  132. /*=====================================
  133. UTC time:hms status latitue north? longitue east? 速度(节) 方位角 UTC time 磁偏角 方向 模式 checksum
  134. $GPRMC hhmmss.sss A/V ddmm.mmmm N/S dddmm.mmmm E/W speed aspect ddmmyy 000-180 E/W A/D/E/N *hh
  135. For example: $GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A*50
  136. */
  137. #define GPRMC_UTC1_TOKEN_INDEX 0
  138. #define GPRMC_STATUS_TOKEN_INDEX 1
  139. #define GPRMC_LATITUE_TOKEN_INDEX 2
  140. #define GPRMC_NORTH_TOKEN_INDEX 3
  141. #define GPRMC_longitue_TOKEN_INDEX 4
  142. #define GPRMC_EAST_TOKEN_INDEX 5
  143. #define GPRMC_SPEED_TOKEN_INDEX 6
  144. #define GPRMC_ASPECT_TOKEN_INDEX 7
  145. #define GPRMC_UTC2_TOKEN_INDEX 8
  146. /*************************************************************************************
  147. *
  148. **************************************************************************************/
  149. void process_gps_data(char *buf,uint32_t size) //这里的buf就是从GPRMC,后开始
  150. {
  151. char tmp_char[16] = {0};
  152. char bcd_tmp[8] = {0};
  153. char *token[16] = {NULL};
  154. char size_each_token[16] = {0};
  155. unsigned char hh,hl;
  156. unsigned char token_index = 0;
  157. int i = 0;
  158. int index = 0;
  159. int dig_bit_num = 0;
  160. int ptr_each = 0;
  161. unsigned short speed;
  162. unsigned short aspect;
  163. // char* g_cGetlongitude=NULL;
  164. // char* g_cGetlatitude=NULL;
  165. double speed_jie = 0.0;
  166. token[token_index] = buf;
  167. //RunMake(THIS_FILE_ID);
  168. while(index < size )
  169. {
  170. if(buf[index] == ',')
  171. {
  172. size_each_token[token_index] = ptr_each;
  173. if(ptr_each == 0)
  174. token[token_index] = NULL;
  175. //*(token[token_index] + ptr_each) = 0; //
  176. ptr_each = 0;
  177. token_index++;
  178. token[token_index] = buf+index+1; //skip this ','
  179. if(*(token[token_index]+1) == '*')
  180. {
  181. size_each_token[token_index] = 1; //cut of *hh\r\n
  182. }
  183. }
  184. else
  185. {
  186. ptr_each++;
  187. }
  188. index++;
  189. }
  190. //RunMake(THIS_FILE_ID);
  191. //UTC time:hms
  192. if(token[GPRMC_UTC1_TOKEN_INDEX])
  193. {
  194. strncpy((char*)tmp_char,(char*)token[0],size_each_token[0]);
  195. translate_digitAscii_to_bcd(tmp_char,6,bcd_tmp); //去掉.xx
  196. //小时需要加8 因为北京是东8区
  197. hl=bcd_tmp[0]&0x0f;
  198. hh=bcd_tmp[0]&0xf0;
  199. hl+=8;
  200. if(hl>=10){
  201. hl-=10;
  202. hh+=0x10;
  203. //if(hh>0x20)hh=0; 处理GPS时间问题 V520_P1
  204. }
  205. sutGpsInfo.hour=(hh | hl);
  206. sutGpsInfo.minu = bcd_tmp[1];
  207. sutGpsInfo.sec = bcd_tmp[2];
  208. }
  209. //RunMake(THIS_FILE_ID);
  210. //Fix status
  211. if(token[GPRMC_STATUS_TOKEN_INDEX])
  212. {
  213. memset(tmp_char , 0 ,sizeof(tmp_char));
  214. strncpy((char*)tmp_char,(char*)token[1],size_each_token[1]);
  215. //if(*tmp_char == 'A'){
  216. if(*tmp_char == 'A'){
  217. sutGpsInfo.isGpsValid = 1;
  218. }else{
  219. sutGpsInfo.isGpsValid = 0;
  220. }
  221. }
  222. //RunMake(THIS_FILE_ID);
  223. if(sutGpsInfo.isGpsValid == 0)return;
  224. //latitude 纬度
  225. /*=====================================
  226. UTC time:hms status latitue north? longitue east? 速度(节) 方位角 UTC time 磁偏角 方向 模式 checksum
  227. $GPRMC hhmmss.ss A/V ddmm.mmmm N/S dddmm.mmmm E/W speed aspect ddmmyy 000-180 E/W A/D/E/N *hh
  228. For example: $GPRMC,024813.640,A,3158.4608,N,11848.3737,E,010.5,324.7,150706,,,A*50
  229. */
  230. if(token[GPRMC_LATITUE_TOKEN_INDEX])
  231. {
  232. //RunMake(THIS_FILE_ID);
  233. memset(tmp_char , 0 ,sizeof(tmp_char));
  234. memset(bcd_tmp , 0 ,sizeof(bcd_tmp));
  235. strncpy((char*)(tmp_char+1),(char*)token[2],size_each_token[2]);
  236. tmp_char[0] = '0';
  237. //RunMake(THIS_FILE_ID);
  238. for(i = 5; i < 8;i++)
  239. tmp_char[i] = tmp_char[i+1];
  240. tmp_char[8] = 0; //小数点后面去掉最后1位
  241. translate_digitAscii_to_bcd(tmp_char,8,bcd_tmp); //去掉.xx
  242. //North or south
  243. memset(tmp_char , 0 ,sizeof(tmp_char));
  244. if(token[GPRMC_NORTH_TOKEN_INDEX])
  245. {
  246. strncpy((char*)tmp_char,(char*)token[3],size_each_token[3]);
  247. // if(*tmp_char == 'S')
  248. // bcd_tmp[0] |= 0x80;
  249. sutGpsInfo.NSFlag=*tmp_char;
  250. sutGpsInfo.latitue = (bcd_tmp[3] | (bcd_tmp[2] << 8) |(bcd_tmp[1] << 16) |(bcd_tmp[0] << 24));
  251. }
  252. //RunMake(THIS_FILE_ID);
  253. }
  254. //longitude 经度
  255. if(token[GPRMC_longitue_TOKEN_INDEX])
  256. {
  257. memset(tmp_char , 0 ,sizeof(tmp_char));
  258. memset(bcd_tmp , 0 ,sizeof(bcd_tmp));
  259. //RunMake(THIS_FILE_ID);
  260. strncpy((char*)tmp_char,(char*)token[4],size_each_token[4]);
  261. for(i = 5; i < 8;i++)
  262. tmp_char[i] = tmp_char[i+1];
  263. tmp_char[8] = 0;
  264. translate_digitAscii_to_bcd(tmp_char,8,bcd_tmp); //去掉.xx
  265. //RunMake(THIS_FILE_ID);
  266. //east or west
  267. if(token[GPRMC_EAST_TOKEN_INDEX])
  268. {
  269. memset(tmp_char , 0 ,sizeof(tmp_char));
  270. strncpy((char*)tmp_char,(char*)token[5],size_each_token[5]);
  271. // if(*tmp_char == 'W')
  272. // bcd_tmp[0] |= 0x80;
  273. sutGpsInfo.EWFlag=*tmp_char;
  274. sutGpsInfo.longitue = (bcd_tmp[3] | (bcd_tmp[2] << 8) |(bcd_tmp[1] << 16) |(bcd_tmp[0] << 24));
  275. }
  276. }
  277. //Speed
  278. if(token[GPRMC_SPEED_TOKEN_INDEX])
  279. {
  280. //RunMake(THIS_FILE_ID);
  281. memset(tmp_char , 0 ,sizeof(tmp_char));
  282. strncpy((char*)tmp_char,(char*)token[GPRMC_SPEED_TOKEN_INDEX],size_each_token[GPRMC_SPEED_TOKEN_INDEX]);
  283. index = 0;
  284. while(index < size_each_token[GPRMC_SPEED_TOKEN_INDEX])
  285. {
  286. if(tmp_char[index] == '.')
  287. break;
  288. index++;
  289. }
  290. dig_bit_num = size_each_token[GPRMC_SPEED_TOKEN_INDEX]-index-1;
  291. //这里,index代表小数点前有多少位,dig_bit_num代表小数点后面有多少位
  292. //RunMake(THIS_FILE_ID);
  293. //ol_debug("dig_bit_num:%d,all len:%d",dig_bit_num,size_each_token[GPRMC_SPEED_TOKEN_INDEX]);
  294. #if 0
  295. if(index == 3)
  296. {
  297. speed_jie = (tmp_char[0]-0x30)*100
  298. + (tmp_char[1]-0x30)*10
  299. + (tmp_char[2]-0x30);
  300. }
  301. else if(index == 2)
  302. {
  303. speed_jie = (tmp_char[0]-0x30)*10
  304. + (tmp_char[1]-0x30);
  305. }
  306. else
  307. speed_jie = (tmp_char[0]-0x30);
  308. if(dig_bit_num > 0)
  309. speed_jie += (tmp_char[index+1]-0x30)*0.1;
  310. #else
  311. speed_jie = atoi(tmp_char);
  312. if(dig_bit_num>0)
  313. speed_jie += (tmp_char[index+1]-0x30)*0.1;
  314. #endif
  315. speed = (unsigned short)(speed_jie*1.852);
  316. sutGpsInfo.speed = ( ((speed/100)<<8) | (((speed /10)%10)<<4) | (speed %10) );
  317. //ol_debug("speed_jie to speed:%d,xinan speed:%d",speed,sutGpsInfo.speed);
  318. }
  319. //RunMake(THIS_FILE_ID);
  320. //Aspect 方向
  321. if(token[GPRMC_ASPECT_TOKEN_INDEX])
  322. {
  323. memset(tmp_char , 0 ,sizeof(tmp_char));
  324. strncpy((char*)tmp_char,(char*)token[GPRMC_ASPECT_TOKEN_INDEX],size_each_token[GPRMC_ASPECT_TOKEN_INDEX]);
  325. #if 0
  326. aspect = (unsigned short)((tmp_char[0]-0x30)*100
  327. + (tmp_char[1]-0x30)*10
  328. + (tmp_char[2]-0x30));
  329. //aspect = 150;
  330. sutGpsInfo.aspect= ( ((aspect/100) << 8) | (((aspect /10)%10)<<4) | (aspect %10) );
  331. #else
  332. aspect=atoi(tmp_char);
  333. aspect %=360;
  334. sutGpsInfo.aspect= ( ((aspect/100) << 8) | (((aspect /10)%10)<<4) | (aspect %10) );
  335. #endif
  336. }
  337. //RunMake(THIS_FILE_ID);
  338. //UTC2
  339. if(token[GPRMC_UTC2_TOKEN_INDEX])
  340. {
  341. memset(tmp_char , 0 ,sizeof(tmp_char));
  342. memset(bcd_tmp , 0 ,sizeof(bcd_tmp));
  343. strncpy((char*)tmp_char,(char*)token[GPRMC_UTC2_TOKEN_INDEX],size_each_token[GPRMC_UTC2_TOKEN_INDEX]);
  344. translate_digitAscii_to_bcd(tmp_char,6,bcd_tmp);
  345. sutGpsInfo.day = bcd_tmp[0];
  346. sutGpsInfo.month= bcd_tmp[1];
  347. sutGpsInfo.year = bcd_tmp[2];
  348. }
  349. }