Revision 2f025967
ID | 2f0259679a0994dd9893ecace84dab0fa118eaeb |
Got scheduler to compile. onto wh robots
scout/libscout/src/behaviors/Scheduler.cpp | ||
---|---|---|
1 | 1 |
#include "Scheduler.h" |
2 |
#include "../helper_classes/Order.h" |
|
2 | 3 |
|
3 | 4 |
using namespace std; |
4 | 5 |
|
5 | 6 |
/** @Brief: Initialize data structures for the scheduler. */ |
6 |
Scheduler::Scheduler() |
|
7 |
Scheduler::Scheduler(std::string scoutname):Behavior(scoutname, "Scheduler")
|
|
7 | 8 |
{ |
8 |
WH_Robot rob1(/*params*/);
|
|
9 |
WH_Robot rob2(/*params*/);
|
|
10 |
WH_Robot rob3(/*params*/);
|
|
9 |
WH_Robot* rob1 = new WH_Robot("Robot1", this);
|
|
10 |
WH_Robot* rob2 = new WH_Robot("Robot2", this);
|
|
11 |
WH_Robot* rob3 = new WH_Robot("Robot3", this);
|
|
11 | 12 |
|
12 |
robots.insert(rob1); |
|
13 |
robots.insert(rob2); |
|
14 |
robots.insert(rob3); |
|
13 |
unassignedOrders = new PQWrapper(NUM_TASKS); |
|
14 |
|
|
15 |
robots.push_back(rob1); |
|
16 |
robots.push_back(rob2); |
|
17 |
robots.push_back(rob3); |
|
15 | 18 |
|
16 | 19 |
create_orders(); |
17 | 20 |
} |
... | ... | |
19 | 22 |
/** @Brief: Free allocatetd memory. */ |
20 | 23 |
Scheduler::~Scheduler() |
21 | 24 |
{ |
22 |
delete rob1;
|
|
23 |
delete rob2;
|
|
24 |
delete rob3;
|
|
25 |
delete robots.at(0);
|
|
26 |
delete robots.at(1);
|
|
27 |
delete robots.at(2);
|
|
25 | 28 |
|
26 |
delete robots; |
|
27 | 29 |
delete unassignedOrders; |
28 |
delete assigned Orders; |
|
29 |
delete waitingRobots; |
|
30 | 30 |
} |
31 | 31 |
|
32 | 32 |
/** @Brief: Add robot to the waiting queue. |
... | ... | |
36 | 36 |
* the robot of the waiting queue, and gives it a |
37 | 37 |
* task. |
38 | 38 |
*/ |
39 |
void Scheduler::get_task(WH_Robot r) |
|
39 |
void Scheduler::get_task(WH_Robot* r)
|
|
40 | 40 |
{ |
41 | 41 |
waitingRobots.push(r); |
42 | 42 |
} |
... | ... | |
44 | 44 |
/** @Brief: Statically add orders to the Priority Queue Wrapper.*/ |
45 | 45 |
void Scheduler::create_orders() |
46 | 46 |
{ |
47 |
Order a(1,0,0,101,0,0,0); |
|
48 |
Order b(2,0,0,11,0,0,0); |
|
49 |
Order c(3,0,0,91,0,0,0); |
|
50 |
Order d(4,0,0,21,0,0,0); |
|
51 |
Order e(5,0,0,41,0,0,0); |
|
47 |
Path p; |
|
48 |
p.len = 0; |
|
49 |
p.path = NULL; |
|
50 |
Time t = ros::Time::now(); |
|
51 |
Duration end(0); |
|
52 |
Order a(1,0,1,t,p,end); |
|
53 |
Order b(2,0,1,t,p,end); |
|
54 |
Order c(3,0,9,t,p,end); |
|
55 |
Order d(4,0,2,t,p,end); |
|
56 |
Order e(5,0,4,t,p,end); |
|
52 | 57 |
|
53 |
unassignedOrders.insert(a);
|
|
54 |
unassignedOrders.insert(b);
|
|
55 |
unassignedOrders.insert(c);
|
|
56 |
unassignedOrders.insert(d);
|
|
57 |
unassignedOrders.insert(e);
|
|
58 |
unassignedOrders->insert(a);
|
|
59 |
unassignedOrders->insert(b);
|
|
60 |
unassignedOrders->insert(c);
|
|
61 |
unassignedOrders->insert(d);
|
|
62 |
unassignedOrders->insert(e);
|
|
58 | 63 |
} |
59 | 64 |
|
60 | 65 |
/** @Brief: This is a confirmation that the task is complete. |
61 | 66 |
This function removes the order from assignedOrders. */ |
62 | 67 |
void Scheduler::task_complete(Order o) |
63 | 68 |
{ |
64 |
for (int i=0; i< assignedOrders.size(); i++)
|
|
69 |
for (int i=0; i<assignedOrders.size(); i++) |
|
65 | 70 |
{ |
66 | 71 |
if (assignedOrders[i].getid()==o.getid()) |
67 | 72 |
{ |
... | ... | |
75 | 80 |
void Scheduler::task_failed(Order o) |
76 | 81 |
{ |
77 | 82 |
task_complete(o); |
78 |
unassignedOrders.insert(o);
|
|
83 |
unassignedOrders->insert(o);
|
|
79 | 84 |
} |
80 | 85 |
|
81 | 86 |
/** @Brief: Do a waiting dance. */ |
... | ... | |
87 | 92 |
/** @Brief: The scheduling algorithm. Picks a task and pops from the PQWrapper. */ |
88 | 93 |
Order Scheduler::get_next_item() |
89 | 94 |
{ |
90 |
Time time = ros::TIME_MAX; |
|
95 |
Time t = ros::TIME_MAX; |
|
96 |
double time = t.toSec(); |
|
91 | 97 |
|
92 |
Order best; |
|
93 |
for(int i=0; i<unassigned_orders.arraySize(); i++)
|
|
98 |
Order* best;
|
|
99 |
for(int i=0; i<unassignedOrders->arraySize(); i++)
|
|
94 | 100 |
{ |
95 |
Order order = unassigned_orders.peek(i); |
|
101 |
Order order = unassignedOrders->peek(i); |
|
102 |
|
|
103 |
Time end_time = order.get_start_time() - order.get_est_time(); |
|
96 | 104 |
|
97 |
if(order.orderDeadline < time) //TODO: use another function
|
|
105 |
if(end_time.toSec() + MAX_WAIT_TIME < time) //TODO: use another function
|
|
98 | 106 |
{ |
99 |
best = order; |
|
100 |
time = order.orderDeadline;
|
|
107 |
best = ℴ
|
|
108 |
time = end_time.toSec() + MAX_WAIT_TIME;
|
|
101 | 109 |
} |
102 | 110 |
} |
103 |
|
|
111 |
return *best; |
|
104 | 112 |
} |
105 | 113 |
|
106 | 114 |
/** @Brief: Continuously checks for waiting robots. If no robots are waiting, |
... | ... | |
109 | 117 |
{ |
110 | 118 |
while (ok()) |
111 | 119 |
{ |
112 |
while(waitingRobots.empty() || unassignedOrders.arraySize()==0)
|
|
120 |
while(waitingRobots.empty() || unassignedOrders->arraySize()==0)
|
|
113 | 121 |
waiting_dance(); |
114 | 122 |
|
115 |
waitingRobots.front().set_task(get_next_item());
|
|
123 |
(waitingRobots.front())->set_task(get_next_item());
|
|
116 | 124 |
waitingRobots.pop(); |
117 | 125 |
} |
118 | 126 |
} |
Also available in: Unified diff