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   // If the action finished, need to do some work here.
00069   if (finished_before_timeout) {
00070     bwi_msgs::LogicalNavigationResultConstPtr result = lnac->getResult();
00071     
00072     // Update fluents based on the result of the logical nav request.
00073     NodeHandle n;
00074     ros::ServiceClient krClient = n.serviceClient<bwi_kr_execution::UpdateFluents> ( "update_fluents" );
00075     krClient.waitForExistence();
00076     bwi_kr_execution::UpdateFluents uf;
00077     transform(result->observations.begin(),
00078               result->observations.end(),
00079               back_inserter(uf.request.fluents),PlannerAtom2AspFluent());
00080     krClient.call(uf);
00081 
00082     // Mark the request as completed.
00083     done = true;
00084 
00085     // Cleanup the simple action client.
00086     request_in_progress = false;
00087     delete lnac;
00088   }
00089         
00090 }
00091 
00092 Action *LogicalNavigation::cloneAndInit(const actasp::AspFluent & fluent) const {
00093   return new LogicalNavigation(fluent.getName(),fluent.getParameters());
00094 }
00095 
00096 
00097 
00098 // static ActionFactory gothroughFactory(new LogicalNavigation("gothrough"));
00099 // static ActionFactory approachFactory(new LogicalNavigation("approach"));
00100         
00101 } //namespace


bwi_kr_execution
Author(s): Matteo Leonetti, Piyush Khandelwal
autogenerated on Fri Aug 28 2015 10:14:46