-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGPS_Work.ino
122 lines (106 loc) · 4.01 KB
/
GPS_Work.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <SoftwareSerial.h>
// Connect the GPS RX/TX to arduino pins 3 and 5
SoftwareSerial serial = SoftwareSerial(3,8);
const unsigned char UBX_HEADER[] = { 0xB5, 0x62 };
struct NAV_PVT {
unsigned char cls;
unsigned char id;
unsigned short len;
unsigned long iTOW; // GPS time of week of the navigation epoch (ms)
unsigned short year; // Year (UTC)
unsigned char month; // Month, range 1..12 (UTC)
unsigned char day; // Day of month, range 1..31 (UTC)
unsigned char hour; // Hour of day, range 0..23 (UTC)
unsigned char minute; // Minute of hour, range 0..59 (UTC)
unsigned char second; // Seconds of minute, range 0..60 (UTC)
char valid; // Validity Flags (see graphic below)
unsigned long tAcc; // Time accuracy estimate (UTC) (ns)
long nano; // Fraction of second, range -1e9 .. 1e9 (UTC) (ns)
unsigned char fixType; // GNSSfix Type, range 0..5
char flags; // Fix Status Flags
char flags2; // reserved
unsigned char numSV; // Number of satellites used in Nav Solution
long lon; // Longitude (deg)
long lat; // Latitude (deg)
long height; // Height above Ellipsoid (mm)
long hMSL; // Height above mean sea level (mm)
unsigned long hAcc; // Horizontal Accuracy Estimate (mm)
unsigned long vAcc; // Vertical Accuracy Estimate (mm)
long velN; // NED north velocity (mm/s)
long velE; // NED east velocity (mm/s)
long velD; // NED down velocity (mm/s)
long gSpeed; // Ground Speed (2-D) (mm/s)
long headMot; // Heading of motion 2-D (deg)
unsigned long sAcc; // Speed Accuracy Estimate
unsigned long headAcc; // Heading Accuracy Estimate
unsigned short pDOP; // Position dilution of precision
unsigned short reserved1;
unsigned long reserved;
long headVeh;
short magDec; // Reserved
unsigned short magACC; // Reserved
};
NAV_PVT pvt;
void calcChecksum(unsigned char* CK) {
memset(CK, 0, 2);
for (int i = 0; i < (int)sizeof(NAV_PVT); i++) {
CK[0] += ((unsigned char*)(&pvt))[i];
CK[1] += CK[0];
}
}
bool processGPS() {
static int fpos = 0;
static unsigned char checksum[2];
const int payloadSize = sizeof(NAV_PVT);
while ( serial.available() ) {
byte c = serial.read();
if ( fpos < 2 ) {
if ( c == UBX_HEADER[fpos] )
fpos++;
else
fpos = 0;
}
else {
if ( (fpos-2) < payloadSize )
((unsigned char*)(&pvt))[fpos-2] = c;
fpos++;
if ( fpos == (payloadSize+2) ) {
calcChecksum(checksum);
}
else if ( fpos == (payloadSize+3) ) {
if ( c != checksum[0] )
fpos = 0;
}
else if ( fpos == (payloadSize+4) ) {
fpos = 0;
if ( c == checksum[1] ) {
return true;
}
}
else if ( fpos > (payloadSize+4) ) {
fpos = 0;
}
}
}
return false;
}
void setup()
{
Serial.begin(9600);
serial.begin(9600);
Serial.print("Booted!");
}
void loop() {
if ( processGPS() ) {
Serial.print("#SV: "); Serial.print(pvt.numSV);
Serial.print(" fixType: "); Serial.print(pvt.fixType);
Serial.print(" Date:"); Serial.print(pvt.year); Serial.print("/"); Serial.print(pvt.month); Serial.print("/");
Serial.print(pvt.day); Serial.print(" "); Serial.print(pvt.hour); Serial.print(":");
Serial.print(pvt.minute); Serial.print(":"); Serial.print(pvt.second);
Serial.print(" lat/lon: "); Serial.print(pvt.lat/10000000.0f); Serial.print(","); Serial.print(pvt.lon/10000000.0f);
Serial.print(" gSpeed: "); Serial.print(pvt.gSpeed/1000.0f);
Serial.print(" heading: "); Serial.print(pvt.headMot/100000.0f);
Serial.print(" hAcc: "); Serial.print(pvt.hAcc/1000.0f);
Serial.println();
}
}