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
00012
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
00022
00023
00024 this->joy_subscriber_ = this->public_node_handle_.subscribe("joy", 100, &TibiDaboConversationAlgNode::joy_callback, this);
00025
00026
00027 this->conversation_server_ = this->public_node_handle_.advertiseService("conversation_server", &TibiDaboConversationAlgNode::conversationCallback, this);
00028
00029
00030 conversation_client_ = this->public_node_handle_.serviceClient<tibi_dabo_conversation::conversation>("conversation_client");
00031
00032
00033
00034
00035 }
00036
00037 TibiDaboConversationAlgNode::~TibiDaboConversationAlgNode(void)
00038 {
00039
00040 }
00041
00042 void TibiDaboConversationAlgNode::mainNodeThread(void)
00043 {
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 }
00084
00085
00086 void TibiDaboConversationAlgNode::joy_callback(const sensor_msgs::Joy::ConstPtr& joy_msg)
00087 {
00088
00089
00090
00091 this->alg_.lock();
00092
00093
00094
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;
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
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
00133 }
00134 this->prev_buttons_ = joy_msg->buttons;
00135
00136
00137 this->alg_.unlock();
00138
00139 }
00140
00141
00142 bool TibiDaboConversationAlgNode::conversationCallback(tibi_dabo_conversation::conversation::Request &req, tibi_dabo_conversation::conversation::Response &res)
00143 {
00144
00145
00146
00147 this->alg_.lock();
00148
00149
00150
00151
00152
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
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
00167
00168 }
00169 else
00170 {
00171
00172 this->buttonIndex = -1;
00173 this->index = -1;
00174 }
00175
00176
00177 this->alg_.unlock();
00178
00179
00180 return true;
00181 }
00182
00183
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
00192 this->conversation_srv_.request.type = this->buttonIndex;
00193 this->index++;
00194 this->conversation_srv_.request.index = this->index;
00195
00196 if (conversation_client_.call(conversation_srv_))
00197 {
00198
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
00211 this->alg_.unlock();
00212 }
00213
00214 void TibiDaboConversationAlgNode::hriActive()
00215 {
00216
00217 }
00218
00219 void TibiDaboConversationAlgNode::hriFeedback(const tibi_dabo_msgs::sequenceFeedbackConstPtr& feedback)
00220 {
00221
00222
00223 bool feedback_is_ok = true;
00224
00225
00226
00227
00228
00229 if( !feedback_is_ok )
00230 {
00231 hri_client_.cancelGoal();
00232
00233 }
00234 }
00235
00236
00237 void TibiDaboConversationAlgNode::hriMakeActionRequest(std::vector<std::string> &sequences)
00238 {
00239 ROS_INFO("TibiDaboConversationAlgNode::hriMakeActionRequest: Starting New Request!");
00240
00241
00242
00243 hri_client_.waitForServer();
00244
00245
00246
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
00261
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
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
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
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
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
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
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
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
00380 sentencesList.clear();
00381 sentencesList.push_back("\\item=throat ---------------------------");
00382 sentencesList.push_back("\\item=throat, \\item=throat-------------");
00383 this->sentences.push_back(sentencesList);
00384 }