run_study.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import roslib; roslib.load_manifest('projector_interface')
00003 from std_msgs.msg import ColorRGBA
00004 from pr2_python.pointclouds import pointcloud2_to_xyz_array, xyz_array_to_pointcloud2
00005 import datetime
00006 import scipy.io
00007 import os
00008 import numpy as np
00009 from itertools import chain
00010 
00011 from point_tests import sameObject
00012 
00013 load('rosh_robot', globals())
00014 
00015 while not rospy.has_param('/screen/height'):
00016     rospy.sleep(0.1)
00017 
00018 data_dir = rospy.get_param('/study_data_dir')
00019 
00020 width = float(rospy.get_param('/screen/width',))
00021 height = float(rospy.get_param('/screen/height'))
00022 
00023 border_x = float(rospy.get_param('/screen/border_x', 0))
00024 border_y = float(rospy.get_param('/screen/border_y', 0))
00025 
00026 xmin = 0 + border_x
00027 xmax = width - border_x
00028 ymin = 0 + border_y
00029 ymax = height - border_y
00030 
00031 services.clear_polygons()
00032 
00033 xs = np.linspace(xmin,xmax,4)
00034 ys = np.linspace(ymin,ymax,3)
00035 # xs = np.arange(xmin+0.05, xmax, abs(ys[1] - ys[0]))
00036 xx, yy = np.meshgrid(xs, ys)
00037 points = np.array([(x,y,0) for x,y in zip(xx.ravel(), yy.ravel())])
00038 points_msg = xyz_array_to_pointcloud2(points, now(), '/bottom_left')
00039 # topics.object_cloud(points_msg)
00040 services.set_selection_method(0)
00041 
00042 std_order = [
00043     0,  2,  1,  3,
00044     7,  5,  6,  4,
00045     8, 10,  9, 11,
00046     0,  4,  8 ,
00047     1,  5,  9 ,
00048     2,  6,  10,
00049     3,  7,  11
00050 ]
00051 
00052 
00053 condition = rospy.get_param('~condition')
00054 print condition
00055 if condition == 'standard':
00056     order = std_order
00057 if condition == 'random':
00058     import random
00059     # use sample instead of shuffle because shuffle is in-place
00060     order = random.sample(std_order, len(std_order))
00061 if condition == 'reversed':
00062     order = reversed(order)
00063 
00064 targets = []
00065 stds = []
00066 times = []
00067 cursor_history = []
00068 history_size = 0
00069 
00070 # sleep(2) # wait for the interface to be ready before trying to hilight
00071 
00072 start_time = now()
00073 for idx in order:
00074     topics.object_cloud(xyz_array_to_pointcloud2(np.array([points[idx]]), now(), '/bottom_left'))
00075     t1 = now()
00076 
00077     point = points[idx]
00078     targets.append(point)
00079     # print 'hilighting ', point
00080     # services.clear_hilights()
00081     
00082     pt = PointStamped()
00083     pt.header.frame_id = '/bottom_left'
00084     pt.header.stamp = now()
00085     pt.point.x, pt.point.y, pt.point.z = point
00086     
00087     # services.hilight_object(pt, ColorRGBA(255,0,0,0))
00088     # import pdb; pdb.set_trace()
00089     click_pts_msg = services.get_cursor_stats()
00090     click_pt = [
00091         click_pts_msg.click_pos.point.x,
00092         click_pts_msg.click_pos.point.y,
00093         click_pts_msg.click_pos.point.z
00094     ]
00095     r = Rate(10)
00096     print click_pt
00097     print point
00098     print '====='
00099 
00100     while not sameObject(click_pt, point):
00101         print click_pt
00102         print point
00103         print '====='
00104         click_pts_msg = services.get_cursor_stats()
00105         click_pt = [
00106             click_pts_msg.click_pos.point.x,
00107             click_pts_msg.click_pos.point.y,
00108             click_pts_msg.click_pos.point.z
00109         ]
00110         r.sleep()
00111     # services.clear_hilights()
00112     click_pts = pointcloud2_to_xyz_array(click_pts_msg.points)
00113     t2 = now()
00114     times.append((t2-t1).to_sec())
00115     history_size = click_pts.shape[0]
00116     cursor_history.extend(click_pts)
00117     stds.append(click_pts.std(0))
00118     print 'std = ', stds[-1]
00119     sleep(3)
00120         
00121 # services.clear_hilights()
00122 
00123 filename = '%s_%s.mat'
00124 path = os.path.join(data_dir, filename)
00125 timestr = datetime.datetime.today().strftime('%d-%m-%y-%H.%M.%f')
00126 filename = path % (condition, timestr)
00127 scipy.io.savemat(
00128     filename,
00129     dict(targets=targets,
00130         stds=stds,
00131         times=times,
00132         cursor_history=cursor_history,
00133         history_size=history_size,
00134         total_time=(now()-start_time).to_sec()
00135     )
00136 )
00137 print 'Saved\n', filename


projector_interface
Author(s): Daniel Lazewatsky
autogenerated on Mon Oct 6 2014 10:12:36