Skip to content

Commit 6e1ed52

Browse files
committed
refactor: Replace sleep functions with std::this_thread::sleep_for for better readability and consistency
1 parent bf0452f commit 6e1ed52

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/dynamixel/dynamixel.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <string>
2222
#include <memory>
2323
#include <functional>
24+
#include <chrono>
2425

2526
namespace dynamixel_hardware_interface
2627
{
@@ -564,6 +565,7 @@ DxlError Dynamixel::WriteItem(uint8_t id, uint16_t addr, uint8_t size, uint32_t
564565
} else {
565566
return DxlError::OK;
566567
}
568+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
567569
}
568570
fprintf(stderr, "MAX_COMM_RETRIES should be set to 1 or more\n");
569571
return DxlError::ITEM_WRITE_FAIL;

src/dynamixel_hardware_interface.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ hardware_interface::CallbackReturn DynamixelHardware::on_init(
160160
RCLCPP_INFO_STREAM(logger_, "Trying to connect to the communication port...");
161161
break;
162162
} else {
163-
sleep(1);
163+
std::this_thread::sleep_for(std::chrono::seconds(1));
164164
if (i == 9) {
165165
RCLCPP_ERROR_STREAM(logger_, "Cannot connect communication port! :(");
166166
return hardware_interface::CallbackReturn::ERROR;
@@ -186,7 +186,7 @@ hardware_interface::CallbackReturn DynamixelHardware::on_init(
186186
RCLCPP_INFO_STREAM(logger_, "Trying to connect to the communication port...");
187187
break;
188188
} else {
189-
sleep(1);
189+
std::this_thread::sleep_for(std::chrono::seconds(1));
190190
if (i == 9) {
191191
RCLCPP_ERROR_STREAM(logger_, "Cannot connect communication port! :(");
192192
return hardware_interface::CallbackReturn::ERROR;
@@ -499,7 +499,7 @@ hardware_interface::CallbackReturn DynamixelHardware::start()
499499

500500
// sync commands = states joint
501501
SyncJointCommandWithStates();
502-
usleep(500 * 1000);
502+
std::this_thread::sleep_for(std::chrono::milliseconds(500));
503503

504504
// Enable torque only for Dynamixels that have torque enabled in their parameters
505505
std::vector<uint8_t> torque_enabled_ids;
@@ -516,7 +516,7 @@ hardware_interface::CallbackReturn DynamixelHardware::start()
516516
break;
517517
}
518518
RCLCPP_ERROR_STREAM(logger_, "Failed to enable torque for Dynamixels, retry...");
519-
usleep(100 * 1000);
519+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
520520
}
521521
}
522522

@@ -703,7 +703,7 @@ bool DynamixelHardware::CommReset()
703703

704704
auto start_time = this->now();
705705
while ((this->now() - start_time) < rclcpp::Duration(3, 0)) {
706-
usleep(200 * 1000);
706+
std::this_thread::sleep_for(std::chrono::milliseconds(200));
707707
RCLCPP_INFO_STREAM(logger_, "Reset Start");
708708
bool result = true;
709709
for (auto id : dxl_id_) {
@@ -712,7 +712,7 @@ bool DynamixelHardware::CommReset()
712712
result = false;
713713
break;
714714
}
715-
usleep(200 * 1000);
715+
std::this_thread::sleep_for(std::chrono::milliseconds(200));
716716
}
717717
if (!result) {continue;}
718718
if (!InitControllerItems()) {continue;}
@@ -721,13 +721,13 @@ bool DynamixelHardware::CommReset()
721721
if (!InitDxlWriteItems()) {continue;}
722722

723723
RCLCPP_INFO_STREAM(logger_, "RESET Success");
724-
usleep(1000 * 1000);
724+
std::this_thread::sleep_for(std::chrono::seconds(1));
725725
start();
726726
dxl_status_ = DXL_OK;
727727
return true;
728728
}
729729
RCLCPP_ERROR_STREAM(logger_, "RESET Failure");
730-
usleep(1000 * 1000);
730+
std::this_thread::sleep_for(std::chrono::seconds(1));
731731
start();
732732
return false;
733733
}

0 commit comments

Comments
 (0)