Skip to content

Commit d05250f

Browse files
Fixed problem reported by dbrooke where small positive latitudes and longitudes (less than 1 degree) were reported as negative.
1 parent ba04655 commit d05250f

9 files changed

Lines changed: 45 additions & 43 deletions

File tree

TinyGPS++.cpp

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,14 @@ int32_t TinyGPSPlus::parseDecimal(const char *term)
127127

128128
// static
129129
// Parse degrees in that funny NMEA format DDMM.MMMM
130-
void TinyGPSPlus::parseDegrees(const char *term, int16_t &degrees, uint32_t &billionths)
130+
void TinyGPSPlus::parseDegrees(const char *term, RawDegrees &deg)
131131
{
132132
uint32_t leftOfDecimal = (uint32_t)atol(term);
133133
uint16_t minutes = (uint16_t)(leftOfDecimal % 100);
134134
uint32_t multiplier = 10000000UL;
135135
uint32_t tenMillionthsOfMinutes = minutes * multiplier;
136136

137-
degrees = (int16_t)(leftOfDecimal / 100);
137+
deg.deg = (int16_t)(leftOfDecimal / 100);
138138

139139
while (isdigit(*term))
140140
++term;
@@ -146,7 +146,8 @@ void TinyGPSPlus::parseDegrees(const char *term, int16_t &degrees, uint32_t &bil
146146
tenMillionthsOfMinutes += (*term - '0') * multiplier;
147147
}
148148

149-
billionths = (5 * tenMillionthsOfMinutes + 1) / 3;
149+
deg.billionths = (5 * tenMillionthsOfMinutes + 1) / 3;
150+
deg.negative = false;
150151
}
151152

152153
#define COMBINE(sentence_type, term_number) (((unsigned)(sentence_type) << 5) | term_number)
@@ -233,21 +234,19 @@ bool TinyGPSPlus::endOfTermHandler()
233234
break;
234235
case COMBINE(GPS_SENTENCE_GPRMC, 3): // Latitude
235236
case COMBINE(GPS_SENTENCE_GPGGA, 2):
236-
location.setLatitude(term);
237+
location.setLatitude(term);
237238
break;
238239
case COMBINE(GPS_SENTENCE_GPRMC, 4): // N/S
239240
case COMBINE(GPS_SENTENCE_GPGGA, 3):
240-
if (term[0] == 'S')
241-
location.iNewLatDegrees = -location.iNewLatDegrees;
241+
location.rawNewLatData.negative = term[0] == 'S';
242242
break;
243243
case COMBINE(GPS_SENTENCE_GPRMC, 5): // Longitude
244244
case COMBINE(GPS_SENTENCE_GPGGA, 4):
245245
location.setLongitude(term);
246246
break;
247247
case COMBINE(GPS_SENTENCE_GPRMC, 6): // E/W
248248
case COMBINE(GPS_SENTENCE_GPGGA, 5):
249-
if (term[0] == 'W')
250-
location.iNewLngDegrees = -location.iNewLngDegrees;
249+
location.rawNewLngData.negative = term[0] == 'W';
251250
break;
252251
case COMBINE(GPS_SENTENCE_GPRMC, 7): // Speed (GPRMC)
253252
speed.set(term);
@@ -335,38 +334,34 @@ const char *TinyGPSPlus::cardinal(double course)
335334

336335
void TinyGPSLocation::commit()
337336
{
338-
iLatDegrees = iNewLatDegrees;
339-
uLatBillionths = uNewLatBillionths;
340-
iLngDegrees = iNewLngDegrees;
341-
uLngBillionths = uNewLngBillionths;
337+
rawLatData = rawNewLatData;
338+
rawLngData = rawNewLngData;
342339
lastCommitTime = millis();
343340
valid = updated = true;
344341
}
345342

346343
void TinyGPSLocation::setLatitude(const char *term)
347344
{
348-
TinyGPSPlus::parseDegrees(term, iNewLatDegrees, uNewLatBillionths);
345+
TinyGPSPlus::parseDegrees(term, rawNewLatData);
349346
}
350347

