⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gps.c

📁 一个GPS解码程序
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <stdio.h>
#include "gps.h"
#include "..\\main\\global_para.h"
#include "..\\softuart\\softuart.h"
#include "..\\cygnal\\analog.h"
#include "math.h"
#include "..\\ppro\\ip_task.h"
#include "..\\sim100\\sim100.h"
#define GPSBUFSIZE 100
uchar gps_buf[GPSBUFSIZE];
bit GPSROK;                                                         //
uchar gps_rf=0;     //receive GPS data flag  ///////////////
uchar gps_rp=0;                                           //
                                //
S_GPS gps_Hold;
S_GPS gps_Hold1;
    
//S_GPS LastPoint;//for PLA
//S_GPS LastPointL;//for length trace

double carspeed=0;

INT16U indin,indout;
INT8U  ifull;
void GPSReceive(uchar tmpl)
{
  static uchar gps_rf,gps_rp;
  switch(gps_rf)
  {
      case 0:
	     if(tmpl=='$')gps_rf=1;
	     break;
	  case 1:
	     if(tmpl=='G')gps_rf=2;
		 else if(tmpl==0xA0)break;
		 else gps_rf=0;
	     break;
	  case 2:
	     if(tmpl=='P')gps_rf=3;
		 else gps_rf=0;
		 break;
	  case 3:
	     if(tmpl=='R')gps_rf=4;
		 else gps_rf=0;
		 break;
	  case 4:
	     if(tmpl=='M')gps_rf=5;
		 else gps_rf=0;
		 break;
	  case 5: /*receive latitude and longitude */
	     if(tmpl=='C')gps_rf=6;
         else gps_rf=0;
         break;
      case 6://skip ,
         if(tmpl==','){gps_rf=7;gps_rp=0;}
         else gps_rf=0;
         break;
      case 7://receive data
         if(tmpl==0x0D)
		 {
           gps_buf[gps_rp]=0;
           gps_rf=0;
		   SW_UART_DISABLE0();
           OSSemPost(Sem_GPSReceived);
         }
         else
		 {
		   gps_buf[gps_rp++]=tmpl;
		   if(gps_rp>=GPSBUFSIZE-1)gps_rf=0;
		 }
		 break;
      default:gps_rf=0;
  }
}

double sdouble(uchar **add)reentrant     /*scan a double */
{
  double  dtmp,j;
  uchar   i;
  uchar  *n;
  n=*add;
  dtmp=0;
  j=10;
  i=*n++;
  if(i!='.' && i!=',')
  {
    while(1)
	{
      dtmp=dtmp*10+i-0x30;
      i=*n++;
	  if(i=='.' || i==',')break;
	}
	i=*n++;
	if(i!=',')
	while(1)
	{
	  dtmp=dtmp+((double)(i-0x30))/j;
	  i=*n++;
	  if(i==',')break;
	  j*=10;
	}
  }
  *add=n;
  return dtmp;
}
//get a long int from a string 2digits or 3digits
//m=2 or 3;
unsigned long slong(uchar **add,uchar m)reentrant /*scan a long int*/
{ 
  unsigned long sdd;
  uchar  *n;
  uchar  c;
  n=*add;
  if(*n==',')return 0; /* if is ',', then return 0*/
  while(*n==' ' || !(*n<=0x39 && *n>=0x30))n++;
  c=*n++;
  sdd=c-0x30;
  m--;
  while(m && *n>=0x30 && *n<=0x39)
  {
    c=*n++;
    sdd=sdd*10+c-0x30;
	m--;
  }
  *add=n;
  return sdd;
}

char code Days[]	= {0,31,28,31,30,31,30,31,31,30,31,30,31};
char code Days1[]   = {0,31,29,31,30,31,30,31,31,30,31,30,31};

void utc2beijing(struct GPSSAVE *Hold)reentrant
{    
    char *nDays=Days;
    uint Year	= 2000+Hold->year;

	//=======================================================================
	// Created: 2006年04月23日 09时38分43秒 
	// Author:  Johnson Wen 
	// Comment: 预处理不可预料异常
	//=======================================================================
	Hold->month	%= 13;if(Hold->month==0)return;
	Hold->day	%= 32;if(Hold->day==0)return;
	//Hold.hour	%= 24;if(Hold.hour==0)return;

	//=======================================================================
	// Created: 2006年04月23日 09时34分07秒 
	// Author:  Johnson Wen 
	// Comment: 处理闰年
	//=======================================================================
	if(Year%4==0&&Year%100!=0||Year%400==0)
		nDays=Days1;
	
	//=======================================================================
	// Created: 2006年04月23日 09时34分22秒 
	// Author:  Johnson Wen 
	// Comment: 处理格林威治时间,没有跨时段
	//=======================================================================
	Hold->hour	+= 8;
	if(Hold->hour<24)
		return;

	//=======================================================================
	// Created: 2006年04月23日 09时35分30秒 
	// Author:  Johnson Wen 
	// Comment: 处理跨时段
	//=======================================================================
	Hold->hour	%= 24;

	//=======================================================================
	// Created: 2006年04月23日 09时36分53秒 
	// Author:  Johnson Wen 
	// Comment: 处理跨日期
	//=======================================================================
	Hold->day	+= 1;
	if(Hold->day<=nDays[Hold->month])
		return;

	//=======================================================================
	// Created: 2006年04月23日 09时43分27秒 
	// Author:  Johnson Wen 
	// Comment: 处理跨月份
	//=======================================================================
	if(Hold->month+1<=12)
	{
		Hold->day-= nDays[Hold->month];
		return;
	}

	//=======================================================================
	// Created: 2006年04月23日 09时50分22秒 
	// Author:  Johnson Wen 
	// Comment: 处理跨年份
	//=======================================================================
	Hold->day-= nDays[1];
	Hold->month	= 1;
	Hold->year	+= 1;
	return;
}

