Working With ROS nodelets in ROS Noetic

In this section, we’ll learn how to work with ROS nodelets.

What is a ROS nodelet?

A node in ROS is a program that runs a specific process. These nodes communicate back and forth with each other on the ROS network via special ROS constructs, known as topics. The disadvantage of this framework is that message passing can get pretty slow if there is a large volume of data that needs to be passed (e.g. data from 3D sensor). 

Fortunately, ROS has something called a nodelet. A nodelet is a special type of ROS node that enables you to run multiple nodes in a single process on a single computer. These nodes are actually separate threads that can communicate with each other directly without going through the traditional ROS communication network middleman. 

The cool thing about nodelets is that they can load as plugins (see the last section of this tutorial), which means that you can add them to your application or remove them from your application without having to make major modifications to your main source code.

The disadvantage of a nodelet is that if a nodelet goes down, all the running processes within it go down as well. Also, the nodelet concept is only implemented in C++, so you can’t use Python.

Bottom Line: If you need a quick and efficient way to pass large amounts of data from one node to another, consider using a nodelet. Otherwise, always use nodes because they are more flexible.

With that background, let’s take a look now at how to create a ROS nodelet. I will loosely follow the official tutorial on the ROS website.

How to Create a ROS Nodelet

The nodelet that we will create will subscribe to a topic (/ros_in). It will then receive a message via that topic and then republish that message to another topic (/ros_out).

Open a new terminal window.

Type the following command:

cd ~/catkin_ws/src

Create a new package called nodelet_basics

catkin_create_pkg nodelet_basics nodelet roscpp std_msgs

The nodelet package we included in the command above provides the tools to build a nodelet.

Move to the src folder of the package you just created.

cd nodelet_basics/src

Create a new file. This file will have the code for the nodelet.

gedit hello_world.cpp

Type the following lines of code:

/*
Author: Addison Sears-Collins

This ROS nodelet will subscribe to a topic (/ros_in). 
It will then receive a message via that topic and then 
republish that message to another topic (/ros_out). 

Date: 6/19/2020

ROS Version: ROS Noetic Ninjemys
*/

// Add the necessary includes
#include <ros/ros.h>
#include <std_msgs/String.h>
#include <nodelet/nodelet.h>
#include <pluginlib/class_list_macros.h>
#include <stdio.h>


namespace nodelet_basics
{
	/*********
	* Nodelet
	**********/

	// This code here means the class needs to inherit from 
	// nodelet::Nodelet
	class Hello_World : public nodelet::Nodelet
	{
	
		public:
			Hello_World()
			{
			}

		private:
			// The onInit method is called by nodelet manager. 
			// It is responsible for initializing the nodelet.
			virtual void onInit()
			{
				// Create a NodeHandle object
				ros::NodeHandle& private_nh = getPrivateNodeHandle();
				NODELET_DEBUG("Initializing nodelet...");
			
				// Create a publisher topic
				pub = private_nh.advertise<std_msgs::String>("ros_out",10); 
			
				// Create a subscriber topic
				sub = private_nh.subscribe("ros_in",10, &Hello_World::callback, this);	
			}

			ros::Publisher pub;
			ros::Subscriber sub;
	
			// Display messages from /ros_in topic to the terminal window.
			// Publish to /ros_out topic
			void callback(const std_msgs::String::ConstPtr& input)
			{

				std_msgs::String output;
				output.data = input->data;
				NODELET_DEBUG("msg data = %s",output.data.c_str());
				ROS_INFO("msg data = %s",output.data.c_str());
				pub.publish(output);		
			}
	};
	// Export the Hello_World class as a plugin using the
	// PLUGINLIB_EXPORT_CLASS macro.
	PLUGINLIB_EXPORT_CLASS(nodelet_basics::Hello_World, nodelet::Nodelet);
}

Save and close the editor.

Now, let’s create an xml file that describes the plugin.

roscd nodelet_basics
gedit nodelet_plugins.xml

Add these lines. 

<library path="lib/libnodelet_basics">
  <class name="nodelet_basics/Hello_World" 
         type="nodelet_basics::Hello_World" 
		 base_class_type="nodelet::Nodelet">
	<description>
		This nodelet receives a message and publishes it.
	</description>
  </class>
</library>

