util.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 import roslib
00004 roslib.load_manifest('room_explore')
00005 import rospy
00006 
00007 import tf
00008 
00009 from geometry_msgs.msg import PoseStamped
00010 from visualization_msgs.msg import Marker, MarkerArray
00011 from geometry_msgs.msg import Vector3, PoseStamped, Quaternion
00012 from std_msgs.msg import ColorRGBA
00013 
00014 from threading import Thread
00015 import numpy as np, math
00016 
00017 class TFthread( Thread ):
00018     def __init__( self, df ):
00019         Thread.__init__( self )
00020         try:
00021             rospy.init_node( 'TFthread' )
00022         except:
00023             pass # parent probably already initialized node.
00024         self.df = df # yaml dictionary of rooms
00025         self.bc = tf.TransformBroadcaster()
00026         self.should_run = True
00027         self.start()
00028 
00029     def publish_transform( self ):
00030         for room in self.df.keys():
00031             self.bc.sendTransform( ( self.df[room]['position']['x'],
00032                                      self.df[room]['position']['y'],
00033                                      self.df[room]['position']['z'] ),
00034                                    ( self.df[room]['orientation']['x'],
00035                                      self.df[room]['orientation']['y'],
00036                                      self.df[room]['orientation']['z'],
00037                                      self.df[room]['orientation']['w'] ),
00038                                    rospy.Time.now(),
00039                                    room,
00040                                    'map' )
00041 
00042     def run( self ):
00043         rospy.logout( 'TFthread: Starting ' )
00044         r = rospy.Rate( 10 )
00045         while self.should_run and not rospy.is_shutdown():
00046             self.publish_transform()
00047             try:
00048                 r.sleep()
00049             except:
00050                 pass # ROS exception.
00051         rospy.logout( 'TFthread: Starting ')
00052         
00053     def stop( self ):
00054         # Kill off the poller thread.
00055         self.should_run = False
00056         self.join(5)
00057         if (self.isAlive()):
00058             raise RuntimeError('TFthread: Unable to stop thread ' )
00059 
00060 
00061 class PosesMarkers( Thread ):
00062     def __init__( self ):
00063         Thread.__init__( self )
00064         try:
00065             rospy.init_node( 'snaking_room_posesmarkers' )
00066         except:
00067             pass # parent probably already initialized node.
00068 
00069         self.poses = []
00070         self.markers = []
00071         self.mid = 0
00072         self.new_poses = False
00073         
00074         self.should_run = True
00075         self.pub = rospy.Publisher( 'visarr', Marker )
00076         self.start()
00077 
00078     def update_poses( self, poses ):
00079         self.poses = list( poses )
00080         self.new_poses = True
00081 
00082     def run( self ):
00083         rospy.logout( 'PosesMarkers: Starting ' )
00084         r = rospy.Rate( 5 )
00085         while self.should_run and not rospy.is_shutdown():
00086             if self.new_poses == True:
00087                 self.new_poses = False
00088 
00089                 # Delete old markers
00090                 for old_m in self.markers:
00091                     old_m.action = old_m.DELETE
00092                     self.pub.publish( old_m )
00093 
00094                 # Create new markers
00095                 self.markers = []
00096                 for p in self.poses:
00097                     self.mid += 1
00098                     m = Marker()
00099                     m.ns = 'explore_poses'
00100                     m.id = self.mid
00101                     m.action = m.ADD
00102                     m.type = m.ARROW
00103                     m.header.frame_id = p.header.frame_id
00104                     m.header.stamp = rospy.Time.now()
00105                     m.scale = Vector3( 0.15, 0.75, 0.75 )
00106                     m.color = ColorRGBA( 0.2, 0.2, 1.0, 0.9 )
00107                     m.pose = p.pose
00108                     self.markers.append( m )
00109 
00110             # Publish markers.
00111             for m in self.markers:
00112                 m.header.stamp = rospy.Time.now()
00113                 self.pub.publish( m )
00114 
00115             r.sleep()
00116 
00117         # Delete old markers
00118         for old_m in self.markers:
00119             old_m.action = old_m.DELETE
00120             self.pub.publish( old_m )
00121 
00122         rospy.logout( 'PoseMarkers: Stopping. ')
00123         
00124     def stop( self ):
00125         # Kill off the poller thread.
00126         self.should_run = False
00127         self.join(5)
00128         if (self.isAlive()):
00129             raise RuntimeError('TFthread: Unable to stop thread ' )
00130 
00131 
00132 
00133 def standard_rad(t):
00134     if t > 0:
00135         return ((t + np.pi) % (np.pi * 2))  - np.pi
00136     else:
00137         return ((t - np.pi) % (np.pi * -2)) + np.pi
00138 
00139 
00140 def robot_in_map( l, fail_msg = '' ):
00141     # l is tf listener
00142     ps = PoseStamped()
00143     ps.header.frame_id = '/base_link'
00144     ps.pose.orientation.w = 1.0
00145 
00146     try:
00147         ps_map = l.transformPose( '/map', ps )
00148     except:
00149         rospy.logout( fail_msg + 'Transform failed.' )
00150         ps_map = None
00151     return ps_map
00152 


rfid_explore_room
Author(s): Travis Deyle
autogenerated on Wed Nov 27 2013 12:04:36