mavros 外部控制例程

mavros 外部控制例程

  • 编写ROS包
  • 开始仿真

前期需要安装好ros、mavros、PX4原生固件。

编写ROS包

首先我们为外部控制例程建立一个工作空间

mkdir -p Offboard_ws/src

接下来我们新建一个功能包

cd Offboard_ws/src
catkin_create_pkg offboard roscpp std_msgs geometry_msgs mavros_msgs

建立好ros包过后我们新建一个可cpp源文件用来作为这个功能包的执行文件

cd offboard/src
gedit 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>

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;

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

接下来我们修改CMakeLists.txt为编译做准备

cd ..
gedit CMakeLists.txt

然后在最末尾添加如下内容后保存退出

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

然后我们就可以开始编译了

cd ~/Offboard_ws
catkin_make

我们可以看到编译成功生成了一个可执行文件,也就是一个Node。
我们可以把这个工作空间添加到.bashrc里面方便启动节点

echo "source ~/Offboard_ws/devel/setup.bash" >> ~/.bashrc

开始仿真

首先打开一个终端启动仿真

cd px4_src/Firmware
make px4_sitl gazebo

然后再开一个终端运行mavros

roslaunch mavros px4.launch fcu_url:="udp://:[email protected]:14557"

接下来在打开一个终端运行外部控制的节点

rosrun offboard offboard_node

飞机最后起飞悬停在两米的位置
关于mavros的其他操作详细见mavros官网

你可能感兴趣的:(无人机)