[Dprglist] PID-tuned Clock in Python?

Murray Altheim murray18 at altheim.com
Wed Feb 10 03:03:31 PST 2021


On 9/02/21 10:06 pm, Murray Altheim via DPRGlist wrote:
> Executive Summary: This has to do with Publish-Subscribe message buses,
> PID controllers, the accuracy of system clocks, Python clocks, threads
> and timing loops, and yes, I know that last subject tickles the fancy
> of those using microcontrollers where these kinds of problems never happen. :-)
Okay, I've had a bit of a play with this. I haven't been entirely
scientific (hey, when the wind is southerly I know a hawk from a
handsaw), but my PID-tuned Clock seems to work at least better than
my non-PID-tuned Clock.

Running the clock_test.py Python script on a Raspberry Pi 3 B+ (on a
robot with an assortment of sensors), I've got the PID control tuned so
that the error on a 50.0ms loop is generally less than 0.01ms, typically
around 0.001-0.004ms. It intermittently bumps up to around 0.02ms error,
and if while the test is running on another console window I start an
"sudo apt update" I can force a couple of burps of ~0.5 error, but
generally it recovers within a clock loop back to under 0.01ms error.
Without the PID control the error is generally more in the 0.1-0.3ms
(or much worse under load) per loop range.

Not bad, methinks. Within the requirements I (think I) have for a timing
loop, it's probably not worth digging much deeper. How accurate is a
millisecond clock on an Arduino, an ESP32, a Tiny or some other
microcontroller/microcomputer? Dunno.

I'd thought about getting a DS3231 Real Time Clock as they're quite
accurate, I2C bus compatible, and not particularly expensive:

   https://www.adafruit.com/product/3013

but surprisingly the DS3231 doesn't support a millisecond function. That
kind of thing is really not an RTC's function, which is more suited as
an accurate system clock and is extremely accurate at the seconds output.
Though the lack of milliseconds or microseconds support makes me wonder
what kind of RTC might be available that does support ms or µs.

I searched around and apparently there aren't any hobbyist RTCs that do
provide milliseconds output. The DS3231 does provide five sub-second
outputs: 32 KHz, 1 KHz, 1.024 KHz, 4.096 KHz and 8.192 KHz*. I don't
know how accurate those outputs are, as I don't know if they are drift-
compensated or not.

Now, the idea does occur to me that if the seconds output or one of the
high frequency outputs is really accurate, what if I used my PID class to
divide the output, then tune the PID to the error so that the Clock's
milliseconds are synced to the DS3231. Maybe that's a crazy idea.

I thought about using a GPS unit, as its 1 PPS clock claims a +/-20-30ns
jitter, the commonly-available MTK3339 down to 10ns. I'm not sure how this
all works as I think they typically have just a 32kHz crystal oscillator,
so maybe they need satellite sync to be seconds-accurate (?). But even
if they have some internal-miracle timer I'd still need to divide that
1 PPS signal myself to get milliseconds. Ugh.

Does anyone know of a millisecond- or even microsecond-supporting RTC?
I'm trying to think of the math formula that states how fast a crystal
oscillator would have to be to provide a certain level of milli- or
micro-second accuracy, but I can't find it right now. Maybe that's the
issue with "cheap" RTCs.

Or maybe I should just declare victory with my +/-0.01ms accuracy and
move on to a more productive sub-project? I'm leaning toward the latter...

Cheers,

Murray

----
The clock_test.py script and the rest of my 'ros' project can be found at:

   https://github.com/ifurusato/ros
   https://github.com/ifurusato/ros/blob/master/clock_test.py
   https://github.com/ifurusato/ros/blob/master/lib/clock.py
   https://github.com/ifurusato/ros/blob/master/lib/pid.py

I've commented out the Potentiometer import in the checked-in file so the
test script can theoretically run on any computer (wouldn't have to be a
robot) with the proper imports/installs. E.g., I ran it on my MacBook on
the train to work this morning.

* https://stackoverflow.com/questions/44644383/how-to-get-millisecond-resolution-from-ds3231-rtc
...........................................................................
Murray Altheim <murray18 at altheim dot com>                       = =  ===
http://www.altheim.com/murray/                                     ===  ===
                                                                    = =  ===
     In the evening
     The rice leaves in the garden
     Rustle in the autumn wind
     That blows through my reed hut.
            -- Minamoto no Tsunenobu



More information about the DPRGlist mailing list