tibi_dabo_head_driver.cpp
Go to the documentation of this file.
00001 #include "tibi_dabo_head_driver.h"
00002 #include "exceptions.h"
00003 #include <stdlib.h>
00004 #include <sys/types.h>
00005 #include <sys/stat.h>
00006 #include <unistd.h>
00007 #include <dirent.h>
00008 
00009 std::string head_name="tibi_dabo_head";
00010 
00011 std::string default_config_file="head_config.xml";
00012 std::string default_motion_file="head_home.xml";
00013 
00014 std::string config_path=std::string("xml/config/");
00015 std::string motion_path=std::string("xml/motion/");
00016 
00017 TibiDaboHeadDriver::TibiDaboHeadDriver()
00018 {
00019   //setDriverId(driver string id);
00020   this->head_driver=NULL;
00021   this->feedback_rate=10;
00022 
00023   this->xml_path="./";
00024   this->head_config_file=this->xml_path + config_path + default_config_file;
00025 }
00026 
00027 void TibiDaboHeadDriver::scan_XML_files(void)
00028 {
00029   std::string full_path;
00030   struct dirent *dp;
00031   struct stat st;
00032   DIR *dir;
00033 
00034   full_path=this->xml_path + config_path;
00035   // check wether the config XML files folder exists or not
00036   if(stat(full_path.c_str(),&st)==0)// the folder exists
00037   {
00038     this->config_files.clear();
00039     dir=opendir(full_path.c_str());
00040     while((dp=readdir(dir)) != NULL) 
00041     {
00042       if(strstr(dp->d_name,".xml")!=NULL)
00043         this->config_files.push_back(full_path + dp->d_name);
00044     }
00045     closedir(dir);
00046   }
00047   ROS_INFO("Found %d configuration files in %s",this->config_files.size(),full_path.c_str());
00048   full_path=this->xml_path + motion_path;
00049   if(stat(full_path.c_str(),&st)==0)// the folder exists
00050   {
00051     this->motion_seq_files.clear();
00052     dir=opendir(full_path.c_str());
00053     while((dp=readdir(dir)) != NULL) 
00054     {
00055       if(strstr(dp->d_name,".xml")!=NULL)
00056         this->motion_seq_files.push_back(full_path + dp->d_name);
00057     }
00058     closedir(dir);
00059   }
00060   ROS_INFO("Found %d motion sequence files in %s",this->motion_seq_files.size(),full_path.c_str());
00061 }
00062 
00063 bool TibiDaboHeadDriver::openDriver(void)
00064 {
00065   //setDriverId(driver string id);
00066   try{
00067     this->lock();
00068     this->head_driver=new CSegwayHead(head_name);
00069     this->head_driver->load_config(this->head_config_file);
00070     this->head_driver->enable();
00071     this->unlock();
00072     return true;
00073   }catch(CException &e){
00074     if(this->head_driver!=NULL)
00075     { 
00076       delete this->head_driver;
00077       this->head_driver=NULL;
00078     }
00079     std::cout << e.what() << std::endl;
00080     ROS_INFO("Impossible to configure the head with the given parameters");
00081     this->unlock();
00082     return false;
00083   }
00084 
00085   return false;
00086 }
00087 
00088 bool TibiDaboHeadDriver::closeDriver(void)
00089 {
00090   if(this->head_driver!=NULL)
00091   {
00092     delete this->head_driver;
00093     this->head_driver=NULL;
00094   }
00095 
00096   return true;
00097 }
00098 
00099 bool TibiDaboHeadDriver::startDriver(void)
00100 {
00101   return true;
00102 }
00103 
00104 bool TibiDaboHeadDriver::stopDriver(void)
00105 {
00106   return true;
00107 }
00108 
00109 void TibiDaboHeadDriver::config_update(Config& new_cfg, uint32_t level)
00110 {
00111   this->lock();
00112   
00113   // depending on current state
00114   // update driver with new_cfg data
00115   switch(this->getState())
00116   {
00117     case TibiDaboHeadDriver::CLOSED:
00118       this->xml_path=new_cfg.XML_path;
00119       this->scan_XML_files();
00120       // get the configuration file
00121       this->head_config_file=this->xml_path + config_path + new_cfg.config_file;
00122       // get the XML file path and update the xml files
00123       // configure the node
00124       break;
00125 
00126     case TibiDaboHeadDriver::OPENED:
00127       break;
00128 
00129     case TibiDaboHeadDriver::RUNNING:
00130       // stop the current motion if necessary
00131       this->head_driver->stop_sequence();
00132       // configure the robot
00133       // update the feedback rate
00134       this->feedback_rate=new_cfg.feedback_rate;
00135       // update the XML files
00136       this->xml_path=new_cfg.XML_path;
00137       this->scan_XML_files();
00138       break;
00139   }
00140 
00141   // save the current configuration
00142   this->config_=new_cfg;
00143 
00144   this->unlock();
00145 }
00146 
00147 // API for the sequences
00148 void TibiDaboHeadDriver::start_motion_sequence(std::string &filename)
00149 {
00150   std::string full_path;
00151 
00152   full_path=this->xml_path + motion_path + filename;
00153   try{
00154     this->head_driver->load_sequence(full_path);
00155     this->head_driver->start_sequence();
00156   }catch(CException &e){
00157     std::cout << e.what() << std::endl;
00158   }
00159 }
00160 
00161 void TibiDaboHeadDriver::start_motion_sequence(std::vector<TDynamixelMotionStep> &seq)
00162 {
00163   unsigned int i=0;
00164 
00165   try{
00166     this->head_driver->clear_sequence();
00167     for(i=0;i<seq.size();i++)
00168       this->head_driver->add_sequence_step(seq[i].position,seq[i].velocity,seq[i].delay);
00169     this->head_driver->start_sequence();
00170   }catch(CException &e){
00171     std::cout << e.what() << std::endl;
00172   }
00173 }
00174 
00175 void TibiDaboHeadDriver::pause_motion_sequence(void)
00176 {
00177   if(this->head_driver!=NULL)
00178     this->head_driver->pause_sequence();
00179 }
00180 
00181 void TibiDaboHeadDriver::resume_motion_sequence(void)
00182 {
00183   if(this->head_driver!=NULL)
00184     this->head_driver->resume_sequence();
00185 }
00186 
00187 void TibiDaboHeadDriver::stop_motion_sequence(void)
00188 {
00189   if(this->head_driver!=NULL)
00190     this->head_driver->stop_sequence();
00191 }
00192 
00193 std::string TibiDaboHeadDriver::get_motion_sequence_complete_event_id(void)
00194 {
00195   if(this->head_driver!=NULL)
00196     return this->head_driver->get_sequence_complete_event_id();
00197   else 
00198     return std::string("");
00199 }
00200 
00201 std::string TibiDaboHeadDriver::get_motion_sequence_error_event_id(void)
00202 {
00203   if(this->head_driver!=NULL)
00204     return this->head_driver->get_sequence_error_event_id();
00205   else
00206     return std::string("");
00207 }
00208 
00209 std::string TibiDaboHeadDriver::get_motion_sequence_error_message(void)
00210 {
00211   if(this->head_driver!=NULL)
00212     return this->head_driver->get_sequence_error_message();
00213   else
00214     return std::string("");
00215 }
00216 
00217 float TibiDaboHeadDriver::get_motion_sequence_completed_percentage(void)
00218 {
00219   if(this->head_driver!=NULL)
00220     return this->head_driver->get_sequence_completed_percentage();
00221   else
00222     return 0.0;
00223 }
00224 
00225 // API for the discete motions
00226 void TibiDaboHeadDriver::move_absolute_angle(std::vector<double> &position,std::vector<double> &velocity)
00227 {
00228   try{
00229     if(this->head_driver!=NULL)
00230       this->head_driver->move_absolute_angle(position,velocity);
00231   }catch(CException &e){
00232     std::cout << e.what() << std::endl;
00233     //throw;  
00234   }
00235 }
00236 
00237 void TibiDaboHeadDriver::move_relative_angle(std::vector<double> &position,std::vector<double> &velocity)
00238 {
00239   try{
00240     if(this->head_driver!=NULL)
00241       this->head_driver->move_relative_angle(position,velocity);
00242   }catch(CException &e){
00243     std::cout << e.what() << std::endl;
00244     //throw;  
00245   }
00246 }
00247 
00248 void TibiDaboHeadDriver::stop(void)
00249 {
00250   if(this->head_driver!=NULL)
00251     this->head_driver->stop();
00252 }
00253 
00254 void TibiDaboHeadDriver::get_position(std::vector<double> &position)
00255 {
00256   if(this->head_driver!=NULL)
00257     this->head_driver->get_current_angle(position);
00258 }
00259 
00260 void TibiDaboHeadDriver::get_velocity(std::vector<double> &velocity)
00261 {
00262   if(this->head_driver!=NULL)
00263     this->head_driver->get_current_speed(velocity);
00264 }
00265 
00266 // face expressions API
00267 void TibiDaboHeadDriver::set_face_expression(std::string &expression)
00268 {
00269   if(this->head_driver!=NULL)
00270   {
00271     if(expression=="stand_by")
00272       this->head_driver->stand_by();
00273     else if(expression=="happy")
00274       this->head_driver->happy();
00275     else if(expression=="sad")
00276       this->head_driver->sad();
00277     else if(expression=="angry")
00278       this->head_driver->angry();
00279     else if(expression=="confused")
00280       this->head_driver->confused();
00281     else if(expression=="shy")
00282       this->head_driver->shy();
00283     else if(expression=="ashamed")
00284       this->head_driver->ashamed();
00285     else if(expression=="speak")
00286       this->head_driver->speak();
00287   }
00288 }
00289 
00290 void TibiDaboHeadDriver::set_face_expression(int index)
00291 {
00292   if(this->head_driver!=NULL)
00293   {
00294     switch(index)
00295     {
00296       case 0: this->head_driver->stand_by();
00297               break;
00298       case 1: this->head_driver->happy();
00299               break;
00300       case 2: this->head_driver->sad();
00301               break;
00302       case 3: this->head_driver->angry();
00303               break;
00304       case 4: this->head_driver->confused();
00305               break;
00306       case 5: this->head_driver->shy();
00307               break;
00308       case 6: this->head_driver->ashamed();
00309               break;
00310       case 7: this->head_driver->speak();
00311               break;
00312       default: /* no nothing */
00313                break;
00314     }
00315   }
00316 }
00317 
00318 void TibiDaboHeadDriver::set_brightness(float brightness)
00319 {
00320   if(this->head_driver!=NULL)
00321     this->head_driver->set_brightness(brightness);
00322 }
00323 
00324 unsigned int TibiDaboHeadDriver::get_num_config_files(void)
00325 {
00326   return this->config_files.size();
00327 }
00328 
00329 std::string TibiDaboHeadDriver::get_config_file(unsigned int index)
00330 {
00331   if(index > this->config_files.size())
00332   {
00333     /* handle exceptions */
00334   }
00335   return this->config_files[index];
00336 }
00337 
00338 unsigned int TibiDaboHeadDriver::get_num_motion_seq_files(void)
00339 {
00340   return this->motion_seq_files.size();
00341 }
00342 
00343 std::string TibiDaboHeadDriver::get_motion_seq_file(unsigned int index)
00344 {
00345   if(index > this->motion_seq_files.size())
00346   {
00347     /* handle exceptions */
00348   }
00349   return this->motion_seq_files[index];
00350 }
00351 
00352 int TibiDaboHeadDriver::get_feedback_rate(void)
00353 {
00354   return this->feedback_rate;
00355 }
00356 
00357 TibiDaboHeadDriver::~TibiDaboHeadDriver()
00358 {
00359   if(this->head_driver!=NULL)
00360   {
00361     delete this->head_driver;
00362     this->head_driver=NULL;
00363   }
00364 }


tibi_dabo_head_node
Author(s): Sergi Hernandez Juan
autogenerated on Fri Dec 6 2013 21:36:40