00001 #include "dynamixel_nc_gripper_driver.h" 00002 #include <stdlib.h> 00003 #include <sys/types.h> 00004 #include <sys/stat.h> 00005 #include <unistd.h> 00006 #include <dirent.h> 00007 00008 std::string config_path = std::string("/config/"); 00009 00010 DynamixelNcGripperDriver::DynamixelNcGripperDriver(void): 00011 // dynamixel attributes (default values) 00012 gripper_config_file_(std::string("gripper_nc_config.xml")), 00013 xml_path_(std::string(".")), 00014 full_path_(xml_path_ + config_path + gripper_config_file_) 00015 { 00016 //setDriverId(driver string id); 00017 } 00018 00019 DynamixelNcGripperDriver::~DynamixelNcGripperDriver(void) 00020 { 00021 } 00022 00023 void DynamixelNcGripperDriver::scan_XML_files(void) 00024 { 00025 std::string full_path; 00026 struct dirent *dp; 00027 struct stat st; 00028 DIR *dir; 00029 00030 full_path=this->xml_path_ + config_path; 00031 // check wether the config XML files folder exists or not 00032 if(stat(full_path.c_str(),&st)==0)// the folder exists 00033 { 00034 dir=opendir(full_path.c_str()); 00035 while((dp=readdir(dir)) != NULL) 00036 { 00037 if(strstr(dp->d_name,".xml")!=NULL) 00038 this->full_path_ = full_path + dp->d_name; 00039 } 00040 closedir(dir); 00041 } 00042 ROS_INFO("Found gripper configuration file %s in %s", gripper_config_file_.c_str(), full_path.c_str()); 00043 } 00044 00045 bool DynamixelNcGripperDriver::openDriver(void) 00046 { 00047 try 00048 { 00049 this->lock(); 00050 this->gripper_ = boost::shared_ptr<CDynamixelGripper>(new CDynamixelGripper()); 00051 gripper_->load_config(this->full_path_); 00052 std::cout << *gripper_ << std::endl; 00053 this->unlock(); 00054 return true; 00055 } 00056 catch(CException &e) 00057 { 00058 std::cout << e.what() << std::endl; 00059 ROS_INFO("Impossible to configure the Gripper with the given parameters"); 00060 this->unlock(); 00061 return false; 00062 } 00063 00064 return false; 00065 00066 } 00067 00068 bool DynamixelNcGripperDriver::closeDriver(void) 00069 { 00070 return true; 00071 } 00072 00073 bool DynamixelNcGripperDriver::startDriver(void) 00074 { 00075 return true; 00076 } 00077 00078 bool DynamixelNcGripperDriver::stopDriver(void) 00079 { 00080 return true; 00081 } 00082 00083 void DynamixelNcGripperDriver::config_update(Config& new_cfg, uint32_t level) 00084 { 00085 this->lock(); 00086 00087 // depending on current state 00088 // update driver with new_cfg data 00089 switch(this->getState()) 00090 { 00091 case DynamixelNcGripperDriver::CLOSED: 00092 // save the values into the class 00093 this->xml_path_ = new_cfg.XML_path; 00094 this->scan_XML_files(); 00095 this->full_path_ = this->xml_path_ + config_path + new_cfg.config_file; 00096 break; 00097 00098 case DynamixelNcGripperDriver::OPENED: 00099 break; 00100 00101 case DynamixelNcGripperDriver::RUNNING: 00102 try 00103 { 00104 // set the torque 00105 } 00106 catch(CDynamixelMotorException &e) 00107 { 00108 ROS_ERROR("%s",e.what().c_str()); 00109 } 00110 break; 00111 } 00112 00113 // save the current configuration 00114 this->config_ = new_cfg; 00115 00116 this->unlock(); 00117 } 00118 00119 00120 bool DynamixelNcGripperDriver::open_gripper(void) 00121 { 00122 // return move_gripper(max_angle_); 00123 return this->gripper_->open_gripper(); 00124 00125 } 00126 00127 bool DynamixelNcGripperDriver::close_gripper() 00128 { 00129 // return move_gripper(min_angle_); 00130 return this->gripper_->close_gripper(); 00131 } 00132 00133 void DynamixelNcGripperDriver::stop_gripper() 00134 { 00135 if (this->getState() != DynamixelNcGripperDriver::CLOSED) 00136 { 00137 try 00138 { 00139 this->gripper_->disable(); 00140 } 00141 catch (CDynamixelMotorException &e) 00142 { 00143 ROS_ERROR("%s", e.what().c_str()); 00144 } 00145 } 00146 } 00147 00148 float DynamixelNcGripperDriver::get_current_effort() 00149 { 00150 return this->gripper_->get_current_effort(); 00151 } 00152 00153 float DynamixelNcGripperDriver::get_current_angle() 00154 { 00155 return this->gripper_->get_current_angle(); 00156 } 00157 00158 bool DynamixelNcGripperDriver::is_at_max_angle() 00159 { 00160 return this->gripper_->is_gripper_opened(); 00161 00162 } 00163 00164 bool DynamixelNcGripperDriver::is_at_min_angle() 00165 { 00166 return this->gripper_->is_gripper_closed(); 00167 } 00168 00169 void DynamixelNcGripperDriver::disable_gripper() 00170 { 00171 this->gripper_->disable(); 00172 } 00173