single_plan_executor.cpp
Go to the documentation of this file.
00001 
00002 #include "msgs_utils.h"
00003 #include "RemoteReasoner.h"
00004 #include "StaticFacts.h"
00005 
00006 #include "actasp/action_utils.h"
00007 #include "actasp/executors/ReplanningActionExecutor.h"
00008 #include "actasp/ExecutionObserver.h"
00009 #include "actasp/PlanningObserver.h"
00010 #include "actasp/AnswerSet.h"
00011 #include <actasp/reasoners/Clingo4_2.h>
00012 
00013 #include "bwi_kr_execution/ExecutePlanAction.h"
00014 
00015 #include "actions/ActionFactory.h"
00016 #include "actions/LogicalNavigation.h"
00017 
00018 #include <actionlib/server/simple_action_server.h>
00019 
00020 #include <ros/ros.h>
00021 #include <ros/package.h>
00022 #include <ros/console.h>
00023 
00024 #include <boost/filesystem.hpp>
00025 
00026 #include <string>
00027 
00028 const int MAX_N = 30;
00029 const int PLANNER_TIMEOUT = 40; //seconds
00030 const std::string queryDirectory("/tmp/bwi_action_execution/");
00031 
00032 
00033 using namespace std;
00034 using namespace bwi_krexec;
00035 using namespace actasp;
00036 
00037 typedef actionlib::SimpleActionServer<bwi_kr_execution::ExecutePlanAction> Server;
00038 
00039 
00040 ActionExecutor *executor;
00041 
00042 struct PrintFluent {
00043   
00044   PrintFluent(ostream& stream) : stream(stream) {}
00045   
00046   string operator()(const AspFluent& fluent) {
00047     stream << fluent.toString() << " ";
00048   }
00049   
00050   ostream &stream;
00051   
00052 };
00053 
00054 struct Observer : public ExecutionObserver, public PlanningObserver {
00055   
00056   void actionStarted(const AspFluent& action) throw() {
00057     ROS_INFO_STREAM("Starting execution: " << action.toString());
00058   }
00059   
00060   void actionTerminated(const AspFluent& action) throw() {
00061     ROS_INFO_STREAM("Terminating execution: " << action.toString());
00062   }
00063   
00064   
00065   void planChanged(const AnswerSet& newPlan) throw() {
00066    stringstream planStream;
00067    
00068    ROS_INFO_STREAM("plan size: " << newPlan.getFluents().size());
00069    
00070    copy(newPlan.getFluents().begin(),newPlan.getFluents().end(),ostream_iterator<string>(planStream," "));
00071    
00072    ROS_INFO_STREAM(planStream.str());
00073   }
00074   
00075     void goalChanged(std::vector<actasp::AspRule> newGoalRules) throw() {}
00076   
00077   void policyChanged(PartialPolicy* policy) throw() {}
00078   
00079   
00080 };
00081 
00082 void executePlan(const bwi_kr_execution::ExecutePlanGoalConstPtr& plan, Server* as) {
00083 
00084   vector<AspRule> goalRules;
00085 
00086   transform(plan->aspGoal.begin(),plan->aspGoal.end(),back_inserter(goalRules),TranslateRule());
00087 
00088         //Update fluents before sending new goals
00089         LogicalNavigation senseState("senseState");
00090         senseState.run();
00091 
00092   executor->setGoal(goalRules);
00093 
00094   ros::Rate loop(10);
00095 
00096   while (!executor->goalReached() && !executor->failed() && ros::ok() && as->isActive()) {
00097 
00098     if (!as->isPreemptRequested()) {
00099       executor->executeActionStep();
00100     }
00101     else {
00102       
00103       as->setPreempted();
00104       
00105       if (executor->goalReached()) 
00106         ROS_INFO("Preempted, but execution succeded");
00107       else 
00108         ROS_INFO("Preempted, execution aborted");
00109       
00110       if(as->isNewGoalAvailable()) {
00111         goalRules.clear();
00112         const bwi_kr_execution::ExecutePlanGoalConstPtr& newGoal = as->acceptNewGoal();
00113         transform(newGoal->aspGoal.begin(),newGoal->aspGoal.end(),back_inserter(goalRules),TranslateRule());
00114 
00115                                 //Update fluents before resending goal
00116                                 senseState.run();
00117         executor->setGoal(goalRules);
00118       }
00119     }
00120          loop.sleep();
00121   }
00122 
00123 
00124   if (executor->goalReached()) {
00125     ROS_INFO("Execution succeded");
00126     if(as->isActive())
00127       as->setSucceeded();
00128   } else {
00129     ROS_INFO("Execution failed");
00130    if(as->isActive())
00131     as->setAborted();
00132   }
00133 }
00134 
00135 int main(int argc, char**argv) {
00136   ros::init(argc, argv, "action_executor");
00137   ros::NodeHandle n;
00138 
00139 //   if (ros::console::set_logger_level(ROSCONSOLE_DEFAULT_NAME, ros::console::levels::Debug)) {
00140 //     ros::console::notifyLoggerLevelsChanged();
00141 //   }
00142   
00143   ros::NodeHandle privateNode("~");
00144   string domainDirectory;
00145   n.param<std::string>("bwi_kr_execution/domain_directory", domainDirectory, ros::package::getPath("bwi_kr_execution")+"/domain/");
00146   
00147   if(domainDirectory.at(domainDirectory.size()-1) != '/')
00148     domainDirectory += '/';
00149 
00150 //  create initial state
00151   LogicalNavigation setInitialState("senseState");
00152   setInitialState.run();
00153 
00154 
00155   bool simulating;
00156   privateNode.param<bool>("simulation",simulating,false);
00157   ActionFactory::setSimulation(simulating); 
00158   
00159   boost::filesystem::create_directories(queryDirectory);
00160 
00161   FilteringQueryGenerator *generator = new Clingo4_2("n",queryDirectory,domainDirectory,actionMapToSet(ActionFactory::actions()),PLANNER_TIMEOUT);
00162   AspKR *reasoner = new RemoteReasoner(generator, MAX_N,actionMapToSet(ActionFactory::actions()));
00163   StaticFacts::retrieveStaticFacts(reasoner, domainDirectory);
00164   
00165   //need a pointer to the specific type for the observer
00166   ReplanningActionExecutor *replanner = new ReplanningActionExecutor(reasoner,reasoner,ActionFactory::actions());
00167   executor = replanner;
00168   
00169   Observer observer;
00170   executor->addExecutionObserver(&observer);
00171   replanner->addPlanningObserver(&observer);
00172 
00173   Server server(privateNode, "execute_plan", boost::bind(&executePlan, _1, &server), false);
00174   server.start();
00175 
00176   ros::spin();
00177 
00178   
00179   return 0;
00180 }


bwi_kr_execution
Author(s): Matteo Leonetti, Piyush Khandelwal
autogenerated on Thu Jun 6 2019 17:57:37