当前位置:网站首页>2.2. Ros+px4 simulation multi-point cruise flight - Square
2.2. Ros+px4 simulation multi-point cruise flight - Square
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
**
**
After this part of the code starts , Drones will be deployed every interval 5 Fly a target point in seconds , Total flight 4 A little bit , Form a square
**
ROS Explanation of UAV multi-point flight
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 flying square node
rosrun square_demo square_demo_node

#include <ros/ros.h>
#include <geometry_msgs/PoseStamped.h>
#include <mavros_msgs/CommandBool.h>
#include <mavros_msgs/SetMode.h>
#include <mavros_msgs/State.h>
int count;
int flag=1;
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, "offboard");
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");
//the setpoint publishing rate MUST be faster than 2Hz
ros::Rate rate(8.0);
// wait for FCU connection
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 = 3;
//send a few setpoints before starting
for(int i = 100; ros::ok() && i > 0; --i){
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();
}
}
if((flag == 1) && (ros::Time::now() - last_request > ros::Duration(5.0)))
{
ROS_INFO("position1(0 , 0, 5)");
pose.pose.position.x = 0;
pose.pose.position.y = 0;
pose.pose.position.z = 5;
last_request = ros::Time::now();
flag=2;
//local_pos_pub.publish(pose);
}
if((flag ==2) && (ros::Time::now() - last_request > ros::Duration(5.0)))
{
ROS_INFO("position2(5 , 0, 5)");
pose.pose.position.x = 5.0;
pose.pose.position.y = 0.0;
pose.pose.position.z = 5;
last_request = ros::Time::now();
flag=3;
//local_pos_pub.publish(pose);
}
if((flag ==3) && (ros::Time::now() - last_request > ros::Duration(5.0)))
{
ROS_INFO("position3(5 , 5, 5)");
pose.pose.position.x = 5.0;
pose.pose.position.y = 5.0;
pose.pose.position.z = 5;
last_request = ros::Time::now();
flag=4;
//local_pos_pub.publish(pose);
}
if((flag ==4) && (ros::Time::now() - last_request > ros::Duration(5.0)))
{
ROS_INFO("position4(0 , 5, 5)");
pose.pose.position.x = 0;
pose.pose.position.y = 5.0;
pose.pose.position.z = 5;
last_request = ros::Time::now();
flag=5;
//local_pos_pub.publish(pose);
}
if((flag ==5) && (ros::Time::now() - last_request > ros::Duration(5.0)))
{
ROS_INFO("position5(0 ,0, 5)");
pose.pose.position.x = 0;
pose.pose.position.y = 0;
pose.pose.position.z = 5;
last_request = ros::Time::now();
flag=6;
//local_pos_pub.publish(pose);
}
local_pos_pub.publish(pose);
ros::spinOnce();
rate.sleep();
}
return 0;
}

边栏推荐
- Yunna PDA wireless fixed assets inventory management system
- 关于mobx
- detectron2训练自己的数据集和转coco格式
- Some tips for programmers to deal with stress
- Record the packaging of the googlechrome browser plug-in
- Introduction to the subsidy fund for leading technological innovation of Beijing enterprises, with a subsidy of 5million yuan
- Introduction to prefix, infix and suffix expressions (code implementation of inverse Polish calculator)
- 腾讯云数据库TDSQL-大咖论道 | 基础软件的过去、现在、未来
- hooks的设计哲学
- The emperors of the Ming Dynasty
猜你喜欢

焱融看|混合云环境下,如何实现数据湖最优存储解决方案

Understanding of multithreading

Inventory management and strategy mode

Yunna PDA wireless fixed assets inventory management system

OCR文字识别经典论文详解
![[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

Merge sort and cardinality sort

How to write this with data and proc without SQL
![[ongoing update...] 2021 National Electronic Design Competition for college students (III) interpretation of the anonymous four axis space developer flight control system design](/img/63/3193186820215b9babc3d00e1ef20b.jpg)
[ongoing update...] 2021 National Electronic Design Competition for college students (III) interpretation of the anonymous four axis space developer flight control system design

PX4从放弃到精通(二十四):自定义机型
随机推荐
Beijing Dongcheng District high tech enterprise cultivation support standard, with a subsidy of 100000 yuan
MultipartFile和File互转工具类
SSH Remote Login configuration sshd_ Config file details
Beijing Pinggu District high tech enterprise cultivation support standard, with a subsidy of 100000 yuan
[VBA Script] extract the information and pending status of all annotations in the word document
Record the packaging of the googlechrome browser plug-in
IRS应用发布之十六:H5 应用设计指南
深圳中国专利奖申报流程介绍,补贴100万
中间件_Redis_05_Redis的持久化
Web3 ecological decentralized financial platform sealem Finance
[path planning] week 1: Path Planning open source code summary (ROS) version
IRS应用发布之十五:应用安全自测指南
SAS principal component analysis (finding correlation matrix, eigenvalue, unit eigenvector, principal component expression, contribution rate and cumulative contribution rate, and data interpretation)
如何使用自定义注解进行参数校验
Current limiting and download interface request number control
[path planning] week 1: hodgepodge
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?
深圳市南山区专精特新企业申报条件,补贴10-50万
Basic introduction of graph and depth first traversal and breadth first traversal
QGC地面站使用教程