taxel_array_visualization.py
Go to the documentation of this file.
00001 
00002 import numpy as np, math
00003 from enthought.mayavi import mlab
00004 import matplotlib.pyplot as pp
00005 import matplotlib.cm as cm
00006 
00007 import scipy.ndimage as ni
00008 
00009 import roslib; roslib.load_manifest('darpa_m3')
00010 import rospy
00011 import hrl_lib.mayavi2_util as mu
00012 import hrl_lib.util as ut
00013 import hrl_lib.matplotlib_util as mpu
00014 
00015 
00016 def plot_taxel_locations(ta):
00017     pts = np.matrix(np.row_stack([ta.centers_x, ta.centers_y, ta.centers_z]))
00018     mu.plot(pts)
00019 
00020 def plot_taxel_array(ta):
00021     pts = np.matrix(np.row_stack([ta.centers_x, ta.centers_y, ta.centers_z]))
00022     forces = np.matrix(np.row_stack([ta.values_x, ta.values_y, ta.values_z]))
00023     mu.plot_quiver(pts, forces)
00024     mu.plot_points(pts[:,::24], (0., 0., 1.), 0.002, mode='sphere')
00025     
00026 # desired_unroll_index of 0 roughly corresponds to the middle of the
00027 # skin PCB box. (Tapo verified this and Advait is putting in the
00028 # comment and setting the default parameter to 0)
00029 def taxel_array_to_np_arrays(ta, desired_unroll_index=0):
00030     force_vectors = np.row_stack([ta.values_x, ta.values_y, ta.values_z])
00031     fmags = ut.norm(force_vectors)
00032     force_arr_raw = fmags.reshape((16,24)) #Verified by Tapo
00033 
00034     force_arr = np.column_stack((force_arr_raw[:,desired_unroll_index:],force_arr_raw[:,:desired_unroll_index]))
00035 
00036     center_arr = np.row_stack([ta.centers_x, ta.centers_y, ta.centers_z]).reshape((3,16,24))
00037     nrml_arr = np.row_stack([ta.normals_x, ta.normals_y, ta.normals_z]).reshape((3,16,24))
00038     return force_arr, center_arr, nrml_arr
00039 
00040 def plot_taxel_array_as_image(ta):
00041     index = 0 #Verified by Tapo
00042     forces_arr = taxel_array_to_np_arrays(ta,index)[0]
00043     pp.imshow(forces_arr, interpolation='nearest', cmap=cm.binary,
00044               origin='upper', vmin=0, vmax=1)
00045     pp.title('Unrolled Taxel Array')
00046     pp.xlabel('Along the circumference')
00047     pp.ylabel('Along the forearm')
00048     pp.xlim(-0.5,23.5)
00049     pp.ylim(15.5, -0.5)
00050 
00051 def compute_contact_regions(arr_2d, threshold):
00052     mask = arr_2d > threshold
00053     label_im, nb_labels = ni.label(mask)
00054     return label_im, nb_labels
00055 
00056 # force_arr - 2D array of contact force magnitude. adjacency of taxels
00057 # is the same as adjacency in the 2D array.
00058 def compute_resultant_force_magnitudes(force_arr, label_im, nb_labels):
00059     total_forces = ni.sum(force_arr, label_im, range(1, nb_labels + 1))
00060     if total_forces.__class__ != list:
00061         total_forces = [total_forces]
00062     return total_forces
00063 
00064 # cx_arr - 2D array of x coordinates of the centers of the taxels.
00065 # adjacency of taxels is the same as adjacency in the 2D array.
00066 def compute_centers_of_pressure(cx_arr, cy_arr, cz_arr, label_im,
00067                                 nb_labels):
00068     # for the forearm skin patch, it might be more accurate to perform
00069     # the averaging in cylindrical coordinates (to ensure that the COP
00070     # lies on the surface of the skin), but Advait does not care
00071     # enough.
00072     cx = ni.mean(cx_arr, label_im, range(1, nb_labels + 1))
00073     cy = ni.mean(cy_arr, label_im, range(1, nb_labels + 1))
00074     cz = ni.mean(cz_arr, label_im, range(1, nb_labels + 1))
00075     if cx.__class__!= list:
00076         cx = [cx]
00077         cy = [cy]
00078         cz = [cz]
00079     return cx, cy, cz
00080 
00081 def compute_resultant_force_directions(nx_arr, ny_arr, nz_arr,
00082                                        label_im, nb_labels):
00083     nx = ni.mean(nx_arr, label_im, range(1, nb_labels + 1))
00084     ny = ni.mean(ny_arr, label_im, range(1, nb_labels + 1))
00085     nz = ni.mean(nz_arr, label_im, range(1, nb_labels + 1))
00086     if nx.__class__!= list:
00087         nx = [nx]
00088         ny = [ny]
00089         nz = [nz]
00090     return nx, ny, nz
00091 
00092 
00093 if __name__ == '__main__':
00094     ta = ut.load_pickle('taxel_array.pkl')
00095 
00096     if False:
00097         # test to ensure that unrolling taxels into an image still
00098         # preverves adjacency.
00099         ta.values_x = list(ta.values_x)
00100         ta.values_x[0] = 1.
00101         ta.values_x[1] = 1.
00102         ta.values_x[23] = 1.
00103 
00104         ta.values_y = list(ta.values_y)
00105         ta.values_y[0] = 1.
00106         ta.values_y[1] = 1.
00107         ta.values_y[23] = 1.
00108 
00109     if False:
00110         # sanity check that taxel centers are along a cylinder.
00111         plot_taxel_locations(ta)
00112 
00113     if True:
00114         plot_taxel_array(ta)
00115         #mu.show()
00116 
00117     if True:
00118         force_arr, center_arr, nrml_arr = taxel_array_to_np_arrays(ta)
00119         label_im, nb_labels = compute_contact_regions(force_arr, 0.01)
00120 
00121         forces = compute_resultant_force_magnitudes(force_arr,
00122                                                     label_im,
00123                                                     nb_labels)
00124 
00125         cx_arr = center_arr[0]
00126         cy_arr = center_arr[1]
00127         cz_arr = center_arr[2]
00128 
00129         cx, cy, cz = compute_centers_of_pressure(cx_arr, cy_arr,
00130                                                  cz_arr, label_im,
00131                                                  nb_labels)
00132         cop_mat = np.matrix(np.row_stack((cx, cy, cz)))
00133 
00134         nx_arr = nrml_arr[0,:,:]
00135         ny_arr = nrml_arr[1,:,:]
00136         nz_arr = nrml_arr[2,:,:]
00137 
00138         nx, ny, nz = compute_resultant_force_directions(nx_arr,
00139                                         ny_arr, nz_arr, label_im,
00140                                         nb_labels)
00141 
00142         res_force_mat = np.matrix(np.row_stack((nx, ny, nz) * np.array(forces)))
00143 
00144         mu.plot_quiver(cop_mat, res_force_mat, (1., 0., 0.), 0.05)
00145         mu.plot_points(cop_mat, (0., 1., 1.), 0.004, mode='sphere')
00146 
00147         print 'Number of regions:', nb_labels
00148         print 'resultant forces:', forces
00149         print 'centers_of pressure -'
00150         print 'cx:', cx
00151         print 'cx.__class__:', cx.__class__
00152         print 'cy:', cy
00153         print 'cz:', cz
00154 
00155         pp.imshow(label_im)
00156 
00157 
00158     if True:
00159         mpu.figure()
00160         plot_taxel_array_as_image(ta)
00161         pp.show()
00162 
00163 


hrl_meka_skin_sensor_darpa_m3
Author(s): Advait Jain. Advisor: Prof. Charlie Kemp. Healthcare Robotics Lab, Georgia Tech.
autogenerated on Wed Nov 27 2013 12:02:01