example_task_client.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 import mongodb_store_msgs.srv as dc_srv
5 from mongodb_store_msgs.msg import StringPair
6 import mongodb_store.util as dc_util
7 from mongodb_store.message_store import MessageStoreProxy
8 from geometry_msgs.msg import Pose, Point, Quaternion
9 import StringIO
10 import random
11 
12 from strands_executive_msgs import task_utils
13 from strands_executive_msgs.msg import Task
14 from strands_executive_msgs.srv import AddTasks, SetExecutionStatus
15 # import strands_executive_msgs
16 
17 import rospy
18 import actionlib
19 from strands_executive_msgs.msg import Task
20 from task_executor.msg import *
21 from topological_navigation.msg import GotoNodeAction, GotoNodeResult
22 from copy import deepcopy
23 from random import random
24 
25 from task_executor.utils import TestTaskAction
26 
27 
29  # get services necessary to do the jon
30  add_tasks_srv_name = '/task_executor/add_tasks'
31  set_exe_stat_srv_name = '/task_executor/set_execution_status'
32  rospy.loginfo("Waiting for task_executor service...")
33  rospy.wait_for_service(add_tasks_srv_name)
34  rospy.wait_for_service(set_exe_stat_srv_name)
35  rospy.loginfo("Done")
36  add_tasks_srv = rospy.ServiceProxy(add_tasks_srv_name, AddTasks)
37  set_execution_status = rospy.ServiceProxy(set_exe_stat_srv_name, SetExecutionStatus)
38  return add_tasks_srv, set_execution_status
39 
41  """
42  Publish a topological map for testing.
43  """
44  # create a test topological map
45  width = 5
46  height = 5
47  nodeSeparation = 10.0
48 
49  test_nodes = topological_navigation.testing.create_cross_map(width = width, height = height, nodeSeparation = nodeSeparation)
50 
51  # now insert the map into the database
52  msg_store = MessageStoreProxy(collection='topological_maps')
53 
54  map_name = 'test_top_map'
55 
56  meta = {}
57  meta['map'] = map_name
58  meta['pointset'] = map_name
59 
60  for (nodeName, node) in test_nodes.iteritems():
61  meta["node"] = nodeName
62  node.map = meta['map']
63  node.pointset = meta['pointset']
64  msg_store.insert(node,meta)
65 
66  # and publish the map
67  ps = map_publisher(map_name)
68 
69  return test_nodes
70 
71 
72 
74  """
75  Create an example of a task which we'll copy for other tasks later.
76  This is a good example of creating a task with a variety of arguments.
77  """
78  # need message store to pass objects around
79  msg_store = MessageStoreProxy()
80 
81  # get the pose of a named object
82  pose_name = "my favourite pose"
83 
84  # get the pose if it's there
85  message, meta = msg_store.query_named(pose_name, Pose._type)
86  # if it's not there, add it in
87  if message == None:
88  message = Pose(Point(0, 1, 2), Quaternion(3, 4, 5, 6))
89  pose_id = msg_store.insert_named(pose_name, message)
90  else:
91  pose_id = meta["_id"]
92 
93  master_task = Task(action='test_task')
94  task_utils.add_string_argument(master_task, 'hello world')
95  task_utils.add_object_id_argument(master_task, pose_id, Pose)
96  task_utils.add_int_argument(master_task, 24)
97  task_utils.add_float_argument(master_task, 63.678)
98  return master_task
99 
100 
101 if __name__ == '__main__':
102  rospy.init_node("example_task_client")
103 
104  # Perform my own actions
105  actual_action_duration = rospy.Duration(20)
106  actual_drive_duration = rospy.Duration(1)
107 
108  # create a task we will copy later,
109  master_task = create_master_task()
110 
111 
112  # now create a bunch of task with different times
113  task_count = 1
114  start_of_window = rospy.get_rostime()
115  # max time we will tell teh scheduler any action is expected to run for
116  max_action_duration = actual_drive_duration + actual_action_duration + actual_action_duration
117  # a total time window to fit all the tasks in
118  end_of_window = start_of_window + rospy.Duration(max_action_duration.secs * (task_count + 1))
119 
120  # how long we tell the scheduler we'll run for
121  # this one will be normal
122  # scheduled_duration = actual_action_duration
123  # this one will trigger task preemption
124  scheduled_duration = rospy.Duration(actual_action_duration.secs / 4)
125 
126 
127  test_nodes = publish_topological_map()
128 
129  tasks = []
130  # create individual tasks with differnt duration to happen within the window
131  for task_id in range(task_count):
132  # copy the task from the master
133  timed_task = deepcopy(master_task)
134  task_node = random.choice(test_nodes).name
135  timed_task.start_node_id=task_node
136  timed_task.end_node_id=task_node
137  timed_task.start_after = start_of_window
138  timed_task.end_before = end_of_window
139  # tell the scheduler we might take longer than we think
140  timed_task.max_duration = scheduled_duration
141  tasks.append(timed_task)
142 
143  # this provides windows that need execution delays
144  # start_of_window += max_action_duration
145 
146 
147  # get services to call into execution framework
148  add_tasks, set_execution_status = get_services()
149 
150 
151  # register task with the scheduler
152  task_ids = add_tasks(tasks)
153  print "Added %s" % task_ids
154 
155 
156  if False:
157  action_server = TestTaskAction(expected_action_duration=actual_action_duration, expected_drive_duration=actual_drive_duration)
158 
159 
160  # Set the task executor is running
161  set_execution_status(True)
162 
163 
164 
165  # rospy.sleep(actual_action_duration)
166 
167  # # # now create a second bunch of tasks which should not be possible
168  # task_ids = add_tasks(tasks)
169  # print "Added %s" % task_ids
170 
171  print 'spinning'
172  rospy.spin()


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