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 CalculationState(EventState):
13  '''
14  Implements a state that can perform a calculation based on userdata.
15  calculation is a function which takes exactly one parameter, input_value from userdata,
16  and its return value is stored in output_value after leaving the state.
17 
18  -- calculation function The function that performs the desired calculation.
19  It could be a private function (self.foo) manually defined in a behavior's source code
20  or a lambda function (e.g., lambda x: x^2, where x will be the input_value).
21 
22  ># input_value object Input to the calculation function.
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):
32  '''
33  Constructor
34  '''
35  super(CalculationState, self).__init__(outcomes=['done'],
36  input_keys=['input_value'],
37  output_keys=['output_value'])
38 
39  self._calculation = calculation
40  self._calculation_result = None
41 
42 
43  def execute(self, userdata):
44  '''Execute this state'''
45 
46  userdata.output_value = self._calculation_result
47 
48  # nothing to check
49  return 'done'
50 
51 
52  def on_enter(self, userdata):
53 
54  if self._calculation is not None:
55  try:
56  self._calculation_result = self._calculation(userdata.input_value)
57  except Exception as e:
58  Logger.logwarn('Failed to execute calculation function!\n%s' % str(e))
59  else:
60  Logger.logwarn('Passed no calculation!')


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