Wednesday, October 31, 2018

ROS Service client

  • Terminal 1
roscore
  • Terminal 2
rosrun turtlesim turtlesim_node
  • Terminal 3
rosservice list /clear /kill /reset /rosout/get_loggers /spawn /turtle1/set_pen /turtle1/teleport_absolute /turtle1/teleport_relative /turtlesim/get_loggers

spawn service

Spawn another turtle with spawn service

  • rosservice info <service name>
rosservice info /spawn Node: /turtlesim URI: rosrpc://user-N56VM:43083 Type: turtlesim/Spawn Args: x y theta name
  • rossrv info <service type>
float32 x float32 y float32 theta string name --- string name

call / execute

#left down corner: 0,0 rosservice call /spawn "x: 3.0 y: 3.0 theta: 0.0 name: 't1'"

service client

  • rosservice info result show the the service implement in /turtlesim package

  • most simple service client (without error handler)

#!/usr/bin/env python import rospy from turtlesim.srv import Spawn def main(): service = rospy.ServiceProxy('/spawn', Spawn) service(x=3.0, y=3.0, theta=0.0, name="t1") if __name__ == "__main__": main()

Service client send Request and server answer with response

The service declare by service message that has tow parts request and response

request --- response
  • For example Spawn.srv file under turtlesim.srv folder
float32 x float32 y float32 theta string name --- string

When compile the packaged catkin build and install class file that contains the class for example:

  • Spawn
  • SpawnRequest
  • SpawnResponse
#!/usr/bin/env python import rospy from turtlesim.srv import Spawn, SpawnRequest def main(): service = rospy.ServiceProxy('/spawn', Spawn) req = SpawnRequest(x=3.0, y=3.0, theta=0.0, name="t1") res = service(req) print res.name if __name__ == "__main__": main

No comments: