Now let’s suppose we want the robot to do something specific. Something that’s synchronous and guaranteed. That’s a service call.
Similar to the other ROS commands we’ve used, there’s a ros2 service list
command to show which services are available. The following services are available for the turtlesim:
/clear
/kill
/reset
/spawn
/turtle1/set_pen
/turtle1/teleport_absolute
/turtle1/teleport_relative
/turtlesim/describe_parameters
/turtlesim/get_parameter_types
/turtlesim/get_parameters
/turtlesim/list_parameters
/turtlesim/set_parameters
/turtlesim/set_parameters_atomically
The turtlebot service list is much longer and most service are parameter-related services. In ROS 2, parameters are implemented through services and lengthen the list:
/camera_driver/describe_parameters
/camera_driver/get_parameter_types
...
/gazebo/set_parameters
/gazebo/set_parameters_atomically
/pause_physics
/reset_simulation
/reset_world
/robot_state_publisher/describe_parameters
...
/turtlebot3_laserscan/set_parameters
/turtlebot3_laserscan/set_parameters_atomically
/unpause_physics
Calling a service in ROS 2 requires three arguments: the service, the service type and the message data to send to the service. After retrieving the list of service names, the ros2 service type [service]
command shows the service type. By examining the turtlesim \spawn
that’s used to add a new turtle to the simulator, we find the service type is turtlesim/srv/Spawn
.
The message data is defined by the service interface. Use the command
ros2 interface show turtlesim/srv/Spawn
to display the message format required to make the service call:
float32 x
float32 y
float32 theta
string name # Optional. A unique name will be created and returned if this is empty
---
string name
This output follows the ROS .srv
file format. This format allows for comments using the #
sign, and sections are delimited with three hyphens, ---
. The first section is the format used to call the service, and the second section shows the message type returned after the service has been called. Calling the Spawn service requires two floats for the turtle position, the orientation of the turtle in radians, and an optional name for the new turtle; it returns the name of the new turtle.
Call the service by specifying the service name, service type, and the message data in yaml format:
ros2 service call /spawn turtlesim/srv/Spawn "{x: 2, y: 2, theta: 0.0, name: 'tortise'}"
The service returns the name of the turtle that was added to the playfield.
Explore the /kill
service within turtlesim to remove the newly created turtle!