This nodelet receives a message and publishes it.

Save and close the editor.

Open the package.xml file.

gedit package.xml

Make sure you add this line between the <export></export> tags.

<nodelet plugin="${prefix}/nodelet_plugins.xml" />

Save and close the file.

Now let’s edit CMakeLists.txt.

gedit CMakeLists.txt

Add these lines to the bottom of the CMakeLists.txt file.

## Declare C++ library
add_library(nodelet_basics src/hello_world.cpp)

## Link libraries
target_link_libraries(nodelet_basics ${catkin_LIBRARIES}) 

Click Save and close the editor.

Build the package.

cd ~/catkin_ws/
catkin_make

If the build is successful, you’ll see a shared object named libnodelet_basics.co. This file is the plugin.

26-thats-our-pluginJPG

Now let’s create a launch file.

roscd nodelet_basics
mkdir launch
cd launch
gedit hello_world_launch.launch
<launch>
  <node pkg="nodelet" 
        type="nodelet" 
        name="standalone_nodelet"  
        args="manager" 
        output="screen"
  />

  <node pkg="nodelet" 
        type="nodelet" 
        name="Hello_World" 
        args="load nodelet_basics/Hello_World standalone_nodelet" 
        output="screen"
  />
</launch>

Save and close the editor.

Now, launch the nodelet. Remember when you use the roslaunch command, you don’t have to launch the ROS master using roscore.

roslaunch nodelet_basics hello_world_launch.launch

Here is the output.

27-ran-launch-fileJPG

In another terminal, check out the list of active nodes.

rosnode list
28-rosnode-listJPG

In another terminal, check out the list of topics.

rostopic list
29-rostopic-listJPG

Let’s publish a string message to the /ros_in topic. In a new terminal, type the following

rostopic echo /Hello_World/ros_out

This code above is now ready for you to publish your string. It will republish the string it receives. Let’s type the string now.

rostopic pub /Hello_World/ros_in std_msgs/String "Hi Automatic Addison"

Here is what you should see on the /Hello_World/ros_in topic terminal window.

30-publish-messageJPG

Here is what you should see on the /Hello_World/ros_out terminal window.

31-rosout-republish-messageJPG

Let’s check out the node graph.

rqt_graph
32-node-graphJPG

That’s it! You now know how to create a nodelet. 

Working With ROS pluginlib in ROS Noetic

In this tutorial, we’ll learn how to use ROS pluginlib.

What is ROS pluginlib?

Over the course of your work with ROS, there may be some situations where you want an application to have added functionality. However, you want that functionality to remain independent from the main application so that you can “plug it in” when you need it and unplug it when you don’t need it.

In short, the job of a plugin is to add some specific feature to an existing program.

The ROS pluginlib is a C++ library that enables you to load and unload plugins from within a package in ROS, giving your robot added capabilities.

How to Create and Load a Plugin

Let’s see how to create and load a plugin in ROS. We will follow the tutorial given on the ROS website.

What we’ll do is 

  • Create a base class for a polygon. This base class will be called RegularPolygon, and it will serve as the template for the plugins we’ll create.
  • Use that base class to create two new classes, Triangle and Square. These two classes inherit the properties of the RegularPolygon class.
  • Use pluginlib to register the Triangle and Square classes as plugins. 
  • Build the Plugin library using CMakeLists.txt.
  • Make the plugins available to ROS using a package.xml file.
  • Create a C++ program to actually use the plugins (Triangle and Square).
  • Run the code.

So those are all the steps. Let’s go through each step-by-step.

Open up a new terminal window, and create a new package.

cd ~/catkin_ws/src/
catkin_create_pkg pluginlib_tutorials_ roscpp pluginlib

Move into the pluginlib_tutorials_ package.

cd  pluginlib_tutorials_ 

Move into the include/pluginlib_tutorials_ folder.

Move into the directory.

cd include/pluginlib_tutorials_ 

Let’s create the RegularPolygon class.

gedit polygon_base.h

Copy and paste the following code into it.

#ifndef PLUGINLIB_TUTORIALS__POLYGON_BASE_H_
#define PLUGINLIB_TUTORIALS__POLYGON_BASE_H_

namespace polygon_base
{
  class RegularPolygon
  {
    public:
      virtual void initialize(double side_length) = 0;
      virtual double area() = 0;
      virtual ~RegularPolygon(){}

