Rewrite to use TinyGPS instead of TinyGPS++, but it's still not small enough!

This commit is contained in:
Starbeamrainbowlabs 2019-06-28 13:57:39 +01:00
parent 04cc89ea50
commit 40f4e9130b
4 changed files with 26 additions and 20 deletions

View File

@ -1,7 +1,10 @@
#include "gps.h" #include "gps.h"
#include "settings.h" #include "settings.h"
#include <TinyGPS.h>
SoftwareSerial gps_begin() { SoftwareSerial gps_begin() {
// FUTURE: If this doesn't work as expected, we may want to find a more reliable SoftwareSerial library
SoftwareSerial serial_gps(PIN_GPS_RX, PIN_GPS_TX); SoftwareSerial serial_gps(PIN_GPS_RX, PIN_GPS_TX);
// Initialise the software-based serial connection to the GPS device // Initialise the software-based serial connection to the GPS device
serial_gps.begin(BAUD_GPS); serial_gps.begin(BAUD_GPS);
@ -12,16 +15,24 @@ GPSLocation gps_fetch() {
// The serial connection to the GPS module // The serial connection to the GPS module
SoftwareSerial serial_gps = gps_begin(); SoftwareSerial serial_gps = gps_begin();
// The GPS message decoder // The GPS message decoder
TinyGPSPlus gps; TinyGPS gps;
// The struct that will hold the result // The struct that will hold the result
GPSLocation result; GPSLocation result;
Serial.print(F("[gps] Working: ")); Serial.print(F("[gps] Working: "));
uint32_t ms_last_update = millis(); uint32_t start = millis(), ms_last_update = millis();
uint8_t ticks = 0; uint8_t ticks = 0;
unsigned long fix_age = TinyGPS::GPS_INVALID_AGE;
unsigned long chars;
unsigned short sentences, failed_checksum;
// We WILL discover our location - if it's the last thing we do! // We WILL discover our location - if it's the last thing we do!
while(true) { while(true) {
if(millis() > 5000 && gps.charsProcessed() < 10) { gps.stats(&chars, &sentences, &failed_checksum);
if(millis() - start > 5000 && chars < 10) {
Serial.println(F("\n[error] GPS device init failed")); Serial.println(F("\n[error] GPS device init failed"));
while(true) { delay(1); } while(true) { delay(1); }
} }
@ -34,20 +45,14 @@ GPSLocation gps_fetch() {
if(!gps.encode(serial_gps.read())) if(!gps.encode(serial_gps.read()))
continue; continue;
gps.f_get_position(&(result.lat), &(result.lng), &fix_age);
// If we haven't acquired a lock yet, go around again // If we haven't acquired a lock yet, go around again
if(!gps.location.isValid() if(fix_age == TinyGPS::GPS_INVALID_AGE) {
#ifdef SD_DEBUG_ENABLED
|| !gps.time.isValid()
#endif
) {
// NOTE: It can take a rather long time to get a lock. Just wait patiently :-) // NOTE: It can take a rather long time to get a lock. Just wait patiently :-)
if(millis() - ms_last_update > 500) { if(millis() - ms_last_update > 500) {
ticks++; ticks++;
#ifdef SD_DEBUG_ENABLED
Serial.print(gps.time.isValid() ? '-' : '.');
#else
Serial.print('.'); Serial.print('.');
#endif
if(ticks > 80) { if(ticks > 80) {
Serial.println(); Serial.println();
ticks = 0; ticks = 0;
@ -65,12 +70,16 @@ GPSLocation gps_fetch() {
// Hooray! // Hooray!
Serial.println(F("ok")); Serial.println(F("ok"));
result.lat = gps.location.lat(); // gps.f_get_position pushes the values into result directly
result.lng = gps.location.lng();
#ifdef SD_DEBUG_ENABLED #ifdef SD_DEBUG_ENABLED
int year;
byte month, day, hour, minute, second, _hundredths;
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &_hundredths, &fix_age);
snprintf(result.time, 19, "%04d-%02d-%02d %02d:%02d:%02d", snprintf(result.time, 19, "%04d-%02d-%02d %02d:%02d:%02d",
gps.date.year(), gps.date.month(), gps.date.day(), year, month, day,
gps.time.hour(), gps.time.minute(), gps.time.second() hour, minute, second
); );
#endif #endif
return result; return result;

View File

@ -2,8 +2,6 @@
#include <SoftwareSerial.h> #include <SoftwareSerial.h>
#include <TinyGPS++.h>
#include "settings.h" #include "settings.h"
// A lightweight struct to hold location information. // A lightweight struct to hold location information.

View File

@ -1,6 +1,5 @@
#include "settings.h" #include "settings.h"
#include <Arduino.h> #include <Arduino.h>
#include <TinyGPS++.h>
#include <MemoryFree.h> #include <MemoryFree.h>
#include "random.h" #include "random.h"

View File

@ -3,7 +3,7 @@
#include <SPI.h> #include <SPI.h>
#include <SdFat.h> #include <SdFat.h>
#include <TinyGPS++.h> #include "gps.h"
void store_init(); void store_init();