首页 > 其他分享 >pymavlink使用

pymavlink使用

时间:2022-10-11 12:22:34浏览次数:40  
标签:pymavlink target request 使用 list connection mavlink mavutil

一、建立连接

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 specified address and port.
    • tcpin: Listen for a TCP connection on the specified address and port.
    • udpin: Listen for a UDP connection on the specified address and port.
    • udpout: Initiate a TCP connection on the specified address and port.
    • udp: By default, same as udpin. Set mavlink_connection parameter input=False to make same as udpout.
    • udpcast: Broadcast UDP address and port. This is the same as udp with mavlink_connection() parameters input=False and broadcast=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 typeConnection 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

相关文章

  • grep 命令使用
    egrep相当于grep-Egrep-A-B-C(大写)后面都跟阿拉伯数字-A是显示匹配后和它后面的n行。after-B是显示匹配行和它前面的n行。before-C是匹配行和它前后各n行。co......
  • Mysql 插入timestamp没有使用默认值问题
    在一次升级过程中,发现Mysql插入数据报了个错Column'create_time'cannotbenull.但是看了下这个字段虽然是非null,但是是有默认值的`create_time`timestampNOTNULL......
  • 挡土墙图集使用
    @目录一、图集二、种类三、基本参数选取四、挡土墙选型五、承载力复核一、图集选用:17J008范例:土质边坡,地基夯实到150KPa,厂区高差6米,6度区二、种类墙高:包含埋在地......
  • 使用CDN方式引用Vue3和ElementPlus
    使用CDN方式引用Vue3和ElementPlus​​引用Vue​​​​代码​​​​注意事项​​​​Elementplus​​​​代码​​​​注意事项​​升级了之前做的vue2+ElementUI的项目,......
  • 使用 IAsyncResult 进行 .NET 异步编程
    微软早在.net2.0,也就是VS2005的时候,就提供了一整套的异步编程设计模式,有3中常用的方式:1.使用IAsyncResult调用异步方法2.使用委托进行异步编程3......
  • AspNetCore中 使用 Grpc 简单Demo
    为什么要用Grpc跨语言进行,调用服务,获取跨服务器调用等目前我的需要使用我的抓取端是go写的查询端用Net6写的导致很多时候我需要把一些临时数据写入到Redis在两......
  • IIS 绿盟检测到HOST头攻击漏洞的解决:web应用使用SERVER_NAME而非host header。
    来源:https://blog.csdn.net/fightingintherain/article/details/1256648851、漏洞描述2、修复方案(IIS服务端) 1)下载安装url重写工具(官网URLRewrite:TheOfficial......
  • tcpdump 简单使用
    语法解析tcpdump-vvnn-c10-s0-ieth0“tcpdump原语表达式”-vvnn:显示ip地址而不是主机名-c:抓包次数-s:抓包大小,大于这个值的包内容会被截断,0表示不限制大小,显示全......
  • 通过client-go使用yaml文件,部署服务到K8S
    通过调用client-go客户端使用yaml文件的方式部署服务到K8S集群内funcmain(){ namespace:="test" config,err:=clientcmd.BuildConfigFromFlags("","config") ......
  • 使用MobaXterm提示Network error: Software caused connection abort
    错误:使用MobaXterm连接centos,经常断开连接提示信息:Networkerror:Softwarecausedconnectionabort 解决方案:修改服务器中/etc/ssh/sshd.config文件将LoginGrac......