cyberman54 / ESP32-Paxcounter

Wifi & BLE driven passenger flow metering with cheap ESP32 boards
https://cyberman54.github.io/ESP32-Paxcounter/
Other
1.72k stars 401 forks source link

Improve accuracy of clock controller #283

Closed cyberman54 closed 5 years ago

cyberman54 commented 5 years ago

For a LoRaWAN-only time synched wall clock we need a precise on board clock, which generates the pulses (DCF77 or IF482) for the external wall clock in realtime. Systime from Time.h is originally a fully software counter based clock, and has to much drift. We need a more precise solution, which is close to hardware.

So, we want to improve accuracy of clock controller by two enhancements:

  1. Use a precise onboard counter/clock as reference time base
  2. Sync systime with 1) in timekeeper.cpp

ad 1):

Times bases could be a) hardware generated PPS by GPS (-> perfect for tests to validate the solution) b) hardware generated PPS or SQW by RTC DS3231 (-> perhaps best solution for production) c) software generated PPS by a ULP routine in RTC of ESP32 (-> perhaps sufficient solution for production)

template for 1c): https://forum.arduino.cc/index.php?topic=121088.15

The PPS signal shall trigger an interrupt in timekeeper.cpp

ad 2):

a) either implement own time-of-day clock, with counter based on 1) b) or use some implementation for syncing systime of Time.h with 1)

templates for 2a): https://github.com/liebman/ESPNTPServer/blob/master/ESPNTPServer/GPS.cpp

templates for 2b): https://github.com/DennisSc/PPS-ntp-server https://hackaday.io/project/13386-arduino-precision-time https://stackoverflow.com/questions/10654258/get-millisecond-part-of-time

Further suggestion from esp32.net:

You can get uSec resolution from the hardware timer and using a semaphore to sync the 1pps from the GPS with the hardware timer set up as a watchdog.

The ESP has a nano second clock you can use: link, which works for the ESP32 as well.

cyberman54 commented 5 years ago

2b) done.

Concept for 1): implement it maybe hardware based with timers an pulse counter of esp32:

https://www.davidpilling.com/wiki/index.php/GPS

cyberman54 commented 5 years ago

1b) done.

Seems to work, we see precisions in range ~40-200ms now.

cyberman54 commented 5 years ago

Further improvement option: Use interrupt on LoRa pin DIO0 to capture TxDone timestamps. Thanks to @terrillmoore who came up with this idea.

cyberman54 commented 5 years ago

v1.7.4 now reaches ~10ms accuracy. Since this is suitable for a wall clock i close this now.