input_state.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import rospy
00004 import actionlib
00005 import pickle
00006 
00007 from flexbe_core import EventState, Logger
00008 from flexbe_msgs.msg import BehaviorInputAction, BehaviorInputGoal, BehaviorInputResult
00009 from flexbe_core.proxy import ProxyActionClient
00010 
00011 '''
00012 Created on 02/13/2015
00013 
00014 @author: Philipp Schillinger
00015 '''
00016 
00017 class InputState(EventState):
00018         '''
00019         Implements a state where the state machine needs an input from the operator.
00020         Requests of different types, such as requesting a waypoint, a template, or a pose, can be specified.
00021 
00022         -- request      uint8           One of the class constants to specify the type of request.
00023         -- message      string          Message displayed to the operator to let him know what to do.
00024 
00025         #> data         object          The data provided by the operator. The exact type depends on the request.
00026 
00027         <= received                     Returned as soon as valid data is available.
00028         <= aborted                              The operator declined to provide the requested data.
00029         <= no_connection                No request could be sent to the operator.
00030         <= data_error                   Data has been received, but could not be deserialized successfully.
00031 
00032         '''
00033 
00034         POINT_LOCATION                  = BehaviorInputGoal.POINT_LOCATION
00035         SELECTED_OBJECT_ID              = BehaviorInputGoal.SELECTED_OBJECT_ID
00036         WAYPOINT_GOAL_POSE              = BehaviorInputGoal.WAYPOINT_GOAL_POSE
00037         GHOST_JOINT_STATES              = BehaviorInputGoal.GHOST_JOINT_STATES
00038         FOOTSTEP_PLAN_HEADER    = BehaviorInputGoal.FOOTSTEP_PLAN_HEADER
00039 
00040 
00041         def __init__(self, request, message):
00042                 '''
00043                 Constructor
00044                 '''
00045                 super(InputState, self).__init__(outcomes=['received', 'aborted', 'no_connection', 'data_error'],
00046                                                                                                 output_keys=['data'])
00047                 
00048                 self._action_topic = 'flexbe/behavior_input'
00049 
00050                 self._client = ProxyActionClient({self._action_topic: BehaviorInputAction})
00051 
00052                 self._request = request
00053                 self._message = message
00054                 self._connected = True
00055                 self._received = False
00056                 
00057                 
00058         def execute(self, userdata):
00059                 '''
00060                 Execute this state
00061                 '''
00062                 if not self._connected:
00063                         return 'no_connection'
00064                 if self._received:
00065                         return 'received'
00066 
00067                 if self._client.has_result(self._action_topic):
00068                         result = self._client.get_result(self._action_topic)
00069                         if result.result_code != BehaviorInputResult.RESULT_OK:
00070                                 userdata.data = None
00071                                 return 'aborted'
00072                         else:
00073                                 try:
00074                                         response_data = pickle.loads(result.data)
00075                                         #print 'Input request response:', response_data
00076                                         userdata.data = response_data
00077                                         # If this was a template ID request, log that template ID:
00078                                         if self._request == BehaviorInputGoal.SELECTED_OBJECT_ID:
00079                                                 Logger.loginfo('Received template ID: %d' % int(response_data))
00080                                 
00081                                 except Exception as e:
00082                                         Logger.logwarn('Was unable to load provided data:\n%s' % str(e))
00083                                         userdata.data = None
00084                                         return 'data_error'
00085 
00086                                 self._received = True
00087                                 return 'received'
00088                         
00089         
00090         def on_enter(self, userdata):
00091 
00092                 self._received = False
00093 
00094                 if not self._connected: return
00095 
00096                 action_goal = BehaviorInputGoal()
00097                 action_goal.request_type = self._request
00098                 action_goal.msg = self._message
00099 
00100                 try:
00101                         self._client.send_goal(self._action_topic, action_goal)
00102                 except Exception as e:
00103                         Logger.logwarn('Was unable to send data request:\n%s' % str(e))
00104                         self._connected = False
00105         
00106         
00107 
00108 
00109 
00110 
00111 
00112 
00113 
00114 
00115 
00116 
00117 
00118 
00119 


flexbe_states
Author(s): Philipp Schillinger
autogenerated on Thu Jun 6 2019 19:32:33