NTU-LEAP-1kD / instrumented-toy-firmware

1 stars 1 forks source link

5/feat/ble time syncing #10

Closed vicgwee closed 2 years ago

vicgwee commented 3 years ago

Will close #11

Right now, it reads unformatted time (after current time). Comparison with millis():

Connected event, central: 6c:1b:2f:f8:23:bc
Current Time:E507090302080C05D3       11138
Disconnected event, central: 6c:1b:2f:f8:23:bc
Connected event, central: 6c:1b:2f:f8:23:bc
Current Time:E507090302081105F4       16227
Disconnected event, central: 6c:1b:2f:f8:23:bc
Connected event, central: 6c:1b:2f:f8:23:bc
Current Time:E50709030208170522       21365
vicgwee commented 3 years ago

What time is received:

Byte Meaning
10 Adjust reasons (1 bit per reason)
9 Fractions of a second (256th)
8 Day of Week
7 Seconds
6 Minutes
5 Hours
4 Day of Month
3 Month
1-2 Year since 1792

Byte 1-7 is commonly known as ble_date_time_t Code here: https://github.com/iot-bus/iot-bus-bluetooth-examples-platformio/blob/7e79bdbfd48774ccd8dcc4f37b71d7f4c40255fa/temperature-monitor/src/bluetooth_time.h

According to https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrf/samples/bluetooth/peripheral_cts_client/README.html

vicgwee commented 3 years ago

Tested

vicgwee commented 3 years ago

I compared how long it took for the onboard clock (millis) to be de-synced with the CTS clock by 1000ms:

MCU 1 took 126 seconds MCU 2 took 120 seconds

In both cases, the Arduino clock was consistently slower than the CTS clock.

Syncing with the RTC, the results are more consistent, no significant de-syncing after several minutes. Will need to test it for longer.

vicgwee commented 3 years ago

I synced the RTC and the CTS times at the start. Then, I plotted the RTT (blue) of each CTS packet and the time difference (red) in ms between the RTC time and the received time over several minutes. If the RTT was over 80ms, the datapoint was not plotted:

received_time

Some insights:

vicgwee commented 2 years ago

Right now, the filter is not optimised. But the RTC updating function works. Will just merge it in and plan to enhance the filter later.