tastycrust
Posts: 49
Joined: Fri Feb 14, 2014 4:59 am

How to use NTP to sync two pi's clocks (don't care about accuracy)?

Sat Feb 17, 2018 5:03 pm

So I've never used NTP before, but I'm working my way through man ntp.conf(5) and having some trouble deciding if this is the right solution for my problem. I'm hoping I can figure this out on my own, but thought I'd toss this out in case there's anyone here who has experience doing something similar and can give pointers. I'll update this thread as I muddle my way through this on my own, too!

Specifically, I want to use NTP to sync time between two physically connected RPi Zero W's over their serial ports, and I'd like to see how closely I can get them synchronized if I don't care about drift (as long as they drift together). My goal is to have them feed realtime video feeds to a central server on my network, which will then essentially stitch the two feeds together into a 3D image for further processing by OpenCV, relying on timestamps to know which frames were taken "simultaneously."

It looks like NTP is usually used by people who care about time accuracy more than synchronization, so most of the examples in the man pages assume you are trying to get an accurate time from an outside source and then propagate that time across your network. However, I don't care about the accuracy of my time, as long as both RPi's have as close as possible to the exact same time (within a few milliseconds would be ideal). I assume I need set them both up as servers and also as peers, without including any outside servers/pools? Is this even possible using NTP over serial, or do I need to get some kind of external card that has a RTC if I want this kind of synchronization to be possible?

P.S. Here's a photo of a preliminary setup (the two cases are super-glued together to ensure consistent relative position of cameras)...
IMG_20180216_230305.jpg
IMG_20180216_230305.jpg (109.46 KiB) Viewed 1434 times
Last edited by tastycrust on Sun Feb 18, 2018 11:11 pm, edited 2 times in total.

tastycrust
Posts: 49
Joined: Fri Feb 14, 2014 4:59 am

Re: How to use NTP to sync two pi's clocks (don't care about accuracy)?

Sat Feb 17, 2018 5:19 pm

I'll add that this isn't the only option I'm considering for how to sync the two RPi's, but since I want them to be connected by serial anyhow I thought it would be nice if the sync could occur over that and leave other GPIO pins for other things I might add later. If syncing over serial ends up being infeasible, I will absolutely consider using other GPIO pins to signal start/stop times instead.

User avatar
DougieLawson
Posts: 34166
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: How to use NTP to sync two pi's clocks (don't care about accuracy)?

Sat Feb 17, 2018 10:25 pm

Run one ntpd as a master, run any other ntpd systems as a slave to that. Master
/etc/ntp.conf

Code: Select all

driftfile /var/lib/ntp/ntp.drift
statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable
server 0.ubuntu.pool.ntp.org burst iburst maxpoll 11
server 1.ubuntu.pool.ntp.org burst iburst maxpoll 11
server 2.ubuntu.pool.ntp.org burst iburst maxpoll 11
server 3.ubuntu.pool.ntp.org burst iburst maxpoll 11
server ntp.ubuntu.com
restrict 127.0.0.1
restrict -6 ::1
restrict 192.168.3.0 mask 255.255.255.0
broadcast 192.168.3.255
broadcast ff05::101
keysdir /etc/ntp
crypto
slave config

Code: Select all

driftfile /var/lib/ntp/ntp.drift
statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable
server 192.168.3.14
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
restrict 127.0.0.1
restrict ::1
keysdir /etc/ntp
crypto
broadcastclient
multicastclient
manycastclient
I'm running with crypto enabled which needs sudo mkdir /etc/ntp ;cd /etc/ntp; sudo ntp_genkeys run on each machine.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

tastycrust
Posts: 49
Joined: Fri Feb 14, 2014 4:59 am

Re: How to use NTP to sync two pi's clocks (don't care about accuracy)?

Sat Feb 17, 2018 10:48 pm

DougieLawson wrote:
Sat Feb 17, 2018 10:25 pm
I'm running with crypto enabled which needs sudo mkdir /etc/ntp ;cd /etc/ntp; sudo ntp_genkeys run on each machine.
Thank you for those sample config files!

For the keygen, I assume that outputs a key pair into /etc/ntp/ and I should copy the public key to the other devices? Is there something analogous to authorized_keys in ~/.ssh that I should add them to?

SurferTim
Posts: 1767
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: How to use NTP to sync two pi's clocks (don't care about accuracy)?

Sat Feb 17, 2018 11:47 pm

I've used mplayer and UDP to sync video with good results. Don't know how you are using NTP to sync your video though. If you are planning on using NTP, it seems UDP might be better.

Just my opinion...

User avatar
DougieLawson
Posts: 34166
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: How to use NTP to sync two pi's clocks (don't care about accuracy)?

Sat Feb 17, 2018 11:48 pm

The keys are symmetric, no key copying needed.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

tastycrust
Posts: 49
Joined: Fri Feb 14, 2014 4:59 am

