Go to the documentation of this file.00001
00002 import rospy
00003
00004 from flexbe_core.core.loopback_state_machine import LoopbackStateMachine
00005 from flexbe_core.core.preemptable_state import PreemptableState
00006
00007 from flexbe_core.proxy import ProxySubscriberCached
00008 from std_msgs.msg import Empty
00009
00010
00011 class PreemptableStateMachine(LoopbackStateMachine):
00012 """
00013 A state machine that can be preempted.
00014 If preempted, the state machine will return the outcome preempted.
00015 """
00016
00017 _preempted_name = 'preempted'
00018
00019 def __init__(self, *args, **kwargs):
00020
00021 if len(args) > 0:
00022 args[0].append(self._preempted_name)
00023 else:
00024 outcomes = kwargs.get('outcomes', [])
00025 outcomes.append(self._preempted_name)
00026 kwargs['outcomes'] = outcomes
00027
00028 super(PreemptableStateMachine, self).__init__(*args, **kwargs)
00029
00030
00031 self._preempt_topic = 'flexbe/command/preempt'
00032 self._sub = ProxySubscriberCached({self._preempt_topic: Empty})
00033 self._sub.set_callback(self._preempt_topic, self._preempt_cb)
00034
00035
00036 def _preempt_cb(self, msg):
00037 if not self._is_controlled:
00038 PreemptableState.preempt = True