5 from flexbe_core
import EventState, Logger
12 @author: Philipp Schillinger 17 Gets the latest message on the given topic and stores it to userdata. 19 -- topic string The topic on which should be listened. 20 -- blocking bool Blocks until a message is received. 21 -- clear bool Drops last message on this topic on enter 22 in order to only handle message received since this state is active. 24 #> message object Latest message on the given topic of the respective type. 26 <= received Message has been received and stored in userdata or state is not blocking. 27 <= unavailable The topic is not available when this state becomes actives. 32 def __init__(self, topic, blocking = True, clear = False):
36 super(SubscriberState, self).
__init__(outcomes=[
'received',
'unavailable'],
37 output_keys=[
'message'])
44 (msg_path, msg_topic, fn) = rostopic.get_topic_type(self.
_topic)
46 if msg_topic == self.
_topic:
48 self.
_sub = ProxySubscriberCached({self.
_topic: msg_type})
51 Logger.logwarn(
'Topic %s for state %s not yet available.\nFound: %s\nWill try again when entering the state...' % (self.
_topic, self.name, str(msg_topic)))
59 userdata.message =
None 63 userdata.message = self._sub.get_last_msg(self.
_topic)
64 self._sub.remove_last_msg(self.
_topic)
70 (msg_path, msg_topic, fn) = rostopic.get_topic_type(self.
_topic)
71 if msg_topic == self.
_topic:
73 self.
_sub = ProxySubscriberCached({self.
_topic: msg_type})
75 Logger.loginfo(
'Successfully subscribed to previously unavailable topic %s' % self.
_topic)
77 Logger.logwarn(
'Topic %s still not available, giving up.' % self.
_topic)
80 self._sub.remove_last_msg(self.
_topic)
84 msg_import = msg_path.split(
'/')
85 msg_module =
'%s.msg' % (msg_import[0])
86 package = __import__(msg_module, fromlist=[msg_module])
87 clsmembers = inspect.getmembers(package,
lambda member: inspect.isclass(member)
and member.__module__.endswith(msg_import[1]))
88 return clsmembers[0][1]
def _get_msg_from_path(self, msg_path)
def execute(self, userdata)
def on_enter(self, userdata)
def __init__(self, topic, blocking=True, clear=False)