Sfoglia il codice sorgente

gps : sync time on fix

Florent 1 anno fa
parent
commit
31cbf9ed0e

+ 1 - 1
examples/companion_radio/main.cpp

@@ -74,7 +74,7 @@ static uint32_t _atoi(const char* sp) {
 /* GLOBAL OBJECTS */
 StdRNG fast_rng;
 SimpleMeshTables tables;
-MyMesh the_mesh(radio_driver, fast_rng, *new VolatileRTCClock(), tables); // TODO: test with 'rtc_clock' in target.cpp
+MyMesh the_mesh(radio_driver, fast_rng, rtc_clock, tables); 
 
 #ifdef DISPLAY_CLASS
   #include "UITask.h"

+ 6 - 0
src/helpers/sensors/LocationProvider.h

@@ -4,13 +4,19 @@
 
 
 class LocationProvider {
+protected:
+    bool _time_sync_needed = true;
 
 public:
+    virtual void syncTime() { _time_sync_needed = true; }
+    virtual bool waitingTimeSync() { return _time_sync_needed; }
     virtual long getLatitude() = 0;
     virtual long getLongitude() = 0;
     virtual long getAltitude() = 0;
+    virtual long satellitesCount() = 0;
     virtual bool isValid() = 0;
     virtual long getTimestamp() = 0;
+    virtual void sendSentence(const char * sentence);
     virtual void reset();
     virtual void begin();
     virtual void stop();

+ 27 - 2
src/helpers/sensors/MicroNMEALocationProvider.h

@@ -19,13 +19,16 @@
 class MicroNMEALocationProvider : public LocationProvider {
     char _nmeaBuffer[100];
     MicroNMEA nmea;
+    mesh::RTCClock* _clock;
     Stream* _gps_serial;
     int _pin_reset;
     int _pin_en;
+    long next_check = 0;
+    long time_valid = 0;
 
 public :
-    MicroNMEALocationProvider(Stream& ser, int pin_reset = GPS_RESET, int pin_en = GPS_EN) :
-    _gps_serial(&ser), nmea(_nmeaBuffer, sizeof(_nmeaBuffer)), _pin_reset(pin_reset), _pin_en(pin_en) {
+    MicroNMEALocationProvider(Stream& ser, mesh::RTCClock* clock = &rtc_clock, int pin_reset = GPS_RESET, int pin_en = GPS_EN) :
+    _gps_serial(&ser), nmea(_nmeaBuffer, sizeof(_nmeaBuffer)), _pin_reset(pin_reset), _pin_en(pin_en), _clock(clock) {
         if (_pin_reset != -1) {
             pinMode(_pin_reset, OUTPUT);
             digitalWrite(_pin_reset, GPS_RESET_FORCE);
@@ -59,6 +62,7 @@ public :
         }        
     }
 
+    void syncTime() override { nmea.clear(); LocationProvider::syncTime(); }
     long getLatitude() override { return nmea.getLatitude(); }
     long getLongitude() override { return nmea.getLongitude(); }
     long getAltitude() override { 
@@ -66,6 +70,7 @@ public :
         nmea.getAltitude(alt);
         return alt;
     }
+    long satellitesCount() override { return nmea.getNumSatellites(); }
     bool isValid() override { return nmea.isValid(); }
 
     long getTimestamp() override { 
@@ -73,7 +78,12 @@ public :
         return dt.unixtime();
     } 
 
+    void sendSentence(const char *sentence) override {
+        nmea.sendSentence(*_gps_serial, sentence);
+    }
+
     void loop() override {
+
         while (_gps_serial->available()) {
             char c = _gps_serial->read();
             #ifdef GPS_NMEA_DEBUG
@@ -81,5 +91,20 @@ public :
             #endif
             nmea.process(c);
         }
+
+        if (!isValid()) time_valid = 0;
+
+        if (millis() > next_check) {
+            next_check = millis() + 1000;
+            if (_time_sync_needed && time_valid > 2) {
+                if (_clock != NULL) {
+                    rtc_clock.setCurrentTime(getTimestamp());
+                    _time_sync_needed = false;
+                }
+            }
+            if (isValid()) {
+                time_valid ++;
+            }
+        }
     }
 };

+ 18 - 3
variants/t1000-e/target.cpp

@@ -9,7 +9,7 @@ RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BU
 WRAPPER_CLASS radio_driver(radio, board);
 
 VolatileRTCClock rtc_clock;
-MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1);
+MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock);
 T1000SensorManager sensors = T1000SensorManager(nmea);
 
 #ifdef DISPLAY_CLASS
@@ -179,14 +179,26 @@ void T1000SensorManager::loop() {
   }
 }
 
-int T1000SensorManager::getNumSettings() const { return 1; }  // just one supported: "gps" (power switch)
+int T1000SensorManager::getNumSettings() const { return 2; }  // just one supported: "gps" (power switch)
 
 const char* T1000SensorManager::getSettingName(int i) const {
-  return i == 0 ? "gps" : NULL;
+  switch (i) {
+    case 0:
+      return "gps";
+      break;
+    case 1:
+      return "sync";
+      break;
+    default:
+      return NULL;
+      break;
+  }
 }
 const char* T1000SensorManager::getSettingValue(int i) const {
   if (i == 0) {
     return gps_active ? "1" : "0";
+  } else if (i == 1) {
+    return _nmea->waitingTimeSync() ? "1" : "0";
   }
   return NULL;
 }
@@ -198,6 +210,9 @@ bool T1000SensorManager::setSettingValue(const char* name, const char* value) {
       start_gps();
     }
     return true;
+  } else if (strcmp(name, "sync") == 0) {
+    _nmea->syncTime(); // whatever the value ...
+    return true;
   }
   return false;  // not supported
 }