-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfibonacci_action_server.py
executable file
·68 lines (54 loc) · 2.5 KB
/
fibonacci_action_server.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#! /usr/bin/env python
import rospy
import actionlib
from actionlib_tutorials.msg import FibonacciFeedback, FibonacciResult, FibonacciAction
class FibonacciClass(object):
# create messages that are used to publish feedback/result
_feedback = FibonacciFeedback()
_result = FibonacciResult()
def __init__(self):
# creates the action server
self._as = actionlib.SimpleActionServer("fibonacci_as", FibonacciAction, self.goal_callback, False)
self._as.start()
def goal_callback(self, goal):
# this callback is called when the action server is called.
# this is the function that computes the Fibonacci sequence
# and returns the sequence to the node that called the action server
# helper variables
r = rospy.Rate(1)
success = True
# append the seeds for the fibonacci sequence
self._feedback.sequence = []
self._feedback.sequence.append(0)
self._feedback.sequence.append(1)
# publish info to the console for the user
rospy.loginfo('"fibonacci_as": Executing, creating fibonacci sequence of order %i with seeds %i, %i' % ( goal.order, self._feedback.sequence[0], self._feedback.sequence[1]))
# starts calculating the Fibonacci sequence
fibonacciOrder = goal.order
for i in xrange(1, fibonacciOrder):
# check that preempt (cancelation) has not been requested by the action client
if self._as.is_preempt_requested():
rospy.loginfo('The goal has been cancelled/preempted')
# the following line, sets the client in preempted state (goal cancelled)
self._as.set_preempted()
success = False
# we end the calculation of the Fibonacci sequence
break
# builds the next feedback msg to be sent
self._feedback.sequence.append(self._feedback.sequence[i] + self._feedback.sequence[i-1])
# publish the feedback
self._as.publish_feedback(self._feedback)
# the sequence is computed at 1 Hz frequency
r.sleep()
# at this point, either the goal has been achieved (success==true)
# or the client preempted the goal (success==false)
# If success, then we publish the final result
# If not success, we do not publish anything in the result
if success:
self._result.sequence = self._feedback.sequence
rospy.loginfo('Succeeded calculating the Fibonacci of order %i' % fibonacciOrder )
self._as.set_succeeded(self._result)
if __name__ == '__main__':
rospy.init_node('fibonacci')
FibonacciClass()
rospy.spin()