speed_profile.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 #
00003 # Copyright (c) 2009, Georgia Tech Research Corporation
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions are met:
00008 #     * Redistributions of source code must retain the above copyright
00009 #       notice, this list of conditions and the following disclaimer.
00010 #     * Redistributions in binary form must reproduce the above copyright
00011 #       notice, this list of conditions and the following disclaimer in the
00012 #       documentation and/or other materials provided with the distribution.
00013 #     * Neither the name of the Georgia Tech Research Corporation nor the
00014 #       names of its contributors may be used to endorse or promote products
00015 #       derived from this software without specific prior written permission.
00016 #
00017 # THIS SOFTWARE IS PROVIDED BY GEORGIA TECH RESEARCH CORPORATION ''AS IS'' AND
00018 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 # DISCLAIMED. IN NO EVENT SHALL GEORGIA TECH BE LIABLE FOR ANY DIRECT, INDIRECT,
00021 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023 # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00025 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00026 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 #
00028 
00029 #  \author Travis Deyle (Healthcare Robotics Lab, Georgia Tech.)
00030 
00031 
00032 
00033 # ROS imports
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 # Modeled off lib_M5e.M5e_Poller
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 ) # use rospy.logout in underlying lib's output
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)    # let current antFunc make appropriate changes
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)    # let current antFunc make appropriate changes
00094                     tagid = self.tag_to_track
00095                     rssi = self.reader.TrackSingleTag(tagid, timeout = 10, safe_response = False)
00096                     #if rssi != -1:
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     # For internal use only
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 # Likely Callbacks: (various antennas)
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     # GPIO1 = 1, GPIO2 = 0
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     # GPIO1 = 1, GPIO2 = 1
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     # GPIO1 = 0, GPIO2 = 0
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     # GPIO1 = 0, GPIO2 = 1
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 


hrl_rfid
Author(s): Travis Deyle, Advisor: Prof. Charlie Kemp (Healthcare Robotics Lab at Georgia Tech) and Prof. Matt Reynolds (Duke University)
autogenerated on Wed Nov 27 2013 11:32:45