0
点赞
收藏
分享

微信扫一扫

2.2、ROS+PX4仿真多点巡航飞行----正方形


**

引言:本小节是在已经安装好仿真环境的情况下进行。仿真环境的安装可以参考视频讲解:链接如下

**

**

此部分代码启动以后,无人机将会每间隔5秒时间飞行一个目标点,一共飞行4个点,形成一个正方形

**

​​ROS无人机多点飞行讲解​​

视频从小海龟例程开始,详细阐述了话题的订阅和发布,后续分析了本小节代码

​​视频:代码详解​​

1、进入到Firmware文件夹下,启动gazebo仿真环境:

make px4_sitl_default gazebo

2.2、ROS+PX4仿真多点巡航飞行----正方形_#include2、启动MAVROS仿真

roslaunch mavros px4.launch fcu_url:="udp://:14540@127.0.0.1:14557"

2.2、ROS+PX4仿真多点巡航飞行----正方形_自动驾驶_023、启动飞行正方形节点

rosrun square_demo square_demo_node

2.2、ROS+PX4仿真多点巡航飞行----正方形_#include_03

#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;
}




举报

相关推荐

0 条评论