ROS Service
In the previous tutorial, we have discussed ROS communication protocols. Here, ROS service is another communication protocol that involves the client and the server. So, a complete ROS server will provide the service and the ROS client consume or utilize the service and ensure proper communication.
It is a one-time communication that involves the request from the client and the response from the server. ROS services can be used in scenarios like where there is a request to the robot to perform the specific action.
Implementation of ROS Service
Initially implement the code roscore to start the ros master node
-->roscore
Now, you can run the turtlesim node to start the turtle simulator
–>rosrun turtlesim turtlesim_node
Identify the list of all the ros services.
–>rosservice list
One of the service is the spawn, which means replication can be used as the rosservice
Now, to send the command or the service request, we need to identify the arguments. So, use the ROS info/Spawn command to identify the syntax of spawn service.
–>rosservie info /spawn
Now, to identify the structure of the message about the data types used for the spawn service use the rossrv info turtlesim/spawn.
–>rossrv info turtlesim/Spawn
Now the new turtle can be replicated in the simulation coordinates, with the suitable arguments
–>rosservice call /spawn 7 7 0 t2
In this way, the spawn service can be implemented with different positions of the turtlebot in the simulation environment.
Writing Simple ROS Service in Python
ROS service is a two way communication between the ROS Server and client.
Steps to create ROS Server/Client Application
- Initially, define the service message clearly in service file
- Create the ROS Server Node
- Create ROS Client Node
- Execute the service
- Then consume the service by the client
Implementation of the Service of Adding two integers
In this service application, the client will send the request to the server with two integers as the arguments. Then the server will send the response to the client with one single argument, that is result of the addition of the two integers.
The service file must consist of the list of the arguments that can be used for requents and responses known as service definition completely.
After that, the source file has to be compiled to work accordingly and the following commands has to be mentioned in the package xml file to obtain the source file for python and c++.
Now, the following implementations has to be done in the cmake list to include the service file.
Now, go to the catkin workspace and generate the source file for c++ and python.
The following command can be used to identify the source file has created successfully and the arguments in the service file.
–> rossrv show AddTwoInts
Writing ROS Service in Python
After writing the code in the visual studio for the two files add_server.py and add_client.py. Now, it is the time to execute the codes and perform the ROS service.