我正在计算PIC单片机中GPS使用纬度和经度的车辆总行驶距离。这里我的GPS模块处于空闲状态(不移动),因此总距离应该显示为“0”(零),是的它显示为零。但有时它显示奇怪的值“9223372036854775808.775808”,后来我的总距离变为零。
这是我的代码;
#include<stdio.h>
#include<math.h>
#include <string.h>
#define COMMA 0x2C
#define MAXSIZE 100
#define pi 3.14159265358979323846
double deg2rad(double);
double rad2deg(double);
float stof(char* s);
float Nmea_Degree(char *data);
double dist;
double distance(double lat1, double lon1, double lat2, double lon2, char unit);
float old_lat=0;
float old_lon=0;
float new_lat=0;
float new_lon=0;
int flag=0;
double theta, dist=0,old_dist=0;
int main()
{
/* Get lattitude */
int k=0,j=0;
unsigned char lastCommaPosition;
unsigned char stringRead[MAXSIZE]="$GPRMC,124130.000,A,1300.4147,N,08015.4269,E,0.13,224.13,151015,,,D*6A";
//$GPRMC,124130.000,A,1300.4147,N,08015.4269,E,0.13,224.13,151015,,,D*6A
//$GPGGA,1300.80,4548.32609,N,01557.99352,E,1,07,1.30,129.1,M,41.4,M,,*5E
unsigned char *pChar;
unsigned char latitudeCardinalString[3];
unsigned char latitudeString[11];
unsigned char longitudeString[12];
float longitude;
float latitude;
if(strstr(stringRead,"GPRMC")) //if(strstr(nmea,"GPRMC"))
{
pChar = stringRead;
lastCommaPosition = j;
j = lastCommaPosition + 1;
while(*(pChar+j) != COMMA)
{
latitudeString[k] = *(pChar+j);
j++;
k++;
}
j=19;
k=18;
lastCommaPosition = j;
latitudeString[k] = '\0';
sscanf(latitudeString, "%f", &latitude);
pChar = stringRead;
j = lastCommaPosition + 1;
k = 0;
while(*(pChar+j) != COMMA) {
latitudeCardinalString[k] = *(pChar+j);
j++;
k++;
}
lastCommaPosition = j;
latitudeCardinalString[k] = '\0';
printf("Latitude(f):%f",Nmea_Degree(latitudeCardinalString)); ////Latitude
printf("\n");
new_lat=Nmea_Degree(latitudeCardinalString);
/* Get Longitude */
pChar = stringRead;
j = lastCommaPosition + 1;
k = 0;
j=32;
while(*(pChar+j) != COMMA) {
longitudeString[k] = *(pChar+j);
j++;
k++;
}
lastCommaPosition = j;
longitudeString[k] = '\0';
sscanf(longitudeString, "%f", &longitude);
printf("Longitude(f):%f",Nmea_Degree(longitudeString)); ////Longitude
printf("\n");
/*extract UTC time*/
pChar=gpsdata;
j = 1; /* start of time field */
k = 0;
while(*(pChar+j) != COMMA)
{
timeString[k] = *(pChar+j);
j++;
k++;
}
lastCommaPosition = j;
timeString[k] = '\0';
sscanf(timeString, "%ld", &utcTime);
utcHour = (utcTime/10000); /* extract Hours from long */
utcMinutes = (utcTime - (utcHour*10000))/100; /* extract minutes from long */
utcSeconds = utcTime - (utcHour*10000) - (utcMinutes*100); /* extract seconds from long */
if(utcHour >= 4 && utcHour <= 23) estHour = utcHour + 5;
else estHour = utcHour + 20;
estMinutes = utcMinutes;
estSeconds = utcSeconds;
/*Total distance calculation*/
if(utcSeconds==15 || utcSeconds==30 || utcSeconds==45 || utcSeconds==00)
{
if(flag==0)
{
old_lat=Nmea_Degree(latitudeCardinalString);
old_lon=Nmea_Degree(longitudeString);
flag=1;
}
else
{
new_lat=Nmea_Degree(latitudeCardinalString);
new_lon=Nmea_Degree(longitudeString);
printf("%f",distance(old_lat,old_lon,new_lat,new_lon,'N'));
old_lat=new_lat;
old_lon=new_lon;
}
}
else
{
printf("Bad GPS");
}
return 0;
}
float stof(char* s)
{
int point_seen;
float rez = 0, fact = 1;
if (*s == '-')
{
s++;
fact = -1;
}
for (point_seen = 0; *s; s++)
{
if (*s == '.')
{
point_seen = 1;
continue;
}
int d = *s - '0';
if (d >= 0 && d <= 9)
{
if (point_seen) fact /= 10.0f;
rez = rez * 10.0f + (float)d;
}
}
return rez * fact;
}
float Nmea_Degree(char *data)
{
double Value;
int Degree;
double minute;
Degree=stof(data)/100;
minute=(float)stof(data)-(100*Degree);
Value = Degree+(minute/60);
return Value;
}
double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
//double theta, dist;
theta = lon1 - lon2;
dist = sin(deg2rad(lat1)) * sin(deg2rad(lat2)) + cos(deg2rad(lat1)) * cos(deg2rad(lat2)) * cos(deg2rad(theta));
dist = acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
dist+=dist;
return (dist);
}
double deg2rad(double deg) {
return (deg * pi / 180);
}
double rad2deg(double rad) {
return (rad * 180 / pi);
}
这是奇怪的价值:
Latitude:13.006949
Longitude:80.257065
Latitude:13.006949
Longitude:80.257065
Latitude:13.006949
Longitude:80.257065
interval: 02
old_lat:13.006952 old_lon:80.257065 new_lat:13.006949 new_lon:80.257065Total distance: 0.000000
Latitude:13.006949
Longitude:80.257065
Latitude:13.006949
Longitude:80.257065
Latitude:13.006949
Longitude:80.257065
Latitude:13.006949
Longitude:80.257065
Latitude:13.006949
Longitude:80.257065
Latitude:13.006949
Longitude:80.257065
Latitude:13.006949
Longitude:80.257065
Latitude:13.006949
Longitude:80.257065
Latitude:13.006949
Longitude:80.257065
Latitude:13.006948
Longitude:80.257065
Latitude:13.006948
Longitude:80.257065
Latitude:13.006948
Longitude:80.257065
Latitude:13.006948
Longitude:80.257065
Latitude:13.006948
Longitude:80.257065
Latitude:13.006948
Longitude:80.257065
old_lat:13.006949 old_lon:80.257065 new_lat:13.006948 new_lon:80.257065Total distance: 9223372036854775808.775808
这里我在stringRead变量中传递我的gps uart值。 问题是什么?
...谢谢