taxel_calibration_viz.py
Go to the documentation of this file.
00001 
00002 import sys, os
00003 import math, numpy as np
00004 import matplotlib.pyplot as pp
00005 import roslib; roslib.load_manifest('hrl_fabric_based_tactile_sensor')
00006 
00007 import hrl_lib.util as ut
00008 import hrl_lib.matplotlib_util as mpu
00009 
00010 def min_max_force_min_pressure_to_trigger_pressure_threshold(th, pressure_l, force_l, adc_l):
00011     p_arr = np.array(pressure_l)
00012     f_arr = np.array(force_l)
00013     adc_arr = np.array(adc_l)
00014     idxs = np.where(p_arr>th)[0]
00015     if len(idxs) == 0:
00016         return None, None, None, None
00017     adc_thresh = np.min(adc_arr[idxs])
00018     if adc_thresh == 0.:
00019         return None, None, None, None
00020     min_force = np.min(f_arr[np.where(adc_arr > adc_thresh)[0]])
00021     max_force = np.max(f_arr[np.where(adc_arr < adc_thresh)[0]])
00022     min_pressure = np.min(p_arr[np.where(adc_arr > adc_thresh)[0]])
00023     return min_force, max_force, min_pressure, adc_thresh
00024 
00025 
00026 def min_force_to_trigger_threshold(threshold, force_l, adc_l):
00027     f_arr = np.array(force_l)
00028     adc_arr = np.array(adc_l)
00029     idxs = np.where(f_arr>threshold)[0]
00030     if len(idxs) == 0:
00031         return None, None
00032     adc_thresh = np.min(adc_arr[idxs])
00033     min_force = np.min(f_arr[np.where(adc_arr > adc_thresh)[0]])
00034     return min_force, adc_thresh
00035 
00036 def uncertainty_in_force(force_l, adc_l, adc_bin_size):
00037     min_adc = min(adc_l)
00038     max_adc = max(adc_l)
00039     adc_val = min_adc + adc_bin_size / 2.
00040 
00041     f_arr = np.array(force_l)
00042     adc_arr = np.array(adc_l)
00043     adc_bin_l = []
00044     max_f_l = []
00045     min_f_l = []
00046     while adc_val < max_adc:
00047         idxs = np.where(np.all(np.row_stack((adc_arr < (adc_val+adc_bin_size/2.),
00048                                              adc_arr >= (adc_val-adc_bin_size/2))), 0))[0]
00049         if len(idxs) != 0:
00050             adc_bin_l.append(adc_val)
00051             max_f_l.append(np.max(f_arr[idxs]))
00052             min_f_l.append(np.min(f_arr[idxs]))
00053         else:
00054             print 'boo'
00055         adc_val += adc_bin_size
00056 
00057     return adc_bin_l, max_f_l, min_f_l
00058 
00059 def plot_uncertainty_in_force(d, color):
00060     ft_l = d['ft']
00061     adc_l = (d['adc_bias'] - np.array(d['adc'])).tolist()
00062     adc_bin_l, max_f_l, min_f_l = uncertainty_in_force(ft_l, adc_l, 20)
00063 
00064     pp.plot(adc_bin_l, np.array(max_f_l) - np.array(min_f_l), color=color,
00065             label='.'.join('/'.join(nm.split('/')[1:]).split('.')[0:-1]))
00066 
00067     pp.xlabel('ADC bias - ADC')
00068     pp.ylabel('max - min of measured FT_z')
00069     pp.legend()
00070 
00071 def force_vs_adc_2(d, color):
00072     ft_l = d['ft']
00073     adc_l = (d['adc_bias'] - np.array(d['adc'])).tolist()
00074     #adc_l = d['adc']
00075 
00076     f_prev = 0.
00077     temp_ft_l = []
00078     temp_adc_l = []
00079     increasing = True
00080     for i in range(len(ft_l)):
00081         f = ft_l[i]
00082         a = adc_l[i]
00083         if f>f_prev:
00084             if increasing:
00085                 temp_ft_l.append(f)
00086                 temp_adc_l.append(a)
00087             else:
00088                 pp.plot(temp_adc_l, temp_ft_l, color='r', alpha=0.5)
00089                 temp_ft_l = [f]
00090                 temp_adc_l = [a]
00091             increasing = True
00092         else:
00093             if increasing:
00094                 pp.plot(temp_adc_l, temp_ft_l, color='b', alpha=0.5)
00095                 temp_ft_l = [f]
00096                 temp_adc_l = [a]
00097             else:
00098                 temp_ft_l.append(f)
00099                 temp_adc_l.append(a)
00100             increasing = False
00101         f_prev = f
00102 
00103     pp.xlabel('ADC bias - ADC')
00104     pp.ylabel('FT_z')
00105     pp.legend()
00106 
00107 def force_vs_adc(d, color):
00108     ft_l = d['ft']
00109     adc_l = (d['adc_bias'] - np.array(d['adc'])).tolist()
00110     #adc_l = d['adc']
00111     #pp.scatter(adc_l, ft_l, marker='x', color=color,
00112                 # label='.'.join('/'.join(nm.split('/')[1:]).split('.')[0:-1]))
00113 
00114     pp.scatter(adc_l, ft_l, marker='x', color=color,
00115                label=nm.split('/')[-1].split('.')[0])
00116 
00117     pp.xlabel('ADC bias - ADC')
00118     pp.ylabel('FT_z')
00119     pp.legend()
00120     pp.grid('on')
00121 
00122 def pressure_vs_adc(d, color):
00123     ft_l = d['ft']
00124     adc_l = (d['adc_bias'] - np.array(d['adc'])).tolist()
00125     #adc_l = d['adc']
00126     area = d['contact_area']
00127     press_l = [f/area *0.001 for f in ft_l]
00128 #pp.scatter(adc_l, press_l, marker='x', color=color,
00129 #               label='.'.join('/'.join(nm.split('/')[1:]).split('.')[0:-1]))
00130     pp.scatter(adc_l, press_l, marker='x', color=color,
00131                label=nm.split('/')[-1].split('.')[0])
00132 
00133     pp.xlabel('ADC bias - ADC')
00134     pp.ylabel('Pressure (kPa)')
00135     pp.legend()
00136     pp.grid('on')
00137 
00138 def resistance_from_adc(adc, adc_max, r1, r2):
00139     if adc_max == adc:
00140         return 5000.
00141     return (r1 * adc) / (adc_max - adc) - r2
00142 
00143 def force_vs_resistance(d, color):
00144     ft_l = d['ft']
00145     adc_max = d['adc_bias'] * 1.
00146     r1 = d['pull_up']
00147     r2 = 40. # conductive thread and connection between taxel and thread
00148     rv_l = [resistance_from_adc(adc, adc_max, r1, r2) for adc in d['adc']]
00149 
00150     pp.scatter(rv_l, ft_l, marker='x', color=color,
00151                label=nm.split('/')[-1].split('.')[0])
00152 
00153     pp.xlabel('variable resistance')
00154     pp.ylabel('FT_z')
00155     pp.xlim(0, 4000.)
00156     pp.legend()
00157 
00158 def pressure_vs_resistivity(d, color):
00159     ft_l = d['ft']
00160     area = d['contact_area']
00161     adc_max = d['adc_bias'] * 1.
00162     r1 = d['pull_up']
00163     r2 = 0. # conductive thread and connection between taxel and thread
00164     rv_l = [resistance_from_adc(adc, adc_max, r1, r2) for adc in d['adc']]
00165 
00166     press_l = [f/(area*1000.) for f in ft_l]
00167     rho_l = [r*area for r in rv_l]
00168 
00169     pp.scatter(press_l, rho_l, marker='x', color=color,
00170                label=nm.split('/')[-1].split('.')[0])
00171 
00172     pp.ylabel('resistivity (assuming length is constant)')
00173     pp.xlabel('pressure')
00174     pp.legend()
00175 
00176 
00177 if __name__ == '__main__':
00178     import optparse
00179     p = optparse.OptionParser()
00180 
00181     p.add_option('--direc', '-d', action='store',
00182                  dest='direc', type='string',
00183                  help='directory with all the pkls (each pkl gets separate color)')
00184 
00185     opt, args = p.parse_args()
00186 
00187     if not opt.direc:
00188         print 'Please specify a directory'
00189         sys.exit()
00190 
00191     nm_l = ut.get_bash_command_output('find '+opt.direc+' -name "*.pkl"')
00192 
00193     color_list = ['r', 'g', 'b']
00194     for nm in nm_l:
00195         c = mpu.random_color()
00196         color_list.append(c)
00197 
00198     if True:
00199         mpu.figure()
00200         for nm, c in zip(nm_l, color_list):
00201             d = ut.load_pickle(nm)
00202             #d['adc_bias']=1023 # For stretching experiments with Sarvagya
00203             force_vs_adc(d, c)
00204             #force_vs_adc_2(d, c)
00205             pp.xlim((0,1000))
00206             pp.ylim((-10,80))
00207 
00208             print 'ADC bias: %d'%d['adc_bias']
00209            
00210 #        mpu.figure()
00211 #        for nm, c in zip(nm_l, color_list):
00212 #            d = ut.load_pickle(nm)
00213 #            plot_uncertainty_in_force(d, c)
00214 
00215     if False:
00216         print '====================================================================='
00217         print 'Min force that will trigger the safety threshold (known contact area)'
00218         print '====================================================================='
00219 
00220         force_threshold_l = [10., 15., 20., 25., 30. ]
00221         fig1 = mpu.figure()
00222         pp.xlabel('Force Threshold')
00223         pp.ylabel('Min Triggered Force')
00224         pp.title('Known Contact Area')
00225         fig2 = mpu.figure()
00226         pp.xlabel('Force Threshold')
00227         pp.ylabel('Min Triggered ADC value')
00228         pp.title('Known Contact Area')
00229         for nm, c in zip(nm_l, color_list):
00230             label='.'.join('/'.join(nm.split('/')[1:]).split('.')[0:-1])
00231             trig_f_l = []
00232             trig_adc_l = []
00233             thresh_l = []
00234             for force_threshold in force_threshold_l:
00235                 d = ut.load_pickle(nm)
00236                 #d['adc_bias']=1023 #For stretching experiments with Sarvagya
00237                 ft_l = d['ft']
00238                 adc_l = (d['adc_bias'] - np.array(d['adc'])).tolist()
00239                 f, a = min_force_to_trigger_threshold(force_threshold, ft_l, adc_l)
00240                 if f == None:
00241                     continue
00242                 trig_f_l.append(f)
00243                 trig_adc_l.append(a)
00244                 thresh_l.append(force_threshold)
00245             pp.figure(fig1.number)
00246             pp.plot(thresh_l, trig_f_l, label=label, color=c, marker='o', ms=7)
00247             pp.figure(fig2.number)
00248             pp.plot(thresh_l, trig_adc_l, label=label, color=c, marker='o', ms=7)
00249         pp.figure(fig1.number)
00250         pp.legend()
00251         pp.figure(fig2.number)
00252         pp.legend()
00253 
00254     # combine a bunch of pkl
00255     if False:
00256         d = {}
00257         d['ft_l'] = []
00258         d['pull_up_l'] = []
00259         d['adc_l'] = []
00260         d['adc_bias_l'] = []
00261         d['contact_area_l'] = []
00262         for nm in nm_l:
00263             d_t = ut.load_pickle(nm)
00264             d['ft_l'].append(d_t['ft'])
00265             d['pull_up_l'].append(d_t['pull_up'])
00266             d['adc_l'].append(d_t['adc'])
00267             d['adc_bias_l'].append(d_t['adc_bias'])
00268             #d['adc_bias_l'].append(1023) # For stretching experiments with Sarvagya
00269             d['contact_area_l'].append(d_t['contact_area'])
00270 
00271         ut.save_pickle(d, 'combined.pkl')
00272 
00273     if False:
00274         print '======================================================================='
00275         print 'Min force that will trigger the safety threshold (unknown contact area)'
00276         print '======================================================================='
00277 
00278         force_threshold_l = [10., 15., 20., 25., 30. ]
00279         fig1 = mpu.figure()
00280         pp.xlabel('Force Threshold')
00281         pp.ylabel('Min Triggered Force')
00282         pp.title('Unknown Contact Area')
00283         fig2 = mpu.figure()
00284         pp.xlabel('Force Threshold')
00285         pp.ylabel('Min Triggered ADC value')
00286         pp.title('Unknown Contact Area')
00287 
00288         ft_l = []
00289         adc_l = []
00290         for nm, c in zip(nm_l, color_list):
00291             label='.'.join(nm.split('/')[-1].split('.')[0:-1])
00292             trig_f_l = []
00293             trig_adc_l = []
00294             thresh_l = []
00295             for force_threshold in force_threshold_l:
00296                 d = ut.load_pickle(nm)
00297                 #d['adc_bias']=1023 #For stretching experiments with Sarvagya
00298                 ft_l = []
00299                 adc_l = []
00300                 for i in range(len(d['adc_bias_l'])):
00301                     ft_l += d['ft_l'][i]
00302                     bias = d['adc_bias_l'][i]
00303                     adc_l += (bias - np.array(d['adc_l'][i])).tolist()
00304                 f, a = min_force_to_trigger_threshold(force_threshold, ft_l, adc_l)
00305                 if f == None:
00306                     continue
00307                 trig_f_l.append(f)
00308                 trig_adc_l.append(a)
00309                 thresh_l.append(force_threshold)
00310             pp.figure(fig1.number)
00311             pp.plot(thresh_l, trig_f_l, label=label, color=c, marker='o', ms=7)
00312             pp.figure(fig2.number)
00313             pp.plot(thresh_l, trig_adc_l, label=label, color=c, marker='o', ms=7)
00314         pp.figure(fig1.number)
00315         pp.legend()
00316         pp.figure(fig2.number)
00317         pp.legend()
00318 
00319     if False:
00320         print '======================================================================='
00321         print 'Forces that will trigger the safety threshold on pressure (unknown contact area)'
00322         print '======================================================================='
00323 
00324         pressure_threshold_l = [50000., 100000, 150000., 200000.,
00325                 250000., 300000., 350000., 400000., 450000., 500000. ]
00326         fig1 = mpu.figure()
00327         pp.xlabel('Force Threshold')
00328         pp.ylabel('Min Triggered Force and max permitted force')
00329         pp.title('Unknown Contact Area')
00330         fig2 = mpu.figure()
00331         pp.xlabel('Pressure Threshold')
00332         pp.ylabel('Min Triggered ADC value')
00333         pp.title('Unknown Contact Area')
00334         fig3 = mpu.figure()
00335         pp.xlabel('Pressure Threshold')
00336         pp.ylabel('Min Triggered Pressure')
00337         pp.title('Unknown Contact Area')
00338 
00339         ft_l = []
00340         adc_l = []
00341         for nm, c in zip(nm_l, color_list):
00342             label='.'.join(nm.split('/')[-1].split('.')[0:-1])
00343             trig_f_min_l = []
00344             trig_f_max_l = []
00345             trig_p_l = []
00346             trig_adc_l = []
00347             thresh_l = []
00348             for th in pressure_threshold_l:
00349                 d = ut.load_pickle(nm)
00350                 #d['adc_bias']=1023 #For stretching experiments with Sarvagya
00351                 ft_l = []
00352                 pressure_l = []
00353                 adc_l = []
00354                 for i in range(len(d['adc_bias_l'])):
00355                     ca = d['contact_area_l'][i]
00356                     ft_l += d['ft_l'][i]
00357                     pressure_l += (np.array(d['ft_l'][i])/ca).tolist()
00358 
00359                     bias = d['adc_bias_l'][i]
00360                     adc_l += (bias - np.array(d['adc_l'][i])).tolist()
00361 
00362                 f_min, f_max, p_min, a = min_max_force_min_pressure_to_trigger_pressure_threshold(th, pressure_l, ft_l, adc_l)
00363                 if f_min == None:
00364                     continue
00365 
00366                 trig_f_min_l.append(f_min)
00367                 trig_f_max_l.append(f_max)
00368                 trig_adc_l.append(a)
00369                 trig_p_l.append(p_min)
00370                 thresh_l.append(th)
00371             pp.figure(fig1.number)
00372             pp.plot(thresh_l, trig_f_min_l, label=label, color=c, marker='o', ms=7)
00373             pp.plot(thresh_l, trig_f_max_l, label=label, color=c, marker='o', ms=7)
00374             pp.figure(fig2.number)
00375             pp.plot(thresh_l, trig_adc_l, label=label, color=c, marker='o', ms=7)
00376             pp.figure(fig3.number)
00377             pp.plot(thresh_l, trig_p_l, label=label, color=c, marker='o', ms=7)
00378         pp.figure(fig1.number)
00379         pp.legend()
00380         pp.figure(fig2.number)
00381         pp.legend()
00382         pp.figure(fig3.number)
00383         pp.legend()
00384 
00385 
00386 
00387 
00388 #    mpu.figure()
00389 #    for nm, c in zip(nm_l, color_list):
00390 #        d = ut.load_pickle(nm)
00391 #        force_vs_resistance(d, c)
00392 #
00393 #    mpu.figure()
00394 #    for nm, c in zip(nm_l, color_list):
00395 #        d = ut.load_pickle(nm)
00396 #        pressure_vs_resistivity(d, c)
00397 
00398     if False:
00399         mpu.figure()
00400         for nm, c in zip(nm_l, color_list):
00401             d = ut.load_pickle(nm)
00402             #d['adc_bias']=1023 #For stretching experiments with Sarvagya
00403             pressure_vs_adc(d, c)
00404 
00405     pp.show()
00406 
00407 
00408 


hrl_fabric_based_tactile_sensor
Author(s): Advait Jain, Advisor: Prof. Charles C. Kemp. Healthcare Robotics Lab, Georgia Tech
autogenerated on Wed Nov 27 2013 12:02:33