I have recently been working with robotic systems that have a greater requirement for deterministic operation. While Ubuntu will likely never be an RTOS the PREEMPT RT patch does allow for soft real-time constraints on specified threads which can be enough of a step up for many research applications.
While there are other guides on how to do this online (some linked below) this post will present as a record for the steps I needed to follow recently for my system.
sudo apt-get install build-essential bc ca-certificates gnupg2 libssl-dev wget gawk flex bison dwarves
Supported Linux RT kernel patches found here. Check your kernel version and find an appropriate patch.
For my system this returns
5.15.0-76-generic so I’ll need Joseph Salisbury’s patch located here.
xz -dk patch-5.15.119-rt65.patch.xz
xz -d linux-5.15.119.tar.xz
You will need to get the appropriate keys for your patch maintainer.
# Main Kernel
gpg2 --locate-keys firstname.lastname@example.org email@example.com
# Patch Maintainer
gpg2 --locate-keys firstname.lastname@example.org
gpg2 --keyserver hkp://keyserver.ubuntu.com --search-keys "Joseph Salisbury"
gpg2 --verify linux-5.15.119.tar.sign
gpg2 --verify patch-5.15.119-rt65.patch.sign
Apply the Patch to the Kernel
tar xf linux-5.15.119.tar
xzcat ../patch-5.15.119-rt65.patch.xz | patch -p1
Select fully pre-emptable kernel option and just enter through the rest
cp /boot/config-5.15.0-76-generic .config
Activate “Fully Preemptible Kernel (Real-Time)” option from “General setup” / “Preemption Model” then SAVE and EXIT.
Apply this fix to the .config file.
Build and Install
Wait a long time…
sudo make modules_install
sudo make install
Real-Time User Privileges
Description of the limits conf on manpage here.
sudo groupadd realtime
sudo usermod -aG realtime $(whoami)
/etc/security/limits.conf to contain…
@realtime soft rtprio 99
@realtime soft priority 99
@realtime soft memlock 102400
@realtime hard rtprio 99
@realtime hard priority 99
@realtime hard memlock 102400
/etc/default/grub to contain…
Then reboot and choose the RT kernel from the Advanced Options for Ubuntu in Grub
Assigning Priorities to System Services
You can leverage the RT kernel by setting the priority of the threads spawned by your system services (see my previous post on running ROS nodes on boot). Use the
CPUSchedulingPriority directives under the Service section of a system service. Take a look at this guide from RedHat for a run down on the options, but for robotics I’d tend to
CPUSchedulingPolicy=rr and either
CPUSchedulingPriority=2-49, for CPU-bound tasks and
CPUSchedulingPriority=51-98 for I/O bound tasks, see the example usage below.
Resources and Further Reading
Found this useful or interesting?
Consider donating to support.
Any question, comments, corrections or suggestions?
Reach out on the social links below through the buttons.