auto_init.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 
18 import rospy
19 import tf
20 
21 from cob_msgs.msg import EmergencyStopState
22 
23 from simple_script_server import *
25 
26 class AutoInit():
27 
28  def __init__(self):
29  self.components = rospy.get_param('~components', {})
30  self.max_retries = rospy.get_param('~max_retries', 50)
31  self.em_state = 1 # assume EMSTOP
32  rospy.Subscriber("/emergency_stop_state", EmergencyStopState, self.em_cb, queue_size=1)
33 
34  # wait for all components to start
35  for component in list(self.components.keys()):
36  rospy.loginfo("[auto_init]: Waiting for %s to start...", component)
37  rospy.wait_for_service("/" + component + "/driver/init")
38 
39  # wait for emergency_stop to be released
40  while not rospy.is_shutdown():
41  if self.em_state == 1: # EMSTOP
42  rospy.loginfo("[auto_init]: Waiting for emergency stop to be released...")
43  try:
44  rospy.sleep(1)
45  except rospy.exceptions.ROSInterruptException as e:
46  pass
47  else: # EMFREE or EMCONFIRMED
48  # call init for all components
49  rospy.loginfo("[auto_init]: Initializing components")
50  for component in list(self.components.keys()):
51  retries = 0
52  while not rospy.is_shutdown():
53  if retries >= self.max_retries:
54  rospy.logerr("[auto_init]: Could not initialize %s after %s retries", component, str(retries))
55  break
56  retries += 1
57  handle = sss.init(component)
58  if not (handle.get_error_code() == 0):
59  rospy.logerr("[auto_init]: Could not initialize %s. Retrying...(%s of %s)", component, str(retries), str(self.max_retries))
60  else:
61  rospy.loginfo("[auto_init]: Component %s initialized successfully", component)
62  break
63  break # done
64 
65  def em_cb(self, msg):
66  self.em_state = msg.emergency_state
67 
68 if __name__ == "__main__":
69  rospy.init_node("auto_init")
70  rospy.loginfo("auto init running")
71  AI = AutoInit()
72  rospy.loginfo("auto init finished")
def __init__(self)
Definition: auto_init.py:28
def em_cb(self, msg)
Definition: auto_init.py:65


cob_helper_tools
Author(s): Felix Messmer
autogenerated on Wed Apr 7 2021 03:03:09