tour.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import random
4 import rospy
5 import actionlib
6 import waypoint_touring.utils as utils
7 
8 import move_base_msgs.msg as move_base_msgs
9 import visualization_msgs.msg as viz_msgs
10 
11 class TourMachine(object):
12 
13  def __init__(self, filename, random_visits=False, repeat=False):
14  self._waypoints = utils.get_waypoints(filename)
15 
16  action_name = 'move_base'
17  self._ac_move_base = actionlib.SimpleActionClient(action_name, move_base_msgs.MoveBaseAction)
18  rospy.loginfo('Wait for %s server' % action_name)
19  self._ac_move_base.wait_for_server
20  self._counter = 0
21  self._repeat = repeat
22  self._random_visits = random_visits
23 
24  if self._random_visits:
25  random.shuffle(self._waypoints)
26  self._pub_viz_marker = rospy.Publisher('viz_waypoints', viz_msgs.MarkerArray, queue_size=1, latch=True)
27  self._viz_markers = utils.create_viz_markers(self._waypoints)
28 
29  def move_to_next(self):
30  p = self._get_next_destination()
31 
32  if not p:
33  rospy.loginfo("Finishing Tour")
34  return True
35 
36  goal = utils.create_move_base_goal(p)
37  rospy.loginfo("Move to %s" % p['name'])
38  self._ac_move_base.send_goal(goal)
39  self._ac_move_base.wait_for_result()
40  result = self._ac_move_base.get_result()
41  rospy.loginfo("Result : %s" % result)
42 
43  return False
44 
46  if self._counter == len(self._waypoints):
47  if self._repeat:
48  self._counter = 0
49  if self._random_visits:
50  random.shuffle(self._waypoints)
51  else:
52  next_destination = None
53  next_destination = self._waypoints[self._counter]
54  self._counter = self._counter + 1
55  return next_destination
56 
57  def spin(self):
58  rospy.sleep(1.0)
59  self._pub_viz_marker.publish(self._viz_markers)
60  finished = False
61  while not rospy.is_shutdown() and not finished:
62  finished = self.move_to_next()
63  rospy.sleep(2.0)
64 
65 if __name__ == '__main__':
66  rospy.init_node('tour')
67 
68  filename = rospy.get_param('~filename')
69  random_visits = rospy.get_param('~random', False)
70  repeat = rospy.get_param('~repeat', False)
71 
72  m = TourMachine(filename, random_visits, repeat)
73  rospy.loginfo('Initialized')
74  m.spin()
75  rospy.loginfo('Bye Bye')
def move_to_next(self)
Definition: tour.py:29
def __init__(self, filename, random_visits=False, repeat=False)
Definition: tour.py:13
def _get_next_destination(self)
Definition: tour.py:45
def spin(self)
Definition: tour.py:57


waypoint_touring
Author(s):
autogenerated on Mon Jun 10 2019 15:49:18