How to sync time between Robot and Host machine for ROS2
When I started working with ROS2, the typical setup involved having a Robot machine [something like Raspberry Pi or Jetson nano] and a Host computer either a laptop/desktop or a virtual machine/docker running on laptop. While running ROS2 nodes that were distributed between Robot machine and Host computer, lot of times I ran into errors where the Nav2 or SLAM or EKF nodes would complain about the messages being missed or out of sync because the time between the two computers was off.
I tried to research and tried a few solutions before the one described below worked pretty well. After setting it up once, I didn’t have to go back and muck with it so it seems to do its job well and without much trouble. Assumption here is — both host and robot computer are running Linux [Ubuntu 20.04 with ROS2 Foxy in my case].
Setup Chrony on Ubuntu [Perform these steps on both Host computer and Robot computer]
# install chrony
sudo apt install chrony# check status of chronyd daemon
systemctl status chronyd# restart chronyd daemon
systemctl restart chronyd# check the status of how it is tracking [including ref server, skew, offset, update interval etc]
chronyc tracking# this will show you the names of time servers with several backups
chronyc sources
After these initial steps are done we need to setup the Robot computer to use Host computer as a reference server to sync time with. This way the Host computer syncs with time servers and the Robot computer syncs with Host computer and everything stays in sync.
This is a two step process
- Edit the /etc/chrony/chrony.conf file on server [Host computer] and add following line at the end of file [assuming this is your subnet]:
allow 192.168.1.0/24
If you are unsure about your subnet, run following command in terminal and it will show you the ip address as well as subnet information.
ip addr
2. Edit the /etc/chrony/chrony.conf file on client [Robot computer and add following line at the end of file where XX is the number you get by running ip addr command above [or simply put the ip address of your host computer]. If you have multiple robot computers, you have to repeat this step with each.
server 192.168.1.XX iburst
After that just restart the chrony daemon like this and you are good to go.
systemctl restart chronyd
Enjoy!!
References: