auto_recover.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00004 #
00005 # Licensed under the Apache License, Version 2.0 (the "License");
00006 # you may not use this file except in compliance with the License.
00007 # You may obtain a copy of the License at
00008 #
00009 #   http://www.apache.org/licenses/LICENSE-2.0
00010 #
00011 # Unless required by applicable law or agreed to in writing, software
00012 # distributed under the License is distributed on an "AS IS" BASIS,
00013 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 # See the License for the specific language governing permissions and
00015 # limitations under the License.
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   # auto recover based on emergency stop
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   # auto recover based on diagnostics
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()


cob_helper_tools
Author(s): Felix Messmer
autogenerated on Sun Jun 9 2019 20:20:17