similar to this guy, I have been trying to get GPS time from a Pixhawk into a Raspberry Pi companion computer.
Before trying out the @vehicle.on_message('SYSTEM_TIME')
approach, I used a wildcard to catch all messages and dumped the results into a file. Examining the file, I saw that the only type of message I am getting from the GPS is GPS_RAW_INT. Most importantly, I am not getting the SYSTEM_TIME_UTC message which, according to the documentation, fetches UTC time from the GPS.
Now, using the @vehicle.on_message('SYSTEM_TIME')
works fine, I am getting something. However, what is that something? The documentation claims it is:
The system time is the time of the master clock, typically the computer clock of the main onboard computer.
OK, I presume the "main onboard computer" is the Pixhawk. But how does it get the time if there is no GPS message containing it. The GPS_RAW_INT does not carry any time info.
Investigating further, I wrote the following handler:
@vehicle.on_message('SYSTEM_TIME')
def listener(self, name, message):
rpi_time = time.time()
mavlink_time = float(message.time_unix_usec)/1000000
print( str(rpi_time) + " - " + str(mavlink_time) + " = " + str(rpi_time-mavlink_time) )
rpi_time comes from the Raspberry Pi and is correct since it is set by NTP at boot time. I wanted to verify that mavlink_time (which I presumed has to be GPS based time) would be pretty close. I was wrong.
Running this multiple times, I notice that, at first, GPS time is always about two seconds ahead. As time goes by, it drops behind. Sample output:
1483754011.89 - 1483754014.72 = -2.83385300636
1483754012.16 - 1483754014.96 = -2.79546308517
1483754012.44 - 1483754015.2 = -2.75652289391
...
1483754154.69 - 1483754044.5 = 110.186414957
1483754173.72 - 1483754044.76 = 128.95413518
1483754184.04 - 1483754045.02 = 139.016195059
In this case, where rpi_time (which is correct), says 172 seconds have elapsed, mavlink_time claims only 30. Other trials showed wild variations (the discrepancy is by no means fixed).
So, my questions are:
- What is the SYSTEM_TIME message based on? Realistically, it can only be GPS (it's the only time data available to the Pixhawk) but, if that is the case...
- why is it so inaccurate and why do I not also get SYSTEM_TIME_UTC messages?
Yes, system time is based on GPS.
Are you doing your testing inside? The GPS module computes position, velocity and time together. If it does not have a good lock, it might not produce a good time.
I think the
SYSTEM_TIME_UTC
message was deprecated.