我已经玩了几天了,但无法弄清楚如何更改 MAVlink 消息间隔。我正在尝试获得流畅的 GLOBAL_POSITION_INT 流,但无论我进行什么更改,我似乎每秒都会获取 1-2 次数据。
我试过同时使用“request_data_stream”和“MAV_CMD_SET_MESSAGE_INTERVAL”,但没有成功。这是我对 message_interval 的尝试(主要是从示例中复制粘贴):
def request_message_interval(message_id: int, frequency_hz: float):
master.mav.command_long_send(
master.target_system, master.target_component,
mavutil.mavlink.MAV_CMD_SET_MESSAGE_INTERVAL, 0,
message_id, # The MAVLink message ID
1e6 / frequency_hz,
# The interval between two messages in microseconds. Set to -1 to disable and 0 to request default rate.
0, 0, 0, 0, # Unused parameters
0,
)
request_message_interval(mavutil.mavlink.MAVLINK_MSG_ID_GLOBAL_POSITION_INT,
20)
这是我让 data_stream 工作的尝试:
def request_data_stream(message_id: int, frequency_hz: float):
master.mav.request_data_stream_send(
master.target_system, master.target_component,
message_id, # The MAVLink message ID
frequency_hz,
1,#start
)
request_data_stream(mavutil.mavlink.MAV_DATA_STREAM_POSITION, 20)
我收到消息,然后将其传递到程序的其他部分:
while True:
time.sleep(0.1)
try:
master.mav.heartbeat_send(mavutil.mavlink.MAV_TYPE_GCS, mavutil.mavlink.MAV_AUTOPILOT_INVALID, 0, 0, 0)
msg = master.recv_match(type='GLOBAL_POSITION_INT', blocking=True)
# print("Message: %s" % msg)
lon = master.messages['GLOBAL_POSITION_INT'].lon
lat = master.messages['GLOBAL_POSITION_INT'].lat
alt = master.messages['GLOBAL_POSITION_INT'].alt
hdg = master.messages['GLOBAL_POSITION_INT'].hdg
print(f"lon:{lon}, lat:{lat}, alt:{alt}, hdg:{hdg}")
calc_azimuth(str(ref_lon), str(ref_lat), str(lon), str(lat))
except:
pass
我觉得我在这里遗漏了一些明显的东西,但我无法弄清楚为什么消息进来这么慢。