通过MAVROS控制仿真无人机

首先,在目录中建立工作区,并进行初始化操作

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws
catkin init
wstool init src
rosinstall_generator --rosdistro kinetic mavlink | tee /tmp/mavros.rosinstall
rosinstall_generator --upstream mavros | tee -a /tmp/mavros.rosinstall
wstool merge -t src /tmp/mavros.rosinstall
wstool update -t src
rosdep install --from-paths src --ignore-src -y
wget https://raw.githubusercontent.com/mavlink/mavros/master/mavros/scripts/install_geographiclib_datasets.sh
sudo chmod 777 install_geographiclib_datasets.sh
sudo ./install_geographiclib_datasets.sh
catkin build
source ~/catkin_ws/devel/setup.bash

然后,在catkin_ws/src目录中运行如下指令

catkin_create_pkg offboard_pkg roscpp std_msgs geometry_msgs mavros_msgs

在~/catkin_ws/src/offboard_pkg/src/目录中新建一个文件offboard_node.cpp,写入如下代码

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

                }

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

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


		 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=1;
                }
        local_pos_pub.publish(pose);
        ros::spinOnce();
        rate.sleep();
    }

    return 0;
}

该代码的作用为让无人机飞至5米高度,然后循环按照正方形轨迹飞行

然后打开目录~/catkin_ws/src/offboard_pkg/下的CMakeLists.txt添加下面的两行:

add_executable(offboard_node src/offboard_node.cpp)
target_link_libraries(offboard_node ${catkin_LIBRARIES})

然后到目录~/catkin_ws下,运行命令

catkin build

然后,使终端进入PX4的安装目录下,运行命令

make px4_sitl gazebo_iris

即可进入gazebo仿真界面

接着,打开QGC地面站,并运行如下命令连接Mavros和PX4

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

执行刚才的C++文件

source ~/catkin_ws/devel/setup.bash
rosrun offboard_pkg offboard_node

在gazebo中观察仿真结果即可。

 

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用mavros控制无人机起飞和降落的C++代码示例: ```cpp #include <ros/ros.h> #include <mavros_msgs/CommandBool.h> #include <mavros_msgs/CommandTOL.h> #include <mavros_msgs/SetMode.h> int main(int argc, char **argv) { ros::init(argc, argv, "takeoff_landing"); ros::NodeHandle nh; // 创建发布器 ros::Publisher arming_client = nh.serviceClient<mavros_msgs::CommandBool>("mavros/cmd/arming"); ros::Publisher takeoff_client = nh.serviceClient<mavros_msgs::CommandTOL>("mavros/cmd/takeoff"); ros::Publisher land_client = nh.serviceClient<mavros_msgs::CommandTOL>("mavros/cmd/land"); ros::ServiceClient set_mode_client = nh.serviceClient<mavros_msgs::SetMode>("mavros/set_mode"); // 设置模式为MANUAL mavros_msgs::SetMode set_mode; set_mode.request.base_mode = 0; set_mode.request.custom_mode = "MANUAL"; set_mode_client.call(set_mode); // 解锁 mavros_msgs::CommandBool arm_cmd; arm_cmd.request.value = true; arming_client.call(arm_cmd); // 等待解锁 while (!arm_cmd.response.success) { arm_cmd.response.success = false; arm_cmd.response.result = ""; ROS_INFO("Waiting for arming..."); arming_client.call(arm_cmd); ros::Duration(1.0).sleep(); } // 起飞 mavros_msgs::CommandTOL takeoff_cmd; takeoff_cmd.request.altitude = 3; // 设置起飞高度 takeoff_client.call(takeoff_cmd); // 等待起飞 while (!takeoff_cmd.response.success) { takeoff_cmd.response.success = false; takeoff_cmd.response.result = ""; ROS_INFO("Waiting for takeoff..."); takeoff_client.call(takeoff_cmd); ros::Duration(1.0).sleep(); } // 降落 mavros_msgs::CommandTOL land_cmd; land_cmd.request.altitude = 0; // 设置降落高度 land_client.call(land_cmd); // 等待降落 while (!land_cmd.response.success) { land_cmd.response.success = false; land_cmd.response.result = ""; ROS_INFO("Waiting for landing..."); land_client.call(land_cmd); ros::Duration(1.0).sleep(); } // 上锁 arm_cmd.request.value = false; arming_client.call(arm_cmd); return 0; } ``` 请注意,这仅仅是一个简单的示例,实际使用中可能需要根据具体需求进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

街边摆摊的宅男

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

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

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

打赏作者

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

抵扣说明:

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

余额充值