Changing the flight mode

Simulate, build and deploy your algorithms with Erle-Copter

Source code

This creates a client for the mavros::SetMode service. Setting the variable srv_setMode.request.custom_mode we can select the mode of Erle-Copter.

Availables flight modes: Stabilize, Alt_Hold, Loiter, RTL (Return-to-Launch), Auto, Acro, AutoTune, Brake, Circle, Drift, Guided, Land, PosHold, Sport ...

More info about flight modes here

#include <cstdlib>

#include <ros/ros.h>
#include <mavros_msgs/SetMode.h>

int main(int argc, char **argv)
{
    if(argc<2){
        printf("Usage: rosrun ros_erle_cpp_examples_change_mode change_mode <mode>\n");
        return -1;
    }

    ros::init(argc, argv, "mavros_change_mode");
    ros::NodeHandle n;

    int rate = 10;
    ros::Rate r(rate);

    ros::ServiceClient cl = n.serviceClient<mavros_msgs::SetMode>("/mavros/set_mode");
    mavros_msgs::SetMode srv_setMode;
    srv_setMode.request.base_mode = 0;
    srv_setMode.request.custom_mode = argv[1];
    if(cl.call(srv_setMode)){
        ROS_ERROR("setmode send ok %d value:", srv_setMode.response.success);
    }else{
        ROS_ERROR("Failed SetMode");
        return -1;
    }

    while (n.ok()){
      ros::spinOnce();
      r.sleep();
    }

    return 0;
}

mkdir -p ~/erle_ws/src
cd ~/erle_ws/src
git clone https://github.com/erlerobot/gazebo_cpp_examples
cd ..
catkin_make --pkg ros_erle_cpp_examples_change_mode

The two following commands indicate how to change the flight mode using this ros node.

rosrun ros_erle_cpp_examples_change_mode changemode STABILIZE
rosrun ros_erle_cpp_examples_change_mode changemode LOITER

If we need to check if the flight mode has change. We can execute the following command in a new terminal

rostopic echo /mavros/state
header:
seq: 22
stamp:
secs: 1472221073
nsecs: 958554366
frame_id: ''
connected: True
armed: True
guided: False
mode: LOITER

...

header:
seq: 35
stamp:
secs: 1472221078
nsecs: 958554366
frame_id: ''
connected: True
armed: True
guided: False
mode: STABILIZE