    protected:
      RegularPolygon(){}
  };
};
#endif

Save and close the editor.

Now let’s create our two plugins, Triangle and Square.

Open a new file, and copy and paste the code into it.

gedit polygon_plugins.h
#ifndef PLUGINLIB_TUTORIALS__POLYGON_PLUGINS_H_
#define PLUGINLIB_TUTORIALS__POLYGON_PLUGINS_H_
#include <pluginlib_tutorials_/polygon_base.h>
#include <cmath>

namespace polygon_plugins
{
  class Triangle : public polygon_base::RegularPolygon
  {
    public:
      Triangle(){}

      void initialize(double side_length)
      {
        side_length_ = side_length;
      }

      double area()
      {
        return 0.5 * side_length_ * getHeight();
      }

      double getHeight()
      {
        return sqrt((side_length_ * side_length_) - ((side_length_ / 2) * (side_length_ / 2)));
      }

    private:
      double side_length_;
  };

  class Square : public polygon_base::RegularPolygon
  {
    public:
      Square(){}

      void initialize(double side_length)
      {
        side_length_ = side_length;
      }

      double area()
      {
        return side_length_ * side_length_;
      }

    private:
      double side_length_;

  };
};
#endif

Click Save and close the editor.

Now let’s register the plugins we just created.  

Go to the src folder of the package.

roscd pluginlib_tutorials_/src

Create a new file named polygon_plugins.cpp.

gedit polygon_plugins.cpp

Copy and paste the following code into it.

#include <pluginlib/class_list_macros.h>
#include <pluginlib_tutorials_/polygon_base.h>
#include <pluginlib_tutorials_/polygon_plugins.h>

PLUGINLIB_EXPORT_CLASS(polygon_plugins::Triangle, polygon_base::RegularPolygon)
PLUGINLIB_EXPORT_CLASS(polygon_plugins::Square, polygon_base::RegularPolygon)

Save and close the editor.

Now let’s build the plugin library.

Go to your CMakeLists.txt file.

roscd pluginlib_tutorials_
gedit CMakeLists.txt

Add these two lines.

include_directories(include)
add_library(polygon_plugins src/polygon_plugins.cpp)

Save and close the editor.

Now compile the package.

cd ~/catkin_ws/
catkin_make

Let’s make the plugins available to ROS.

Go to your package.

roscd pluginlib_tutorials_

Create a new file.

gedit polygon_plugins.xml

Copy and paste the following code inside it.

<library path="lib/libpolygon_plugins">
  <class type="polygon_plugins::Triangle" base_class_type="polygon_base::RegularPolygon">
    <description>This is a triangle plugin.</description>
  </class>
  <class type="polygon_plugins::Square" base_class_type="polygon_base::RegularPolygon">
    <description>This is a square plugin.</description>
  </class>
</library>

Save and close the editor.

Go to the package.xml file.

gedit package.xml

Add this line between the <export> </export> tags at the bottom of the package.xml file.

  <pluginlib_tutorials_ plugin="${prefix}/polygon_plugins.xml" />

Save and close the editor.

Let’s check that everything is working properly. Type this command in your terminal window.

rospack plugins --attrib=plugin pluginlib_tutorials_

Here is what your output should look like:

24-here-is-what-output-should-look-likeJPG

Now, let’s create a C++ program that uses the plugin.

Go to the src file.

cd src

Open a new file.

gedit polygon_loader.cpp

Copy and paste the following code inside it.

#include <pluginlib/class_loader.h>
#include <pluginlib_tutorials_/polygon_base.h>

int main(int argc, char** argv)
{
  pluginlib::ClassLoader<polygon_base::RegularPolygon> poly_loader("pluginlib_tutorials_", "polygon_base::RegularPolygon");

  try
  {
    boost::shared_ptr<polygon_base::RegularPolygon> triangle = poly_loader.createInstance("polygon_plugins::Triangle");
    triangle->initialize(10.0);

    boost::shared_ptr<polygon_base::RegularPolygon> square = poly_loader.createInstance("polygon_plugins::Square");
    square->initialize(10.0);

    ROS_INFO("Triangle area: %.2f", triangle->area());
    ROS_INFO("Square area: %.2f", square->area());
  }
  catch(pluginlib::PluginlibException& ex)
  {
    ROS_ERROR("The plugin failed to load for some reason. Error: %s", ex.what());
  }

  return 0;
}

