37 from __future__
import with_statement
43 from time
import sleep
46 roslib.load_manifest(
'pr2_bringup')
49 from pr2_mechanism_msgs.srv
import LoadController, UnloadController, SwitchController, SwitchControllerRequest
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)
60 if type(controllers)
is not list:
61 controllers = [controllers]
69 rospy.logerr(
"Failed: %s" % c)
73 print(
"Launched: %s" %
', '.join(launched))
79 waiting_for = launched[:]
80 def calibrated(msg, name):
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)
87 while waiting_for
and not rospy.is_shutdown():
88 print(
"Waiting for: %s" %
', '.join(waiting_for))
95 rospy.logerr(
"Failed to stop controller %s" % 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)
104 class is_calibrated_helper:
106 self.is_calibrated =
False 107 self.cond = threading.Condition()
109 def callback(self, msg):
112 self.is_calibrated =
True 115 def wait_for_calibrated(self, topic, timeout):
116 self.sub = rospy.Subscriber(topic,Bool,self.callback)
119 if not self.is_calibrated:
120 self.cond.wait(timeout)
121 return self.is_calibrated
123 self.sub.unregister()
124 return self.is_calibrated
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.")
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 140 rospy.init_node(
'calibration', anonymous=
True)
141 pub_calibrated.publish(
False)
144 cal_imu = rospy.get_param(
'calibrate_imu',
False)
153 controllers = rospy.myargv()[1:]
158 pub_calibrated.publish(
True)
161 print(
"Mechanism calibration complete, but IMU calibration failed.")
163 print(
"Calibration complete")
167 if __name__ ==
'__main__':
def calibrate(controllers)