LogicalNavigation.cpp
Go to the documentation of this file.
00001 #include "LogicalNavigation.h"
00002 
00003 #include "ActionFactory.h"
00004 
00005 #include "actasp/AspFluent.h"
00006 
00007 #include <bwi_planning_common/PlannerInterface.h>
00008 #include <bwi_kr_execution/UpdateFluents.h>
00009 #include <bwi_kr_execution/AspFluent.h>
00010 
00011 #include <ros/ros.h>
00012 
00013 #include <sstream>
00014 
00015 using namespace ros;
00016 using namespace std;
00017 using namespace actasp;
00018 
00019 namespace bwi_krexec {
00020         
00021 LogicalNavigation::LogicalNavigation(const std::string& name, const std::vector<std::string>& parameters) :
00022                         name(name),
00023                         parameters(parameters),
00024                         done(false),
00025       request_in_progress(false) {}
00026 
00027 LogicalNavigation::~LogicalNavigation() {
00028   if (request_in_progress) {
00029     // The goal was sent but the action is being terminated before being allowed to finish. Cancel that command!
00030     lnac->cancelGoal();
00031     delete lnac;
00032   }
00033 }
00034 
00035 
00036 struct PlannerAtom2AspFluent {
00037   bwi_kr_execution::AspFluent operator()(const bwi_planning_common::PlannerAtom& atom) {
00038     
00039     bwi_kr_execution::AspFluent fluent;
00040     fluent.name = atom.name;
00041     if(!atom.value.empty()) {
00042       fluent.variables.insert(fluent.variables.end(),atom.value.begin(),atom.value.end());
00043       
00044       fluent.timeStep = 0; //the observation does not provide a timeStep
00045     }
00046     
00047     return fluent;
00048   }
00049 };
00050     
00051 
00052 void LogicalNavigation::run() {
00053   
00054   ROS_DEBUG_STREAM("Executing " << name);
00055 
00056   if (!request_in_progress) {
00057     lnac = new actionlib::SimpleActionClient<bwi_msgs::LogicalNavigationAction>("execute_logical_goal",
00058                                                                                                  true);
00059     lnac->waitForServer();
00060     goal.command.name = name;
00061     goal.command.value = parameters;
00062     lnac->sendGoal(goal);
00063     request_in_progress = true;
00064   }
00065 
00066   bool finished_before_timeout = lnac->waitForResult(ros::Duration(0.5f));
00067 
00068         //senseState takes a bit longer than 0.5f to complete, but fluents have to be updated
00069         if(name == "senseState"){
00070                 ros::Rate wait_rate(0.5f);
00071                 while(!(finished_before_timeout = lnac->waitForResult(ros::Duration(0.5f)))){
00072                         wait_rate.sleep();
00073                 }
00074         }
00075 
00076   // If the action finished, need to do some work here.
00077   if (finished_before_timeout) {
00078     bwi_msgs::LogicalNavigationResultConstPtr result = lnac->getResult();
00079     
00080     // Update fluents based on the result of the logical nav request.
00081     NodeHandle n;
00082     ros::ServiceClient krClient = n.serviceClient<bwi_kr_execution::UpdateFluents> ( "update_fluents" );
00083     krClient.waitForExistence();
00084     bwi_kr_execution::UpdateFluents uf;
00085     transform(result->observations.begin(),
00086               result->observations.end(),
00087               back_inserter(uf.request.fluents),PlannerAtom2AspFluent());
00088     krClient.call(uf);
00089 
00090     // Mark the request as completed.
00091     done = true;
00092 
00093     // Cleanup the simple action client.
00094     request_in_progress = false;
00095     delete lnac;
00096   }
00097         
00098 }
00099 
00100 Action *LogicalNavigation::cloneAndInit(const actasp::AspFluent & fluent) const {
00101   return new LogicalNavigation(fluent.getName(),fluent.getParameters());
00102 }
00103 
00104 
00105 
00106 // static ActionFactory gothroughFactory(new LogicalNavigation("gothrough"));
00107 // static ActionFactory approachFactory(new LogicalNavigation("approach"));
00108         
00109 } //namespace


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