tibi_dabo_conversation_alg_node.cpp
Go to the documentation of this file.
00001 #include "tibi_dabo_conversation_alg_node.h"
00002 #include <wiimote/State.h>
00003 
00004 TibiDaboConversationAlgNode::TibiDaboConversationAlgNode(void) :
00005   algorithm_base::IriBaseAlgorithm<TibiDaboConversationAlgorithm>(),
00006   hri_client_("hri", true),
00007   robot(""),
00008   sequences(NUM_CLIENTS, std::string("")),
00009   hri_action_in_progress_(false)
00010 {
00011   //init class attributes if necessary
00012   //this->loop_rate_ = 2;//in [Hz]
00013   hri_goal_.sequence_file   = std::vector<std::string>(NUM_CLIENTS, std::string(""));
00014   hri_goal_.num_repetitions = std::vector<int>(NUM_CLIENTS, 0);
00015 
00016   public_node_handle_.getParam("robot", this->robot);
00017 
00018   this->loadSentences();
00019   this->index=-1;
00020 
00021   // [init publishers]
00022 
00023   // [init subscribers]
00024   this->joy_subscriber_ = this->public_node_handle_.subscribe("joy", 100, &TibiDaboConversationAlgNode::joy_callback, this);
00025 
00026   // [init services]
00027   this->conversation_server_ = this->public_node_handle_.advertiseService("conversation_server", &TibiDaboConversationAlgNode::conversationCallback, this);
00028 
00029   // [init clients]
00030   conversation_client_ = this->public_node_handle_.serviceClient<tibi_dabo_conversation::conversation>("conversation_client");
00031 
00032   // [init action servers]
00033 
00034   // [init action clients]
00035 }
00036 
00037 TibiDaboConversationAlgNode::~TibiDaboConversationAlgNode(void)
00038 {
00039   // [free dynamic memory]
00040 }
00041 
00042 void TibiDaboConversationAlgNode::mainNodeThread(void)
00043 {
00044   // [fill msg structures]
00045 
00046   // [fill srv structure and make request to the server]
00047 /*  if(this->hri_action_in_progress_)
00048   {
00049     //When hri succeed, call service so the other robot responds //TODO
00050     this->conversation_srv_.request.type  = this->buttonIndex;
00051     this->index++;
00052     this->conversation_srv_.request.index = this->index;
00053     ROS_INFO("TibiDaboConversationAlgNode:: Sending New Service Request!");
00054     if (conversation_client_.call(conversation_srv_))
00055     {
00056       ROS_INFO("TibiDaboConversationAlgNode:: Conversation Service called");
00057     }
00058     else
00059     {
00060       ROS_INFO("TibiDaboConversationAlgNode:: Failed to Call Conversation Server");
00061     }
00062     this->hri_action_in_progress_=false; //TODO
00063   }
00064 */
00065   /*
00066   conversation_srv_.request.type = 0;
00067   conversation_srv_.request.index = 0;
00068   ROS_INFO("TibiDaboConversationAlgNode:: Sending New Request!");
00069   if (conversation_client_.call(conversation_srv_))
00070   {
00071     //ROS_INFO("TibiDaboConversationAlgNode:: Response: %s", conversation_srv_.response.result);
00072   }
00073   else
00074   {
00075     //ROS_INFO("TibiDaboConversationAlgNode:: Failed to Call Server on topic conversation ");
00076   }
00077   */
00078 
00079   // [fill action structure and make request to the action server]
00080   //hriMakeActionRequest(sequences);
00081 
00082   // [publish messages]
00083 }
00084 
00085 /*  [subscriber callbacks] */
00086 void TibiDaboConversationAlgNode::joy_callback(const sensor_msgs::Joy::ConstPtr& joy_msg)
00087 {
00088   //ROS_INFO("TibiDaboConversationAlgNode::joy_callback: New Message Received");
00089 
00090   //use appropiate mutex to shared variables if necessary
00091   this->alg_.lock();
00092   //this->joy_mutex_.enter();
00093 
00094   //std::cout << msg->data << std::endl;
00095   bool execute_action=false;
00096   static bool first=true;
00097 
00098   if(first)
00099   {
00100     this->prev_buttons_.resize(joy_msg->buttons.size());
00101     first=false;
00102   }
00103 
00104   for(unsigned int i=0; i<joy_msg->buttons.size(); i++)
00105   {
00106     if(joy_msg->buttons[i]==1 && this->prev_buttons_[i]==0)
00107     {
00108       this->buttonIndex= this->useExtraButton(i);
00109       if(this->buttonIndex!=-1)
00110       {
00111         ROS_INFO("TibiDaboConversationAlgNode::joy_callback: Received Button %i", this->buttonIndex);
00112         execute_action=true;
00113         this->index = 0; //initialize conversation
00114       }
00115     }
00116   }
00117 
00118   if(this->hri_action_in_progress_ && execute_action)
00119   {
00120     ROS_INFO("TibiDaboHriTeleopAlgNode::joy_callback: hri_client_.cangelGoal()!");
00121     this->hri_client_.cancelGoal();
00122   }
00123   // Request Action
00124   if(!this->hri_action_in_progress_ && execute_action && unsigned(this->buttonIndex)<this->sentences.size() && unsigned(this->index)<this->sentences[this->buttonIndex].size())
00125   {
00126     this->sequences[TTS_]       = this->sentences[this->buttonIndex][this->index];
00127     this->sequences[LEDS_]      = "";
00128     this->sequences[HEAD_]      = "";
00129     this->sequences[RIGHT_ARM_] = "";
00130     this->sequences[LEFT_ARM_]  = "";
00131     this->hriMakeActionRequest(this->sequences);
00132     //hriMakeActionRequest();
00133   }
00134   this->prev_buttons_ = joy_msg->buttons;
00135 
00136   //unlock previously blocked shared variables
00137   this->alg_.unlock();
00138   //this->joy_mutex_.exit();
00139 }
00140 
00141 /*  [service callbacks] */
00142 bool TibiDaboConversationAlgNode::conversationCallback(tibi_dabo_conversation::conversation::Request &req, tibi_dabo_conversation::conversation::Response &res)
00143 {
00144   //ROS_INFO("TibiDaboConversationAlgNode::conversationCallback: New Request Received!");
00145 
00146   //use appropiate mutex to shared variables if necessary
00147   this->alg_.lock();
00148   //this->conversation_mutex_.enter();
00149 
00150   //ROS_INFO("TibiDaboConversationAlgNode::conversationCallback: Processin New Request!");
00151   //do operations with req and output on res
00152   //res.data2 = req.data1 + my_var;
00153   this->buttonIndex = req.type;
00154   this->index       = req.index;
00155   if(!this->hri_action_in_progress_ && unsigned(this->buttonIndex)<this->sentences.size() && unsigned(this->index)<this->sentences[this->buttonIndex].size())
00156   {
00157 
00158     //ROS_INFO("TibiDaboConversationAlgNode::conversationCallback: conversation continued!");
00159 
00160     this->sequences[TTS_]       = sentences[this->buttonIndex][this->index];
00161     this->sequences[LEDS_]      = "";
00162     this->sequences[HEAD_]      = "";
00163     this->sequences[RIGHT_ARM_] = "";
00164     this->sequences[LEFT_ARM_]  = "";
00165     this->hriMakeActionRequest(this->sequences);
00166     //hriMakeActionRequest();
00167 
00168   }
00169   else
00170   {
00171     //ROS_INFO("TibiDaboConversationAlgNode::conversationCallback: conversation NOT continued or finished (hri busy/end of sentneces!");
00172     this->buttonIndex = -1;
00173     this->index       = -1;
00174   }
00175 
00176   //unlock previously blocked shared variables
00177   this->alg_.unlock();
00178   //this->conversation_mutex_.exit();
00179 
00180   return true;
00181 }
00182 
00183 /*  [action callbacks] */
00184 void TibiDaboConversationAlgNode::hriDone(const actionlib::SimpleClientGoalState& state,  const tibi_dabo_msgs::sequenceResultConstPtr& result)
00185 {
00186   this->alg_.lock();
00187   if( state.toString().compare("SUCCEEDED") == 0 )
00188   {
00189     ROS_INFO("TibiDaboConversationAlgNode::hriDone: Goal Achieved!");
00190 
00191     //When hri succeed, call service so the other robot responds
00192     this->conversation_srv_.request.type  = this->buttonIndex;
00193     this->index++;
00194     this->conversation_srv_.request.index = this->index;
00195     //ROS_INFO("TibiDaboConversationAlgNode:: Sending New Service Request!");
00196     if (conversation_client_.call(conversation_srv_))
00197     {
00198       //ROS_INFO("TibiDaboConversationAlgNode:: Conversation Service called");
00199     }
00200     else
00201     {
00202       ROS_INFO("TibiDaboConversationAlgNode:: Failed to Call Conversation Server");
00203     }
00204 
00205   }
00206   else
00207     ROS_INFO("TibiDaboConversationAlgNode::hriDone: %s", state.toString().c_str());
00208 
00209   this->hri_action_in_progress_=false;
00210   //copy & work with requested result
00211   this->alg_.unlock();
00212 }
00213 
00214 void TibiDaboConversationAlgNode::hriActive()
00215 {
00216   //ROS_INFO("TibiDaboConversationAlgNode::hriActive: Goal just went active!");
00217 }
00218 
00219 void TibiDaboConversationAlgNode::hriFeedback(const tibi_dabo_msgs::sequenceFeedbackConstPtr& feedback)
00220 {
00221   //ROS_INFO("TibiDaboConversationAlgNode::hriFeedback: Got Feedback!");
00222 
00223   bool feedback_is_ok = true;
00224 
00225   //analyze feedback
00226   //my_var = feedback->var;
00227 
00228   //if feedback is not what expected, cancel requested goal
00229   if( !feedback_is_ok )
00230   {
00231     hri_client_.cancelGoal();
00232     //ROS_INFO("TibiDaboConversationAlgNode::hriFeedback: Cancelling Action!");
00233   }
00234 }
00235 
00236 /*  [action requests] */
00237 void TibiDaboConversationAlgNode::hriMakeActionRequest(std::vector<std::string> &sequences)
00238 {
00239   ROS_INFO("TibiDaboConversationAlgNode::hriMakeActionRequest: Starting New Request!");
00240 
00241   //wait for the action server to start
00242   //will wait for infinite time
00243   hri_client_.waitForServer(); //TODO
00244   //ROS_INFO("TibiDaboConversationAlgNode::hriMakeActionRequest: Server is Available!");
00245 
00246   //set goal
00247   hri_goal_.sequence_file[TTS_]       = this->sequences[TTS_];
00248   hri_goal_.sequence_file[LEDS_]      = this->sequences[LEDS_];
00249   hri_goal_.sequence_file[HEAD_]      = this->sequences[HEAD_];
00250   hri_goal_.sequence_file[RIGHT_ARM_] = this->sequences[RIGHT_ARM_];
00251   hri_goal_.sequence_file[LEFT_ARM_]  = this->sequences[LEFT_ARM_];
00252 
00253   std::string goal_str("goal : {");
00254   for(unsigned int ii=0; ii<hri_goal_.sequence_file.size(); ii++)
00255   {
00256     goal_str += hri_goal_.sequence_file[ii] + ", ";
00257   }
00258   ROS_INFO("TibiDaboConversationAlgNode::hriMakeActionRequest: %s}", goal_str.c_str());
00259 
00260   //send a goal to the action
00261   //hri_goal_.data = my_desired_goal;
00262   hri_client_.sendGoal(hri_goal_,
00263               boost::bind(&TibiDaboConversationAlgNode::hriDone,     this, _1, _2),
00264               boost::bind(&TibiDaboConversationAlgNode::hriActive,   this),
00265               boost::bind(&TibiDaboConversationAlgNode::hriFeedback, this, _1));
00266   //ROS_INFO("TibiDaboConversationAlgNode::hriMakeActionRequest: Goal Sent. Wait for Result!");
00267   this->hri_action_in_progress_ = true;
00268 }
00269 
00270 void TibiDaboConversationAlgNode::node_config_update(Config &config, uint32_t level)
00271 {
00272   this->alg_.lock();
00273 
00274   this->alg_.unlock();
00275 }
00276 
00277 void TibiDaboConversationAlgNode::addNodeDiagnostics(void)
00278 {
00279 }
00280 
00281 /* main function */
00282 int main(int argc,char *argv[])
00283 {
00284   return algorithm_base::main<TibiDaboConversationAlgNode>(argc, argv, "tibi_dabo_conversation_alg_node");
00285 }
00286 
00287 int TibiDaboConversationAlgNode::useExtraButton(const unsigned int & index)
00288 {
00289   ROS_DEBUG("Tibi Dabo wiimote use extra Button");
00290 
00291   switch(index)
00292   {
00293     case wiimote::State::MSG_BTN_1:
00294       ROS_DEBUG("Button 1!");
00295       return 0;
00296       break;
00297 
00298     case wiimote::State::MSG_BTN_2:
00299       ROS_DEBUG("Button 2!");
00300       return 1;
00301       break;
00302 
00303     case wiimote::State::MSG_BTN_B:
00304       ROS_DEBUG("Button B!");
00305       return 2;
00306       break;
00307 
00308     case wiimote::State::MSG_BTN_PLUS:
00309       ROS_DEBUG("Button (+)!");
00310       return 3;
00311       break;
00312 
00313     case wiimote::State::MSG_BTN_MINUS:
00314       ROS_DEBUG("Button (-)!");
00315       return 4;
00316       break;
00317 
00318     case wiimote::State::MSG_BTN_HOME:
00319       ROS_DEBUG("Button Home!");
00320       return 5;
00321       break;
00322 
00323     default:
00324       ROS_DEBUG("Button UNDEFINED!");
00325       return -1;
00326       break;
00327   }
00328 
00329 }
00330 
00331 void TibiDaboConversationAlgNode::loadSentences()
00332 {
00333   // Button 1
00334   std::vector<std::string> sentencesList;
00335   sentencesList.push_back("Hola!");
00336   sentencesList.push_back("Ey, hola!");
00337   sentencesList.push_back("Què tal?");
00338   sentencesList.push_back("Be, i tu?");
00339   sentencesList.push_back("Molt be també!");
00340   sentencesList.push_back("Me n'alegro");
00341   this->sentences.push_back(sentencesList);
00342 
00343   //Button 2
00344   sentencesList.clear();
00345   sentencesList.push_back("Per cert");
00346   sentencesList.push_back("Diguem");
00347   sentencesList.push_back("Saps que?");
00348   sentencesList.push_back("Que");
00349   sentencesList.push_back("Mec!");
00350   sentencesList.push_back("Interesant...");
00351   sentencesList.push_back("\\item=laugh");
00352   this->sentences.push_back(sentencesList);
00353 
00354   // Button B
00355   sentencesList.clear();
00356   sentencesList.push_back("Com et dius?");
00357   sentencesList.push_back("Jo? Em dic "+this->robot+", i tu?");
00358   sentencesList.push_back("Jo soc "+this->robot+"-------------------------------------");
00359   sentencesList.push_back("Un plaer --------------------------------------");
00360   sentencesList.push_back("Igualment -------------------------------------");
00361   this->sentences.push_back(sentencesList);
00362 
00363   // Button +
00364   sentencesList.clear();
00365   sentencesList.push_back("Som -----------------------");
00366   sentencesList.push_back(this->robot+ "-----------------------------------------------");
00367   sentencesList.push_back("i "+ this->robot + " ---------------------------------------");
00368   sentencesList.push_back("Una parella de robots -----------------------");
00369   sentencesList.push_back("Socials i urbans -----------------------");
00370   this->sentences.push_back(sentencesList);
00371 
00372   // Button -
00373   sentencesList.clear();
00374   sentencesList.push_back("Bé, me n'he d'anar -----------------------");
00375   sentencesList.push_back("Val, fins un altr ---------------------------");
00376   sentencesList.push_back("Adeu! ---------------------------------------");
00377   this->sentences.push_back(sentencesList);
00378 
00379   // Button Home
00380   sentencesList.clear();
00381   sentencesList.push_back("\\item=throat ---------------------------");
00382   sentencesList.push_back("\\item=throat, \\item=throat-------------");
00383   this->sentences.push_back(sentencesList);
00384 }


tibi_dabo_conversation
Author(s): fherrero
autogenerated on Fri Dec 6 2013 21:50:17