scoutos / scout / libscout / src / behaviors / WH_Robot.cpp @ ad5b5826
History | View | Annotate | Download (3.28 KB)
1 |
#include "WH_Robot.h" |
---|---|
2 |
#include "../helper_classes/Order.h" |
3 |
|
4 |
/** @Brief: warehouse robot constructor **/
|
5 |
WH_Robot::WH_Robot(std::string scoutname):Behavior(scoutname, "WH_Robot") |
6 |
{ |
7 |
nav_map = new navigationMap(scoutname);
|
8 |
curr_task = DEFAULT_TASK; |
9 |
name = scoutname; |
10 |
reg_failed = 1;
|
11 |
} |
12 |
|
13 |
WH_Robot::~WH_Robot() |
14 |
{ |
15 |
delete(nav_map);
|
16 |
} |
17 |
|
18 |
Duration WH_Robot::get_worst_case_time(State start_state, State target_state) |
19 |
{ |
20 |
return nav_map->get_worst_case_time(start_state, target_state);
|
21 |
} |
22 |
|
23 |
int WH_Robot::exec_task()
|
24 |
{ |
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 |
} |
40 |
} |
41 |
|
42 |
void WH_Robot::robot_callback(const std_msgs::String::ConstPtr& msg) |
43 |
{ |
44 |
std::cout << "robot callback called." << std::endl;
|
45 |
if(msg->data.compare(0, 11, "REG_SUCCESS") == 0) |
46 |
{ |
47 |
id = atoi(msg->data.substr(12).c_str());
|
48 |
reg_failed = 0;
|
49 |
} |
50 |
else if(msg->data.compare(0, 8, "SET_TASK") == 0) |
51 |
{ |
52 |
ROS_INFO("WH_ROBOT: Setting robot task");
|
53 |
char* string = (char*)msg->data.c_str(); |
54 |
char* data;
|
55 |
data = strtok(string, " "); |
56 |
int order_id = atoi(data);
|
57 |
data = strtok(string, " "); |
58 |
Address source = atoi(data); |
59 |
data = strtok(string, " "); |
60 |
Address dest = atoi(data); |
61 |
Time start_time = ros::Time::now(); |
62 |
Path path; |
63 |
path.len = 0;
|
64 |
path.path = NULL;
|
65 |
Duration est_time(0);
|
66 |
|
67 |
curr_task = new Order(order_id, source, dest, start_time, path, est_time);
|
68 |
} |
69 |
else
|
70 |
{ |
71 |
ROS_INFO("I got a bad message: %s", msg->data.c_str());
|
72 |
} |
73 |
} |
74 |
|
75 |
void WH_Robot::run ()
|
76 |
{ |
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 |
|
99 |
std_msgs::String msg; |
100 |
std::stringstream ss; |
101 |
ss << "GET_TASK "<<id;
|
102 |
msg.data = ss.str(); |
103 |
robot_to_sched.publish(msg); |
104 |
|
105 |
while(curr_task == DEFAULT_TASK && ok()){
|
106 |
spinOnce(); |
107 |
} |
108 |
|
109 |
int error = exec_task();
|
110 |
if(error == TASK_COMPLETED)
|
111 |
{ |
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); |
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 |
} |
130 |
} |
131 |
|
132 |
void WH_Robot::set_task(Order order)
|
133 |
{ |
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;
|
137 |
} |
138 |
|