当前位置:网站首页>2.1 ros+px4 simulation - Fixed Point flight control
2.1 ros+px4 simulation - Fixed Point flight control
2022-06-11 01:36:00 【Mbot】
**
introduction : This section is performed after the simulation environment has been installed . The installation of simulation environment can refer to the video explanation : Links are as follows
**
Detailed explanation of environment installation and fixed-point flight code
The video starts with the little turtle routine , The topic subscription and publication are described in detail , The code in this section is analyzed later
video : Code details
1、 Enter into Firmware Under the folder , start-up gazebo Simulation environment :
make px4_sitl_default gazebo

2、 start-up MAVROS Simulation
`roslaunch mavros px4.launch fcu_url:="udp://:[email protected]:14557"`

3、 Start flight node
rosrun offboard_node offboard_node

4、 code annotation :
// The relevant header file contains
#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;
// Callback function
void state_cb(const mavros_msgs::State::ConstPtr& msg){
current_state = *msg;
}
int main(int argc, char **argv)
{
// Initialize node
ros::init(argc, argv, "offb_node");
// Create node handle
ros::NodeHandle nh;
// Create subscribers , Subscribe to the topic mavros/state
ros::Subscriber state_sub = nh.subscribe<mavros_msgs::State>
("mavros/state", 10, state_cb);
// Create a publisher , Release "mavros/setpoint_position/local" topic of conversation
ros::Publisher local_pos_pub = nh.advertise<geometry_msgs::PoseStamped>
("mavros/setpoint_position/local", 10);
// Create a client , This client is used to request PX4 Unlock the UAV
ros::ServiceClient arming_client = nh.serviceClient<mavros_msgs::CommandBool>
("mavros/cmd/arming");
// Create a client , This client is used to request entry offboard Pattern
ros::ServiceClient set_mode_client = nh.serviceClient<mavros_msgs::SetMode>
("mavros/set_mode");
// collocation rate.sleep() function , Realization 20Hz Cycle of frequency ;
ros::Rate rate(20.0);
// Wait for flight control connection
while(ros::ok() && !current_state.connected){
ros::spinOnce();
rate.sleep();
}
// Create a variable , This variable is the position where the UAV is about to take off , The starting point is the power on time of the UAV
geometry_msgs::PoseStamped pose;
pose.pose.position.x = 0;
pose.pose.position.y = 0;
pose.pose.position.z = 2;
// Pre release desired location information , Give the drone a target point
//send a few setpoints before starting
for(int i = 100; ros::ok() && i > 0; --i){
local_pos_pub.publish(pose);
ros::spinOnce();
rate.sleep();
}
// Set the client to request the UAV to enter “OFFBOARD” Pattern
mavros_msgs::SetMode offb_set_mode;
offb_set_mode.request.custom_mode = "OFFBOARD";
// Set the customer to ask the UAV to unlock
mavros_msgs::CommandBool arm_cmd;
arm_cmd.request.value = true;
// Get the timestamp
ros::Time last_request = ros::Time::now();
while(ros::ok()){
//if Statement loop request to enter OFFBOARD Pattern , After entering, you will enter else Statement request to unlock the UAV .
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();
}
}
// Circularly publish the desired location information ,20Hz frequency
local_pos_pub.publish(pose);
ros::spinOnce();
rate.sleep();
}
return 0;
}
边栏推荐
- Sealem finance builds Web3 decentralized financial platform infrastructure
- 如何下载网页照片
- 北京东城区高新技术企业培育支持标准,补贴10万
- 函数的节流和防抖
- Record the packaging of the googlechrome browser plug-in
- SAS因子分析(proc factor过程和因子旋转以及回归法求因子得分函数)
- 2.1、ROS+PX4仿真---定点飞行控制
- Yunna provincial administrative unit fixed assets management system
- Introduction to China patent award policy support, with a subsidy of 1million yuan
- A/B机器正常连接后, B机器突然重启, 问A此时处于TCP的 什么状态?如何消除服务器程序中的这个状态?
猜你喜欢

库存管理与策略模式

云呐|PDA无线固定资产盘点管理系统

Project_ Visual analysis of epidemic data based on Web Crawler

PX4从放弃到精通(二十四):自定义机型

如何下载网页照片

2.1、ROS+PX4仿真---定点飞行控制

简述自定义注解

Merge sort and cardinality sort

Projet Visualisation et analyse des données sur les épidémies basées sur le Web crawler
![[VBA Script] extract the information and pending status of all annotations in the word document](/img/dc/0db51d092cde019cef4113796e4882.png)
[VBA Script] extract the information and pending status of all annotations in the word document
随机推荐
Introduction to China patent award policy support, with a subsidy of 1million yuan
I was so excited about the college entrance examination in 2022
[recommended by Zhihu knowledge master] castle in UAV - focusing on the application of UAV in different technical fields
Classic questions: 01 backpack, complete backpack, multiple backpack, two-dimensional cost Backpack
Millions of visits - resolution of high concurrency problems
Simple select sort and heap sort
深圳中国专利奖政策支持介绍,补贴100万
中间件_Redis_05_Redis的持久化
云呐|PDA无线固定资产盘点管理系统
MultipartFile和File互转工具类
中间件_Redis_06_Redis的事务
Inventory management and strategy mode
Bad RequestThis combination of host and port requires TLS.
北京通州区高新技术企业培育支持标准,补贴10万
腾讯云数据库TDSQL-大咖论道 | 基础软件的过去、现在、未来
关于mobx
OCR文字识别经典论文详解
Sealem finance builds Web3 decentralized financial platform infrastructure
Yunna PDA wireless fixed assets inventory management system
SAS factor analysis (proc factor process, factor rotation and regression method for factor score function)