一、建立连接
from pymavlink import mavutil # Start a connection listening on a UDP port the_connection = mavutil.mavlink_connection('udp:localhost:14540') # Wait for the first heartbeat # This sets the system and component ID of remote system for the link the_connection.wait_heartbeat() print("Heartbeat from system (system %u component %u)" % (the_connection.target_system, the_connection.target_component)) # Once connected, use 'the_connection' to get and send messages
连接字符串
[protocol:]address[:port]
- protocol (optional): The IP protocol. If not specified pymavlink will attempt to determine if the address is a serial port (e.g. USB) or a file, and if not will default to a UDP address.
tcp
: Initiate a TCP connection on the specifiedaddress
andport
.tcpin
: Listen for a TCP connection on the specifiedaddress
andport
.udpin
: Listen for a UDP connection on the specifiedaddress
andport
.udpout
: Initiate a TCP connection on the specifiedaddress
andport
.udp
: By default, same asudpin
. Setmavlink_connection
parameterinput=False
to make same asudpout
.udpcast
: Broadcast UDP address and port. This is the same asudp
withmavlink_connection()
parametersinput=False
andbroadcast=True
.
- address: IP address, serial port name, or file name
- port: IP port (only if address is an IP address)
Some of the strings you can use for different types of connections are listed below.
Connection type | Connection string |
---|---|
Linux computer connected to the vehicle via USB | /dev/ttyUSB0 |
Linux computer connected to the vehicle via Serial port (RaspberryPi example) | /dev/ttyAMA0 (also set baud=57600) |
MAVLink API listening for SITL connection via UDP | udpin:localhost:14540 (or udp:localhost:14540, 127.0.0.1:14540,etc.) |
MAVLink API initiating a connection to SITL via UDP | udpout:localhost:14540 (or udpout:127.0.0.1:14540) |
GCS connected to the vehicle via UDP | 127.0.0.1:14550 or udp:localhost:14550 |
SITL connected to the vehicle via TCP | tcp:127.0.0.1:5760 (ArduPilot only, PX4 does not support TCP) |
OSX computer connected to the vehicle via USB | dev/cu.usbmodem1 |
Windows computer connected to the vehicle via USB (in this case on COM14) | com14 |
Windows computer connected to the vehicle using a 3DR Telemetry Radio on COM14 | com14 (also set baud=57600) |
二、发送消息
方式1、使用封装函数
封装函数位置: ../pymavlink/mavutil.py => mavlink_connection
from pymavlink import mavutil the_connection = mavutil.mavlink_connection('udp:localhost:14540') the_connection.wait_heartbeat() # 获取航线列表 the_connection.waypoint_request_list_send()
注意:该方式单个mavlink连接不支持多架无人机共用通信
方式2、使用<message_name>_send()直接发送
<message_name>_send()函数位置: ../pymavlink/dialects/v20/ardupilotmega.py => MAVLink
../pymavlink/dialects/v10/ardupilotmega.py => MAVLink
from pymavlink import mavutil the_connection = mavutil.mavlink_connection('udp:localhost:14540') the_connection.wait_heartbeat() # 获取航线列表 target_system = the_connection.target_system target_component = the_connection.target_compontent the_connection.mav.mission_request_list_send(target_system, target_component)
注意:单个mavlink连接复用多架无人机通信时,可以使用该方式
方式3、使用send()发送<message_name>_encode()生成的mavlink消息
<message_name>_encode()函数位置: ../pymavlink/dialects/v20/ardupilotmega.py => MAVLink
../pymavlink/dialects/v10/ardupilotmega.py => MAVLink
from pymavlink import mavutil the_connection = mavutil.mavlink_connection('udp:localhost:14540') the_connection.wait_heartbeat() # 获取航线列表 target_system = the_connection.target_system target_component = the_connection.target_compontent # 生成mavlink消息 # pymavlink.dialects.v20.ardupilotmega.MAVLink_param_request_list_message mission_request_list_message = the_connection.mav.mission_request_list_encode(target_system, target_component) # 发送mavlink消息 the_connection.mav.send(mission_request_list_message)
方式4、使用write()发送mavlink消息bytes
from pymavlink import mavutil the_connection = mavutil.mavlink_connection('udp:localhost:14540') the_connection.wait_heartbeat() # 获取航线列表 target_system = the_connection.target_system target_component = the_connection.target_compontent # 生成mavlink消息 # pymavlink.dialects.v20.ardupilotmega.MAVLink_param_request_list_message mission_request_list_message = the_connection.mav.mission_request_list_encode(target_system, target_component) # mavlink消息打包成字节码 mission_request_list_message_bytes = mission_request_list_message.pack(the_connection.mav) # 发送mavlink消息字节码 the_connection.write(mission_request_list_message_bytes)
三、接收消息
1、recv_msg()
from pymavlink import mavutil the_connection = mavutil.mavlink_connection('udp:localhost:14540') the_connection.wait_heartbeat() # mavlink消息 msg = the_connection.recv_msg()
2、recv_match()
from pymavlink import mavutil the_connection = mavutil.mavlink_connection('udp:localhost:14540') the_connection.wait_heartbeat() # mavlink消息 msg = the_connection.recv_match(type='SYS_STATUS', condition='SYS_STATUS.mode==2 and SYS_STATUS.nav_mode==4', block=True)
四、发送一个心跳
# Send heartbeat from a GCS (types are define as enum in the dialect file). the_connection.mav.heartbeat_send(mavutil.mavlink.MAV_TYPE_GCS, mavutil.mavlink.MAV_AUTOPILOT_INVALID, 0, 0, 0) # Send heartbeat from a MAVLink application. the_connection.mav.heartbeat_send(mavutil.mavlink.MAV_TYPE_ONBOARD_CONTROLLER, mavutil.mavlink.MAV_AUTOPILOT_INVALID, 0, 0, 0)
五、消息签名
# Assuming you already have a connection set up the_connection = mavutil.mavlink_connection(...) # Create a callback to specify the messages to accept def my_allow_unsigned_callback(self,msgId): #Allow radio status messages if msgId==mavutil.mavlink.MAVLINK_MSG_ID_RADIO_STATUS: return True return False # Pass the callback to the connection (here we also pass an arbitrary secret key) secret_key = chr(42)*32 the_connection.setup_signing(secret_key, sign_outgoing=True, allow_unsigned_callback=my_allow_unsigned_callback)
六、生成mavlink消息bytes
from pymavlink import mavutil the_connection = mavutil.mavlink_connection('udp:localhost:14540') the_connection.wait_heartbeat() # 获取航线列表 target_system = the_connection.target_system target_component = the_connection.target_compontent # 生成mavlink消息 # pymavlink.dialects.v20.ardupilotmega.MAVLink_param_request_list_message mission_request_list_message = the_connection.mav.mission_request_list_encode(target_system, target_component) # mavlink消息打包成字节码 mission_request_list_message_bytes = mission_request_list_message.pack(the_connection.mav)
七、mavlink消息bytes转mavlink消息
from pymavlink import mavutil from pymavlink.dialects.v20.ardupilotmega import MAVLink the_connection = mavutil.mavlink_connection('udp:localhost:14540') the_connection.wait_heartbeat() # 获取航线列表 target_system = the_connection.target_system target_component = the_connection.target_compontent # 生成mavlink消息 # pymavlink.dialects.v20.ardupilotmega.MAVLink_param_request_list_message mission_request_list_message = the_connection.mav.mission_request_list_encode(target_system, target_component) # mavlink消息打包成字节码 mission_request_list_message_bytes = mission_request_list_message.pack(the_connection.mav) # mavlink消息bytes转mavlink消息 # 创建MAVLink对象用来解析bytes数据 mavlink_obj = MAVLink('parse_message') # 解析mavlink消息bytes mavlink_message = mavlink_obj.parse_buffer(mission_request_list_message_bytes)[0] msg_dict = mavlink_message.to_dict()
参考:https://mavlink.io/en/mavgen_python/#setting_up_connection
标签:pymavlink,target,request,使用,list,connection,mavlink,mavutil From: https://www.cnblogs.com/holden1/p/16778313.html