@@ -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