monitoring_state.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import rospy
3 import smach
4 
5 from flexbe_core.proxy import ProxyPublisher, ProxySubscriberCached
6 from flexbe_msgs.msg import CommandFeedback, OutcomeRequest
7 from flexbe_core.logger import Logger
8 
9 from diagnostic_msgs.msg import DiagnosticArray, DiagnosticStatus
10 
11 
12 class MonitoringState(smach.State):
13  """
14  A state to monitor a custom set of conditions.
15  For each conditions, an outcome is added or mapped which will be returned if the condition is not met.
16  """
17 
18  def __init__(self, *args, **kwargs):
19  super(MonitoringState, self).__init__(*args, **kwargs)
20 
21  outcomes = kwargs.get('outcomes', [])
22  self._outcome_list = list(outcomes)
23  self._outcome_list.remove('loopback')
24 
25  self.name = None
26  self._parent = None
27  self._is_controlled = False
28  self._force_monitoring = False
29 
30  self._monitored_keys = dict()
31  self._sent_keys = list()
32  self._current_status = None
33 
34  self.__execute = self.execute
36 
37  self._diagnostics_topic = 'diagnostics_agg'
38  self._sub = ProxySubscriberCached()
39 
40 
41  def _monitoring_execute(self, *args, **kwargs):
42  new_status = None
43  had_warning = False
44  if (self._force_monitoring or self._is_controlled) and self._sub.has_buffered(self._diagnostics_topic):
45  new_status = ""
46  diag_msg = self._sub.get_from_buffer(self._diagnostics_topic)
47  for status in diag_msg.status:
48  if not status.name in self._monitored_keys.keys(): continue
49  if status.level == DiagnosticStatus.WARN:
50  had_warning = True
51  if not status.name + "_warn" in self._sent_keys:
52  self._sent_keys.append(status.name + "_warn")
53  Logger.logwarn("%s: %s" % (status.name, status.message))
54  if status.level == DiagnosticStatus.ERROR:
55  if not status.name + "_err" in self._sent_keys:
56  self._sent_keys.append(status.name + "_err")
57  Logger.logerr("%s: %s" % (status.name, status.message))
58  new_status = status.name
59 
60  if new_status == "":
61  self._current_status = None
62  new_status = None
63  if not had_warning:
64  self._sent_keys = list()
65 
66  if new_status is None or self._current_status == new_status:
67  return self.__execute(*args, **kwargs)
68 
69  self._current_status = new_status
70 
71  return self._monitored_keys[self._current_status]
72 
73 
74  def monitor(self, key, outcome = None):
75  oc = outcome if not outcome is None else key
76  self._monitored_keys[key] = oc
77  if not oc in self._outcomes:
78  self.register_outcomes([oc])
79  self._outcome_list.append(oc)
80 
81  def force_monitoring(self):
82  self._force_monitoring = True
83  if not self._is_controlled:
84  self._sub.subscribe(self._diagnostics_topic, DiagnosticArray)
85  self._sub.enable_buffer(self._diagnostics_topic)
86 
88  self._is_controlled = True
89  self._sub.subscribe(self._diagnostics_topic, DiagnosticArray)
90  self._sub.enable_buffer(self._diagnostics_topic)
91 
93  self._is_controlled = False
94  self._sub.unsubscribe_topic(self._diagnostics_topic)
95 
96 
97  def _get_path(self):
98  return self._parent._get_path() + "/" + self.name


flexbe_core
Author(s): Philipp Schillinger
autogenerated on Wed Jun 5 2019 21:51:59