Arming and disarming Erle-Copter

Simulate, build and deploy your algorithms with Erle-Copter

Source code

#include <cstdlib>

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

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

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

    ////////////////////////////////////////////
    ///////////////////ARM//////////////////////
    ////////////////////////////////////////////
    ros::ServiceClient arming_cl = n.serviceClient<mavros_msgs::CommandBool>("/mavros/cmd/arming");
    mavros_msgs::CommandBool srv;
    srv.request.value = true;
    if(arming_cl.call(srv)){
        ROS_ERROR("ARM send ok %d", srv.response.success);
    }else{
        ROS_ERROR("Failed arming");
    }

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

    return 0;
}

ros/ros.h is a convenience include that includes all the headers necessary to use the most common public pieces of the ROS system. This includes the mavros_msgs/*.h messages, which resides in the mavros package.

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

Initialize ROS. This allows ROS to do name remapping through the command line — not important for now. This is also where we specify the name of our node. Node names must be unique in a running system. The ros::init() function needs to see argc and argv so that it can perform any ROS arguments and name remapping that were provided at the command line.

ros::init(argc, argv, "mavros_arming");

Create a handle to this process’ node. The first NodeHandle created will actually do the initialization of the node, and the last one destructed will cleanup any resources the node was using. NodeHandle is the main access point to communications with the ROS system. The first NodeHandle constructed will fully initialize this node, and the last NodeHandle destructed will close down the node.

ros::NodeHandle n;

The ros::ServiceClient object is used to call the service later on. Here we instantiate an autogenerated service class, and assign values into its request member. A service class contains two members, request and response. It also contains two class definitions, Request and Response. This actually calls the service. Since service calls are blocking, it will return once the call is done. If the service call succeeded, call() will return true and the value in srv.response will be valid. If the call did not succeed, call() will return false and the value in srv.response will be invalid.

This creates a client for the arming service. If srv.request.value is True the vehicle try to arm.

ros::ServiceClient arming_cl = n.serviceClient<mavros_msgs::CommandBool>("/mavros/cmd/arming");
mavros::CommandBool srv;
srv.request.value = true;
if(arming_cl.call(srv)){
    ROS_ERROR("ARM send ok %d", srv.response.success);
}else{
    ROS_ERROR("Failed arming");
}

#include <cstdlib>

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

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

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

    ////////////////////////////////////////////
    //////////////////DISARM////////////////////
    ////////////////////////////////////////////
    ros::ServiceClient disarming_cl = n.serviceClient<mavros_msgs::CommandBool>("/mavros/cmd/arming");
    mavros_msgs::CommandBool srv;
    srv.request.value = false;
    if(disarming_cl.call(srv)){
        ROS_ERROR("ARM send ok %d", srv.response.success);
    }else{
        ROS_ERROR("Failed disarming");
    }

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

    return 0;
}

The main difference between these two codes is the following line. If srv.request.value is True the vehicle try to arm and if this value is equal to False the vehicle try to disarm.

srv.request.value = false;

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_arming_and_disarming

If you want to arm the vehicle:

rosrun ros_erle_cpp_examples_arming_and_disarming arming

If the autopilot is launched and armed the green and orange LED will be ON.

Armed
Armed

If you want to arm the vehicle:

rosrun ros_erle_cpp_examples_arming_and_disarming disarming

If the autopilot is launched and not armed the green LED will be ON and the orange will be blinking

Not Armed
Not Armed