PX4|mavros offboard控制

12 篇文章 1 订阅
9 篇文章 0 订阅

在px4官网中有相关mavros的教程MAVROS Offboard control example (C++) | PX4 自动驾驶用户指南

本文将对该教程进行复现,并加以细节补充

首先安装mavros

sudo apt-get install ros-melodic-mavros ros-melodic-mavros-extras

wget http://ghproxy.com/https://raw.githubusercontent.com/mavlink/mavros/master/mavros/scripts/install_geographiclib_datasets.sh
./install_geographiclib_datasets.sh   

提示成功后即可完成mavros的二进制安装

构建offboard指令发布节点

mkdir -p px4_ros/src
cd px4_ros/src
catkin_create_pkg offboard roscpp std_msgs geometry_msgs mavros_msgs
cd offboard/src
gedit offboard_node.cpp

将官网上的offboard相关代码复制至offboard_node.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;

    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进行修改

#回到offboard包的目录中,更改其中的cmakelists
cd ..
gedit CMakeLists.txt

将以下内容粘贴至最后面

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

保存退出后在px4_ros目录下进行编译

catkin make
#将setup.bash加入至~/.bashrc中
echo"source ~/px4_ros/devel/setup,bash">> ~/.bashrc

重新打开终端

在三个终端中分别输入

roslaunch mavros px4.launch fcu_url:="udp://:14540@127.0.0.1:14557"
cd PX4-Autopilots
make px4-sitl gazebo
rosrun offboard pbn

即可在gazebo仿真环境中实现起飞

 

和飞控通信

将飞控通过usb连接至pc端,关闭地面站及gazebo

另开终端输入

chmod 777 /dev/ttyACM0
roslaunch mavros px4.launch fcu_url:=/dev/ttyACM0

#这里需确定串口地址,可能为ACM0或ACM1  也可能是USB0
#可以逐一尝试

即可完成飞控与pc端的通信

另开终端输入

rostopic echo /mavros/state

在connected:项可查看连接状态 

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值