registration_loader.py
Go to the documentation of this file.
00001 #! /usr/bin/python
00002 
00003 import sys
00004 import numpy as np
00005 
00006 import roslib
00007 roslib.load_manifest('hrl_face_adls')
00008 import rospy
00009 import rosbag
00010 from std_msgs.msg import String
00011 from geometry_msgs.msg import Transform, Pose
00012 from tf import TransformBroadcaster
00013 
00014 from hrl_ellipsoidal_control.msg import EllipsoidParams
00015 from hrl_face_adls.srv import InitializeRegistration, InitializeRegistrationResponse
00016 from hrl_face_adls.srv import RequestRegistration, RequestRegistrationResponse
00017 from hrl_head_registration.srv import HeadRegistration
00018 from hrl_geom.pose_converter import PoseConv
00019 
00020 class RegistrationLoader(object):
00021     def __init__(self):
00022         self.head_reg_tf = None
00023         self.ell_frame = None
00024         self.ell_frame_bcast = TransformBroadcaster()
00025         self.init_reg_srv = rospy.Service("/initialize_registration", InitializeRegistration, 
00026                                           self.init_reg_cb)
00027         self.req_reg_srv = rospy.Service("/request_registration", RequestRegistration, 
00028                                           self.req_reg_cb)
00029         self.head_registration_r = rospy.ServiceProxy("/head_registration_r", HeadRegistration) # TODO
00030         self.head_registration_l = rospy.ServiceProxy("/head_registration_l", HeadRegistration) # TODO
00031         self.ell_params_pub = rospy.Publisher("/ellipsoid_params", EllipsoidParams, latch=True)
00032         self.feedback_pub = rospy.Publisher("/feedback", String)
00033 
00034     def publish_feedback(self, msg):
00035         rospy.loginfo("[registration_loader] %s" % msg)
00036         self.feedback_pub.publish(msg)
00037 
00038     def init_reg_cb(self, req):
00039         # TODO REMOVE THIS SHAVING SIDE MESS
00040         self.shaving_side = rospy.get_param("/shaving_side", 'r')
00041         if self.shaving_side == 'r':
00042             head_registration = self.head_registration_r
00043         else:
00044             head_registration = self.head_registration_l
00045         # TODO
00046 
00047         try:
00048             self.head_reg_tf = head_registration(req.u, req.v).tf_reg
00049         except:
00050             self.publish_feedback("Registration failed.")
00051             return None
00052 
00053         if self.shaving_side == 'r':
00054             self.publish_feedback("Registered head using right cheek model, please visually confirm.")
00055         else:
00056             self.publish_feedback("Registered head using left cheek model, please visually confirm.")
00057         rospy.loginfo('[registration loader] Head PC frame registered at:\r\n %s' %self.head_reg_tf)
00058         return InitializeRegistrationResponse()
00059 
00060     def req_reg_cb(self, req):
00061         reg_e_params = EllipsoidParams()
00062         if self.head_reg_tf is None:
00063             rospy.logwarn("[registration_loader] Head registration not loaded yet.")
00064             return RequestRegistrationResponse(False, reg_e_params)
00065         reg_prefix = rospy.get_param("~registration_prefix", "")
00066         registration_files = rospy.get_param("~registration_files", None)
00067         if req.mode not in registration_files:
00068             rospy.logerr("[registration_loader] Mode not in registration_files parameters")
00069             return RequestRegistrationResponse(False, reg_e_params)
00070 
00071         try:
00072             print reg_prefix + registration_files[req.mode][req.side]
00073             bag = rosbag.Bag(reg_prefix + registration_files[req.mode][req.side], 'r')
00074             e_params = None
00075             for topic, msg, ts in bag.read_messages():
00076                 e_params = msg
00077             assert e_params is not None
00078             bag.close()
00079         except:
00080             rospy.logerr("[registration_loader] Cannot load registration parameters from %s" %
00081                          registration_files[req.mode][req.side])
00082             return RequestRegistrationResponse(False, reg_e_params)
00083 
00084         head_reg_mat = PoseConv.to_homo_mat(self.head_reg_tf)
00085         ell_reg = PoseConv.to_homo_mat(Transform(e_params.e_frame.transform.translation,
00086                                                       e_params.e_frame.transform.rotation))
00087         reg_e_params.e_frame = PoseConv.to_tf_stamped_msg(head_reg_mat**-1 * ell_reg)
00088         reg_e_params.e_frame.header.frame_id = self.head_reg_tf.header.frame_id
00089         reg_e_params.e_frame.child_frame_id = '/ellipse_frame'
00090         reg_e_params.height = e_params.height
00091         reg_e_params.E = e_params.E
00092         self.ell_params_pub.publish(reg_e_params)
00093 
00094         self.ell_frame = reg_e_params.e_frame
00095         return RequestRegistrationResponse(True, reg_e_params)
00096 
00097 def main():
00098     rospy.init_node("registration_loader")
00099     rl = RegistrationLoader()
00100     rospy.spin()
00101         
00102 if __name__ == "__main__":
00103     main()


hrl_face_adls
Author(s): Kelsey Hawkins, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab
autogenerated on Wed Nov 27 2013 11:47:39