Save and close the editor.

Now go to the CMakeLists.txt file.

cd ..
gedit CMakeLists.txt

Add these two lines to the bottom of the file.

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

Save and close.

Now we need to compile the code.

cd /~catkin_ws/
catkin_make

Let’s run the executable.

In a new terminal window, type:

roscore

In a new terminal tab, type:

rosrun pluginlib_tutorials_ polygon_loader

Here is the output:

25-here-is-the-outputJPG

Working With ROS actionlib in ROS Noetic

What is a ROS Action?

ROS services are all about request/reply communication. A Service Client node sends a request (typically for some data) to a Service Server node. The Service Server node then replies to that request.

However, there may be some situations when the Service Client wants to cancel a request. This situation could occur when it is taking too long to receive a reply from the Service Server.

There are also some situations when the Service Client wants to get regular updates on the processing status of the request. 

Fortunately, ROS has a solution for this. It is called the ROS’s actionlib package. The ROS actionlib package enables cancellation of requests and periodic feedback on the status of requests.

What is the Difference Between a ROS Service and a ROS Action?

Here is an analogy to understand the difference between ROS Services and ROS Actions.

Let’s say you’re hungry. You order Chinese food from your local Chinese restaurant. Here are two ways communication might work during the ordering process between you (the client) and the restaurant (the server).

chinese_takeout_t_png

Option 1: Go to the Chinese Restaurant to Order Takeout

  1. Place your order.
  2. Wait for your order.
  3. Receive your order.

Option 2: Order Chinese Food via an App on Your Smartphone (for Delivery)

  1. Place your order.
  2. Receive an order confirmation.
  3. Consider cancelling your order.
  4. Receive periodic updates on the status of your order.
  5. Do some household tasks while you wait for your order.
  6. Chinese food is delivered.

A ROS Service is like Option 1. It is a synchronous form of communication. After the client sends a request to a server, the client has to wait for a response from the server in order to continue doing other things. 

Synchronous communication is also like talking on the telephone. When you’re talking on the telephone, you’re not free to do other things. You have to wait for the call to end to resume doing other things.

A ROS Action is like Option 2. It is an asynchronous form of communication. The client is free to do other things while it is waiting for a response from the server.

Asynchronous communication is like communicating via text message or e-mail. You send a request and then wait for a reply. In the meantime, you can do other things besides waiting for a response.

Here’s how ROS actions work. An ActionClient sends a request (i.e. a Goal) to an ActionServer. The goal might be, for example, for the robot to move to a particular location in the environment.

The ActionServer gives the ActionClient feedback on the progress it is making towards the execution of that goal (e.g. the robot’s most recent position along the path towards the goal location).

When the ActionServer completes the goal, it sends a result message to the ActionClient.

How to Create an ActionServer

Let’s create an ActionServer. The ActionServer’s job is to generate a Fibonacci sequence of a certain order (i.e. length). Don’t be scared if you’ve never heard of Fibonacci before.

A Fibonacci sequence is just a series of numbers in which the next number is found by adding up the two numbers before it, starting from 0 and 1. For example, here is a Fibonacci sequence:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

Notice that after 0 and 1, each number is the sum of the two numbers before it.

1 + 2 = 3 … 2 + 3 = 5, 3 + 5 = 8, etc.

A Fibonacci sequence of order 5, means the first 5 numbers of the sequence after the two seed numbers, which are 0 and 1:

0, 1, 1, 2, 3, 5, 8

In the next section of this tutorial, we will create an ActionClient. The ActionClient must send a goal to the ActionServer. The goal in this example will be the ActionClient’s desired order of the Fibonacci sequence (e.g. 5). 

The ActionServer will take that order number as input (e.g. 5) and then reply to the ActionClient with the result (e.g. 0, 1, 1, 2, 3, 5, 8). 

While the ActionServer is busy generating the requested Fibonacci sequence, it will send feedback to the ActionClient on its progress towards completing the goal.

Open up a new terminal window.

Move to the src folder of your workspace.

cd ~/catkin_ws/src

