display_particles.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 import rfid_model
00004 import lib_pfilter
00005 
00006 import roslib
00007 roslib.load_manifest('visualization_msgs')
00008 roslib.load_manifest('hrl_lib')
00009 roslib.load_manifest('pfilter')
00010 import rospy
00011 
00012 import visualization_msgs.msg as vm
00013 import hrl_lib.transforms as tr
00014 import hrl_lib.viz as viz
00015 vsm = viz.single_marker
00016 import pfilter.pfilter as pfilter
00017 
00018 import random as rd
00019 import numpy as np, math
00020 import pylab as pl
00021 import time
00022 import cPickle as pkl
00023 import os
00024 import glob
00025 
00026 class DisplayParticles:
00027     def __init__( self, pub_topic = '/particles' ):
00028         self.m = None
00029         self.pub_mark = rospy.Publisher( pub_topic, vm.Marker )
00030         self.mid = 0
00031 
00032         try:
00033             rospy.init_node('display_particles')
00034         except:
00035             rospy.logout( 'DisplayParticles: Node already initialized' )
00036             pass
00037 
00038     def create_mark( self, p, c = [1.0, 0.0, 0.0, 0.8], mid = None ):
00039         if mid == None:
00040             self.mid += 1
00041             mid = self.mid
00042             
00043         m =  vsm( np.matrix([ p[0], p[1], 0.0 ]).T,
00044                   np.matrix([ 0.0, 0.0, 0.0, 1.0 ]).T,
00045                   'sphere', '/map',
00046                   scale = [0.025, 0.025, 0.025],
00047                   color = [1.0, 0.0, 0.0, 0.3], # rgba,
00048                   duration = 10.0,
00049                   m_id = mid )
00050         m.header.stamp = rospy.Time.now()
00051         return m
00052     
00053     
00054     def update( self, particles ):
00055         xyz = np.column_stack([ particles[:,0:2], np.zeros( particles.shape[0] )]).T
00056         w = particles[:,2] # becomes 1D
00057         # print w
00058         wmin = np.min( w )
00059         wmax = np.max( w )
00060         # import pdb
00061         # pdb.set_trace()
00062 
00063         if wmin == wmax:
00064             colors = np.row_stack([ np.ones( particles.shape[0] ),
00065                                     np.zeros( particles.shape[0] ),
00066                                     np.zeros( particles.shape[0] ),
00067                                     np.ones( particles.shape[0] ) ])
00068         else:
00069             colors = np.array([ pl.cm.jet( int( 1.0 * ( wi - wmin ) / (wmax - wmin) * 255.0 ))
00070                                 for wi in w ]).T
00071         m = viz.list_marker( xyz, colors, [0.025, 0.025, 0.025], 'points', '/map', 300 )
00072         m.header.stamp = rospy.Time.now()
00073         for i in xrange( 10 ):
00074             self.pub_mark.publish( m )
00075             rospy.sleep( 0.2 )
00076 
00077         return
00078 
00079 
00080     def update2( self, particles ):
00081         xyz = np.column_stack([ particles[:,0:2], np.zeros( particles.shape[0] )]).T
00082         w = particles[:,2] # becomes 1D
00083         # print w
00084         wmin = np.min( w )
00085         wmax = np.max( w )
00086         # import pdb
00087         # pdb.set_trace()
00088 
00089         if wmin == wmax:
00090             colors = np.row_stack([ np.ones( particles.shape[0] ),
00091                                     np.zeros( particles.shape[0] ),
00092                                     np.zeros( particles.shape[0] ),
00093                                     np.ones( particles.shape[0] ) ])
00094             iv = np.ones( len( w )).tolist()
00095         else:
00096             iv = [ int( 1.0 * ( wi - wmin ) / (wmax - wmin) * 255.0 ) for wi in w ]
00097             colors = np.array([ pl.cm.jet( ivi ) for ivi in iv ]).T
00098             # colors[3] *= 0.3
00099 
00100         print np.array(iv)
00101         ind = np.where( np.array(iv) > 5 )[0]
00102         aind = np.argsort( w[ind] )  # sort them so that some come to top.
00103         
00104         m = viz.list_marker( xyz[:,ind][:,aind], colors[:,ind][:,aind], [0.05, 0.05, 0.025], 'points', '/map', 30 )
00105         m.header.stamp = rospy.Time.now()
00106         for i in xrange( 10 ):
00107             self.pub_mark.publish( m )
00108             rospy.sleep( 0.2 )
00109 
00110         return
00111 
00112 
00113 
00114 
00115 
00116 def display_trialobj( trial_num, obj_num, servo_yn, screen_cap = False ):
00117     try:
00118         rospy.init_node( 'ros_pf' )
00119     except:
00120         print 'display_trialobj: node already initialized'
00121         pass
00122     
00123 
00124     tdb = { 0: ['OrangeMedBot',[]],
00125             1: ['TravisTVremo',[]],
00126             2: ['RedBottle   ',[]],
00127             3: ['OnMetalKeys ',[]],
00128             4: ['WhiteMedsBot',[]],
00129             5: ['BlueMedsBox ',[]],
00130             6: ['TeddyBearToy',[]],
00131             7: ['CordlessPhon',[]],
00132             8: ['BlueHairBrus',[]]}
00133 
00134     pts = { 0: ['BehindTree',[3.757, 6.017, 0.036]],
00135             1: ['FireplaceMantle',[5.090, 4.238, 1.514]],
00136             2: ['CircleEndTable',[5.399, 2.857, 0.607]],
00137             3: ['Couch',[3.944, 1.425, 0.527]],
00138             4: ['RectEndTable',[3.302, 0.932, 0.534]],
00139             5: ['BehindKitchenTable',[-0.339, -2.393, 0.793]],
00140             6: ['NearDishwaser',[-1.926, -0.835, 0.946]],
00141             7: ['InCupboard',[-3.257, 1.294, 1.397]],
00142             8: ['OnFilingCabinet',[-0.083, 2.332, 0.670]]}
00143 
00144     
00145     obj_name = tdb[obj_num][0]
00146     tname = obj_name.replace( ' ', '' )
00147 
00148     loc = (trial_num + obj_num) % 9
00149     loc_name = pts[loc][0]
00150     loc_pos  = np.array(pts[loc][1])  # Tag ground-truth location
00151 
00152 
00153     fname_prefix = '/home/travis/svn/robot1/src/projects/rfid_datacapture/src/rfid_datacapture/search_cap/'
00154 
00155     servo_fname = fname_prefix
00156     servo_fname += 'search_aware_home/'
00157     servo_fname += 'woot_150_'+str(trial_num)+'_tag_'+obj_name.replace(' ','')+'_servo.pkl'
00158 
00159     pf_search = servo_fname.replace('_servo.pkl', '_pf_search.pkl')
00160     pf_servo = servo_fname.replace('_servo.pkl', '_pf_servo.pkl')
00161 
00162     glob_r = glob.glob( servo_fname )
00163     if glob_r == []:
00164         print '\t No results for this instance.\n\n'
00165         return
00166     if len(glob_r) > 1:
00167         print '\t Multiple results...?! Weirdness.  Skipping.'
00168         return
00169 
00170     if servo_yn:
00171         f = open( pf_servo, 'r' )
00172         p_set = pkl.load( f )
00173         f.close()
00174     else:
00175         f = open( pf_search, 'r' )
00176         p_set = pkl.load( f )
00177         f.close()
00178 
00179     dp = DisplayParticles()
00180 
00181     pub_mark = rospy.Publisher( '/tag_poses', vm.Marker )
00182 
00183     tm = vsm( np.matrix([ pts[loc][1][0],
00184                             pts[loc][1][1],
00185                             pts[loc][1][2] ]).T,
00186                 np.matrix([ [0.0], [0.0], [0.0], [1.0] ]),
00187                 'sphere', '/map',
00188                 color = [0.0, 1.0, 0.0, 1.0], # rgba
00189                 duration = 150.0,
00190                 m_id = 2*p_set.shape[0] + 1 )
00191 
00192     def pub_tm():
00193         tm.header.stamp = rospy.Time.now()
00194         for i in xrange( 10 ):
00195             pub_mark.publish( tm )
00196             rospy.sleep( 0.3 )
00197 
00198     def update_display():
00199         print 'UPDATING DISPLAY... '
00200         pub_tm()
00201         pub_tm()
00202         dp.update2( p_set )
00203         rospy.sleep( 0.3 )
00204         print 'DONE.'
00205 
00206 
00207     print 'UPDATING'
00208     update_display()
00209     rospy.sleep( 1.0 )
00210 
00211     if screen_cap:
00212         os.system( 'scrot -d 2 -u Obj%d_Trial%d_Servo%d_pf_results.png' % ( obj_num, trial_num, int(servo_yn) ))
00213 
00214     rospy.sleep( 2.0 )
00215     print 'DERP'
00216 
00217     return
00218 
00219 
00220 if __name__ == '__main__':
00221     import optparse
00222     p = optparse.OptionParser()
00223     p.add_option('--trial', action='store', type='int', dest='trial',
00224                  help='trial number (0-8)')
00225     p.add_option('--obj', action='store', type='int', dest='obj',
00226                  help='object number (0-8)')
00227     p.add_option('--servo', action='store_true', dest='servo',
00228                  help='Use combined search and servo?', default = False)
00229     opt, args = p.parse_args()
00230 
00231     obj_num = opt.obj
00232     trial_num = opt.trial
00233     servo_yn = opt.servo
00234     
00235     if trial_num < 9:
00236         while not rospy.is_shutdown():
00237             display_trialobj( trial_num, obj_num, servo_yn )
00238     else:
00239         print 'Click on RVIZ!'
00240         time.sleep( 3 )
00241 
00242         for trial_num in range( 0, 9 ):
00243             for obj_num in range( 0, 9 ):
00244                 for servo_yn in [False, True]:
00245                     print 'Calling display trialobj: ', trial_num, obj_num, servo_yn
00246                     display_trialobj( trial_num, obj_num, servo_yn, screen_cap = True )
00247                     print 'Done.\n\n\n'


rfid_pf
Author(s): Travis Deyle
autogenerated on Wed Nov 27 2013 12:03:34