mirror_pointer.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import math
00004 import numpy as np
00005 
00006 import roslib; 
00007 roslib.load_manifest('assistive_teleop')
00008 roslib.load_manifest('hrl_face_adls')
00009 import rospy
00010 from geometry_msgs.msg import PoseStamped, Quaternion, Point, PointStamped
00011 from tf import TransformListener, TransformBroadcaster, transformations as tft
00012 import pose_utils as pu
00013 from assistive_teleop.srv import PointMirror, PointMirrorResponse
00014 
00015 class MirrorPointer(object):
00016     def __init__(self):
00017         self.tf = TransformListener()
00018         self.tfb = TransformBroadcaster()
00019         self.active = True
00020         self.head_pose = PoseStamped()
00021         self.goal_pub = rospy.Publisher('goal_pose', PoseStamped)
00022         rospy.Subscriber('/head_center', PoseStamped, self.head_pose_cb)
00023         rospy.Service('/point_mirror', PointMirror, self.point_mirror_cb)
00024 
00025     def head_pose_cb(self, msg):
00026         """Save update head pose, transforming to torso frame if necessary"""
00027         msg.header.stamp = rospy.Time(0)
00028         if not (msg.header.frame_id.lstrip('/') == 'torso_lift_link'):
00029             self.head_pose = self.tf.transformPose('/torso_lift_link', msg)
00030         else:
00031             self.head_pose = msg
00032 
00033     def get_current_mirror_pose(self):
00034         """Get the current pose of the mirror (hardcoded relative to tool frame"""
00035         mp = PoseStamped()
00036         mp.header.frame_id = "/r_gripper_tool_frame"
00037         mp.pose.position = Point(0.15, 0, 0)
00038         mp.pose.orientation = Quaternion(0,0,0,1)
00039         mirror_pose = self.tf.transformPose("torso_lift_link", mp)
00040         return mirror_pose
00041 
00042     def get_pointed_mirror_pose(self):
00043         """Get the pose of the mirror pointet at the goal location"""
00044         target_pt = PointStamped(self.head_pose.header, self.head_pose.pose.position)
00045         mp = self.get_current_mirror_pose()
00046         pu.aim_pose_to(mp, target_pt, (0,1,0))
00047         return mp
00048 
00049     def trans_mirror_to_wrist(self, mp):
00050         """Get the wrist location from a mirror pose"""
00051         mp.header.stamp = rospy.Time(0)
00052         try:
00053             mp_in_mf = self.tf.transformPose('mirror',mp)
00054         except:
00055             return
00056         mp_in_mf.pose.position.x -= 0.15
00057         try:
00058             wp = self.tf.transformPose('torso_lift_link',mp_in_mf)
00059         except:
00060             return
00061         return wp
00062 
00063     def head_pose_sensible(self, ps):
00064         """Set a bounding box on reasonably expected head poses"""
00065         if ((ps.pose.position.x < 0.35) or
00066             (ps.pose.position.x > 1.0) or
00067             (ps.pose.position.y < -0.2) or
00068             (ps.pose.position.y > 0.85) or
00069             (ps.pose.position.z < -0.3) or
00070             (ps.pose.position.z > 1) ):
00071             return False
00072         else:
00073             return True
00074 
00075     def point_mirror_cb(self, req):
00076         rospy.loginfo("Mirror Adjust Request Received")
00077         if not self.head_pose_sensible(self.head_pose):
00078             rospy.logwarn("Registered Head Position outside of expected region: %s" %self.head_pose)
00079             return PoseStamped()
00080         mp = self.get_pointed_mirror_pose()
00081         goal = self.trans_mirror_to_wrist(mp)
00082         goal.header.stamp = rospy.Time.now()
00083         resp = PointMirrorResponse()
00084         resp.wrist_pose = goal
00085         #print "Head Pose: "
00086         #print self.head_pose
00087         self.goal_pub.publish(goal)
00088         return resp
00089 
00090     def broadcast_mirror_tf(self):
00091         self.tfb.sendTransform((0.15,0,0),
00092                                (0,0,0,1),
00093                                rospy.Time.now(),
00094                                "mirror",
00095                                "r_gripper_tool_frame")
00096 
00097 if __name__=='__main__':
00098     rospy.init_node('mirror_pointer')
00099     mp = MirrorPointer()
00100     rospy.sleep(1)
00101     r = rospy.Rate(10)
00102     while not rospy.is_shutdown():
00103         mp.broadcast_mirror_tf()
00104         mp.trans_mirror_to_wrist(mp.get_current_mirror_pose())


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