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

👉👉👉**无人机硬件,提供全程指导**👈👈👈

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

**

**

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

**

ROS无人机多点飞行讲解

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

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

 make px4_sitl_default gazebo

在这里插入图片描述2、启动MAVROS仿真

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

在这里插入图片描述3、启动飞行正方形节点

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

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超维空间科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值