PX4 Offboard Control Using MAVROS on ROS

AI助手已提取文章相关产品:

原文:https://404warehouse.net/2015/12/20/autopilot-offboard-control-using-mavros-package-on-ros/

Overview

Recently opensource autopilots have become reliable by various failsafe functions integrated by the opensource community. Developing different softwares for each application can be time consuming and hard to verify the reliablility of the software. An alternative to modifying the autopilot software, is to use an onboard computer to command the autopilot using high level commands. This way, the developer can use a reliable autopilot software which is still reliable and capable of staying in the air and use a software developed for the developer’s own purpose.
This article is to introduce how easy and practical it is to connect a PC, using ROS to control the autopilot running onbaord an onboard PC. A nice tutorial is up on the PX4 website but is a little unfriendly and I wanted to share some of the troubleshooting experiences while I was implementing the system.

로열모_5분스피치_임재영

what is ROS?

ROS is a framework for incorporating various modules of robotics by configuring modules as a TCP network. The ROS community is currently growing in a very fast pace both in the community and industry. The ROS wiki has great tutorials in understanding the framework. Please get used to using ROS following tutorials 1.~9. in the tutorials page.

Introduction to MAVROS

MAVROS is a ROS package which enables ROS to interact with autopilot software using the MAVLink protocol. MAVlink consists of 17 bytes and includes the message ID, target ID and data. The message ID shows what the data is. Message IDs can be seen in the messageID command set.
b15e6d130c9d606305c31ffedb3c1118.media.400x98

This enables MAVLink to be able to get information from multiple UAVs if messages are transferred in the same channel. Messages can either be transmitted through wireless signals.

mavros_raw


Installing MAVROS

MAVROS can be installed using the source in the mavros repository. The default dialect of MAVROS is apm. As we are installing px4 on the pixhawk flight controller, MAVROS should be built from source, by configuring mavros by the command below.

catkin config --cmake-args -DMAVLINK_DIALECT=common

Configuring PX4 for ROS

For the companion computer to communicate with the flight controller, a USB2TTL converter is needed to convert the voltage of the communication levels. A brief overview of configuring the companion computer with the pixhawk is shown in this link.

IMG_20151221_122909

It is recommended that the offboard control is done through the TELEM2 port. Telem2 port can be activated usign the SYS_COMPANION parameter. This can be done by modifying the parameter on qgroundcontrol.

SYS_COMPANION 921600

Configuring MAVROS for PX4

To launch MAVROS, the easiest way is to use the launch file

roslaunch mavros px4.launch

Launching MAVROS, it may not work on the first time. Do not panic!

Some parameters should be modified to be able to talk with the flight controller. The device name and baudrate should be configured to be able to talk to the autopilot.

1
2
3
4
5
6
7
8
9
10
11
12
< node name = "mavros" pkg = "mavros" type = "mavros_node" output = "screen" >
     < param name = "fcu_url" value = "$(arg fcu_url)" />
     < param name = "gcs_url" value = "$(arg gcs_url)" />
     < param name = "target_system_id" value = "$(arg tgt_system)" />
     < param name = "target_component_id" value = "$(arg tgt_component)" />
     <!-- enable heartbeat send and reduce timeout -->
     < param name = "conn_heartbeat" value = "5.0" />
     < param name = "conn_timeout" value = "5.0" />
     <!-- automatically start mavlink on USB -->
     < param name = "startup_px4_usb_quirk" value = "true" />
 
</ node >

Setting Setpoints

Setpoints should be sent to the flight controller with a 0.5s time out. This is a safety factor so that the control should converge to a specific objective. Otherwise the flight controller will not go into offboard mode. Setpoints that can be used are as below.

  • Position Setpoints
  • Velocity Setpoints
  • Attitude Setpoints
  • Acceleration Setpoints
  • Actuator Control

An example node written in cpp is shown below. The node publishes setpoint_poistion_local message.

Integrating Companion Computer to quadrotor

The quadrotor is based on a F330 frame and the current integration follows as below. 330 frame feeled a little small for an onboard computer. For future integration, 450 frame should be more appropriate.

IMG_20151221_202606

Position Control Example

For position control, external position estimation using the Vicon motion capture system was used for more precise position measurement. The block diagram visualized using rtq_graph is shown as below.rosgraph2





