0

time.monotonic() develops glitches on long runs
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

time.monotonic() develops glitches on long runs

by seidler on Mon Mar 01, 2021 6:59 pm

Howdy,
I'm running recent Circuit Python on MKRzero. We're doing a datalogging project (seismic event detection) where we take data for about 5msec, send it to SD card, repeat for 24 hours (saved in 10 minute blocks). The issue is that after several hours the time returned by time.monotonic() starts to get glitchy, For example, here are the first 10 measurements in the data block starting at 24000 sec (the times in the first column are multiplied by 1000, so they are msec):

24000464, 33427.2
24000528, 33278.4
24000528, 33310.4
24000528, 33328.0
24000528, 33296.0
24000544, 33315.2
24000544, 33078.4
24000544, 32979.2
24000544, 32936.0
24000560, 32889.6

Each measurement (the second column) is the average of 10 'value' reads of the ADC. At earlier times, the delay between successive measurements is more typically 3-5msec, so the measurements all occurring within 1msec isn't reasonable -- instead the clock is sticking. I suspect that we have a LSB or other variable-size limitation, but haven't found any documentation to lead me to the explanation. Ideally, I need reliable time-stamps for the measurements. Worst case I can use the endpoints and linear extrapolate and things will be "ok-ish", but it would be nice to have real msec time stamps. Note that the MKRzero does not seem to support monotonic_ns.

With thanks!

seidler
 
Posts: 17
Joined: Sat Nov 07, 2020 1:45 am

Re: time.monotonic() develops glitches on long runs

by kevinjwalters on Mon Mar 01, 2021 7:23 pm

The 30 bit FP representation is the usual issue with CircuitPython's time.monotonic(): Adafruit Learn: CLUE Sensor Plotter in CircuitPython: Time in CircuitPython.

As you note the normal answer is to use time.monotonic_ns() but not sure what other alternatives are when that's not available.

kevinjwalters
 
Posts: 975
Joined: Sun Oct 01, 2017 3:15 pm

Re: time.monotonic() develops glitches on long runs

by seidler on Mon Mar 01, 2021 9:10 pm

Many thanks, that makes perfect sense. Am I correct that there is no way to reset the epoch for time.monotonic to zero, except for a hardware reset/power-cycle?

seidler
 
Posts: 17
Joined: Sat Nov 07, 2020 1:45 am

Re: time.monotonic() develops glitches on long runs

by kevinjwalters on Tue Mar 02, 2021 8:59 am

Not sure, I know microcontroller.reset() does not currently reset the time to 0, just tested 6.1.0 on a CLUE.

This topic links with: Best way to write library code using time.monotonic_ns()?.

kevinjwalters
 
Posts: 975
Joined: Sun Oct 01, 2017 3:15 pm

Please be positive and constructive with your questions and comments.