Skip to content

Commit 0bb65ca

Browse files
committed
fix: Enhance error handling in Dynamixel::InitDxlComm and ReadItem methods
1 parent 8dfe465 commit 0bb65ca

File tree

1 file changed

+45
-42
lines changed

1 file changed

+45
-42
lines changed

src/dynamixel/dynamixel.cpp

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,17 @@ DxlError Dynamixel::InitDxlComm(
199199
if (dxl_comm_result != COMM_SUCCESS) {
200200
fprintf(stderr, " - COMM_ERROR : %s\n", packet_handler_->getTxRxResult(dxl_comm_result));
201201
return DxlError::CANNOT_FIND_CONTROL_ITEM;
202-
} else if (dxl_error != 0) {
202+
}
203+
204+
// First, read the model file to get the control table structure
205+
try {
206+
dxl_info_.ReadDxlModelFile(it_id, dxl_model_number);
207+
} catch (const std::exception & e) {
208+
fprintf(stderr, "[InitDxlComm][ID:%03d] Error reading model file: %s\n", it_id, e.what());
209+
return DxlError::CANNOT_FIND_CONTROL_ITEM;
210+
}
211+
212+
if (dxl_error != 0) {
203213
fprintf(stderr, " - RX_PACKET_ERROR : %s\n", packet_handler_->getRxPacketError(dxl_error));
204214

205215
// Check if Hardware Error Status control item exists
@@ -214,46 +224,38 @@ DxlError Dynamixel::InitDxlComm(
214224
uint32_t hw_error_status = 0;
215225
DxlError hw_error_result = ReadItem(it_id, "Hardware Error Status", hw_error_status);
216226

217-
if (hw_error_result == DxlError::OK) {
218-
fprintf(stderr, "[ID:%03d] Read Hardware Error Status : 0x%x\n", it_id, hw_error_status);
219-
220-
std::string error_string = "";
221-
uint8_t hw_error_byte = static_cast<uint8_t>(hw_error_status);
222-
223-
for (int bit = 0; bit < 8; ++bit) {
224-
if (hw_error_byte & (1 << bit)) {
225-
const HardwareErrorStatusBitInfo * bit_info = get_hardware_error_status_bit_info(bit);
226-
if (bit_info) {
227-
error_string += bit_info->label;
228-
error_string += " (" + std::string(bit_info->description) + ")/ ";
229-
} else {
230-
error_string += "Unknown Error Bit " + std::to_string(bit) + "/ ";
231-
}
227+
std::string error_string = "";
228+
uint8_t hw_error_byte = static_cast<uint8_t>(hw_error_status);
229+
230+
for (int bit = 0; bit < 8; ++bit) {
231+
if (hw_error_byte & (1 << bit)) {
232+
const HardwareErrorStatusBitInfo * bit_info = get_hardware_error_status_bit_info(bit);
233+
if (bit_info) {
234+
error_string += bit_info->label;
235+
error_string += " (" + std::string(bit_info->description) + ")/ ";
236+
} else {
237+
error_string += "Unknown Error Bit " + std::to_string(bit) + "/ ";
232238
}
233239
}
240+
}
234241

235-
if (!error_string.empty()) {
236-
fprintf(stderr, "[ID:%03d] Hardware Error Details: 0x%x (%d): %s\n",
237-
it_id, hw_error_byte, hw_error_byte, error_string.c_str());
238-
}
242+
if (!error_string.empty()) {
243+
fprintf(stderr, "[ID:%03d] Hardware Error Details: 0x%x (%d): %s\n",
244+
it_id, hw_error_byte, hw_error_byte, error_string.c_str());
239245
}
240246
} else if (error_code_exists) {
241247
uint32_t error_code = 0;
242248
DxlError error_code_result = ReadItem(it_id, "Error Code", error_code);
243249

244-
if (error_code_result == DxlError::OK) {
245-
fprintf(stderr, "[ID:%03d] Read Error Code : 0x%x\n", it_id, error_code);
246-
247-
uint8_t error_code_byte = static_cast<uint8_t>(error_code);
248-
if (error_code_byte != 0x00) {
249-
const ErrorCodeInfo * error_info = get_error_code_info(error_code_byte);
250-
if (error_info) {
251-
fprintf(stderr, "[ID:%03d] Error Code Details: 0x%x (%s): %s\n",
252-
it_id, error_code_byte, error_info->label, error_info->description);
253-
} else {
254-
fprintf(stderr, "[ID:%03d] Error Code Details: 0x%x (Unknown Error Code)\n",
255-
it_id, error_code_byte);
256-
}
250+
uint8_t error_code_byte = static_cast<uint8_t>(error_code);
251+
if (error_code_byte != 0x00) {
252+
const ErrorCodeInfo * error_info = get_error_code_info(error_code_byte);
253+
if (error_info) {
254+
fprintf(stderr, "[ID:%03d] Error Code Details: 0x%x (%s): %s\n",
255+
it_id, error_code_byte, error_info->label, error_info->description);
256+
} else {
257+
fprintf(stderr, "[ID:%03d] Error Code Details: 0x%x (Unknown Error Code)\n",
258+
it_id, error_code_byte);
257259
}
258260
}
259261
} else {
@@ -264,7 +266,6 @@ DxlError Dynamixel::InitDxlComm(
264266
);
265267
}
266268

267-
fprintf(stderr, "[ID:%03d] Hardware Error detected, rebooting...\n", it_id);
268269
Reboot(it_id);
269270
return DxlError::DXL_HARDWARE_ERROR;
270271
} else {
@@ -274,14 +275,6 @@ DxlError Dynamixel::InitDxlComm(
274275
model_name.c_str());
275276
}
276277

277-
// First, read the model file to get the control table structure
278-
try {
279-
dxl_info_.ReadDxlModelFile(it_id, dxl_model_number);
280-
} catch (const std::exception & e) {
281-
fprintf(stderr, "[InitDxlComm][ID:%03d] Error reading model file: %s\n", it_id, e.what());
282-
return DxlError::CANNOT_FIND_CONTROL_ITEM;
283-
}
284-
285278
// Read firmware version and reload model file with firmware-specific version if available
286279
uint8_t firmware_version = 0;
287280
DxlError fw_result = ReadFirmwareVersion(it_id, firmware_version);
@@ -784,6 +777,16 @@ DxlError Dynamixel::ReadItem(uint8_t id, std::string item_name, uint32_t & data)
784777
return DxlError::ITEM_READ_FAIL;
785778
}
786779
} else if (dxl_error != 0) {
780+
bool is_alert = dxl_error & 0x80;
781+
if (is_alert) {
782+
fprintf(
783+
stderr,
784+
"[ReadItem][ID:%03d][comm_id:%03d] RX_PACKET_ERROR : %s\n",
785+
id,
786+
comm_id,
787+
packet_handler_->getRxPacketError(dxl_error));
788+
return DxlError::OK;
789+
}
787790
fprintf(
788791
stderr,
789792
"[ReadItem][ID:%03d][comm_id:%03d] RX_PACKET_ERROR : %s (retry %d/%d)\n",

0 commit comments

Comments
 (0)