GpsData.c 10 KB

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