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_kr_execution/CurrentStateQuery.h>
00007 #include <bwi_kr_execution/UpdateFluents.h>
00008 
00009 #include <std_srvs/Empty.h>
00010 #include <ros/ros.h>
00011 
00012 #include <ctime> //to seed rand
00013 
00014 typedef actionlib::SimpleActionClient<bwi_kr_execution::ExecutePlanAction> Client;
00015 
00016 using namespace std;
00017 
00018 
00019 struct Task {
00020   
00021   Task(const bwi_kr_execution::ExecutePlanGoal& initial_state, const bwi_kr_execution::ExecutePlanGoal& goal) :
00022         initial_state(initial_state), goal(goal) {}
00023   
00024   bwi_kr_execution::ExecutePlanGoal initial_state;
00025   bwi_kr_execution::ExecutePlanGoal goal;
00026 };
00027 
00028 struct NotPositionFluent {
00029 
00030   bool operator()(const bwi_kr_execution::AspFluent& fluent) const {
00031 
00032     return !(fluent.name == "at" ||
00033              fluent.name == "-at" ||
00034              fluent.name == "beside" ||
00035              fluent.name == "-beside" ||
00036              fluent.name == "facing" ||
00037              fluent.name == "-facing");
00038   }
00039 };
00040 
00041 bool goToInitialState(Client &client,const  bwi_kr_execution::ExecutePlanGoal& initial_state) {
00042 
00043   ROS_INFO("Going to the initial state");
00044 
00045   client.sendGoalAndWait(initial_state);
00046 
00047   if (client.getState() != actionlib::SimpleClientGoalState::SUCCEEDED) {
00048     ROS_INFO_STREAM("I can't reach the initial position, giving up... ");
00049     return false;
00050   }
00051   
00052   ROS_INFO("Inital state achieved");
00053 
00054   return true;
00055 }
00056 
00057 bwi_kr_execution::ExecutePlanGoal initialStateFormula() {
00058   
00059   bwi_kr_execution::ExecutePlanGoal go_to_initial_state;
00060   
00061     bwi_kr_execution::AspRule initial_rule1;
00062   bwi_kr_execution::AspFluent front_of_lab_door;
00063   front_of_lab_door.name = "not facing";
00064   front_of_lab_door.variables.push_back("d3_414b1");
00065   initial_rule1.body.push_back(front_of_lab_door);
00066   
00067   bwi_kr_execution::AspRule initial_rule2;
00068   bwi_kr_execution::AspFluent outside_of_lab;
00069   outside_of_lab.name = "at";
00070   outside_of_lab.variables.push_back("l3_414b");
00071   initial_rule2.body.push_back(outside_of_lab);
00072 
00073   go_to_initial_state.aspGoal.push_back(initial_rule1);
00074   go_to_initial_state.aspGoal.push_back(initial_rule2);
00075   
00076   return go_to_initial_state;
00077 }
00078   
00079   
00080 bwi_kr_execution::ExecutePlanGoal findPerson(const std::string& personName) {
00081   
00082   bwi_kr_execution::ExecutePlanGoal goal;
00083 
00084     bwi_kr_execution::AspRule main_rule;
00085     bwi_kr_execution::AspFluent fluent;
00086     fluent.name = "not ingdc";
00087     fluent.variables.push_back(personName);
00088 
00089     main_rule.body.push_back(fluent);
00090 
00091 
00092     bwi_kr_execution::AspFluent fluent_not;
00093     fluent_not.name = "not -ingdc";
00094     fluent_not.variables.push_back(personName);
00095 
00096     main_rule.body.push_back(fluent_not);
00097     
00098     bwi_kr_execution::AspRule flag_rule;
00099     bwi_kr_execution::AspFluent find_person_flag;
00100     find_person_flag.name = "findPersonTask";
00101     flag_rule.head.push_back(find_person_flag);
00102 
00103     goal.aspGoal.push_back(main_rule);
00104     goal.aspGoal.push_back(flag_rule);
00105     
00106     return goal;
00107 }
00108 
00109 bwi_kr_execution::ExecutePlanGoal goToPlace(const std::string& place) {
00110   
00111   bwi_kr_execution::ExecutePlanGoal goal;
00112 
00113     bwi_kr_execution::AspRule rule;
00114     bwi_kr_execution::AspFluent fluent;
00115     fluent.name = "not at";
00116     fluent.variables.push_back(place);
00117 
00118     rule.body.push_back(fluent);
00119 
00120     goal.aspGoal.push_back(rule);
00121     
00122     return goal;
00123 }
00124 
00125 
00126 void resetMemory() {
00127 
00128   ros::NodeHandle n;
00129 
00130   ROS_INFO("Resetting the knowledge base (forgetting everything)");
00131 
00132   ros::ServiceClient current_state_query = n.serviceClient<bwi_kr_execution::CurrentStateQuery>("current_state_query");
00133   bwi_kr_execution::CurrentStateQuery query;
00134   current_state_query.call(query);
00135 
00136   list<bwi_kr_execution::AspFluent> fluents_to_keep;
00137   remove_copy_if(query.response.answer.fluents.begin(), query.response.answer.fluents.end(), back_inserter(fluents_to_keep), NotPositionFluent());
00138 
00139   ros::ServiceClient forget_everything = n.serviceClient<std_srvs::Empty>("reset_state");
00140   std_srvs::Empty nothingness;
00141   forget_everything.call(nothingness);
00142 
00143   ros::ServiceClient put_fluents_back = n.serviceClient<bwi_kr_execution::UpdateFluents>("update_fluents");
00144   bwi_kr_execution::UpdateFluents toUpdate;
00145 
00146   toUpdate.request.fluents.insert(toUpdate.request.fluents.end(),fluents_to_keep.begin(), fluents_to_keep.end());
00147 
00148   put_fluents_back.call(toUpdate);
00149 
00150   ROS_INFO_STREAM("The oblivion feels good!");
00151 }
00152 
00153 int main(int argc, char**argv) {
00154   ros::init(argc, argv, "look_for_person");
00155   ros::NodeHandle n;
00156 
00157   bwi_kr_execution::ExecutePlanGoal in_front_of_lab = initialStateFormula();
00158   
00159   vector<Task> tasks;
00160 //  tasks.push_back(Task(in_front_of_lab, goToPlace("l2_302")));
00161   tasks.push_back(Task(in_front_of_lab, findPerson("matteo")));
00162 //  tasks.push_back(Task(in_front_of_lab, findPerson("peter")));
00163 //  tasks.push_back(Task(in_front_of_lab, goToPlace("l3_418")));
00164 //  tasks.push_back(Task(in_front_of_lab, goToPlace("l3_414a")));
00165   //tasks.push_back(Task(in_front_of_lab, goToPlace("l3_418")));
00166   //tasks.push_back(Task(in_front_of_lab, goToPlace("l3_414b")));
00167   
00168 
00169   Client client("action_executor/execute_plan", true);
00170   client.waitForServer();
00171   
00172   srand(time(0));
00173   
00174   size_t task_counter = 0;
00175 
00176   while (ros::ok()) {
00177     
00178     Task &chosen = tasks[task_counter];
00179     task_counter = (task_counter + 1) % tasks.size();
00180     
00181     if (!goToInitialState(client, chosen.initial_state))
00182       continue;
00183 
00184    // resetMemory();
00185 
00186 
00187     ROS_INFO("sending goal");
00188     client.sendGoal(chosen.goal);
00189     
00190     ros::Rate rate(10);
00191     
00192     ros::Time start_time = ros::Time::now();
00193     bool too_late = false;
00194     
00195     while(ros::ok() && !client.getState().isDone()) {
00196       rate.sleep();
00197       ros::spinOnce(); //not sure this is necessary.
00198       
00199       if(!too_late && ((ros::Time::now() - start_time) > ros::Duration(900.0))) { //15 minutes to complete the task
00200         too_late = true;
00201         client.cancelGoal();
00202       }
00203     }
00204 
00205     if (client.getState() == actionlib::SimpleClientGoalState::ABORTED) {
00206       ROS_INFO("Aborted");
00207     } else if (client.getState() == actionlib::SimpleClientGoalState::PREEMPTED) {
00208       ROS_INFO("Preempted");
00209     }
00210 
00211     else if (client.getState() == actionlib::SimpleClientGoalState::SUCCEEDED) {
00212       ROS_INFO("Succeeded!");
00213     } else
00214       ROS_INFO("Terminated");
00215 
00216   }
00217 
00218   client.cancelAllGoals();
00219   
00220   ros::spinOnce();
00221   
00222   return 0;
00223 }


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