Reader small image

You're reading from  Mastering ROS for Robotics Programming, Third edition - Third Edition

Product typeBook
Published inOct 2021
PublisherPackt
ISBN-139781801071024
Edition3rd Edition
Concepts
Right arrow
Authors (2):
Lentin Joseph
Lentin Joseph
author image
Lentin Joseph

Lentin Joseph is an author and robotics entrepreneur from India. He runs a robotics software company called Qbotics Labs in India. He has 7 years of experience in the robotics domain primarily in ROS, OpenCV, and PCL. He has authored four books in ROS, namely, Learning Robotics using Python, Mastering ROS for Robotics Programming, ROS Robotics Projects, and Robot Operating System for Absolute Beginners. He is currently pursuing his master's in Robotics from India and is also doing research at Robotics Institute, CMU, USA.
Read more about Lentin Joseph

Jonathan Cacace
Jonathan Cacace
author image
Jonathan Cacace

Jonathan Cacace was born in Naples, Italy, on December 13, 1987. He received his Master's degree in computer science, and a Ph.D. degree in Information and Automation Engineering, from the University of Naples Federico II. Currently, he is an Assistant Professor at the University of Naples Federico II. He is also a member of PRISMA Lab where he is involved in different research projects focused on industrial and service robotics in which he has developed several ROS-based applications integrating robot perception and control.
Read more about Jonathan Cacace

View More author details
Right arrow

Chapter 2: Getting Started with ROS Programming

After discussing the basics of the ROS master, the parameter server, and roscore, we can now start to create and build a ROS package. In this chapter, we will create different ROS nodes by implementing the ROS communication system. While working with ROS packages, we will also refresh ourselves on the basic concepts of ROS nodes, topics, messages, services, and actionlib.

In this chapter, we will cover the following topics:

  • Creating a ROS package
  • Adding custom message and service files
  • Working with ROS services
  • Creating launch files
  • Applications of topics, services, and actionlib

Technical requirements

To follow this chapter, you will need a standard laptop running Ubuntu 20.04 with ROS Noetic installed. The reference code for this chapter can be downloaded from the following GitHub repository: https://github.com/PacktPublishing/Mastering-ROS-for-Robotics-Programming-Third-edition.git. The necessary code is contained in the Chapter2/mastering_ros_demo_pkg folder.

You can view this chapter's code in action here: https://bit.ly/3iXO5lW.

Creating a ROS package

ROS packages are the basic units of ROS programs. We can create a ROS package, build it, and release it to the public. The current distribution of ROS we are using is Noetic Ninjemys. We are using the catkin build system to build ROS packages. A build system is responsible for generating targets (executable/libraries) from textual source code that can be used by end users. In older distributions, such as Electric and Fuerte, rosbuild was the build system. Because of the various flaws of rosbuild, catkin came into existence. This also allowed us to move the ROS compilation system closer to Cross Platform Make (CMake). This has a lot of advantages, such as porting the package to another OS, such as Windows. If an OS supports CMake and Python, catkin-based packages can be ported to it.

The first requirement for working with ROS packages is to create a ROS catkin workspace. After installing ROS, we can create and build a catkinworkspace called catkin_ws:

mkdir...

Adding custom .msg and .srv files

In this section, we will look at how to create custom messages and service definitions in the current package. The message definitions are stored in a .msg file, while the service definitions are stored in a .srv file. These definitions inform ROS about the type of data and the name of the data to be transmitted from a ROS node. When a custom message is added, ROS will convert the definitions into equivalent C++ codes, which we can include in our nodes.

We will start with message definitions. Message definitions must be written in the .msg file and must be kept in the msg folder, which is inside the package. We are going to create a message file called demo_msg.msg with the following definition:

string greeting 
int32 number 

So far, we have only worked with standard message definitions. Now, we have created our own definitions, which means we can learn how to use them in our code.

The first step is to edit the package.xml file of the current...

Working with ROS services

In this section, we are going to create ROS nodes, which can use the service definition that we defined already. The service nodes we are going to create can send a string message as a request to the server; then, the server node will send another message as a response.

