root / scout / libscout / src / behaviors / Scheduler.cpp @ b2876335
History | View | Annotate | Download (5.01 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 |
robot_to_sched = node.subscribe("robot_to_sched", 1000, &Scheduler::msg_callback, this); |
12 |
|
13 |
create_orders(); |
14 |
} |
15 |
|
16 |
/** @Brief: Free allocatetd memory. */
|
17 |
Scheduler::~Scheduler() |
18 |
{ |
19 |
delete unassignedOrders;
|
20 |
} |
21 |
|
22 |
/** @Brief: Add robot to the waiting queue.
|
23 |
* A robot calls this function with itself
|
24 |
* and gets pushed on a list of waiting robots.
|
25 |
* When a task is availaible the scheduler, removes
|
26 |
* the robot of the waiting queue, and gives it a
|
27 |
* task.
|
28 |
*/
|
29 |
void Scheduler::get_task(int robot) |
30 |
{ |
31 |
waitingRobots.push(robots[robot-1]);
|
32 |
} |
33 |
|
34 |
/** @Brief: Statically add orders to the Priority Queue Wrapper.*/
|
35 |
void Scheduler::create_orders()
|
36 |
{ |
37 |
ROS_INFO("Scheduler: Creating orders...");
|
38 |
|
39 |
Path p; |
40 |
p.len = 0;
|
41 |
p.path = NULL;
|
42 |
Time t = ros::Time::now(); |
43 |
Duration end(0);
|
44 |
Order a(1,0,1,t,p,end); |
45 |
Order b(2,0,1,t,p,end); |
46 |
Order c(3,0,9,t,p,end); |
47 |
Order d(4,0,2,t,p,end); |
48 |
Order e(5,0,4,t,p,end); |
49 |
|
50 |
ROS_INFO("Scheduler: Orders created. Adding to list");
|
51 |
|
52 |
unassignedOrders->insert(a); |
53 |
unassignedOrders->insert(b); |
54 |
unassignedOrders->insert(c); |
55 |
unassignedOrders->insert(d); |
56 |
unassignedOrders->insert(e); |
57 |
|
58 |
ROS_INFO("Scheduler: Orders initialized in list");
|
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 |
for(unsigned int i=0; i<unassignedOrders->arraySize(); i++) |
98 |
{ |
99 |
Order order = unassignedOrders->peek(i); |
100 |
|
101 |
Time end_time = order.get_start_time() - order.get_est_time(); |
102 |
|
103 |
if(end_time.toSec() + MAX_WAIT_TIME < time) //TODO: use another function |
104 |
{ |
105 |
best = ℴ |
106 |
time = end_time.toSec() + MAX_WAIT_TIME; |
107 |
} |
108 |
} |
109 |
return *best;
|
110 |
} |
111 |
|
112 |
void Scheduler::msg_callback(const std_msgs::String::ConstPtr& msg) |
113 |
{ |
114 |
ROS_INFO("I got a message: %s", msg->data.c_str());
|
115 |
if(msg->data.compare(0, 6, "FAILED") == 0) |
116 |
{ |
117 |
int order_id = atoi(msg->data.substr(7).c_str()); |
118 |
for(unsigned int i=0; i<assignedOrders.size(); i++) |
119 |
{ |
120 |
if(assignedOrders[i].getid() == order_id)
|
121 |
{ |
122 |
task_failed(assignedOrders[i]); |
123 |
break;
|
124 |
} |
125 |
} |
126 |
} |
127 |
else if(msg->data.compare(0, 7, "SUCCESS") == 0) |
128 |
{ |
129 |
int order_id = atoi(msg->data.substr(8).c_str()); |
130 |
for(unsigned int i=0; i<assignedOrders.size(); i++) |
131 |
{ |
132 |
if(assignedOrders[i].getid() == order_id)
|
133 |
{ |
134 |
task_complete(assignedOrders[i]); |
135 |
break;
|
136 |
} |
137 |
} |
138 |
} |
139 |
else if(msg->data.compare(0, 8, "GET_TASK") == 0) |
140 |
{ |
141 |
int robot = atoi(msg->data.substr(9).c_str()); |
142 |
get_task(robot); |
143 |
} |
144 |
else if(msg->data.compare(0, 8, "REGISTER") == 0) |
145 |
{ |
146 |
string robot_name = msg->data.substr(9); |
147 |
for(unsigned int i=0; i<robots.size(); i++) |
148 |
{ |
149 |
if(robots[i].name.compare(robot_name) == 0){ |
150 |
return;
|
151 |
} |
152 |
} |
153 |
|
154 |
int id = robots.size() +1; |
155 |
Robot new_robot; |
156 |
new_robot.name = robot_name; |
157 |
new_robot.topic = node.advertise<std_msgs::String>(new_robot.name + "_topic", 1000); |
158 |
robots.push_back(new_robot); |
159 |
|
160 |
ROS_INFO("Registration a robot %s", new_robot.name.c_str());
|
161 |
std_msgs::String msg; |
162 |
std::stringstream ss; |
163 |
ss<<"REG_SUCCESS "<<id;
|
164 |
msg.data = ss.str(); |
165 |
new_robot.topic.publish(msg); |
166 |
ROS_INFO("Registration a success");
|
167 |
} |
168 |
else
|
169 |
{ |
170 |
ROS_INFO("I got a bad message: %s", msg->data.c_str());
|
171 |
} |
172 |
|
173 |
return;
|
174 |
} |
175 |
|
176 |
/** @Brief: Continuously checks for waiting robots. If no robots are waiting,
|
177 |
this function calls the waiting_dance() function. */
|
178 |
void Scheduler::run()
|
179 |
{ |
180 |
ROS_INFO("I am a scheduler!. Now waiting for robots");
|
181 |
while(robots.size() < 3) |
182 |
spinOnce(); |
183 |
|
184 |
while (ok())
|
185 |
{ |
186 |
ROS_INFO("Scheduler running");
|
187 |
spinOnce(); |
188 |
while(waitingRobots.empty() || unassignedOrders->arraySize()==0) |
189 |
waiting_dance(); |
190 |
|
191 |
|
192 |
Order next = get_next_item(); |
193 |
Robot r = waitingRobots.front(); |
194 |
|
195 |
std_msgs::String msg; |
196 |
std::stringstream ss; |
197 |
ss<<"SET_TASK "<<next.getid()<<" "<<next.get_source()<<" "<<next.get_dest(); |
198 |
msg.data = ss.str(); |
199 |
r.topic.publish(msg); |
200 |
|
201 |
waitingRobots.pop(); |
202 |
} |
203 |
} |
204 |
|
205 |
|
206 |
|
207 |
|