00001 import roslib; roslib.load_manifest('hai_sandbox')
00002 import rospy
00003 import cv
00004 import sys
00005 import hrl_lib.util as ut
00006 import hrl_lib.rutils as ru
00007 import sensor_msgs.msg as sm
00008 import pr2_msgs.msg as pm
00009 import hai_sandbox.features as fea
00010 import tf
00011 import tf.transformations as tr
00012 import hrl_lib.tf_utils as tfu
00013 import numpy as np
00014 import features as fea
00015 from cv_bridge import CvBridge, CvBridgeError
00016 import scipy.spatial as sp
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 class ListenAndFindContactLocs:
00028 def __init__(self):
00029
00030 rospy.Subscriber('/pressure/l_gripper_motor', pm.PressureState, self.lpress_cb)
00031 self.ftip_frames = ['r_gripper_l_finger_tip_link',
00032 'r_gripper_r_finger_tip_link',
00033 'l_gripper_l_finger_tip_link',
00034 'l_gripper_r_finger_tip_link']
00035
00036 self.tflistener = tf.TransformListener()
00037
00038 self.lmat0 = None
00039 self.rmat0 = None
00040 self.contact_locs = []
00041 self.base_frame = '/base_footprint'
00042
00043 self.contact = False
00044 self.contact_stopped = False
00045 self.pointcloud_transform = None
00046
00047
00048
00049 def lpress_cb(self, pmsg):
00050
00051
00052 lmat = np.matrix((pmsg.l_finger_tip)).T
00053 rmat = np.matrix((pmsg.r_finger_tip)).T
00054 if self.lmat0 == None:
00055 self.lmat0 = lmat
00056 self.rmat0 = rmat
00057 return
00058
00059
00060 lmat = lmat - self.lmat0
00061 rmat = rmat - self.rmat0
00062
00063
00064 if np.any(np.abs(lmat) > 250) or np.any(np.abs(rmat) > 250):
00065
00066 def frame_loc(from_frame):
00067 p_base = tfu.transform(self.base_frame, from_frame, self.tflistener) \
00068 * tfu.tf_as_matrix(([0., 0., 0., 1.], tr.quaternion_from_euler(0,0,0)))
00069 return tfu.matrix_as_tf(p_base)
00070 tip_locs = [frame_loc(n)[0] for n in self.ftip_frames]
00071 t = pmsg.header.stamp.to_time()
00072 rospy.loginfo("contact detected at %.3f" % t)
00073 self.contact_locs.append([t, tip_locs])
00074 self.contact = True
00075 else:
00076 if self.contact == True:
00077 print 'contact stopped'
00078 self.contact_stopped = True
00079 self.contact = False
00080
00081
00082
00083
00084
00085 if __name__ == '__main__':
00086 import sys
00087
00088 proc_img_name = sys.argv[1]
00089 pickle_name = sys.argv[2]
00090
00091 data_dict = ut.load_pickle(pickle_name)
00092 proc_cam_info = ut.load_pickle('prosilica_caminfo.pkl')
00093
00094 rospy.init_node('prosilica_set_view')
00095 points_pub = rospy.Publisher('/pc_snap_shot', sm.PointCloud)
00096 touchll_pub = rospy.Publisher('touch_ll', sm.PointCloud)
00097 touchlr_pub = rospy.Publisher('touch_lr', sm.PointCloud)
00098 proc_pub = rospy.Publisher('/prosilica/image_rect_color', sm.Image)
00099 cam_info = rospy.Publisher('/prosilica/camera_info', sm.CameraInfo)
00100
00101 print 'pointcloud frame', data_dict['points'].header.frame_id
00102 point_cloud = ru.pointcloud_to_np(data_dict['points'])
00103
00104
00105 try:
00106 print 'loading contact_locs_proc.pkl'
00107 contact_locs = ut.load_pickle('contact_locs_proc.pkl')
00108 except Exception, e:
00109 print e
00110 print 'listening'
00111 et = ListenAndFindContactLocs()
00112 r = rospy.Rate(10)
00113 while not rospy.is_shutdown() and not et.contact_stopped:
00114 r.sleep()
00115 contact_locs = et.contact_locs
00116 ut.save_pickle(et.contact_locs, 'contact_locs_proc.pkl')
00117
00118
00119 print 'detecting features'
00120 proc_img = cv.LoadImage(proc_img_name)
00121 proc_gray = fea.grayscale(proc_img)
00122 sloc, sdesc = fea.surf(proc_gray)
00123 proc_surfed = fea.draw_surf(proc_img, sloc, (200, 0, 0))
00124
00125
00126
00127
00128
00129
00130 point_cloud_pro = data_dict['pro_T_bf'] * np.row_stack((point_cloud, 1+np.zeros((1, point_cloud.shape[1]))))
00131
00132 point_cloud_2d = data_dict['camera_info'].project(point_cloud_pro[0:3,:])
00133 point_cloud_2d_tree = sp.KDTree(np.array(point_cloud_2d.T))
00134
00135
00136 surf_loc3d = []
00137 for loc, lap, size, d, hess in sloc:
00138 idx = point_cloud_2d_tree.query(np.array(loc))[1]
00139 surf_loc3d.append(point_cloud[:, idx])
00140 surf_loc3d = np.column_stack(surf_loc3d)
00141 surf_loc_tree_bf = sp.KDTree(np.array(surf_loc3d.T))
00142
00143
00144 left_contact, right_contact = zip(*[(np.matrix(r[1][2]).T, np.matrix(r[1][3]).T) for r in contact_locs])
00145 left_contact = np.column_stack(left_contact)
00146 right_contact = np.column_stack(right_contact)
00147 mid_contact_bf = (left_contact[:,0] + right_contact[:,0]) / 2.
00148
00149
00150 surf_closest_idx = surf_loc_tree_bf.query(np.array(mid_contact_bf.T))[1]
00151 surf_closest3d = surf_loc3d[:, surf_closest_idx]
00152 surf_closest_fea = sloc[surf_closest_idx]
00153
00154
00155 proc_surfed = fea.draw_surf(proc_surfed, [surf_closest_fea], (0,0,255))
00156 import pdb
00157 pdb.set_trace()
00158 cv.SaveImage('proc_surfed.png', proc_surfed )
00159
00160 bridge = CvBridge()
00161 image_message = bridge.cv_to_imgmsg(proc_surfed, "bgr8")
00162
00163 print 'init for viz'
00164
00165
00166 left_con_pc = ru.np_to_pointcloud(left_contact, '/base_footprint')
00167 right_con_pc = ru.np_to_pointcloud(right_contact, '/base_footprint')
00168
00169 print 'publishing to rviz'
00170 r = rospy.Rate(2)
00171 while not rospy.is_shutdown():
00172 points_pub.publish(data_dict['points'])
00173 touchll_pub.publish(left_con_pc)
00174 touchlr_pub.publish(right_con_pc)
00175 proc_pub.publish(image_message)
00176 cam_info.publish(proc_cam_info)
00177 r.sleep()
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199