A ROS service is a node that must run and you must wait until it has completed.
ROS Service
A service is a node that must run and you must wait until it has completed.
Commands
1
2
3
4
rosservice list
rosservice info <service name> # Node, URI, Type and Argsrossrv show <service name> # Args and return typerosservice call <service name> <args> # "{}" is empty arg
Python Call ROS Service
1
2
3
4
5
6
7
8
9
10
11
importrospyfromgazebo_msgs.srvimportDeleteModel,DeleteModelRequestimportsysrospy.init_node('service_client')rospy.wait_for_service('/gazebo/delete_model')# Wait for the service client to be runningdelete_model_service=rospy.ServiceProxy('/gazebo/delete_model',DeleteModel)# Create the connection to the servicekk=DeleteRequest()kk.model_name="bowl_1"# Create an Arg for the serviceresult=delete_model_service(kk)print(result)
Python Create an ROS Service
1
2
3
4
5
6
7
8
9
10
11
importrospyfromstd_srvs.srvimportEmpty,EmptyResponsedefmy_callback(request):print"My_callback has been called"returnEmptyResponse()#return MyServiceResponse(len(request.words.split()))rospy.init_node('service_server')my_service=rospy.Service('/my_service',Empty,my_callback)rospy.spin()# maintain the service open
Call Service
1
rosservice call /my_service "{}"
Create a Catkin Service Project
Create Project
1
2
3
4
5
6
roscd
cd ../src/
catkin_create_pkg my_custom_srv_msg_pkg rospy
roscd my_custom_srv_msg_pkg
mkdir srv
vim srv/MyCustomServiceMessage.srv
Create Service File
Service file containing the arguments and return type.