preemptable_state.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 from flexbe_core.logger import Logger
3 
4 from flexbe_msgs.msg import CommandFeedback
5 from std_msgs.msg import Empty
6 
7 from flexbe_core.core.lockable_state import LockableState
8 
9 
11  """
12  A state that can be preempted.
13  If preempted, the state will not be executed anymore and return the outcome preempted.
14  """
15 
16  _preempted_name = 'preempted'
17  preempt = False
18 
19  def __init__(self, *args, **kwargs):
20  super(PreemptableState, self).__init__(*args, **kwargs)
21  self.__execute = self.execute
23 
24  PreemptableState.preempt = False
25 
26  self._feedback_topic = 'flexbe/command_feedback'
27  self._preempt_topic = 'flexbe/command/preempt'
28 
29  def _preemptable_execute(self, *args, **kwargs):
30  if self._is_controlled and self._sub.has_msg(self._preempt_topic):
31  self._sub.remove_last_msg(self._preempt_topic)
32  self._pub.publish(self._feedback_topic, CommandFeedback(command="preempt"))
33  PreemptableState.preempt = True
34  Logger.localinfo("--> Behavior will be preempted")
35 
36  if PreemptableState.preempt:
37  if not self._is_controlled:
38  Logger.localinfo("Behavior will be preempted")
39  self._force_transition = True
40  return self._preempted_name
41 
42  return self.__execute(*args, **kwargs)
43 
44  def _notify_skipped(self):
45  # make sure we dont miss a preempt even if not being executed
46  if self._is_controlled and self._sub.has_msg(self._preempt_topic):
47  self._sub.remove_last_msg(self._preempt_topic)
48  self._pub.publish(self._feedback_topic, CommandFeedback(command="preempt"))
49  PreemptableState.preempt = True
50 
52  super(PreemptableState, self)._enable_ros_control()
53  self._pub.createPublisher(self._feedback_topic, CommandFeedback)
54  self._sub.subscribe(self._preempt_topic, Empty)
55  PreemptableState.preempt = False
56 
58  super(PreemptableState, self)._disable_ros_control()
59  self._sub.unsubscribe_topic(self._preempt_topic)


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