utils.py
Go to the documentation of this file.
00001 #! /usr/bin/python
00002 import roslib
00003 roslib.load_manifest('rfid_datacapture')
00004 roslib.load_manifest('geometry_msgs')
00005 roslib.load_manifest('move_base_msgs')
00006 roslib.load_manifest('tf')
00007 roslib.load_manifest('sound_play')
00008 roslib.load_manifest('hrl_lib')
00009 import rospy
00010 
00011 import smach
00012 from smach_ros import SimpleActionState, ServiceState, IntrospectionServer
00013 import actionlib
00014 import tf
00015 import tf.transformations as tft
00016 
00017 from geometry_msgs.msg import PoseStamped, Quaternion
00018 from sound_play.msg import SoundRequest
00019 from rfid_datacapture.srv import BagCapture
00020 from sound_play.msg import SoundRequest
00021 
00022 from hrl_lib.cmd_process import CmdProcess
00023 
00024 import yaml
00025 import numpy as np, math
00026 import os
00027 import time
00028 
00029 class BagCap():
00030     def __init__( self, topic_name = 'capture' ):
00031         self._srv = rospy.Service( 'bag_cap/'+topic_name, BagCapture, self.process_srv )
00032 
00033     def process_srv( self, req ):
00034         cmd = 'rosbag record -O %s ' % req.dest
00035         cmd += req.topics
00036         if req.topics != '':
00037             self.bag_cap = CmdProcess( cmd.split() )
00038             self.bag_cap.run()
00039         else:
00040             self.bag_cap.kill()
00041         return True
00042             
00043 
00044 class YAMLproc(smach.State):
00045     def __init__(self, fname):
00046         smach.State.__init__(self,
00047                              outcomes=['succeeded', 'aborted'],
00048                              output_keys = ['next_move_pose','bagfile_name',
00049                                             'bagfile_topics','tagid'])
00050         self.fname = fname
00051         self.d = None
00052 
00053     def execute(self, userdata):
00054         if not self.d:
00055             f = open( self.fname, 'r' )
00056             self.d = yaml.load( f )
00057             self.size = len( self.d['servo_cap']['captures'] ) 
00058             self.ind = 0
00059             f.close()
00060 
00061         if self.ind < self.size:
00062             rospy.logout( 'YamlProc: issuing %d of %d' % (self.ind+1, self.size))
00063             ps = PoseStamped()
00064             ps.header.frame_id = '/map'
00065             ps.header.stamp = rospy.Time(0)
00066 
00067             ps.pose.position.x = self.d['servo_cap']['captures'][ self.ind ][0]
00068             ps.pose.position.y = self.d['servo_cap']['captures'][ self.ind ][1]
00069             ang = self.d['servo_cap']['captures'][ self.ind ][2]
00070             q = Quaternion( *tft.quaternion_from_euler( 0.0, 0.0, math.radians( ang )))
00071             ps.pose.orientation = q
00072 
00073             self.ind += 1
00074             
00075             userdata.next_move_pose = ps
00076             userdata.tagid = self.d['servo_cap']['tagid']
00077             userdata.bagfile_name = self.d['servo_cap']['bagfile_path'] + str(int(rospy.Time.now().to_sec()))
00078             userdata.bagfile_topics = self.d['servo_cap']['bagfile_topics']
00079             return 'succeeded'
00080         else:
00081             return 'aborted' # done!
00082 
00083 
00084 class YAMLprocPoses(smach.State):
00085     def __init__(self, fname):
00086         smach.State.__init__(self,
00087                              outcomes=['succeeded', 'aborted'],
00088                              output_keys = ['next_move_pose'])
00089         self.fname = fname
00090         self.d = None
00091 
00092     def execute(self, userdata):
00093         if not self.d:
00094             f = open( self.fname, 'r' )
00095             self.d = yaml.load( f )
00096             self.size = len( self.d['rad_cap']['captures'] ) 
00097             self.ind = 0
00098             f.close()
00099             self.t0 = rospy.Time.now().to_sec()
00100 
00101         if self.ind < self.size:
00102             rospy.logout( 'YamlProcPoses: issuing %d of %d' % (self.ind+1, self.size))
00103             rospy.logout( 'YamlProcPoses: Time to capture %2.2f' % ( rospy.Time.now().to_sec() - self.t0 ))
00104             self.t0 = rospy.Time.now().to_sec()
00105             ps = PoseStamped()
00106             ps.header.frame_id = '/map'
00107             ps.header.stamp = rospy.Time(0)
00108 
00109             ps.pose.position.x = self.d['rad_cap']['captures'][ self.ind ][0]
00110             ps.pose.position.y = self.d['rad_cap']['captures'][ self.ind ][1]
00111             ang = self.d['rad_cap']['captures'][ self.ind ][2]
00112             q = Quaternion( *tft.quaternion_from_euler( 0.0, 0.0, math.radians( ang )))
00113             ps.pose.orientation = q
00114 
00115             self.ind += 1
00116             
00117             userdata.next_move_pose = ps
00118             return 'succeeded'
00119         else:
00120             return 'aborted' # done!
00121 
00122 class YAMLprocMultitag(smach.State):
00123     def __init__(self, fname):
00124         smach.State.__init__(self,
00125                              outcomes=['succeeded', 'aborted'],
00126                              output_keys = ['bagfile_name',
00127                                             'bagfile_topics',
00128                                             'tagid',
00129                                             'panrate',
00130                                             'tilt_left',
00131                                             'tilt_right',
00132                                             'tilt_rate',
00133                                             'tilt_block'])
00134         self.fname = fname
00135         self.d = None
00136 
00137     def execute(self, userdata):
00138         if not self.d:
00139             f = open( self.fname, 'r' )
00140             self.d = yaml.load( f )
00141             self.tagids = self.d['rad_cap']['tagids'].keys()
00142             self.keys_list = list( self.tagids )
00143             f.close()
00144             self.tilt_angs = []
00145 
00146         if not self.keys_list and not self.tilt_angs: # Done! reset.
00147             self.keys_list = list( self.tagids )
00148             return 'aborted'
00149 
00150         if not self.tilt_angs: # get next tagid
00151             self.tid = self.keys_list.pop()
00152             self.tilt_angs = list( self.d['rad_cap']['tagids'][self.tid]['tilts'] )
00153             rospy.logout( 'setting tid: %s' % self.tid )
00154             print 'Tilt angs: ', self.tilt_angs
00155 
00156         ta = self.tilt_angs.pop()[0]
00157         rospy.logout( 'Using tilt angle: %2.2f' % ta )
00158         userdata.tilt_left = -1.0 * ta
00159         userdata.tilt_right = ta
00160         userdata.tilt_rate = 0.4
00161         userdata.tilt_block = 1
00162         tad = math.degrees( ta )
00163 
00164         rospy.logout( 'YamlProcMultitag: issuing %d of %d for tag \'%s\'' %
00165                       (len(self.tagids) - len(self.keys_list), len(self.tagids), self.tid))
00166 
00167         userdata.tagid = self.tid
00168 
00169         tmp = self.tid
00170         path = self.d['rad_cap']['bagfile_path']
00171         tsec = str(int(rospy.Time.now().to_sec()))
00172         tads = str(int( tad ))
00173         userdata.bagfile_name = path + tsec + '_' + tads + '_' + tmp.replace(' ', '' )
00174         userdata.bagfile_topics = self.d['rad_cap']['bagfile_topics']
00175         userdata.panrate = 30.0
00176         return 'succeeded'
00177 
00178 
00179 class ManualSkip(smach.State):
00180     def __init__(self):
00181         smach.State.__init__(self,outcomes=['succeeded', 'aborted'])
00182         self.init = None
00183 
00184     def execute(self, userdata):
00185         if not self.init:
00186             self.init = True
00187             self.pub = rospy.Publisher( 'robotsound', SoundRequest )
00188             rospy.sleep( 1.0 )
00189 
00190         self.pub.publish( SoundRequest( -3, 1, 'MANUAL or SKIP' ))
00191         ui = ''
00192         while not (ui == '0' or ui == '1'):
00193             print '\'0\' to Position Manually (position robot first)'
00194             print '\'1\' to skip this position'
00195             ui = raw_input()
00196         if ui == '0':
00197             return 'succeeded'  # Robot manually positioned
00198         else:
00199             return 'aborted'    # Forget this position
00200 
00201 class MoveNotify(smach.State):
00202     def __init__(self):
00203         smach.State.__init__(self,outcomes=['succeeded', 'aborted'])
00204         self.init = None
00205 
00206     def execute(self, userdata):
00207         if not self.init:
00208             self.init = True
00209             self.pub = rospy.Publisher( 'robotsound', SoundRequest )
00210             rospy.sleep( 1.0 )
00211 
00212         self.pub.publish( SoundRequest( -3, 1, 'Ready to Move' ))
00213         ui = ''
00214         while not (ui == '0' or ui == '1'):
00215             print '\'0\' or \'1\' when Ready.'
00216             ui = raw_input()
00217 
00218         return 'succeeded'
00219 
00220 
00221 
00222 if __name__ == '__main__':
00223     rospy.init_node( 'bag_capture_services' )
00224     
00225     bc = BagCap()
00226     rospy.spin()
00227 
00228     # rosservice call /bag_cap/capture '/tf' 'out'
00229     # rosservice call /bag_cap/capture '' ''
00230 


rfid_datacapture
Author(s): Travis Deyle
autogenerated on Wed Nov 27 2013 12:11:16