Services are another way through which nodes can communicate with each other. Services allow nodes to send a request and receive a response.
The tool that we are going to use to interact with services is called rosservice. The accepted parameters for this command are as follows:
- rosservice args /service: This prints the service arguments
- rosservice call /service: This calls the service with the arguments provided
- rosservice find msg-type: This finds services by their service type
- rosservice info /service: This prints information about the service
- rosservice list: This lists the active services
- rosservice type /service: This prints the service type
- rosservice uri /service: This prints the ROSRPC URI service
We are going to list the services available for the turtlesim node using the following command, so if it is not working, run roscore and run theturtlesim node:
$ rosservice list
You will obtain the following output:
/clear /kill /reset /rosout/get_loggers /rosout/set_logger_level /spawn /teleop_turtle/get_loggers /teleop_turtle/set_logger_level /turtle1/set_pen /turtle1/teleport_absolute /turtle1/teleport_relative /turtlesim/get_loggers /turtlesim/set_logger_level
If you want to see the type of any service, for example, the /clear service, use the following command:
$ rosservice type /clear
You will see something similar to the following output:
std_srvs/Empty
To invoke a service, you will use rosservice call [service] [args]. If you want to invoke the /clear service, use the following command:
$ rosservice call /clear
In the turtlesim window, you will now see that the lines created by the movements of the turtle will be deleted.
Now we are going to try another service, for example, the /spawn service. This service will create another turtle in another location with a different orientation.
To start with, we are going to see the following type of message:
$ rosservice type /spawn | rossrv show
You will see something similar to the following output:
float32 x float32 y float32 theta string name --- string name
The preceding command is the same as the following commands. If you want to know why these lines are the same, search in Google about piping Linux:
$ rosservice type /spawn
You will see something similar to the following output:
turtlesim/Spawn
Type in the following command:
$ rossrv show turtlesim/Spawn
You will see something similar to the following output:
float32 x float32 y float32 theta string name --- string name
With these fields, we know how to invoke the service. We need the position of x and y, the orientation (theta), and the name of the new turtle:
$ rosservice call /spawn 3 3 0.2 "new_turtle"
We then obtain the following result: