test_rfid_pf.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 import random as rd
00004 import numpy as np, math
00005 
00006 import rfid_model
00007 import lib_pfilter
00008 
00009 import roslib
00010 roslib.load_manifest('visualization_msgs')
00011 roslib.load_manifest('hrl_lib')
00012 import rospy
00013 
00014 import visualization_msgs.msg as vm
00015 import hrl_lib.transforms as tr
00016 import hrl_lib.viz as viz
00017 vsm = viz.single_marker
00018 
00019 import pylab as pl
00020 
00021 class DisplayWeights:
00022     def __init__( self, pub_topic = '/particles' ):
00023         self.m = None
00024         self.pub_mark = rospy.Publisher( pub_topic, vm.Marker )
00025         self.mid = 0
00026 
00027     def create_mark( self, p, c = [1.0, 0.0, 0.0, 0.8], mid = None ):
00028         if mid == None:
00029             self.mid += 1
00030             mid = self.mid
00031             
00032         m =  vsm( np.matrix([ p[0], p[1], 0.0 ]).T,
00033                   np.matrix([ 0.0, 0.0, 0.0, 1.0 ]).T,
00034                   'sphere', '/map',
00035                   scale = [0.05, 0.05, 0.05],
00036                   color = [1.0, 0.0, 0.0, 0.8], # rgba,
00037                   duration = 10.0,
00038                   m_id = mid )
00039         m.header.stamp = rospy.Time.now()
00040         return m
00041     
00042     
00043     def update( self, particles ):
00044         xyz = np.column_stack([ particles[:,0:2], np.zeros( particles.shape[0] )]).T
00045         w = particles[:,2] # becomes 1D
00046         # print w
00047         wmin = np.min( w )
00048         wmax = np.max( w )
00049         # import pdb
00050         # pdb.set_trace()
00051 
00052         if wmin == wmax:
00053             colors = np.row_stack([ np.ones( particles.shape[0] ),
00054                                     np.zeros( particles.shape[0] ),
00055                                     np.zeros( particles.shape[0] ),
00056                                     np.ones( particles.shape[0] ) ])
00057         else:
00058             colors = np.array([ pl.cm.jet( int( 1.0 * ( wi - wmin ) / (wmax - wmin) * 255.0 ))
00059                                 for wi in w ]).T
00060         m = viz.list_marker( xyz, colors, [0.05, 0.05, 0.05], 'points', '/map', 300 )
00061         m.header.stamp = rospy.Time.now()
00062         for i in xrange( 10 ):
00063             self.pub_mark.publish( m )
00064             rospy.sleep( 0.2 )
00065 
00066         return
00067         
00068         # if not self.m:
00069         #     self.m = [ self.create_mark( p, mid=None ) for i,p in enumerate( particles )]
00070         # else:
00071         #     self.m = [ self.create_mark( p, mid=i ) for i,p in enumerate( particles )]
00072 
00073         # for i in xrange( 10 ):
00074         #     [ self.pub_mark.publish( m ) for m in self.m ]
00075         #     rospy.sleep( 0.2 )
00076         
00077 
00078 
00079 
00080 # if __name__ == '__main__':
00081 
00082 rospy.init_node( 'test_rfid_pf' )
00083 
00084 # build particles
00085 X,Y = np.meshgrid( np.arange(-10,10,0.1),
00086                    np.arange(-10,10,0.1) )
00087 xyw = np.row_stack([ X.flatten(),  # Build Nx3
00088                      Y.flatten(),
00089                      np.ones( X.shape ).flatten() ]).T # weights (multiplicative)
00090 
00091 xyw_orig = np.copy( xyw )
00092 
00093 rm = rfid_model.RfidModel( rfid_model.yaml_fname )
00094 # pf = lib_pfilter.PFilter( lib_pfilter.NoMotion(), rm, xyw )
00095 # pf.measurement( 80 )
00096 
00097 
00098 # Antenna reading at origin (max near 4,0)
00099 # rv = np.copy( xyw )
00100 rv = rm.weight_set( 83, xyw )
00101 
00102 # Move antenna to 4,-4 and rotated 90 deg
00103 xy = xyw[:,0:2]
00104 trans = tr.composeHomogeneousTransform( tr.rotZ( math.radians(-90) ), np.matrix([5,5,0]).T )
00105 xy_new = np.array((trans * tr.xyToHomogenous( xy.T) )[0:2].T)
00106 
00107 xyw_new = np.column_stack([ xy_new, rv[:,2] ])  # take weights from last iteration
00108 rv_new = rm.weight_set( 83, xyw_new )
00109 
00110 to_print = np.copy( xyw_orig )
00111 to_print[:,2] = rv_new[:,2]  # Just keep weights, but use original points in "map" frame
00112 
00113 
00114 
00115 dw = DisplayWeights()
00116 
00117 while not rospy.is_shutdown():
00118     print 'UPDATING'
00119     dw.update( to_print )
00120     # dw.update( rv )
00121     m = int( raw_input( 'New RSSI: ' ))
00122     # rv = rm.weight_set( m, xyw )
00123     # rospy.sleep( 150 )
00124 
00125 


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