-
Notifications
You must be signed in to change notification settings - Fork 0
/
GpsDevice.cpp
170 lines (156 loc) · 3.97 KB
/
GpsDevice.cpp
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "GpsDevice.h"
#include <Arduino.h>
//#define LOGGING 1 // delete this line to turn logging off
//#define LOGRAWGPSSTRINGS 1 // delete this line to stop printing full GPS strings
GpsDevice::~GpsDevice() {
};
GpsDevice::GpsDevice() :
gpsSerial(gpsRxPin, gpsTxPin),
haveGpsFix(false),
lat(0.0), // degrees minutes seconds
latDir(""), // N/S
lon(0.0), // degrees minutes seconds
lonDir(""), // E/W
altitudeMeters(0.0), // in meters
theTimeUtc(""),
numSatsInView(0), // how many gps satellites we can see
numSatsBeingTracked(0), // how many gps satellites we're actually tracking
rmcReceived(false),
ggaReceived(false)
{
gpsSerial.begin(9600);
};
void GpsDevice::getRawGpsDataFromSerial() {
logmsg("Reading GPS Serial...");
static int i = 0;
while (!(rmcReceived && ggaReceived))
{
while (gpsSerial.available())
{
char ch = gpsSerial.read();
if (ch != '\n' && i < sentenceSize)
{
sentence[i] = ch;
i++;
}
else
{
sentence[i] = '\0';
i = 0;
parseReceivedGpsData();
}
}
}
rmcReceived = false;
ggaReceived = false;
logmsg("---------------------------");
}
void GpsDevice::parseReceivedGpsData() {
char field[20];
getField(field, 0);
if (strcmp(field, "$GPRMC") == 0)
{
////// LAT
//String(latFloat, 4).toCharArray(gps_data_lat, 10);
getField(field, 3); // number
lat = String(field).toDouble();
getField(field, 4); // N/S
latDir = field;
logmsg("Lat: " + String(lat, 4) + latDir);
////// LON
//String(lonFloat, 4).toCharArray(gps_data_lon, 10);
getField(field, 5); // number
lon = String(field).toDouble();
getField(field, 6); // E/W
lonDir = field;
logmsg("Lon: " + String(lon, 4) + lonDir);
////// TIME
getField(field, 1);
theTimeUtc = field;
logmsg("TIME: " + theTimeUtc);
logmsg("Parsed RMC fields.");
logGpsString(sentence);
rmcReceived = true;
}
if (strcmp(field, "$GPGGA") == 0)
{
////// FIX
getField(field, 6); // 0=invalid 1=gpsfix ... etc.
if (strcmp(field, "1") == 0)
{
haveGpsFix = true;
}
////// NUM SATS TRACKED
getField(field, 7); // number
numSatsBeingTracked = String(field).toInt();
logmsg("Satellites Tracked: " + String(numSatsBeingTracked));
////// ALTITUDE
//String(altFloat, 0).toCharArray(alt_data, 6);
getField(field, 9); // number
altitudeMeters = String(field).toDouble();
float altitudeFeet = altitudeMeters * 3.28084; // convert to feet
logmsg("Alt: " + String(altitudeFeet, 4) + "ft");
// field 10 has a letter M for when you want meters directly
getField(field, 10);
logmsg("Parsed GGA fields.");
logGpsString(sentence);
ggaReceived = true;
}
if ( (strcmp(field, "$GPGSV") == 0) || (strcmp(field, "$GPGSA") == 0) )
{
logmsg("Printing GSV and GSA fields.");
logGpsString(sentence);
ggaReceived = true;
}
}
void GpsDevice::getField(char* buffer, int index)
{
int sentencePos = 0;
int fieldPos = 0;
int commaCount = 0;
while (sentencePos < sentenceSize)
{
if (sentence[sentencePos] == ',')
{
commaCount ++;
sentencePos ++;
}
if (commaCount == index)
{
buffer[fieldPos] = sentence[sentencePos];
fieldPos ++;
}
sentencePos ++;
}
buffer[fieldPos] = '\0';
}
String GpsDevice::getLatitude() {
if(!haveGpsFix)
{
return " NOGPS";
}
return String(lat, 4);
}
String GpsDevice::getLongitude() {
if(!haveGpsFix)
{
return " NOGPS";
}
return String(lon, 4);
}
String GpsDevice::getAltitudeFeet() {
return String(altitudeMeters * 3.28084, 0);
}
bool GpsDevice::getGpsFixState() {
return haveGpsFix;
}
void GpsDevice::logmsg(String msg) {
#ifdef LOGGING
Serial.println(classname + " " + msg);
#endif
}
void GpsDevice::logGpsString(String msg) {
#ifdef LOGRAWGPSSTRINGS
Serial.println(classname + " RAW: " + msg);
#endif
}