input_state.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import pickle
3 
4 from flexbe_core import EventState, Logger
5 from flexbe_msgs.msg import BehaviorInputAction, BehaviorInputGoal, BehaviorInputResult
6 from flexbe_core.proxy import ProxyActionClient
7 
8 
9 class InputState(EventState):
10  '''
11  Implements a state where the state machine needs an input from the operator.
12  Requests of different types, such as requesting a waypoint, a template, or a pose, can be specified.
13 
14  -- request uint8 One of the custom-defined values to specify the type of request.
15  -- message string Message displayed to the operator to let him know what to do.
16 
17  #> data object The data provided by the operator. The exact type depends on the request.
18 
19  <= received Returned as soon as valid data is available.
20  <= aborted The operator declined to provide the requested data.
21  <= no_connection No request could be sent to the operator.
22  <= data_error Data has been received, but could not be deserialized successfully.
23  '''
24 
25  def __init__(self, request, message):
26  '''
27  Constructor
28  '''
29  super(InputState, self).__init__(outcomes=['received', 'aborted', 'no_connection', 'data_error'],
30  output_keys=['data'])
31  self._action_topic = 'flexbe/behavior_input'
32  self._client = ProxyActionClient({self._action_topic: BehaviorInputAction})
33 
34  self._request = request
35  self._message = message
36  self._connected = True
37  self._received = False
38 
39  def execute(self, userdata):
40  if not self._connected:
41  return 'no_connection'
42  if self._received:
43  return 'received'
44 
45  if self._client.has_result(self._action_topic):
46  result = self._client.get_result(self._action_topic)
47  if result.result_code != BehaviorInputResult.RESULT_OK:
48  userdata.data = None
49  return 'aborted'
50  else:
51  try:
52  response_data = pickle.loads(result.data)
53  userdata.data = response_data
54  except Exception as e:
55  Logger.logwarn('Was unable to load provided data:\n%s' % str(e))
56  userdata.data = None
57  return 'data_error'
58 
59  self._received = True
60  return 'received'
61 
62  def on_enter(self, userdata):
63  self._received = False
64 
65  action_goal = BehaviorInputGoal()
66  action_goal.request_type = self._request
67  action_goal.msg = self._message
68 
69  try:
70  self._client.send_goal(self._action_topic, action_goal)
71  except Exception as e:
72  Logger.logwarn('Was unable to send data request:\n%s' % str(e))
73  self._connected = False
def __init__(self, request, message)
Definition: input_state.py:25


flexbe_states
Author(s): Philipp Schillinger
autogenerated on Sun Dec 13 2020 04:01:46