calibrate.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2008, Willow Garage, Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of the Willow Garage nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 #
34 
35 # Author: Stuart Glaser
36 
37 from __future__ import with_statement
38 
39 import roslib
40 import copy
41 import threading
42 import sys, os
43 from time import sleep
44 
45 # Loads interface with the robot.
46 roslib.load_manifest('pr2_bringup')
47 import rospy
48 from std_msgs.msg import *
49 from pr2_mechanism_msgs.srv import LoadController, UnloadController, SwitchController, SwitchControllerRequest
50 from std_msgs.msg import Bool
51 
52 load_controller = rospy.ServiceProxy('pr2_controller_manager/load_controller', LoadController)
53 unload_controller = rospy.ServiceProxy('pr2_controller_manager/unload_controller', UnloadController)
54 switch_controller = rospy.ServiceProxy('pr2_controller_manager/switch_controller', SwitchController)
55 
56 def calibrate(controllers):
57 
58  success = True
59 
60  if type(controllers) is not list:
61  controllers = [controllers]
62 
63  launched = []
64  try:
65  # Loads the controllers
66  for c in controllers:
67  resp = load_controller(c)
68  if resp.ok == 0:
69  rospy.logerr("Failed: %s" % c)
70  success = False
71  else:
72  launched.append(c)
73  print("Launched: %s" % ', '.join(launched))
74 
75  # Starts the launched controllers
76  switch_controller(launched, [], SwitchControllerRequest.BEST_EFFORT)
77 
78  # Sets up callbacks for calibration completion
79  waiting_for = launched[:]
80  def calibrated(msg, name): # Somewhat not thread-safe
81  if name in waiting_for:
82  waiting_for.remove(name)
83  for name in waiting_for:
84  rospy.Subscriber("%s/calibrated" % name, Empty, calibrated, name)
85 
86  # Waits until all the controllers have calibrated
87  while waiting_for and not rospy.is_shutdown():
88  print("Waiting for: %s" % ', '.join(waiting_for))
89  sleep(0.5)
90  finally:
91  for name in launched:
92  try:
93  resp_stop = switch_controller([], [name], SwitchControllerRequest.STRICT)
94  if (resp_stop == 0):
95  rospy.logerr("Failed to stop controller %s" % name)
96  resp_unload = unload_controller(name)
97  if (resp_unload == 0):
98  rospy.logerr("Failed to unload controller %s" % name)
99  except Exception as ex:
100  rospy.logerr("Failed to stop/unload controller %s" % name)
101  return success
102 
104  class is_calibrated_helper:
105  def __init__(self):
106  self.is_calibrated = False
107  self.cond = threading.Condition()
108 
109  def callback(self, msg):
110  if msg.data:
111  with self.cond:
112  self.is_calibrated = True
113  self.cond.notify()
114 
115  def wait_for_calibrated(self, topic, timeout):
116  self.sub = rospy.Subscriber(topic,Bool,self.callback)
117  try:
118  with self.cond:
119  if not self.is_calibrated:
120  self.cond.wait(timeout)
121  return self.is_calibrated
122  finally:
123  self.sub.unregister()
124  return self.is_calibrated
125 
126  print("Waiting up to 20s for IMU calibration to complete.")
127  helper = is_calibrated_helper()
128  if not helper.wait_for_calibrated("torso_lift_imu/is_calibrated", 20):
129  rospy.logerr("IMU took too long to calibrate.")
130  return False
131  return True
132 
133 def main():
134  pub_calibrated = rospy.Publisher('calibrated', Bool, latch=True, queue_size=10)
135  rospy.wait_for_service('pr2_controller_manager/load_controller')
136  rospy.wait_for_service('pr2_controller_manager/switch_controller')
137  rospy.wait_for_service('pr2_controller_manager/unload_controller')
138  if rospy.is_shutdown(): return
139 
140  rospy.init_node('calibration', anonymous=True)
141  pub_calibrated.publish(False)
142 
143  # Don't calibrate the IMU unless ordered to by user
144  cal_imu = rospy.get_param('calibrate_imu', False)
145 
146  if cal_imu:
147  imustatus = calibrate_imu()
148  else:
149  imustatus = True
150 
151  xml = ''
152 
153  controllers = rospy.myargv()[1:]
154 
155  if not calibrate(controllers):
156  sys.exit(3)
157 
158  pub_calibrated.publish(True)
159 
160  if not imustatus:
161  print("Mechanism calibration complete, but IMU calibration failed.")
162  else:
163  print("Calibration complete")
164 
165  rospy.spin()
166 
167 if __name__ == '__main__':
168  main()
def main()
Definition: calibrate.py:133
def calibrate(controllers)
Definition: calibrate.py:56
def calibrate_imu()
Definition: calibrate.py:103
unload_controller
Definition: calibrate.py:53
load_controller
Definition: calibrate.py:52
switch_controller
Definition: calibrate.py:54


pr2_bringup
Author(s): Wim Meeussen
autogenerated on Tue Jun 1 2021 02:50:59