I’m trying to capture the live barometer data using pymavlink. To do this I have requested a message stream at 25hz from both the barometric sensor and cube system time.
def request_message_interval(self,message_id: int, frequency_hz: float):
# This is done via command_long_send (sends a command with up to 7 paramaters (floats))
self.conn.mav.command_long_send(self.conn.target_system, self.conn.target_component, mavutil.mavlink.MAV_CMD_SET_MESSAGE_INTERVAL, 0, message_id, 1e6 / frequency_hz, 0,0,0,0,0)
# Retrieve BAROMETRIC data (MESSAGE ID: #29)
self.request_message_interval(mavutil.mavlink.MAVLINK_MSG_ID_SCALED_PRESSURE, 25)
# Retrieve SYSTEM_TIME data (MESSAGE ID: #2)
self.request_message_interval(mavutil.mavlink.MAVLINK_MSG_ID_SYSTEM_TIME, 25)
I have noticed that if I set my loop to receive these messages at a rate slower than 25hz, my sensor readings lag behind.
while(1):
time_UX = self.conn.recv_match(type='SYSTEM_TIME', blocking=True, timeout=1)
if time_UX is not None:
time_UX = time_UX.to_dict()
self.cubeTime = time_UX["time_boot_ms"]*1e-3 # divide by 1000, milliseconds to seconds
else:
pass
baroData = self.conn.recv_match(type='SCALED_PRESSURE', blocking=True, timeout=1)
if baroData is not None:
baroData = baroData.to_dict()
self.baro = baroData["press_abs"]*100.0
else:
pass
print(f"telemetry data \t {iter_number} \t {self.threadTime - self.threadTime0:.2f} \t {self.cubeTime - self.cubeTime0:.2f} \t {self.baro:.2f}")
time.sleep(5)
Output:
telemetry data iter_number thread_time cube_time barometer
telemetry data 0 0.42 0.00 102206.43
telemetry data 1 5.45 0.04 102206.73
telemetry data 2 10.46 0.08 102206.73
telemetry data 3 15.49 0.12 102206.73
telemetry data 4 20.50 0.16 102205.76
My understanding is that rather than returning the most recent barometric value, it is instead returning each message in the order that it was sent. How can I set it so that every 5 seconds, the most recent message is received instead.
I know I can fix this by matching the message interval with the receiving interval, but for my project, I don’t have a consistent receiving rate so I can’t rely on that.
Thanks.