srv 그림
https://docs.ros.org/en/dashing/Tutorials/Services/Understanding-ROS2-Services.html
Understanding ROS 2 services — ROS 2 Documentation: Dashing documentation
Goal: Learn about services in ROS 2 using command line tools.
docs.ros.org
msg = noneblock 개념 = 1 : 다
srv = block 개념 = 1 : 1 =
action = block + noneblock 개념
사용할 srv data type
add_two_ints_server.py
#서버
#!/usr/bin/env python3
# import the AddTwoInts service
from rospy_tutorials.srv import *
import rospy
def add_two_ints(req):
print("Returning [%s + %s = %s]" % (req.a, req.b, (req.a + req.b)))
return AddTwoIntsResponse(req.a + req.b)
def add_two_ints_server():
rospy.init_node('add_two_ints_server')
s = rospy.Service('add_two_ints', AddTwoInts, add_two_ints)
# spin() keeps Python from exiting until node is shutdown
rospy.spin()
if __name__ == "__main__":
add_two_ints_server()
add_two_ints_client.py
#클라이언트
#!/usr/bin/env python3
import sys
import os
import rospy
# imports the AddTwoInts service
from rospy_tutorials.srv import *
## add two numbers using the add_two_ints service
## @param x int: first number to add
## @param y int: second number to add
def add_two_ints_client(x, y):
# NOTE: you don't have to call rospy.init_node() to make calls against
# a service. This is because service clients do not have to be
# nodes.
# block until the add_two_ints service is available
# you can optionally specify a timeout
rospy.wait_for_service('add_two_ints')
try:
# create a handle to the add_two_ints service
add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)
print("Requesting %s+%s"%(x, y))
# simplified style
resp1 = add_two_ints(x, y)
# formal style
resp2 = add_two_ints.call(AddTwoIntsRequest(x, y))
if not resp1.sum == (x + y):
raise Exception("test failure, returned sum was %s"%resp1.sum)
if not resp2.sum == (x + y):
raise Exception("test failure, returned sum was %s"%resp2.sum)
return resp1.sum
except rospy.ServiceException as e:
print("Service call failed: %s"%e)
def usage():
return "%s [x y]"%sys.argv[0]
if __name__ == "__main__":
argv = rospy.myargv()
if len(argv) == 1:
import random
x = random.randint(-50000, 50000)
y = random.randint(-50000, 50000)
elif len(argv) == 3:
try:
x = int(argv[1])
y = int(argv[2])
except:
print(usage())
sys.exit(1)
else:
print(usage())
sys.exit(1)
print("%s + %s = %s"%(x, y, add_two_ints_client(x, y)))
서비스랑 클라이언트도 딱 3개임 ( srv명의 request와 response 데이터타입에 맞춰서 날려줘야함 )
connect , request , response
1. server가 특정 네임스페이스+ 특정 srv로 서비스 등록
2. client 가 server에 request
3. server가 response
keyword :
init_node
Service
wait_for_service
ServiceProxy
네임스페이스.call(srv명Request(x, y))
srv명Response(req.a + req.b)
connect : rospy.Service('add_two_ints', AddTwoInts, add_two_ints),
rospy.wait_for_service(네임스페이스) , rospy.ServiceProxy(네임스페이스,srv명)
request : 네임스페이스.call(srv명Request(x, y))
response : srv명Response(req.a + req.b)
'ROS_python_정리 > msgs , srv , action' 카테고리의 다른 글
ros1_msg (0) | 2022.04.18 |
---|---|
action fancy예제 _2 ( 비동기부분 포함 , srvs + msgs ) (0) | 2022.04.12 |
action simple예제 _1 ( 비동기부분 제외 , service와 비슷함) (0) | 2022.04.11 |
action 정리 (0) | 2022.04.10 |
댓글