Project

General

Profile

Statistics
| Branch: | Revision:

scoutos / scout / libscout / src / behaviors / Scheduler.cpp @ ad5b5826

History | View | Annotate | Download (5.52 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
  ROS_INFO("SCHEDULER: robots vector size is: %d", robots.size());
30
  Robot my_robot = robots[robot-1];
31
  ROS_INFO("SCHEDULER: My robot is %s", my_robot.name.c_str());
32
  if(my_robot.sched_status != WAITING_ROBOT)
33
  {
34
          waitingRobots.push(my_robot);
35
    my_robot.sched_status = WAITING_ROBOT;
36
    ROS_INFO("SCHEDULER: Added to scheduler %d", my_robot.sched_status);
37
  }
38
}
39

    
40
/** @Brief: Statically add orders to the Priority Queue Wrapper.*/
41
void Scheduler::create_orders()
42
{
43
  Path p;
44
  p.len = 0;
45
  p.path = NULL;
46
  Time t = ros::Time::now();
47
  Duration end(0);
48
        Order a(1,0,1,t,p,end);
49
  Order b(2,0,1,t,p,end);
50
  Order c(3,0,9,t,p,end);
51
  Order d(4,0,2,t,p,end);
52
  Order e(5,0,4,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
  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 = &order;
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
  if(msg->data.compare(0, 6, "FAILED") == 0)
115
  {
116
    int order_id = atoi(msg->data.substr(7).c_str());
117
    for(unsigned int i=0; i<assignedOrders.size(); i++)
118
    {
119
      if(assignedOrders[i].getid() == order_id)
120
      {
121
        task_failed(assignedOrders[i]);
122
        break;
123
      }
124
    }
125
  }
126
  else if(msg->data.compare(0, 7, "SUCCESS") == 0)
127
  {
128
    int order_id = atoi(msg->data.substr(8).c_str());
129
    for(unsigned int i=0; i<assignedOrders.size(); i++)
130
    {
131
      if(assignedOrders[i].getid() == order_id)
132
      {
133
        task_complete(assignedOrders[i]);
134
        break;
135
      }
136
    }
137
  }
138
  else if(msg->data.compare(0, 8, "GET_TASK") == 0)
139
  {
140
    int robot = atoi(msg->data.substr(9).c_str());
141
    get_task(robot);
142
  }
143
  else if(msg->data.compare(0, 8, "REGISTER") == 0)
144
  {
145
    string robot_name = msg->data.substr(9);
146
    for(unsigned int i=0; i<robots.size(); i++)
147
    {
148
      if(robots[i].name.compare(robot_name) == 0)
149
      { 
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
    new_robot.sched_status = NEW_ROBOT;
159
    robots.push_back(new_robot);
160

    
161
    ROS_INFO("Registration a robot %s %d", new_robot.name.c_str(), robots.size());
162
    std_msgs::String msg;
163
    std::stringstream ss;
164
    ss<<"REG_SUCCESS "<<id;
165
    msg.data = ss.str();
166
    new_robot.topic.publish(msg);
167
    ros::spinOnce();
168

    
169
    ROS_INFO("Registration a success");
170

    
171
    assert(robots.size() > 0);
172
  }
173
  else
174
  {
175
    ROS_INFO("I got a bad message: %s", msg->data.c_str());
176
  }
177

    
178
  return;
179
}
180

    
181
/** @Brief: Continuously checks for waiting robots. If no robots are waiting,
182
    this function calls the waiting_dance() function. */
183
void Scheduler::run()
184
{
185
  robot_to_sched = node.subscribe("robot_to_sched", 1000, &Scheduler::msg_callback, this);
186
  ROS_INFO("I am a scheduler!. Now waiting for robots");
187
  while(robots.size() < 1 && ok())
188
  {
189
    ROS_INFO("Robots size: %d", robots.size());
190
    spinOnce();
191
  }
192
  ROS_INFO("SCHEDULER: main loop");
193
        while (ok())
194
        {
195
    ROS_INFO("Scheduler running");
196
    spinOnce();
197
                while(waitingRobots.empty() || unassignedOrders->arraySize()==0) 
198
      waiting_dance();
199
  
200
    ROS_INFO("SCHEDULER: Setting task");
201
    Order next = get_next_item();
202
    Robot r = waitingRobots.front();
203

    
204
    ROS_INFO("SCHEDULER: Setting task %d to robot %s", next.getid(), r.name.c_str());
205
    std_msgs::String msg;
206
    std::stringstream ss;
207
    ss<<"SET_TASK "<<next.getid()<<" "<<next.get_source()<<" "<<next.get_dest();
208
    msg.data = ss.str();
209
    r.topic.publish(msg);
210

    
211
    waitingRobots.front().sched_status = ORDERED_ROBOT;
212
                waitingRobots.pop();
213
        }
214
}
215

    
216

    
217

    
218