Re: How to use NTP to sync two pi's clocks (don't care about accuracy)?

Sun Feb 18, 2018 12:22 am

SurferTim wrote:
Sat Feb 17, 2018 11:47 pm
I've used mplayer and UDP to sync video with good results. Don't know how you are using NTP to sync your video though. If you are planning on using NTP, it seems UDP might be better.

Just my opinion...
I'm actually not sure how that would work (I have no experience streaming video), but my goal here is to pair video frames taken at the same time for a deep neural network (on my local network) to analyze them in realtime for object detection, depth perception, etc.

Each separate RPi0 will be sending video streams to a PC on my network, and that PC will attempt to pair frames that were taken simultaneously, to perform image analysis using OpenCV and perhaps other tools (still deciding the scope of that end). One of the RPi0's has the no-IR camera and the other is the normal camera, so I'm hoping each pair of images will yield some interesting data to play around with once they're properly synced up.

My goal with NTP was to get the system clocks synced to within a few milliseconds, and then each frame could be tagged with current time to the nearest millisecond. Then, the PC would pick the frames with matching timestamps to analyze together.

When I ping the RPi0's from the desktop machine over my wireless network, it's ~9ms ping, but varies a lot (6-14 in one set of pings, but just now it varied from 8 to 20ms when I tried again), so I worry that any system relying on adjusting for network latency will not be effective.

SurferTim
Posts: 1767
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: How to use NTP to sync two pi's clocks (don't care about accuracy)?

Sun Feb 18, 2018 11:54 am

tastycrust wrote:
SurferTim wrote:
Sat Feb 17, 2018 11:47 pm
I've used mplayer and UDP to sync video with good results. Don't know how you are using NTP to sync your video though. If you are planning on using NTP, it seems UDP might be better.

Just my opinion...
I'm actually not sure how that would work (I have no experience streaming video), but my goal here is to pair video frames taken at the same time for a deep neural network (on my local network) to analyze them in realtime for object detection, depth perception, etc.
...
When I ping the RPi0's from the desktop machine over my wireless network, it's ~9ms ping, but varies a lot (6-14 in one set of pings, but just now it varied from 8 to 20ms when I tried again), so I worry that any system relying on adjusting for network latency will not be effective.
You do realize NTP is a UDP protocol service, correct? How did you plan on using it?

You will have a problem getting <20ms accuracy with NTP or any TCP/UDP protocol. How many frames per second do you plan on capturing?

tastycrust
Posts: 49
Joined: Fri Feb 14, 2014 4:59 am

Re: How to use NTP to sync two pi's clocks (don't care about accuracy)?

Sun Feb 18, 2018 7:05 pm

My understanding is a local network can pretty easily sync to within a couple milliseconds using NTP. See, e.g.:

https://blog.rapid7.com/2014/03/14/sync ... e-problem/

Also the answers here:
https://serverfault.com/questions/62507 ... rs-in-sync

I'd like to achieve at least 12fps, but I'll have to figure out what the bottlenecks are and anything above 4fps would be worthwhile, I think.

SurferTim
Posts: 1767
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: How to use NTP to sync two pi's clocks (don't care about accuracy)?

Sun Feb 18, 2018 7:36 pm

Then what is your question? At even 20ms accuracy, you should be able to time 50fps.

tastycrust
Posts: 49
Joined: Fri Feb 14, 2014 4:59 am

Re: How to use NTP to sync two pi's clocks (don't care about accuracy)?

Sun Feb 18, 2018 9:25 pm

For binocular vision algorithms to work reasonably well, you want each frame's left and right component synced to within a few milliseconds even if you're only capturing a dozen frames per second.

tastycrust
Posts: 49
Joined: Fri Feb 14, 2014 4:59 am

Re: How to use NTP to sync two pi's clocks (don't care about accuracy)?

Mon Feb 19, 2018 6:52 pm

As a preliminary test I just got around to setting up NTP with the configs above, using this structure:

Code: Select all

     [x86 desktop]
         /  \
        /    \
    [RPi0a] [RPi0b]
And after running it for a few hours over wifi, the local network is synced to within 0.1ms (assuming that's what "offset" means"), though jittering about 5ms either way:

Code: Select all

[email protected]:~ $ ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*192.168.0.27    208.75.88.4      3 u   50  256  377    5.545   -0.071   4.934
One modification to the instructions above is that to generate the NTP keys in the version in the Raspbian repositories, the command was "ntp-keygen" not "ntp_genkeys." Otherwise it's working great!

tastycrust
Posts: 49
Joined: Fri Feb 14, 2014 4:59 am

Re: How to use NTP to sync two pi's clocks (don't care about accuracy)?

Wed Feb 21, 2018 8:16 pm

FYI, anyone interested in following along, I'll be updating the other thread about this project. It looks like NTP alone has me synced enough for my needs:
viewtopic.php?t=205309

Return to “Networking and servers”