Two clocks, and the bug they cause
You will: Keep simulation time and wall time apart, and measure the real-time factor honestly.
Builds on A simulator that runs in CI.
A simulator keeps its own clock. When physics is fast, simulated time
runs at real speed; when it is not, simulated time runs slow. The ratio
is the real-time factor. Every node here uses simulated time, read
from /clock, which is why /clock is the one bridge between Gazebo
and ROS that is never optional.
The SO-101 on a CPU runs at roughly real time, when the machine is otherwise quiet. Do not read precision into that. Five consecutive samples of one unchanged run, with RViz drawing it, measured:
0.674 0.485 0.789 0.999 1.175
More than a factor of two between the lowest and the highest, from background load alone. An earlier version of this course published a headless figure and a GUI figure side by side, as though the difference had been measured. It had not — each was a single reading — and both were removed. One sample cannot tell you whether a change helped.
It used to be much slower, and the reason is worth knowing. Upstream's collision shapes are its full visual meshes, tens of thousands of triangles each, and with them the arm ran at about an eighth of real time. This project's overlay swaps each one for its bounding box, for the physics only; what you see is still upstream's mesh.
Now the bug. On this project's first robot, which ran at about a quarter of real time, a test scenario commanded the arm, slept 0.3 seconds of wall time, and checked the position once. At a real-time factor of 0.27, that is about 0.08 seconds of robot time — nowhere near enough for the arm to move. The arm reached its target exactly, measured error 0.0000 rad, and the test failed anyway, because it looked too early. On the SO-101 at real time the same sleep is still far shorter than a two-second motion.
The fix is to stop asking "has enough time passed?" and ask "has the thing happened?" — poll the post-condition until it holds, with a generous timeout as a backstop. That is correct at any real-time factor, on a fast laptop or a starved CI runner. Timeouts in the robot client went the same way: they spend simulated seconds, with a wall-clock backstop so a frozen simulator cannot hang the caller forever.
In the simulation
This page's header.stamp counts wall seconds since it loaded; in Gazebo the same field is simulated time from /clock. Each task here is a post-condition the page polls: it completes when the arm is there and still, however long that takes, not after a fixed wait.
Kinematic: joints move at their real speed limit, but there is no gravity, no load and no physics. That is what the steps below are for.
The SO-101 loads as you reach it.
make drive keysIn this lesson
- Open Inspect, then /joint_states, and watch
header.stamp.seccount. - Press rest. The task completes only once the arm has arrived and stopped.
- Press
h(or home) and count. A home is a 2 s move; a check made 0.3 s after the key press would fail on a perfect arm.
Joints · SO-101 follower
1–5 joint · , . jog · [ ] jaw · - + step · h home · f frames
the keys of make drive
The buttons do what the keys do: hold one to repeat.
Limits from the description; speed capped at 1.7434 rad/s, the servo's datasheet derated. Poses from the profile.
Inspector
Code
The repository's own code behind what you do here appears once you move the arm.
Do it
With
make playrunning, sample the real-time factor five times.make shell gz topic -e -t /world/empty_bench/stats -n 5 | grep real_time_factorExpect: Five numbers, probably between about 0.5 and 1.2, and not one of them the same as the last.
Read the docstring that records the bug, and the polling loop that fixed it.
grep -n -A28 "def step" src/threevn_control/threevn_control/scenarios/base.pyFind the client's simulated-time wait.
grep -n -A25 "def _wait" src/threevn_control/threevn_control/robot_client.py
When it goes wrong
A test passes on a fast machine and fails on a slow one, or in CI.
Why: Something waited a fixed wall-clock time and assumed the robot had moved.
Fix: Poll the post-condition with a timeout. Never sleep and check once.
A trajectory looks like it undershoots.
Why: Often the real-time factor, not the controller — three wall seconds is about one robot second.
Fix: Check the real-time factor before tuning anything.
Terms
- Simulation time
- The simulator's own clock, published on
/clock. Nodes use it whenuse_sim_timeis set. - Real-time factor (RTF)
- Simulated seconds per wall-clock second. Around 1 for the SO-101 on a quiet CPU, and noisy.
- Polling a post-condition
- Repeatedly checking whether the expected state has been reached, until it has or a timeout expires — instead of waiting a fixed time and checking once.
Every term in the course: glossary.
Check yourself
Answer in your head first, then open it.
A step sleeps 0.3 s of wall time and checks once. The real-time factor is 0.27. How much robot time has passed?
About 0.08 seconds — far too little for a motion lasting a second or two.
Polling makes correct tests take longer to fail. Why is that an acceptable trade?
A correct pass is fast at any speed; only a genuine failure waits out the timeout. Slow-to-fail is a cost you pay rarely; a wrong result is a cost you pay every time.
Why can one real-time-factor reading not tell you whether a change helped?
Back-to-back samples of an unchanged run here varied by more than a factor of two. A single before-and-after pair is inside the noise.
Stretch
Take five samples with RViz running and five with make play RVIZ=0. Compute the mean and spread of each. Is the difference bigger than the spread? Write one sentence you would be willing to publish about what RViz costs — and nothing stronger.