subscriber_state.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import rospy
3 import rostopic
4 from flexbe_core import EventState, Logger
5 
6 from flexbe_core.proxy import ProxySubscriberCached
7 
8 
9 class SubscriberState(EventState):
10  '''
11  Gets the latest message on the given topic and stores it to userdata.
12 
13  -- topic string The topic on which should be listened.
14  -- blocking bool Blocks until a message is received.
15  -- clear bool Drops last message on this topic on enter
16  in order to only handle message received since this state is active.
17 
18  #> message object Latest message on the given topic of the respective type.
19 
20  <= received Message has been received and stored in userdata or state is not blocking.
21  <= unavailable The topic is not available when this state becomes actives.
22  '''
23 
24  def __init__(self, topic, blocking=True, clear=False):
25  super(SubscriberState, self).__init__(outcomes=['received', 'unavailable'],
26  output_keys=['message'])
27  self._topic = topic
28  self._blocking = blocking
29  self._clear = clear
30  self._connected = False
31 
32  if not self._connect():
33  Logger.logwarn('Topic %s for state %s not yet available.\n'
34  'Will try again when entering the state...' % (self._topic, self.name))
35 
36  def execute(self, userdata):
37  if not self._connected:
38  userdata.message = None
39  return 'unavailable'
40 
41  if self._sub.has_msg(self._topic) or not self._blocking:
42  userdata.message = self._sub.get_last_msg(self._topic)
43  self._sub.remove_last_msg(self._topic)
44  return 'received'
45 
46  def on_enter(self, userdata):
47  if not self._connected:
48  if self._connect():
49  Logger.loginfo('Successfully subscribed to previously unavailable topic %s' % self._topic)
50  else:
51  Logger.logwarn('Topic %s still not available, giving up.' % self._topic)
52 
53  if self._connected and self._clear and self._sub.has_msg(self._topic):
54  self._sub.remove_last_msg(self._topic)
55 
56  def _connect(self):
57  global_topic = self._topic
58  if global_topic[0] != '/':
59  global_topic = rospy.get_namespace() + global_topic
60  msg_type, msg_topic, _ = rostopic.get_topic_class(global_topic)
61  if msg_topic == global_topic:
62  self._sub = ProxySubscriberCached({self._topic: msg_type})
63  self._connected = True
64  return True
65  return False
flexbe_states.subscriber_state.SubscriberState._clear
_clear
Definition: subscriber_state.py:29
flexbe_states.subscriber_state.SubscriberState._blocking
_blocking
Definition: subscriber_state.py:28
flexbe_states.subscriber_state.SubscriberState._connect
def _connect(self)
Definition: subscriber_state.py:56
flexbe_states.subscriber_state.SubscriberState._connected
_connected
Definition: subscriber_state.py:30
flexbe_states.subscriber_state.SubscriberState._topic
_topic
Definition: subscriber_state.py:27
flexbe_states.subscriber_state.SubscriberState.execute
def execute(self, userdata)
Definition: subscriber_state.py:36
flexbe_core::proxy
flexbe_states.subscriber_state.SubscriberState.on_enter
def on_enter(self, userdata)
Definition: subscriber_state.py:46
flexbe_states.subscriber_state.SubscriberState.__init__
def __init__(self, topic, blocking=True, clear=False)
Definition: subscriber_state.py:24
flexbe_states.subscriber_state.SubscriberState
Definition: subscriber_state.py:9
flexbe_states.subscriber_state.SubscriberState._sub
_sub
Definition: subscriber_state.py:62


flexbe_states
Author(s): Philipp Schillinger
autogenerated on Fri Jul 21 2023 02:26:17