Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 import roslib; roslib.load_manifest('hrl_rfid')
00035 import rospy
00036 from hrl_rfid.msg import RFIDread
00037 from hrl_rfid.srv import RfidSrv
00038 import hrl_rfid.lib_M5e as M5e
00039
00040 import time
00041 from threading import Thread
00042
00043
00044
00045 class ROS_M5e( ):
00046 QUERY_MODE = 'query'
00047 TRACK_MODE = 'track'
00048
00049 def __init__(self, name = 'reader1', readPwr = 2300,
00050 portStr = '/dev/robot/RFIDreader',
00051 antFuncs = [], callbacks = []):
00052
00053 try:
00054 rospy.init_node( 'rfid_m5e_' + name )
00055 except rospy.ROSException:
00056 pass
00057
00058 self.mode = 'track'
00059 self.tag_to_track = 'In Hand Tag '
00060 self.name = name + '_reader'
00061
00062 rospy.logout( 'ROS_M5e: Launching RFID Reader' )
00063 rospy.logout( 'ROS_M5e: Please check out our related work @ http://www.hsi.gatech.edu/hrl/project_rfid.shtml' )
00064 rospy.logout( 'ROS_M5e: '+self.name+' Building & Connecting to reader' )
00065
00066 def prin( x ): rospy.logout( 'ROS_M5e: lib_M5e: ' + x )
00067
00068 self.reader = M5e.M5e(readPwr=readPwr, portSTR = portStr, verbosity_func = prin)
00069 self.antFuncs = antFuncs
00070 self.callbacks = callbacks + [self.broadcast]
00071
00072 rospy.logout( 'ROS_M5e: publishing RFID reader with type RFIDread to channel /rfid/'+name+'_reader' )
00073 self.channel = rospy.Publisher('/rfid/'+name+'_reader', RFIDread)
00074 self._mode_service_obj = rospy.Service('/rfid/'+name+'_mode',
00075 RfidSrv, self._mode_service)
00076
00077 rospy.logout( 'ROS_M5e: '+self.name+' Inialized and awaiting instructions' )
00078
00079 def run( self, total_iter = 1000 ):
00080 for i in xrange( total_iter ):
00081 if self.mode == self.QUERY_MODE:
00082 for aF in self.antFuncs:
00083 antennaName = aF(self.reader)
00084 results = self.reader.QueryEnvironment()
00085 if len(results) == 0:
00086 datum = [antennaName, '', -1]
00087 [cF(datum) for cF in self.callbacks]
00088 for tagid, rssi in results:
00089 datum = [antennaName, tagid, rssi]
00090 [cF(datum) for cF in self.callbacks]
00091 elif self.mode == self.TRACK_MODE:
00092 for aF in self.antFuncs:
00093 antennaName = aF(self.reader)
00094 tagid = self.tag_to_track
00095 rssi = self.reader.TrackSingleTag(tagid, timeout = 10, safe_response = False)
00096
00097 datum = [antennaName, tagid, rssi]
00098 [cF(datum) for cF in self.callbacks]
00099 else:
00100 time.sleep(0.005)
00101
00102 rospy.logout( 'ROS_M5e: '+self.name+' Shutting down reader' )
00103
00104 def broadcast(self, data):
00105 antName, tagid, rssi = data
00106 rv = RFIDread( None, antName, tagid, rssi )
00107 rv.header.stamp = rospy.Time.now()
00108 self.channel.publish( rv )
00109
00110
00111 def _mode_service(self, data):
00112 val = data.data
00113 if len(val) == 0:
00114 rospy.logout( 'ROS_M5e: Mode Service called with invalid argument: ' + str(val) )
00115 elif len(val) == 1:
00116 if val[0] == self.QUERY_MODE:
00117 rospy.logout( 'ROS_M5e: '+self.name+' Entering Query Mode' )
00118 self.mode = self.QUERY_MODE
00119 else:
00120 rospy.logout( 'ROS_M5e: '+self.name+' Stopping Reader' )
00121 self.mode = ''
00122 elif len(val) == 2:
00123 if val[0] == self.TRACK_MODE and len(val[1]) == 12:
00124 rospy.logout( 'ROS_M5e: '+self.name+' Entering Track Mode: ' + str(val[1]) )
00125 self.mode = self.TRACK_MODE
00126 self.tag_to_track = val[1]
00127 else:
00128 rospy.logout( 'ROS_M5e: Mode Service called with invalid argument: ' + str(val) )
00129 else:
00130 rospy.logout( 'ROS_M5e: Mode Service called with invalid argument: ' + str(val) )
00131 return True
00132
00133
00134
00135
00136
00137
00138
00139 def EleLeftEar(M5e):
00140 M5e.ChangeAntennaPorts(1,1)
00141 time.sleep(0.010)
00142 return 'EleLeftEar'
00143
00144 def EleRightEar(M5e):
00145 M5e.ChangeAntennaPorts(2,2)
00146 time.sleep(0.010)
00147 return 'EleRightEar'
00148
00149 def Hand_Right_1(M5e):
00150
00151 M5e.TransmitCommand('\x02\x96\x01\x01')
00152 M5e.ReceiveResponse()
00153 M5e.TransmitCommand('\x02\x96\x02\x00')
00154 M5e.ReceiveResponse()
00155 return 'Hand_Right_1'
00156
00157 def Hand_Right_2(M5e):
00158
00159 M5e.TransmitCommand('\x02\x96\x01\x01')
00160 M5e.ReceiveResponse()
00161 M5e.TransmitCommand('\x02\x96\x02\x01')
00162 M5e.ReceiveResponse()
00163 return 'Hand_Right_2'
00164
00165 def Hand_Left_1(M5e):
00166
00167 M5e.TransmitCommand('\x02\x96\x01\x00')
00168 M5e.ReceiveResponse()
00169 M5e.TransmitCommand('\x02\x96\x02\x00')
00170 M5e.ReceiveResponse()
00171 return 'Hand_Left_1'
00172
00173 def Hand_Left_2(M5e):
00174
00175 M5e.TransmitCommand('\x02\x96\x01\x00')
00176 M5e.ReceiveResponse()
00177 M5e.TransmitCommand('\x02\x96\x02\x01')
00178 M5e.ReceiveResponse()
00179 return 'Hand_Left_2'
00180
00181 def PrintDatum(data):
00182 ant, ids, rssi = data
00183 print data
00184
00185 if __name__ == '__main__':
00186 print 'Starting Ears RFID Services'
00187 ros_rfid = ROS_M5e( name = 'ears', readPwr = 2300,
00188 portStr = '/dev/robot/RFIDreader',
00189 antFuncs = [EleLeftEar, EleRightEar],
00190 callbacks = [] )
00191 total_iter = 500
00192 t0 = time.time()
00193 ros_rfid.run( total_iter )
00194 t1 = time.time()
00195 print 'Reads per sec: ', (total_iter * 2.0) / ( 1.0 * (t1 - t0))
00196