//uchar code grmc[]="161229.487,A,3723.2475,N,12158.3416,S,0.13,309.62,120598,,*10";
//test gpschange
uchar CheckSumGps()reentrant
{
  uchar i,len,sum1;
  int sum;
  if(!StrStr(gps_buf,",A,"))return 0;
  len=StrLen(gps_buf);
  if(len>sizeof(gps_buf)-2)return 0;
  if(len<35)return 0;
  for(i=0;i<len;i++)//is there a '*'?
  {
    if(gps_buf[i]=='*')break;
  }  
  if(i>=len)return 0;
  len=i;
  OS_ENTER_CRITICAL();
  sscanf(gps_buf+i+1,"%x",&sum);
  OS_EXIT_CRITICAL();
  sum1='G'^'P'^'R'^'M'^'C'^',';
  for(i=0;i<len;i++)
  sum1^=gps_buf[i];
  return (sum==sum1);
}

uint SpeedFilter(uint Speed)reentrant
{
  static uint preSpeed=0;  //前一秒的速度
  static INT8U expTimes=0;  //超过前一秒速度太大时的时间
  static INT8U TimesNotLocate=0;
  if(led3==0) //不定位时,速度置为上一个速度,但不定位时间不应大于是10秒,否则速度置为0.
  {
    if(++TimesNotLocate>10)
    {
      TimesNotLocate = 100; //不致溢出.
      preSpeed=0;
    }
    return preSpeed;
  }
  TimesNotLocate=0;//定位时,置为0
  if(abs((int)Speed-(int)preSpeed)<25)
  {
    preSpeed = Speed;
    expTimes=0;
    return Speed;
  }
  if (++expTimes > 10)//加速太快,需要过滤,
  {
     preSpeed = Speed;
     expTimes=0;
     return Speed;
  }
  led3=0;
  return preSpeed;
}
uchar gpschange(S_GPS *sgps)reentrant
{
  unsigned long  ltmp;
  double  dtmp;
  uchar  *i;
  uchar ctmp;
  ctmp=(gps_buf[0]-0x30)*10+(gps_buf[1]-0x30);
  if(ctmp<60)sgps->hour=ctmp;else return false;
  ctmp=(gps_buf[2]-0x30)*10+(gps_buf[3]-0x30);
  if(ctmp<60)sgps->min=ctmp;else return false;
  ctmp=(gps_buf[4]-0x30)*10+(gps_buf[5]-0x30);
  if(ctmp<60)sgps->sec=ctmp;else return false;
  i=gps_buf;
  while(*i!=',')i++;
  i++;
  if(*i=='A')led3=1;else led3=0;
  sNET=!led2;
  sgps->BitState=MobileEvent;
  //else return false;
  while(*i!=',')i++;if(*i==',' && *(i+1)==',' && *(i+2)==',')return false;
  i++;
  ltmp=slong(&i,2);
  dtmp=sdouble(&i);
  dtmp=dtmp/60+ltmp;
  if(dtmp>90)return false;
  if(*i++=='S')dtmp=0-dtmp;
  sgps->latitude=dtmp/90*324000000; /*tanslate to motorola MGT format*/
  i++;
  ltmp=slong(&i,3);
  dtmp=sdouble(&i);
  dtmp=dtmp/60+ltmp;
  if(dtmp>=180)return false;
  if(*i++=='W')dtmp=0-dtmp;
  sgps->longitude=dtmp/180*648000000;
  i++; /*skip ,*/
  dtmp=sdouble(&i);
  if(IN_ACC && !pla_sta)dtmp=0;    //
  if(dtmp<2)dtmp=0;
  dtmp=dtmp*1.852;/*translate the knots to cm/s */
  carspeed=dtmp;
  sgps->speed=SpeedFilter(dtmp);//dtmp;
  dtmp=sdouble(&i);	 /*get direction*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -