Syncronize multiple PiMotion™ clocks¶
This Python code syncronizes the clocks on two PiMotion™ devices.
import pilib
import time
def ns_to_s(t):
# function to convert nanoseconds to seconds
return (float)(t/1000000000.)
def set_master(ip, master):
pm = pilib.Pidev(ip)
old_master = pm.config_get_ptp_master()
if old_master != master:
pm.config_set_ptp_master(master)
pm.config_commit()
try:
pm.config_reset()
except:
pass
print 'resetting', ip
time.sleep(10) #wait for reboot
return old_master
#configure one master, one slave
old1 = set_master('192.168.1.201', 1)
old2 = set_master('192.168.1.200', 0)
# make connections
pm1 = pilib.Pidev('192.168.1.201')
pm2 = pilib.Pidev('192.168.1.200')
for i in range(60):
time.sleep(1)
offset = pm2.time_get_offset_from_master()
print 'Clock offset from master:', offset, 'ns'
#restore configuration
set_master('192.168.1.201', old1)
set_master('192.168.1.200', old2)