Posted by John Haine on 24/03/2023 07:29:47:
Do you really need to do this? … it's the counter that converts frequency to phase (time) that needs synchronising to ntp surely? …
Yes that's right – I don't need to do what I described. It's the counter that needs synchronising, not pendulum.
I asked the question because my logic and solution both felt overcomplicated. Now I've explained my thinking, and seen the responses, I realise my approach was muddled, and it is overcomplicated. I fixated on using the clock's ability to predict bob position.
Martin got in first by asking 'Is it really necessary to synchronise the pendulum with the ref.', at which point my mental model collapsed because the answer is 'NO'.
Proof of the pudding is in the eating. Took me about 10 hours cursing to write and debug the code for my complicated approach that only set the clock with 0.025s. Abandoning that, the alternative took under 10 minutes to write and test – it's much simpler.
What it does now is:
- On receiving a set-clock command, the raspberryPi spins as before to get just past the next whole NTP second.
- The second value is sent as a UNIX timestamp to my clock, with microseconds zeroed.
- My clock loads the timestamp into the counter
- On the next beat, the counter is incremented by one period, and the result, also a UNIX timestamp, is sent back to the raspberry
- The raspberry calculates the difference between my clock's time stamp and NTP time. The difference is sent back to my clock which applies it as a correction to the counter.
Needs more work because the protocol makes no allowance for transmission and processing delays. I'm going to write code to test these further: the exchange over USB serial takes about 0.0002s, which can be compensated out, but I don't know how much the exchange time varies. It's not an exact constant.
Michael's got me thinking about sub-second accuracy. Hopefully the rebuild will improve my frisky pendulum – i's not supposed to be all of a twitch! Assuming I can get it to swing reliably, I plan to use the pendulum's compensated period to set a counter timer to generate a 1s NTP aligned pulse at the next second point. Say the clock ticks and increments the clock's counter to a value like 1679653264.750020 . That means the Arduino has to output a seconds pulse 0.24998s later. I propose to do that with another counter/timer, setting it with numbers in which the Arduino's oscillator frequency is calculated from the pendulum. Not sure how accurately it's possible to set the counter to multi-digit delays like 0.24998s. As the counter is binary, the worst case error might be horrible. If the method works at all, should be possible to generate 0.1s pulses in the same way.
Dave