Skip to content

Commit b02edd3

Browse files
Avoid idle sleep during lockstep steps (#8)
Co-authored-by: Jessie Li <jessieli@nvidia.com>
1 parent 4f3fc73 commit b02edd3

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

‎mujoco_ros2_control/src/mujoco_simulation.cpp‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,15 +1515,22 @@ void MujocoSimulation::physics_loop()
15151515
// run until asked to exit
15161516
while (!sim_->exitrequest.load())
15171517
{
1518-
// sleep for 1 ms or yield, to let main thread run
1519-
// yield results in busy wait - which has better timing but kills battery life
1520-
if (sim_->run && sim_->busywait)
1518+
// Sleep while idle, but do not add a mandatory 1 ms delay between
1519+
// controller-requested lockstep steps. A lockstep request may contain
1520+
// multiple physics steps, all of which must complete within one control
1521+
// period.
1522+
const bool lockstep_step_pending = !sim_->run && pending_steps_.load() > 0;
1523+
if (!lockstep_step_pending)
15211524
{
1522-
std::this_thread::yield();
1523-
}
1524-
else
1525-
{
1526-
std::this_thread::sleep_for(std::chrono::milliseconds(1));
1525+
// Yielding gives better timing while running, at the cost of CPU usage.
1526+
if (sim_->run && sim_->busywait)
1527+
{
1528+
std::this_thread::yield();
1529+
}
1530+
else
1531+
{
1532+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
1533+
}
15271534
}
15281535

15291536
{

0 commit comments

Comments
 (0)