tibi_dabo_questions_alg_node.cpp
Go to the documentation of this file.
00001 #include "tibi_dabo_questions_alg_node.h"
00002 
00003 TibiDaboQuestionsAlgNode::TibiDaboQuestionsAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<TibiDaboQuestionsAlgorithm>(),
00005   voice_recognition_client_("voice_recognition", true),
00006   hri_client_("hri", true)
00007 {
00008   //init class attributes if necessary
00009   this->loop_rate_ = 10;//in [Hz]
00010   this->answer_="";
00011   this->status_=-1;
00012   this->lastStatus_=-1;
00013   
00014   this->questionsOn_ = true;
00015   this->hriOn_       = true;
00016   this->infoOn_      = true;
00017   
00018   this->hri_active_       = false;
00019   this->questions_active_ = false;
00020   
00021   hri_goal_.sequence_file   = std::vector<std::string>(NUM_CLIENTS, std::string(""));
00022   hri_goal_.num_repetitions = std::vector<int>(NUM_CLIENTS, 0);
00023   
00024   this->public_node_handle_.getParam("path", this->alg_.path_);
00025   this->alg_.initialize();
00026 
00027   // [init publishers]
00028   
00029   // [init subscribers]
00030   
00031   // [init services]
00032   
00033   // [init clients]
00034   
00035   // [init action servers]
00036   
00037   // [init action clients]
00038 }
00039 
00040 TibiDaboQuestionsAlgNode::~TibiDaboQuestionsAlgNode(void)
00041 {
00042   // [free dynamic memory]
00043 }
00044 
00045 void TibiDaboQuestionsAlgNode::mainNodeThread(void)
00046 {
00047 
00048   if(this->questionsOn_)
00049   {
00050     this->alg_.lock();
00051 
00052     // Questions
00053     if(!this->questions_active_ && (this->answer_.size()==0))
00054     {
00055       //this->questionsOn_=false;
00056       ROS_INFO("TibiDaboQuestionsAlgNode::mainNodeThread: questions Request!");
00057       voice_recognitionMakeActionRequest();
00058     }
00059 
00060     // HRI
00061     if(this->hriOn_ && !this->hri_active_ && (this->answer_.size()!=0) && this->alg_.showingAnswer_)
00062     {
00063       ROS_INFO("TibiDaboQuestionsAlgNode::mainNodeThread: hri Request!");
00064       hriMakeActionRequest();
00065     }
00066 
00067     // Info
00068     if(this->infoOn_)
00069     {
00070       this->alg_.iteration(this->status_, this->answer_);
00071     }
00072     else
00073     {
00074       this->alg_.iteration(-1, ""); //irilogo
00075     }
00076 
00077     if(!this->hriOn_ && (this->answer_.size()!=0) && this->alg_.showingAnswer_)
00078     {
00079       this->alg_.showingAnswer_ = false;
00080       this->answer_.clear();
00081       ROS_INFO("TibiDaboQuestionsAlgNode::mainNodeThread: sleeping 3 seconds");
00082       sleep(3);
00083     }
00084 
00085     this->alg_.unlock();
00086   }
00087   else
00088   {
00089     this->alg_.iteration(-1, ""); //irilogo
00090     if(this->questions_active_)
00091     {
00092       voice_recognition_client_.cancelGoal();
00093     }
00094   }
00095 
00096   if(!hriOn_ && this->hri_active_)
00097   {
00098     hri_client_.cancelGoal();
00099   }
00100 
00101   // [fill msg structures]
00102   
00103   // [fill srv structure and make request to the server]
00104   
00105   // [fill action structure and make request to the action server]
00106   
00107   
00108 
00109   // [publish messages]
00110 }
00111 
00112 /*  [subscriber callbacks] */
00113 
00114 /*  [service callbacks] */
00115 
00116 /*  [action callbacks] */
00117 void TibiDaboQuestionsAlgNode::voice_recognitionDone(const actionlib::SimpleClientGoalState& state,  const iri_perception_msgs::voiceRecognitionResultConstPtr& result) 
00118 { 
00119   if( state.toString().compare("SUCCEEDED") == 0 ) 
00120     ROS_INFO("TibiDaboQuestionsAlgNode::voice_recognitionDone: Goal Achieved!"); 
00121   else 
00122     ROS_INFO("TibiDaboQuestionsAlgNode::voice_recognitionDone: %s", state.toString().c_str()); 
00123 
00124   //copy & work with requested result
00125     if(questionsOn_)
00126     {
00127       this->alg_.lock();
00128       this->answer_ = result->answer;
00129       this->status_ = result->code;
00130       ROS_INFO("TibiDaboQuestionsAlgNode::voice_recognitionDone: Result, code: \"%s\", %i", this->answer_.c_str(), this->status_);
00131       this->alg_.unlock();
00132     }
00133     this->questions_active_ = false;
00134 }
00135 
00136 void TibiDaboQuestionsAlgNode::voice_recognitionActive() 
00137 { 
00138   //ROS_INFO("TibiDaboQuestionsAlgNode::voice_recognitionActive: Goal just went active!"); 
00139 } 
00140 
00141 void TibiDaboQuestionsAlgNode::voice_recognitionFeedback(const iri_perception_msgs::voiceRecognitionFeedbackConstPtr& feedback) 
00142 { 
00143   //ROS_INFO("TibiDaboQuestionsAlgNode::voice_recognitionFeedback: Got Feedback!"); 
00144 
00145   bool feedback_is_ok = true; 
00146 
00147   //analyze feedback 
00148   //my_var = feedback->var;
00149   if(feedback->status != this->lastStatus_)
00150   {
00151     this->alg_.lock();
00152     ROS_INFO("TibiDaboQuestionsAlgNode::voice_recognitionFeedback: new feedback->status: %i", feedback->status); 
00153     this->status_ = feedback->status;
00154     this->lastStatus_ = this->status_;
00155     this->alg_.unlock();
00156   }
00157 
00158   //if feedback is not what expected, cancel requested goal 
00159   if( !feedback_is_ok ) 
00160   { 
00161     voice_recognition_client_.cancelGoal(); 
00162   } 
00163 }
00164 
00165 void TibiDaboQuestionsAlgNode::hriDone(const actionlib::SimpleClientGoalState& state,  const tibi_dabo_msgs::sequenceResultConstPtr& result) 
00166 { 
00167   if( state.toString().compare("SUCCEEDED") == 0 ) 
00168     ROS_INFO("TibiDaboQuestionsAlgNode::hriDone: Goal Achieved!"); 
00169   else 
00170     ROS_INFO("TibiDaboQuestionsAlgNode::hriDone: %s", state.toString().c_str()); 
00171 
00172   //copy & work with requested result
00173   this->alg_.lock();
00174   this->hri_active_ = false;
00175   this->alg_.showingAnswer_ = false;
00176   this->answer_.clear();
00177   ROS_INFO("TibiDaboQuestionsAlgNode::hriDone: sleeping 3 seconds");
00178   sleep(3);
00179   this->alg_.unlock();
00180 } 
00181 
00182 void TibiDaboQuestionsAlgNode::hriActive() 
00183 { 
00184   //ROS_INFO("TibiDaboQuestionsAlgNode::hriActive: Goal just went active!"); 
00185 } 
00186 
00187 void TibiDaboQuestionsAlgNode::hriFeedback(const tibi_dabo_msgs::sequenceFeedbackConstPtr& feedback) 
00188 { 
00189   //ROS_INFO("TibiDaboQuestionsAlgNode::hriFeedback: Got Feedback!"); 
00190 
00191   bool feedback_is_ok = true; 
00192 
00193   //analyze feedback 
00194   //my_var = feedback->var; 
00195 
00196   //if feedback is not what expected, cancel requested goal 
00197   if( !feedback_is_ok || !hriOn_ || !questionsOn_ ) 
00198   { 
00199     hri_client_.cancelGoal();
00200     this->hri_active_=false;
00201     //ROS_INFO("TibiDaboQuestionsAlgNode::hriFeedback: Cancelling Action!"); 
00202   } 
00203 }
00204 
00205 /*  [action requests] */
00206 void TibiDaboQuestionsAlgNode::voice_recognitionMakeActionRequest() 
00207 { 
00208   ROS_INFO("TibiDaboQuestionsAlgNode::voice_recognitionMakeActionRequest: Starting New Request!"); 
00209 
00210   //wait for the action server to start 
00211   //will wait for infinite time 
00212   voice_recognition_client_.waitForServer();  
00213   ROS_INFO("TibiDaboQuestionsAlgNode::voice_recognitionMakeActionRequest: Server is Available!"); 
00214 
00215   //send a goal to the action 
00216   //voice_recognition_goal_.data = my_desired_goal; 
00217   voice_recognition_client_.sendGoal(voice_recognition_goal_, 
00218               boost::bind(&TibiDaboQuestionsAlgNode::voice_recognitionDone,     this, _1, _2), 
00219               boost::bind(&TibiDaboQuestionsAlgNode::voice_recognitionActive,   this), 
00220               boost::bind(&TibiDaboQuestionsAlgNode::voice_recognitionFeedback, this, _1)); 
00221   ROS_INFO("TibiDaboQuestionsAlgNode::voice_recognitionMakeActionRequest: Goal Sent. Wait for Result!");
00222   this->questions_active_ = true;
00223 }
00224 
00225 void TibiDaboQuestionsAlgNode::hriMakeActionRequest() 
00226 { 
00227   ROS_INFO("TibiDaboQuestionsAlgNode::hriMakeActionRequest: Starting New Request!"); 
00228 
00229   //wait for the action server to start 
00230   //will wait for infinite time 
00231   hri_client_.waitForServer();  
00232   ROS_INFO("TibiDaboQuestionsAlgNode::hriMakeActionRequest: Server is Available!"); 
00233 
00234   //send a goal to the action 
00235   //hri_goal_.data = my_desired_goal;
00236   hri_goal_.sequence_file[TTS_] = this->answer_;
00237   hri_client_.sendGoal(hri_goal_, 
00238               boost::bind(&TibiDaboQuestionsAlgNode::hriDone,     this, _1, _2), 
00239               boost::bind(&TibiDaboQuestionsAlgNode::hriActive,   this), 
00240               boost::bind(&TibiDaboQuestionsAlgNode::hriFeedback, this, _1)); 
00241   ROS_INFO("TibiDaboQuestionsAlgNode::hriMakeActionRequest: Goal Sent. Wait for Result!");
00242   ROS_INFO("TibiDaboQuestionsAlgNode::hriMakeActionRequest: Goal Sent. Answer: \"%s\"", hri_goal_.sequence_file[TTS_].c_str());
00243   this->hri_active_=true;
00244 }
00245 
00246 void TibiDaboQuestionsAlgNode::node_config_update(Config &config, uint32_t level)
00247 {
00248   this->alg_.lock();
00249   this->questionsOn_ = config.recognition;
00250   this->hriOn_       = config.hri;
00251   this->infoOn_      = config.info;
00252   this->alg_.config_ = config;
00253   this->alg_.unlock();
00254 }
00255 
00256 void TibiDaboQuestionsAlgNode::addNodeDiagnostics(void)
00257 {
00258 }
00259 
00260 /* main function */
00261 int main(int argc,char *argv[])
00262 {
00263   return algorithm_base::main<TibiDaboQuestionsAlgNode>(argc, argv, "tibi_dabo_questions_alg_node");
00264 }


tibi_dabo_questions
Author(s): fherrero
autogenerated on Fri Dec 6 2013 21:16:32