20260324
This commit is contained in:
353
IAPV1.1/Driver/L76X/L76X.c
Normal file
353
IAPV1.1/Driver/L76X/L76X.c
Normal file
@ -0,0 +1,353 @@
|
||||
#include "L76X.h"
|
||||
#include "usart.h"
|
||||
#include <math.h>
|
||||
_SaveData Save_Data;
|
||||
|
||||
static const double pi = 3.14159265358979324;
|
||||
static const double a = 6378245.0;
|
||||
static const double ee = 0.00669342162296594323;
|
||||
static const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
|
||||
static double Lat_f,Lon_f;
|
||||
|
||||
|
||||
|
||||
void L76X_POWER_ON(void)
|
||||
{
|
||||
PWR_CTRLL76C_H;
|
||||
}
|
||||
|
||||
void L76X_POWER_OFF(void)
|
||||
{
|
||||
PWR_CTRLL76C_L;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
function:
|
||||
Latitude conversion
|
||||
******************************************************************************/
|
||||
static double transformLat(double x,double y)
|
||||
{
|
||||
double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 *sqrt(abs(x));
|
||||
ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
|
||||
ret += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0;
|
||||
ret += (160.0 * sin(y / 12.0 * pi) + 320 * sin(y * pi / 30.0)) * 2.0 / 3.0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
function:
|
||||
Longitude conversion
|
||||
******************************************************************************/
|
||||
static double transformLon(double x,double y)
|
||||
{
|
||||
double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x));
|
||||
ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
|
||||
ret += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0;
|
||||
ret += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
function:
|
||||
GCJ-02 international standard converted to Baidu map BD-09 standard
|
||||
******************************************************************************/
|
||||
static Coordinates bd_encrypt(Coordinates gg)
|
||||
{
|
||||
Coordinates bd;
|
||||
double x = gg.Lon, y = gg.Lat;
|
||||
double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
|
||||
double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
|
||||
bd.Lon = z * cos(theta) + 0.0065;
|
||||
bd.Lat = z * sin(theta) + 0.006;
|
||||
return bd;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
function:
|
||||
GPS's WGS-84 standard is converted into GCJ-02 international standard
|
||||
******************************************************************************/
|
||||
static Coordinates transform(Coordinates gps)
|
||||
{
|
||||
Coordinates gg;
|
||||
double sqrtMagic;
|
||||
double dLat = transformLat(gps.Lon - 105.0, gps.Lat - 35.0);
|
||||
double dLon = transformLon(gps.Lon - 105.0, gps.Lat - 35.0);
|
||||
double radLat = gps.Lat / 180.0 * pi;
|
||||
double magic = sin(radLat);
|
||||
magic = 1 - ee * magic * magic;
|
||||
sqrtMagic = sqrt(magic);
|
||||
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
|
||||
dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
|
||||
gg.Lat = gps.Lat + dLat;
|
||||
gg.Lon = gps.Lon + dLon;
|
||||
return gg;
|
||||
}
|
||||
/******************************************************************************
|
||||
function:
|
||||
Convert GPS latitude and longitude into GPS84 coordinates
|
||||
******************************************************************************/
|
||||
Coordinates L76X_GPS84_Coordinates()
|
||||
{
|
||||
Coordinates temp;
|
||||
temp.Lat =((int)(Lat_f)) + (Lat_f - ((int)(Lat_f)))*100 / 60;
|
||||
temp.Lon =((int)(Lon_f)) + (Lon_f - ((int)(Lon_f)))*100 / 60;
|
||||
// temp = transform(temp);
|
||||
return temp;
|
||||
}
|
||||
/******************************************************************************
|
||||
function:
|
||||
Convert GPS latitude and longitude into Google Maps coordinates
|
||||
******************************************************************************/
|
||||
Coordinates L76X_Google_Coordinates()
|
||||
{
|
||||
Coordinates temp;
|
||||
temp.Lat =((int)(Lat_f)) + (Lat_f - ((int)(Lat_f)))*100 / 60;
|
||||
temp.Lon =((int)(Lon_f)) + (Lon_f - ((int)(Lon_f)))*100 / 60;
|
||||
temp = transform(temp);
|
||||
return temp;
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
function:
|
||||
Convert GPS latitude and longitude into Baidu map coordinates
|
||||
******************************************************************************/
|
||||
Coordinates L76X_Baidu_Coordinates()
|
||||
{
|
||||
Coordinates temp;
|
||||
temp.Lat =((int)(Lat_f)) + (Lat_f - ((int)(Lat_f)))*100 / 60;
|
||||
temp.Lon =(int)(Lon_f) + (Lon_f - ((int)(Lon_f)))*100 / 60;
|
||||
temp = transform(temp);
|
||||
temp = bd_encrypt(temp);
|
||||
return temp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
function:
|
||||
Send a command to the L76X,Automatic calculation of the code
|
||||
parameter:
|
||||
data :The end of the command ends with ‘\0’ or it will go wrong,
|
||||
no need to increase the validation code.
|
||||
******************************************************************************/
|
||||
void L76X_Send_Command(char *data)
|
||||
{
|
||||
// char Check = data[1], Check_char[3]={0};
|
||||
// u8 i = 0;
|
||||
// //printf(" 1i = %d Check =%x \n", i, Check);
|
||||
// for(i=2; data[i] != '\0'; i++){
|
||||
// Check ^= data[i]; //Calculate the check value
|
||||
// }
|
||||
//printf(" i = %d Check =%x \n", i, Check);
|
||||
// Check_char[0] = Temp[Check/16%16];
|
||||
// Check_char[1] = Temp[Check%16];
|
||||
// Check_char[2] = '\0';
|
||||
|
||||
// DEV_Uart_SendString(data);
|
||||
// DEV_Uart_SendByte('*');
|
||||
// DEV_Uart_SendString(Check_char);
|
||||
// DEV_Uart_SendByte('\r');
|
||||
// DEV_Uart_SendByte('\n');
|
||||
}
|
||||
/**************清除GPS数据***********************/
|
||||
void L76X_Sturct_Clear(void)
|
||||
{
|
||||
memset(Save_Data.GPS_Buffer,0,GPS_Buffer_Length-1) ;
|
||||
Save_Data.isGetData=0;
|
||||
Save_Data.isParseData=0;
|
||||
memset(Save_Data.UTCTime,0,UTCTime_Length) ;
|
||||
memset(Save_Data.latitude,0,latitude_Length) ;
|
||||
memset(Save_Data.N_S,0,N_S_Length) ;
|
||||
memset(Save_Data.longitude,0,longitude_Length) ;
|
||||
memset(Save_Data.E_W,0,E_W_Length) ;
|
||||
Save_Data.isUsefull=0;
|
||||
|
||||
}
|
||||
|
||||
void parseGpsBuffer(void)
|
||||
{
|
||||
char *subString;
|
||||
char *subStringNext;
|
||||
char i = 0;
|
||||
char TempBuffer[10];
|
||||
char temp_time;
|
||||
if (Save_Data.isGetData)
|
||||
{
|
||||
Save_Data.isGetData = false;
|
||||
for (i = 0 ; i <= 9 ; i++)
|
||||
{
|
||||
if (i == 0)
|
||||
{
|
||||
if ((subString = strstr(Save_Data.GPS_Buffer, ",")) == NULL)
|
||||
;//解析错误
|
||||
}
|
||||
else
|
||||
{
|
||||
subString++;
|
||||
if ((subStringNext = strstr(subString, ",")) != NULL)
|
||||
{
|
||||
char usefullBuffer[2];
|
||||
char spdBuffer[10];
|
||||
char cogBuffer[10];
|
||||
switch(i)
|
||||
{
|
||||
case 1:memcpy(Save_Data.UTCTime, subString, subStringNext - subString);break; //获取UTC时间
|
||||
case 2:memcpy(usefullBuffer, subString, subStringNext - subString);break; //获取UTC时间
|
||||
case 3:memcpy(Save_Data.latitude, subString, subStringNext - subString);break; //获取纬度信息
|
||||
case 4:memcpy(Save_Data.N_S, subString, subStringNext - subString);break; //获取N/S
|
||||
case 5:memcpy(Save_Data.longitude, subString, subStringNext - subString);break; //获取经度信息
|
||||
case 6:memcpy(Save_Data.E_W, subString, subStringNext - subString);break; //获取E/W
|
||||
case 7:memcpy(spdBuffer, subString, subStringNext - subString);break; //获取E/W
|
||||
case 8:memcpy(cogBuffer, subString, subStringNext - subString);break; //获取E/W
|
||||
case 9:memcpy(Save_Data.UTCData, subString, subStringNext - subString);break; //获取E/W
|
||||
|
||||
default:break;
|
||||
}
|
||||
|
||||
subString = subStringNext;
|
||||
Save_Data.isParseData = true;
|
||||
if((usefullBuffer[0] == 'A')&(Save_Data.UTCData[0]!=0))
|
||||
{
|
||||
Save_Data.isUsefull = true;
|
||||
}
|
||||
|
||||
else if(usefullBuffer[0] == 'V')
|
||||
Save_Data.isUsefull = false;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
//解析错误
|
||||
}
|
||||
|
||||
|
||||
Lat_f=(Save_Data.latitude[0]-'0')*10000000.0+(Save_Data.latitude[1]-'0')*1000000.0+(Save_Data.latitude[2]-'0')*100000.0+(Save_Data.latitude[3]-'0')*10000.0+(Save_Data.latitude[5]-'0')*1000.0
|
||||
+(Save_Data.latitude[6]-'0')*100.0+(Save_Data.latitude[7]-'0')*10.0+(Save_Data.latitude[8]-'0')*1.0;
|
||||
Lon_f=(Save_Data.longitude[0]-'0')*100000000.0+(Save_Data.longitude[1]-'0')*10000000.0+(Save_Data.longitude[2]-'0')*1000000.0+(Save_Data.longitude[3]-'0')*100000.0+(Save_Data.longitude[4]-'0')*10000.0
|
||||
+(Save_Data.longitude[6]-'0')*1000.0+(Save_Data.longitude[7]-'0')*100.0+(Save_Data.longitude[8]-'0')*10.0+(Save_Data.longitude[9]-'0')*1.0;
|
||||
Lat_f=Lat_f/1000000.0;
|
||||
Lon_f=Lon_f/1000000.0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
//转换日期
|
||||
memset(TempBuffer,0,10);
|
||||
TempBuffer[0] = Save_Data.UTCData[4];
|
||||
TempBuffer[1] = Save_Data.UTCData[5];
|
||||
TempBuffer[2] = '-';
|
||||
TempBuffer[3] = Save_Data.UTCData[2];
|
||||
TempBuffer[4] = Save_Data.UTCData[3];
|
||||
TempBuffer[5] = '-';
|
||||
TempBuffer[6] = Save_Data.UTCData[0];
|
||||
TempBuffer[7] = Save_Data.UTCData[1];
|
||||
memcpy(Save_Data.UTCData,TempBuffer,strlen(TempBuffer));
|
||||
memset(TempBuffer,0,10);
|
||||
//转换时间
|
||||
temp_time=(Save_Data.UTCTime[0]-'0')*10+(Save_Data.UTCTime[1]-'0')+8;
|
||||
Save_Data.UTCTime[0]=(char)((temp_time/10)+'0');
|
||||
Save_Data.UTCTime[1]=(char)((temp_time%10)+'0');
|
||||
TempBuffer[0] = Save_Data.UTCTime[0];
|
||||
TempBuffer[1] = Save_Data.UTCTime[1];
|
||||
TempBuffer[2] = ':';
|
||||
TempBuffer[3] = Save_Data.UTCTime[2];
|
||||
TempBuffer[4] = Save_Data.UTCTime[3];
|
||||
TempBuffer[5] = ':';
|
||||
TempBuffer[6] = Save_Data.UTCTime[4];
|
||||
TempBuffer[7] = Save_Data.UTCTime[5];
|
||||
memset(Save_Data.UTCTime,0,UTCTime_Length);
|
||||
memcpy(Save_Data.UTCTime,TempBuffer,strlen(TempBuffer));
|
||||
memset(TempBuffer,0,10);
|
||||
|
||||
}
|
||||
|
||||
void printGpsBuffer(void)
|
||||
{
|
||||
int i=0;
|
||||
|
||||
//输出UTC时间
|
||||
if (Save_Data.isParseData)
|
||||
{
|
||||
i=0;
|
||||
Save_Data.isParseData = false;
|
||||
printf("Save_Data.UTCTime = ");
|
||||
while(Save_Data.UTCTime[i] != 0)
|
||||
{
|
||||
printf("%c",Save_Data.UTCTime[i]);
|
||||
i++;
|
||||
}
|
||||
printf("\r\n");
|
||||
//如果定位可用,输出定位信息
|
||||
if(Save_Data.isUsefull)
|
||||
{
|
||||
Save_Data.isUsefull = false;
|
||||
i=0;
|
||||
|
||||
printf("Save_Data.latitude = ");
|
||||
while(Save_Data.latitude[i] != 0)
|
||||
{
|
||||
printf("%c",Save_Data.latitude[i]);
|
||||
i++;
|
||||
}
|
||||
printf("\r\n");
|
||||
|
||||
i=0;
|
||||
printf("Save_Data.N_S = ");
|
||||
printf("%c",Save_Data.N_S[0]);
|
||||
printf("%c",Save_Data.N_S[1]);
|
||||
printf("\r\n");
|
||||
|
||||
i=0;
|
||||
printf("Save_Data.longitude = ");
|
||||
while(Save_Data.longitude[i]!=0)
|
||||
{
|
||||
|
||||
printf("%c",Save_Data.longitude[i]);
|
||||
i++;
|
||||
}
|
||||
printf("\r\n");
|
||||
|
||||
printf("Save_Data.E_W = ");
|
||||
printf("%c",Save_Data.E_W[0]);
|
||||
printf("%c",Save_Data.E_W[1]);
|
||||
printf("\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("GPS DATA is not usefull!\r\n");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void L76C_Process(void)
|
||||
{
|
||||
u8 times=0;
|
||||
Coordinates GPS;
|
||||
Save_Data.Flag_finish_gnss=0;
|
||||
while(times<35)
|
||||
{
|
||||
//等待GPS信号
|
||||
delay_ms(1000);
|
||||
if(Save_Data.isGetData)
|
||||
parseGpsBuffer();
|
||||
times++;
|
||||
if(Save_Data.isUsefull)
|
||||
{
|
||||
GPS=L76X_GPS84_Coordinates();
|
||||
times=61;
|
||||
}
|
||||
else
|
||||
{
|
||||
GPS.Lat=0.0;
|
||||
GPS.Lon=0.0;
|
||||
}
|
||||
// Set_GNSSTIME_Rtc();//RTC校准
|
||||
}
|
||||
//GPS=L76X_GPS84_Coordinates();
|
||||
sprintf(data_common1.GPS_DATA,"%.7f%c%s%c%.7f%c%s%c",GPS.Lat,',',Save_Data.N_S,',', GPS.Lon,',',Save_Data.E_W,',');//经纬度
|
||||
L76X_Sturct_Clear();
|
||||
USART_DeInit(USART1);
|
||||
Save_Data.Flag_finish_gnss=1;
|
||||
}
|
||||
Reference in New Issue
Block a user