Project

General

Profile

Revision 3f72678f

ID3f72678f171cbcb5499524f6cbfe994f4a416364
Parent e7b4f56d
Child fa8e76a4

Added by Priya about 12 years ago

Changing scheduler and WH robot to ROS messages

View differences:

scout/libscout/src/behaviors/Scheduler.cpp
6 6
/** @Brief: Initialize data structures for the scheduler. */
7 7
Scheduler::Scheduler(std::string scoutname):Behavior(scoutname, "Scheduler")
8 8
{
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);
9

  
10
  ROS_INFO("Scheduler: Creating order PQ...");
12 11

  
13 12
  unassignedOrders = new PQWrapper(NUM_TASKS);
14 13

  
15
	robots.push_back(rob1);
16
	robots.push_back(rob2);
17
	robots.push_back(rob3);
18
	
14
  ROS_INFO("Scheduler: PQ orders initialized. ");
15

  
16
  sched_to_robot = n.advertise("sched_to_robot", 1000);
17

  
19 18
	create_orders();
19
  ROS_INFO("Scheduler: I am initialized!");
20 20
}
21 21

  
22 22
/** @Brief: Free allocatetd memory. */
23 23
Scheduler::~Scheduler()
24 24
{
25
   delete robots.at(0);
26
   delete robots.at(1);
27
   delete robots.at(2);
25
  while(robots.size() != 0)
26
    delete robots.at(0);
28 27
   
29
   delete unassignedOrders;
28
  delete unassignedOrders;
30 29
}
31 30

  
32 31
/** @Brief: Add robot to the waiting queue. 
......
36 35
 *  the robot of the waiting queue, and gives it a
37 36
 *  task.
38 37
 */
39
void Scheduler::get_task(WH_Robot* r)
38
void Scheduler::get_task(int robot)
40 39
{
41
	waitingRobots.push(r);
40
	waitingRobots.push(robots[robot]);
42 41
}
43 42

  
44 43
/** @Brief: Statically add orders to the Priority Queue Wrapper.*/
45 44
void Scheduler::create_orders()
46 45
{
46
  ROS_INFO("Scheduler: Creating orders...");
47

  
47 48
  Path p;
48 49
  p.len = 0;
49 50
  p.path = NULL;
......
55 56
  Order d(4,0,2,t,p,end);
56 57
  Order e(5,0,4,t,p,end);
57 58
	
59
  ROS_INFO("Scheduler: Orders created. Adding to list");
60

  
58 61
	unassignedOrders->insert(a);
59 62
	unassignedOrders->insert(b);
60 63
	unassignedOrders->insert(c);
61 64
	unassignedOrders->insert(d);
62 65
	unassignedOrders->insert(e);
66

  
67
  ROS_INFO("Scheduler: Orders initialized in list");
63 68
}
64 69

  
65 70
/** @Brief: This is a confirmation that the task is complete. 
66 71
    This function removes the order from assignedOrders. */
67 72
void Scheduler::task_complete(Order o)
68 73
{
69
    for (unsigned int i=0; i<assignedOrders.size(); i++)
70
	{
71
	    if (assignedOrders[i].getid()==o.getid())
72
		{
73
			assignedOrders.erase(assignedOrders.begin()+i);
74
		}
75
	}
74
  ROS_INFO("Scheduler: Task id %d was completed", o.getid());
75
  for (unsigned int i=0; i<assignedOrders.size(); i++)
76
  {
77
    if (assignedOrders[i].getid()==o.getid())
78
    {
79
      assignedOrders.erase(assignedOrders.begin()+i);
80
    }
81
  }
76 82
}
77 83

  
78 84
/** @Brief: This is a confirmation that the task failed. 
79 85
    This function places the order back on the PQWrapper. */
80 86
void Scheduler::task_failed(Order o)
81 87
{
88
  ROS_INFO("Scheduler: Task id %d was failed", o.getid());
82 89
	task_complete(o);
83 90
	unassignedOrders->insert(o);
84 91
}
85 92

  
86 93
/** @Brief: Do a waiting dance. */
87 94
void Scheduler::waiting_dance()
88
{
89
	
95
{ 
96
    ROS_INFO("Scheduler: TEEHEE i do a dance!");
90 97
}
91 98

  
92 99
/** @Brief: The scheduling algorithm. Picks a task and pops from the PQWrapper. */
......
111 118
  return *best;
112 119
}
113 120

  
121
void Scheduler::msg_callback(const std_msgs::String::ConstPtr& msg)
122
{
123
  if(msg->data.compare(0, 6, "FAILED") == 0)
124
  {
125
    int order_id = atoi(msg->data.substr(7).c_str());
126
    for(int i=0; i<assignedOrders.size(); i++)
127
    {
128
      if(assignedOrders[i].id == order_id)
129
      {
130
        task_failed(assignedOrders[i]);
131
        break;
132
      }
133
    }
134
  }
135
  else if(msg->data.compare(0, 7, "SUCCESS") == 0)
136
  {
137
    int order_id = atoi(msg->data.substr(8).c_str());
138
    for(int i=0; i<assignedOrders.size(); i++)
139
    {
140
      if(assignedOrders[i].id == order_id)
141
      {
142
        task_complete(assignedOrders[i]);
143
        break;
144
      }
145
    }
146
  }
147
  else if(msg->data.compare(0, 8, "GET_TASK") == 0)
148
  {
149
    int robot = atoi(msg->data.substr(9).c_str());
150
    get_task(robot);
151
  }
152
  else if(msg->data.compare(0, 8, "REGISTER") == 0)
153
  {
154
    int id = robots.size() +1;
155
    Robot new_robot;
156
    new_robot.name = msg->data.substr(9);
157
    new_robot.topic = n.subscribe(new_robot.name + "_topic", 1000, msg_callback);
158
    robots.push_back(new_robot);
159

  
160
    std_msgs::String msg;
161
    std::stringstream ss;
162
    ss<<"REG_SUCCESS";
163
    msg.data = ss.str();
164
    sched_to_robot.publish(msg);
165

  
166
  }
167
  else
168
  {
169
    ROS_INFO("I got a bad message: %s", msg->data);
170
  }
171

  
172
  return;
173
}
174

  
114 175
/** @Brief: Continuously checks for waiting robots. If no robots are waiting,
115 176
    this function calls the waiting_dance() function. */
116 177
void Scheduler::run()
117 178
{
179
  while(robots.size() < 3);
180

  
118 181
	while (ok())
119 182
	{
183
    ROS_INFO("Scheduler running");
120 184
		while(waitingRobots.empty() || unassignedOrders->arraySize()==0) 
121 185
      waiting_dance();
122 186

  
123
		(waitingRobots.front())->set_task(get_next_item());
187

  
188
    Order next = get_next_item();
189
    std_msgs::String msg;
190
    std::stringstream ss;
191
    ss<<"SET_TASK "<<next.id<<" "<<next.get_source()<<" "<<next.get_dest;
192
    msg.data = ss.str();
193
    sched_to_robot.publish(msg);
194

  
124 195
		waitingRobots.pop();
125 196
	}
126 197
}

Also available in: Unified diff