root / scout / libscout / src / behaviors / Scheduler.cpp @ 1905324e
History | View | Annotate | Download (5.54 KB)
1 |
#include "Scheduler.h" |
---|---|
2 |
#include "../helper_classes/Order.h" |
3 |
|
4 |
using namespace std; |
5 |
|
6 |
/** @Brief: Initialize data structures for the scheduler. */
|
7 |
Scheduler::Scheduler(std::string scoutname):Behavior(scoutname, "Scheduler") |
8 |
{ |
9 |
unassignedOrders = new PQWrapper(NUM_TASKS);
|
10 |
|
11 |
create_orders(); |
12 |
} |
13 |
|
14 |
/** @Brief: Free allocatetd memory. */
|
15 |
Scheduler::~Scheduler() |
16 |
{ |
17 |
delete unassignedOrders;
|
18 |
} |
19 |
|
20 |
/** @Brief: Add robot to the waiting queue.
|
21 |
* A robot calls this function with itself
|
22 |
* and gets pushed on a list of waiting robots.
|
23 |
* When a task is availaible the scheduler, removes
|
24 |
* the robot of the waiting queue, and gives it a
|
25 |
* task.
|
26 |
*/
|
27 |
void Scheduler::get_task(int robot) |
28 |
{ |
29 |
Robot my_robot = robots[robot-1];
|
30 |
if(my_robot.sched_status != WAITING_ROBOT)
|
31 |
{ |
32 |
waitingRobots.push(my_robot); |
33 |
my_robot.sched_status = WAITING_ROBOT; |
34 |
} |
35 |
} |
36 |
|
37 |
/** @Brief: Statically add orders to the Priority Queue Wrapper.*/
|
38 |
void Scheduler::create_orders()
|
39 |
{ |
40 |
Path p; |
41 |
p.len = 0;
|
42 |
p.path = NULL;
|
43 |
Time t = ros::Time::now(); |
44 |
Duration end(0);
|
45 |
Duration five(5);
|
46 |
Duration three(3);
|
47 |
Duration ten(10);
|
48 |
Order a(1,0,2,t+ten,p,end); |
49 |
Order b(2,0,4,t+five,p,end); |
50 |
Order c(3,0,5,t+three,p,end); |
51 |
Order d(4,0,6,t,p,end); |
52 |
Order e(5,0,7,t,p,end); |
53 |
|
54 |
unassignedOrders->insert(a); |
55 |
unassignedOrders->insert(b); |
56 |
unassignedOrders->insert(c); |
57 |
unassignedOrders->insert(d); |
58 |
unassignedOrders->insert(e); |
59 |
} |
60 |
|
61 |
/** @Brief: This is a confirmation that the task is complete.
|
62 |
This function removes the order from assignedOrders. */
|
63 |
void Scheduler::task_complete(Order o)
|
64 |
{ |
65 |
ROS_INFO("Scheduler: Task id %d was completed", o.getid());
|
66 |
for (unsigned int i=0; i<assignedOrders.size(); i++) |
67 |
{ |
68 |
if (assignedOrders[i].getid()==o.getid())
|
69 |
{ |
70 |
assignedOrders.erase(assignedOrders.begin()+i); |
71 |
} |
72 |
} |
73 |
} |
74 |
|
75 |
/** @Brief: This is a confirmation that the task failed.
|
76 |
This function places the order back on the PQWrapper. */
|
77 |
void Scheduler::task_failed(Order o)
|
78 |
{ |
79 |
ROS_INFO("Scheduler: Task id %d was failed", o.getid());
|
80 |
task_complete(o); |
81 |
unassignedOrders->insert(o); |
82 |
} |
83 |
|
84 |
/** @Brief: Do a waiting dance. */
|
85 |
void Scheduler::waiting_dance()
|
86 |
{ |
87 |
//ROS_INFO("Scheduler: TEEHEE i do a dance!");
|
88 |
} |
89 |
|
90 |
/** @Brief: The scheduling algorithm. Picks a task and pops from the PQWrapper. */
|
91 |
Order Scheduler::get_next_item() |
92 |
{ |
93 |
Time t = ros::TIME_MAX; |
94 |
double time = t.toSec();
|
95 |
|
96 |
Order* best; |
97 |
int best_index;
|
98 |
for(unsigned int i=0; i<unassignedOrders->arraySize(); i++) |
99 |
{ |
100 |
Order order = unassignedOrders->peek(i); |
101 |
|
102 |
Time end_time = order.get_start_time() - order.get_est_time(); |
103 |
|
104 |
if(end_time.toSec() + MAX_WAIT_TIME < time) //TODO: use another function |
105 |
{ |
106 |
best = ℴ |
107 |
best_index = i; |
108 |
time = end_time.toSec() + MAX_WAIT_TIME; |
109 |
} |
110 |
} |
111 |
|
112 |
Order ret; |
113 |
ret = unassignedOrders->remove(best_index); |
114 |
assignedOrders.push_back(ret); |
115 |
return ret;
|
116 |
} |
117 |
|
118 |
void Scheduler::msg_callback(const std_msgs::String::ConstPtr& msg) |
119 |
{ |
120 |
if(msg->data.compare(0, 6, "FAILED") == 0) |
121 |
{ |
122 |
int order_id = atoi(msg->data.substr(7).c_str()); |
123 |
for(unsigned int i=0; i<assignedOrders.size(); i++) |
124 |
{ |
125 |
if(assignedOrders[i].getid() == order_id)
|
126 |
{ |
127 |
task_failed(assignedOrders[i]); |
128 |
break;
|
129 |
} |
130 |
} |
131 |
} |
132 |
else if(msg->data.compare(0, 7, "SUCCESS") == 0) |
133 |
{ |
134 |
int order_id = atoi(msg->data.substr(8).c_str()); |
135 |
for(unsigned int i=0; i<assignedOrders.size(); i++) |
136 |
{ |
137 |
if(assignedOrders[i].getid() == order_id)
|
138 |
{ |
139 |
task_complete(assignedOrders[i]); |
140 |
break;
|
141 |
} |
142 |
} |
143 |
} |
144 |
else if(msg->data.compare(0, 8, "GET_TASK") == 0) |
145 |
{ |
146 |
ROS_INFO("SCHEDULER: got get task message");
|
147 |
int robot = atoi(msg->data.substr(9).c_str()); |
148 |
get_task(robot); |
149 |
} |
150 |
else if(msg->data.compare(0, 8, "REGISTER") == 0) |
151 |
{ |
152 |
string robot_name = msg->data.substr(9); |
153 |
for(unsigned int i=0; i<robots.size(); i++) |
154 |
{ |
155 |
if(robots[i].name.compare(robot_name) == 0) |
156 |
{ |
157 |
return;
|
158 |
} |
159 |
} |
160 |
|
161 |
int id = robots.size() +1; |
162 |
Robot new_robot; |
163 |
new_robot.name = robot_name; |
164 |
new_robot.topic = node.advertise<std_msgs::String>(new_robot.name + "_topic", 1000); |
165 |
new_robot.sched_status = NEW_ROBOT; |
166 |
robots.push_back(new_robot); |
167 |
|
168 |
std_msgs::String msg; |
169 |
std::stringstream ss; |
170 |
ss<<"REG_SUCCESS "<<id;
|
171 |
msg.data = ss.str(); |
172 |
new_robot.topic.publish(msg); |
173 |
spinOnce(); |
174 |
|
175 |
ROS_INFO("Registration a success");
|
176 |
|
177 |
assert(robots.size() > 0);
|
178 |
} |
179 |
else
|
180 |
{ |
181 |
ROS_INFO("I got a bad message: %s", msg->data.c_str());
|
182 |
} |
183 |
|
184 |
return;
|
185 |
} |
186 |
|
187 |
/** @Brief: Continuously checks for waiting robots. If no robots are waiting,
|
188 |
this function calls the waiting_dance() function. */
|
189 |
void Scheduler::run()
|
190 |
{ |
191 |
robot_to_sched = node.subscribe("robot_to_sched", 1000, &Scheduler::msg_callback, this); |
192 |
ROS_INFO("I am a scheduler!. Now waiting for robots");
|
193 |
while(robots.size() < 1 && ok()) |
194 |
{ |
195 |
spinOnce(); |
196 |
} |
197 |
ROS_INFO("SCHEDULER: main loop");
|
198 |
while (ok())
|
199 |
{ |
200 |
ROS_INFO("Scheduler running");
|
201 |
spinOnce(); |
202 |
while((waitingRobots.empty() || unassignedOrders->arraySize()==0) && ok()) |
203 |
{ |
204 |
waiting_dance(); |
205 |
spinOnce(); |
206 |
} |
207 |
|
208 |
ROS_INFO("SCHEDULER: Setting task");
|
209 |
Order next = get_next_item(); |
210 |
Robot r = waitingRobots.front(); |
211 |
|
212 |
ROS_INFO("SCHEDULER: Setting task %d to robot %s", next.getid(), r.name.c_str());
|
213 |
std_msgs::String msg; |
214 |
std::stringstream ss; |
215 |
ss<<"SET_TASK "<<next.getid()<<" "<<next.get_source()<<" "<<next.get_dest(); |
216 |
std::cout<<"msg: "<<ss.str()<<endl;
|
217 |
msg.data = ss.str(); |
218 |
r.topic.publish(msg); |
219 |
|
220 |
spinOnce(); |
221 |
|
222 |
waitingRobots.front().sched_status = ORDERED_ROBOT; |
223 |
waitingRobots.pop(); |
224 |
} |
225 |
} |
226 |
|
227 |
|
228 |
|
229 |
|