351348
void TinyGPSLocation::setLongitude(const char *term)
352349
{
353-
TinyGPSPlus::parseDegrees(term, iNewLngDegrees, uNewLngBillionths);
350+
TinyGPSPlus::parseDegrees(term, rawNewLngData);
354351
}
355352

356353
double TinyGPSLocation::lat()
357354
{
358355
updated = false;
359-
return iLatDegrees > 0 ?
360-
(iLatDegrees + uLatBillionths / 1000000000.0) :
361-
(iLatDegrees - uLatBillionths / 1000000000.0);
356+
double ret = rawLatData.deg + rawLatData.billionths / 1000000000.0;
357+
return rawLatData.negative ? -ret : ret;
362358
}
363359

364360
double TinyGPSLocation::lng()
365361
{
366362
updated = false;
367-
return iLngDegrees > 0 ?
368-
(iLngDegrees + uLngBillionths / 1000000000.0) :
369-
(iLngDegrees - uLngBillionths / 1000000000.0);
363+
double ret = rawLngData.deg + rawLngData.billionths / 1000000000.0;
364+
return rawLngData.negative ? -ret : ret;
370365
}
371366

372367
void TinyGPSDate::commit()

TinyGPS++.h

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,34 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
4040
#define _GPS_FEET_PER_METER 3.2808399
4141
#define _GPS_MAX_FIELD_SIZE 15
4242

