tibi_arm_node_driver.cpp
Go to the documentation of this file.
00001 #include "tibi_arm_node_driver.h"
00002 #include "exceptions.h"
00003 #include <sys/types.h>
00004 #include <sys/stat.h>
00005 #include <unistd.h>
00006 #include <dirent.h>
00007 
00008 std::string arm_name="tibi_arm";
00009 std::string arm_motion_sequence_name="arm_motion_sequence";
00010 
00011 std::string default_config_file="tibi_arm_config.xml";
00012 std::string default_motion_file="motion1.xml";
00013 
00014 std::string config_path=std::string("xml/config/");
00015 std::string motion_path=std::string("xml/motion/");
00016 
00017 TibiArmNodeDriver::TibiArmNodeDriver(void)
00018 {
00019   //setDriverId(driver string id);
00020   this->arm_driver=NULL;
00021   this->feedback_rate=10;
00022 
00023   this->xml_path="./";
00024   this->arm_config_file=this->xml_path + config_path + default_config_file;
00025 }
00026 
00027 void TibiArmNodeDriver::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 files in %s",this->motion_seq_files.size(),full_path.c_str());
00061 }
00062 
00063 
00064 bool TibiArmNodeDriver::openDriver(void)
00065 {
00066   //setDriverId(driver string id);
00067   try{
00068     this->lock();
00069     this->arm_driver=new CPTUD46(arm_name);
00070     this->arm_driver->load_config(this->arm_config_file);
00071     this->unlock();
00072     return true;
00073   }catch(CException &e){
00074     if(this->arm_driver!=NULL)
00075     {
00076       delete this->arm_driver;
00077       this->arm_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   }catch(...){
00084     std::cout << "unknown exception!!!" << std::endl;
00085     this->unlock();
00086   }
00087 
00088   return false;
00089 }
00090 
00091 bool TibiArmNodeDriver::closeDriver(void)
00092 {
00093   if(this->arm_driver!=NULL)
00094   {
00095     delete this->arm_driver;
00096     this->arm_driver=NULL;
00097   }
00098 
00099   return true;
00100 }
00101 
00102 bool TibiArmNodeDriver::startDriver(void)
00103 {
00104   return true;
00105 }
00106 
00107 bool TibiArmNodeDriver::stopDriver(void)
00108 {
00109   return true;
00110 }
00111 
00112 void TibiArmNodeDriver::config_update(Config& new_cfg, uint32_t level)
00113 {
00114   this->lock();
00115 
00116   // depending on current state
00117   // update driver with new_cfg data
00118   switch(this->getState())
00119   {
00120     case TibiArmNodeDriver::CLOSED:
00121       this->xml_path=new_cfg.XML_path;
00122       this->scan_XML_files();
00123       // get the configuration file
00124       this->arm_config_file=this->xml_path + config_path + new_cfg.config_file;
00125       // get the XML file path and update the xml files
00126       // configure the node
00127       break;
00128 
00129     case TibiArmNodeDriver::OPENED:
00130       break;
00131 
00132     case TibiArmNodeDriver::RUNNING:
00133       // stop the current motion if necessary
00134       this->arm_driver->stop_sequence();
00135       // configure the robot
00136       // update the feedback rate
00137       this->feedback_rate=new_cfg.feedback_rate;
00138       // update the XML files
00139       this->xml_path=new_cfg.XML_path;
00140       this->scan_XML_files();
00141       break;
00142   }
00143 
00144   // save the current configuration
00145   this->config_=new_cfg;
00146 
00147   this->unlock();
00148 }
00149 
00150 // API for the sequences
00151 void TibiArmNodeDriver::start_motion_sequence(std::string &filename)
00152 {
00153   std::string full_path;
00154 
00155   full_path=this->xml_path + motion_path + filename;
00156   std::cout << full_path << std::endl;
00157   try{
00158     this->arm_driver->load_sequence(full_path);
00159     this->arm_driver->start_sequence();
00160   }catch(CException &e){
00161     std::cout << e.what() << std::endl;
00162   }
00163 }
00164 
00165 void TibiArmNodeDriver::start_motion_sequence(std::vector<TPTUD46MotionStep> &seq)
00166 {
00167   unsigned int i=0;
00168 
00169   try{
00170     this->arm_driver->clear_sequence();
00171     for(i=0;i<seq.size();i++)
00172       this->arm_driver->add_sequence_step(seq[i].position,seq[i].velocity,seq[i].delay);
00173     this->arm_driver->start_sequence();
00174   }catch(CException &e){
00175     std::cout << e.what() << std::endl;
00176   }
00177 }
00178 
00179 void TibiArmNodeDriver::pause_motion_sequence(void)
00180 {
00181   if(this->arm_driver!=NULL)
00182     this->arm_driver->pause_sequence();
00183 }
00184 
00185 void TibiArmNodeDriver::resume_motion_sequence(void)
00186 {
00187   if(this->arm_driver!=NULL)
00188     this->arm_driver->resume_sequence();
00189 }
00190 
00191 void TibiArmNodeDriver::stop_motion_sequence(void)
00192 {
00193   if(this->arm_driver!=NULL)
00194     this->arm_driver->stop_sequence();
00195 }
00196 
00197 std::string TibiArmNodeDriver::get_motion_sequence_complete_event_id(void)
00198 {
00199   if(this->arm_driver!=NULL)
00200     return this->arm_driver->get_sequence_complete_event_id();
00201   else
00202     return std::string("");
00203 }
00204 
00205 std::string TibiArmNodeDriver::get_motion_sequence_error_event_id(void)
00206 {
00207   if(this->arm_driver!=NULL)
00208     return this->arm_driver->get_sequence_error_event_id();
00209   else
00210     return std::string("");
00211 }
00212 
00213 std::string TibiArmNodeDriver::get_motion_sequence_error_message(void)
00214 {
00215   if(this->arm_driver!=NULL)
00216     return this->arm_driver->get_sequence_error_message();
00217   else
00218     return std::string("");
00219 }
00220 
00221 float TibiArmNodeDriver::get_motion_sequence_completed_percentage(void)
00222 {
00223   if(this->arm_driver!=NULL)
00224     return this->arm_driver->get_sequence_completed_percentage();
00225   else
00226     return 0.0;
00227 }
00228 
00229 // API for the discete motions
00230 void TibiArmNodeDriver::move_absolute_angle(std::vector<double> &position,std::vector<double> &velocity)
00231 {
00232   try{
00233     if(this->arm_driver!=NULL)
00234       this->arm_driver->move_absolute_angle(position,velocity);
00235   }catch(CException &e){
00236     std::cout << e.what() << std::endl;
00237     //throw;  
00238   }
00239 }
00240 
00241 void TibiArmNodeDriver::move_relative_angle(std::vector<double> &position,std::vector<double> &velocity)
00242 {
00243   try{
00244     if(this->arm_driver!=NULL)
00245       this->arm_driver->move_relative_angle(position,velocity);
00246   }catch(CException &e){
00247     std::cout << e.what() << std::endl;
00248     //throw;  
00249   }
00250 }
00251 
00252 void TibiArmNodeDriver::stop(void)
00253 {
00254   if(this->arm_driver!=NULL)
00255     this->arm_driver->stop();
00256 }
00257 
00258 void TibiArmNodeDriver::get_position(std::vector<double> &position)
00259 {
00260   if(this->arm_driver!=NULL)
00261     position=this->arm_driver->get_current_angles();
00262 }
00263 
00264 void TibiArmNodeDriver::get_velocity(std::vector<double> &velocity)
00265 {
00266   if(this->arm_driver!=NULL)
00267     velocity=this->arm_driver->get_current_speeds();
00268 }
00269 
00270 unsigned int TibiArmNodeDriver::get_num_config_files(void)
00271 {
00272   return this->config_files.size();
00273 }
00274 
00275 std::string TibiArmNodeDriver::get_config_file(unsigned int index)
00276 {
00277   if(index > this->config_files.size())
00278   {
00279     /* handle exceptions */
00280   }
00281   return this->config_files[index];
00282 }
00283 
00284 unsigned int TibiArmNodeDriver::get_num_motion_seq_files(void)
00285 {
00286   return this->motion_seq_files.size();
00287 }
00288 
00289 std::string TibiArmNodeDriver::get_motion_seq_file(unsigned int index)
00290 {
00291   if(index > this->motion_seq_files.size())
00292   {
00293     /* handle exceptions */
00294   }
00295   return this->motion_seq_files[index];
00296 }
00297 
00298 int TibiArmNodeDriver::get_feedback_rate(void)
00299 {
00300   return this->feedback_rate;
00301 }
00302 
00303 TibiArmNodeDriver::~TibiArmNodeDriver(void)
00304 {
00305   this->lock();
00306   if(this->arm_driver!=NULL)
00307   {
00308     delete this->arm_driver;
00309     this->arm_driver=NULL;
00310   }
00311   this->unlock();
00312 }


tibi_arm_node
Author(s): Sergi Hernandez Juan
autogenerated on Fri Dec 6 2013 22:58:41