auto_init.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 rospy
00019 import tf
00020 
00021 from cob_msgs.msg import EmergencyStopState
00022 
00023 from simple_script_server import *
00024 sss = simple_script_server()
00025 
00026 class AutoInit():
00027 
00028   def __init__(self):
00029     self.components = rospy.get_param('~components', {})
00030     self.max_retries = rospy.get_param('~max_retries', 50)
00031     self.em_state = 1  # assume EMSTOP
00032     rospy.Subscriber("/emergency_stop_state", EmergencyStopState, self.em_cb, queue_size=1)
00033 
00034     # wait for all components to start
00035     for component in self.components.keys():
00036       rospy.loginfo("[auto_init]: Waiting for %s to start...", component)
00037       rospy.wait_for_service("/" + component + "/driver/init")
00038 
00039     # wait for emergency_stop to be released
00040     while not rospy.is_shutdown():
00041       if self.em_state == 1: # EMSTOP
00042         rospy.loginfo("[auto_init]: Waiting for emergency stop to be released...")
00043         try:
00044           rospy.sleep(1)
00045         except rospy.exceptions.ROSInterruptException as e:
00046           pass
00047       else: # EMFREE or EMCONFIRMED
00048         # call init for all components
00049         rospy.loginfo("[auto_init]: Initializing components")
00050         for component in self.components.keys():
00051           retries = 0
00052           while not rospy.is_shutdown():
00053             if retries >= self.max_retries:
00054               rospy.logerr("[auto_init]: Could not initialize %s after %s retries", component, str(retries))
00055               break
00056             retries += 1
00057             handle = sss.init(component)
00058             if not (handle.get_error_code() == 0):
00059               rospy.logerr("[auto_init]: Could not initialize %s. Retrying...(%s of %s)", component, str(retries), str(self.max_retries))
00060             else:
00061               rospy.loginfo("[auto_init]: Component %s initialized successfully", component)
00062               break
00063         break # done
00064 
00065   def em_cb(self, msg):
00066     self.em_state = msg.emergency_state
00067 
00068 if __name__ == "__main__":
00069   rospy.init_node("auto_init")
00070   rospy.loginfo("auto init running")
00071   AI = AutoInit()
00072   rospy.loginfo("auto init finished")


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