本博客将介绍如何在ROS中,结合MAVROS包,创建一个无人机控制节点,使无人机能够沿着预设轨迹(如圆形)飞行。本教程假设您已经安装了ROS和MAVROS包。
1.创建ROS包
首先,我们需要创建一个新的ROS包。在您的工作空间中的src
目录下执行以下命令:
catkin_create_pkg my_offboard_node roscpp mavros_msgs geometry_msgs
2. 编写代码
在 my_offboard_node
包中创建两个文件:offb_node.cpp
和 CircularTrajectory.h
。
2.1. offb_node.cpp
将以下代码保存为 my_offboard_node/src/offb_node.cpp
:
#include <ros/ros.h>
#include <geometry_msgs/PoseStamped.h>
#include <mavros_msgs/CommandBool.h>
#include <mavros_msgs/SetMode.h>
#include <mavros_msgs/State.h>
#include "my_offboard_node/CircularTrajectory.h"
// 全局状态变量
mavros_msgs::State current_state;
void state_cb(const mavros_msgs::State::ConstPtr& msg){
current_state = *msg;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "offb_node");
ros::NodeHandle nh;
ros::Subscriber state_sub = nh.subscribe<mavros_msgs::State>("mavros/state", 10, state_cb);
ros::Publisher local_pos_pub = nh.advertise<geometry_msgs::PoseStamped>("mavros/setpoint_position/local", 10);
ros::ServiceClient arming_client = nh.serviceClient<mavros_msgs::CommandBool>("mavros/cmd/arming");
ros::ServiceClient set_mode_client = nh.serviceClient<mavros_msgs::SetMode>("mavros/set_mode");
// 设置发布速率大于2Hz
ros::Rate rate(20.0);
// 等待与飞控的连接
while(ros::ok() && !current_state.connected){
ros::spinOnce();
rate.sleep();
}
// 创建轨迹对象
CircularTrajectory trajectory(5.0, 0.0, 0.0, 2.5, 0.1);
// 发送几个设置点以启动
geometry_msgs::PoseStamped pose;
for(int i = 100; ros::ok() && i > 0; --i){
pose = trajectory.calculate_pose(0); // 使用初始位置
local_pos_pub.publish(pose);
ros::spinOnce();
rate.sleep();
}
mavros_msgs::SetMode offb_set_mode;
offb_set_mode.request.custom_mode = "OFFBOARD";
mavros_msgs::CommandBool arm_cmd;
arm_cmd.request.value = true;
ros::Time last_request = ros::Time::now();
while(ros::ok()){
if( current_state.mode != "OFFBOARD" &&
(ros::Time::now() - last_request > ros::Duration(5.0))){
if( set_mode_client.call(offb_set_mode) &&
offb_set_mode.response.mode_sent){
ROS_INFO("Offboard enabled");
}
last_request = ros::Time::now();
} else {
if( !current_state.armed &&
(ros::Time::now() - last_request > ros::Duration(5.0))){
if( arming_client.call(arm_cmd) &&
arm_cmd.response.success){
ROS_INFO("Vehicle armed");
}
last_request = ros::Time::now();
}
}
double elapsed = (ros::Time::now() - last_request).toSec();
pose = trajectory.calculate_pose(elapsed);
local_pos_pub.publish(pose);
ros::spinOnce();
rate.sleep();
}
return 0;
}
2.2. CircularTrajectory.h
创建一个头文件来定义轨迹类。将以下内容保存为 my_offboard_node/include/my_offboard_node/CircularTrajectory.h
:
#ifndef CIRCULAR_TRAJECTORY_H
#define CIRCULAR_TRAJECTORY_H
#include <geometry_msgs/PoseStamped.h>
#include <math.h>
// 轨迹基类
class Trajectory {
public:
virtual geometry_msgs::PoseStamped calculate_pose(double time) = 0;
};
// 圆形轨迹
class CircularTrajectory : public Trajectory {
double radius, center_x, center_y, altitude, angular_velocity;
public:
CircularTrajectory(double r, double x, double y, double z, double w)
: radius(r), center_x(x), center_y(y), altitude(z), angular_velocity(w) {}
geometry_msgs::PoseStamped calculate_pose(double time) override {
geometry_msgs::PoseStamped pose;
double theta = angular_velocity * time;
pose.pose.position.x = center_x + radius * cos(theta);
pose.pose.position.y = center_y + radius * sin(theta);
pose.pose.position.z = altitude;
return pose;
}
};
#endif // CIRCULAR_TRAJECTORY_H
3. 编写CMakeLists.txt
修改 CMakeLists.txt
来编译节点。将以下内容添加到 my_offboard_node/CMakeLists.txt
文件中:
cmake_minimum_required(VERSION 2.8.3)
project(my_offboard_node)
find_package(catkin REQUIRED COMPONENTS
roscpp
mav
os_msgs
geometry_msgs
)
catkin_package()
include_directories(
${catkin_INCLUDE_DIRS}
include
)
add_executable(offb_node src/offb_node.cpp)
target_link_libraries(offb_node ${catkin_LIBRARIES})
4. 创建Launch文件
为了方便运行节点,创建一个launch文件。在 my_offboard_node/launch
目录下创建一个名为 offb_node.launch
的文件,并添加以下内容:
<launch>
<node name="offb_node" pkg="my_offboard_node" type="offb_node" output="screen"/>
</launch>
5. 编译和运行
编译ROS包。在工作空间中运行以下命令:
catkin_make
编译完成后,不要忘记运行source devel/setup.bash
来更新环境。
最后,可以通过运行以下命令来启动节点:
roslaunch my_offboard_node offb_node.launch