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


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