AskPerson.cpp
Go to the documentation of this file.
00001 #include "AskPerson.h"
00002 
00003 #include "ActionFactory.h"
00004 
00005 #include "CallGUI.h"
00006 
00007 #include "bwi_kr_execution/AspFluent.h"
00008 
00009 #include <bwi_kr_execution/UpdateFluents.h>
00010 
00011 #include <ros/ros.h>
00012 #include <sound_play/sound_play.h>
00013 
00014 #include <string>
00015 #include <iostream>
00016 
00017 using namespace std;
00018 
00019 namespace bwi_krexec {
00020 
00021 AskPerson::AskPerson() : 
00022             person_to_ask(),
00023             person_to_know(),
00024             done(false){
00025             }
00026 
00027 ros::Publisher AskPerson::ask_pub;
00028 bool AskPerson::pub_set(false);
00029   
00030 void AskPerson::run() {
00031 
00032   ros::NodeHandle n;
00033 
00034   if (!pub_set) { 
00035     ask_pub = n.advertise<sound_play::SoundRequest>("robotsound", 1000);
00036     pub_set = true;
00037   }
00038 
00039   if (ask_pub.getNumSubscribers() == 0) return; //if the subscriber is not connected, sleep
00040   
00041   ros::ServiceClient krClient = n.serviceClient<bwi_kr_execution::UpdateFluents> ( "update_fluents" );
00042   krClient.waitForExistence();
00043   
00044   //speak
00045   sound_play::SoundRequest sound_req;
00046   sound_req.sound = sound_play::SoundRequest::SAY;
00047   sound_req.command = sound_play::SoundRequest::PLAY_ONCE;
00048 
00049   stringstream ss;
00050   ss << "Hi " << person_to_ask << "! ";
00051   ss << "Do you know where " << person_to_know << " is?";
00052   sound_req.arg = ss.str();
00053   ask_pub.publish(sound_req);
00054 
00055   vector<string> options;
00056   options.push_back("Yes! " + person_to_know + " is in GDC and I know exactly where.");
00057   options.push_back("No, I have no idea.");
00058 
00059   CallGUI askPerson("askPerson", CallGUI::CHOICE_QUESTION,  "Hi " + person_to_ask + "! Do you know where " + person_to_know + " is?", 60.0, options);
00060   askPerson.run();
00061 
00062   int response = askPerson.getResponseIndex();
00063 
00064   if (response < 0) {    
00065     bwi_kr_execution::UpdateFluents uf;
00066     bwi_kr_execution::AspFluent fluent;
00067     
00068     fluent.timeStep = 0;
00069     fluent.variables.push_back(person_to_ask);
00070     fluent.variables.push_back(person_to_know);
00071 
00072     fluent.name = "-know";
00073 
00074     uf.request.fluents.push_back(fluent);
00075     krClient.call(uf);
00076   }
00077 
00078   if (response == 1) {
00079     sound_req.arg = "OK, thank you anyway!";
00080     ask_pub.publish(sound_req);
00081     CallGUI thank("thank", CallGUI::DISPLAY,  "OK, thank you anyway!");
00082     thank.run();
00083 
00084     bwi_kr_execution::UpdateFluents uf;
00085     bwi_kr_execution::AspFluent fluent;
00086     
00087     fluent.timeStep = 0;
00088     fluent.variables.push_back(person_to_ask);
00089     fluent.variables.push_back(person_to_know);
00090 
00091     fluent.name = "-know";
00092 
00093     uf.request.fluents.push_back(fluent);
00094     krClient.call(uf);
00095   }
00096 
00097   if (response == 0) {
00098 
00099       bool know = false;
00100       int count = 0;
00101 
00102       sound_req.arg = "Great! Please tell me the room number.";
00103       CallGUI *askPerson = new CallGUI("askPerson", CallGUI::TEXT_QUESTION,  "Great! Please tell me the room number.", 60.0);
00104       
00105       while ((! know) && (count < 3)) {
00106 
00107         ask_pub.publish(sound_req);
00108         askPerson->run();
00109 
00110         if ((askPerson->getResponseIndex() == -3) && (askPerson->getResponse() != "")) {
00111               bwi_kr_execution::UpdateFluents uf;
00112               bwi_kr_execution::AspFluent fluent;
00113               
00114               fluent.timeStep = 0;
00115               fluent.variables.push_back(person_to_know);
00116               fluent.variables.push_back(askPerson->getResponse());
00117 
00118               fluent.name = "inroom";
00119 
00120               uf.request.fluents.push_back(fluent);
00121 
00122               krClient.call(uf);
00123               know = uf.response.consistent;
00124         }
00125 
00126         count++;
00127 
00128         sound_req.arg = "The room number doesn't exist. Please try again.";
00129         delete askPerson;
00130         askPerson = new CallGUI("askPerson", CallGUI::TEXT_QUESTION,  "The room number doesn't exist. Please try again.", 60.0);
00131       }
00132 
00133       sound_req.arg = "Thank you!";
00134       CallGUI *thank = new CallGUI("thank", CallGUI::DISPLAY,  "Thank you!");
00135 
00136       if (!know) {
00137         sound_req.arg = "OK, thank you anyway!";
00138         delete thank;
00139         thank = new CallGUI("thank", CallGUI::DISPLAY,  "OK, thank you anyway!");
00140       }
00141 
00142       //ask_pub.publish(sound_req);
00143       thank->run();
00144 
00145       bwi_kr_execution::UpdateFluents uf;
00146       bwi_kr_execution::AspFluent fluent;
00147       
00148       fluent.timeStep = 0;
00149       fluent.variables.push_back(person_to_ask);
00150       fluent.variables.push_back(person_to_know);
00151 
00152       fluent.name = (know ? "know" : "-know");
00153 
00154       uf.request.fluents.push_back(fluent);
00155       krClient.call(uf);
00156 
00157   }
00158 
00159   done = true;
00160 
00161 }  
00162   
00163 actasp::Action* AskPerson::cloneAndInit(const actasp::AspFluent& fluent) const {
00164   AskPerson *newAction = new AskPerson();
00165   newAction->person_to_ask = fluent.getParameters().at(0);
00166   newAction->person_to_know = fluent.getParameters().at(1);
00167   
00168   return newAction;
00169 }
00170 
00171 std::vector<std::string> AskPerson::getParameters() const {
00172   vector<string> param;
00173   param.push_back(person_to_ask);
00174   param.push_back(person_to_know);
00175   return param;
00176 }
00177 
00178 
00179 ActionFactory AskPersonFactory(new AskPerson());
00180   
00181 }


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