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 //Matteo: the sound node may not be up, I'm commenting this line.
00041   
00042   ros::ServiceClient krClient = n.serviceClient<bwi_kr_execution::UpdateFluents> ( "update_fluents" );
00043   krClient.waitForExistence();
00044   
00045   //speak
00046   sound_play::SoundRequest sound_req;
00047   sound_req.sound = sound_play::SoundRequest::SAY;
00048   sound_req.command = sound_play::SoundRequest::PLAY_ONCE;
00049 
00050   stringstream ss;
00051   ss << "Hi " << person_to_ask << "! ";
00052   ss << "Do you know where " << person_to_know << " is?";
00053   sound_req.arg = ss.str();
00054   ask_pub.publish(sound_req);
00055 
00056   vector<string> options;
00057   options.push_back("Yes! In GDC");
00058   options.push_back("Yes! NOT in GDC");
00059   options.push_back("No, I have no idea");
00060 
00061   CallGUI askPerson("askPerson", CallGUI::CHOICE_QUESTION,  "Hi " + person_to_ask + "! Do you know where " + person_to_know + " is?", 60.0, options);
00062   askPerson.run();
00063 
00064   int response = askPerson.getResponseIndex();
00065 
00066   if (response < 0) {    
00067     bwi_kr_execution::UpdateFluents uf;
00068     bwi_kr_execution::AspFluent fluent;
00069     
00070     fluent.timeStep = 0;
00071     fluent.variables.push_back(person_to_ask);
00072     fluent.variables.push_back(person_to_know);
00073 
00074     fluent.name = "-know";
00075 
00076     uf.request.fluents.push_back(fluent);
00077     krClient.call(uf);
00078   }
00079 
00080   if (response == 2) {
00081     sound_req.arg = "OK, thank you anyway!";
00082     ask_pub.publish(sound_req);
00083     CallGUI thank("thank", CallGUI::DISPLAY,  "OK, thank you anyway!");
00084     thank.run();
00085 
00086     bwi_kr_execution::UpdateFluents uf;
00087     bwi_kr_execution::AspFluent fluent;
00088     
00089     fluent.timeStep = 0;
00090     fluent.variables.push_back(person_to_ask);
00091     fluent.variables.push_back(person_to_know);
00092 
00093     fluent.name = "-know";
00094 
00095     uf.request.fluents.push_back(fluent);
00096     krClient.call(uf);
00097   }
00098 
00099     if (response == 1) {
00100     sound_req.arg = "OK, thank you!";
00101     ask_pub.publish(sound_req);
00102     CallGUI thank("thank", CallGUI::DISPLAY,  "OK, thank you!");
00103     thank.run();
00104 
00105     bwi_kr_execution::UpdateFluents uf;
00106     bwi_kr_execution::AspFluent fluent;
00107     
00108     fluent.name = "-ingdc";
00109     
00110     fluent.variables.push_back(person_to_know);
00111 
00112     uf.request.fluents.push_back(fluent);
00113     krClient.call(uf);
00114   }
00115   
00116   
00117   if (response == 0) {
00118 
00119       bool know = false;
00120       int count = 0;
00121 
00122       sound_req.arg = "Great! Please tell me the room number.";
00123       
00124       stringstream question_text;
00125       question_text << "Great! Please tell me the room number." << endl;
00126       question_text << "Examples of rooms are:" << endl; 
00127       question_text << "Peter's office: l3_508" << endl;
00128       question_text << "Matteo's office: l3_418" << endl;
00129       question_text << "Shiqi's office: l3_420" << endl;
00130       question_text << "Jivko's office: l3_432" << endl;
00131       question_text << "Lab: l3_414b" << endl;
00132       
00133       CallGUI *askPerson = new CallGUI("askPerson", CallGUI::TEXT_QUESTION,  question_text.str(), 60.0);
00134       
00135       while ((! know) && (count < 3)) {
00136 
00137         ask_pub.publish(sound_req);
00138         askPerson->run();
00139 
00140         if ((askPerson->getResponseIndex() == -3) && (askPerson->getResponse() != "")) {
00141               bwi_kr_execution::UpdateFluents uf;
00142               bwi_kr_execution::AspFluent fluent;
00143               
00144               fluent.timeStep = 0;
00145               fluent.variables.push_back(person_to_know);
00146               fluent.variables.push_back(askPerson->getResponse());
00147 
00148               fluent.name = "inroom";
00149 
00150               uf.request.fluents.push_back(fluent);
00151 
00152               krClient.call(uf);
00153               know = uf.response.consistent;
00154         }
00155 
00156         count++;
00157 
00158         sound_req.arg = "The room number doesn't exist. Please try again.";
00159         delete askPerson;
00160         askPerson = new CallGUI("askPerson", CallGUI::TEXT_QUESTION,  "The room number doesn't exist. Please try again.", 60.0);
00161       }
00162 
00163       sound_req.arg = "Thank you!";
00164       CallGUI *thank = new CallGUI("thank", CallGUI::DISPLAY,  "Thank you!");
00165 
00166       if (!know) {
00167         sound_req.arg = "OK, thank you anyway!";
00168         delete thank;
00169         thank = new CallGUI("thank", CallGUI::DISPLAY,  "OK, thank you anyway!");
00170       }
00171 
00172       //ask_pub.publish(sound_req);
00173       thank->run();
00174 
00175       bwi_kr_execution::UpdateFluents uf;
00176       bwi_kr_execution::AspFluent fluent;
00177       
00178       fluent.timeStep = 0;
00179       fluent.variables.push_back(person_to_ask);
00180       fluent.variables.push_back(person_to_know);
00181 
00182       fluent.name = (know ? "know" : "-know");
00183 
00184       uf.request.fluents.push_back(fluent);
00185       krClient.call(uf);
00186 
00187   }
00188 
00189   done = true;
00190 
00191 }  
00192   
00193 actasp::Action* AskPerson::cloneAndInit(const actasp::AspFluent& fluent) const {
00194   AskPerson *newAction = new AskPerson();
00195   newAction->person_to_ask = fluent.getParameters().at(0);
00196   newAction->person_to_know = fluent.getParameters().at(1);
00197   
00198   return newAction;
00199 }
00200 
00201 std::vector<std::string> AskPerson::getParameters() const {
00202   vector<string> param;
00203   param.push_back(person_to_ask);
00204   param.push_back(person_to_know);
00205   return param;
00206 }
00207 
00208 
00209 ActionFactory AskPersonFactory(new AskPerson());
00210   
00211 }


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