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/BehaviorList.cpp
7 7
  behavior_list.push_back((Behavior*)new Odometry(scoutname));
8 8
  behavior_list.push_back((Behavior*)new navigationMap(scoutname));
9 9
  behavior_list.push_back((Behavior*)new sim_line(scoutname));
10
//  behavior_list.push_back((Behavior*)new Scheduler(scoutname));
10
  behavior_list.push_back((Behavior*)new Scheduler(scoutname));
11
  behavior_list.push_back((Behavior*)new WH_Robot(scoutname));
11 12
  return;
12 13
}
13 14

  
scout/libscout/src/BehaviorList.h
8 8
#include "behaviors/Odometry.h"
9 9
#include "behaviors/navigationMap.h"
10 10
#include "behaviors/Scheduler.h"
11
#include "behaviors/WH_Robot.h"
11 12

  
12 13
class BehaviorList
13 14
{
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
}
scout/libscout/src/behaviors/Scheduler.h
3 3

  
4 4
#include "../helper_classes/PQWrapper.h"
5 5
#include "../helper_classes/Order.h"
6
#include "WH_Robot.h"
7 6
#include "../Behavior.h"
8 7

  
9 8
#define NUM_TASKS 5
10 9

  
10
typedef struct{
11
  std::string name;
12
  ros::Subscriber topic;
13
} Robot;
14

  
11 15
class Scheduler : Behavior {
12
  std::vector<WH_Robot*> robots;
16
  std::vector<Robot> robots;
13 17
  PQWrapper* unassignedOrders;
14 18
	std::vector<Order> assignedOrders;
15
	std::queue<WH_Robot*> waitingRobots;
19
	std::queue<Robot> waitingRobots;
16 20

  
17 21
	void create_orders();
18 22

  
19 23
	void waiting_dance();
20 24

  
25
  void msg_callback(const std_msgs::String::ConstPtr& msg);
26

  
21 27
public:
22 28
  Scheduler(std::string scoutname);
23 29
	~Scheduler();
24 30
	
25
	void get_task(WH_Robot* robot);
31
	void get_task(int robot);
26 32
	
27 33
	void task_complete(Order o);
28 34
	void task_failed(Order o);
......
31 37
	Order get_next_item();
32 38
	
33 39
	void run();
40

  
41
  ros::Publisher sched_to_robot;
34 42
    
35 43
};
36 44
#endif
scout/libscout/src/behaviors/WH_Robot.cpp
1 1
#include "WH_Robot.h"
2
#include "Scheduler.h" 
3 2
#include "../helper_classes/Order.h"
4 3

  
5 4
/** @Brief: warehouse robot constructor **/
6
WH_Robot::WH_Robot(std::string scoutname, void* sched):Behavior(scoutname, "WH_Robot")
5
WH_Robot::WH_Robot(std::string scoutname):Behavior(scoutname, "WH_Robot")
7 6
{
7
    ROS_INFO("WH_ROBOT: Creating a WH_Robot...");
8 8
    nav_map = new navigationMap(scoutname);
9 9
    curr_task = DEFAULT_TASK;
10
    scheduler = sched;
10
    name = scoutname;
11
    reg_failed = 1;
12

  
13
    robot_to_sched = n.advertise<std_msgs::String>(name + "_topic", 1000);
14
    sched_to_robot = n.subscribe("sched_to_robot", 1000, msg_callback);
15

  
16
    while(reg_failed)
17
    {
18
      std_msgs::String msg;
19
      std::stringstream ss;
20
      ss << "REGISTER "<<name;
21
      msg.data = ss.str();
22
      robot_to_sched.publish(msg);
23
    }
24

  
25
    ROS_INFO("WH_Robot: I am a created WH robot");
11 26
}
12 27

  
13 28
WH_Robot::~WH_Robot()
......
22 37

  
23 38
int WH_Robot::exec_task()
24 39
{
40
    ROS_INFO("WH_robot: Executing a task");
25 41
    assert(curr_task != DEFAULT_TASK);
26 42
    //TODO: do task
27 43
    srand(0xDEADBEEF);
28 44
    int error = rand() % 12;
29 45
    if(error < 9) //Fail with 1/4 probability
30 46
    {
47
      ROS_INFO("WH_robot: TASK COMPLETE!");
31 48
      return TASK_COMPLETED;
32 49
    }
33 50
    else
34 51
    {
52
      ROS_INFO("WH_robot: TASK FAILED!");
35 53
      return TASK_FAILED;
36 54
    }
37 55
}
38 56

  
39
void WH_Robot::run (){
40
    ((Scheduler*)scheduler)->get_task(this);
41
    while(curr_task == DEFAULT_TASK)
42
      continue;
43
    int error = exec_task();
44
    if(error == TASK_COMPLETED)
45
    {
46
        ((Scheduler*)scheduler)->task_complete(*curr_task);
47
    }
48
    else //error == TASK_FAILED
57
void WH_Robot::msg_callback(const std_msgs::String::ConstPtr& msg)
58
{
59
  if(msg->data.compare(0, 11, "REG_SUCCESS") == 0)
60
  {
61
    reg_failed = 0;
62
  }
63
  else if(msg->data.compare(0, 8, "SET_TASK") == 0)
64
  {
65
    char* string = msg->data.c_str();
66
    char* data;
67
    data = strtok(string, " ");
68
    int id = atoi(data);
69
    data = strtok(string, " ");
70
    Address source = atoi(data);
71
    data = strtok(string, " ");
72
    Address dest = atoi(data);
73
    Time start_time = ros::Time::now();
74
    Path path = NULL;
75
    Duration est_time(0);
76

  
77
    curr_task = new Order(id, source, dest, start_time, path, est_time);
78
  }
79
  else
80
  {
81
    ROS_INFO("I got a bad message: %s", msg->data);
82
  }
83
}
84

  
85
void WH_Robot::run ()
86
{
87

  
88
    while(ok())
49 89
    {
50
        ((Scheduler*)scheduler)->task_failed(*curr_task);
90
        ROS_INFO("WH_ROBOT: Running main loop");
91

  
92

  
93
        std_msgs::String msg;
94
        std::stringstream ss;
95
        ss << "GET_TASK "<<id;
96
        msg.data = ss.str();
97
        robot_to_sched.publish(msg);
98

  
99

  
100
        while(curr_task == DEFAULT_TASK)
101
          continue;
102
        int error = exec_task();
103
        if(error == TASK_COMPLETED)
104
        {
105

  
106

  
107
        ss << "SUCCESS "<<curr_task->id;
108
        msg.data = ss.str();
109
        robot_to_sched.publish(msg);
110

  
111

  
112
        }
113
        else //error == TASK_FAILED
114
        {
115
 
116

  
117
        ss << "FAILED "<<curr_task->id;
118
        msg.data = ss.str();
119
        robot_to_sched.publish(msg);
120

  
121

  
122
       }
123
        delete curr_task;
124
        curr_task = DEFAULT_TASK;
51 125
    }
52
    curr_task = DEFAULT_TASK;
53 126
}
54 127

  
55 128
void WH_Robot::set_task(Order order)
scout/libscout/src/behaviors/WH_Robot.h
12 12
#include <stdlib.h>
13 13

  
14 14
class WH_Robot : Behavior{
15
        std::string name;
16
        int id;
17

  
18
        int reg_failed;
15 19

  
16 20
        Order* curr_task;
17 21
        navigationMap* nav_map;
18
        void* scheduler;
19 22

  
20 23
        Duration get_worst_case_time(State start_state, State target_state);
21 24
        int exec_task();
22 25

  
26
        void msg_callback(const std_msgs::String::ConstPtr& msg);
27

  
23 28
    public:
24
        WH_Robot(std::string scoutname, void* sched);
29
        WH_Robot(std::string scoutname);
25 30
        ~WH_Robot();
26 31
        void run();
27 32

  
28 33
        void set_task(Order order);
29 34

  
35
        ros::Publisher robot_to_sched;
36
        ros::Subscriber sched_to_robot;
37

  
30 38
};
31 39

  
32 40
#endif

Also available in: Unified diff