2019-07-15 14:25:51 +00:00
|
|
|
#include <TinyGPS.h>
|
|
|
|
|
2019-06-13 13:13:03 +00:00
|
|
|
#include "gps.h"
|
|
|
|
#include "settings.h"
|
2019-07-15 14:25:51 +00:00
|
|
|
#include "power.h"
|
|
|
|
#include "storage.h"
|
2019-06-13 13:13:03 +00:00
|
|
|
|
2019-06-28 12:57:39 +00:00
|
|
|
|
2019-06-26 14:59:49 +00:00
|
|
|
SoftwareSerial gps_begin() {
|
2019-06-28 12:57:39 +00:00
|
|
|
// FUTURE: If this doesn't work as expected, we may want to find a more reliable SoftwareSerial library
|
2019-06-26 14:59:49 +00:00
|
|
|
SoftwareSerial serial_gps(PIN_GPS_RX, PIN_GPS_TX);
|
2019-06-13 13:33:08 +00:00
|
|
|
// Initialise the software-based serial connection to the GPS device
|
2019-06-26 14:59:49 +00:00
|
|
|
serial_gps.begin(BAUD_GPS);
|
|
|
|
return serial_gps;
|
2019-06-13 13:13:03 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 14:46:07 +00:00
|
|
|
GPSLocation gps_fetch() {
|
2019-06-26 14:59:49 +00:00
|
|
|
// The serial connection to the GPS module
|
|
|
|
SoftwareSerial serial_gps = gps_begin();
|
2019-06-26 14:46:07 +00:00
|
|
|
// The GPS message decoder
|
2019-06-28 12:57:39 +00:00
|
|
|
TinyGPS gps;
|
2019-06-26 14:59:49 +00:00
|
|
|
// The struct that will hold the result
|
2019-06-26 14:46:07 +00:00
|
|
|
GPSLocation result;
|
|
|
|
|
2019-07-02 13:07:11 +00:00
|
|
|
Serial.print(F("[gps] Working "));
|
2019-06-28 12:57:39 +00:00
|
|
|
uint32_t start = millis(), ms_last_update = millis();
|
2019-06-27 15:27:33 +00:00
|
|
|
uint8_t ticks = 0;
|
2019-06-28 12:57:39 +00:00
|
|
|
unsigned long fix_age = TinyGPS::GPS_INVALID_AGE;
|
|
|
|
unsigned long chars;
|
|
|
|
unsigned short sentences, failed_checksum;
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-06-13 13:33:08 +00:00
|
|
|
// We WILL discover our location - if it's the last thing we do!
|
|
|
|
while(true) {
|
2019-06-28 12:57:39 +00:00
|
|
|
gps.stats(&chars, &sentences, &failed_checksum);
|
|
|
|
|
|
|
|
if(millis() - start > 5000 && chars < 10) {
|
2019-07-02 13:07:11 +00:00
|
|
|
Serial.println(F("\n[error/gps] Init failed"));
|
2019-06-13 13:53:59 +00:00
|
|
|
while(true) { delay(1); }
|
|
|
|
}
|
|
|
|
|
2019-06-13 13:33:08 +00:00
|
|
|
// Make sure there's something to read
|
2019-06-26 14:59:49 +00:00
|
|
|
if(serial_gps.available() <= 0)
|
2019-06-13 13:33:08 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// If it failed, go around again
|
2019-06-26 14:59:49 +00:00
|
|
|
if(!gps.encode(serial_gps.read()))
|
2019-06-13 13:13:03 +00:00
|
|
|
continue;
|
|
|
|
|
2019-06-28 12:57:39 +00:00
|
|
|
gps.f_get_position(&(result.lat), &(result.lng), &fix_age);
|
|
|
|
|
2019-06-13 13:33:08 +00:00
|
|
|
// If we haven't acquired a lock yet, go around again
|
2019-06-28 12:57:39 +00:00
|
|
|
if(fix_age == TinyGPS::GPS_INVALID_AGE) {
|
2019-06-26 14:59:49 +00:00
|
|
|
// NOTE: It can take a rather long time to get a lock. Just wait patiently :-)
|
2019-06-27 15:27:33 +00:00
|
|
|
if(millis() - ms_last_update > 500) {
|
|
|
|
ticks++;
|
2019-06-27 15:28:28 +00:00
|
|
|
Serial.print('.');
|
2019-06-27 15:27:33 +00:00
|
|
|
if(ticks > 80) {
|
|
|
|
Serial.println();
|
|
|
|
ticks = 0;
|
|
|
|
}
|
2019-07-08 13:48:09 +00:00
|
|
|
ms_last_update = millis();
|
2019-06-27 15:27:33 +00:00
|
|
|
}
|
2019-06-13 13:13:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-13 13:33:08 +00:00
|
|
|
// Hooray!
|
2019-06-24 14:47:00 +00:00
|
|
|
Serial.println(F("ok"));
|
2019-06-26 14:46:07 +00:00
|
|
|
|
2019-06-28 12:57:39 +00:00
|
|
|
// gps.f_get_position pushes the values into result directly
|
|
|
|
|
2019-07-15 14:25:51 +00:00
|
|
|
#ifdef GPS_DELTA_CHECK
|
|
|
|
float last_reading_lat = store_eeprom_float_retrieve(sizeof(uint32_t) * 2);
|
|
|
|
float last_reading_lng = store_eeprom_float_retrieve(sizeof(uint32_t) * 2 + sizeof(float));
|
|
|
|
// Serial.println(last_reading_lat);
|
|
|
|
// Serial.println(last_reading_lng);
|
|
|
|
// Serial.println(result.lat);
|
|
|
|
// Serial.println(result.lng);
|
|
|
|
if(abs(last_reading_lat - result.lat) < GPS_DELTA_MIN_LAT &&
|
|
|
|
abs(last_reading_lng - result.lng) < GPS_DELTA_MIN_LNG &&
|
|
|
|
last_reading_lat == last_reading_lat && // Ensure that the last readings aren't NaN
|
|
|
|
last_reading_lng == last_reading_lng) {
|
|
|
|
Serial.println(F("[gps] No movement detected"));
|
|
|
|
power_off();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save this location to EEPROM
|
|
|
|
store_eeprom_float_save(sizeof(uint32_t) * 2, result.lat);
|
|
|
|
store_eeprom_float_save(sizeof(uint32_t) * 2 + sizeof(float), result.lng);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// TODO: Don't send a message at night
|
2019-06-26 14:46:07 +00:00
|
|
|
#ifdef SD_DEBUG_ENABLED
|
2019-06-28 12:57:39 +00:00
|
|
|
int year;
|
|
|
|
byte month, day, hour, minute, second, _hundredths;
|
|
|
|
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &_hundredths, &fix_age);
|
|
|
|
|
2019-06-26 14:46:07 +00:00
|
|
|
snprintf(result.time, 19, "%04d-%02d-%02d %02d:%02d:%02d",
|
2019-06-28 12:57:39 +00:00
|
|
|
year, month, day,
|
|
|
|
hour, minute, second
|
2019-06-26 14:46:07 +00:00
|
|
|
);
|
|
|
|
#endif
|
|
|
|
return result;
|
2019-06-13 13:13:03 +00:00
|
|
|
}
|
2019-06-26 14:59:49 +00:00
|
|
|
|
|
|
|
gps_end(serial_gps);
|
2019-06-13 13:13:03 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 14:59:49 +00:00
|
|
|
void gps_end(SoftwareSerial serial_gps) {
|
|
|
|
serial_gps.end();
|
2019-06-13 13:13:03 +00:00
|
|
|
}
|