ardupilot之mavlink消息--飞控接收--单向

2023-05-16

由于项目需要,完成一个测试demo, 本次从dronekit中发送mavlink消息给飞控,飞控接收发来的wp信息,然后进行修改供程序使用。
首先祭出测试视频 dronekit_arudpilot_test

dronekit

  1. 首先从copter.cpp中找到到对应的函数,如下图所示:在这里插入图片描述
  2. 跳转进去
    在这里插入图片描述
  3. 再次套娃,可以得到下图:
    在这里插入图片描述
  4. 该函数中的最后一个函数 handleMessage();在这里插入图片描述
  5. 涉及的文件主要是 GCS_mavlink.cpp , 找到其中 headleMessage() 即可。里面是通过switch语句来判断对应的消息ID的, 如下面代码所示:
case MAVLINK_MSG_ID_SET_POSITION_TARGET_GLOBAL_INT:    // MAV ID: 86
    {
        // decode packet
        mavlink_set_position_target_global_int_t packet;
        mavlink_msg_set_position_target_global_int_decode(&msg, &packet);

        // exit if vehicle is not in Guided mode or Auto-Guided mode
        if (!copter.flightmode->in_guided_mode()) {
            break;
        }

        bool pos_ignore      = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_POS_IGNORE;
        bool vel_ignore      = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_VEL_IGNORE;
        bool acc_ignore      = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_ACC_IGNORE;
        bool yaw_ignore      = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_YAW_IGNORE;
        bool yaw_rate_ignore = packet.type_mask & MAVLINK_SET_POS_TYPE_MASK_YAW_RATE_IGNORE;

        // exit immediately if acceleration provided
        if (!acc_ignore) {
            break;
        }

        // extract location from message
        Location loc;
        if (!pos_ignore) {
            // sanity check location
            if (!check_latlng(packet.lat_int, packet.lon_int)) {
                break;
            }
            Location::AltFrame frame;
            if (!mavlink_coordinate_frame_to_location_alt_frame((MAV_FRAME)packet.coordinate_frame, frame)) {
                // unknown coordinate frame
                break;
            }
            loc = {packet.lat_int, packet.lon_int, int32_t(packet.alt*100), frame};
        }

        // prepare yaw
        float yaw_cd = 0.0f;
        bool yaw_relative = false;
        float yaw_rate_cds = 0.0f;
        if (!yaw_ignore) {
            yaw_cd = ToDeg(packet.yaw) * 100.0f;
            yaw_relative = packet.coordinate_frame == MAV_FRAME_BODY_NED || packet.coordinate_frame == MAV_FRAME_BODY_OFFSET_NED;
        }
        if (!yaw_rate_ignore) {
            yaw_rate_cds = ToDeg(packet.yaw_rate) * 100.0f;
        }

        // send targets to the appropriate guided mode controller
        if (!pos_ignore && !vel_ignore) {
            // convert Location to vector from ekf origin for posvel controller
            if (loc.get_alt_frame() == Location::AltFrame::ABOVE_TERRAIN) {
                // posvel controller does not support alt-above-terrain
                break;
            }
            Vector3f pos_neu_cm;
            if (!loc.get_vector_from_origin_NEU(pos_neu_cm)) {
                break;
            }
            copter.mode_guided.set_destination_posvel(pos_neu_cm, Vector3f(packet.vx * 100.0f, packet.vy * 100.0f, -packet.vz * 100.0f), !yaw_ignore, yaw_cd, !yaw_rate_ignore, yaw_rate_cds, yaw_relative);
        } else if (pos_ignore && !vel_ignore) {
            copter.mode_guided.set_velocity(Vector3f(packet.vx * 100.0f, packet.vy * 100.0f, -packet.vz * 100.0f), !yaw_ignore, yaw_cd, !yaw_rate_ignore, yaw_rate_cds, yaw_relative);
        } else if (!pos_ignore && vel_ignore) {
            copter.mode_guided.set_destination(loc, !yaw_ignore, yaw_cd, !yaw_rate_ignore, yaw_rate_cds, yaw_relative);
        }

        break;

大概流程就是判断接收到的数据是什么类型,然后进行相应的控制(控制速度,控制航点,还是速度和位置都控制)。需要修改的地方就显而易见了。

需要注意的点:

  1. 本次实验采用的是dronekit脚本的形式完成mavnlink消息的解析。在dronekit中 不要采用simple_go函数,该函数好像用的是manvlink_command, 而不是mavlink_message,导致捕获不到消息。
  2. dronekit脚本在处理 goto_position_target_global_int的时候,pymavlink解析出错,报错为python中的struct.pack需要整形的数据,但是发送的不是。最后采用强制数据类型转换完成。
  3. dronekit脚本附赠如下:
import time
import random
import dronekit
import math
from pymavlink import mavutil
import threading
vehicle = dronekit.connect('127.0.0.1:14550',wait_ready=True)
master =  mavutil.mavlink_connection('udp:127.0.0.1:{}'.format(14551))
master.wait_heartbeat()

def prearm_check():
    if vehicle.mode.name != "STABILIZE":
        vehicle.mode = "STABILIZE"
        print("waiting for changing the mode to stabilize...")
        vehicle.wait_for_mode("STABILIZE")
        print("changing mode to stabilize successfully...")
    while not vehicle.gps_0.fix_type:
        time.sleep(0.5)
    print("gps check passed...")
    while not vehicle.ekf_ok:
        time.sleep(0.5)
    print("ekf check passed...")

def goto_position_target_global_int(alocation):
    """
    Send SET_POSITION_TARGET_GLOBAL_INT command to request the vehicle fly to a specified location.
    """
    msg = vehicle.message_factory.set_position_target_global_int_encode(
        0,       # time_boot_ms (not used)
        0, 0,    # target system, target component
        mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT_INT, # frame
        0b0000111111111000, # type_mask (only speeds enabled)
        alocation.lat*1e7, # lat_int - X Position in WGS84 frame in 1e7 * meters
        alocation.lon*1e7, # lon_int - Y Position in WGS84 frame in 1e7 * meters
        alocation.alt, # alt - Altitude in meters in AMSL altitude, not WGS84 if absolute or relative, above terrain if GLOBAL_TERRAIN_ALT_INT
        0, # X velocity in NED frame in m/s
        0, # Y velocity in NED frame in m/s
        0, # Z velocity in NED frame in m/s
        0, 0, 0, # afx, afy, afz acceleration (not supported yet, ignored in GCS_Mavlink)
        0, 0)    # yaw, yaw_rate (not supported yet, ignored in GCS_Mavlink)
    # send command to vehicle
    vehicle.send_mavlink(msg)

def goto_position_target_local_ned(vehicle, north, east, down):
    """
    Send SET_POSITION_TARGET_LOCAL_NED command to request the vehicle fly to a specified
    location in the North, East, Down frame.
    """
    msg = vehicle.message_factory.set_position_target_local_ned_encode(
        0,  # time_boot_ms (not used)
        0, 0,  # target system, target component
        mavutil.mavlink.MAV_FRAME_LOCAL_NED,  # frame
        0b0000111111111000,  # type_mask (only positions enabled)
        north, east, down,
        0, 0, 0,  # x, y, z velocity in m/s  (not used)
        0, 0, 0,  # x, y, z acceleration (not supported yet, ignored in GCS_Mavlink)
        0, 0)  # yaw, yaw_rate (not supported yet, ignored in GCS_Mavlink)
    # send command to vehicle
    vehicle.send_mavlink(msg)

def is_fly_land_reset():
    if vehicle.armed == True:
        while vehicle.mode != "RTL":
            vehicle.wait_for_mode("RTL")
        print("Set mode to RTL...")
    while vehicle.armed != False:
        time.sleep(1)
    vehicle.wait_for_mode("STABILIZE")
    print("Vehicle reset OK...")


def get_location_metres(original_location, dNorth, dEast):
    earth_radius = 6378137.0  # Radius of "spherical" earth
    # Coordinate offsets in radians
    dLat = dNorth / earth_radius
    dLon = dEast / (earth_radius * math.cos(math.pi * original_location.lat / 180))

    # New position in decimal degrees
    newlat = original_location.lat + (dLat * 180 / math.pi)
    newlon = original_location.lon + (dLon * 180 / math.pi)
    if type(original_location) is dronekit.LocationGlobal:
        targetlocation = dronekit.LocationGlobal(newlat, newlon, original_location.alt)
    elif type(original_location) is dronekit.LocationGlobalRelative:
        targetlocation = dronekit.LocationGlobalRelative(newlat, newlon, original_location.alt)
    else:
        raise Exception("Invalid Location object passed")

    return targetlocation

def goto(dNorth, dEast, gotoFunction=vehicle.simple_goto):

    currentLocation = vehicle.location.global_relative_frame
    targetLocation = get_location_metres(currentLocation, dNorth, dEast)
    # targetDistance = get_distance_metres(currentLocation, targetLocation)
    gotoFunction(targetLocation)

delay_time = 60
def time_schedule():
    while True:
        if 'i' == input():
            delay_time  = delay_time + 0.1
        elif 'k' == input():
            delay_time = delay_time - 0.1
        else:
            print("Wrong input... Nothing changed...")
        if delay_time < 0:
            delay_time = 0.1
        elif delay_time > 60:
            delay_time = 60
        print(delay_time)
        time.sleep(0.1)

if __name__ == '__main__':
    is_fly_land_reset()
    prearm_check()
    vehicle.mode = "GUIDED"
    vehicle.wait_for_mode("GUIDED")

    while vehicle.armed == False:
        vehicle.arm(True)
    print("vehicle armed...")

    # vehicle.simple_takeoff(alt=12)
    vehicle.wait_simple_takeoff(alt= 12,epsilon=2)
    while vehicle.location.local_frame.down > -8:
        print(vehicle.location.local_frame.down)
        time.sleep(0.5)
    print("Begin to navigation...")
    while True:
        lat = 39.1036 + random.random() * 32 * 1e-4
        lon = 117.16 + random.random() * 66 * 1e-4
        alt = 30
        location_send = dronekit.LocationGlobalRelative(lat, lon, alt)
        # vehicle.simple_goto(location=location_send)
        # goto(-100, -130, goto_position_target_global_int)
        goto_position_target_global_int(location_send)
        # goto_position_target_local_ned(vehicle,5,2,-20)
        # master.mav.set_position_target_global_int_send(
        #     0,
        #     master.target_system,
        #     master.target_component,
        #     6,
        #     lat*1e7,
        #     lon*1e7,
        #     alt,
        #     0,0,0,
        #     0,0,0,
        #     0,0,0, force_mavlink1=True)
        print("target lat=%f,\t lon=%f,\t current lat=%f,\t lon=%f"
              %(lat, lon, vehicle.location.global_frame.lat, vehicle.location.global_frame.lon))
        random_time = random.random()*9.9+0.1
        time.sleep(random_time)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ardupilot之mavlink消息--飞控接收--单向 的相关文章

  • MAVLINK消息在Ardupilot中的接收和发送过程

    MAVLINK消息在Ardupilot中的接收和发送过程 SCHED TASKupdate receive update send 由于现在网上很多的都是APM旧版本的解释 xff0c 因此把自己的一些学习所得记录下来 截至写博客日期 xf
  • Ardupilot速率控制器rate_controller_run解析

    Ardupilot速率控制器rate controller run解析 PID速率控制器源码解析rate controller run PID运算积分限制update i get ff set xxx 内容补充 xff1a 函数中陀螺仪数据
  • px4调试bug--添加mavlink_log_info信息

    写在前面的话 有一阵子没有看px4的代码了 由于项目和论文的需要 又要接触这个 其中又遇到一些新的问题 找到了一些新的解决方法 故在此记录一下 总是在几种飞控代码之间跳来跳去 没有认真研究一个 有点遗憾 PX4的代码调试还没有找到什么好的方
  • C语言实现mavlink库与px4通信仿真

    参照网址 http mavlink io en mavgen c 首先从github上下载对应的C uart interface example 对应的github仓库网址为https github com mavlink c uart i
  • ardupilot之mavlink消息--从飞控发出--单向

    飞控采用mavlink消息进行数据的传输 普遍说法是 xff0c 现有的mavlink消息几乎已经涵盖了所有你的能想象到的内容 xff0c 完全可以覆盖多处需求 无奈科研总是要定义一些新鲜玩意 xff0c 所以总是有无法完全满足需求 xff
  • ardupilot & PX4 RTK配置指南

    ardupilot amp PX4 RTK配置指南 随着无人机对于高精度位置需求越来越强烈 xff0c 同时也伴随着北斗三代导航系统正式服务全球 xff0c 国产的实时载波相位差分 xff08 RTK xff09 导航产品也正在以更优惠 更
  • ADRC Ardupilot代码分析

    记录一下自己对于Ardupilot ADRC控制代码的一些理解 GitHub链接 ADRC Active Disturbance Rejection Control by MichelleRos Pull Request 20243 Ard
  • MAVLink代码

    MAVLink是一种通信协议 xff0c 用于在微型无人机 MAV 和其他系统之间进行通信 它是一种二进制消息格式 xff0c 用于在无人机和地面站之间传送数据 MAVLink代码通常用于编写无人机的固件或为无人机开发地面站软件 它也可以用
  • ardupilot & px4 书写自己的app & drivers (二)

    新建任务列表任务 打印时间 任务列表 const AP Scheduler span class hljs tag Task span Copter span class hljs tag scheduler tasks span span
  • 无人机飞控平台ArduPilot源码入门教程 - 首页

    原文链接 简介 ArduPilot代码库有点大 核心的ardupilot git树大概有70万行代码 对新人来说这有点吓人 这个文档打算给出一点建议 关于如何快速上手相关代码 我们假设你熟悉C 43 43 的关键概念 另外好多例子都是假设你
  • QGC二次开发---自定义MAVLink消息

    MAVLink库下载 下载网站https github com mavlink mavlink 可以通过git工具 xff0c 在存放文件夹下打开git工具 xff0c 输入命令 xff1a git clone https github c
  • APM 学习 6 --- ArduPilot 线程

    ArduPilot 学习之路 6 xff0c 线程 英文原文地址 xff1a https ardupilot org dev docs learning ardupilot threading html 理解 ArduPilot 线程 线程
  • PX4模块设计之五:自定义MAVLink消息

    PX4模块设计之五 xff1a 自定义MAVLink消息 1 MAVLink Dialects1 1 PX4 Dialects1 2 Paprazzi Dialects1 3 MAVLink XML File Format 2 添加自定义M
  • SITL Simulator —— ArduPilot —— Windows

    版权声明 xff1a 本文为博主原创博文 xff0c 未经允许不得转载 xff0c 若要转载 xff0c 请说明出处并给出博文链接 参考网页 xff1a http ardupilot org dev docs sitl native on
  • uORB和MAVLink通讯例程

    uORB uORB 是一种异步 publish subscribe 的消息传递 API xff0c 用于进程或者线程间通信 IPC 添加新的Topic xff08 主题 xff09 在msg 目录下创建一个新的 msg文件 xff0c 并将
  • ArduPilot/APM源码学习笔记(一)

    最近开始学习ArduPilot APM飞控的源码 xff0c 源码托管在github上 源码链接 xff1a https github com diydrones ardupilot 飞控主页 xff1a http ardupilot co
  • MISSION_MAVLINK

    上传航点的mavlink包 MISSION ITEM 39 Message encoding a mission item This message is emitted to announce the presence of a miss
  • 树莓派3B使用mavlink串口连接PIXHAWK_V5

    参考网址 xff1a http ardupilot org dev docs raspberry pi via mavlink html https dev px4 io en robotics dronekit html https do
  • 在ubuntu20.4下安装ardupilot 4.3.6

    这次重新安装真的是遇到了好多坑啊 xff01 从github上靠过来按照之前的那篇文章流程做完之后 xff0c 还会有一些别的问题 首先是module里面的包都没有拷过来 xff0c 所以需要用git add将文件都添加过来 之后进行编译时
  • ArduPilot-sitl中的一些操作记录

    ArduPilot 这么优秀的代码 提供了一套很方便的SITL仿真开发模式 在git clone代码的时候 已经将相关的东西下载下来了 问题是如何进行使用 首先要安装mavproxy 这个软件 pymavlink mavlink封装的pyt

随机推荐

  • ArduPilot+mavros+gazebo+QGC联合仿真初体验

    首先给出最终效果图 xff1a 实现内容与PX4官网代码功能类似 xff0c 四旋翼飞机自动起飞至2 5m高度 xff0c 悬停一定时间 xff0c 然后自主降落 记录如下几个需要注意的地方 xff1a 一共使用到三个文件夹 xff0c 其
  • ArduPilot姿态控制方法解析---(倾转分离)

    先给出一些预备知识 xff1a 欧拉角 xff1a 即所谓的Roll Pitch Yaw 而事实上 xff0c 根据其定义 xff0c 欧拉角的具有不同的表示 存在先绕哪个轴 xff0c 后绕哪个轴转的差别 xff08 将旋转分解成绕三个轴
  • C中main函数解析

    参考链接 main函数不同写法 以下为main函数的6种不同写法 xff1a span class token function main span span class token punctuation span span class
  • ArduPilot姿态环控制-----传感器初始化

    参考链接 xff1a https blog csdn net lixiaoweimashixiao article details 80540295 首先我们假定从void AP Vehicle setup 开始 xff0c 这里是飞控所有
  • ArduPilot飞行前检查——PreArm解析

    ArduPilot飞行前检查 主要包括两个部分 1 初始化中遥控器输入检查 xff1b 2 1Hz解锁前检查 附 xff1a 显示地面站信息 参考文章 xff1a Ardupilot Pre Arm安全检查程序分析 1 初始化中遥控器输入检
  • ROS_PX4_gazebo学习记录

    在官方程序上 xff08 PX4 wiki上为offboard起飞到2m高度 xff09 进行更改 xff0c 实现首先起飞到固定点 xff08 x 61 1 y 61 2 z 61 5 然后按照给定角度飞行 补充 xff1a 最终实现效果
  • Rospy初次使用记录-定点飞行

    由于接触到pytorch xff0c 所以用python完成与ROS的通信 xff0c 下面例子为从程序中摘出来的一部分 xff0c 用到了ROS消息的订阅与发布 xff0c 服务的通信 xff0c 可以作为参考使用 xff1a span
  • 四旋翼飞行器数学模型

    最近接触到四旋翼无人机的位置控制方法 xff0c 就又了解了一下四旋翼飞机的数学模型 xff0c 现总结如下 xff1a 位置环 xff08 均定义在惯性坐标系下 xff09 P
  • 基于ROS与optitrack的四旋翼飞机开发流程

    本文将一些注意点记录下来 xff0c 适合于开发调试 xff1a 目前只是分段调试通了 xff0c 带后续联合开发的时候在来补充还有没有什么注意点 xff08 过程也算麻烦 xff0c 也算不麻烦 xff09 xff1b xff32 xff
  • ROS_调试(三) 打印输出

    ROS INFO 采用类似C语言的形式 ROS DEBUG ROS DEBUG STREAM 采用类似C 43 43 语言的形式打印 ROS DEBUG STREAM NAMED ROS DEBUG STREAM THROTTLE NAME
  • px4调试bug--添加mavlink_log_info信息

    写在前面的话 有一阵子没有看px4的代码了 由于项目和论文的需要 又要接触这个 其中又遇到一些新的问题 找到了一些新的解决方法 故在此记录一下 总是在几种飞控代码之间跳来跳去 没有认真研究一个 有点遗憾 PX4的代码调试还没有找到什么好的方
  • APM,PX4之开源协议

    APM代码设计的是GPLv3协议 xff0c PX4代码采用的是BSD协议 从上图可以看出 xff0c ardupilot的代码是允许别人修改 xff0c 但是修改之后必须开源且采用相同的许可证书 而PX4代码则是允许别人修改 xff0c
  • C语言实现mavlink库与px4通信仿真

    参照网址 http mavlink io en mavgen c 首先从github上下载对应的C uart interface example 对应的github仓库网址为https github com mavlink c uart i
  • RK3308--8声道改成双声道+录音增益

    改为双声道 修改dts文件 相关路径 xff1a Y hxy RK3308 sdk 1 5 kernel arch arm64 boot dts rockchipY hxy RK3308 sdk 1 5 kernel Documentati
  • Flightmare install 安装指南

    flightmare 是ETH推出的一个用于gazebo仿真 xff0c 强化学习训练的平台 xff0c 并在github上公开了其源代码 本文主要记录在配置环境过程中出现的问题 github网址链接 https github com uz
  • matlab发送mavlink消息

    主要介绍了通过matlab脚本实现UDP发送mavlink消息 xff0c 为后面matlab计算 xff0c 与Optitrack联合调试 xff0c 控制无人机做准备 示例演示效果链接为 matlab通过UDP协议发送mavlink消息
  • apm-ros-optitrack初步尝试

    本文记录采用ArduPilot固件 xff0c 室内optitrack环境下飞行实现中遇到的一些问题 在apm mavros仿真中 xff0c 总是出现mavros state 显示 not connected 在实际的操作中 xff0c
  • APM代码调试知识点汇总

    由于项目的需要 xff0c 对ardupilot的源码进行二次开发 本文记录在二次开发中遇到的问题以及注意事项 xff1a CUAV V5 实测 apm 串口 xff0c 对于姿态数据的发送和接收在200Hz的时候 xff0c 是没有问题的
  • ardupilot之mavlink消息--从飞控发出--单向

    飞控采用mavlink消息进行数据的传输 普遍说法是 xff0c 现有的mavlink消息几乎已经涵盖了所有你的能想象到的内容 xff0c 完全可以覆盖多处需求 无奈科研总是要定义一些新鲜玩意 xff0c 所以总是有无法完全满足需求 xff
  • ardupilot之mavlink消息--飞控接收--单向

    由于项目需要 xff0c 完成一个测试demo 本次从dronekit中发送mavlink消息给飞控 xff0c 飞控接收发来的wp信息 xff0c 然后进行修改供程序使用 首先祭出测试视频 dronekit arudpilot test