Skip to content

Commit 6fedcc5

Browse files
committed
refactor: reorganize dxl_comm_ member for safe destruction and improve error handling in stop method
1 parent 610fb3f commit 6fedcc5

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

include/dynamixel_hardware_interface/dynamixel_hardware_interface.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ class DynamixelHardware : public
224224
bool CommReset();
225225

226226
///// dxl variable
227-
std::shared_ptr<Dynamixel> dxl_comm_;
228227
std::string port_name_;
229228
std::string baud_rate_;
230229
std::vector<uint8_t> dxl_id_;
@@ -369,6 +368,9 @@ class DynamixelHardware : public
369368
const std::string & conversion_iface = "",
370369
const std::string & conversion_name = "",
371370
std::function<double(double)> conversion = nullptr);
371+
372+
// Move dxl_comm_ to the end for safe destruction order
373+
std::shared_ptr<Dynamixel> dxl_comm_;
372374
};
373375

374376
// Conversion maps between ROS2 and Dynamixel interface names

src/dynamixel/dynamixel.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Dynamixel::Dynamixel(const char * path)
4444

4545
Dynamixel::~Dynamixel()
4646
{
47+
fprintf(stderr, "Dynamixel destructor start\n");
4748
if (group_sync_read_) {
4849
delete group_sync_read_;
4950
group_sync_read_ = nullptr;
@@ -73,11 +74,8 @@ Dynamixel::~Dynamixel()
7374
delete port_handler_;
7475
port_handler_ = nullptr;
7576
}
76-
if (packet_handler_) {
77-
delete packet_handler_;
78-
packet_handler_ = nullptr;
79-
}
80-
fprintf(stderr, "closed port\n");
77+
packet_handler_ = nullptr;
78+
fprintf(stderr, "Dynamixel destructor end\n");
8179
}
8280

8381
DxlError Dynamixel::InitDxlComm(
@@ -1301,11 +1299,15 @@ DxlError Dynamixel::GetDxlValueFromBulkRead(double period_ms)
13011299
"FastBulkRead failed 10 times, switching to normal BulkRead permanently.\n");
13021300
use_fast_read_protocol_ = false;
13031301
// Set up normal bulk read handler
1304-
std::vector<uint8_t> id_arr;
1302+
std::vector<uint8_t> indirect_id_arr;
1303+
13051304
for (auto it_read_data : read_data_list_) {
1306-
id_arr.push_back(it_read_data.id);
1305+
if (CheckIndirectReadAvailable(it_read_data.id) == DxlError::OK) {
1306+
indirect_id_arr.push_back(it_read_data.id);
1307+
}
13071308
}
1308-
SetBulkReadHandler(id_arr);
1309+
1310+
SetBulkReadHandler(indirect_id_arr);
13091311
}
13101312
// Return error for this attempt
13111313
return DxlError::BULK_READ_FAIL;

src/dynamixel_hardware_interface.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,12 @@ hardware_interface::CallbackReturn DynamixelHardware::start()
475475

476476
hardware_interface::CallbackReturn DynamixelHardware::stop()
477477
{
478-
dxl_comm_->DynamixelDisable(dxl_id_);
478+
if (dxl_comm_) {
479+
dxl_comm_->DynamixelDisable(dxl_id_);
480+
} else {
481+
RCLCPP_ERROR_STREAM(logger_, "Dynamixel Hardware Stop Fail : dxl_comm_ is nullptr");
482+
return hardware_interface::CallbackReturn::ERROR;
483+
}
479484

480485
RCLCPP_INFO_STREAM(logger_, "Dynamixel Hardware Stop!");
481486

0 commit comments

Comments
 (0)