43+
struct RawDegrees
44+
{
45+
uint16_t deg;
46+
uint32_t billionths;
47+
bool negative;
48+
public:
49+
RawDegrees() : deg(0), billionths(0), negative(false)
50+
{}
51+
};
52+
4353
struct TinyGPSLocation
4454
{
4555
friend class TinyGPSPlus;
4656
public:
4757
bool isValid() const { return valid; }
4858
bool isUpdated() const { return updated; }
4959
uint32_t age() const { return valid ? millis() - lastCommitTime : (uint32_t)ULONG_MAX; }
50-
51-
int16_t rawLatDegrees() { updated = false; return iLatDegrees; }
52-
int16_t rawLngDegrees() { updated = false; return iLngDegrees; }
53-
uint32_t rawLatBillionths() { updated = false; return uLatBillionths; }
54-
uint32_t rawLngBillionths() { updated = false; return uLngBillionths; }
60+
const RawDegrees &rawLat() { updated = false; return rawLatData; }
61+
const RawDegrees &rawLng() { updated = false; return rawLngData; }
5562
double lat();
5663
double lng();
5764

58-
TinyGPSLocation() : valid(false), updated(false),
59-
iLatDegrees(0), iLngDegrees(0), uLatBillionths(0), uLngBillionths(0)
65+
TinyGPSLocation() : valid(false), updated(false)
6066
{}
6167

6268
private:
6369
bool valid, updated;
64-
int16_t iLatDegrees, iNewLatDegrees, iLngDegrees, iNewLngDegrees;
65-
uint32_t uLatBillionths, uNewLatBillionths, uLngBillionths, uNewLngBillionths;
70+
RawDegrees rawLatData, rawLngData, rawNewLatData, rawNewLngData;
6671
uint32_t lastCommitTime;
6772
void commit();
6873
void setLatitude(const char *term);
@@ -229,7 +234,7 @@ class TinyGPSPlus
229234
static const char *cardinal(double course);
230235

231236
static int32_t parseDecimal(const char *term);
232-
static void parseDegrees(const char *term, int16_t &degrees, uint32_t &billionths);
237+
static void parseDegrees(const char *term, RawDegrees &deg);
233238

234239
uint32_t charsProcessed() const { return encodedCharCount; }
235240
uint32_t sentencesWithFix() const { return sentencesWithFixCount; }

examples/BasicExample/BasicExample.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
// A sample NMEA stream.
9-
char *gpsStream =
9+
const char *gpsStream =
1010
"$GPRMC,045103.000,A,3014.1984,N,09749.2872,W,0.67,161.46,030913,,,A*7C\r\n"
1111
"$GPGGA,045104.000,3014.1985,N,09749.2873,W,1,09,1.2,211.6,M,-22.5,M,,0000*62\r\n"
1212
"$GPRMC,045200.000,A,3014.3820,N,09748.9514,W,36.88,65.02,030913,,,A*77\r\n"

examples/DeviceExample/DeviceExample.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
77
*/
88
static const int RXPin = 4, TXPin = 3;
9-
static const unsigned long GPSBaud = 4800;
9+
static const uint32_t GPSBaud = 4800;
1010

1111
// The TinyGPS++ object
1212
TinyGPSPlus gps;

examples/FullExample/FullExample.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
77
*/
88
static const int RXPin = 4, TXPin = 3;
9-
static const unsigned long GPSBaud = 4800;
9+
static const uint32_t GPSBaud = 4800;
1010

1111
// The TinyGPS++ object
1212
TinyGPSPlus gps;

examples/KitchenSink/KitchenSink.ino

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
77
*/
88
static const int RXPin = 4, TXPin = 3;
9-
static const unsigned long GPSBaud = 4800;
9+
static const uint32_t GPSBaud = 4800;
1010

1111
// The TinyGPS++ object
1212
TinyGPSPlus gps;
@@ -40,13 +40,15 @@ void loop()
4040
Serial.print(F("LOCATION Fix Age="));
4141
Serial.print(gps.location.age());
4242
Serial.print(F("ms Raw Lat="));
43-
Serial.print(gps.location.rawLatDegrees());
43+
Serial.print(gps.location.rawLat().negative ? "-" : "+");
44+
Serial.print(gps.location.rawLat().deg);
4445
Serial.print("[+");
45-
Serial.print(gps.location.rawLatBillionths());
46+
Serial.print(gps.location.rawLat.billionths());
4647
Serial.print(F(" billionths], Raw Long="));
47-
Serial.print(gps.location.rawLngDegrees());
48+
Serial.print(gps.location.rawLng().negative ? "-" : "+");
49+
Serial.print(gps.location.rawLng().deg);
4850
Serial.print("[+");
49-
Serial.print(gps.location.rawLngBillionths());
51+
Serial.print(gps.location.rawLng.billionths());
5052
Serial.print(F(" billionths], Lat="));
5153
Serial.print(gps.location.lat(), 6);
5254
Serial.print(F(" Long="));
@@ -85,7 +87,7 @@ void loop()
8587

8688
else if (gps.speed.isUpdated())
8789
{
88-
Serial.print(F("SPEED Fix Age=%ulms Raw=%ul Knots=%f MPH=%f m/s=%f km/h=%f"));
90+
Serial.print(F("SPEED Fix Age="));
8991
Serial.print(gps.speed.age());
9092
Serial.print(F("ms Raw="));
9193
Serial.print(gps.speed.value());
@@ -184,4 +186,4 @@ void loop()
184186
last = millis();
185187
Serial.println();
186188
}
187-
}
189+
}

examples/SatElevTracker/SatElevTracker.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
4800-baud serial GPS device hooked up on pins 4(RX) and 3(TX).
1111
*/
1212
static const int RXPin = 4, TXPin = 3;
13-
static const unsigned long GPSBaud = 4800;
13+
static const uint32_t GPSBaud = 4800;
1414
static const int MAX_SATELLITES = 40;
1515
static const int PAGE_LENGTH = 40;
1616

examples/SatelliteTracker/SatelliteTracker.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
4800-baud serial GPS device hooked up on pins 4(RX) and 3(TX).
1515
*/
1616
static const int RXPin = 4, TXPin = 3;
17-
static const unsigned long GPSBaud = 4800;
17+
static const uint32_t GPSBaud = 4800;
1818

1919
// The TinyGPS++ object
2020
TinyGPSPlus gps;

examples/UsingCustomFields/UsingCustomFields.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
4800-baud serial GPS device hooked up on pins 4(RX) and 3(TX).
1313
*/
1414
static const int RXPin = 4, TXPin = 3;
15-
static const unsigned long GPSBaud = 4800;
15+
static const uint32_t GPSBaud = 4800;
1616

1717
// The TinyGPS++ object
1818
TinyGPSPlus gps;

0 commit comments

Comments
 (0)