Mavlink synchronization with MCU

Hello everyone,

For the problem I’m going to describe I have already made some research but taking into account that I am not an expert in code development I would really like you to clarify some doubts I have.

To begin, what I’m trying to do is send Mavlink messages via UART to a MCU and make the best synchronization possible between the timestamps seen at the autopilot and an the clock of the MCU.

What I would like to do, is to take a GPS message and match it to the sensor data (not a camera) viewed in the MCU. The way I’m doing it for the moment is

  1. Take the sensor data and match it to the time of the MCU (t_MCU) at regular intervals (I’m confident that the data rates are stable and well-controlled.
  2. Receive the MAVLINK data stream and match every message with the t_MCU, sot that every Mavlink message is linked, at its reception, to the MCU.
  3. Match every GPS position message to the sensor data with the help of its t_MCU

The problem is the time seen in the mavlink messages, for example :
GPS_RAW_INT {time_usec : 66930000, fix_type : 3, lat : 485235991, lon : 77370416, alt : 166500, eph : 99, epv : 158, vel : 18, cog : 13899, satellites_visible : 12}

This message arrives at a time that does not correspond to the MCU time, because as it is expected, there is an offset between the two systems (FCU and MCU). This offset is not constant and it can vary up to 300ms bteween the two clocks, which I find difficult to achieve the synchronization.

The solution I’m exploring at the moment is the use of TIMESYNC. As I read the forums and blogs this is the first step to take into account. The thing is that TIMESYNC is not completely clear to me, because I think there is not enough documentation and all I can find is this link that I’m finding hard to understand given my lack of experience in the field :

I would really appreaciate if someone could give me a hint on how to achieve a good synchronization.
I don’t know if the times I’m seeing are normal, if I should really use TIMESYNC or there is a simple way…

I’ve been working with this since a time now and would really like to finish this project as soon as possible :sob:
Thank you for your time!