Create a package named actionlib_basics. Everything below is a single command that you type on one line in the terminal. 

catkin_create_pkg actionlib_basics actionlib message_generation roscpp rospy std_msgs actionlib_msgs

Now, let’s create an action. In this action, we’ll define the goal, result, and feedback messages. Create a new folder named action inside the actionlib_basics package.

cd actionlib_basics
mkdir action
cd action
gedit Fibonacci.action

Type the following code inside the file.

#goal definition
int32 order
---
#result definition
int32[] sequence
---
#feedback
int32[] sequence

Click Save.

Close the editor.

Open the CMakeLists.txt file.

cd ..
gedit CMakeLists.txt

Make sure your find_package macro looks like this.

7b-find-package-macro

Uncomment and modify the add_action_files macro:

add_action_files(
  DIRECTORY action
  FILES Fibonacci.action
)
8-add-action-filesJPG

Uncomment and modify the generate_messages macro:

generate_messages(
  DEPENDENCIES actionlib_msgs std_msgs  # Or other packages containing msgs
)
8b-generate-messages-macroJPG

Make the catkin_package macro look like this. If you scroll down, you’ll see it further down in your CMakeLists.txt file.

catkin_package(
  CATKIN_DEPENDS actionlib_msgs
)
9-uncomment-catkin-packageJPG

Click Save and close CMakeLists.txt.

Now, open your “manifest file.” This file is also known as package.xml.

gedit package.xml

Under where you see all the <exec_depend> declarations, add the following line:

<exec_depend>message_generation</exec_depend>
10-exec-dependJPG

Click Save. 

Close package.xml.

Now, let’s generate the msg files out of the action files. Type the following commands in your terminal.

cd ~/catkin_ws/
catkin_make

Now let’s see all the messages that were created.

ls devel/share/actionlib_basics/msg/

You should see 7 files in there.

11-seven-files-in-thereJPG

Let’s take a look at the header files.

ls devel/include/actionlib_basics/
12-header-filesJPG

Now that we’ve set up the message types, let’s create a program for the ActionServer.

Move to the src folder of the package.

roscd actionlib_basics/src

Create a new C++ program called fibonacci_server.cpp.

Type the following code inside it.

#include <ros/ros.h>
#include <actionlib/server/simple_action_server.h>
#include <actionlib_basics/FibonacciAction.h>

class FibonacciAction
{
protected:

  ros::NodeHandle nh_;
  actionlib::SimpleActionServer<actionlib_basics::FibonacciAction> as_; // NodeHandle instance must be created before this line. Otherwise strange error occurs.
  std::string action_name_;
  // create messages that are used to published feedback/result
  actionlib_basics::FibonacciFeedback feedback_;
  actionlib_basics::FibonacciResult result_;

public:

  FibonacciAction(std::string name) :
    as_(nh_, name, boost::bind(&FibonacciAction::executeCB, this, _1), false),
    action_name_(name)
  {
    as_.start();
  }

  ~FibonacciAction(void)
  {
  }

  void executeCB(const actionlib_basics::FibonacciGoalConstPtr &goal)
  {
    // helper variables
    ros::Rate r(1);
    bool success = true;

    // push_back the seeds for the fibonacci sequence
    feedback_.sequence.clear();
    feedback_.sequence.push_back(0);
    feedback_.sequence.push_back(1);

    // publish info to the console for the user
    ROS_INFO("%s: Executing, creating fibonacci sequence of order %i with seeds %i, %i", action_name_.c_str(), goal->order, feedback_.sequence[0], feedback_.sequence[1]);

    // start executing the action
    for(int i=1; i<=goal->order; i++)
    {
      // check that preempt has not been requested by the client
      if (as_.isPreemptRequested() || !ros::ok())
      {
        ROS_INFO("%s: Preempted", action_name_.c_str());
        // set the action state to preempted
        as_.setPreempted();
        success = false;
        break;
      }
      feedback_.sequence.push_back(feedback_.sequence[i] + feedback_.sequence[i-1]);
      // publish the feedback
      as_.publishFeedback(feedback_);
      // this sleep is not necessary, the sequence is computed at 1 Hz for demonstration purposes
      r.sleep();
    }

    if(success)
    {
      result_.sequence = feedback_.sequence;
      ROS_INFO("%s: Succeeded", action_name_.c_str());
      // set the action state to succeeded
      as_.setSucceeded(result_);
    }
  }


};


