0% found this document useful (0 votes)
102 views

Ros Lab 3

This document provides an overview of ROS 2 services and how to create custom ROS 2 services. It begins with basics of ROS services including how they allow for bidirectional communication between a server and client in a synchronous manner. Examples of using existing ROS 2 services in turtlesim are demonstrated. The document then explains the steps to create a custom ROS 2 service which includes: [1] defining the service message in an .srv file, [2] modifying CMakeLists.txt, [3] updating package.xml, and [4] creating a Python server node to handle requests.

Uploaded by

Souad Bouziane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

Ros Lab 3

This document provides an overview of ROS 2 services and how to create custom ROS 2 services. It begins with basics of ROS services including how they allow for bidirectional communication between a server and client in a synchronous manner. Examples of using existing ROS 2 services in turtlesim are demonstrated. The document then explains the steps to create a custom ROS 2 service which includes: [1] defining the service message in an .srv file, [2] modifying CMakeLists.txt, [3] updating package.xml, and [4] creating a Python server node to handle requests.

Uploaded by

Souad Bouziane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Robot Operating System 2 (ROS 2)

Lab 3: Understanding ROS 2 Services

Haitham El-Hussieny, PhD


July 30, 2023
Department of Mechatronics and Robotics Engineering
Egypt-Japan University of Science and Technology (E-JUST)
Alexandria, Egypt.
Basics of ROS Services Create Custom ROS 2 Services.

O UTLINE

1. Basics of ROS Services

2. Create Custom ROS 2 Services.

1
Basics of ROS Services
Basics of ROS Services Create Custom ROS 2 Services.

P UBLISHER /S UBSCRIBER C OMMUNICATION

ROS 2 topics are unidirectional and allow communication of 1:N, N:1 or N:N.

2
Basics of ROS Services Create Custom ROS 2 Services.

ROS S ERVICES C OMMUNICATION

ROS services allow for bidirectional communications between a server and client.
Blocked client: The client is waiting for the response from server.
3
Basics of ROS Services Create Custom ROS 2 Services.

ROS S ERVICES

ROS Services consist of:


ROS Server.
ROS Client.

When to use ROS services?


When asking a robot to do a task
and waiting for a feedback.
Examples: A client sends a request and the server responds
Find a path from A to B. with a response (Synchronous communication).
Open the door. We could have many clients, but only one server.
Recognize a cat. 4
Basics of ROS Services Create Custom ROS 2 Services.

ROS S ERVICES

Communication over topics vs. communication over services.

5
Basics of ROS Services Create Custom ROS 2 Services.

ROS S ERVICES WITH T URTLESIM

1. Open new terminal and run


$ ros2 run turtlesim turtlesim node

2. Return all running services


$ ros2 service list

Note: Any service with parameter in its name is out of the scope of this tutorial.
let’s focus on the turtlesim-specific services: /clear, /kill, /reset, /spawn,
/turtle1/set pen, /turtle1/teleport absolute, and
/turtle1/teleport relative 6
Basics of ROS Services Create Custom ROS 2 Services.

ROS S ERVICES WITH T URTLESIM

Get service type


$ ros2 service type /clear
std srvs/srv/Empty
The service requests no data and receives no data when receiving a response.

7
Basics of ROS Services Create Custom ROS 2 Services.

ROS S ERVICES WITH T URTLESIM

Get service type


$ ros2 service type /clear
std srvs/srv/Empty
The service requests no data and receives no data when receiving a response.
Get service type for /spawn
$ ros2 service type /spawn #To create a new turtle

7
Basics of ROS Services Create Custom ROS 2 Services.

ROS S ERVICES WITH T URTLESIM

Get service type


$ ros2 service type /clear
std srvs/srv/Empty
The service requests no data and receives no data when receiving a response.
Get service type for /spawn
$ ros2 service type /spawn #To create a new turtle
To know the structure of the service
$ ros2 interface show turtlesim/srv/Spawn
float32 x x positon of the turtle
float32 y y positon of the turtle
float32 theta orientation of the turtle
string name set name for the turtle
--- The three dashes seperate request from response
7
string name returned name for the turtle
Basics of ROS Services Create Custom ROS 2 Services.

ROS S ERVICES WITH T URTLESIM

Calling a service
ros2 service call <service name> <service type> <arguments>

