calc_area_server.py
#!/usr/bin/env python3
import rospy
from calc_area_package.srv import AreaServiceDialogue, AreaServiceDialogueResponse
def calculate_area(request):
area = AreaServiceDialogueResponse(request.length * request.width)
return area
def area_server():
rospy.init_node('area_server_node')
s = rospy.Service('area_service', AreaServiceDialogue, calculate_area)
print("Ready...")
rospy.spin()
if __name__ == "__main__":
area_server()
calc_area_client.py
#!/usr/bin/env python3
import sys
import rospy
from calc_area_package.srv import AreaServiceDialogue, AreaServiceDialogueResponse
def area_client(length, width):
rospy.wait_for_service('area_service')
try:
calc_area = rospy.ServiceProxy('area_service', AreaServiceDialogue)
response = calc_area(length, width)
return response.area
except rospy.ServiceException as e:
print("Service call failed: %s"%e)
def usage():
return "%s [x y]"%sys.argv[0]
if __name__ == "__main__":
if len(sys.argv) == 3:
length = int(sys.argv[1])
width = int(sys.argv[2])
else:
print(usage())
sys.exit(1)
print("The area is: %s"%(area_client(length, width)))