Configuring NTP
Network Time Protocol, or NTP, is a network protocol that allows computers on a network to synchronize their clocks with each other. This is essential for ROS, as many types of data are time-sensitive.
Chrony
Clearpath uses a Linux package called chrony
to provide clock synchronization across multiple devices inside a robot.
The robot's primary PC acts as the main NTP source for other computers inside the robot.
To (re-)configure chrony
, edit the file /etc/chrony/chrony.conf
. The following shows the default configuration
for the primary PC:
# Welcome to the chrony configuration file. See chrony.conf(5) for more
# information about usuable directives.
# This will use (up to):
# - 4 sources from ntp.ubuntu.com which some are ipv6 enabled
# - 2 sources from 2.ubuntu.pool.ntp.org which is ipv6 enabled as well
# - 1 source from [01].ubuntu.pool.ntp.org each (ipv4 only atm)
# This means by default, up to 6 dual-stack and up to 2 additional IPv4-only
# sources will be used.
# At the same time it retains some protection against one of the entries being
# down (compare to just using one of the lines). See (LP: #1754358) for the
# discussion.
#
# About using servers from the NTP Pool Project in general see (LP: #104525).
# Approved by Ubuntu Technical Board on 2011-02-08.
# See http://www.pool.ntp.org/join.html for more information.
pool ntp.ubuntu.com iburst maxsources 4
pool 0.ubuntu.pool.ntp.org iburst maxsources 1
pool 1.ubuntu.pool.ntp.org iburst maxsources 1
pool 2.ubuntu.pool.ntp.org iburst maxsources 2
# This directive specify the location of the file containing ID/key pairs for
# NTP authentication.
keyfile /etc/chrony/chrony.keys
# This directive specify the file into which chronyd will store the rate
# information.
driftfile /var/lib/chrony/chrony.drift
# Uncomment the following line to turn logging on.
#log tracking measurements statistics
# Log files location.
logdir /var/log/chrony
# Stop bad estimates upsetting machine clock.
maxupdateskew 100.0
# This directive enables kernel synchronisation (every 11 minutes) of the
# real-time clock. Note that it can’t be used along with the 'rtcfile' directive.
rtcsync
# Step the system clock instead of slewing it if the adjustment is larger than
# one second, but only in the first three clock updates.
makestep 1 3
# Configure this host to act as the NTP source for the rest of the devices in this robot
local stratum 10
allow 192.168.131/24
For secondary PCs inside the robot, the chrony
configuration file is very similar, but adds a new server
directive
after the pool
, and omits the final local
and allow
sections:
# Welcome to the chrony configuration file. See chrony.conf(5) for more
# information about usuable directives.
# This will use (up to):
# - 4 sources from ntp.ubuntu.com which some are ipv6 enabled
# - 2 sources from 2.ubuntu.pool.ntp.org which is ipv6 enabled as well
# - 1 source from [01].ubuntu.pool.ntp.org each (ipv4 only atm)
# This means by default, up to 6 dual-stack and up to 2 additional IPv4-only
# sources will be used.
# At the same time it retains some protection against one of the entries being
# down (compare to just using one of the lines). See (LP: #1754358) for the
# discussion.
#
# About using servers from the NTP Pool Project in general see (LP: #104525).
# Approved by Ubuntu Technical Board on 2011-02-08.
# See http://www.pool.ntp.org/join.html for more information.
pool ntp.ubuntu.com iburst maxsources 4
pool 0.ubuntu.pool.ntp.org iburst maxsources 1
pool 1.ubuntu.pool.ntp.org iburst maxsources 1
pool 2.ubuntu.pool.ntp.org iburst maxsources 2
# Add the robot's primary PC as an NTP source
server 192.168.131.1 offline minpoll 8
# This directive specify the location of the file containing ID/key pairs for
# NTP authentication.
keyfile /etc/chrony/chrony.keys
# This directive specify the file into which chronyd will store the rate
# information.
driftfile /var/lib/chrony/chrony.drift
# Uncomment the following line to turn logging on.
#log tracking measurements statistics
# Log files location.
logdir /var/log/chrony
# Stop bad estimates upsetting machine clock.
maxupdateskew 100.0
# This directive enables kernel synchronisation (every 11 minutes) of the
# real-time clock. Note that it can’t be used along with the 'rtcfile' directive.
rtcsync
# Step the system clock instead of slewing it if the adjustment is larger than
# one second, but only in the first three clock updates.
makestep 1 3
Work-around for no RTC
Most computers have a realtme clock (RTC) that keeps time while the computer is otherwise powered-off. This clock has a replaceable battery, referred to either as the RTC backup battery or CMOS battery. If you are using a computer that does not have an RTC backup battery, or does not have a RTC at all, you may see unusual behaviour. Typically the clock will reset to a default date (often January 1, 1970) when the computer loses power. The clock will then jump ahead to the present as soon as a connection with an NTP server is established.
Under most circumstances this is fine, but some ROS nodes will behave incorrectly if the clock suddenly jumps ahead by
50+ years. To mitigate this, if you are configuring a computer that does not have a reliable RTC, modify the file
/lib/systemd/system/ros.service
to add an ExecStartPre
command to the service:
[Service]
ExecStart=/usr/sbin/ros-start
ExecStartPre=/usr/sbin/ros-wait-clock
Then create the file /usr/sbin/ros-wait-clock
by running
sudo nano /usr/sbin/ros-wait-clock
and entering the following:
#!/bin/bash
hw_time="$(hwclock -r -f /dev/rtc1)"
# wait at most 15s
for i in {1..15};
do
if [[ "$hw_time" == 19* ]];
then
echo "HW clock says it's ${hw_time}. Waiting for clock-sync..."
sleep 1
hw_time="$(hwclock -r -f /dev/rtc1)"
else
echo "HW clock says it's now ${hw_time}. Clock appears synchronized with reality"
exit 0
fi
done
echo "Wait for clock sync timed out. Moving ahead with possibly bad clock."
Save the file, close the editor, and make the file executable by running
sudo chmod +x /usr/sbin/ros-wait-clock