flexible_calculation_state.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 from flexbe_core import EventState, Logger
5 
6 '''
7 Created on 29.08.2013
8 
9 @author: Philipp Schillinger
10 '''
11 
12 class FlexibleCalculationState(EventState):
13  '''
14  Implements a state that can perform a calculation based on multiple userdata inputs provided as a list to the calculation function.
15 
16  -- calculation function The function that performs the desired calculation.
17  It could be a private function (self.foo) manually defined in a behavior's source code
18  or a lambda function (e.g., lambda x: x[0]^2 + x[1]^2).
19  -- input_keys string[] List of available input keys.
20 
21  ># input_keys object[] Input(s) to the calculation function as a list of userdata.
22  The individual inputs can be accessed as list elements (see lambda expression example).
23 
24  #> output_value object The result of the calculation.
25 
26  <= done Indicates completion of the calculation.
27 
28  '''
29 
30 
31  def __init__(self, calculation, input_keys):
32  '''Constructor'''
33  super(FlexibleCalculationState, self).__init__(outcomes=['done'],
34  input_keys=input_keys,
35  output_keys=['output_value'])
36 
37  self._calculation = calculation
38  self._calculation_result = None
39  self._input_keys = input_keys
40 
41 
42  def execute(self, userdata):
43  '''Execute this state'''
44 
45  userdata.output_value = self._calculation_result
46 
47  # nothing to check
48  return 'done'
49 
50 
51  def on_enter(self, userdata):
52 
53  if self._calculation is not None:
54  try:
55  self._calculation_result = self._calculation(map(lambda key: userdata[key], self._input_keys))
56  except Exception as e:
57  Logger.logwarn('Failed to execute calculation function!\n%s' % str(e))
58  else:
59  Logger.logwarn('Passed no calculation!')


flexbe_states
Author(s): Philipp Schillinger
autogenerated on Wed Jun 5 2019 21:52:08