您可能感兴趣的与本文相关内容

#include <ros/ros.h> #include <std_msgs/Bool.h> #include <geometry_msgs/PoseStamped.h> #include <mavros_msgs/CommandBool.h> #include <mavros_msgs/SetMode.h> #include <mavros_msgs/State.h> #include <mavros_msgs/PositionTarget.h> #include <cmath> #include <tf/transform_listener.h> #include <nav_msgs/Odometry.h> #include <string> #include <geometry_msgs/Twist.h> #include <mavros_msgs/CommandTOL.h> using namespace std; mavros_msgs::PositionTarget setpoint_raw; //定义变量,用于接收无人机的里程计信息 tf::Quaternion quat; double roll, pitch, yaw; float init_position_x_take_off =0; float init_position_y_take_off =0; float init_position_z_take_off =0; bool flag_init_position = false; nav_msgs::Odometry local_pos; void local_pos_cb(const nav_msgs::Odometry::ConstPtr& msg) { local_pos = *msg; if (flag_init_position==false && (local_pos.pose.pose.position.z!=0)) { init_position_x_take_off = local_pos.pose.pose.position.x; init_position_y_take_off = local_pos.pose.pose.position.y; init_position_z_take_off = local_pos.pose.pose.position.z; flag_init_position = true; } tf::quaternionMsgToTF(local_pos.pose.pose.orientation, quat); tf::Matrix3x3(quat).getRPY(roll, pitch, yaw); } mavros_msgs::State current_state; void state_cb(const mavros_msgs::State::ConstPtr& msg) { current_state = *msg; } int main(int argc, char **argv) { //初始化节点,名称为v2_ar_track_landing ros::init(argc, argv, "offboard_multi_position"); //创建句柄 ros::NodeHandle nh; //订阅无人机状态话题 ros::Subscriber state_sub = nh.subscribe<mavros_msgs::State>("mavros/state", 100, state_cb); //订阅无人机实时位置信息 ros::Subscriber local_pos_sub = nh.subscribe<nav_msgs::Odometry>("/mavros/local_position/odom", 100, local_pos_cb); //发布无人机多维控制话题 ros::Publisher mavros_setpoint_pos_pub = nh.advertise<mavros_msgs::PositionTarget>("/mavros/setpoint_raw/local", 100); //请求无人机解锁服务 ros::ServiceClient arming_cl = nh.serviceClient<mavros_msgs::CommandBool>("/mavros/cmd/arming"); //请求无人机设置飞行模式,本代码请求进入offboard ros::ServiceClient cl = nh.serviceClient<mavros_msgs::SetMode>("/mavros/set_mode"); ros::ServiceClient takeoff_cl = nh.serviceClient<mavros_msgs::CommandTOL>("/mavros/cmd/takeoff"); //循环频率 ros::Rate rate(20.0); ROS_INFO("Initializing..."); while (ros::ok() && !current_state.connected) { ros::spinOnce(); rate.sleep(); } ROS_INFO("Connected."); // 设置无人机模式为GUIDED mavros_msgs::SetMode srv_setMode; srv_setMode.request.base_mode = 0; srv_setMode.request.custom_mode = "GUIDED"; if (cl.call(srv_setMode)) { ROS_INFO("setmode send ok %d value:", srv_setMode.response.mode_sent); } else { ROS_ERROR("Failed SetMode"); return -1; } // 解锁无人机 mavros_msgs::CommandBool srv; srv.request.value = true; if (arming_cl.call(srv)) { ROS_INFO("ARM send ok %d", srv.response.success); } else { ROS_ERROR("Failed arming or disarming"); } // 发布起飞的指令,向上飞1米 mavros_msgs::CommandTOL srv_takeoff; srv_takeoff.request.altitude = 0; srv_takeoff.request.latitude = 0; srv_takeoff.request.longitude = 0; srv_takeoff.request.min_pitch = 0; srv_takeoff.request.yaw = 0; if (takeoff_cl.call(srv_takeoff)) { ROS_INFO("srv_takeoff send ok %d", srv_takeoff.response.success); } else { ROS_ERROR("Failed Takeoff"); } sleep(10); bool flag_arrive = false; while(ros::ok()) { setpoint_raw.type_mask = /*1 + 2 + 4 + 8 + 16 + 32*/ + 64 + 128 + 256 + 512 /*+ 1024 + 2048*/; setpoint_raw.coordinate_frame = 1; //setpoint_raw.velocity.x = 0.2; //setpoint_raw.velocity.y = 0.0; setpoint_raw.yaw = 3.14; //setpoint_raw.position.x =init_position_x_take_off + 2; //setpoint_raw.position.y =init_position_y_take_off + 0; setpoint_raw.position.z =init_position_z_take_off + 1; mavros_setpoint_pos_pub.publish(setpoint_raw); ros::spinOnce(); rate.sleep(); } return 0; }
最新发布
07-29
### 配置和运行 ROS 2 PX4 MAVROS Gazebo 无人机仿真 #### 安装依赖项 为了确保所有组件能够正常工作,建议按照官方文档安装必要的依赖包。对于 Ubuntu 系统而言,可以通过以下命令来更新软件源并安装所需工具链: ```bash sudo apt update && sudo apt install git wget qtcreator clang-format python3-pip -y pip3 install --user pyulog pymavlink dronecan empy toml geographiclib pandas jinja2 numpy ``` #### 获取 PX4MAVROS 源码 下载最新的 PX4 自动驾驶仪固件以及 MAVROS 软件包到本地计算机中。 ```bash cd ~/ git clone https://github.com/PX4/PX4-Autopilot.git -b v1.12.0 cd ~/PX4-Autopilot DONT_RUN=1 make px4_sitl_default gazebo ``` MAVROS 的获取方式取决于所使用的 ROS 版本,在此假设为 ROS Foxy: ```bash source /opt/ros/foxy/setup.bash mkdir -p ~/ros2_ws/src cd ~/ros2_ws/src vcs import <(wget -qO- https://raw.githubusercontent.com/mavlink/mavros/master/mavros.yaml) cd .. colcon build --symlink-install source install/local_setup.bash ``` #### 启动 SITL (Software In The Loop) 模拟器 启动包含 MAVROS 插件在内的 Gazebo 场景文件,并加载默认的 iris 型号四旋翼机架结构模型。 ```bash export ROS_DOMAIN_ID=30 ign gazebo -r ~/PX4-Autopilot/Tools/simulation/gazebo-classic/models/empty_world/empty.world sleep 5s pxh> commander takeoff ``` 此时应当可以在终端窗口观察到来自于 MAVLink 协议的数据流输出信息[^1]。 #### 编写简单的 Python 控制脚本 编写一段用于发送指令给 MAVROS 设备节点的小型应用程序实例,以便测试基本功能是否正常运作。 ```python import rclpy from geometry_msgs.msg import PoseStamped, TwistStamped from mavros_msgs.srv import CommandBool, SetMode def main(args=None): rclpy.init() node = rclpy.create_node('simple_offboard') arm_client = node.create_client(CommandBool, '/mavros/cmd/arming') set_mode_client = node.create_client(SetMode, '/mavros/set_mode') while not arm_client.wait_for_service(timeout_sec=1.0): pass request = CommandBool.Request(value=True) future = arm_client.call_async(request) rclpy.spin_until_future_complete(node, future) mode_request = SetMode.Request(base_mode=0, custom_mode="OFFBOARD") future = set_mode_client.call_async(mode_request) rclpy.spin_until_future_complete(node, future) if __name__ == '__main__': main() ``` 上述代码片段展示了如何通过调用服务接口向飞行控制器请求切换至离板模式(Offboard Mode),从而接管姿态控制权柄;同时发出武装电机信号使螺旋桨开始旋转准备升空动作[^2]。 #### 故障排除技巧 如果遇到无法成功解锁或起飞的情况,则需重点检查以下几个方面: - 确认已正确设置了环境变量 `ROS_DOMAIN_ID` ,防止多台设备间发生冲突; - 使用 `rostopic list` 或者 `ros2 topic list` 查看当前活跃话题列表,验证各模块之间的连接状况良好无误; - 利用 `mavros_commander.py` 工具辅助诊断潜在问题所在位置,例如查看日志记录、重置参数设定等操作; - 尝试重启整个系统或者单独的服务进程,有时候可以有效解决问题。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值