Go to the documentation of this file.00001 #include "dynamixel_gripper_driver.h"
00002
00003 DynamixelGripperDriver::DynamixelGripperDriver() :
00004 motor_(),
00005 baudrate_(1000000),
00006 bus_id_(0),
00007 servo_id_(255),
00008 max_angle_(170),
00009 min_angle_(115),
00010 effort_(10.0),
00011 closed_(false)
00012 { }
00013
00014 bool
00015 DynamixelGripperDriver::openDriver()
00016 {
00017 std::vector<bool> enable(1,true);
00018 std::string motor_name="gripper_motor";
00019
00020 if (servo_id_ == 255) {
00021 ROS_WARN("No servo identifier has been set");
00022 return false;
00023 }
00024
00025 try
00026 {
00027 motor_ = boost::shared_ptr<CDynamixelMotor>(new CDynamixelMotor(motor_name, bus_id_, baudrate_, servo_id_));
00028
00029 motor_->enable(enable);
00030 motor_->set_torque(effort_);
00031 }
00032 catch(CException &e)
00033 {
00034 ROS_ERROR("%s",e.what().c_str());
00035 return false;
00036 }
00037
00038 ROS_INFO("Gripper is ready");
00039
00040 return true;
00041 }
00042
00043 bool
00044 DynamixelGripperDriver::closeDriver()
00045 {
00046 motor_->close();
00047 return true;
00048 }
00049
00050 bool
00051 DynamixelGripperDriver::startDriver()
00052 {
00053 return true;
00054 }
00055
00056 bool
00057 DynamixelGripperDriver::stopDriver()
00058 {
00059 return true;
00060 }
00061
00062 void
00063 DynamixelGripperDriver::config_update(Config& new_cfg, uint32_t level)
00064 {
00065 std::cout << "Config update" << std::endl;
00066 this->lock();
00067
00068 switch(this->getState())
00069 {
00070 case DynamixelGripperDriver::CLOSED:
00071 servo_id_ = new_cfg.servo_id;
00072 bus_id_ = new_cfg.bus_id;
00073 baudrate_ = new_cfg.baudrate;
00074 max_angle_ = new_cfg.max_angle;
00075 min_angle_ = new_cfg.min_angle;
00076 effort_ = new_cfg.effort;
00077 break;
00078
00079 case DynamixelGripperDriver::OPENED:
00080 break;
00081
00082 case DynamixelGripperDriver::RUNNING:
00083 try
00084 {
00085 motor_->set_torque(new_cfg.effort);
00086 max_angle_ = new_cfg.max_angle;
00087 min_angle_ = new_cfg.min_angle;
00088 effort_ = new_cfg.effort;
00089 }
00090 catch(CException &e)
00091 {
00092 ROS_ERROR("%s",e.what().c_str());
00093 new_cfg.max_angle = max_angle_;
00094 new_cfg.min_angle = min_angle_;
00095 new_cfg.effort = effort_;
00096 }
00097 break;
00098 }
00099
00100
00101 this->config_=new_cfg;
00102
00103 this->unlock();
00104 }
00105
00106 bool
00107 DynamixelGripperDriver::open_gripper()
00108 {
00109 return move_gripper(max_angle_);
00110 }
00111
00112 bool
00113 DynamixelGripperDriver::close_gripper()
00114 {
00115 return move_gripper(min_angle_);
00116 }
00117
00118 void
00119 DynamixelGripperDriver::stop_gripper()
00120 {
00121 if (this->getState() != DynamixelGripperDriver::CLOSED)
00122 motor_->stop();
00123 }
00124
00125 bool
00126 DynamixelGripperDriver::is_moving()
00127 {
00128 if (this->getState() == DynamixelGripperDriver::CLOSED)
00129 return false;
00130
00131 bool response = true;
00132
00133 CEventServer * event_server = CEventServer::instance();
00134
00135 if (event_server->event_is_set(motor_->get_position_feedback_event()))
00136 response = false;
00137
00138 return response;
00139 }
00140
00141 float
00142 DynamixelGripperDriver::get_effort()
00143 {
00144 if (this->getState() == DynamixelGripperDriver::CLOSED)
00145 return 0.0;
00146
00147 return motor_->get_torque();
00148 }
00149
00150 float
00151 DynamixelGripperDriver::get_max_effort()
00152 {
00153 return effort_;
00154 }
00155
00156 float
00157 DynamixelGripperDriver::get_angle()
00158 {
00159 if (this->getState() == DynamixelGripperDriver::CLOSED)
00160 return 0.0;
00161
00162 return motor_->get_position()[0];
00163 }
00164
00165 bool
00166 DynamixelGripperDriver::move_gripper(const double position_in_radians)
00167 {
00168 std::vector<float> position(1),velocity(1),accel(1);
00169 std::vector<float> current_pos(1);
00170
00171 if (this->getState() == DynamixelGripperDriver::CLOSED) {
00172 ROS_WARN("Open driver action was tried without having the driver CLOSED");
00173 return false;
00174 }
00175
00176 std::cout << "Moving the gripper to: " << position_in_radians << std::endl;
00177
00178
00179 motor_->stop();
00180
00181
00182 position[0] = position_in_radians;
00183 velocity[0] = 100.0;
00184 current_pos = motor_->get_position();
00185
00186
00187 if (fabs(current_pos[0] - position[0]) > 1.0) {
00188 accel[0] = velocity[0] * velocity[0]/(2*0.1*fabs(position[0]-current_pos[0]));
00189 std::cout << "Position: " << position[0] <<
00190 "Vel: " << velocity[0] <<
00191 "Acc: " << accel[0] << std::endl;
00192 motor_->load(position,velocity,accel);
00193 motor_->move();
00194 }
00195
00196 return true;
00197 }
00198
00199 DynamixelGripperDriver::~DynamixelGripperDriver()
00200 {
00201 motor_->close();
00202 }