tibi_dabo_arm_driver.cpp
Go to the documentation of this file.
00001 #include "tibi_dabo_arm_driver.h"
00002 #include <sys/types.h>
00003 #include <sys/stat.h>
00004 #include <unistd.h>
00005 #include <dirent.h>
00006 
00007 std::string arm_name="tibi_dabo_arm";
00008 std::string arm_motion_sequence_name="arm_motion_sequence";
00009 
00010 std::string default_config_file="base_arm_config.xml"; 
00011 std::string default_motion_file="motion1.xml";
00012 
00013 std::string config_path=std::string("xml/config/");
00014 std::string motion_path=std::string("xml/motion/");
00015 
00016 TibiDaboArmDriver::TibiDaboArmDriver():arm_driver(arm_name)
00017 {
00018   //setDriverId(driver string id);
00019   //setDriverId(driver string id);
00020   this->motion_sequence=NULL;
00021   this->motion_sequence=new CMotionSequence(arm_motion_sequence_name);
00022   this->feedback_rate=10;
00023   this->absolute_motion=true;
00024   this->position_control=true;
00025 
00026 
00027   this->xml_path="./";
00028   this->arm_config_file=this->xml_path  + config_path + default_config_file;
00029 }
00030 
00031 void TibiDaboArmDriver::scan_XML_files(void)
00032 {
00033   std::string full_path;
00034   struct dirent *dp;
00035   struct stat st;
00036   DIR *dir;
00037 
00038   full_path=this->xml_path + config_path;
00039   // check wether the config XML files folder exists or not
00040   if(stat(full_path.c_str(),&st)==0)// the folder exists
00041   {
00042     this->config_files.clear();
00043     dir=opendir(full_path.c_str());
00044     while((dp=readdir(dir)) != NULL)
00045     {
00046       if(strstr(dp->d_name,".xml")!=NULL)
00047         this->config_files.push_back(full_path + dp->d_name);
00048     }
00049     closedir(dir);
00050   }
00051   ROS_INFO("Found %d configuration files in %s",this->config_files.size(),full_path.c_str());
00052   full_path=this->xml_path + motion_path;
00053   if(stat(full_path.c_str(),&st)==0)// the folder exists
00054   {
00055     this->motion_seq_files.clear();
00056     dir=opendir(full_path.c_str());
00057     while((dp=readdir(dir)) != NULL)
00058     {
00059       if(strstr(dp->d_name,".xml")!=NULL)
00060         this->motion_seq_files.push_back(full_path + dp->d_name);
00061     }
00062     closedir(dir);
00063   }
00064   ROS_INFO("Found %d motion sequence files files in %s",this->motion_seq_files.size(),full_path.c_str());
00065 }
00066 
00067 bool TibiDaboArmDriver::openDriver(void)
00068 {
00069   std::vector<bool> absolute(4);
00070   std::vector<bool> enabled;
00071   unsigned int i=0;
00072 
00073   //setDriverId(driver string id);
00074   try{
00075     this->lock();
00076     this->arm_driver.load_config(this->arm_config_file);
00077     for(i=0;i<this->arm_driver.get_num_motors();i++)
00078       enabled.push_back(true);
00079     this->arm_driver.enable(enabled);
00080     if(this->position_control)
00081       this->arm_driver.set_position_control();
00082     else
00083       this->arm_driver.set_velocity_control();
00084     if(this->absolute_motion)
00085     {
00086       for(i=0;i<this->arm_driver.get_num_motors();i++)
00087         absolute[i]=true;
00088     }
00089     else
00090     {
00091       for(i=0;i<this->arm_driver.get_num_motors();i++)
00092         absolute[i]=false;
00093     }
00094     this->arm_driver.set_absolute_motion(absolute);
00095     this->motion_sequence->set_motor_group(&this->arm_driver);
00096     this->motion_sequence->set_timeout_factor(1.5);
00097     this->motion_sequence->set_acceleration_factor(0.5);
00098     this->unlock();
00099     return true;
00100   }catch(CException &e){
00101     std::cout << e.what() << std::endl;
00102     ROS_INFO("Impossible to configure the arm with the given parameters");
00103     this->unlock();
00104     return false;
00105   }
00106 }
00107 
00108 bool TibiDaboArmDriver::closeDriver(void)
00109 {
00110   return true;
00111 }
00112 
00113 bool TibiDaboArmDriver::startDriver(void)
00114 {
00115   return true;
00116 }
00117 
00118 bool TibiDaboArmDriver::stopDriver(void)
00119 {
00120   return true;
00121 }
00122 
00123 void TibiDaboArmDriver::config_update(Config& new_cfg, uint32_t level)
00124 {
00125   std::vector<bool> absolute(2);
00126   std::string motion_seq;
00127   unsigned int i=0;
00128 
00129   this->lock();
00130 
00131   std::cout << "config_update" << std::endl; 
00132   // depending on current state
00133   // update driver with new_cfg data
00134   switch(this->getState())
00135   {
00136     case TibiDaboArmDriver::CLOSED:
00137       this->xml_path=new_cfg.XML_path;
00138       this->scan_XML_files();
00139       // get the configuration file
00140       this->arm_config_file=this->xml_path + config_path + new_cfg.config_file;
00141       if(new_cfg.motion_mode==0)// absolute motion
00142         this->absolute_motion=true;
00143       else
00144         this->absolute_motion=false;
00145       if(new_cfg.control_mode==0)// position control
00146         this->position_control=true;
00147       else
00148         this->position_control=false;
00149       break;
00150 
00151       break;
00152 
00153     case TibiDaboArmDriver::OPENED:
00154       break;
00155 
00156     case TibiDaboArmDriver::RUNNING:
00157       // stop the current motion if necessary
00158       if(this->motion_sequence!=NULL)
00159       {
00160         if(this->motion_sequence->get_current_state()==mtn_started ||
00161            this->motion_sequence->get_current_state()==mtn_paused)
00162           this->motion_sequence->stop_sequence();
00163       }
00164       // configure the robot
00165       if(new_cfg.control_mode==0)// position control
00166       {
00167         this->position_control=true;
00168         this->arm_driver.set_position_control();
00169       }
00170       else
00171       {
00172         this->position_control=false;
00173         this->arm_driver.set_velocity_control();
00174       }
00175       if(new_cfg.motion_mode==0)//absolute motion
00176         for(i=0;i<this->arm_driver.get_num_motors();i++)
00177           absolute[i]=true;
00178       else
00179         for(i=0;i<this->arm_driver.get_num_motors();i++)
00180           absolute[i]=false;
00181       this->arm_driver.set_absolute_motion(absolute);
00182       // update the feedback rate
00183       this->feedback_rate=new_cfg.feedback_rate;
00184       // update the XML files
00185       this->xml_path=new_cfg.XML_path;
00186       this->scan_XML_files();
00187       break;
00188   }
00189 
00190   // save the current configuration
00191   this->config_=new_cfg;
00192   this->unlock();
00193 
00194 }
00195 
00196 // API for the sequences
00197 void TibiDaboArmDriver::start_motion_sequence(std::string &filename)
00198 {
00199   std::string full_path;
00200 
00201   full_path=this->xml_path + motion_path + filename;
00202   try{
00203     this->motion_sequence->load_sequence(full_path);
00204     this->motion_sequence->start_sequence();
00205   }catch(CException &e){
00206     std::cout << e.what() << std::endl;
00207   }
00208 }
00209 
00210 void TibiDaboArmDriver::start_motion_sequence(std::vector<TMotionStep> &seq)
00211 {
00212   unsigned int i=0;
00213 
00214   try{
00215     if(this->motion_sequence==NULL)
00216     {
00217       this->motion_sequence=new CMotionSequence(arm_motion_sequence_name);
00218       this->motion_sequence->set_motor_group(&this->arm_driver);
00219       this->motion_sequence->set_timeout_factor(1.5);
00220       this->motion_sequence->set_acceleration_factor(0.5);
00221     }
00222     this->motion_sequence->clear_sequence();
00223     for(i=0;i<seq.size();i++)
00224       this->motion_sequence->add_step(seq[i].position,seq[i].velocity,seq[i].delay);
00225     this->motion_sequence->start_sequence();
00226   }catch(CException &e){
00227     std::cout << e.what() << std::endl;
00228   }
00229 }
00230 
00231 void TibiDaboArmDriver::pause_motion_sequence(void)
00232 {
00233   this->motion_sequence->pause_sequence();
00234 }
00235 
00236 void TibiDaboArmDriver::resume_motion_sequence(void)
00237 {
00238   this->motion_sequence->resume_sequence();
00239 }
00240 
00241 void TibiDaboArmDriver::stop_motion_sequence(void)
00242 {
00243   this->motion_sequence->stop_sequence();
00244 }
00245 
00246 std::string TibiDaboArmDriver::get_motion_sequence_complete_event_id(void)
00247 {
00248   if(this->motion_sequence!=NULL)
00249     return this->motion_sequence->get_sequence_complete_event_id();
00250   else
00251     return std::string("");
00252 }
00253 
00254 std::string TibiDaboArmDriver::get_motion_sequence_error_event_id(void)
00255 {
00256   if(this->motion_sequence!=NULL)
00257     return this->motion_sequence->get_sequence_error_event_id();
00258   else
00259     return std::string("");
00260 }
00261 
00262 std::string TibiDaboArmDriver::get_motion_sequence_error_message(void)
00263 {
00264   if(this->motion_sequence!=NULL)
00265     return this->motion_sequence->get_error_message();
00266   else
00267     return std::string("");
00268 }
00269 
00270 float TibiDaboArmDriver::get_motion_sequence_completed_percentage(void)
00271 {
00272   if(this->motion_sequence!=NULL)
00273     return this->motion_sequence->get_completed_percentage();
00274   else
00275     return 0.0;
00276 }
00277 
00278 // API for the discete motions
00279 std::string TibiDaboArmDriver::get_position_reached_event_id(void)
00280 {
00281   return this->arm_driver.get_position_reached_event_id();
00282 }
00283 
00284 void TibiDaboArmDriver::move(std::vector<float> &position,std::vector<float> &velocity,std::vector<float> &acceleration)
00285 {
00286   CEventServer *event_server=CEventServer::instance();
00287   std::list<std::string> events;
00288 
00289   try{
00290     if(this->motion_sequence!=NULL)
00291     {
00292       delete this->motion_sequence;
00293       this->motion_sequence=NULL;
00294       this->arm_driver.move(position,velocity,acceleration);
00295     }
00296     else
00297     {
00298       if(event_server->event_is_set(this->arm_driver.get_position_reached_event_id()))
00299       {
00300         events.push_back(this->arm_driver.get_position_reached_event_id());
00301         event_server->wait_all(events);
00302         this->arm_driver.move(position,velocity,acceleration);
00303       }
00304     }
00305   }catch(CException &e){
00306     std::cout << e.what() << std::endl;
00307     //throw;
00308   }
00309 }
00310 
00311 void TibiDaboArmDriver::stop(void)
00312 {
00313   this->arm_driver.stop();
00314 }
00315 
00316 void TibiDaboArmDriver::get_position(std::vector<float> &position)
00317 {
00318   this->arm_driver.get_position(position);
00319 }
00320 
00321 void TibiDaboArmDriver::get_velocity(std::vector<float> &velocity)
00322 {
00323   this->arm_driver.get_velocity(velocity);
00324 }
00325 
00326 void TibiDaboArmDriver::set_absolute_motion(bool status)
00327 {
00328   std::vector<bool> absolute;
00329   unsigned int i=0;
00330 
00331   try{
00332     for(i=0;i<this->arm_driver.get_num_motors();i++)
00333       absolute.push_back(status);
00334     this->arm_driver.set_absolute_motion(absolute);
00335   }catch(CException &e){
00336     std::cout << e.what() << std::endl;
00337     throw;
00338   }
00339 }
00340 
00341 void TibiDaboArmDriver::set_position_control(bool status)
00342 {
00343   try{
00344     if(status==true)
00345       this->arm_driver.set_position_control();
00346     else
00347       this->arm_driver.set_velocity_control();
00348   }catch(CException &e){
00349     std::cout << e.what() << std::endl;
00350     throw;
00351   }
00352 }
00353 
00354 bool TibiDaboArmDriver::using_absolute_motion(void)
00355 {
00356   return this->absolute_motion;
00357 }
00358 
00359 bool TibiDaboArmDriver::using_position_control(void)
00360 {
00361   return this->position_control;
00362 }
00363 
00364 unsigned int TibiDaboArmDriver::get_num_config_files(void)
00365 {
00366   return this->config_files.size();
00367 }
00368 
00369 std::string TibiDaboArmDriver::get_config_file(unsigned int index)
00370 {
00371   if(index > this->config_files.size())
00372   {
00373     /* handle exceptions */
00374   }
00375   return this->config_files[index];
00376 }
00377 
00378 unsigned int TibiDaboArmDriver::get_num_motion_seq_files(void)
00379 {
00380   return this->motion_seq_files.size();
00381 }
00382 
00383 std::string TibiDaboArmDriver::get_motion_seq_file(unsigned int index)
00384 {
00385   if(index > this->motion_seq_files.size())
00386   {
00387     /* handle exceptions */
00388   }
00389   return this->motion_seq_files[index];
00390 }
00391 
00392 int TibiDaboArmDriver::get_feedback_rate(void)
00393 {
00394   return this->feedback_rate;
00395 }
00396 
00397 TibiDaboArmDriver::~TibiDaboArmDriver()
00398 {
00399 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


tibi_dabo_arm_node
Author(s): Sergi Hernandez Juan
autogenerated on Fri Sep 27 2013 10:24:22