Time_boot_ms to real time?

Hello,
I have a question about this time_boot_ms value.
I retrieve the realtime rov location with : recv_match(type=‘GLOBAL_POSITION_INT’) and the time stamp it has is the ‘time_boot_ms’. I am wondering how I can relate this time to the real time accurately?

thanks.

Hello,

All Mavlink2 messages have a timestamp in the header
Try this:

import time
from datetime import datetime
from pymavlink import mavutil

master = mavutil.mavlink_connection('udpin:0.0.0.0:14550')

while True:
    try:
        message = master.recv_match()
        print(datetime.utcfromtimestamp(message._timestamp).strftime('%Y-%m-%d %H:%M:%S.%f'))
    except:
        pass
    time.sleep(0.1)

Willian, Thanks!
I was able to use your code to get the DateTime output (I use the /dev/ttyACM0 connection as i am running on the raspberry pi).

Two follow up questions here:

  1. Why the time output I got is like 2019-06-12 xx:xx:xx.xxxx. What could be the reason for the time shift?
  2. How to link those DateTime output to the real-time ROV position output I got from:
    master.recv_match(type=‘GLOBAL_POSITION_INT’).to_dict()
    Thanks!

That is odd, maybe that timestamp is from the raspberry pi, which only updates date and time when it is online.

Try checking the “SYSTEM_TIME” messages

That’s right. The timestamp message returned the local machine time (which is 6/12 now).

Do I use
print(datetime.utcfromtimestamp(message._SYSTEM_TIME).strftime(‘%Y-%m-%d %H:%M:%S.%f’))

?

How about the 2nd question?

Thanks a lot!

SYSTEM_TIME is a mavlink message, so you should use master.recv_match(type=‘SYSYEM_TIME’).to_dict()

For the SYSTEM_TIME message, I think the best way is to read the first SYSTEM_TIME and store both the time reported by it and time_boot_ms. With this you can calculate when the Pixhawk booted (let’s call it base_time) and then, to get the real time from a message you can just do base_time + time_boot_ms.