当前位置:网站首页>2.1、ROS+PX4仿真---定点飞行控制
2.1、ROS+PX4仿真---定点飞行控制
2022-06-11 00:25:00 【Mbot】
**
引言:本小节是在已经安装好仿真环境的情况下进行。仿真环境的安装可以参考视频讲解:链接如下
**
视频从小海龟例程开始,详细阐述了话题的订阅和发布,后续分析了本小节代码
视频:代码详解
1、进入到Firmware文件夹下,启动gazebo仿真环境:
make px4_sitl_default gazebo

2、启动MAVROS仿真
`roslaunch mavros px4.launch fcu_url:="udp://:[email protected]:14557"`

3、启动飞行节点
rosrun offboard_node offboard_node

4、代码注释:
//相关头文件包含
#include <ros/ros.h>
#include <geometry_msgs/PoseStamped.h>
#include <mavros_msgs/CommandBool.h>
#include <mavros_msgs/SetMode.h>
#include <mavros_msgs/State.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;
//创建订阅者,订阅话题mavros/state
ros::Subscriber state_sub = nh.subscribe<mavros_msgs::State>
("mavros/state", 10, state_cb);
//创建一个发布者,发布"mavros/setpoint_position/local"话题
ros::Publisher local_pos_pub = nh.advertise<geometry_msgs::PoseStamped>
("mavros/setpoint_position/local", 10);
//创建一个客户端,该客户端用来请求PX4无人机的解锁
ros::ServiceClient arming_client = nh.serviceClient<mavros_msgs::CommandBool>
("mavros/cmd/arming");
//创建一个客户端,该客户端用来请求进入offboard模式
ros::ServiceClient set_mode_client = nh.serviceClient<mavros_msgs::SetMode>
("mavros/set_mode");
//搭配rate.sleep()函数,实现20Hz频率的循环;
ros::Rate rate(20.0);
// 等待飞控连接
while(ros::ok() && !current_state.connected){
ros::spinOnce();
rate.sleep();
}
//创建一个变量,该变量是无人机即将起飞到的位置,相对于无人机上电时刻作为起始点
geometry_msgs::PoseStamped pose;
pose.pose.position.x = 0;
pose.pose.position.y = 0;
pose.pose.position.z = 2;
//预发布期望位置信息,给无人机目标点
//send a few setpoints before starting
for(int i = 100; ros::ok() && i > 0; --i){
local_pos_pub.publish(pose);
ros::spinOnce();
rate.sleep();
}
//设置客户端请求无人机进入“OFFBOARD”模式
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语句循环请求进入OFFBOARD模式,进入以后则会进入else语句请求对无人机解锁。
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();
}
}
//循环发布期望位置信息,20Hz频率
local_pos_pub.publish(pose);
ros::spinOnce();
rate.sleep();
}
return 0;
}
边栏推荐
- Classic questions: 01 backpack, complete backpack, multiple backpack, two-dimensional cost Backpack
- Non presented paper (no show) policy
- Introduction and creation of Huffman tree
- IRS application release 16: H5 application design guide
- Function of barcode fixed assets management system, barcode management of fixed assets
- 北京朝阳区专精特新制造业企业支持标准介绍,补贴100万
- Conda安装Pytorch后numpy出现问题
- Understanding of multithreading
- [paper translation] recent advances in open set recognition: a survey
- Shenzhen Nanshan District specialized, special and new enterprise application conditions, with a subsidy of 100000-500000 yuan
猜你喜欢
![[paper reading] tganet: text guided attention for improved polyp segmentation](/img/e4/a80615e78b819a50886410cc69146d.png)
[paper reading] tganet: text guided attention for improved polyp segmentation

Middleware_ Redis_ 05_ Persistence of redis

Recruitment | Nanjing | triostudio Sanli Agency - Interior Designer / construction drawing deepening Designer / device / Product Designer / Intern, etc

Web3 ecological decentralized financial platform sealem Finance

Implementing MySQL fuzzy search with node and express

限流与下载接口请求数控制

对多线程的理解

How to use user-defined annotation for parameter verification

Record the packaging of the googlechrome browser plug-in

Inventory management and strategy mode
随机推荐
北京门头沟区高新技术企业培育支持标准,补贴10万
網絡基礎(1)-----認識網絡
北京延庆区高新技术企业培育支持标准,补贴10万
Deepstream series fish eye camera test
深圳中国专利奖申报流程介绍,补贴100万
Shenzhen Nanshan District specialized, special and new enterprise application conditions, with a subsidy of 100000-500000 yuan
数字ic设计自学ing
Support standard for cultivation of high-tech enterprises in Miyun District, Beijing, with a subsidy of 100000 yuan
北京中国专利奖申报流程介绍,补贴100万
The emperors of the Ming Dynasty
SQL question brushing and sorting in leetcode of partial deduction
中间件_Redis_06_Redis的事务
Hooks' design philosophy
Understanding of multithreading
Support standard for cultivation of high-tech enterprises in Changping District, Beijing, with a subsidy of 100000 yuan
Bubble sort and quick sort
深圳市南山区专精特新企业申报流程,补贴10-50万
快递鸟系统对接
Summary of SAS final review knowledge points (notes on Application of multivariate statistics experiment)
北京朝阳区专精特新制造业企业支持标准介绍,补贴100万