Navigate to mastering_ros_demo_pkg/src and find the demo_service_server.cpp and demo_service_client.cpp nodes.

demo_service_server.cpp is the server, and its definition is as follows:

#include "ros/ros.h" 
#include "mastering_ros_demo_pkg/demo_srv.h" 
#include <iostream> 
#include <sstream> 
using namespace std; 
 
bool demo_service_callback(mastering_ros_demo_pkg::demo_srv::Request &req, 
     mastering_ros_demo_pkg::demo_srv::Response &res) { 
    ss << "Received Here"; 
    ROS_INFO("From Client [%s], Server says [%s]",req.in.c_str(),res.out.c_str()...

Creating launch files

The launch files in ROS are very useful for launching more than one node. In the preceding examples, we saw a maximum of two ROS nodes, but imagine a scenario in which we have to launch 10 or 20 nodes for a robot. It would be difficult if we had to run each node in a terminal one by one. Instead, we can write all the nodes inside an XML-based file called a launch file and, using a command called roslaunch, we parse this file and launch the nodes.

The roslaunch command will automatically start the ROS master and the parameter server. So, in essence, there is no need to start the roscore command and any individual nodes; if we launch the file, all operations will be done in a single command. Note that if you start a node using the roslaunch command, terminating or restarting this command will have the same effect as restarting roscore.

Let's start by creating the launch files. Switch to the package folder and create a new launch file called demo_topic...

Applications of topics, services, and actionlib

Topics, services, and actionlib are used in different scenarios. We know topics are a unidirectional communication method, services are a bidirectional request/reply kind of communication, and that actionlib is a modified form of ROS service in which we can cancel the process that's running on the server as required.

Here are some of the areas where we use these methods:

  • Topics: Streaming continuous data flows, such as sensor data; for example, we can stream joypad data to teleoperate a robot, publish robot odometry, and publish a video stream from a camera.
  • Services: Executing procedures that terminate quickly; for example, to save the calibration parameters of sensors, to save a map that's been generated by the robot during its navigation, or to load a parameter file.
  • actionlib: Executing long and complex actions while managing their feedback; for example, to navigate toward a target or plan a motion path...

Summary

In this chapter, we provided different examples of ROS nodes in which ROS features such as ROS topics, services, and actions were implemented. Such tools are used in every ROS package, both the one already available in the ROS repository and the one created by you. We also discussed how to create and compile ROS packages using custom and standard messages. Usually, different packages use custom messages to handle data generated by their nodes, so it's important to be able to manage custom messages provided by a package.

In the next chapter, we will discuss ROS robot modeling using URDF and xacro, and we will design some robot models.

Here are a few questions based on what we covered in this chapter.

Questions

  • Which kinds of communication protocols between nodes are supported by ROS?
  • What is the difference between the rosrun and roslaunch commands?
  • How do ROS topics and services differ in their operations?
  • How do ROS services and actionlib differ in their operations?
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering ROS for Robotics Programming, Third edition - Third Edition
Published in: Oct 2021Publisher: PacktISBN-13: 9781801071024
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Lentin Joseph

Lentin Joseph is an author and robotics entrepreneur from India. He runs a robotics software company called Qbotics Labs in India. He has 7 years of experience in the robotics domain primarily in ROS, OpenCV, and PCL. He has authored four books in ROS, namely, Learning Robotics using Python, Mastering ROS for Robotics Programming, ROS Robotics Projects, and Robot Operating System for Absolute Beginners. He is currently pursuing his master's in Robotics from India and is also doing research at Robotics Institute, CMU, USA.
Read more about Lentin Joseph

author image
Jonathan Cacace

Jonathan Cacace was born in Naples, Italy, on December 13, 1987. He received his Master's degree in computer science, and a Ph.D. degree in Information and Automation Engineering, from the University of Naples Federico II. Currently, he is an Assistant Professor at the University of Naples Federico II. He is also a member of PRISMA Lab where he is involved in different research projects focused on industrial and service robotics in which he has developed several ROS-based applications integrating robot perception and control.
Read more about Jonathan Cacace