tibi_dabo_hri_teleop_alg_node.cpp
Go to the documentation of this file.
00001 #include "tibi_dabo_hri_teleop_alg_node.h"
00002 
00003 #include <wiimote/State.h>
00004 
00005 TibiDaboHriTeleopAlgNode::TibiDaboHriTeleopAlgNode(void) :
00006   algorithm_base::IriBaseAlgorithm<TibiDaboHriTeleopAlgorithm>(),
00007   hri_client_("hri", true),
00008   hri_action_in_progress_(false),
00009   xml_files(NUM_CLIENTS, std::string(""))
00010 {
00011   //init class attributes if necessary
00012   //this->loop_rate_ = 2;//in [Hz]
00013 
00014   // [init publishers]
00015   
00016   // [init subscribers]
00017   this->joy_subscriber_ = this->public_node_handle_.subscribe("joy", 100, &TibiDaboHriTeleopAlgNode::joy_callback, this);
00018   
00019   // [init services]
00020   
00021   // [init clients]
00022   
00023   // [init action servers]
00024   
00025   // [init action clients]
00026   
00027   hri_goal_.sequence_file   = std::vector<std::string>(NUM_CLIENTS, std::string(""));
00028   hri_goal_.num_repetitions = std::vector<int>(NUM_CLIENTS, 0);
00029   this->sequence_file="";
00030   button_files_.resize(10);
00031   button_files_[0]= this->alg_.xml_path + "/motion/" + "motion1.xml";
00032   button_files_[1]= this->alg_.xml_path + "/motion/" + "motion2.xml";
00033   button_files_[2]= this->alg_.xml_path + "/motion/" + "motionPlus.xml";
00034   button_files_[3]= this->alg_.xml_path + "/motion/" + "motionMinus.xml";
00035   button_files_[4]= this->alg_.xml_path + "/motion/" + "motionHome.xml";
00036   button_files_[5]= this->alg_.xml_path + "/motion/" + "motionB1.xml";
00037   button_files_[6]= this->alg_.xml_path + "/motion/" + "motionB2.xml";
00038   button_files_[7]= this->alg_.xml_path + "/motion/" + "motionBPlus.xml";
00039   button_files_[8]= this->alg_.xml_path + "/motion/" + "motionBMinus.xml";
00040   button_files_[9]= this->alg_.xml_path + "/motion/" + "motionBHome.xml";
00041 }
00042 
00043 TibiDaboHriTeleopAlgNode::~TibiDaboHriTeleopAlgNode(void)
00044 {
00045   // [free dynamic memory]
00046 }
00047 
00048 void TibiDaboHriTeleopAlgNode::mainNodeThread(void)
00049 {
00050   // [fill msg structures]
00051   
00052   // [fill srv structure and make request to the server]
00053   
00054   // [fill action structure and make request to the action server]
00055   
00056   // [publish messages]
00057 }
00058 
00059 /*  [subscriber callbacks] */
00060 void TibiDaboHriTeleopAlgNode::joy_callback(const sensor_msgs::Joy::ConstPtr& joy_msg) 
00061 { 
00062   bool execute_action=false;
00063   static bool first=true;
00064 
00065   if(first)
00066   {
00067     prev_buttons_.resize(joy_msg->buttons.size());
00068     first=false;
00069   }
00070   
00071   index_ = indexFromButtons(joy_msg->buttons);
00072   if(index_!=-1)
00073   {
00074     execute_action=true;
00075     ROS_INFO("TibiDaboHriTeleopAlgNode::joy_callback: hri sequence index %i",index_);
00076   }
00077   /*
00078   for(unsigned int i=0; i<joy_msg->buttons.size(); i++)
00079   {
00080     if(joy_msg->buttons[i]==1 && prev_buttons_[i]==0)
00081     {
00082       index_= useExtraButton(i);
00083       if(index_!=-1)
00084         execute_action=true;
00085     }
00086   }
00087   */ 
00088 
00089   if(hri_action_in_progress_ && execute_action)
00090   {
00091     ROS_INFO("TibiDaboHriTeleopAlgNode::joy_callback: hri_client_.cangelGoal()!");
00092     hri_client_.cancelGoal();
00093   }  
00094   // Request Action
00095   if(!hri_action_in_progress_ && execute_action)
00096   {
00097     for(unsigned int i=0; i<this->alg_.motion_seq_files_.size(); i++)
00098     {
00099       if(button_files_[index_]==this->alg_.motion_seq_files_[i])
00100       {
00101         this->alg_.load_goal(this->alg_.motion_seq_files_[i],xml_files);
00102         break;
00103       }
00104     }
00105     this->hriMakeActionRequest(xml_files);
00106     //hriMakeActionRequest();
00107   }
00108   prev_buttons_ = joy_msg->buttons;
00109 }
00110 
00111 /*  [service callbacks] */
00112 
00113 /*  [action callbacks] */
00114 void TibiDaboHriTeleopAlgNode::hriDone(const actionlib::SimpleClientGoalState& state,  const tibi_dabo_msgs::sequenceResultConstPtr& result) 
00115 { 
00116   ROS_INFO("TibiDaboHriTeleopAlgNode::HRIDone: %s", state.toString().c_str());
00117   if(state.state_==actionlib::SimpleClientGoalState::SUCCEEDED)
00118   {
00119     ROS_INFO("TibiDaboHriTeleopAlgNode::HRIDone: Goal Achieved!");
00120   }
00121   else if(state.state_==actionlib::SimpleClientGoalState::ABORTED)
00122   {
00123     ROS_INFO("TibiDaboHriTeleopAlgNode::HRIDone: Sequence aborted!");
00124   }
00125   else
00126   {
00127     ROS_INFO("TibiDaboHriTeleopAlgNode::HRIDone: Unknown termination state!");
00128   }
00129   hri_action_in_progress_=false;
00130 } 
00131 
00132 void TibiDaboHriTeleopAlgNode::hriActive() 
00133 { 
00134   //ROS_INFO("TibiDaboHriTeleopAlgNode::hriActive: Goal just went active!"); 
00135 } 
00136 
00137 void TibiDaboHriTeleopAlgNode::hriFeedback(const tibi_dabo_msgs::sequenceFeedbackConstPtr& feedback) 
00138 { 
00139   //ROS_INFO("TibiDaboHriTeleopAlgNode::hriFeedback: Got Feedback!"); 
00140 
00141   bool feedback_is_ok = true; 
00142 
00143   //analyze feedback 
00144   //my_var = feedback->var; 
00145 
00146   //if feedback is not what expected, cancel requested goal 
00147   if( !feedback_is_ok ) 
00148   { 
00149     hri_client_.cancelGoal(); 
00150     //ROS_INFO("TibiDaboHriTeleopAlgNode::hriFeedback: Cancelling Action!"); 
00151   }
00152 }
00153 
00154 /*  [action requests] */
00155 void TibiDaboHriTeleopAlgNode::hriMakeActionRequest(std::vector<std::string> &xml_files) 
00156 { 
00157    ROS_INFO("TibiDaboHriTeleopAlgNode::hriMakeActionRequest: Starting New Request!"); 
00158  
00159    //wait for the action server to start 
00160    //will wait for infinite time
00161    ROS_DEBUG("TibiDaboHriTeleopAlgNode::hriMakeActionRequest: Waiting for Server...");
00162    hri_client_.waitForServer();  
00163    ROS_INFO("TibiDaboHriTeleopAlgNode::hriMakeActionRequest: Server is Available!"); 
00164  
00165    //set goal
00166    hri_goal_.sequence_file[TTS_]       = xml_files[TTS_];
00167    hri_goal_.sequence_file[LEDS_]      = xml_files[LEDS_];
00168    hri_goal_.sequence_file[HEAD_]      = xml_files[HEAD_];
00169    hri_goal_.sequence_file[RIGHT_ARM_] = xml_files[RIGHT_ARM_];
00170    hri_goal_.sequence_file[LEFT_ARM_]  = xml_files[LEFT_ARM_];
00171  
00172    //send a goal to the action 
00173    hri_client_.sendGoal(hri_goal_, 
00174                boost::bind(&TibiDaboHriTeleopAlgNode::hriDone,     this, _1, _2), 
00175                boost::bind(&TibiDaboHriTeleopAlgNode::hriActive,   this), 
00176                boost::bind(&TibiDaboHriTeleopAlgNode::hriFeedback, this, _1)); 
00177    ROS_INFO("TibiDaboHriTeleopAlgNode::hriMakeActionRequest: Goal Sent. Wait for Result!"); 
00178    hri_action_in_progress_ = true;
00179 }
00180 
00181 void TibiDaboHriTeleopAlgNode::node_config_update(Config &config, uint32_t level)
00182 {
00183   ROS_INFO("TibiDaboHriTeleopAlgNode: Dynamic reconfigure!");
00184   this->alg_.lock();
00185   button_files_[0] = config.path + "/motion/" + config.file1;
00186   button_files_[1] = config.path + "/motion/" + config.file2;
00187   button_files_[2] = config.path + "/motion/" + config.file3;
00188   button_files_[3] = config.path + "/motion/" + config.file4;
00189   button_files_[4] = config.path + "/motion/" + config.file5;
00190   button_files_[5] = config.path + "/motion/" + config.file6;
00191   button_files_[6] = config.path + "/motion/" + config.file7;
00192   button_files_[7] = config.path + "/motion/" + config.file8;
00193   button_files_[8] = config.path + "/motion/" + config.file9;
00194   button_files_[9] = config.path + "/motion/" + config.file10;
00195   this->alg_.config_   = config; 
00196   this->alg_.unlock();
00197 }
00198 
00199 
00200 void TibiDaboHriTeleopAlgNode::addNodeDiagnostics(void)
00201 {
00202 }
00203 
00204 int TibiDaboHriTeleopAlgNode::indexFromButtons(const std::vector<int> & buttons)
00205 {
00206   ROS_DEBUG("Tibi Dabo wiimote use extra Button");
00207   int index=-1;
00208   for(unsigned int i=0; i<buttons.size(); i++)
00209   {
00210     if(buttons[i]==1 && prev_buttons_[i]==0 && i!=wiimote::State::MSG_BTN_B)
00211     {
00212       index= i;
00213     }
00214   }
00215   
00216   switch(index)
00217   {
00218     case wiimote::State::MSG_BTN_1:
00219       ROS_DEBUG("Button 1!");
00220       if(buttons[wiimote::State::MSG_BTN_B]==0)
00221         return 0;
00222       else
00223         return 5;
00224       break;
00225 
00226     case wiimote::State::MSG_BTN_2:
00227       ROS_DEBUG("Button 2!");
00228       if(buttons[wiimote::State::MSG_BTN_B]==0)
00229         return 1;
00230       else
00231         return 6;
00232       break;
00233 
00234     case wiimote::State::MSG_BTN_B:
00235       ROS_DEBUG("Button B!");
00236       return -1;
00237       break;
00238 
00239     case wiimote::State::MSG_BTN_PLUS:
00240       ROS_DEBUG("Button (+)!");
00241       if(buttons[wiimote::State::MSG_BTN_B]==0)
00242         return 2;
00243       else
00244         return 7;
00245       break;
00246 
00247     case wiimote::State::MSG_BTN_MINUS:
00248       ROS_DEBUG("Button (-)!");
00249       if(buttons[wiimote::State::MSG_BTN_B]==0)
00250         return 3;
00251       else
00252         return 8;
00253       break;
00254 
00255     case wiimote::State::MSG_BTN_HOME:
00256       ROS_DEBUG("Button Home!");
00257       if(buttons[wiimote::State::MSG_BTN_B]==0)
00258         return 4;
00259       else
00260         return 9;
00261       break;
00262 
00263     default:
00264       ROS_DEBUG("Button UNDEFINED!");
00265       return -1;
00266       break;
00267   }
00268 
00269 }
00270 
00271 /* main function */
00272 int main(int argc,char *argv[])
00273 {
00274   return algorithm_base::main<TibiDaboHriTeleopAlgNode>(argc, argv, "tibi_dabo_hri_teleop_alg_node");
00275 }


tibi_dabo_hri_teleop
Author(s):
autogenerated on Fri Dec 6 2013 23:16:37