当前位置:网站首页>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;
}
边栏推荐
- 如何使用自定义注解进行参数校验
- 对多线程的理解
- 深圳中国专利奖政策支持介绍,补贴100万
- SAS期末复习知识点总结(应用多元统计实验笔记)
- SAS聚类分析(系统聚类cluster,动态聚类fastclus,变量聚类varclus)
- I was so excited about the college entrance examination in 2022
- Basic introduction of graph and depth first traversal and breadth first traversal
- Uninstall mavros
- 云呐|省级行政单位固定资产管理系统
- Introduction to China patent award policy support, with a subsidy of 1million yuan
猜你喜欢

中间件_Redis_05_Redis的持久化

SAS principal component analysis (finding correlation matrix, eigenvalue, unit eigenvector, principal component expression, contribution rate and cumulative contribution rate, and data interpretation)

Inventory management and strategy mode

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

云呐|庆远固定资产管理及条码盘点系统

SAS主成分分析(求相关阵,特征值,单位特征向量,主成分表达式,贡献率和累计贡献率以及进行数据解释)

记录打包GoogleChrome浏览器插件

The emperors of the Ming Dynasty

Implementing MySQL fuzzy search with node and express

库存管理与策略模式
随机推荐
Introduction to the policy support of Beijing China Patent Award, with a subsidy of 1million yuan
数字ic设计自学ing
快递鸟系统对接
Classic questions: 01 backpack, complete backpack, multiple backpack, two-dimensional cost Backpack
Store binary tree in sequence [store tree in array]
CSRF attack
Time dependent - format, operation, comparison, conversion
ava. Lang.noclassdeffounderror: org/apache/velocity/context/context solution
SAS cluster analysis (system cluster, dynamic cluster fastclus, variable cluster varclus)
Understanding of multithreading
Throttling and anti chattering of functions
SAS discriminant analysis (Bayes criterion and proc discrim process)
云呐|庆远固定资产管理及条码盘点系统
Summary of SAS final review knowledge points (notes on Application of multivariate statistics experiment)
Record the packaging of the googlechrome browser plug-in
After a/b machine is connected normally, B machine suddenly restarts. Ask a what is the TCP status at this time? How to eliminate this state in the server program?
Docking of express bird system
Yunna Qingyuan fixed assets management and barcode inventory system
焱融看|混合云环境下,如何实现数据湖最优存储解决方案
About mobx