tour.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import random
00004 import rospy
00005 import actionlib
00006 import waypoint_touring.utils as utils
00007 
00008 import move_base_msgs.msg as move_base_msgs
00009 import visualization_msgs.msg as viz_msgs
00010 
00011 class TourMachine(object):
00012 
00013     def __init__(self, filename, random_visits=False, repeat=False):
00014         self._waypoints = utils.get_waypoints(filename)
00015 
00016         action_name = 'move_base'
00017         self._ac_move_base = actionlib.SimpleActionClient(action_name, move_base_msgs.MoveBaseAction)
00018         rospy.loginfo('Wait for %s server' % action_name)
00019         self._ac_move_base.wait_for_server
00020         self._counter = 0
00021         self._repeat = repeat
00022         self._random_visits = random_visits
00023 
00024         if self._random_visits:
00025             random.shuffle(self._waypoints)
00026         self._pub_viz_marker = rospy.Publisher('viz_waypoints', viz_msgs.MarkerArray, queue_size=1, latch=True)
00027         self._viz_markers = utils.create_viz_markers(self._waypoints)
00028 
00029     def move_to_next(self):
00030         p = self._get_next_destination()
00031 
00032         if not p:
00033             rospy.loginfo("Finishing Tour")
00034             return True
00035 
00036         goal = utils.create_move_base_goal(p)
00037         rospy.loginfo("Move to %s" % p['name'])
00038         self._ac_move_base.send_goal(goal)
00039         self._ac_move_base.wait_for_result()
00040         result = self._ac_move_base.get_result()
00041         rospy.loginfo("Result : %s" % result)
00042 
00043         return False
00044 
00045     def _get_next_destination(self):
00046         if self._counter == len(self._waypoints):
00047             if self._repeat:
00048                 self._counter = 0
00049                 if self._random_visits:
00050                     random.shuffle(self._waypoints)
00051             else:
00052                 next_destination = None
00053         next_destination = self._waypoints[self._counter]
00054         self._counter = self._counter + 1
00055         return next_destination
00056 
00057     def spin(self):
00058         rospy.sleep(1.0)
00059         self._pub_viz_marker.publish(self._viz_markers)
00060         finished = False
00061         while not rospy.is_shutdown() and not finished:
00062             finished = self.move_to_next()
00063             rospy.sleep(2.0)
00064 
00065 if __name__ == '__main__':
00066     rospy.init_node('tour')
00067 
00068     filename = rospy.get_param('~filename')
00069     random_visits = rospy.get_param('~random', False)
00070     repeat = rospy.get_param('~repeat', False)
00071 
00072     m = TourMachine(filename, random_visits, repeat)
00073     rospy.loginfo('Initialized')
00074     m.spin()
00075     rospy.loginfo('Bye Bye')


waypoint_touring
Author(s):
autogenerated on Thu Jun 6 2019 21:48:43