Skip to content

Commit 2459626

Browse files
committed
feat: Maintain the program until timeout
1 parent ddd6fac commit 2459626

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-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: 34 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,7 @@ 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+
err_timeout_ms_ = stod(info_.hardware_parameters["error_timeout_ms"]);
6872

6973
RCLCPP_INFO_STREAM(
7074
logger_,
@@ -394,11 +398,23 @@ hardware_interface::return_type DynamixelHardware::read(
394398
} else if (dxl_status_ == DXL_OK || dxl_status_ == COMM_ERROR) {
395399
dxl_comm_err_ = CheckError(dxl_comm_->ReadMultiDxlData());
396400
if (dxl_comm_err_ != DxlError::OK) {
401+
if (!is_read_in_error_) {
402+
is_read_in_error_ = true;
403+
read_error_duration_ = rclcpp::Duration(0, 0);
404+
}
405+
read_error_duration_ = read_error_duration_ + period;
406+
397407
RCLCPP_ERROR_STREAM(
398408
logger_,
399-
"Dynamixel Read Fail :" << Dynamixel::DxlErrorToString(dxl_comm_err_));
400-
return hardware_interface::return_type::ERROR;
409+
"Dynamixel Read Fail (Duration: " << read_error_duration_.seconds() * 1000 << "ms/" << err_timeout_ms_ << "ms)");
410+
411+
if (read_error_duration_.seconds() * 1000 >= err_timeout_ms_) {
412+
return hardware_interface::return_type::ERROR;
413+
}
414+
return hardware_interface::return_type::OK;
401415
}
416+
is_read_in_error_ = false;
417+
read_error_duration_ = rclcpp::Duration(0, 0);
402418
} else if (dxl_status_ == HW_ERROR) {
403419
dxl_comm_err_ = CheckError(dxl_comm_->ReadMultiDxlData());
404420
if (dxl_comm_err_ != DxlError::OK) {
@@ -434,7 +450,6 @@ hardware_interface::return_type DynamixelHardware::read(
434450
}
435451
return hardware_interface::return_type::OK;
436452
}
437-
438453
hardware_interface::return_type DynamixelHardware::write(
439454
const rclcpp::Time & time, const rclcpp::Duration & period)
440455
{
@@ -447,16 +462,28 @@ hardware_interface::return_type DynamixelHardware::write(
447462

448463
dxl_comm_->WriteMultiDxlData();
449464

465+
is_write_in_error_ = false;
466+
write_error_duration_ = rclcpp::Duration(0, 0);
467+
450468
return hardware_interface::return_type::OK;
451469
} else {
452-
RCLCPP_ERROR_STREAM(logger_, "Dynamixel Write Fail");
453-
return hardware_interface::return_type::ERROR;
470+
write_error_duration_ = write_error_duration_ + period;
471+
472+
RCLCPP_ERROR_STREAM(
473+
logger_,
474+
"Dynamixel Write Fail (Duration: " << write_error_duration_.seconds() * 1000 << "ms/" << err_timeout_ms_ << "ms)");
475+
476+
if (write_error_duration_.seconds() * 1000 >= err_timeout_ms_) {
477+
return hardware_interface::return_type::ERROR;
478+
}
479+
return hardware_interface::return_type::OK;
454480
}
455481
}
456482

457483
DxlError DynamixelHardware::CheckError(DxlError dxl_comm_err)
458484
{
459485
DxlError error_state = DxlError::OK;
486+
dxl_status_ = DXL_OK;
460487

461488
// check comm error
462489
if (dxl_comm_err != DxlError::OK) {

0 commit comments

Comments
 (0)