patrol_routine.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 
5 from strands_executive_msgs import task_utils
6 from strands_executive_msgs.msg import Task
7 from strands_navigation_msgs.msg import TopologicalMap
8 from strands_navigation_msgs.srv import EstimateTravelTime
9 from mongodb_store_msgs.msg import StringList
10 from mongodb_store.message_store import MessageStoreProxy
11 
12 from datetime import time, date, timedelta
13 from dateutil.tz import tzlocal
14 
15 from routine_behaviours.robot_routine import RobotRoutine
16 
17 import random
18 
19 
20 def create_patrol_task(waypoint_name):
21  return Task(start_node_id=waypoint_name, end_node_id=waypoint_name, max_duration=rospy.Duration(30))
22 
23 
25  """ Creates a routine which simply visits nodes. """
26 
27  def __init__(self, daily_start, daily_end, tour_duration_estimate=None, idle_duration=rospy.Duration(5), charging_point = 'ChargingPoint'):
28  # super(PatrolRoutine, self).__init__(daily_start, daily_end)
29  RobotRoutine.__init__(self, daily_start, daily_end, idle_duration=idle_duration, charging_point=charging_point)
30  self.node_names = set()
31  self.tour_duration_estimate = tour_duration_estimate
32  rospy.Subscriber('topological_map', TopologicalMap, self.map_callback)
33  self.random_nodes = []
34 
35  def map_callback(self, msg):
36  print 'got map: %s' % len(msg.nodes)
37  self.node_names = set([node.name for node in msg.nodes if node.name != 'ChargingPoint'])
38  if len(self.random_nodes) == 0:
39  self.random_nodes = list(self.node_names)
40 
41  def get_nodes(self):
42  while len(self.node_names) == 0:
43  print 'no nodes'
44  rospy.sleep(1)
45  return self.node_names
46 
47 
48  def all_waypoints(self):
49  return self.get_nodes()
50 
51  def all_waypoints_except(self, exceptions = []):
52  return self.all_waypoints() - set(exceptions)
53 
54 
55  def max_single_trip_time(self, waypoints):
56 
57  expected_time = rospy.ServiceProxy('topological_navigation/travel_time_estimator', EstimateTravelTime)
58 
59  max_time = rospy.Duration(0)
60  for start in waypoints:
61  for end in waypoints:
62  if start != end:
63  et = expected_time(start=start, target=end).travel_time
64  if et > max_time:
65  max_time = et
66 
67  return max_time
68 
69  def create_patrol_routine(self, waypoints=None, daily_start=None, daily_end=None, repeat_delta=None):
70  if not waypoints:
71  waypoints = self.get_nodes()
72 
73  if not repeat_delta:
74  # ignoring this now
75  # if not self.tour_duration_estimate:
76  single_node_estimate = self.max_single_trip_time(waypoints).to_sec()
77  tour_duration_estimate = single_node_estimate * (len(waypoints)-1) * 2
78  repeat_delta = timedelta(seconds=tour_duration_estimate)
79 
80  tasks = [ create_patrol_task(n) for n in waypoints ]
81 
82  self.create_task_routine(tasks=tasks, daily_start=daily_start, daily_end=daily_end, repeat_delta=repeat_delta)
83 
84 
85 
86  def create_routine(self):
87 
89 
90 
91 
92  def on_idle(self):
93  """
94  Called when the routine is idle. Default is to trigger travel to the charging. As idleness is determined by the current schedule, if this call doesn't utlimately cause a task schedule to be generated this will be called repeatedly.
95  """
96  if not isinstance(self.random_nodes, list):
97  self.random_nodes = list(self.random_nodes)
98 
99  rospy.loginfo('Idle for too long, generating a random waypoint task')
100  self.add_tasks([create_patrol_task(random.choice(self.random_nodes))])
101 
102 
def create_patrol_task(waypoint_name)


routine_behaviours
Author(s): Nick Hawes
autogenerated on Tue Mar 17 2015 21:43:31