-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_server_template.py
executable file
·35 lines (28 loc) · 1.11 KB
/
service_server_template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#! /usr/bin/env python
#coding:utf-8
import rospy
from ros_template_programs.msg import Expression
from ros_template_programs.srv import Calculation, CalculationResponse
class ServiceServer():
def __init__(self):
self.service = rospy.Service('calculate_two_numbers', Calculation, self.calculate )
rospy.loginfo('\n [ ' + rospy.get_name() + ' ] Ready to calculate_two_numbers.\n' )
def calculate(self, req):
res = CalculationResponse()
ex = req.expression
if ex.calculate == '+':
res.result = ex.a + ex.b
elif ex.calculate == '-':
res.result = ex.a - ex.b
elif ex.calculate == '/':
res.result = ex.a * ex.b
else:
res.result = ex.a / ex.b
rospy.loginfo('\n [ ' + rospy.get_name() + ' ] Received a call from a client\nRequest: %.2f %s %.2f = %.2f\n', ex.a, ex.calculate, ex.b, res.result )
return res
if __name__ == '__main__':
try:
rospy.init_node('service_server_template')
node = ServiceServer()
rospy.spin()
except rospy.ROSInterruptException: pass