|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
| 15 | +#include <cmath> |
15 | 16 |
|
16 | 17 | #include "dynamixel_xm.hpp" |
17 | 18 |
|
18 | 19 |
|
19 | 20 | namespace dynamixel_xm { |
20 | 21 |
|
| 22 | +const uint16_t ADDR_OPERATING_MODE = 11; |
| 23 | +const uint16_t ADDR_CURRENT_LIMIT = 38; |
| 24 | +const uint16_t ADDR_MAX_POSITION_LIMIT = 48; |
| 25 | +const uint16_t ADDR_MIN_POSITION_LIMIT = 52; |
21 | 26 | const uint16_t ADDR_TORQUE_ENABLE = 64; |
| 27 | +const uint16_t ADDR_VELOCITY_I_GAIN = 76; |
| 28 | +const uint16_t ADDR_VELOCITY_P_GAIN = 78; |
| 29 | +const uint16_t ADDR_POSITION_D_GAIN = 80; |
| 30 | +const uint16_t ADDR_POSITION_I_GAIN = 82; |
| 31 | +const uint16_t ADDR_POSITION_P_GAIN = 84; |
| 32 | +const uint16_t ADDR_PROFILE_ACCELERATION = 108; |
| 33 | +const uint16_t ADDR_PROFILE_VELOCITY = 112; |
22 | 34 |
|
23 | | -DynamixelXM::DynamixelXM(const uint8_t id) |
24 | | - : dynamixel_base::DynamixelBase(id) { |
| 35 | +const double TO_ACCELERATION_REV_PER_MM = 214.577; |
| 36 | +const double TO_ACCELERATION_TO_RAD_PER_MM = TO_ACCELERATION_REV_PER_MM * 2.0 * M_PI; |
| 37 | +const double TO_ACCELERATION_TO_RAD_PER_SS = TO_ACCELERATION_TO_RAD_PER_MM / 3600.0; |
| 38 | +const double DXL_ACCELERATION_FROM_RAD_PER_SS = 1.0 / TO_ACCELERATION_TO_RAD_PER_SS; |
| 39 | +const int DXL_MAX_ACCELERATION = 32767; |
| 40 | +const double TO_VELOCITY_REV_PER_MIN = 0.229; |
| 41 | +const double TO_VELOCITY_RAD_PER_MIN = TO_VELOCITY_REV_PER_MIN * 2.0 * M_PI; |
| 42 | +const double TO_VELOCITY_RAD_PER_SEC = TO_VELOCITY_RAD_PER_MIN / 60.0; |
| 43 | +const double DXL_VELOCITY_FROM_RAD_PER_SEC = 1.0 / TO_VELOCITY_RAD_PER_SEC; |
| 44 | +const int DXL_MAX_VELOCITY = 32767; |
| 45 | +const double TO_RADIANS = (180.0 / 2048.0) * M_PI / 180.0; |
| 46 | +const double TO_CURRENT_AMPERE = 0.00269; |
| 47 | +const double TO_VOLTAGE_VOLT = 0.1; |
| 48 | +const double TO_DXL_POS = 1.0 / TO_RADIANS; |
| 49 | +const double TO_DXL_CURRENT = 1.0 / TO_CURRENT_AMPERE; |
| 50 | + |
| 51 | +DynamixelXM::DynamixelXM(const uint8_t id, const int home_position) |
| 52 | + : dynamixel_base::DynamixelBase(id), HOME_POSITION_(home_position) { |
25 | 53 | name_ = "XM"; |
26 | 54 | } |
27 | 55 |
|
| 56 | +bool DynamixelXM::read_operating_mode(const dynamixel_base::comm_t & comm, uint8_t & mode) { |
| 57 | + return comm->read_byte_data(id_, ADDR_OPERATING_MODE, mode); |
| 58 | +} |
| 59 | + |
| 60 | +bool DynamixelXM::write_operating_mode(const dynamixel_base::comm_t & comm, const uint8_t mode) { |
| 61 | + return comm->write_byte_data(id_, ADDR_OPERATING_MODE, mode); |
| 62 | +} |
| 63 | + |
| 64 | +bool DynamixelXM::read_current_limit( |
| 65 | + const dynamixel_base::comm_t & comm, double & limit_ampere) { |
| 66 | + uint16_t dxl_current_limit = 0; |
| 67 | + bool retval = comm->read_word_data(id_, ADDR_CURRENT_LIMIT, dxl_current_limit); |
| 68 | + limit_ampere = to_current_ampere(dxl_current_limit); |
| 69 | + return retval; |
| 70 | +} |
| 71 | + |
| 72 | +bool DynamixelXM::read_max_position_limit( |
| 73 | + const dynamixel_base::comm_t & comm, double & limit_radian) { |
| 74 | + uint16_t dxl_position_limit = 0; |
| 75 | + bool retval = comm->read_word_data(id_, ADDR_MAX_POSITION_LIMIT, dxl_position_limit); |
| 76 | + limit_radian = to_position_radian(dxl_position_limit); |
| 77 | + return retval; |
| 78 | +} |
| 79 | + |
| 80 | +bool DynamixelXM::read_min_position_limit( |
| 81 | + const dynamixel_base::comm_t & comm, double & limit_radian) { |
| 82 | + uint16_t dxl_position_limit = 0; |
| 83 | + bool retval = comm->read_word_data(id_, ADDR_MIN_POSITION_LIMIT, dxl_position_limit); |
| 84 | + limit_radian = to_position_radian(dxl_position_limit); |
| 85 | + return retval; |
| 86 | +} |
| 87 | + |
28 | 88 | bool DynamixelXM::write_torque_enable(const dynamixel_base::comm_t & comm, const bool enable) { |
29 | 89 | return comm->write_byte_data(id_, ADDR_TORQUE_ENABLE, enable); |
30 | 90 | } |
31 | 91 |
|
| 92 | +bool DynamixelXM::write_velocity_i_gain( |
| 93 | + const dynamixel_base::comm_t & comm, const unsigned int gain) { |
| 94 | + return comm->write_word_data(id_, ADDR_VELOCITY_I_GAIN, static_cast<uint16_t>(gain)); |
| 95 | +} |
| 96 | + |
| 97 | +bool DynamixelXM::write_velocity_p_gain( |
| 98 | + const dynamixel_base::comm_t & comm, const unsigned int gain) { |
| 99 | + return comm->write_word_data(id_, ADDR_VELOCITY_P_GAIN, static_cast<uint16_t>(gain)); |
| 100 | +} |
| 101 | + |
| 102 | +bool DynamixelXM::write_position_d_gain( |
| 103 | + const dynamixel_base::comm_t & comm, const unsigned int gain) { |
| 104 | + return comm->write_word_data(id_, ADDR_POSITION_D_GAIN, static_cast<uint16_t>(gain)); |
| 105 | +} |
| 106 | + |
| 107 | +bool DynamixelXM::write_position_i_gain( |
| 108 | + const dynamixel_base::comm_t & comm, const unsigned int gain) { |
| 109 | + return comm->write_word_data(id_, ADDR_POSITION_I_GAIN, static_cast<uint16_t>(gain)); |
| 110 | +} |
| 111 | + |
| 112 | +bool DynamixelXM::write_position_p_gain( |
| 113 | + const dynamixel_base::comm_t & comm, const unsigned int gain) { |
| 114 | + return comm->write_word_data(id_, ADDR_POSITION_P_GAIN, static_cast<uint16_t>(gain)); |
| 115 | +} |
| 116 | + |
| 117 | +bool DynamixelXM::write_profile_acceleration( |
| 118 | + const dynamixel_base::comm_t & comm, const double acceleration_rpss) { |
| 119 | + return comm->write_double_word_data( |
| 120 | + id_, |
| 121 | + ADDR_PROFILE_ACCELERATION, |
| 122 | + static_cast<uint32_t>(to_profile_acceleration(acceleration_rpss))); |
| 123 | +} |
| 124 | + |
| 125 | +bool DynamixelXM::write_profile_velocity( |
| 126 | + const dynamixel_base::comm_t & comm, const double velocity_rps) { |
| 127 | + return comm->write_double_word_data( |
| 128 | + id_, |
| 129 | + ADDR_PROFILE_VELOCITY, |
| 130 | + static_cast<uint32_t>(to_profile_velocity(velocity_rps))); |
| 131 | +} |
| 132 | + |
| 133 | +unsigned int DynamixelXM::to_profile_acceleration(const double acceleration_rpss) { |
| 134 | + int dxl_acceleration = DXL_ACCELERATION_FROM_RAD_PER_SS * acceleration_rpss; |
| 135 | + if (dxl_acceleration > DXL_MAX_ACCELERATION) { |
| 136 | + dxl_acceleration = DXL_MAX_ACCELERATION; |
| 137 | + } else if (dxl_acceleration <= 0) { |
| 138 | + // XMシリーズでは、'0'が最大加速度を意味する |
| 139 | + // よって、加速度の最小値は'1'である |
| 140 | + dxl_acceleration = 1; |
| 141 | + } |
| 142 | + |
| 143 | + return static_cast<unsigned int>(dxl_acceleration); |
| 144 | +} |
| 145 | + |
| 146 | +unsigned int DynamixelXM::to_profile_velocity(const double velocity_rps) { |
| 147 | + int dxl_velocity = DXL_VELOCITY_FROM_RAD_PER_SEC * velocity_rps; |
| 148 | + if (dxl_velocity > DXL_MAX_VELOCITY) { |
| 149 | + dxl_velocity = DXL_MAX_VELOCITY; |
| 150 | + } else if (dxl_velocity <= 0) { |
| 151 | + // XMシリーズでは、'0'が最大速度を意味する |
| 152 | + // よって、速度の最小値は'1'である |
| 153 | + dxl_velocity = 1; |
| 154 | + } |
| 155 | + |
| 156 | + return static_cast<unsigned int>(dxl_velocity); |
| 157 | +} |
| 158 | + |
| 159 | +double DynamixelXM::to_position_radian(const int position) { |
| 160 | + return (position - HOME_POSITION_) * TO_RADIANS; |
| 161 | +} |
| 162 | + |
| 163 | +double DynamixelXM::to_velocity_rps(const int velocity) { |
| 164 | + return velocity * TO_VELOCITY_RAD_PER_SEC; |
| 165 | +} |
| 166 | + |
| 167 | +double DynamixelXM::to_current_ampere(const int current) { |
| 168 | + return current * TO_CURRENT_AMPERE; |
| 169 | +} |
| 170 | + |
| 171 | +double DynamixelXM::to_voltage_volt(const int voltage) { |
| 172 | + return voltage * TO_VOLTAGE_VOLT; |
| 173 | +} |
| 174 | + |
| 175 | +unsigned int DynamixelXM::from_position_radian(const double position_rad) { |
| 176 | + return position_rad * TO_DXL_POS + HOME_POSITION_; |
| 177 | +} |
| 178 | + |
| 179 | +unsigned int DynamixelXM::from_velocity_rps(const double velocity_rps) { |
| 180 | + return velocity_rps * DXL_VELOCITY_FROM_RAD_PER_SEC; |
| 181 | +} |
| 182 | + |
| 183 | +unsigned int DynamixelXM::from_current_ampere(const double current_ampere) { |
| 184 | + return current_ampere * TO_DXL_CURRENT; |
| 185 | +} |
| 186 | + |
32 | 187 | } // namespace dynamixel_xm |
0 commit comments