CallElevator.cpp
Go to the documentation of this file.
00001 #include "CallElevator.h"
00002 
00003 #include <boost/foreach.hpp>
00004 
00005 #include "ActionFactory.h"
00006 #include "../StaticFacts.h"
00007 
00008 #include "bwi_kr_execution/UpdateFluents.h"
00009 #include "ros/console.h"
00010 #include "ros/ros.h"
00011 
00012 namespace bwi_krexec {
00013 
00014 CallElevator::CallElevator() :
00015             done(false),
00016             asked(false),
00017             failed(false) {}
00018 
00019 void CallElevator::run() {
00020   
00021   if(!asked) {
00022     std::string direction_text = (going_up) ? "up" : "down";
00023 
00024     // Get the doors for this elevator.
00025     doors.clear();
00026     std::list<actasp::AspAtom> static_facts = StaticFacts::staticFacts(); 
00027     BOOST_FOREACH(const actasp::AspAtom fact, static_facts) {
00028       if (fact.getName() == "elevhasdoor") {
00029         std::vector<std::string> params = fact.getParameters();
00030         if (params[0] == elevator) {
00031           doors.push_back(params[1]);
00032         }
00033       }
00034     }
00035 
00036     if (doors.size() == 0) {
00037       ROS_ERROR_STREAM("Unable to retrieve doors for elevator " << elevator << ". Cannot complete action!");
00038       done = true;
00039       failed = true;
00040     } else {
00041       askToCallElevator.reset(new CallGUI("askToCallElevator", 
00042                                           CallGUI::CHOICE_QUESTION,  
00043                                           "Could you call elevator " + elevator + " to go " + direction_text + 
00044                                             ", and then let me know which door opened?", 
00045                                           120.0f, 
00046                                           doors));
00047       askToCallElevator->run();
00048     }
00049     asked = true;
00050   } else if(!done) {
00051     if (askToCallElevator->hasFinished()) {
00052       // Check response to see it's positive.
00053       int response_idx = askToCallElevator->getResponseIndex();
00054       if (response_idx >= 0 && response_idx < doors.size()) {
00055 
00056         ros::NodeHandle n;
00057         ros::ServiceClient krClient = n.serviceClient<bwi_kr_execution::UpdateFluents> ( "update_fluents" );
00058         krClient.waitForExistence();
00059         bwi_kr_execution::UpdateFluents uf;
00060         bwi_kr_execution::AspFluent open_door;
00061         open_door.name = "open";
00062         open_door.variables.push_back(doors[response_idx]);
00063         uf.request.fluents.push_back(open_door);
00064         krClient.call(uf);
00065 
00066         CallGUI thanks("thanks", CallGUI::DISPLAY,  "Thanks! Would you mind helping me inside the elevator as well?");
00067         thanks.run();
00068       } else {
00069         // A door didn't open in the timeout specified.
00070         failed = true;
00071       }
00072       done = true;
00073     }
00074   }
00075  
00076   
00077 }
00078 
00079 bool CallElevator::hasFinished() const {
00080   return done;
00081 }
00082 
00083 bool CallElevator::hasFailed() const {
00084   return failed;
00085 }
00086 
00087 actasp::Action *CallElevator::cloneAndInit(const actasp::AspFluent & fluent) const {
00088   CallElevator *other = new CallElevator();
00089   other->elevator = fluent.getParameters().at(0);
00090   other->going_up = fluent.getParameters().at(1) == "up"; //throws an exception if the parameter doesn't exist
00091   return other;
00092 }
00093 
00094 std::vector<std::string> CallElevator::getParameters() const {
00095   std::vector<std::string> params;
00096   params.reserve(2);
00097   params.push_back(elevator);
00098   params.push_back(going_up? "up" : "down");
00099   return params;
00100 }
00101 
00102 //if you want the action to be available only in simulation, or only
00103 //on the robot, use the constructor that also takes a boolean.
00104 ActionFactory callElevator(new CallElevator());
00105 
00106 }


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