Skip to content

Commit 5cc5b12

Browse files
authored
Merge pull request #7 from ROBOTIS-GIT/feature-timeout
Enhance Error Handling and Timeout Management
2 parents ddd6fac + c636183 commit 5cc5b12

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

include/dynamixel_hardware_interface/dynamixel_hardware_interface.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,12 @@ class DynamixelHardware : public
181181
std::map<uint8_t /*id*/, uint8_t /*err*/> dxl_hw_err_;
182182
DxlTorqueStatus dxl_torque_status_;
183183
std::map<uint8_t /*id*/, bool /*enable*/> dxl_torque_state_;
184-
double err_timeout_sec_;
184+
double err_timeout_ms_;
185+
rclcpp::Duration read_error_duration_{0, 0};
186+
rclcpp::Duration write_error_duration_{0, 0};
187+
bool is_read_in_error_{false};
188+
bool is_write_in_error_{false};
189+
185190
bool use_revolute_to_prismatic_{false};
186191
std::string conversion_dxl_name_{""};
187192
std::string conversion_joint_name_{""};

src/dynamixel_hardware_interface.cpp

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ DynamixelHardware::DynamixelHardware()
3535
{
3636
dxl_status_ = DXL_OK;
3737
dxl_torque_status_ = TORQUE_ENABLED;
38-
err_timeout_sec_ = 3.0;
38+
err_timeout_ms_ = 500;
39+
is_read_in_error_ = false;
40+
is_write_in_error_ = false;
41+
read_error_duration_ = rclcpp::Duration(0, 0);
42+
write_error_duration_ = rclcpp::Duration(0, 0);
3943
}
4044

4145
DynamixelHardware::~DynamixelHardware()
@@ -64,7 +68,11 @@ hardware_interface::CallbackReturn DynamixelHardware::on_init(
6468

6569
port_name_ = info_.hardware_parameters["port_name"];
6670
baud_rate_ = info_.hardware_parameters["baud_rate"];
67-
err_timeout_sec_ = stod(info_.hardware_parameters["error_timeout_sec"]);
71+
try {
72+
err_timeout_ms_ = stod(info_.hardware_parameters["error_timeout_ms"]);
73+
} catch (const std::exception& e) {
74+
RCLCPP_ERROR(logger_, "Failed to parse error_timeout_ms parameter: %s, using default value", e.what());
75+
}
6876

6977
RCLCPP_INFO_STREAM(
7078
logger_,
@@ -394,11 +402,23 @@ hardware_interface::return_type DynamixelHardware::read(
394402
} else if (dxl_status_ == DXL_OK || dxl_status_ == COMM_ERROR) {
395403
dxl_comm_err_ = CheckError(dxl_comm_->ReadMultiDxlData());
396404
if (dxl_comm_err_ != DxlError::OK) {
405+
if (!is_read_in_error_) {
406+
is_read_in_error_ = true;
407+
read_error_duration_ = rclcpp::Duration(0, 0);
408+
}
409+
read_error_duration_ = read_error_duration_ + period;
410+
397411
RCLCPP_ERROR_STREAM(
398412
logger_,
399-
"Dynamixel Read Fail :" << Dynamixel::DxlErrorToString(dxl_comm_err_));
400-
return hardware_interface::return_type::ERROR;
413+
"Dynamixel Read Fail (Duration: " << read_error_duration_.seconds() * 1000 << "ms/" << err_timeout_ms_ << "ms)");
414+
415+
if (read_error_duration_.seconds() * 1000 >= err_timeout_ms_) {
416+
return hardware_interface::return_type::ERROR;
417+
}
418+
return hardware_interface::return_type::OK;
401419
}
420+
is_read_in_error_ = false;
421+
read_error_duration_ = rclcpp::Duration(0, 0);
402422
} else if (dxl_status_ == HW_ERROR) {
403423
dxl_comm_err_ = CheckError(dxl_comm_->ReadMultiDxlData());
404424
if (dxl_comm_err_ != DxlError::OK) {
@@ -434,7 +454,6 @@ hardware_interface::return_type DynamixelHardware::read(
434454
}
435455
return hardware_interface::return_type::OK;
436456
}
437-
438457
hardware_interface::return_type DynamixelHardware::write(
439458
const rclcpp::Time & time, const rclcpp::Duration & period)
440459
{
@@ -447,16 +466,28 @@ hardware_interface::return_type DynamixelHardware::write(
447466

448467
dxl_comm_->WriteMultiDxlData();
449468

469+
is_write_in_error_ = false;
470+
write_error_duration_ = rclcpp::Duration(0, 0);
471+
450472
return hardware_interface::return_type::OK;
451473
} else {
452-
RCLCPP_ERROR_STREAM(logger_, "Dynamixel Write Fail");
453-
return hardware_interface::return_type::ERROR;
474+
write_error_duration_ = write_error_duration_ + period;
475+
476+
RCLCPP_ERROR_STREAM(
477+
logger_,
478+
"Dynamixel Write Fail (Duration: " << write_error_duration_.seconds() * 1000 << "ms/" << err_timeout_ms_ << "ms)");
479+
480+
if (write_error_duration_.seconds() * 1000 >= err_timeout_ms_) {
481+
return hardware_interface::return_type::ERROR;
482+
}
483+
return hardware_interface::return_type::OK;
454484
}
455485
}
456486

457487
DxlError DynamixelHardware::CheckError(DxlError dxl_comm_err)
458488
{
459489
DxlError error_state = DxlError::OK;
490+
dxl_status_ = DXL_OK;
460491

461492
// check comm error
462493
if (dxl_comm_err != DxlError::OK) {

0 commit comments

Comments
 (0)