ROS Report
ROS Report
SL.N PAGE
DESCRIPRITIONS
O. NO.
→ To add custom message and services in ROS, you need to follow a series of steps
for both .msg and .srv files.
→ From defining the files to modifying the package.xml and CMakeLists.txt files,
and then building and testing the package.
In this section, we are going to create ROS nodes, which can use the services definition
that we defined already. The service nodes we are going to create can send a string
message as a request to the server and the server node will send another message as a
response.
Navigate to mastering_ros_demo_pkg/src, and find nodes with the names
demo_service_server.cpp and demo_service_client.cpp
#include "ros/ros.h"
#include "mastering_ros_demo_pkg/demo_srv.h"
#include <iostream>
#include <sstream>
using namespace std;
#include "ros/ros.h"
#include <iostream>
#include "mastering_ros_demo_pkg/demo_srv.h"
#include <iostream>
#include <sstream>
using namespace std;
}
else
{
ROS_ERROR("Failed to call service");
return 1;
}
ros::spinOnce();
loop_rate.sleep();
}
return 0;
}
Working with ROS actionlib
In ROS services, the user implements a request/reply interaction between two nodes,
but if the reply takes too much time or the server is not finished with the given work,
we have to wait until it completes, blocking the main application while waiting for the
termination of the requested action. In addition, the calling client could be
implemented to monitor the execution of the remote process. In these cases, we should
implement our application using actionlib. This is another method in ROS in which we
can preempt the running request and start sending another one if the request is not
finished on time as we expected. Actionlib packages provide a standard way to
implement these kinds of preemptive tasks. Actionlib is highly used in robot arm
navigation and mobile robot navigation. We can see how to implement an action server
and action client implementation.
There is another method in ROS in which we can preempt the running request and start
sending another one if the request is not finished on time as we expected. Actionlib
packages provide a standard way to implement these kinds of preemptive tasks.
Actionlib is highly used in robot arm navigation and mobile robot navigation. We can
see how to implement an action server and action client implementation.
Like ROS services, in actionlib, we have to specify the action specification. The action
specification is stored inside the action file having an extension of .action. This file
must be kept inside the action folder, which is inside the ROS package. The action file
has the following parts:
Goal: The action client can send a goal that has to be executed by the action server.
This is similar to the request in the ROS service. For example, if a robot arm joint
wants to move from 45 degrees to 90 degrees, the goal here is 90 degrees.
Feedback: When an action client sends a goal to the action server, it will start
executing a callback function. Feedback is simply giving the progress of the
current operation inside the callback function. Using the feedback definition, we
can get the current progress. In the preceding case, the robot arm joint has to move
to 90 degrees; in this case, the feedback can be the intermediate value between 45
and 90 degrees in which the arm is moving.
Result: After completing the goal, the action server will send a final result of
completion, it can be the computational result or an acknowledgment. In the
preceding example, if the joint reaches 90 degrees it achieves the goal and the
result can be anything indicating it finished the goal.
We can discuss a demo action server and action client here. The demo action client
will send a number as the goal. When an action server receives the goal, it will
count from 0 to the goal number with a step size of 1 and with a 1 second delay. If
it completes before the given time, it will send the result; otherwise, the task will
be preempted by the client. The feedback here is the progress of counting. The
action file of this task is as follows. The action file is named Demo_action.action:
#goal definition
int32 count
--
#result definition
int32 final_count
--
#feedback
int32 current_number
Here, the count value is the goal in which the server has to count from zero to this
number. final_count is the result, in which the final value after completion of a task
and current_number is the feedback value. It will specify how much the progress
is.
Navigate to mastering_ros_demo_pkg/src and you can find the action server node
as demo_action_server.cpp and action client node as demo_action_client.cpp.
Creating the ROS action server
In this section, we will discuss demo_action_server.cpp. The action server receives
a goal value that is a number. When the server gets this goal value, it will start
counting from zero to this number. If the counting is complete, it will successfully
finish the action, if it is preempted before finishing, the action server will look for
another goal value.
This code is a bit lengthy, so we can discuss the important code snippet of this
code.
#include <actionlib/server/simple_action_server.h>
#include "mastering_ros_demo_pkg/Demo_actionAction.h"
The first header is the standard action library to implement an action server node.
The second header is generated from the stored action files. It should include
accessing our action definition:
class Demo_actionAction
{
Create a simple action server instance with our custom action message type:
mastering_ros_demo_pkg::Demo_actionFeedback feedback;
mastering_ros_demo_pkg::Demo_actionResult result;
Demo_actionAction(std::string name) :
as(nh_, name, boost::bind(&Demo_actionAction::executeCB, this, _1), false),
action_name(name)
This is an action constructor, and an action server is created here by taking an
argument such as Nodehandle, action_name, and executeCB, where executeCB is the
action callback where all the processing is done:
as.registerPreemptCallback(boost::bind(&Demo_actionAction::preemptCB, this));
This line registers a callback when the action is preempted. The preemtCB is the
callback name executed when there is a preempt request from the action client:
This is the callback definition which is executed when the action server receives a
goal value. It will execute callback functions only after checking whether the
action server is currently active or it is preempted already:
This loop will execute until the goal value is reached. It will continuously send the
current progress as feedback:
if(!as.isActive() || as.isPreemptRequested()){
return;
}
Inside this loop, it will check whether the action server is active or it is preempted.
If it occurs, the function will return:
if(goal->count == progress){
result.final_count = progress;
as.setSucceeded(result);
}
If the current value reaches the goal value, then it publishes the final result:
Demo_actionAction demo_action_obj(ros::this_node::getName());