operatable_state.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import rospy
3 
4 from flexbe_core.core.preemptable_state import PreemptableState
5 from flexbe_core.core.operatable_state_machine import OperatableStateMachine
6 
7 from flexbe_core.proxy import ProxyPublisher, ProxySubscriberCached
8 from flexbe_msgs.msg import Container, OutcomeRequest
9 from std_msgs.msg import UInt8, String
10 
11 from flexbe_core.state_logger import StateLogger
12 
13 
15  """
16  A state that supports autonomy levels and silent mode.
17  Also, it allows being tracked by a GUI or anything else
18  as it reports each transition and its initial structure.
19  An additional method is provided to report custom status messages to the widget.
20  """
21 
22  def __init__(self, *args, **kwargs):
23  super(OperatableState, self).__init__(*args, **kwargs)
24  self.transitions = None
25  self.autonomy = None
26 
27  self._mute = False # is set to true when used in silent state machine (don't report transitions)
28  self._sent_outcome_requests = [] # contains those outcomes that already requested a transition
29 
30  self._outcome_topic = 'flexbe/mirror/outcome'
31  self._request_topic = 'flexbe/outcome_request'
32  self._debug_topic = 'flexbe/debug/current_state'
33  self._pub = ProxyPublisher()
34 
35  self.__execute = self.execute
37 
38 
39  def _build_msg(self, prefix, msg):
40  """
41  Adds this state to the initial structure message.
42 
43  @type prefix: string
44  @param prefix: A path consisting of the container hierarchy containing this state.
45 
46  @type msg: ContainerStructure
47  @param msg: The message that will finally contain the structure message.
48  """
49 
50  # set path
51  name = prefix + self.name
52 
53  # no children
54  children = None
55 
56  # set outcomes
57  outcomes = self._outcome_list
58 
59  # set transitions and autonomy levels
60  transitions = []
61  autonomy = []
62  for i in range(len(outcomes)):
63  outcome = outcomes[i]
64  if outcome == 'preempted': # set preempt transition
65  transitions.append('preempted')
66  autonomy.append(-1)
67  else:
68  transitions.append(str(self.transitions[outcome]))
69  autonomy.append(self.autonomy[outcome])
70 
71  # add to message
72  msg.containers.append(Container(name, children, outcomes, transitions, autonomy))
73 
74 
75  def _operatable_execute(self, *args, **kwargs):
76  outcome = self.__execute(*args, **kwargs)
77 
78  if self._is_controlled:
79  log_requested_outcome = outcome
80 
81  # request outcome because autonomy level is too low
82  if not self._force_transition and (self.autonomy.has_key(outcome) and self.autonomy[outcome] >= OperatableStateMachine.autonomy_level):
83  if self._sent_outcome_requests.count(outcome) == 0:
84  self._pub.publish(self._request_topic, OutcomeRequest(outcome=self._outcome_list.index(outcome), target=self._parent._get_path() + "/" + self.name))
85  rospy.loginfo("<-- Want result: %s > %s", self.name, outcome)
86  StateLogger.log_state_execution(self._get_path(), self.__class__.__name__, outcome, not self._force_transition, False)
87  self._sent_outcome_requests.append(outcome)
88  outcome = OperatableState._loopback_name
89 
90  # autonomy level is high enough, report the executed transition
91  elif outcome != OperatableState._loopback_name:
92  self._sent_outcome_requests = []
93  rospy.loginfo("State result: %s > %s", self.name, outcome)
94  self._pub.publish(self._outcome_topic, UInt8(self._outcome_list.index(outcome)))
95  self._pub.publish(self._debug_topic, String("%s > %s" % (self._get_path(), outcome)))
96  StateLogger.log_state_execution(self._get_path(), self.__class__.__name__, outcome, not self._force_transition, True)
97 
98  self._force_transition = False
99 
100  return outcome
101 
102  def _notify_skipped(self):
103  super(OperatableState, self)._notify_skipped()
104 
105 
107  super(OperatableState, self)._enable_ros_control()
108  self._pub.createPublisher(self._outcome_topic, UInt8)
109  self._pub.createPublisher(self._debug_topic, String)
110  self._pub.createPublisher(self._request_topic, OutcomeRequest)
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 


flexbe_core
Author(s): Philipp Schillinger
autogenerated on Wed Jun 5 2019 21:51:59