src/cob_auto_tools/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 
20 from cob_msgs.msg import EmergencyStopState
21 
22 from simple_script_server import *
24 
25 class AutoInit():
26 
27  def __init__(self):
28  self.components = rospy.get_param('~components', {})
29  self.max_retries = rospy.get_param('~max_retries', 50)
30  self.retry_delay = rospy.get_param('~retry_delay', 1.0)
31  self.em_state_ignore = rospy.get_param('~em_state_ignore', False)
32  self.em_state = EmergencyStopState.EMSTOP
33  rospy.Subscriber("/emergency_stop_state", EmergencyStopState, self.em_cb, queue_size=1)
34 
35  # wait for all components to start
36  for component in list(self.components.keys()):
37  rospy.loginfo("[auto_init]: Waiting for %s to start...", component)
38  rospy.wait_for_service("/" + component + "/driver/init")
39 
40  # wait for emergency_stop to be released
41  while not rospy.is_shutdown():
42  if not self.em_state_ignore and self.em_state == EmergencyStopState.EMSTOP:
43  rospy.loginfo("[auto_init]: Waiting for emergency stop to be released...")
44  try:
45  rospy.sleep(1)
46  except rospy.exceptions.ROSInterruptException as e:
47  pass
48  else: # EMFREE or EMCONFIRMED
49  # call init for all components
50  rospy.loginfo("[auto_init]: Initializing components")
51  for component in list(self.components.keys()):
52  retries = 0
53  while not rospy.is_shutdown():
54  if self.max_retries > 0 and retries >= self.max_retries:
55  rospy.logerr("[auto_init]: Could not initialize %s after %s retries", component, str(retries))
56  break
57  retries += 1
58  handle = sss.init(component)
59  if not (handle.get_error_code() == 0):
60  rospy.logerr("[auto_init]: Could not initialize %s. Retrying...(%s of %s, waiting for %.1f s to retry...)", component, str(retries), str(self.max_retries), self.retry_delay)
61  rospy.sleep(self.retry_delay)
62  else:
63  rospy.loginfo("[auto_init]: Component %s initialized successfully", component)
64  break
65  break # done
66 
67  def em_cb(self, msg):
68  self.em_state = msg.emergency_state
cob_auto_tools.auto_init.AutoInit.retry_delay
retry_delay
Definition: src/cob_auto_tools/auto_init.py:30
cob_auto_tools.auto_init.AutoInit.__init__
def __init__(self)
Definition: src/cob_auto_tools/auto_init.py:27
simple_script_server
cob_auto_tools.auto_init.AutoInit.components
components
Definition: src/cob_auto_tools/auto_init.py:28
cob_auto_tools.auto_init.AutoInit.em_cb
def em_cb(self, msg)
Definition: src/cob_auto_tools/auto_init.py:67
cob_auto_tools.auto_init.AutoInit.em_state_ignore
em_state_ignore
Definition: src/cob_auto_tools/auto_init.py:31
cob_auto_tools.auto_init.AutoInit.max_retries
max_retries
Definition: src/cob_auto_tools/auto_init.py:29
cob_auto_tools.auto_init.AutoInit.em_state
em_state
Definition: src/cob_auto_tools/auto_init.py:32
cob_auto_tools.auto_init.AutoInit
Definition: src/cob_auto_tools/auto_init.py:25


cob_helper_tools
Author(s): Felix Messmer
autogenerated on Fri Aug 2 2024 09:45:50