Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 import copy
00019 import rospy
00020
00021 from cob_msgs.msg import EmergencyStopState
00022 from diagnostic_msgs.msg import DiagnosticArray
00023
00024 from simple_script_server import *
00025 sss = simple_script_server()
00026
00027 class AutoRecover():
00028
00029 def __init__(self):
00030 now = rospy.Time.now()
00031 self.em_state = 0
00032 self.recover_emergency = rospy.get_param('~recover_emergency', True)
00033 self.recover_diagnostics = rospy.get_param('~recover_diagnostics', True)
00034 self.components = rospy.get_param('~components', {})
00035 self.components_recover_time = {}
00036 for component in self.components.keys():
00037 self.components_recover_time[component] = now
00038 rospy.Subscriber("/emergency_stop_state", EmergencyStopState, self.em_cb, queue_size=1)
00039 rospy.Subscriber("/diagnostics_agg", DiagnosticArray, self.diagnostics_cb, queue_size=1)
00040
00041
00042 def em_cb(self, msg):
00043 if not self.recover_emergency:
00044 return
00045 if msg.emergency_state == 0 and self.em_state != 0:
00046 rospy.loginfo("auto_recover from emergency state")
00047 self.recover(self.components.keys())
00048 self.em_state = copy.deepcopy(msg.emergency_state)
00049
00050
00051 def diagnostics_cb(self, msg):
00052 if not self.recover_diagnostics:
00053 return
00054 for status in msg.status:
00055 for component in self.components.keys():
00056 if status.name.lower().startswith(self.components[component].lower()) and status.level > 0 and self.em_state == 0 and (rospy.Time.now() - self.components_recover_time[component] > rospy.Duration(10)):
00057 rospy.loginfo("auto_recover from diagnostic failure")
00058 self.recover([component])
00059
00060 def recover(self, components):
00061 for component in components:
00062 handle = sss.recover(component)
00063 if not (handle.get_error_code() == 0):
00064 rospy.logerr("[auto_recover]: Could not recover %s", component)
00065 else:
00066 rospy.loginfo("[auto_recover]: Component %s recovered successfully", component)
00067 self.components_recover_time[component] = rospy.Time.now()
00068
00069 if __name__ == "__main__":
00070 rospy.init_node("auto_recover")
00071 AR = AutoRecover()
00072 rospy.loginfo("auto recover running")
00073 rospy.spin()