|
22 | 22 | #include <memory>
|
23 | 23 | #include <vector>
|
24 | 24 | #include <string>
|
| 25 | +#include <map> |
25 | 26 |
|
26 | 27 | #include "hardware_interface/types/hardware_interface_type_values.hpp"
|
27 | 28 | #include "rclcpp/rclcpp.hpp"
|
@@ -76,17 +77,6 @@ hardware_interface::CallbackReturn DynamixelHardware::on_init(
|
76 | 77 | e.what());
|
77 | 78 | }
|
78 | 79 |
|
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 |
| - |
90 | 80 | RCLCPP_INFO_STREAM(
|
91 | 81 | logger_,
|
92 | 82 | "port_name " << port_name_.c_str() << " / baudrate " << baud_rate_.c_str());
|
@@ -419,11 +409,15 @@ hardware_interface::CallbackReturn DynamixelHardware::start()
|
419 | 409 | }
|
420 | 410 | usleep(500 * 1000);
|
421 | 411 |
|
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 | + } |
426 | 418 | }
|
| 419 | + |
| 420 | + dxl_comm_->DynamixelEnable(torque_enabled_ids); |
427 | 421 |
|
428 | 422 | RCLCPP_INFO_STREAM(logger_, "Dynamixel Hardware Start!");
|
429 | 423 |
|
@@ -666,21 +660,47 @@ bool DynamixelHardware::initItems(const std::string & type_filter)
|
666 | 660 | }
|
667 | 661 | uint8_t id = static_cast<uint8_t>(stoi(gpio.parameters.at("ID")));
|
668 | 662 |
|
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)))) { |
673 | 683 | return false;
|
674 | 684 | }
|
675 | 685 | }
|
676 | 686 | }
|
677 | 687 |
|
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; |
684 | 704 | }
|
685 | 705 | }
|
686 | 706 | }
|
|
0 commit comments