Bootstrap

PX4+Offboard模式+代码控制无人机起飞(Gazebo)

参考PX4自动驾驶用户指南
https://docs.px4.io/main/zh/ros/mavros_offboard_cpp.html

我的另一篇博客写了 键盘控制PX4无人机飞行
PX4无人机 - 键盘控制飞行代码

可以先借鉴本篇博客,实现代码控制无人机起飞,熟悉offboard模式

新建ros项目工程

mkdir -p px4_offboard_ws/src

接着进入文件编译一下

cd px4_offboard_ws
catkin_make

进入src目录,创建ros功能包

catkin_create_pkg t1_offboard_takeoff rospy roscpp

在src目录下新建offboard.cpp文件
并将以下代码复制进去

/**
 * @file offb_node.cpp
 * @brief Offboard control example node, written with MAVROS version 0.19.x, PX4 Pro Flight
 * Stack and tested in Gazebo SITL
 */

#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;
void state_cb(const mavros_msgs::State::ConstPtr& msg){
    current_state = *msg;
}

int main(int argc, char **argv)
{
    ros::init(argc, argv, "offb_node");
    ros::NodeHandle nh;

    //订阅mavros状态
    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");
    //定义设置模式服务客户端(设置offboard模式)
    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(20.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 = 2;

    //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();
            }
        }

        local_pos_pub.publish(pose);

        ros::spinOnce();
        rate.sleep();
    }

    return 0;
}

在src目录下的CMakeLists.txt文件中,在Build的最后(Install的上面)添加下面两行代码

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

再回到px4_offboard_ws下进行编译

cd ~/px4_offboard_ws
catkin_make

编译成功后运行先px4仿真

roslaunch px4 mavros_posix_sitl.launch

px4仿真的教程在我上一篇博客中Ubuntu20.04+MAVROS+PX4+Gazebo保姆级安装教程
接着运行ros功能包

cd ~/px4_offboard_ws
source devel/setup.bash
rosrun t1_offboard_takeoff t1_offboard_takeoff

可以看到gazebo中的无人机已经起飞

在这里插入图片描述想要无人机降落可以通过代码控制,也可以在运行px4的终端中输入 commander land
(需要先终止ros程序 ctrl+c)
在这里插入图片描述可以看到无人机已经降落

;