深入了解ROS之编写无人机控制程序包
这篇教程来详细介绍一下如何编写出一个控制无人机的ROS程序包
编写ROS程序包推荐roboware-studio,这款软件是在开源的vscode基础上二次开发,用于ROS程序包的开发,非常好用
roboware-studio的安装,roboware-studio软件包的下载地址,官网已经进不去了,直接到github上下载deb包就好,下载时注意软件包有32位和64位之分,根据自己系统的类型进行下载,可以双击安装或者使用命令:
sudo dpkg -i xxxxxxx.deb
这款集成开发环境很好用,减少了很多手工操作,但是还是建议先弄明白每个文件都是什么作用,先手工编写几次软件包后再考虑使用这种自动化的IDE
开始编写ROS程序包
前面的建立程序包的操作这里不赘述了,参考这篇博客,直接介绍如何编写src文件夹下的.cpp文件,这里以一个控制无人机起飞和降落的程序为例进行讲解
示例程序如下:
/**
* @file offb_node.cpp
* @brief Offboard control example node, written with MAVROS version 0.19.x, PX4 Pro Flight
* Stack and tested in Gazebo SITL
*/
#include
#include
#include
#include
#include
mavros_msgs::State current_state;
void state_cb(const mavros_msgs::State::ConstPtr& msg){
current_state = *msg;
}
geometry_msgs::PoseStamped aim_pos;
bool flag = true;
void arrive_pos(const geometry_msgs::PoseStamped::ConstPtr& msg){
if((*msg).pose.position.z > aim_pos.pose.position.z * 0.95){
flag = false;
}
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "takeoff_land_node");
ros::NodeHandle nh;
ros::Subscriber state_sub = nh.subscribe<:state>
("mavros/state", 10, state_cb);
ros::Publisher local_pos_pub = nh.advertise<:posestamped>
("mavros/setpoint_position/local", 10);
ros::ServiceClient arming_client = nh.serviceClient<:commandbool>
("mavros/cmd/arming");
ros::ServiceClient set_mode_client = nh.serviceClient<:setmode>
("mavros/set_mode