fibonacci_server.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 import rospy
00004 
00005 import actionlib
00006 
00007 import actionlib_tutorials.msg
00008 
00009 class FibonacciAction(object):
00010     # create messages that are used to publish feedback/result
00011     _feedback = actionlib_tutorials.msg.FibonacciFeedback()
00012     _result = actionlib_tutorials.msg.FibonacciResult()
00013 
00014     def __init__(self, name):
00015         self._action_name = name
00016         self._as = actionlib.SimpleActionServer(self._action_name, actionlib_tutorials.msg.FibonacciAction, execute_cb=self.execute_cb, auto_start = False)
00017         self._as.start()
00018       
00019     def execute_cb(self, goal):
00020         # helper variables
00021         r = rospy.Rate(1)
00022         success = True
00023         
00024         # append the seeds for the fibonacci sequence
00025         self._feedback.sequence = []
00026         self._feedback.sequence.append(0)
00027         self._feedback.sequence.append(1)
00028         
00029         # publish info to the console for the user
00030         rospy.loginfo('%s: Executing, creating fibonacci sequence of order %i with seeds %i, %i' % (self._action_name, goal.order, self._feedback.sequence[0], self._feedback.sequence[1]))
00031         
00032         # start executing the action
00033         for i in range(1, goal.order):
00034             # check that preempt has not been requested by the client
00035             if self._as.is_preempt_requested():
00036                 rospy.loginfo('%s: Preempted' % self._action_name)
00037                 self._as.set_preempted()
00038                 success = False
00039                 break
00040             self._feedback.sequence.append(self._feedback.sequence[i] + self._feedback.sequence[i-1])
00041             # publish the feedback
00042             self._as.publish_feedback(self._feedback)
00043             # this step is not necessary, the sequence is computed at 1 Hz for demonstration purposes
00044             r.sleep()
00045           
00046         if success:
00047             self._result.sequence = self._feedback.sequence
00048             rospy.loginfo('%s: Succeeded' % self._action_name)
00049             self._as.set_succeeded(self._result)
00050         
00051 if __name__ == '__main__':
00052     rospy.init_node('fibonacci')
00053     server = FibonacciAction(rospy.get_name())
00054     rospy.spin()


actionlib_tutorials
Author(s): Melonee Wise
autogenerated on Thu Jun 6 2019 21:11:25