voice_recognition_client_alg_node.cpp
Go to the documentation of this file.
00001 #include "voice_recognition_client_alg_node.h"
00002 
00003 VoiceRecognitionClientAlgNode::VoiceRecognitionClientAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<VoiceRecognitionClientAlgorithm>(),
00005   hri_client_("hri", true),
00006   voice_recognition_client_("voice_recognition", true)
00007 {
00008   //init class attributes if necessary
00009   //this->loop_rate_ = 2;//in [Hz]
00010 
00011   // [init publishers]
00012   //this->code_publisher_ = this->public_node_handle_.advertise<std_msgs::String>("code", 100);
00013   this->code_publisher_ = this->public_node_handle_.advertise<iri_perception_msgs::voiceRecognitionAnswer>("answerCode", 100);
00014   
00015   // [init subscribers]
00016   
00017   // [init services]
00018   
00019   // [init clients]
00020   
00021   // [init action servers]
00022   
00023   // [init action clients]
00024   this->voice_recognition_action_in_progress = false;
00025   this->voice_recognition_done               = false;
00026   this->hri_action_in_progress               = false;
00027   this->lastStatus_                          = 0;
00028   this->code_                                = 0;
00029   this->lastCode_                            = 0;
00030 
00031   hri_goal_.sequence_file   = std::vector<std::string>(NUM_CLIENTS, std::string(""));
00032   hri_goal_.num_repetitions = std::vector<int>(NUM_CLIENTS, 0);
00033 }
00034 
00035 VoiceRecognitionClientAlgNode::~VoiceRecognitionClientAlgNode(void)
00036 {
00037   // [free dynamic memory]
00038 }
00039 
00040 void VoiceRecognitionClientAlgNode::mainNodeThread(void)
00041 {
00042   //ROS_INFO("VoiceRecognitionClientAlgNode::this->code_: %i", this->code_);
00043   // [fill msg structures]
00044   //this->String_msg_.data = my_var;
00045   
00046   // [fill srv structure and make request to the server]
00047   
00048   // [fill action structure and make request to the action server] 
00049   
00050   // Start voice_recognition action request
00051   bool run = true;
00052   if(run && !this->voice_recognition_action_in_progress && !this->hri_action_in_progress && !this->voice_recognition_done)
00053   {
00054     this->voice_recognition_action_in_progress = true;
00055     voice_recognitionMakeActionRequest();
00056     this->voice_recognition_done = true;    
00057   }
00058   // Start hri action request if voice_recognition action is done
00059   if(this->voice_recognition_done && !this->hri_action_in_progress)
00060   {
00061     if(this->answer_.size()!=0)
00062     {
00063       //ROS_INFO("VoiceRecognitionClientAlgNode::Selected answer is: %s", this->answer_.c_str());
00064       this->hri_action_in_progress = true;
00065       //ROS_INFO("VoiceRecognitionClientAlgNode::Supposed hriMakeActionRequest and succeeded, to continue!");
00066       hriMakeActionRequest();
00067       this->hri_action_in_progress = false;
00068       //this->answer_.clear();
00069     }
00070     this->voice_recognition_done = false;
00071   }
00072 
00073   // [publish messages]
00074   // Publish message with code and answer (if code has changed)
00075   if(this->code_ != this->lastCode_)
00076   {
00077     //std::ostringstream osstream;
00078     //osstream << this->code_;
00079     //std::string aux = osstream.str();  
00080     //this->String_msg_.data = aux;
00081     this->voiceRecognitionAnswer_msg_.code   = this->code_;
00082     this->voiceRecognitionAnswer_msg_.answer = this->answer_;
00083     this->code_publisher_.publish(this->voiceRecognitionAnswer_msg_);
00084     this->lastCode_ = this->code_;
00085     
00086     if(this->answer_.size()!=0)
00087     {
00088       if(this->code_<100) //normal answer
00089       {
00090         sleep(3);
00091       }
00092       else //location answer
00093       {
00094         sleep(4);
00095       }
00096     }
00097       //std::cin.get(); //wait for key
00098     this->answer_.clear();
00099   }
00100 }
00101 
00102 /*  [subscriber callbacks] */
00103 
00104 /*  [service callbacks] */
00105 
00106 /*  [action callbacks] */
00107 void VoiceRecognitionClientAlgNode::hriDone(const actionlib::SimpleClientGoalState& state,  const tibi_dabo_msgs::sequenceResultConstPtr& result) 
00108 { 
00109   if( state.toString().compare("SUCCEEDED") == 0 ) 
00110     ROS_INFO("VoiceRecognitionClientAlgNode::hriDone: Goal Achieved!"); 
00111   else 
00112     ROS_INFO("VoiceRecognitionClientAlgNode::hriDone: %s", state.toString().c_str()); 
00113 
00114   //copy & work with requested result
00115   this->hri_action_in_progress = false;
00116 } 
00117 
00118 void VoiceRecognitionClientAlgNode::hriActive() 
00119 { 
00120   //ROS_INFO("VoiceRecognitionClientAlgNode::hriActive: Goal just went active!"); 
00121 } 
00122 
00123 void VoiceRecognitionClientAlgNode::hriFeedback(const tibi_dabo_msgs::sequenceFeedbackConstPtr& feedback) 
00124 { 
00125   //ROS_INFO("VoiceRecognitionClientAlgNode::hriFeedback: Got Feedback!"); 
00126 
00127   bool feedback_is_ok = true; 
00128 
00129   //analyze feedback 
00130   //my_var = feedback->var; 
00131 
00132   //if feedback is not what expected, cancel requested goal 
00133   if( !feedback_is_ok ) 
00134   { 
00135     hri_client_.cancelGoal(); 
00136     //ROS_INFO("VoiceRecognitionClientAlgNode::hriFeedback: Cancelling Action!"); 
00137   } 
00138 }
00139 
00140 void VoiceRecognitionClientAlgNode::voice_recognitionDone(const actionlib::SimpleClientGoalState& state,  const iri_perception_msgs::voiceRecognitionResultConstPtr& result) 
00141 { 
00142   if( state.toString().compare("SUCCEEDED") == 0 ) 
00143     ROS_INFO("VoiceRecognitionClientAlgNode::voice_recognitionDone: Goal Achieved!"); 
00144   else 
00145     ROS_INFO("VoiceRecognitionClientAlgNode::voice_recognitionDone: %s", state.toString().c_str()); 
00146 
00147   //copy & work with requested result
00148   this->answer_ = result->answer;
00149   this->code_   = result->code;
00150   ROS_INFO("VoiceRecognitionClientAlgNode:: result, code: %s, %i", this->answer_.c_str(), this->code_);
00151   this->voice_recognition_action_in_progress = false;
00152   this->voice_recognition_done = true;
00153 } 
00154 
00155 void VoiceRecognitionClientAlgNode::voice_recognitionActive() 
00156 { 
00157   //ROS_INFO("VoiceRecognitionClientAlgNode::voice_recognitionActive: Goal just went active!"); 
00158 } 
00159 
00160 void VoiceRecognitionClientAlgNode::voice_recognitionFeedback(const iri_perception_msgs::voiceRecognitionFeedbackConstPtr& feedback) 
00161 { 
00162   //ROS_INFO("VoiceRecognitionClientAlgNode::voice_recognitionFeedback: Got Feedback!"); 
00163 
00164   bool feedback_is_ok = true; 
00165 
00166   //analyze feedback 
00167   //my_var = feedback->var;
00168   if(feedback->status != this->lastStatus_)
00169   {
00170     this->code_ = feedback->status;
00171     ROS_INFO("VoiceRecognitionClientAlgNode:: New status: %i", feedback->status);
00172     this->lastStatus_ = feedback->status;
00173     
00174   }
00175 
00176   //if feedback is not what expected, cancel requested goal 
00177   if( !feedback_is_ok ) 
00178   { 
00179     voice_recognition_client_.cancelGoal(); 
00180     //ROS_INFO("VoiceRecognitionClientAlgNode::voice_recognitionFeedback: Cancelling Action!"); 
00181   } 
00182 }
00183 
00184 /*  [action requests] */
00185 void VoiceRecognitionClientAlgNode::hriMakeActionRequest() 
00186 { 
00187   ROS_INFO("VoiceRecognitionClientAlgNode::hriMakeActionRequest: Starting New Request!"); 
00188 
00189   //wait for the action server to start 
00190   //will wait for infinite time 
00191   hri_client_.waitForServer();  
00192   ROS_INFO("VoiceRecognitionClientAlgNode::hriMakeActionRequest: Server is Available!"); 
00193 
00194   //send a goal to the action 
00195   //hri_goal_.data = my_desired_goal;
00196   hri_goal_.sequence_file[TTS_] = this->answer_;
00197   
00198   hri_client_.sendGoal(hri_goal_, 
00199               boost::bind(&VoiceRecognitionClientAlgNode::hriDone,     this, _1, _2), 
00200               boost::bind(&VoiceRecognitionClientAlgNode::hriActive,   this), 
00201               boost::bind(&VoiceRecognitionClientAlgNode::hriFeedback, this, _1)); 
00202   ROS_INFO("VoiceRecognitionClientAlgNode::hriMakeActionRequest: Goal Sent. Wait for Result!");
00203   this->hri_action_in_progress = true;
00204 }
00205 
00206 void VoiceRecognitionClientAlgNode::voice_recognitionMakeActionRequest() 
00207 { 
00208   ROS_INFO("VoiceRecognitionClientAlgNode::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("VoiceRecognitionClientAlgNode::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(&VoiceRecognitionClientAlgNode::voice_recognitionDone,     this, _1, _2), 
00219               boost::bind(&VoiceRecognitionClientAlgNode::voice_recognitionActive,   this), 
00220               boost::bind(&VoiceRecognitionClientAlgNode::voice_recognitionFeedback, this, _1)); 
00221   ROS_INFO("VoiceRecognitionClientAlgNode::voice_recognitionMakeActionRequest: Goal Sent. Wait for Result!");
00222   this->voice_recognition_action_in_progress = true;  
00223 }
00224 
00225 void VoiceRecognitionClientAlgNode::node_config_update(Config &config, uint32_t level)
00226 {
00227   this->alg_.lock();
00228 
00229   this->alg_.unlock();
00230 }
00231 
00232 void VoiceRecognitionClientAlgNode::addNodeDiagnostics(void)
00233 {
00234 }
00235 
00236 /* main function */
00237 int main(int argc,char *argv[])
00238 {
00239   return algorithm_base::main<VoiceRecognitionClientAlgNode>(argc, argv, "voice_recognition_client_alg_node");
00240 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends


iri_voice_recognition_client
Author(s): fherrero
autogenerated on Mon Jan 14 2013 13:13:33