Ardupilot之Mavros实现Ros节点控制(二)

Ardupilot之Mavros实现Ros节点控制(二)

  • offboard建立
  • 仿真运行

未完成mavros安装及相关设置的参考上一篇文章
Ardupilot之Mavros实现Ros节点控制(一)

这一部分写ros相关节点的设置
首先要建立一个工作空间,具体看前面文章
Ros机器人之(三)创建工作空间和功能包
src中新建文件夹offboard

offboard建立

cd src/
catkin_create_pkg offboard roscpp mavros geometry_msgs
cd ..
catkin_make
source devel/setup.bash 

offboard/src新建offboard.cpp,代码参考官网。
修改两个地方:
一是 offb_set_mode.response.mode_sent
二是 ardupilot中没有offboard模式,修改成GUIDED模式

最终效果是,能连接,能解锁,但是不能起飞。
我理解应该是poses位置节点发布与ardupilot的问题。网上毫无参考。求看到的大佬告知一二。


#include 
#include 
#include 
#include 
#include 

mavros_msgs::State current_state;
void state_cb(const mavros_msgs::State::ConstPtr& msg){
    current_state = *msg;
}

int main(int argc, char **argv)
{
    ROS_INFO("HELLO");
    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 = "GUIDED";

    mavros_msgs::CommandBool arm_cmd;
    arm_cmd.request.value = true;

    ros::Time last_request = ros::Time::now();

    while(ros::ok()){
        if( current_state.mode != "GUIDED" &&
            (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;
}

修改cmakelist中内容:

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

然后编译一下:

cd catkin/
catkin_make
source devel/setup.bash

仿真运行

一个终端:

sim_vehicle.py -v ArduCopter -f gazebo-iris  -m --mav10 --map --console -I0

二个终端:

gazebo --verbose worlds/iris_arducopter_runway.world

三个终端:

roslaunch apm.launch

四个终端:

rosrun offboard offboard

Ardupilot之Mavros实现Ros节点控制(二)_第1张图片

你可能感兴趣的:(ROS,飞控相关)