Revision ad5b5826
some fixes to whrobot and scheduler
scout/libscout/src/behaviors/WH_Robot.cpp | ||
---|---|---|
4 | 4 |
/** @Brief: warehouse robot constructor **/ |
5 | 5 |
WH_Robot::WH_Robot(std::string scoutname):Behavior(scoutname, "WH_Robot") |
6 | 6 |
{ |
7 |
nav_map = new navigationMap(scoutname); |
|
8 |
curr_task = DEFAULT_TASK; |
|
9 |
name = scoutname; |
|
10 |
reg_failed = 1; |
|
11 |
|
|
12 |
robot_to_sched = node.advertise<std_msgs::String>("robot_to_sched", 1000); |
|
13 |
sched_to_robot = node.subscribe(name + "_topic", 1000, &WH_Robot::robot_callback, this); |
|
14 |
ROS_INFO("A WH_Robot has been created"); |
|
7 |
nav_map = new navigationMap(scoutname); |
|
8 |
curr_task = DEFAULT_TASK; |
|
9 |
name = scoutname; |
|
10 |
reg_failed = 1; |
|
15 | 11 |
} |
16 | 12 |
|
17 | 13 |
WH_Robot::~WH_Robot() |
18 | 14 |
{ |
19 |
delete(nav_map);
|
|
15 |
delete(nav_map); |
|
20 | 16 |
} |
21 | 17 |
|
22 | 18 |
Duration WH_Robot::get_worst_case_time(State start_state, State target_state) |
... | ... | |
26 | 22 |
|
27 | 23 |
int WH_Robot::exec_task() |
28 | 24 |
{ |
29 |
ROS_INFO("WH_robot: Executing a task");
|
|
30 |
assert(curr_task != DEFAULT_TASK);
|
|
31 |
//TODO: do task
|
|
32 |
srand(0xDEADBEEF);
|
|
33 |
int error = rand() % 12;
|
|
34 |
if(error < 9) //Fail with 1/4 probability
|
|
35 |
{
|
|
36 |
ROS_INFO("WH_robot: TASK COMPLETE!");
|
|
37 |
return TASK_COMPLETED;
|
|
38 |
}
|
|
39 |
else
|
|
40 |
{
|
|
41 |
ROS_INFO("WH_robot: TASK FAILED!");
|
|
42 |
return TASK_FAILED;
|
|
43 |
}
|
|
25 |
ROS_INFO("WH_robot: Executing a task"); |
|
26 |
assert(curr_task != DEFAULT_TASK); |
|
27 |
//TODO: do task |
|
28 |
srand(0xDEADBEEF); |
|
29 |
int error = rand() % 12; |
|
30 |
if(error < 9) //Fail with 1/4 probability |
|
31 |
{ |
|
32 |
ROS_INFO("WH_robot: TASK COMPLETE!"); |
|
33 |
return TASK_COMPLETED; |
|
34 |
} |
|
35 |
else |
|
36 |
{ |
|
37 |
ROS_INFO("WH_robot: TASK FAILED!"); |
|
38 |
return TASK_FAILED; |
|
39 |
} |
|
44 | 40 |
} |
45 | 41 |
|
46 | 42 |
void WH_Robot::robot_callback(const std_msgs::String::ConstPtr& msg) |
47 | 43 |
{ |
44 |
std::cout << "robot callback called." << std::endl; |
|
48 | 45 |
if(msg->data.compare(0, 11, "REG_SUCCESS") == 0) |
49 | 46 |
{ |
50 | 47 |
id = atoi(msg->data.substr(12).c_str()); |
... | ... | |
52 | 49 |
} |
53 | 50 |
else if(msg->data.compare(0, 8, "SET_TASK") == 0) |
54 | 51 |
{ |
52 |
ROS_INFO("WH_ROBOT: Setting robot task"); |
|
55 | 53 |
char* string = (char*)msg->data.c_str(); |
56 | 54 |
char* data; |
57 | 55 |
data = strtok(string, " "); |
... | ... | |
76 | 74 |
|
77 | 75 |
void WH_Robot::run () |
78 | 76 |
{ |
79 |
ROS_INFO("I am a robot. Registering with scheduler..."); |
|
80 |
while(reg_failed) |
|
81 |
{ |
|
77 |
ROS_INFO("A WH_Robot has been created"); |
|
78 |
|
|
79 |
robot_to_sched = node.advertise<std_msgs::String>("robot_to_sched", 1000); |
|
80 |
sched_to_robot = node.subscribe(name + "_topic", 1000, &WH_Robot::robot_callback, this); |
|
81 |
|
|
82 |
ROS_INFO("I am a robot. Registering with scheduler..."); |
|
83 |
while(reg_failed && ok()) |
|
84 |
{ |
|
85 |
std_msgs::String msg; |
|
86 |
std::stringstream ss; |
|
87 |
ss << "REGISTER "<<name; |
|
88 |
msg.data = ss.str(); |
|
89 |
robot_to_sched.publish(msg); |
|
90 |
|
|
91 |
spinOnce(); |
|
92 |
} |
|
93 |
|
|
94 |
while(ok()) |
|
95 |
{ |
|
96 |
ROS_INFO("WH_ROBOT: Running main loop"); |
|
97 |
|
|
98 |
|
|
82 | 99 |
std_msgs::String msg; |
83 | 100 |
std::stringstream ss; |
84 |
ss << "REGISTER "<<name;
|
|
101 |
ss << "GET_TASK "<<id;
|
|
85 | 102 |
msg.data = ss.str(); |
86 | 103 |
robot_to_sched.publish(msg); |
87 | 104 |
|
105 |
while(curr_task == DEFAULT_TASK && ok()){ |
|
88 | 106 |
spinOnce(); |
89 | 107 |
} |
90 | 108 |
|
91 |
while(ok()) |
|
109 |
int error = exec_task(); |
|
110 |
if(error == TASK_COMPLETED) |
|
92 | 111 |
{ |
93 |
ROS_INFO("WH_ROBOT: Running main loop"); |
|
94 |
|
|
95 |
|
|
96 |
std_msgs::String msg; |
|
97 |
std::stringstream ss; |
|
98 |
ss << "GET_TASK "<<id; |
|
99 |
msg.data = ss.str(); |
|
100 |
robot_to_sched.publish(msg); |
|
101 |
|
|
102 |
spinOnce(); |
|
103 |
|
|
104 |
while(curr_task == DEFAULT_TASK){ |
|
105 |
continue; |
|
106 |
} |
|
107 |
|
|
108 |
int error = exec_task(); |
|
109 |
if(error == TASK_COMPLETED) |
|
110 |
{ |
|
111 |
|
|
112 |
|
|
113 |
ss << "SUCCESS "<<curr_task->getid(); |
|
114 |
msg.data = ss.str(); |
|
115 |
robot_to_sched.publish(msg); |
|
116 |
|
|
117 |
|
|
118 |
} |
|
119 |
else //error == TASK_FAILED |
|
120 |
{ |
|
121 |
|
|
122 |
|
|
123 |
ss << "FAILED "<<curr_task->getid(); |
|
124 |
msg.data = ss.str(); |
|
125 |
robot_to_sched.publish(msg); |
|
126 |
|
|
127 |
|
|
128 |
} |
|
129 |
delete curr_task; |
|
130 |
curr_task = DEFAULT_TASK; |
|
131 |
|
|
112 |
std_msgs::String msg; |
|
113 |
std::stringstream ss; |
|
114 |
ss << "SUCCESS "<<curr_task->getid(); |
|
115 |
msg.data = ss.str(); |
|
116 |
robot_to_sched.publish(msg); |
|
132 | 117 |
} |
118 |
else //error == TASK_FAILED |
|
119 |
{ |
|
120 |
std_msgs::String msg; |
|
121 |
std::stringstream ss; |
|
122 |
ss << "FAILED "<<curr_task->getid(); |
|
123 |
msg.data = ss.str(); |
|
124 |
robot_to_sched.publish(msg); |
|
125 |
} |
|
126 |
delete curr_task; |
|
127 |
curr_task = DEFAULT_TASK; |
|
128 |
|
|
129 |
} |
|
133 | 130 |
} |
134 | 131 |
|
135 | 132 |
void WH_Robot::set_task(Order order) |
136 | 133 |
{ |
137 |
curr_task = new Order(order.getid(), order.get_source(), order.get_dest(),
|
|
138 |
order.get_start_time(), order.get_path(), order.get_est_time());
|
|
139 |
return;
|
|
134 |
curr_task = new Order(order.getid(), order.get_source(), order.get_dest(), |
|
135 |
order.get_start_time(), order.get_path(), order.get_est_time()); |
|
136 |
return; |
|
140 | 137 |
} |
141 | 138 |
|
Also available in: Unified diff