Go to the documentation of this file.00001
00002
00003 import rospy
00004 from flexbe_core import EventState, Logger
00005
00006 '''
00007 Created on 29.08.2013
00008
00009 @author: Philipp Schillinger
00010 '''
00011
00012 class CalculationState(EventState):
00013 '''
00014 Implements a state that can perform a calculation based on userdata.
00015 calculation is a function which takes exactly one parameter, input_value from userdata,
00016 and its return value is stored in output_value after leaving the state.
00017
00018 -- calculation function The function that performs the desired calculation.
00019 It could be a private function (self.foo) manually defined in a behavior's source code
00020 or a lambda function (e.g., lambda x: x^2, where x will be the input_value).
00021
00022 ># input_value object Input to the calculation function.
00023
00024 #> output_value object The result of the calculation.
00025
00026 <= done Indicates completion of the calculation.
00027
00028 '''
00029
00030
00031 def __init__(self, calculation):
00032 '''
00033 Constructor
00034 '''
00035 super(CalculationState, self).__init__(outcomes=['done'],
00036 input_keys=['input_value'],
00037 output_keys=['output_value'])
00038
00039 self._calculation = calculation
00040 self._calculation_result = None
00041
00042
00043 def execute(self, userdata):
00044 '''Execute this state'''
00045
00046 userdata.output_value = self._calculation_result
00047
00048
00049 return 'done'
00050
00051
00052 def on_enter(self, userdata):
00053
00054 if self._calculation is not None:
00055 try:
00056 self._calculation_result = self._calculation(userdata.input_value)
00057 except Exception as e:
00058 Logger.logwarn('Failed to execute calculation function!\n%s' % str(e))
00059 else:
00060 Logger.logwarn('Passed no calculation!')