simulator_learning_tasks.cpp
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 
00015 typedef actionlib::SimpleActionClient<bwi_kr_execution::ExecutePlanAction> Client;
00016 
00017 using namespace std;
00018 
00019 const size_t ep_per_phase = 200;
00020 const double max_time_per_episode = 300.0; //set to negative to remove the limit
00021 
00022 
00023 struct Task {
00024   
00025   Task(const bwi_kr_execution::ExecutePlanGoal& initial_state, const bwi_kr_execution::ExecutePlanGoal& goal) :
00026         initial_state(initial_state), goal(goal) {}
00027   
00028   bwi_kr_execution::ExecutePlanGoal initial_state;
00029   bwi_kr_execution::ExecutePlanGoal goal;
00030 };
00031 
00032 struct NotPositionFluent {
00033 
00034   bool operator()(const bwi_kr_execution::AspFluent& fluent) const {
00035 
00036     return !(fluent.name == "at" ||
00037              fluent.name == "-at" ||
00038              fluent.name == "beside" ||
00039              fluent.name == "-beside" ||
00040              fluent.name == "facing" ||
00041              fluent.name == "-facing");
00042   }
00043 };
00044 
00045 bool goToInitialState(Client &client,const  bwi_kr_execution::ExecutePlanGoal& initial_state) {
00046 
00047   ROS_INFO("Going to the initial state");
00048 
00049   client.sendGoalAndWait(initial_state);
00050 
00051   if (client.getState() != actionlib::SimpleClientGoalState::SUCCEEDED) {
00052     ROS_INFO_STREAM("I can't reach the initial position, giving up... ");
00053     return false;
00054   }
00055   
00056   ROS_INFO("Inital state achieved");
00057 
00058   return true;
00059 }
00060 
00061 bwi_kr_execution::ExecutePlanGoal initialStateFormula() {
00062   
00063   bwi_kr_execution::ExecutePlanGoal go_to_initial_state;
00064   
00065     bwi_kr_execution::AspRule initial_rule1;
00066   bwi_kr_execution::AspFluent front_of_lab_door;
00067   front_of_lab_door.name = "not facing";
00068   front_of_lab_door.variables.push_back("d3_414b1");
00069   initial_rule1.body.push_back(front_of_lab_door);
00070   
00071   bwi_kr_execution::AspRule initial_rule2;
00072   bwi_kr_execution::AspFluent outside_of_lab;
00073   outside_of_lab.name = "not at";
00074   outside_of_lab.variables.push_back("l3_414b");
00075   initial_rule2.body.push_back(outside_of_lab);
00076 
00077   go_to_initial_state.aspGoal.push_back(initial_rule1);
00078   go_to_initial_state.aspGoal.push_back(initial_rule2);
00079   
00080   return go_to_initial_state;
00081 }
00082 
00083 bwi_kr_execution::ExecutePlanGoal goToPlace(const std::string& place) {
00084   
00085   bwi_kr_execution::ExecutePlanGoal goal;
00086 
00087     bwi_kr_execution::AspRule rule;
00088     bwi_kr_execution::AspFluent fluent;
00089     fluent.name = "not at";
00090     fluent.variables.push_back(place);
00091 
00092     rule.body.push_back(fluent);
00093 
00094     goal.aspGoal.push_back(rule);
00095     
00096     return goal;
00097 }
00098 
00099 void phaseTransition(int episode) {
00100   
00101   ROS_INFO_STREAM("episode: " << episode);
00102   
00103  if(episode % ep_per_phase == 0) {
00104    
00105    stringstream mkdir_cmd;
00106    mkdir_cmd << "mkdir -p values" << (episode/ep_per_phase);
00107    
00108    system(mkdir_cmd.str().c_str());
00109    
00110    stringstream cp_cmd;
00111    cp_cmd << "cp /var/tmp/bwi_action_execution/values/* values" << (episode/ep_per_phase);
00112    
00113    system(cp_cmd.str().c_str());
00114   
00115    ros::NodeHandle n;
00116    ros::ServiceClient update_doors = n.serviceClient<bwi_msgs::DoorHandlerInterface>("update_doors");
00117    
00118    bwi_msgs::DoorHandlerInterface open_all_doors;
00119    open_all_doors.request.all_doors = true;
00120    open_all_doors.request.open = true;
00121    
00122    update_doors.call(open_all_doors);
00123    
00124    bwi_msgs::DoorHandlerInterface close_414b3;
00125   close_414b3.request.door = "d3_414b3";
00126   
00127    bwi_msgs::DoorHandlerInterface close_414a3;
00128    close_414a3.request.door = "d3_414a3";
00129    
00130    bwi_msgs::DoorHandlerInterface close_414a2;
00131    close_414a2.request.door = "d3_414a2";
00132    
00133    bwi_msgs::DoorHandlerInterface close_414a1;
00134    close_414a1.request.door = "d3_414a1";
00135    
00136    switch(episode) {
00137      case 0:       
00138               update_doors.call(close_414b3);
00139               update_doors.call(close_414a3);
00140               update_doors.call(close_414a2);
00141           break;
00142      case ep_per_phase:
00143               update_doors.call(close_414b3);
00144               update_doors.call(close_414a3);
00145               update_doors.call(close_414a1); 
00146           break;
00147      case ep_per_phase * 2:
00148               update_doors.call(close_414a1);
00149        break;
00150    }
00151    
00152  }
00153   
00154 }
00155 
00156 
00157 void resetMemory() {
00158 
00159   ros::NodeHandle n;
00160 
00161   ROS_INFO("Resetting the knowledge base (forgetting everything)");
00162 
00163   ros::ServiceClient current_state_query = n.serviceClient<bwi_kr_execution::CurrentStateQuery>("current_state_query");
00164   bwi_kr_execution::CurrentStateQuery query;
00165   current_state_query.call(query);
00166 
00167   list<bwi_kr_execution::AspFluent> fluents_to_keep;
00168   remove_copy_if(query.response.answer.fluents.begin(), query.response.answer.fluents.end(), back_inserter(fluents_to_keep), NotPositionFluent());
00169 
00170   ros::ServiceClient forget_everything = n.serviceClient<std_srvs::Empty>("reset_state");
00171   std_srvs::Empty nothingness;
00172   forget_everything.call(nothingness);
00173 
00174   ros::ServiceClient put_fluents_back = n.serviceClient<bwi_kr_execution::UpdateFluents>("update_fluents");
00175   bwi_kr_execution::UpdateFluents toUpdate;
00176 
00177   toUpdate.request.fluents.insert(toUpdate.request.fluents.end(),fluents_to_keep.begin(), fluents_to_keep.end());
00178 
00179   put_fluents_back.call(toUpdate);
00180 
00181   ROS_INFO_STREAM("The oblivion feels good!");
00182 }
00183 
00184 int main(int argc, char**argv) {
00185   ros::init(argc, argv, "look_for_person");
00186   ros::NodeHandle n;
00187 
00188   bwi_kr_execution::ExecutePlanGoal in_front_of_lab = initialStateFormula();
00189   
00190   Task chosen(in_front_of_lab, goToPlace("l3_414a"));
00191   
00192 
00193   Client client("action_executor/execute_plan", true);
00194   client.waitForServer();
00195   
00196   size_t task_counter = 0;
00197 
00198   while (ros::ok() && task_counter < ep_per_phase * 3) {
00199     
00200     phaseTransition(task_counter);
00201     
00202     if (!goToInitialState(client, chosen.initial_state))
00203       continue;
00204     
00205     resetMemory();
00206 
00207 
00208     ROS_INFO("sending goal");
00209     client.sendGoal(chosen.goal);
00210     
00211     ros::Rate rate(10);
00212     
00213     ros::Time start_time = ros::Time::now();
00214     bool too_late = false;
00215     
00216     while(ros::ok() && !client.getState().isDone()) {
00217       rate.sleep();
00218       ros::spinOnce(); //not sure this is necessary.
00219       
00220       if(max_time_per_episode >= 0) {
00221       
00222         if(!too_late && ((ros::Time::now() - start_time) > ros::Duration(max_time_per_episode))) {
00223           too_late = true;
00224           client.cancelGoal();
00225         }
00226       
00227       }
00228     }
00229 
00230     if (client.getState() == actionlib::SimpleClientGoalState::ABORTED) {
00231       ROS_INFO("Aborted");
00232     } else if (client.getState() == actionlib::SimpleClientGoalState::PREEMPTED) {
00233       ROS_INFO("Preempted");
00234     }
00235 
00236     else if (client.getState() == actionlib::SimpleClientGoalState::SUCCEEDED) {
00237       ROS_INFO("Succeeded!");
00238     } else
00239       ROS_INFO("Terminated");
00240     
00241     ++task_counter;
00242   }
00243 
00244   client.cancelAllGoals();
00245   
00246   ros::spinOnce();
00247   
00248   return 0;
00249 }


bwi_tasks
Author(s): Matteo Leonetti, Shiqi Zhang
autogenerated on Thu Jun 6 2019 17:58:00