SearchRoom.cpp
Go to the documentation of this file.
00001 #include "SearchRoom.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/CurrentStateQuery.h"
00010 #include <bwi_kr_execution/UpdateFluents.h>
00011 
00012 #include <ros/ros.h>
00013 #include <sound_play/sound_play.h>
00014 
00015 #include <string>
00016 #include <iostream>
00017 
00018 using namespace std;
00019 
00020 namespace bwi_krexec {
00021 
00022 SearchRoom::SearchRoom() : 
00023             person(),
00024             room(),
00025             done(false),
00026             failed(false),
00027             waiting_speach(false),
00028             wait_over(false){
00029             }
00030 
00031 ros::Publisher SearchRoom::ask_pub;
00032 bool SearchRoom::pub_set(false);
00033   
00034 void SearchRoom::run() {
00035 
00036   ros::NodeHandle n;
00037   if (!pub_set) { 
00038     ask_pub = n.advertise<sound_play::SoundRequest>("robotsound", 1000);
00039     pub_set = true;
00040   }
00041 
00042   //current state query
00043   ros::ServiceClient currentClient = n.serviceClient<bwi_kr_execution::CurrentStateQuery> ( "current_state_query" );
00044   currentClient.waitForExistence();
00045 
00046   bwi_kr_execution::AspFluent atFluent;
00047   atFluent.name = "at";
00048   atFluent.timeStep = 0;
00049   atFluent.variables.push_back(room);
00050   
00051   bwi_kr_execution::AspRule rule;
00052   rule.head.push_back(atFluent);
00053   
00054   bwi_kr_execution::CurrentStateQuery csq;
00055   csq.request.query.push_back(rule);
00056   
00057   currentClient.call(csq);
00058   
00059   bool at = csq.response.answer.satisfied;
00060 
00061   if (at) {
00062 
00063     if (!wait_over && !waiting_speach && ask_pub.getNumSubscribers() == 0 ) {
00064       waiting_speach = true;
00065       starting_wating = ros::Time::now();
00066       return;
00067     }
00068     else if (!wait_over&& waiting_speach && 
00069       (ask_pub.getNumSubscribers() != 0 || (ros::Time::now() - starting_wating).toSec() > 1.)) {
00070       waiting_speach = false;
00071       wait_over = true;
00072     }
00073       
00074 
00075     //speak
00076     sound_play::SoundRequest sound_req;
00077     sound_req.sound = sound_play::SoundRequest::SAY;
00078     sound_req.command = sound_play::SoundRequest::PLAY_ONCE;
00079     std::stringstream ss;
00080     ss << "Is " << person << " in the room?";
00081     sound_req.arg = ss.str();
00082     
00083     ask_pub.publish(sound_req);
00084   }
00085 
00086   vector<string> options;
00087   options.push_back("yes");
00088   options.push_back("no");
00089 
00090   CallGUI searchRoom("searchRoom", CallGUI::CHOICE_QUESTION,  "Is " + person + " in the room " + room + "?", 60.0, options);
00091   searchRoom.run();
00092 
00093   int response = searchRoom.getResponseIndex();
00094 
00095   if (response >= 0) {
00096     CallGUI thank("thank", CallGUI::DISPLAY,  "Thank you!");
00097     thank.run();
00098     /*if (at) {
00099       sound_play::SoundRequest sound_req;
00100       sound_req.sound = sound_play::SoundRequest::SAY;
00101       sound_req.command = sound_play::SoundRequest::PLAY_ONCE;
00102       sound_req.arg = "Thank you !";
00103       ask_pub.publish(sound_req);
00104     }*/
00105   }else
00106     failed = true;
00107 
00108   ros::ServiceClient krClient = n.serviceClient<bwi_kr_execution::UpdateFluents> ( "update_fluents" );
00109   krClient.waitForExistence();
00110 
00111   bwi_kr_execution::UpdateFluents uf;
00112   bwi_kr_execution::AspFluent fluent;
00113   fluent.timeStep = 0;
00114   fluent.variables.push_back(person);
00115   fluent.variables.push_back(room);
00116 
00117   fluent.name = ((response == 0) ? "inroom" : "-inroom");
00118 
00119   uf.request.fluents.push_back(fluent);
00120   krClient.call(uf);
00121 
00122   done = true;
00123 
00124 }  
00125   
00126 actasp::Action* SearchRoom::cloneAndInit(const actasp::AspFluent& fluent) const {
00127   SearchRoom *newAction = new SearchRoom();
00128   newAction->person = fluent.getParameters().at(0);
00129   newAction->room = fluent.getParameters().at(1);
00130   
00131   return newAction;
00132 }
00133 
00134 std::vector<std::string> SearchRoom::getParameters() const {
00135   vector<string> param;
00136   param.push_back(person);
00137   param.push_back(room);
00138   return param;
00139 }
00140 
00141 
00142 ActionFactory SearchRoomFactory(new SearchRoom());
00143   
00144 }


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