Skip to content

Time Sync with Internet or local NTP

In rocket development and experiments, we use Foxglove Studio to visualize quite a number of topics to facilitate debugging. Since we stream quite a number of topics at high rates (~300HZ), it's best to visualize with "Header Timestamp" instead of "Received Time" in Foxglove, due to possibility of messages being buffered and sent in uneven intervals.

That being said, Foxglove would then require the robot to have a reasonable timestamp, compared to the laptop. This page describes briefly how to use commands to synchronize time with the Internet, or how to setup a local NTP server and sync time with it.

Syncing time with Internet access

If the robot has internet access, but the time is not automatically updated, one could use these commands to check the clock synchronization status and force the sync.

Checking:

timedatectl

Force sync:

sudo systemctl restart systemd-timesyncd.service

Check again and the date-time should be correct local time.

Reference: https://ubuntu.com/server/docs/use-timedatectl-and-timesyncd

Setting up a local NTP server and synchronizing the robot to it

It's also possible to just sync robot to a local NTP server running on a machine in the local network, e.g. a PC/laptop. This could come in handy, when the LAN doens't have internet (e.g. current Qualisys system is connected to a router without internet, and the robot has to be in the same network).

Setting up a local NTP server

Ref: cturra/docker-ntp

The docker-compose.yml file:

version: '3.9'

services:
  ntp:
    build: .
    image: cturra/ntp:latest
    container_name: ntp
    # # to always restart the container (even on reboots)
    # restart: always
    ports:
      - 123:123/udp
    environment:
      - NTP_SERVERS=127.127.1.1
      - LOG_LEVEL=0
#      - TZ=America/Vancouver
#      - NOCLIENTLOG=true
#      - ENABLE_NTS=true

And launch in the same folder with:

docker compose up -d

(optional) check the logs of the running docker container. It should somewhat look like:

docker logs ntp

# --- below are the output ---
ntp  | 2024-03-14T16:41:27Z chronyd version 4.5 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +NTS +SECHASH +IPV6 -DEBUG)                                                               
ntp  | 2024-03-14T16:41:27Z Disabled control of system clock

Finally, obtain the computer's IP address for the robot to connect to. For examples below, the IP 192.168.1.163 is the IP of the NTP server/laptop.

Configuring the robot to sync to that NTP server

Edit the NTP server address in /etc/systemd/timesyncd.conf, by uncommenting the #NTP= section and assigning it to the IP we found previously of the NTP server.

...
[Time]
NTP=192.168.1.163
...

Save the above config file, and restart the time synchronizer:

sudo systemctl restart systemd-timesyncd.service

And verify the status of the service in a minute, info similar to the following should appear:

systemctl status systemd-timesyncd.service

# --- below are the output of the command ---
● systemd-timesyncd.service - Network Time Synchronization
     Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2024-03-14 17:51:50 CET; 2min 13s ago
       Docs: man:systemd-timesyncd.service(8)
   Main PID: 9138 (systemd-timesyn)
     Status: "Initial synchronization to time server 192.168.1.163:123 (192.168.1.163)."
      Tasks: 2 (limit: 4127)
     Memory: 1.1M
        CPU: 370ms
     CGroup: /system.slice/systemd-timesyncd.service
             └─9138 /lib/systemd/systemd-timesyncd

Mar 14 17:51:50 emborocket systemd[1]: Starting Network Time Synchronization...
Mar 14 17:51:50 emborocket systemd[1]: Started Network Time Synchronization.
Mar 14 17:51:50 emborocket systemd-timesyncd[9138]: Initial synchronization to time server 192.168.1.163:123 (192.168.1.163).

And you may check again the time on the robot has been synchronized with the laptop.

timedatectl

P.S. remember to backup and restore the config file

Otherwise there might be problems syncing the time with the Internet option.