event_state.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 from flexbe_core.logger import Logger
3 from flexbe_core.state_logger import StateLogger
4 from flexbe_core.core.preemptable_state import PreemptableState
5 from flexbe_core.core.priority_container import PriorityContainer
6 
7 from flexbe_msgs.msg import CommandFeedback
8 from std_msgs.msg import Bool, Empty
9 
10 from flexbe_core.core.operatable_state import OperatableState
11 
12 
13 @StateLogger.log_events('flexbe.events',
14  start='on_start', stop='on_stop',
15  pause='on_pause', resume='on_resume',
16  enter='on_enter', exit='on_exit')
17 @StateLogger.log_userdata('flexbe.userdata')
19  """
20  A state that allows implementing certain events.
21  """
22 
23  def __init__(self, *args, **kwargs):
24  super(EventState, self).__init__(*args, **kwargs)
25  self.__execute = self.execute
26  self.execute = self._event_execute
27 
28  self._entering = True
29  self._skipped = False
30  self._paused = False
32 
33  self._feedback_topic = 'flexbe/command_feedback'
34  self._repeat_topic = 'flexbe/command/repeat'
35  self._pause_topic = 'flexbe/command/pause'
36 
37  def _event_execute(self, *args, **kwargs):
38  if self._is_controlled and self._sub.has_msg(self._pause_topic):
39  msg = self._sub.get_last_msg(self._pause_topic)
40  self._sub.remove_last_msg(self._pause_topic)
41  if msg.data:
42  Logger.localinfo("--> Pausing in state %s", self.name)
43  self._pub.publish(self._feedback_topic, CommandFeedback(command="pause"))
44  self._last_active_container = PriorityContainer.active_container
45  # claim priority to propagate pause event
46  PriorityContainer.active_container = self.path
47  self._paused = True
48  else:
49  Logger.localinfo("--> Resuming in state %s", self.name)
50  self._pub.publish(self._feedback_topic, CommandFeedback(command="resume"))
51  PriorityContainer.active_container = self._last_active_container
52  self._last_active_container = None
53  self._paused = False
54 
55  if self._paused and not PreemptableState.preempt:
56  self._notify_skipped()
57  return None
58 
59  if self._entering:
60  self._entering = False
61  self.on_enter(*args, **kwargs)
62  if self._skipped and not PreemptableState.preempt:
63  self._skipped = False
64  self.on_resume(*args, **kwargs)
65 
66  outcome = self.__execute(*args, **kwargs)
67 
68  repeat = False
69  if self._is_controlled and self._sub.has_msg(self._repeat_topic):
70  Logger.localinfo("--> Repeating state %s", self.name)
71  self._sub.remove_last_msg(self._repeat_topic)
72  self._pub.publish(self._feedback_topic, CommandFeedback(command="repeat"))
73  repeat = True
74 
75  if repeat or outcome is not None and not PreemptableState.preempt:
76  self._entering = True
77  self.on_exit(*args, **kwargs)
78  return outcome
79 
80  def _notify_skipped(self):
81  if not self._skipped:
82  self.on_pause()
83  self._skipped = True
84  super(EventState, self)._notify_skipped()
85 
87  super(EventState, self)._enable_ros_control()
88  self._pub.createPublisher(self._feedback_topic, CommandFeedback)
89  self._sub.subscribe(self._repeat_topic, Empty)
90  self._sub.subscribe(self._pause_topic, Bool)
91 
93  super(EventState, self)._disable_ros_control()
94  self._sub.unsubscribe_topic(self._repeat_topic)
95  self._sub.unsubscribe_topic(self._pause_topic)
96  self._last_active_container = None
97  if self._paused:
98  PriorityContainer.active_container = None
99 
100  # Events
101  # (just implement the ones you need)
102 
103  def on_start(self):
104  """
105  Will be executed once when the behavior starts.
106  """
107  pass
108 
109  def on_stop(self):
110  """
111  Will be executed once when the behavior stops or is preempted.
112  """
113  pass
114 
115  def on_pause(self):
116  """
117  Will be executed each time this state is paused.
118  """
119  pass
120 
121  def on_resume(self, userdata):
122  """
123  Will be executed each time this state is resumed.
124  """
125  pass
126 
127  def on_enter(self, userdata):
128  """
129  Will be executed each time the state is entered from any other state (but not from itself).
130  """
131  pass
132 
133  def on_exit(self, userdata):
134  """
135  Will be executed each time the state will be left to any other state (but not to itself).
136  """
137  pass
def _event_execute(self, args, kwargs)
Definition: event_state.py:37
def __init__(self, args, kwargs)
Definition: event_state.py:23


flexbe_core
Author(s): Philipp Schillinger
autogenerated on Sun Dec 13 2020 04:01:39