ROS 2 Lyrical Luth · Lesson 73 of 86
Real-time ROS 2
Real-time kernels, priorities, memory locking and avoiding allocation in the loop.
Estimated time · 60–90 min
Learning goals
What you will understand
- Explain real-time ros 2 in your own words
- Use it in a small robotics example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
Real-time means meeting a deadline every cycle, not being fast on average. On Linux use a PREEMPT_RT kernel, run the control thread with a real-time scheduling policy (SCHED_FIFO) at a fixed priority, lock memory with mlockall so it is never paged out, and pre-allocate everything before the loop starts.
Inside the loop avoid heap allocation, blocking calls, exceptions, logging and locks that other non-real-time threads hold. The ros2_control controller manager follows this design: read, update and write run in a real-time thread. Measure with cyclictest and your own jitter histogram; do not claim hard real-time without measurements.
Guided lesson
Work through these steps
- 1.
Source the ROS 2 Lyrical environment in a fresh terminal.
- 2.
Read the real-time ros 2 example and identify what each line does.
- 3.
Build only the affected package, source the workspace overlay, and run the example.
- 4.
Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.
Worked example
Read it before you run it
#include <sys/mman.h>
// Header for mlockall.
#include <pthread.h>
// Header for thread scheduling.
mlockall(MCL_CURRENT | MCL_FUTURE);
// Keep all present and future memory in RAM.
sched_param sp{}; sp.sched_priority = 80;
// Ask for real-time priority 80.
pthread_setschedparam(pthread_self(), SCHED_FIFO, &sp);
// Switch this thread to the SCHED_FIFO policy (needs permission, see limits.conf).
std::vector<double> buffer; buffer.reserve(1024);
// Allocate before the loop, never inside it.
// Loop: read sensors, compute, write actuators, sleep_until(next_deadline)
// The steady 1 kHz-style cycle.What the example is doing
- 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
- 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
- 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.
Watch it run · The robot's computer
See what this code does
The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.
Demo for Real-time ROS 2. Expected result: cyclictest reports the maximum latency in microseconds
Practice code
Now type it yourself
Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.
# Allow real-time priorities for your user (add to /etc/security/limits.conf, then log in again):
# youruser - rtprio 99
# youruser - memlock unlimited
# TODO: run cyclictest with memory locking (-m), SCHED_FIFO priority 80 (-p80), and a 1000 microsecond interval (-i1000)Expected output
cyclictest reports the maximum latency in microsecondsStuck? Show the full solution (try it yourself first)
# Allow real-time priorities for your user (add to /etc/security/limits.conf, then log in again):
# youruser - rtprio 99
# youruser - memlock unlimited
sudo cyclictest -m -p80 -i1000 -l10000Expected result
How you know it worked
- ROS_DISTRO is lyrical in every terminal used for the exercise.
- The complete package from the exercise builds and the expected graph entity appears in a second terminal.
Practical exercise
Apply the lesson
Run a 1 kHz loop for 60 seconds on a normal kernel and on PREEMPT_RT (if available), recording each cycle's lateness. Report worst case and 99th percentile, and note which allocations you removed from the loop.
Before you continue
- ROS_DISTRO reports lyrical in every terminal you used
- The example builds and runs without errors
- You checked the result with a ros2 command, RViz2 or a test, not just by reading the code
If something goes wrong
Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.
Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.