subscriber_state.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import rospy
3 import rostopic
4 import inspect
5 from flexbe_core import EventState, Logger
6 
7 from flexbe_core.proxy import ProxySubscriberCached
8 
9 '''
10 Created on 11.06.2013
11 
12 @author: Philipp Schillinger
13 '''
14 
15 class SubscriberState(EventState):
16  '''
17  Gets the latest message on the given topic and stores it to userdata.
18 
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.
23 
24  #> message object Latest message on the given topic of the respective type.
25 
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.
28 
29  '''
30 
31 
32  def __init__(self, topic, blocking = True, clear = False):
33  '''
34  Constructor
35  '''
36  super(SubscriberState, self).__init__(outcomes=['received', 'unavailable'],
37  output_keys=['message'])
38 
39  self._topic = topic
40  self._blocking = blocking
41  self._clear = clear
42  self._connected = False
43 
44  (msg_path, msg_topic, fn) = rostopic.get_topic_type(self._topic)
45 
46  if msg_topic == self._topic:
47  msg_type = self._get_msg_from_path(msg_path)
48  self._sub = ProxySubscriberCached({self._topic: msg_type})
49  self._connected = True
50  else:
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)))
52 
53 
54  def execute(self, userdata):
55  '''
56  Execute this state
57  '''
58  if not self._connected:
59  userdata.message = None
60  return 'unavailable'
61 
62  if self._sub.has_msg(self._topic) or not self._blocking:
63  userdata.message = self._sub.get_last_msg(self._topic)
64  self._sub.remove_last_msg(self._topic)
65  return 'received'
66 
67 
68  def on_enter(self, userdata):
69  if not self._connected:
70  (msg_path, msg_topic, fn) = rostopic.get_topic_type(self._topic)
71  if msg_topic == self._topic:
72  msg_type = self._get_msg_from_path(msg_path)
73  self._sub = ProxySubscriberCached({self._topic: msg_type})
74  self._connected = True
75  Logger.loginfo('Successfully subscribed to previously unavailable topic %s' % self._topic)
76  else:
77  Logger.logwarn('Topic %s still not available, giving up.' % self._topic)
78 
79  if self._connected and self._clear and self._sub.has_msg(self._topic):
80  self._sub.remove_last_msg(self._topic)
81 
82 
83  def _get_msg_from_path(self, msg_path):
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 __init__(self, topic, blocking=True, clear=False)


flexbe_states
Author(s): Philipp Schillinger
autogenerated on Wed Jun 5 2019 21:52:08