int main(int argc, char** argv)
{
  ros::init(argc, argv, "fibonacci");

  FibonacciAction fibonacci("fibonacci");
  ros::spin();

  return 0;
}

You can find a full explanation of what is going on inside the code at this page on the ROS website.

Click Save.

Close the editor.

Now let’s compile the code.

In the same terminal window, go back to the actionlib_basics package.

cd ..

Open CMakeLists.txt.

gedit CMakeLists.txt

Add these lines to the bottom of your CMakeLists.txt file.

add_executable(fibonacci_server src/fibonacci_server.cpp)

target_link_libraries(
  fibonacci_server
  ${catkin_LIBRARIES}
)

add_dependencies(
  fibonacci_server
  ${actionlib_basics_EXPORTED_TARGETS}
)

Click Save.

Close the file.

Now, let’s build everything again using the usual process.

cd ~/catkin_ws/
catkin_make

If it builds successfully, you should see this message in the terminal window.

13-success-fibonacciJPG

Now let’s run the server.

In a fresh terminal window, type the following:

roscore

Open a new terminal tab, and type this command:

rosrun actionlib_basics fibonacci_server

You shouldn’t see any output. The ActionServer (i.e. fibonacci_server) is patiently waiting to receive a goal to execute.

Let’s check to see if the actions are running properly by listing the current topics.

rostopic list -v
14-rostopic-listJPG

Press CTRL+C on all terminal windows to shutdown ROS.

That’s it. Now let’s create an ActionClient that can send the ActionServer (that we just created) some goals.

How to Create an ActionClient

Here is the official tutorial for creating an ActionClient, but I’ll run through the process below.

Move to the src folder of the package.

roscd actionlib_basics/src

Create a new C++ program called fibonacci_client.cpp.

Type the following code inside it.

#include <ros/ros.h>
#include <actionlib/client/simple_action_client.h>
#include <actionlib/client/terminal_state.h>
#include <actionlib_basics/FibonacciAction.h>

int main (int argc, char **argv)
{
  ros::init(argc, argv, "test_fibonacci");

  // create the action client
  // true causes the client to spin its own thread
  actionlib::SimpleActionClient<actionlib_basics::FibonacciAction> ac("fibonacci", true);

  ROS_INFO("Waiting for action server to start.");
  // wait for the action server to start
  ac.waitForServer(); //will wait for infinite time

  ROS_INFO("Action server started, sending goal.");
  // send a goal to the action
  actionlib_basics::FibonacciGoal goal;
  goal.order = 20;
  ac.sendGoal(goal);

  //wait for the action to return
  bool finished_before_timeout = ac.waitForResult(ros::Duration(30.0));

  if (finished_before_timeout)
  {
    actionlib::SimpleClientGoalState state = ac.getState();
    ROS_INFO("Action finished: %s",state.toString().c_str());
  }
  else
    ROS_INFO("Action did not finish before the time out.");

  //exit
  return 0;
}

You can find a full explanation of what is going on inside the code at this page on the ROS website.

Click Save.

Close the editor.

Now let’s compile the code.

In the same terminal window, go back to the actionlib_basics package.

cd ..

Open CMakeLists.txt.

gedit CMakeLists.txt

Add these lines to the bottom of your CMakeLists.txt file.

add_executable(fibonacci_client src/fibonacci_client.cpp)

target_link_libraries( 
  fibonacci_client
  ${catkin_LIBRARIES}
)

add_dependencies(
  fibonacci_client
  ${actionlib_basics_EXPORTED_TARGETS}
)

Click Save.

Close the file.

Now, let’s build everything again using the usual process.

cd ~/catkin_ws/
catkin_make

If it builds successfully, you should see this message in the terminal window.

15-fibonacci-client-successful-buildJPG

Now let’s run the client.

In a fresh terminal window, type the following:

roscore

Open a new terminal tab, and type this command:

rosrun actionlib_basics fibonacci_client

You should see a message that says “Waiting for action server to start.” Going back to our Chinese restaurant analogy, the customer is waiting for the restaurant to open.

16-waiting-for-actionserverJPG

Let’s check to see the current topics. In a new terminal window, type:

