|
| 1 | +/******************************************************************************* |
| 2 | +* Copyright 2016 ROBOTIS CO., LTD. |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*******************************************************************************/ |
| 16 | + |
| 17 | +/* Authors: Yoonseok Pyo, Leon Jung, Darby Lim, HanCheol Cho */ |
| 18 | + |
| 19 | +#include "turtlebot3_conveyor_motor_driver.h" |
| 20 | + |
| 21 | +Turtlebot3MotorDriver::Turtlebot3MotorDriver() |
| 22 | +: baudrate_(BAUDRATE), |
| 23 | + protocol_version_(PROTOCOL_VERSION) |
| 24 | +{ |
| 25 | +} |
| 26 | + |
| 27 | +Turtlebot3MotorDriver::~Turtlebot3MotorDriver() |
| 28 | +{ |
| 29 | + closeDynamixel(); |
| 30 | +} |
| 31 | + |
| 32 | +bool Turtlebot3MotorDriver::init(void) |
| 33 | +{ |
| 34 | + portHandler_ = dynamixel::PortHandler::getPortHandler(DEVICENAME); |
| 35 | + packetHandler_ = dynamixel::PacketHandler::getPacketHandler(PROTOCOL_VERSION); |
| 36 | + |
| 37 | + // Open port |
| 38 | + if (portHandler_->openPort()) |
| 39 | + { |
| 40 | + ERROR_PRINT("Port is opened"); |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + ERROR_PRINT("Port couldn't be opened"); |
| 45 | + |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + // Set port baudrate |
| 50 | + if (portHandler_->setBaudRate(baudrate_)) |
| 51 | + { |
| 52 | + ERROR_PRINT("Baudrate is set"); |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + ERROR_PRINT("Baudrate couldn't be set"); |
| 57 | + |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + // Enable Dynamixel Torque |
| 62 | + setTorque(WHEEL_L_R, true); |
| 63 | + setTorque(WHEEL_R_R, true); |
| 64 | + setTorque(WHEEL_L_F, true); |
| 65 | + setTorque(WHEEL_R_F, true); |
| 66 | + |
| 67 | + setTorque(JOINT_L_R, true); |
| 68 | + setTorque(JOINT_R_R, true); |
| 69 | + setTorque(JOINT_L_F, true); |
| 70 | + setTorque(JOINT_R_F, true); |
| 71 | + |
| 72 | + groupSyncWriteVelocity_ = new dynamixel::GroupSyncWrite(portHandler_, packetHandler_, ADDR_X_GOAL_VELOCITY, LEN_X_GOAL_VELOCITY); |
| 73 | + groupSyncWritePosition_ = new dynamixel::GroupSyncWrite(portHandler_, packetHandler_, ADDR_X_GOAL_POSITION, LEN_X_GOAL_POSITION); |
| 74 | + |
| 75 | + return true; |
| 76 | +} |
| 77 | + |
| 78 | +bool Turtlebot3MotorDriver::setTorque(uint8_t id, bool onoff) |
| 79 | +{ |
| 80 | + uint8_t dxl_error = 0; |
| 81 | + int dxl_comm_result = COMM_TX_FAIL; |
| 82 | + |
| 83 | + dxl_comm_result = packetHandler_->write1ByteTxRx(portHandler_, id, ADDR_X_TORQUE_ENABLE, onoff, &dxl_error); |
| 84 | + if(dxl_comm_result != COMM_SUCCESS) |
| 85 | + { |
| 86 | + packetHandler_->getTxRxResult(dxl_comm_result); |
| 87 | + } |
| 88 | + else if(dxl_error != 0) |
| 89 | + { |
| 90 | + packetHandler_->getRxPacketError(dxl_error); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +void Turtlebot3MotorDriver::closeDynamixel(void) |
| 95 | +{ |
| 96 | + // Disable Dynamixel Torque |
| 97 | + setTorque(WHEEL_L_R, false); |
| 98 | + setTorque(WHEEL_R_R, false); |
| 99 | + setTorque(WHEEL_L_F, false); |
| 100 | + setTorque(WHEEL_R_F, false); |
| 101 | + |
| 102 | + setTorque(JOINT_L_R, false); |
| 103 | + setTorque(JOINT_R_R, false); |
| 104 | + setTorque(JOINT_L_F, false); |
| 105 | + setTorque(JOINT_R_F, false); |
| 106 | + |
| 107 | + // Close port |
| 108 | + portHandler_->closePort(); |
| 109 | +} |
| 110 | + |
| 111 | +bool Turtlebot3MotorDriver::controlJoints(int32_t *value) |
| 112 | +{ |
| 113 | + bool dxl_addparam_result_; |
| 114 | + int8_t dxl_comm_result_; |
| 115 | + |
| 116 | + dxl_addparam_result_ = groupSyncWritePosition_->addParam(JOINT_L_R, (uint8_t*)&value[0]); |
| 117 | + if (dxl_addparam_result_ != true) |
| 118 | + return false; |
| 119 | + |
| 120 | + dxl_addparam_result_ = groupSyncWritePosition_->addParam(JOINT_R_R, (uint8_t*)&value[1]); |
| 121 | + if (dxl_addparam_result_ != true) |
| 122 | + return false; |
| 123 | + |
| 124 | + dxl_addparam_result_ = groupSyncWritePosition_->addParam(JOINT_L_F, (uint8_t*)&value[2]); |
| 125 | + if (dxl_addparam_result_ != true) |
| 126 | + return false; |
| 127 | + |
| 128 | + dxl_addparam_result_ = groupSyncWritePosition_->addParam(JOINT_R_F, (uint8_t*)&value[3]); |
| 129 | + if (dxl_addparam_result_ != true) |
| 130 | + return false; |
| 131 | + |
| 132 | + dxl_comm_result_ = groupSyncWritePosition_->txPacket(); |
| 133 | + if (dxl_comm_result_ != COMM_SUCCESS) |
| 134 | + { |
| 135 | + packetHandler_->getTxRxResult(dxl_comm_result_); |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + groupSyncWritePosition_->clearParam(); |
| 140 | + return true; |
| 141 | +} |
| 142 | + |
| 143 | +bool Turtlebot3MotorDriver::controlWheels(int32_t *value) |
| 144 | +{ |
| 145 | + bool dxl_addparam_result_; |
| 146 | + int8_t dxl_comm_result_; |
| 147 | + |
| 148 | + dxl_addparam_result_ = groupSyncWriteVelocity_->addParam(WHEEL_L_R, (uint8_t*)&value[0]); |
| 149 | + if (dxl_addparam_result_ != true) |
| 150 | + return false; |
| 151 | + |
| 152 | + dxl_addparam_result_ = groupSyncWriteVelocity_->addParam(WHEEL_R_R, (uint8_t*)&value[1]); |
| 153 | + if (dxl_addparam_result_ != true) |
| 154 | + return false; |
| 155 | + |
| 156 | + dxl_addparam_result_ = groupSyncWriteVelocity_->addParam(WHEEL_L_F, (uint8_t*)&value[2]); |
| 157 | + if (dxl_addparam_result_ != true) |
| 158 | + return false; |
| 159 | + |
| 160 | + dxl_addparam_result_ = groupSyncWriteVelocity_->addParam(WHEEL_R_F, (uint8_t*)&value[3]); |
| 161 | + if (dxl_addparam_result_ != true) |
| 162 | + return false; |
| 163 | + |
| 164 | + dxl_comm_result_ = groupSyncWriteVelocity_->txPacket(); |
| 165 | + if (dxl_comm_result_ != COMM_SUCCESS) |
| 166 | + { |
| 167 | + packetHandler_->getTxRxResult(dxl_comm_result_); |
| 168 | + return false; |
| 169 | + } |
| 170 | + |
| 171 | + groupSyncWriteVelocity_->clearParam(); |
| 172 | + return true; |
| 173 | +} |
0 commit comments