Go to the documentation of this file.00001
00002
00003 import rospy
00004 from flexbe_core import EventState, Logger
00005
00006 '''
00007 Created on 08.11.2016
00008
00009 @author: Alberto Romay
00010 '''
00011
00012 class FlexibleCheckConditionState(EventState):
00013 '''
00014 Implements a state that checks if the given condition is true based on multiple userdata inputs provided as a list to the calculation function
00015 and returns the corresponding outcome.
00016 This state can be used if the further control flow of the behavior depends on a simple condition.
00017
00018 -- predicate function The condition whose truth value will be evaluated.
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[0]^2 + x[1]^2).
00021 -- input_keys string[] List of available input keys.
00022
00023 ># input_keys object[] Input(s) to the calculation function as a list of userdata.
00024 The individual inputs can be accessed as list elements (see lambda expression example).
00025
00026 <= true Returned if the condition evaluates to True
00027 <= false Returned if the condition evaluates to False
00028
00029 '''
00030
00031
00032 def __init__(self, predicate, input_keys):
00033 '''Constructor'''
00034 super(FlexibleCheckConditionState, self).__init__(outcomes=['true', 'false'],
00035 input_keys=input_keys)
00036
00037 self._input_keys = input_keys
00038 self._predicate = predicate
00039 self._outcome = 'false'
00040
00041
00042 def execute(self, userdata):
00043 '''Execute this state'''
00044
00045 return self._outcome
00046
00047
00048 def on_enter(self, userdata):
00049
00050 if self._predicate is not None:
00051 try:
00052 self._outcome = "true" if self._predicate(map(lambda key: userdata[key], self._input_keys)) else 'false'
00053 except Exception as e:
00054 Logger.logwarn('Failed to execute condition function!\n%s' % str(e))
00055 else:
00056 Logger.logwarn('Passed no predicate!')