rostopic list -v
17-current-topicsJPG

Press CTRL+C on all terminal windows to shutdown ROS.

That’s it. Now let’s see how to run both the ActionClient and ActionServer at the same time.

How to Run an ActionClient and ActionServer

Here is the official tutorial on how to run the ActionClient and ActionServer, but let’s run through this together.

In a new terminal window, launch ROS.

roscore

In a new terminal tab, type:

rosrun actionlib_basics fibonacci_server
18-fibonacci-serverJPG

In another terminal tab, type:

rosrun actionlib_basics fibonacci_client
19-fibonacci-client-outputJPG

Let’s see the feedback from the ActionServer while it is in the middle of executing the goal. In a new tab, type:

rostopic echo /fibonacci/feedback
20-feedback-outputJPG

Let’s check out the action result. In a new terminal tab, type:

rostopic echo /fibonacci/result
20a-result-outputJPG

To see the communication lines between the ActionServer and ActionClient, in a new terminal tab, type:

rqt_graph
21-node-graphJPG

If you didn’t get the same output, try again, typing in the commands above quickly. It doesn’t take long (~10 seconds on my computer) for the ActionServer to execute the goal in this case (i.e. creating a Fibonacci sequence of order 20).

In a real robotics project, you won’t need to type in all the commands so fast because your robot will most likely be operating for longer than 10 seconds.

When you’re done, type CTRL+C on the window where you launched the ROS Master (i.e. roscore).

How to Create an ActionClient in Python

For the last part of this tutorial on ROS Actions, let’s look at how to create an ActionClient in Python.

Go to your package.

roscd actionlib_basics

Create a new folder called scripts.

mkdir scripts

Move inside that folder

cd scripts

Create a new Python program called fibonacci_client.py.

gedit fibonacci_client.py

Type the following code inside it.

#! /usr/bin/env python3
from __future__ import print_function
import rospy


# Brings in the SimpleActionClient
import actionlib

# Brings in the messages used by the fibonacci action, including the
# goal message and the result message.
import actionlib_basics.msg

def fibonacci_client():
    # Creates the SimpleActionClient, passing the type of the action
    # (FibonacciAction) to the constructor.
    client = actionlib.SimpleActionClient('fibonacci', actionlib_basics.msg.FibonacciAction)

    # Waits until the action server has started up and started
    # listening for goals.
    client.wait_for_server()

    # Creates a goal to send to the action server.
    goal = actionlib_basics.msg.FibonacciGoal(order=20)

    # Sends the goal to the action server.
    client.send_goal(goal)

    # Waits for the server to finish performing the action.
    client.wait_for_result()

    # Prints out the result of executing the action
    return client.get_result()  # A FibonacciResult

if __name__ == '__main__':
    try:
        # Initializes a rospy node so that the SimpleActionClient can
        # publish and subscribe over ROS.
        rospy.init_node('fibonacci_client_py')
        result = fibonacci_client()
        print("Result:", ', '.join([str(n) for n in result.sequence]))
    except rospy.ROSInterruptException:
        print("program interrupted before completion", file=sys.stderr)

Click Save.

Close the editor.

Make the file executable.

chmod +x fibonacci_client.py

Now type the following commands to build the node:

cd ~/catkin_ws
catkin_make

Now let’s run our ActionServer (created using C++) and our ActionClient (create using Python).

In a new terminal window, launch ROS.

roscore

In a new terminal tab, type:

rosrun actionlib_basics fibonacci_server

In another terminal tab, type:

rosrun actionlib_basics fibonacci_client.py

Let’s see the feedback from the ActionServer while it is in the middle of executing the goal. In a new tab, type:

rostopic echo /fibonacci/feedback

Let’s check out the action result. In a new terminal tab, type:

rostopic echo /fibonacci/result

To see the communication lines between the ActionServer and ActionClient, in a new terminal tab, type:

rqt_graph

Here is the node graph:

22-node-graph-pythonJPG

Here is the output of the /fibonacci/result tab we opened up.

23-result-python-action-clientJPG

To run the client again, go back to the terminal where you ran fibonacci_client.py, and type the up arrow on your keyboard. Then press Enter to run this command again:

rosrun actionlib_basics fibonacci_client.py

When you’re done, press CTRL+C to close down everything.