Skip to content

Commit 5c46f10

Browse files
committed
Refactored torque management in Dynamixel interface
- Introduced a new map for torque enable states per Dynamixel ID. - Removed global torque enable variable and updated logic to enable torque based on individual settings. - Adjusted parameter handling to include torque enable settings during initialization. - Updated model files to include additional parameters for current readings.
1 parent 541632c commit 5c46f10

File tree

4 files changed

+51
-28
lines changed

4 files changed

+51
-28
lines changed

include/dynamixel_hardware_interface/dynamixel_hardware_interface.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,13 @@ class DynamixelHardware : public
185185
std::map<uint8_t /*id*/, uint8_t /*err*/> dxl_hw_err_;
186186
DxlTorqueStatus dxl_torque_status_;
187187
std::map<uint8_t /*id*/, bool /*enable*/> dxl_torque_state_;
188+
std::map<uint8_t /*id*/, bool /*enable*/> dxl_torque_enable_;
188189
double err_timeout_ms_;
189190
rclcpp::Duration read_error_duration_{0, 0};
190191
rclcpp::Duration write_error_duration_{0, 0};
191192
bool is_read_in_error_{false};
192193
bool is_write_in_error_{false};
193194

194-
bool global_torque_enable_{true};
195-
196195
bool use_revolute_to_prismatic_{false};
197196
std::string conversion_dxl_name_{""};
198197
std::string conversion_joint_name_{""};

param/dxl_model/ph42_020_s300.model

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ Address Size Data Name
6262
592 2 Present Input Voltage
6363
594 1 Present Temperature
6464
878 1 Backup Ready
65+
168 2 Indirect Address 1
66+
634 1 Indirect Data 1
6567
168 2 Indirect Address Write
6668
634 1 Indirect Data Write
67-
350 2 Indirect Address Read
69+
296 2 Indirect Address Read
6870
698 1 Indirect Data Read

param/dxl_model/xl430_w250.model

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Address Size Data Name
4343
98 1 Bus Watchdog
4444
100 2 Goal PWM
4545
104 4 Goal Velocity
46+
104 2 Goal Current
4647
108 4 Profile Acceleration
4748
112 4 Profile Velocity
4849
116 4 Goal Position
@@ -52,6 +53,7 @@ Address Size Data Name
5253
124 2 Present PWM
5354
126 2 Present Load
5455
128 4 Present Velocity
56+
128 2 Present Current
5557
132 4 Present Position
5658
136 4 Velocity Trajectory
5759
140 4 Position Trajectory

src/dynamixel_hardware_interface.cpp

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <memory>
2323
#include <vector>
2424
#include <string>
25+
#include <map>
2526

2627
#include "hardware_interface/types/hardware_interface_type_values.hpp"
2728
#include "rclcpp/rclcpp.hpp"
@@ -76,17 +77,6 @@ hardware_interface::CallbackReturn DynamixelHardware::on_init(
7677
e.what());
7778
}
7879

79-
try {
80-
global_torque_enable_ =
81-
std::stoi(info_.hardware_parameters["torque_enable"]) != 0;
82-
RCLCPP_INFO_STREAM(
83-
logger_, "Torque enable parameter: " << global_torque_enable_);
84-
} catch (const std::exception & e) {
85-
RCLCPP_ERROR(
86-
logger_, "Failed to parse torque_enable parameter: %s, using default value",
87-
e.what());
88-
}
89-
9080
RCLCPP_INFO_STREAM(
9181
logger_,
9282
"port_name " << port_name_.c_str() << " / baudrate " << baud_rate_.c_str());
@@ -419,11 +409,15 @@ hardware_interface::CallbackReturn DynamixelHardware::start()
419409
}
420410
usleep(500 * 1000);
421411

422-
if (global_torque_enable_) {
423-
dxl_comm_->DynamixelEnable(dxl_id_);
424-
} else {
425-
RCLCPP_INFO_STREAM(logger_, "Global Torque is disabled!");
412+
// Enable torque only for Dynamixels that have torque enabled in their parameters
413+
std::vector<uint8_t> torque_enabled_ids;
414+
for (const auto& [id, enabled] : dxl_torque_enable_) {
415+
if (enabled) {
416+
torque_enabled_ids.push_back(id);
417+
}
426418
}
419+
420+
dxl_comm_->DynamixelEnable(torque_enabled_ids);
427421

428422
RCLCPP_INFO_STREAM(logger_, "Dynamixel Hardware Start!");
429423

@@ -666,21 +660,47 @@ bool DynamixelHardware::initItems(const std::string & type_filter)
666660
}
667661
uint8_t id = static_cast<uint8_t>(stoi(gpio.parameters.at("ID")));
668662

669-
// First write items containing "Limit"
670-
for (auto it : gpio.parameters) {
671-
if (it.first != "ID" && it.first != "type" && it.first.find("Limit") != std::string::npos) {
672-
if (!retryWriteItem(id, it.first, static_cast<uint32_t>(stoi(it.second)))) {
663+
// Handle torque enable parameter
664+
bool torque_enabled = true; // Default to enabled
665+
if (gpio.parameters.find("Torque Enable") != gpio.parameters.end()) {
666+
torque_enabled = std::stoi(gpio.parameters.at("Torque Enable")) != 0;
667+
}
668+
dxl_torque_enable_[id] = torque_enabled;
669+
670+
// Write parameters in two passes:
671+
// 1. First pass: Write all Limit parameters
672+
for (const auto& param : gpio.parameters) {
673+
const std::string& param_name = param.first;
674+
675+
// Skip special parameters
676+
if (param_name == "ID" || param_name == "type" || param_name == "Torque Enable") {
677+
continue;
678+
}
679+
680+
// Write Limit parameters first
681+
if (param_name.find("Limit") != std::string::npos) {
682+
if (!retryWriteItem(id, param_name, static_cast<uint32_t>(stoi(param.second)))) {
673683
return false;
674684
}
675685
}
676686
}
677687

678-
// Then write the remaining items
679-
for (auto it : gpio.parameters) {
680-
if (it.first != "ID" && it.first != "type" && it.first.find("Limit") == std::string::npos) {
681-
if (!retryWriteItem(id, it.first, static_cast<uint32_t>(stoi(it.second)))) {
682-
return false;
683-
}
688+
// 2. Second pass: Write all non-Limit parameters
689+
for (const auto& param : gpio.parameters) {
690+
const std::string& param_name = param.first;
691+
692+
// Skip special parameters
693+
if (param_name == "ID" || param_name == "type" || param_name == "Torque Enable") {
694+
continue;
695+
}
696+
697+
// Skip Limit parameters (already written)
698+
if (param_name.find("Limit") != std::string::npos) {
699+
continue;
700+
}
701+
702+
if (!retryWriteItem(id, param_name, static_cast<uint32_t>(stoi(param.second)))) {
703+
return false;
684704
}
685705
}
686706
}

0 commit comments

Comments
 (0)