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 FlexibleCalculationState(EventState):
00013 '''
00014 Implements a state that can perform a calculation based on multiple userdata inputs provided as a list to the calculation function.
00015
00016 -- calculation function The function that performs the desired calculation.
00017 It could be a private function (self.foo) manually defined in a behavior's source code
00018 or a lambda function (e.g., lambda x: x[0]^2 + x[1]^2).
00019 -- input_keys string[] List of available input keys.
00020
00021 ># input_keys object[] Input(s) to the calculation function as a list of userdata.
00022 The individual inputs can be accessed as list elements (see lambda expression example).
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, input_keys):
00032 '''Constructor'''
00033 super(FlexibleCalculationState, self).__init__(outcomes=['done'],
00034 input_keys=input_keys,
00035 output_keys=['output_value'])
00036
00037 self._calculation = calculation
00038 self._calculation_result = None
00039 self._input_keys = input_keys
00040
00041
00042 def execute(self, userdata):
00043 '''Execute this state'''
00044
00045 userdata.output_value = self._calculation_result
00046
00047
00048 return 'done'
00049
00050
00051 def on_enter(self, userdata):
00052
00053 if self._calculation is not None:
00054 try:
00055 self._calculation_result = self._calculation(map(lambda key: userdata[key], self._input_keys))
00056 except Exception as e:
00057 Logger.logwarn('Failed to execute calculation function!\n%s' % str(e))
00058 else:
00059 Logger.logwarn('Passed no calculation!')