Skip to content

Commit 046bffa

Browse files
Create and check fw device_ready flag before checking device_status
1 parent 7f530fb commit 046bffa

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

turtlebot3_node/include/turtlebot3_node/control_table.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ typedef struct
4646
ControlItem baud_rate = {8, EEPROM, 1, READ};
4747

4848
ControlItem millis = {10, RAM, 4, READ};
49-
ControlItem micros = {14, RAM, 4, READ};
49+
// ControlItem micros = {14, RAM, 4, READ}; // Does not match firmware
5050

51+
ControlItem device_ready = {17, RAM, 1, READ};
5152
ControlItem device_status = {18, RAM, 1, READ};
5253
ControlItem heartbeat = {19, RAM, 1, READ_WRITE};
5354

turtlebot3_node/src/turtlebot3.cpp

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,47 @@ void TurtleBot3::check_device_status()
114114
return;
115115
}
116116

117-
// Check if motors are connected, retry after longer interval if not
118-
const int8_t NOT_CONNECTED_MOTOR = -1;
119-
int8_t device_status = NOT_CONNECTED_MOTOR;
120-
for (int i = 0; i < 4; i++) {
121-
device_status = dxl_sdk_wrapper_->get_data_from_device<int8_t>(
122-
extern_control_table.device_status.addr,
123-
extern_control_table.device_status.length);
124-
125-
if (device_status == NOT_CONNECTED_MOTOR) {
126-
RCLCPP_INFO(this->get_logger(), "Motors not yet initialized, retrying in %d seconds", i+1);
127-
rclcpp::sleep_for(std::chrono::seconds(i+1));
128-
} else {
129-
RCLCPP_INFO(this->get_logger(), "Motors successfully initialized");
130-
break;
117+
// Wait for device to be fully ready before checking status
118+
bool device_ready = false;
119+
const int MAX_RETRIES = 4;
120+
int retries = 0;
121+
122+
while (!device_ready && retries < MAX_RETRIES) {
123+
try {
124+
device_ready = dxl_sdk_wrapper_->get_data_from_device<bool>(
125+
extern_control_table.device_ready.addr,
126+
extern_control_table.device_ready.length);
127+
128+
if (!device_ready) {
129+
RCLCPP_INFO(this->get_logger(), "Waiting %d seconds for device to become ready... (%d/%d)",
130+
retries+1, retries+1, MAX_RETRIES);
131+
rclcpp::sleep_for(std::chrono::seconds(retries+1));
132+
retries++;
131133
}
134+
} catch (...) {
135+
RCLCPP_WARN(this->get_logger(), "Failed to read device_ready flag, retrying in %d seconds... (%d/%d)",
136+
retries+1, retries+1, MAX_RETRIES);
137+
rclcpp::sleep_for(std::chrono::seconds(retries+1));
138+
retries++;
139+
}
132140
}
133-
if (device_status == NOT_CONNECTED_MOTOR) {
134-
RCLCPP_WARN(this->get_logger(), "Motors not initialized, please double check your Dynamixels and Power");
141+
142+
// Only check device_status if device is ready
143+
if (device_ready) {
144+
RCLCPP_INFO(this->get_logger(), "Device is ready, retrieving device status");
145+
int8_t device_status = dxl_sdk_wrapper_->get_data_from_device<int8_t>(
146+
extern_control_table.device_status.addr,
147+
extern_control_table.device_status.length);
148+
149+
// Check if motors are connected, retry after longer interval if not
150+
const int8_t NOT_CONNECTED_MOTOR = -1;
151+
if (device_status == NOT_CONNECTED_MOTOR) {
152+
RCLCPP_INFO(this->get_logger(), "Motors not initialized");
153+
} else {
154+
RCLCPP_INFO(this->get_logger(), "Motors successfully initialized");
155+
}
156+
} else {
157+
RCLCPP_WARN(this->get_logger(), "Device did not become ready in time, skipping device status check");
135158
}
136159
}
137160

0 commit comments

Comments
 (0)