$ ros2 service call /spawn turtlesim/srv/Spawn "x: 2, y: 2,


theta: 0.2, name: ’Ferial’"

A client asks the /spawn to insert a new


turtle at location (2, 2) and orientation of 0.2
rad with the name ’Ferial’. The server replies
with name of the new turtle.

8
Basics of ROS Services Create Custom ROS 2 Services.

ROS S ERVICES WITH T URTLESIM

Calling services with rqt


Run rqt and add Service Caller plugin.

9
Create Custom ROS 2 Services.
Basics of ROS Services Create Custom ROS 2 Services.

C REATE C USTOM ROS 2 S ERVICES .

To create a client/server ROS


service:
1. Define the service message
(service *.srv file).
2. Create the ROS server node.
3. Create the ROS client node.
4. Execute the service server.
5. Call the server by the client.

10
Basics of ROS Services Create Custom ROS 2 Services.

C REATE C USTOM ROS 2 S ERVICES .

Creat a service to add two integers and return the sum.

11
Basics of ROS Services Create Custom ROS 2 Services.

S TEP 1: D EFINE THE SERVICE MESSAGE ( SERVICE *. SRV FILE ).

Create a new ROS 2 package with cmake build option to define our service file.
$ ros2 pkg create --build-type ament cmake my own srv
note: ROS 2 services cannot be created with pure python packages, i.e.
ament python.
Navigate inside the newly created package and create a new folder named srv
$ mkdir srv
Inside the srv folder create a new text file named AddTwoInts.srv file and add the
following
int64 a
int64 b
---
int64 sum

12
Basics of ROS Services Create Custom ROS 2 Services.

S TEP 2: M ODIFY THE CM AKE L ISTS . TXT FILE

To convert the service you defined into language-specific code, add the following lines to
CMakeLists.txt:

13
Basics of ROS Services Create Custom ROS 2 Services.

S TEP 3: U PDATE THE PACKAGE . XML FILE AND BUILD THE PACKAGE .

Update the Dependencies in package.xml file


<depend>std msgs</depend>
<buildtool depend>rosidl default generators</buildtool depend>
<exec depend>rosidl default runtime</exec depend>
<member of group>rosidl interface packages</member of group>

Build your colcon worksapce


$ colcon build --symlink-install

Test your generated service. The service variables shoud appear.


$ ros2 interface show my own srv/srv/AddTwoInts

14
Basics of ROS Services Create Custom ROS 2 Services.

S TEP 4: M ODIFY THE PACKAGE . XML FILE IN YOUR PACKAGE .

Inside your add two ints srv/package.xml

15
Basics of ROS Services Create Custom ROS 2 Services.

S TEP 5: C REATE A ROS 2 SERVER NODE IN P YTHON .

Create a new ROS 2 package


$ ros2 pkg create
--build-type ament python
add two ints srv
Build your colcon workspace
$ colcon build
--symlink-install

Create a server.py file inside


your package.
Server node server.py

16
Basics of ROS Services Create Custom ROS 2 Services.

S TEP 6: A DD THE EXECUTABLE SERVER . PY NODE IN SETUP. PY

Inside your add two ints srv/setup.py

17
Basics of ROS Services Create Custom ROS 2 Services.

S TEP 7: T EST YOUR SERVER ADD TWO INTS

Run the add two int srv server


$ ros2 run add two ints srv service

List all the running services


$ ros2 service list

Call the service using rqt


$ rqt

18
Basics of ROS Services Create Custom ROS 2 Services.

S TEP 8: C REATE A ROS 2 CLIENT NODE IN P YTHON .

Create a client.py node inside


add two ints srv.
Add the executale client file
into setup.py

Colcon build your workspace.

Client node client.py 19


Basics of ROS Services Create Custom ROS 2 Services.

S TEP 10: R UN THE SERVER AND CLIENT SERVICES .

Run the server:


$ ros2 run add two ints srv service

Run the client:


$ ros2 run add two ints srv client 5 6

20
Basics of ROS Services Create Custom ROS 2 Services.

ACTIVITY

Activity
Develop a ROS 2 service that control a Turtlbot3 inside Gazebo to move to a certain goal
given by the client in (x, y, θ). The server will return the time taken by the robot to reach the
goal.

21
Basics of ROS Services Create Custom ROS 2 Services.

End of Lecture

haitham.elhussieny@ejust.edu.eg

22

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy