task_setup.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import rospy
00004 import actionlib
00005 from actionlib import GoalStatus
00006 from geometry_msgs.msg import Pose, Point, Quaternion, Twist
00007 from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal, MoveBaseActionFeedback
00008 from tf.transformations import quaternion_from_euler
00009 from visualization_msgs.msg import Marker
00010 from math import  pi
00011 from collections import OrderedDict
00012 
00013 def setup_task_environment(self):
00014     # How big is the square we want the robot to patrol?
00015     self.square_size = rospy.get_param("~square_size", 1.0) # meters
00016     
00017     # Set the low battery threshold (between 0 and 100)
00018     self.low_battery_threshold = rospy.get_param('~low_battery_threshold', 50)
00019     
00020     # How many times should we execute the patrol loop
00021     self.n_patrols = rospy.get_param("~n_patrols", 2) # meters
00022     
00023     # How long do we have to get to each waypoint?
00024     self.move_base_timeout = rospy.get_param("~move_base_timeout", 10) #seconds
00025     
00026     # Initialize the patrol counter
00027     self.patrol_count = 0
00028     
00029     # Subscribe to the move_base action server
00030     self.move_base = actionlib.SimpleActionClient("move_base", MoveBaseAction)
00031     
00032     rospy.loginfo("Waiting for move_base action server...")
00033     
00034     # Wait up to 60 seconds for the action server to become available
00035     self.move_base.wait_for_server(rospy.Duration(60))    
00036     
00037     rospy.loginfo("Connected to move_base action server")
00038     
00039     # Create a list to hold the target quaternions (orientations)
00040     quaternions = list()
00041     
00042     # First define the corner orientations as Euler angles
00043     euler_angles = (pi/2, pi, 3*pi/2, 0)
00044     
00045     # Then convert the angles to quaternions
00046     for angle in euler_angles:
00047         q_angle = quaternion_from_euler(0, 0, angle, axes='sxyz')
00048         q = Quaternion(*q_angle)
00049         quaternions.append(q)
00050     
00051     # Create a list to hold the waypoint poses
00052     self.waypoints = list()
00053             
00054     # Append each of the four waypoints to the list.  Each waypoint
00055     # is a pose consisting of a position and orientation in the map frame.
00056     self.waypoints.append(Pose(Point(0.0, 0.0, 0.0), quaternions[3]))
00057     self.waypoints.append(Pose(Point(self.square_size, 0.0, 0.0), quaternions[0]))
00058     self.waypoints.append(Pose(Point(self.square_size, self.square_size, 0.0), quaternions[1]))
00059     self.waypoints.append(Pose(Point(0.0, self.square_size, 0.0), quaternions[2]))
00060     
00061     # Create a mapping of room names to waypoint locations
00062     room_locations = (('hallway', self.waypoints[0]),
00063                       ('living_room', self.waypoints[1]),
00064                       ('kitchen', self.waypoints[2]),
00065                       ('bathroom', self.waypoints[3]))
00066     
00067     # Store the mapping as an ordered dictionary so we can visit the rooms in sequence
00068     self.room_locations = OrderedDict(room_locations)
00069     
00070     # Where is the docking station?
00071     self.docking_station_pose = (Pose(Point(0.5, 0.5, 0.0), Quaternion(0.0, 0.0, 0.0, 1.0)))            
00072     
00073     # Initialize the waypoint visualization markers for RViz
00074     init_waypoint_markers(self)
00075     
00076     # Set a visualization marker at each waypoint        
00077     for waypoint in self.waypoints:           
00078         p = Point()
00079         p = waypoint.position
00080         self.waypoint_markers.points.append(p)
00081         
00082     # Set a marker for the docking station
00083     init_docking_station_marker(self)
00084         
00085     # Publisher to manually control the robot (e.g. to stop it)
00086     self.cmd_vel_pub = rospy.Publisher('cmd_vel', Twist, queue_size=5)
00087     
00088     rospy.loginfo("Starting Tasks")
00089     
00090     # Publish the waypoint markers
00091     self.marker_pub.publish(self.waypoint_markers)
00092     rospy.sleep(1)
00093     self.marker_pub.publish(self.waypoint_markers)
00094     
00095     # Publish the docking station marker
00096     self.docking_station_marker_pub.publish(self.docking_station_marker)
00097     rospy.sleep(1)
00098 
00099 def init_waypoint_markers(self):
00100     # Set up our waypoint markers
00101     marker_scale = 0.2
00102     marker_lifetime = 0 # 0 is forever
00103     marker_ns = 'waypoints'
00104     marker_id = 0
00105     marker_color = {'r': 1.0, 'g': 0.7, 'b': 1.0, 'a': 1.0}
00106     
00107     # Define a marker publisher.
00108     self.marker_pub = rospy.Publisher('waypoint_markers', Marker, queue_size=5)
00109     
00110     # Initialize the marker points list.
00111     self.waypoint_markers = Marker()
00112     self.waypoint_markers.ns = marker_ns
00113     self.waypoint_markers.id = marker_id
00114     self.waypoint_markers.type = Marker.CUBE_LIST
00115     self.waypoint_markers.action = Marker.ADD
00116     self.waypoint_markers.lifetime = rospy.Duration(marker_lifetime)
00117     self.waypoint_markers.scale.x = marker_scale
00118     self.waypoint_markers.scale.y = marker_scale
00119     self.waypoint_markers.color.r = marker_color['r']
00120     self.waypoint_markers.color.g = marker_color['g']
00121     self.waypoint_markers.color.b = marker_color['b']
00122     self.waypoint_markers.color.a = marker_color['a']
00123     
00124     self.waypoint_markers.header.frame_id = 'odom'
00125     self.waypoint_markers.header.stamp = rospy.Time.now()
00126     self.waypoint_markers.points = list()
00127 
00128 def init_docking_station_marker(self):
00129     # Define a marker for the charging station
00130     marker_scale = 0.3
00131     marker_lifetime = 0 # 0 is forever
00132     marker_ns = 'waypoints'
00133     marker_id = 0
00134     marker_color = {'r': 0.7, 'g': 0.7, 'b': 0.0, 'a': 1.0}
00135     
00136     self.docking_station_marker_pub = rospy.Publisher('docking_station_marker', Marker, queue_size=5)
00137     
00138     self.docking_station_marker = Marker()
00139     self.docking_station_marker.ns = marker_ns
00140     self.docking_station_marker.id = marker_id
00141     self.docking_station_marker.type = Marker.CYLINDER
00142     self.docking_station_marker.action = Marker.ADD
00143     self.docking_station_marker.lifetime = rospy.Duration(marker_lifetime)
00144     self.docking_station_marker.scale.x = marker_scale
00145     self.docking_station_marker.scale.y = marker_scale
00146     self.docking_station_marker.scale.z = 0.02
00147     self.docking_station_marker.color.r = marker_color['r']
00148     self.docking_station_marker.color.g = marker_color['g']
00149     self.docking_station_marker.color.b = marker_color['b']
00150     self.docking_station_marker.color.a = marker_color['a']
00151     
00152     self.docking_station_marker.header.frame_id = 'odom'
00153     self.docking_station_marker.header.stamp = rospy.Time.now()
00154     self.docking_station_marker.pose = self.docking_station_pose


pi_trees_lib
Author(s): Patrick Goebel
autogenerated on Thu Jun 6 2019 17:33:29