向Ardupilot中添加新的飞行模式(以ArduSub为例)

本次以ArduSub为例介绍如何向APM固件中添加新的飞行模式以方便后续开发,其他车辆类型操作相似,本篇也可用作参考。

参考资料:

Adding a New Flight Mode

Creating newflight mode in Ardusub


首先在ardupilot/ArduSub/文件路径下,新建一个用于添加新的飞行模式的文件control_newMode.cpp。然后,向内部添加代码如下:

#include "Sub.h"

bool Sub::newmode_init()
{
	return true;
}

void Sub::newmode_init()
{

}

保存退出之后,进入ardupilot/libraries/AP_JSButton/AP_JSBottton.h,找到k_mode_xxx形式的模式定义如下:

        k_mode_manual           = 5,            ///< enter enter manual mode
        k_mode_stabilize        = 6,            ///< enter stabilize mode
        k_mode_depth_hold       = 7,            ///< enter depth hold mode
        k_mode_poshold          = 8,            ///< enter poshold mode
        k_mode_auto             = 9,            ///< enter auto mode
        k_mode_circle           = 10,           ///< enter circle mode
        k_mode_guided           = 11,           ///< enter guided mode
        k_mode_acro             = 12,           ///< enter acro mode
        k_mode_newmode			= 13,			///...

在最后添加你的新模式,我这里选择的数字为13(只要不超过后面参数的值21即可)。然后添加你对应的备注。

回到ardupilot/ArduSub/defines.h文件中,找到control_mode_t枚举类型定义,在最后面添加你的新模式,并添加备注描述

// Auto Pilot Modes enumeration
enum control_mode_t {
    STABILIZE =     0,  // manual angle with manual depth/throttle
    ACRO =          1,  // manual body-frame angular rate with manual depth/throttle
    ALT_HOLD =      2,  // manual angle with automatic depth/throttle
    AUTO =          3,  // fully automatic waypoint control using mission commands
    GUIDED =        4,  // fully automatic fly to coordinate or fly at velocity/direction using GCS immediate commands
    CIRCLE =        7,  // automatic circular flight with automatic throttle
    SURFACE =       9,  // automatically return to surface, pilot maintains horizontal control
    POSHOLD =      16,  // automatic position hold with manual override, with automatic throttle
    MANUAL =       19,  // Pass-through input with no stabilization
    MOTOR_DETECT = 20,  // Automatically detect motors orientation
	NEWMODE =	   21	// ...
};

再进入ardupilot/ArduSub/Sub.h,向内部添加代码段如下(理论上是可以在任意位置,但是最好添加在别的飞行模式的旁边)

bool newmode_init(void);
void newmode_run();

然后在ardupilot/ArduSub/flight_mode.cpp中,对set_mode()和update_flight_mode()方法进行修改,具体就是在switch函数下面添加对应的case情况函数。

// 在set_mode()方法中
    case NEWMODE:
    	success = newmode_init();
    	break;
...
///在update_flight_mode()方法中
    case NEWMODE:
    	success = newmode_run();
    	break;

最后,在ardupilot/ArduSub/joystick.cpp中,找到

void Sub::handle_jsbutton_press(uint8_t button, bool shift, bool held)

在其中的switch-case语句中的default前添加

    case JSButton::button_function_t::k_mode_newmode:
    	set_mode(NEWMODE, MODE_REASON_TX_COMMAND);

然后保存退出即可。


编译

cd ardupilot/
rm -rf build/
./waf configure --board Pixhawk1
./waf sub

编译成功!

在这里插入图片描述
后续工作是如何使QGC能够识别出新的飞行模式,目前尚在研究中。也可考虑使用mavros或者pymavlink进行后续的开发作业。目前先研究到这~

### ArduPilot Follow模式功能和使用 在ArduPilot系统中,Follow模式允许无人机跟随指定的目标移动。此目标通常是一个GPS设备或者另一个带有GPS信号的车辆或人员[^3]。 #### 启用Follow模式的要求 为了启用Follow模式,需要满足几个条件: - 需要配置好地面站软件(如Mission Planner),并设置好相应的参数。 - 被跟踪对象需配备能够发送位置数据给无人机的有效通信链路。 - 无人机本身应处于良好的工作状态,并已校准完毕以便精确导航。 #### 参数调整与设定 一些重要的参数用于定义如何以及何时启动跟随后的行为: - `FOLL_ENABLE`:开启或关闭Follow Me 功能。 - `FOLL_WPSPEED` : 设置跟随过程中使用的速度,默认值为5米每秒。 - `FOLL_DELAY` : 设定从接收到新坐标到实际改变航线之间的时间延迟。 - `FOLL_RADIUS`: 当被追踪者位于该半径范围内时,无人机会停止前进等待其离开后再继续接近。 这些参数可以在地面站上进行修改以适应不同的应用场景需求。 #### 进入Follow模式的方法 一旦所有必要的硬件准备就绪并且适当设置了上述提到的关键参数之后,在飞行期间可以通过多种方式切换至Follow模式: - **通过RC开关**:如果已经映射了一个特定通道作为模式选择器,则可以直接利用遥控发射机上的拨动开关来激活它; - **借助GCS命令**:也可以经由连接着互联网或其他无线网络接口的电脑端应用程序下达指令让飞机进入这种特殊操作状态; - **预编程的任务列表里加入相应条目**:还可以事先编写好一系列动作序列其中包括转换成此种类型的指示从而实现自动化流程管理的目的。 ```python # Python API example to set follow mode via MAVLink (pseudo code) from pymavlink import mavutil the_connection = mavutil.mavlink_connection('udp:localhost:14550') the_connection.wait_heartbeat() msg = the_connection.message_info('COMMAND_LONG', force_mavlink1=False) msg.command = mavutil.mavlink.MAV_CMD_DO_SET_MODE param1 = 1 # Custom mode, depends on vehicle type param2 = 6 # Follow me submode ID according to documentation the_connection.mav.command_long_send(the_connection.target_system, the_connection.target_component, msg.command,param1,param2,0,0,0,0,0,0) ``` #### 注意事项 当采用Follow模式时需要注意安全性和合法性方面的问题。确保始终遵循当地法律法规对于无人驾驶航空系统的相关规定,并采取一切必要措施保障周围人群的安全不受威胁。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值