40 roslib.load_manifest(
'pr2_bringup')
46 from pr2_mechanism_msgs.srv
import LoadController, UnloadController, SwitchController, SwitchControllerRequest, ListControllers
47 from diagnostic_msgs.msg
import DiagnosticArray, DiagnosticStatus, KeyValue
48 from pr2_controllers_msgs.srv
import QueryCalibrationState
50 from std_srvs.srv
import Empty
55 calibration_params_namespace =
"calibration_controllers" 56 load_controller = rospy.ServiceProxy(
'pr2_controller_manager/load_controller', LoadController)
57 unload_controller = rospy.ServiceProxy(
'pr2_controller_manager/unload_controller', UnloadController)
58 switch_controller = rospy.ServiceProxy(
'pr2_controller_manager/switch_controller', SwitchController)
59 list_controllers = rospy.ServiceProxy(
'pr2_controller_manager/list_controllers', ListControllers)
61 hold_position = {
'r_shoulder_pan': -0.7,
'l_shoulder_pan': 0.7,
'r_elbow_flex': -2.0,
62 'l_elbow_flex': -2.0,
'r_upper_arm_roll': 0.0,
'l_upper_arm_roll': 0.0,
63 'r_shoulder_lift': 1.0,
'l_shoulder_lift': 1.0}
65 force_calibration =
False 68 return calibration_params_namespace+
"/calibrate/cal_%s" % joint_name
71 rospy.set_param(calibration_params_namespace+
"/calibrate/cal_%s/force_calibration" % joint_name,
True)
74 return "%s/hold/%s_position_controller" % (calibration_params_namespace, joint_name)
79 global last_joint_states
80 last_joint_states =
None 82 global last_joint_states
83 last_joint_states = msg
84 rospy.Subscriber(
'joint_states', JointState, joint_states_cb)
90 motors_halted = msg.data
91 rospy.logdebug(
"motors halted = %d"%motors_halted)
92 rospy.Subscriber(
'pr2_ethercat/motors_halted', Bool, motor_state_cb)
94 pub_diag = rospy.Publisher(
'/diagnostics', DiagnosticArray, queue_size=10)
97 rospy.loginfo(msg_long)
99 rospy.logwarn(msg_long)
101 rospy.logerr(msg_long)
102 d = DiagnosticArray()
103 d.header.stamp = rospy.Time.now()
104 ds = DiagnosticStatus()
106 ds.message = msg_long
116 rospy.logdebug(
"Loading holding controller: %s" %
get_holding_name(joint_name))
119 rospy.logdebug(
"Starting holding controller for joint %s."%joint_name)
123 rospy.logerr(
"Failed to load holding controller for joint %s."%joint_name)
124 raise Exception(
'Failure to load holding controller')
129 self.pub_cmd.unregister()
132 self.pub_cmd.publish(Float64(position))
138 self.
pub_status = rospy.Publisher(
'calibration_status', String, queue_size=10)
140 self.
status[
'active'] = []
145 self.
status[
'active'] = active
148 self.
status[
'active'] = []
151 str +=
"Calibrating: %s\n"%
", ".join(self.
status[
"active"])
152 str +=
"Calibrated: %s\n"%
", ".join(self.
status[
"done"])
153 self.pub_status.publish(str)
167 global force_calibration
168 if force_calibration:
176 self.joints.append(j)
178 rospy.logerr(
"Failed to load calibration for joint %s. Skipping this joint"%j)
192 if self.
services[j]().is_calibrated:
193 rospy.logdebug(
"joint %s is already calibrated"%j)
195 rospy.logdebug(
"joint %s needs to be calibrated"%j)
202 rospy.logdebug(
"Start calibrating joints %s"%self.
joints)
206 self.status.publish(self.
joints)
207 start_time = rospy.Time.now()
209 if motors_halted
and motors_halted == 1:
210 diagnostics(2,
'Calibration on hold',
'Calibration is on hold because motors are halted. Enable the run-stop')
211 start_time = rospy.Time.now()
213 elif rospy.Time.now() > start_time + rospy.Duration(30.0):
214 diagnostics(2,
'Calibration stuck',
'Joint %s is taking a long time to calibrate. It might be stuck and need some human help'%self.
joints)
218 rospy.logdebug(
"Stop calibration controllers for joints %s"%self.
joints)
222 rospy.logdebug(
"Loading holding controllers for joints %s"%self.
joints)
225 if j
in hold_position:
227 holder.hold(hold_position[j])
228 self.hold_controllers.append(holder)
229 self.status.publish()
246 if not g.is_calibrated():
267 rospy.init_node(
'calibration', anonymous=
True, disable_signals=
True)
268 calibration_start_time = rospy.Time.now()
270 rospy.wait_for_service(
'pr2_controller_manager/load_controller')
271 rospy.wait_for_service(
'pr2_controller_manager/switch_controller')
272 rospy.wait_for_service(
'pr2_controller_manager/unload_controller')
276 allowed_flags = [
'alpha-casters',
'alpha-head',
'alpha2b-head',
'arms=',
'force_calibration',
'recalibrate']
277 opts, args = getopt.gnu_getopt(rospy.myargv(),
'h', allowed_flags)
278 caster_list = [
'caster_fl',
'caster_fr',
'caster_bl',
'caster_br']
279 head_list = [
'head_pan',
'head_tilt']
282 global force_calibration
285 rospy.loginfo(
"Flags:",
' '.join([
'--'+f
for f
in allowed_flags]))
287 elif o ==
'--alpha-casters':
288 caster_list = [
'caster_fl_alpha2',
'caster_fr_alpha2',
'caster_bl_alpha2',
'caster_br_alpha2']
289 elif o ==
'--alpha-head':
290 head_list = [
'head_pan_alpha2',
'head_tilt']
291 elif o ==
'--alpha2b-head':
292 head_list = [
'head_pan_alpha2',
'head_tilt_alpha2b']
295 elif o ==
'--force_calibration':
296 force_calibration =
True 297 elif o ==
'--recalibrate':
299 force_calibration =
True 301 if arms
not in [
'both',
'none',
'left',
'right',
'auto']:
302 print(
'Arms must be "both", "none", "left", "right", or "auto"')
306 rospy.loginfo(
"Loading controller configuration on parameter server...")
307 pr2_controller_configuration_dir = roslib.packages.get_pkg_dir(
'pr2_controller_configuration')
308 calibration_yaml =
'%s/pr2_calibration_controllers.yaml' % pr2_controller_configuration_dir
309 hold_yaml =
'%s/pr2_joint_position_controllers.yaml' % pr2_controller_configuration_dir
311 rospy.loginfo(
"No yaml files specified for calibration and holding controllers, using defaults")
313 calibration_yaml = args[1]
315 rospy.set_param(calibration_params_namespace+
"/calibrate", yaml.load(open(calibration_yaml)))
316 rospy.set_param(calibration_params_namespace+
"/hold", yaml.load(open(hold_yaml)))
320 joints_status =
False 322 pub_calibrated = rospy.Publisher(
'calibrated', Bool, latch=
True, queue_size=10)
323 pub_calibrated.publish(
False)
328 started_waiting = rospy.get_rostime()
329 global last_joint_states
330 while rospy.get_rostime() <= started_waiting + rospy.Duration(50.0):
331 if last_joint_states:
333 js = last_joint_states
335 rospy.logwarn(
'Could not do auto arm selection because no joint state was received')
338 if 'r_shoulder_pan_joint' in js.name
and 'l_shoulder_pan_joint' in js.name:
340 elif 'r_shoulder_pan_joint' in js.name:
342 elif 'l_shoulder_pan_joint' in js.name:
346 rospy.loginfo(
"Arm selection was set to \"auto\". Chose to calibrate using \"%s\"" % arms)
353 arm_list = [[
'r_shoulder_pan',
'l_shoulder_pan'], [
'r_elbow_flex',
'l_elbow_flex'],
354 [
'r_upper_arm_roll',
'l_upper_arm_roll'], [
'r_shoulder_lift',
'l_shoulder_lift'],
355 [
'r_forearm_roll',
'r_wrist',
'l_forearm_roll',
'l_wrist']]
356 gripper_list = [
'r_gripper',
'l_gripper']
358 arm_list = [[
'l_shoulder_pan'], [
'l_elbow_flex'], [
'l_upper_arm_roll'], [
'l_shoulder_lift'], [
'l_forearm_roll',
'l_wrist']]
359 gripper_list = [
'l_gripper']
361 arm_list = [[
'r_shoulder_pan'], [
'r_elbow_flex'], [
'r_upper_arm_roll'], [
'r_shoulder_lift'], [
'r_forearm_roll',
'r_wrist']]
362 gripper_list = [
'r_gripper']
371 def is_running(c) :
return c[1]==
'running' 372 running_controllers = [c[0]
for c
in filter(is_running, zip(controller_list.controllers, controller_list.state))]
373 print(
"Running controllers : ", running_controllers)
374 if not switch_controller([], running_controllers, SwitchControllerRequest.STRICT):
375 print(
"Failed to stop controllers")
379 if not torso.is_calibrated():
380 rospy.loginfo(
'Calibrating imu')
381 status.publish([
'imu'])
382 imu_calibrate_srv_name =
'torso_lift_imu/calibrate' 383 rospy.wait_for_service(imu_calibrate_srv_name)
384 imu_calibrate_srv = rospy.ServiceProxy(imu_calibrate_srv_name, Empty)
391 rospy.loginfo(
'Calibrating imu finished')
393 rospy.loginfo(
'Not calibrating imu')
400 if not arm.is_calibrated():
402 torso_holder.hold(0.25)
404 rospy.loginfo(
'Moving up spine to allow arms to calibrate')
406 rospy.loginfo(
'Moving down spine after arm calibration')
407 torso_holder.hold(0.01)
418 except Exception
as e:
419 rospy.logerr(
"Calibration failed: %s" % str(e))
422 rospy.loginfo(
"Bringing down calibration node")
424 rospy.set_param(calibration_params_namespace,
"")
432 if not imustatus
and not joints_status:
433 rospy.logerr(
"Both mechanism and IMU calibration failed")
434 elif not joints_status:
435 rospy.logerr(
"IMU calibration complete, but mechanism calibration failed")
437 rospy.logerr(
"Mechanism calibration complete, but IMU calibration failed.")
439 rospy.loginfo(
'Calibration completed in %f sec' %(rospy.Time.now() - calibration_start_time).to_sec())
442 if not switch_controller(running_controllers, [], SwitchControllerRequest.STRICT):
443 print(
"Could not start previously running controllers")
447 pub_calibrated.publish(
True)
451 if __name__ ==
'__main__':
main()
def __init__(self, joint_name)
def set_force_calibration(joint_name)
def __init__(self, joints, status)
def get_controller_name(joint_name)
def get_holding_name(joint_name)
def publish(self, active=None)
def __init__(self, sequence, status)
def get_service_name(joint_name)
def diagnostics(level, msg_short, msg_long)