Synchronising clocks between GS and UAV

Hi,
I’m trying to get the system clocks on both the ground station and the UAV(running arducopter) to be synchronised, ideally to sub-millisecond accuracy. The ground station connects over wifi to a companion computer (Jetson nano) which runs a mavproxy relay to the Ardupilot board connected via USB. This is also indoors, so GPS can’t be used.

I’m using the system_time module in mavproxy. (https://ardupilot.org/mavproxy/docs/modules/systemtime.html)
I’ve set BRD_RTC_TYPES to 2 on the UAV
Running the module in verbose mode, it seems to be sending and receiving packets.
When I use the Mavlink inspector, the system time (time_unix_usec) on the ardupilot remains set at 0.

Anyone have any tips on how to get this working?
Thanks in advance.

I think you want “1” here.

According to https://ardupilot.org/dev/docs/ros-timesync.html, BRD_RTC_TYPES should be 2, to select MAVLINK_SYSTEM_TIME in the bitmask. Nonetheless, I have also tried setting the parameter to 1, but nothing changed.

Sorry, my bad, 2 is the correct value.

I’ve tested this locally and it does seem to Work For Me™

STABILIZE> 29: SYSTEM_TIME {time_unix_usec : 0, time_boot_ms : 15401}
status SYSTEM_TIME
STABILIZE> 32: SYSTEM_TIME {time_unix_usec : 0, time_boot_ms : 16151}
status SYSTEM_TIME
STABILIZE> 34: SYSTEM_TIME {time_unix_usec : 0, time_boot_ms : 16651}
status SYSTEM_TIME
STABILIZE> 36: SYSTEM_TIME {time_unix_usec : 0, time_boot_ms : 17154}
status SYSTEM_TIME
STABILIZE> 38: SYSTEM_TIME {time_unix_usec : 0, time_boot_ms : 17654}
status SYSTEM_TIME
STABILIZE> 40: SYSTEM_TIME {time_unix_usec : 0, time_boot_ms : 18153}
status SYSTEM_TIME
STABILIZE> 44: SYSTEM_TIME {time_unix_usec : 0, time_boot_ms : 19154}

STABILIZE> param show BRD_RTC_TYPES
STABILIZE> BRD_RTC_TYPES    2.000000

STABILIZE> status SYSTEM_TIME
STABILIZE> 87: SYSTEM_TIME {time_unix_usec : 0, time_boot_ms : 29903}

STABILIZE> module load system_time
STABILIZE> Loaded module system_time

STABILIZE> status SYSTEM_TIME
STABILIZE> 122: SYSTEM_TIME {time_unix_usec : 1606882725394270, time_boot_ms : 38653}
status SYSTEM_TIME
STABILIZE> 129: SYSTEM_TIME {time_unix_usec : 1606882727141399, time_boot_ms : 40401}

Hi Peter,
Thanks for your help with this.
I think I’ve found a bug:

 Traceback (most recent call last):
      File "/home/jackhenderson/miniconda3/bin/mavproxy.py", line 965, in periodic_tasks
        m.idle_task()
      File "/home/jackhenderson/miniconda3/lib/python3.8/site-packages/MAVProxy/modules/mavproxy_system_time.py", line 68, in idle_task
        self.master.mav.system_time_send(time_us,
    struct.error: required argument is not an integer

I’m running python 3.8, so perhaps there is a change in precision of the time() function. If I cast time_us the value to an int in line 68 (and the other similar lines of code), then it seems to work correctly.
I’m interested in understanding how the synchronisation works, so I’ll explore the code some more. Once I’ve figured it out, I’ll try an put in a pull request to fix it up.

Gah! I found and fixed that during my testing, but completely left it in
my dirty tree.

PR is here: https://github.com/ArduPilot/MAVProxy/pull/848