continuous_patrolling.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 
5 from strands_executive_msgs.msg import Task
6 from strands_executive_msgs.srv import AddTasks, SetExecutionStatus
7 from std_msgs.msg import String
8 from random import shuffle
9 from strands_navigation_msgs.msg import TopologicalNode
10 from strands_navigation_msgs.msg import TopologicalMap
11 
12 
13 class PatrolScheduler(object):
14  """ Repeatedly sends a randomised list of task to the task executor. These don't have tasks associated with them. If you want to add tasks, see fifo_tester.py for an example."""
15  def __init__(self):
16  self.node_names = []
17  rospy.Subscriber('topological_map', TopologicalMap, self.map_callback)
18 
19  # load waypoint from datacentre
20  self.waypoints = self.get_nodes()
21  rospy.loginfo('Patrolling the following nodes: %s' % self.waypoints)
22  self.current_waypoint = ''
23  self.add_tasks_srv = None
24  rospy.Subscriber("/current_node", String, self.current_node_cb)
25 
26  add_tasks_srv_name = 'task_executor/add_tasks'
27  set_exe_stat_srv_name = 'task_executor/set_execution_status'
28  rospy.loginfo("Waiting for task_executor service...")
29  rospy.wait_for_service(add_tasks_srv_name)
30  rospy.wait_for_service(set_exe_stat_srv_name)
31  rospy.loginfo("Done")
32  self.add_tasks_srv = rospy.ServiceProxy(add_tasks_srv_name, AddTasks)
33  set_execution_status_srv = rospy.ServiceProxy(set_exe_stat_srv_name, SetExecutionStatus)
34 
35  shuffle(self.waypoints)
36  self.send_tasks()
37 
38  try:
39  # Make sure the task executor is running
40  set_execution_status_srv(True)
41  except rospy.ServiceException, e:
42  print "Service call failed: %s"%e
43 
44 
45  def map_callback(self, msg):
46  print 'got map: %s' % len(msg.nodes)
47  self.node_names = [node.name for node in msg.nodes]
48 
49  def get_nodes(self):
50  while len(self.node_names) == 0:
51  print 'no nodes'
52  rospy.sleep(1)
53  return self.node_names
54 
55  def send_tasks(self):
56  rospy.loginfo("Sending next batch of patrol tasks")
57 
58  patrol_tasks = [Task(start_node_id=wp, end_node_id=wp) for wp in self.waypoints]
59 
60  self.add_tasks_srv(patrol_tasks)
61 
62 
63  def current_node_cb(self, data):
64 
65  # if we have just changed nodes
66  if self.current_waypoint != data.data:
67  rospy.loginfo("Changed nodes to %s from %s" % (data.data, self.current_waypoint))
68  self.current_waypoint = data.data
69  # if we're on the last waypoint, send the tasks again
70  # actually this is incorrect as we could pass through a node on the way to somewhere else, but it doesn't really matter
71  if self.current_waypoint == self.waypoints[-1]:
72  shuffle(self.waypoints)
73  self.send_tasks()
74 
75 
76 
77 if __name__ == '__main__':
78  rospy.init_node("patrol_scheduler")
79 
80  patrol_scheduler = PatrolScheduler()
81  rospy.spin()
82 
83 
84 
85 


task_executor
Author(s): Nick Hawes
autogenerated on Tue Mar 17 2015 20:08:13