Go to the documentation of this file.00001
00002 #include "bwi_kr_execution/ExecutePlanAction.h"
00003
00004 #include <actionlib/client/simple_action_client.h>
00005 #include <bwi_msgs/LogicalNavigationAction.h>
00006 #include <bwi_msgs/DoorHandlerInterface.h>
00007 #include <bwi_kr_execution/CurrentStateQuery.h>
00008 #include <bwi_kr_execution/UpdateFluents.h>
00009
00010 #include <std_srvs/Empty.h>
00011 #include <ros/ros.h>
00012
00013 #include <sstream>
00014 #include <stdlib.h>
00015 #include <time.h>
00016
00017 typedef actionlib::SimpleActionClient<bwi_kr_execution::ExecutePlanAction> Client;
00018
00019 using namespace std;
00020
00021 const size_t ep_per_phase = 15;
00022 const double max_time_per_episode = 300.0;
00023
00024
00025 struct Task {
00026
00027 Task(const bwi_kr_execution::ExecutePlanGoal& initial_state, const bwi_kr_execution::ExecutePlanGoal& goal) :
00028 initial_state(initial_state), goal(goal) {}
00029
00030 bwi_kr_execution::ExecutePlanGoal initial_state;
00031 bwi_kr_execution::ExecutePlanGoal goal;
00032 };
00033
00034 struct NotPositionFluent {
00035
00036 bool operator()(const bwi_kr_execution::AspFluent& fluent) const {
00037
00038 return !(fluent.name == "at" ||
00039 fluent.name == "-at" ||
00040 fluent.name == "beside" ||
00041 fluent.name == "-beside" ||
00042 fluent.name == "facing" ||
00043 fluent.name == "-facing");
00044 }
00045 };
00046
00047 bool goToInitialState(Client &client,const bwi_kr_execution::ExecutePlanGoal& initial_state) {
00048
00049 ROS_INFO("Going to the initial state");
00050
00051 client.sendGoalAndWait(initial_state);
00052
00053 if (client.getState() != actionlib::SimpleClientGoalState::SUCCEEDED) {
00054 ROS_INFO_STREAM("I can't reach the initial position, giving up... ");
00055 return false;
00056 }
00057
00058 ROS_INFO("Inital state achieved");
00059
00060 return true;
00061 }
00062
00063 bwi_kr_execution::ExecutePlanGoal findPerson(const std::string& personName) {
00064
00065 bwi_kr_execution::ExecutePlanGoal goal;
00066
00067 bwi_kr_execution::AspRule main_rule;
00068 bwi_kr_execution::AspFluent fluent;
00069 fluent.name = "not ingdc";
00070 fluent.variables.push_back(personName);
00071
00072 main_rule.body.push_back(fluent);
00073
00074
00075 bwi_kr_execution::AspFluent fluent_not;
00076 fluent_not.name = "not -ingdc";
00077 fluent_not.variables.push_back(personName);
00078
00079 main_rule.body.push_back(fluent_not);
00080
00081 bwi_kr_execution::AspRule flag_rule;
00082 bwi_kr_execution::AspFluent find_person_flag;
00083 find_person_flag.name = "findPersonTask";
00084 flag_rule.head.push_back(find_person_flag);
00085
00086 goal.aspGoal.push_back(main_rule);
00087 goal.aspGoal.push_back(flag_rule);
00088
00089 return goal;
00090 }
00091
00092
00093 bwi_kr_execution::ExecutePlanGoal initialStateFormula() {
00094
00095 bwi_kr_execution::ExecutePlanGoal go_to_initial_state;
00096
00097 bwi_kr_execution::AspRule initial_rule1;
00098 bwi_kr_execution::AspFluent front_of_lab_door;
00099 front_of_lab_door.name = "not facing";
00100 front_of_lab_door.variables.push_back("d3_414b1");
00101 initial_rule1.body.push_back(front_of_lab_door);
00102
00103 bwi_kr_execution::AspRule initial_rule2;
00104 bwi_kr_execution::AspFluent outside_of_lab;
00105 outside_of_lab.name = "not at";
00106 outside_of_lab.variables.push_back("l3_414b");
00107 initial_rule2.body.push_back(outside_of_lab);
00108
00109 go_to_initial_state.aspGoal.push_back(initial_rule1);
00110 go_to_initial_state.aspGoal.push_back(initial_rule2);
00111
00112 return go_to_initial_state;
00113 }
00114
00115 bwi_kr_execution::ExecutePlanGoal goToPlace(const std::string& place) {
00116
00117 bwi_kr_execution::ExecutePlanGoal goal;
00118
00119 bwi_kr_execution::AspRule rule;
00120 bwi_kr_execution::AspFluent fluent;
00121 fluent.name = "not at";
00122 fluent.variables.push_back(place);
00123
00124 rule.body.push_back(fluent);
00125
00126 goal.aspGoal.push_back(rule);
00127
00128 return goal;
00129 }
00130
00131 bwi_kr_execution::ExecutePlanGoal goToPlaceSpecific(const std::string& place, const std::string& where) {
00132
00133 bwi_kr_execution::ExecutePlanGoal goal;
00134
00135
00136 bwi_kr_execution::AspRule rule1;
00137 bwi_kr_execution::AspFluent fluent1;
00138 fluent1.name = "not at";
00139 fluent1.variables.push_back(place);
00140
00141 rule1.body.push_back(fluent1);
00142 goal.aspGoal.push_back(rule1);
00143
00144
00145 bwi_kr_execution::AspRule rule2;
00146 bwi_kr_execution::AspFluent fluent2;
00147 fluent2.name = "not facing";
00148 fluent2.variables.push_back(where);
00149
00150 rule2.body.push_back(fluent2);
00151 goal.aspGoal.push_back(rule2);
00152
00153 return goal;
00154 }
00155
00156 void phaseTransition(int episode) {
00157
00158 srand(time(NULL));
00159
00160 ROS_INFO_STREAM("episode: " << episode);
00161
00162 if(episode % ep_per_phase == 0) {
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 ros::NodeHandle n;
00175 ros::ServiceClient update_doors = n.serviceClient<bwi_msgs::DoorHandlerInterface>("update_doors");
00176
00177 bwi_msgs::DoorHandlerInterface open_all_doors;
00178 open_all_doors.request.all_doors = true;
00179 open_all_doors.request.open = true;
00180
00181 update_doors.call(open_all_doors);
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 bwi_msgs::DoorHandlerInterface close_414b3;
00200 close_414b3.request.door = "d3_414b3";
00201
00202 bwi_msgs::DoorHandlerInterface close_414a3;
00203 close_414a3.request.door = "d3_414a3";
00204
00205 bwi_msgs::DoorHandlerInterface close_414b2;
00206 close_414b2.request.door = "d3_414b2";
00207
00208 bwi_msgs::DoorHandlerInterface close_414a2;
00209 close_414a2.request.door = "d3_414a2";
00210
00211 bwi_msgs::DoorHandlerInterface close_414b1;
00212 close_414b1.request.door = "d3_414b1";
00213
00214 bwi_msgs::DoorHandlerInterface close_414a1;
00215 close_414a1.request.door = "d3_414a1";
00216
00217 int howmanylab = rand() % 4 + 1;
00218
00219 if (howmanylab==1) {
00220 int whichlab = rand() % 4;
00221 if (whichlab==0) {
00222 update_doors.call(close_414a2);
00223 update_doors.call(close_414a1);
00224 update_doors.call(close_414b1);
00225 }
00226 else if (whichlab==1) {
00227 update_doors.call(close_414b2);
00228 update_doors.call(close_414a1);
00229 update_doors.call(close_414b1);
00230 }
00231 else if (whichlab==2) {
00232 update_doors.call(close_414b2);
00233 update_doors.call(close_414a2);
00234 update_doors.call(close_414b1);
00235 }
00236 else {
00237 update_doors.call(close_414b2);
00238 update_doors.call(close_414a1);
00239 update_doors.call(close_414a2);
00240 }
00241 }
00242 else if (howmanylab==2) {
00243 int sameside = rand() %2;
00244 if (sameside) {
00245 int whichside = rand() % 2;
00246 if (whichside==0) {
00247 update_doors.call(close_414a1);
00248 update_doors.call(close_414a2);
00249 }
00250 else {
00251 update_doors.call(close_414b2);
00252 update_doors.call(close_414b1);
00253 }
00254 }
00255 else {
00256 int whichhere = rand() %2;
00257 if (whichhere == 0)
00258 update_doors.call(close_414a1);
00259 else
00260 update_doors.call(close_414a2);
00261 int whichthere = rand() %2;
00262 if (whichthere == 0)
00263 update_doors.call(close_414b1);
00264 else
00265 update_doors.call(close_414b2);
00266 int center = rand() % 2;
00267 if (center) {
00268 update_doors.call(close_414a3);
00269 update_doors.call(close_414b3);
00270 }
00271 }
00272 }
00273 else if (howmanylab==3) {
00274 int center = rand() % 2;
00275 if (center) {
00276 update_doors.call(close_414a3);
00277 update_doors.call(close_414b3);
00278 }
00279 int whichclose = rand() % 4;
00280 if (whichclose==0)
00281 update_doors.call(close_414b1);
00282 else if (whichclose==1)
00283 update_doors.call(close_414b2);
00284 else if (whichclose==2)
00285 update_doors.call(close_414a1);
00286 else
00287 update_doors.call(close_414a2);
00288 }
00289 else {
00290 int center = rand() % 2;
00291 if (center) {
00292 update_doors.call(close_414a3);
00293 update_doors.call(close_414b3);
00294 }
00295 }
00296
00297 }
00298
00299 if(episode % 3 == 1) {
00300
00301 ros::NodeHandle n;
00302 ros::ServiceClient update_doors = n.serviceClient<bwi_msgs::DoorHandlerInterface>("update_doors");
00303
00304
00305 bwi_msgs::DoorHandlerInterface open_516a;
00306 open_516a.request.door = "d3_516a";
00307 open_516a.request.open = true;
00308 update_doors.call(open_516a);
00309
00310 bwi_msgs::DoorHandlerInterface open_516b;
00311 open_516b.request.door = "d3_516b";
00312 open_516b.request.open = true;
00313 update_doors.call(open_516b);
00314
00315
00316 bwi_msgs::DoorHandlerInterface close_516a;
00317 close_516a.request.door = "d3_516a";
00318
00319 bwi_msgs::DoorHandlerInterface close_516b;
00320 close_516b.request.door = "d3_516b";
00321
00322 int whichseminar = rand() % 3;
00323 if (whichseminar==0)
00324 update_doors.call(close_516a);
00325 else if (whichseminar==1)
00326 update_doors.call(close_516b);
00327
00328 }
00329
00330 }
00331
00332
00333 void resetMemory() {
00334
00335 ros::NodeHandle n;
00336
00337 ROS_INFO("Resetting the knowledge base (forgetting everything)");
00338
00339 ros::ServiceClient current_state_query = n.serviceClient<bwi_kr_execution::CurrentStateQuery>("current_state_query");
00340 bwi_kr_execution::CurrentStateQuery query;
00341 current_state_query.call(query);
00342
00343 list<bwi_kr_execution::AspFluent> fluents_to_keep;
00344 remove_copy_if(query.response.answer.fluents.begin(), query.response.answer.fluents.end(), back_inserter(fluents_to_keep), NotPositionFluent());
00345
00346 ros::ServiceClient forget_everything = n.serviceClient<std_srvs::Empty>("reset_state");
00347 std_srvs::Empty nothingness;
00348 forget_everything.call(nothingness);
00349
00350 ros::ServiceClient put_fluents_back = n.serviceClient<bwi_kr_execution::UpdateFluents>("update_fluents");
00351 bwi_kr_execution::UpdateFluents toUpdate;
00352
00353 toUpdate.request.fluents.insert(toUpdate.request.fluents.end(),fluents_to_keep.begin(), fluents_to_keep.end());
00354
00355 put_fluents_back.call(toUpdate);
00356
00357 ROS_INFO_STREAM("The oblivion feels good!");
00358 }
00359
00360 int main(int argc, char**argv) {
00361 ros::init(argc, argv, "look_for_person");
00362 ros::NodeHandle n;
00363
00364 bwi_kr_execution::ExecutePlanGoal in_front_of_lab = initialStateFormula();
00365
00366
00367
00368 Client client("action_executor/execute_plan", true);
00369 client.waitForServer();
00370
00371 size_t task_counter = 0;
00372
00373 while (ros::ok() && task_counter < ep_per_phase * 100) {
00374
00375 phaseTransition(task_counter);
00376
00377
00378
00379
00380
00381
00382
00383 ROS_INFO("sending goal");
00384
00385
00386
00387
00388
00389 if (task_counter % 3 == 0)
00390 client.sendGoal(goToPlaceSpecific("l3_414a", "d3_414a1"));
00391 else if (task_counter % 3 == 1)
00392 client.sendGoal(goToPlaceSpecific("l3_414b", "d3_414b1"));
00393 else
00394 client.sendGoal(goToPlaceSpecific("l3_516", "d3_516a"));
00395
00396 ros::Rate rate(10);
00397
00398 ros::Time start_time = ros::Time::now();
00399 bool too_late = false;
00400
00401 while(ros::ok() && !client.getState().isDone()) {
00402 rate.sleep();
00403 ros::spinOnce();
00404
00405 if(max_time_per_episode >= 0) {
00406
00407 if(!too_late && ((ros::Time::now() - start_time) > ros::Duration(max_time_per_episode))) {
00408 too_late = true;
00409 client.cancelGoal();
00410 }
00411
00412 }
00413 }
00414
00415 if (client.getState() == actionlib::SimpleClientGoalState::ABORTED) {
00416 ROS_INFO("Aborted");
00417 } else if (client.getState() == actionlib::SimpleClientGoalState::PREEMPTED) {
00418 ROS_INFO("Preempted");
00419 }
00420
00421 else if (client.getState() == actionlib::SimpleClientGoalState::SUCCEEDED) {
00422 ROS_INFO("Succeeded!");
00423 } else
00424 ROS_INFO("Terminated");
00425
00426 ++task_counter;
00427 }
00428
00429 client.cancelAllGoals();
00430
00431 ros::spinOnce();
00432
00433 return 0;
00434 }