sim_capture.py
Go to the documentation of this file.
00001 #! /usr/bin/python
00002 import time
00003 import roslib
00004 roslib.load_manifest('rospy')
00005 roslib.load_manifest('actionlib')
00006 roslib.load_manifest( 'move_base_msgs' )
00007 roslib.load_manifest('tf')
00008 roslib.load_manifest('std_srvs')
00009 roslib.load_manifest('geometry_msgs')
00010 roslib.load_manifest('std_msgs')
00011 roslib.load_manifest('hrl_rfid')
00012 roslib.load_manifest('robotis')
00013 roslib.load_manifest('rfid_people_following')
00014 import rospy
00015 
00016 import tf
00017 import tf.transformations as tft
00018 import actionlib
00019 from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal
00020 from geometry_msgs.msg import Twist
00021 from geometry_msgs.msg import PointStamped
00022 from geometry_msgs.msg import Point
00023 from geometry_msgs.msg import PoseStamped
00024 from geometry_msgs.msg import Quaternion
00025 from geometry_msgs.msg import PoseWithCovarianceStamped
00026 from std_msgs.msg import Float64
00027 from std_srvs.srv import Empty
00028 
00029 from rfid_people_following.srv import *
00030 
00031 import numpy as np, math
00032 import os
00033 import time
00034 import pickle as pkl
00035 from threading import Thread
00036 from collections import deque
00037 
00038 
00039 class BasePose( Thread ):
00040     def __init__( self ):
00041         self.should_run = True
00042         Thread.__init__( self )
00043 
00044         self.listener = tf.TransformListener()
00045         self.listener.waitForTransform('/base_link', '/map',
00046                                        rospy.Time(0), timeout = rospy.Duration(100) )
00047         self.ps = PointStamped()
00048         self.ps.header.frame_id = '/base_link'
00049         self.pts = []
00050         self.start()
00051 
00052     def run( self ):
00053         rate = rospy.Rate( 5 )
00054         while self.should_run and not rospy.is_shutdown():
00055             try:
00056                 self.ps.header.stamp = rospy.Time(0)
00057                 ps_map = self.listener.transformPoint( '/odom_combined', self.ps )
00058                 x = ps_map.point.x
00059                 y = ps_map.point.y
00060                 self.pts.append([ x, y ])
00061 
00062                 
00063             except:
00064                 rospy.logout( 'base_pose: Failed transform.' )
00065                 pass
00066             rate.sleep()
00067         
00068 
00069     def stop(self):
00070         self.should_run = False
00071         self.join(3)
00072         if (self.isAlive()):
00073             raise RuntimeError("ServoMode: unable to stop thread")
00074         
00075 
00076 
00077 #        sm = ServoMode( self.follow1 )
00078 
00079 class ServoMode( Thread ):
00080     def __init__( self, func ):
00081         self.done = False
00082         self.should_run = True
00083         Thread.__init__( self )
00084         self.func = func
00085         self.start()
00086 
00087     def run( self ):
00088         self.func()
00089         self.done = True
00090         while self.should_run and not rospy.is_shutdown():
00091             rospy.sleep(0.1)
00092             
00093     def stop(self):
00094         self.should_run = False
00095         self.join(3)
00096         if (self.isAlive()):
00097             raise RuntimeError("ServoMode: unable to stop thread")
00098         
00099 
00100 class SimCapture():
00101     def __init__( self ):
00102         try:
00103             rospy.init_node( 'sim_capture' )
00104         except:
00105             pass
00106 
00107         rospy.logout( 'sim_capture: Initializing' )
00108         self.listener = tf.TransformListener()
00109         self.listener.waitForTransform('/base_link', '/map',
00110                                        rospy.Time(0), timeout = rospy.Duration(100) )
00111         
00112         rospy.logout( 'sim_capture: Waiting for services' )
00113         self._sh = rospy.Service( '/sim_capture/capture' , FloatFloatFloatFloat_Int32, self.capture )
00114         rospy.wait_for_service( '/rfid_servo/servo' )
00115         rospy.wait_for_service( '/rfid_servo/abort' )
00116         rospy.wait_for_service( '/rotate_backup/navstack' )
00117 
00118         self._servo = rospy.ServiceProxy( '/rfid_servo/servo', StringInt32_Int32 )
00119         self.follow1 = lambda : self._servo( 'person      ', 1 ) # Stops at first obs
00120 
00121         self._abort = rospy.ServiceProxy( '/rfid_servo/abort', Empty )
00122         self.abort = lambda : self._abort( ) # Kill current servoing
00123 
00124         self._navstack = rospy.ServiceProxy( '/rotate_backup/navstack' , FloatFloatFloatFloat_Int32 )
00125         self.navstack = lambda x,y,z,theta: self._navstack( x,y,z,theta )
00126 
00127 
00128         rospy.logout( 'sim_capture: Services ready' )
00129 
00130     def capture( self, request, fname = None ):
00131         rospy.logout( 'sim_capture: New capture initiated @ %3.2f.' % rospy.Time.now().to_sec() )
00132         rospy.logout( 'sim_capture: Moving to <%3.2f, %3.2f, %3.2f> %3.2f-deg' % ( request.x,
00133                                                                                    request.y,
00134                                                                                    request.z,
00135                                                                                    math.degrees( request.ang )))
00136         self.navstack( request.x, request.y, request.z, request.ang )
00137         rospy.logout( 'sim_capture: Arrived at location.' )
00138         rospy.logout( 'sim_capture: Initializing recorder and servoing.' )
00139 
00140         
00141         ps = PointStamped()
00142         ps.header.frame_id = '/base_link'
00143 
00144         ps2 = PointStamped()
00145         ps2.header.frame_id = '/base_link'
00146         ps2.point.x = 0.1
00147 
00148         # Begin Servoing.  Warning: Hack!
00149         sm = ServoMode( self.follow1 )
00150         #bp = BasePose( )
00151         pts = []
00152         
00153         t0 = rospy.Time.now().to_sec()
00154         while sm.done == False:
00155             if rospy.Time.now().to_sec() - t0 > 180:
00156                 rospy.logout( 'sim_capture: Time up.  Aborting.' )
00157                 self.abort()
00158 
00159             try:
00160                 ps.header.stamp = rospy.Time(0)
00161                 ps_map = self.listener.transformPoint( '/map', ps )
00162                 x = ps_map.point.x
00163                 y = ps_map.point.y
00164 
00165                 ps2.header.stamp = rospy.Time(0)
00166                 ps2_map = self.listener.transformPoint( '/map', ps2 )
00167 
00168                 pts.append([ x, y, ps2_map.point.x, ps2_map.point.y ])
00169 
00170                 inside = x > -0.5 and x < 10.5 and y > -0.5 and y < 6.5
00171                 if not inside:
00172                     self.abort()
00173 
00174             except:
00175                 rospy.logout( 'sim_capture: Failed transform.' )
00176                 pass
00177                     
00178             rospy.sleep( 0.2 )
00179         rospy.logout( 'sim_capture: Done servoing.' )
00180         #pts = list( bp.pts )
00181 
00182         sm.stop()
00183         #bp.stop()
00184 
00185         # Stop recorder and shuttle to disk.
00186         if fname:
00187             f = open( fname, 'w' )
00188         else:
00189             f = open( 'trajectory_caps/' + str(int(time.time())) + '_cap.pkl', 'w' )
00190         pkl.dump( pts, f )
00191         f.close()
00192         
00193         rospy.logout( 'sim_capture: Capture complete' )
00194         return int( True )
00195 
00196         
00197         
00198         
00199 if __name__ == '__main__':
00200     sc = SimCapture()
00201     rospy.spin()


rfid_people_following
Author(s): Travis Deyle (Healthcare Robotics Lab, Georgia Tech)
autogenerated on Wed Nov 27 2013 11:38:30