This guide details the steps to set up a Machinekit installation with an EtherCAT master on a Raspberry Pi (3B, 4B, or newer) using a real-time (RT-Preempt) patched Linux kernel.
The main steps are:
- Install Raspberry Pi OS.
- Build and install a real-time Linux kernel.
- Install the IgH EtherCAT master.
- Set up Machinekit.
- Configure and test the system.
Start by installing the latest version of Raspberry Pi OS on a microSD card. Use the official Raspberry Pi Imager for an easy installation. A "Lite" version is sufficient if you plan to run the Pi headless.
After installation, boot the Raspberry Pi, connect it to the network, and ensure you can access it via SSH.
First, install the necessary tools for kernel building.
sudo apt update
sudo apt -y install git bc bison flex libssl-dev libncurses-devNext, download the Raspberry Pi kernel sources. We'll use a specific long-term support (LTS) branch. As of this writing, rpi-6.1.y is a good choice.
cd /usr/src
# Check for the latest stable branch on the repository page if needed
git clone -b 'rpi-6.1.y' --single-branch --depth 1 https://github.com/raspberrypi/linux.gitDownload the corresponding RT-Preempt patch for your kernel version. You can find the kernel version in the Makefile.
cd /usr/src/linux
KERNEL_VERSION=$(make kernelversion)
echo "Kernel version is ${KERNEL_VERSION}"Find the matching patch on the official kernel.org site. For example, for kernel 6.1.x, you would look in the 6.1 folder.
# Example for a 6.1.x kernel. Adjust the version if necessary.
wget https://www.kernel.org/pub/linux/kernel/projects/rt/6.1/patch-6.1.55-rt15.patch.gz
zcat patch-6.1.55-rt15.patch.gz | patch -p1Check for any rejected patch parts, which would indicate an incompatibility.
find -iname "*.rej"If you find any .rej files, you might need a different patch version or a different kernel source branch.
For a Raspberry Pi 4 or newer (64-bit):
export KERNEL=kernel8
make bcm2711_defconfigFor a Raspberry Pi 3 (32-bit):
export KERNEL=kernel7
make bcm2709_defconfigNow, launch the kernel configuration menu to enable the real-time preemption.
make menuconfigIn the configuration menu, make the following changes:
-
Enable Full Real-Time Preemption:
Kernel Features → Preemption Model → Fully Preemptible Kernel (RT)
-
Ensure High-Resolution Timers are enabled (usually enabled by default):
General setup → Timers subsystem → High Resolution Timer Support
Save the configuration and exit menuconfig.
Now, build the kernel, modules, and device tree blobs. This will take a long time on a Raspberry Pi.
make -j$(nproc)
sudo make modules_installFinally, install the new kernel and device tree files to the /boot directory.
# For RPi 4 or newer
sudo cp -v arch/arm64/boot/dts/broadcom/*.dtb /boot/
sudo cp -v arch/arm64/boot/Image /boot/$KERNEL.img
# For RPi 3
# sudo cp -v arch/arm/boot/dts/*.dtb /boot/
# sudo cp -v arch/arm/boot/zImage /boot/$KERNEL.img
sudo systemctl rebootAfter rebooting, verify that the new kernel is running. The kernel name should contain PREEMPT_RT.
uname -aThe IgH EtherCAT master can be installed from Debian packages, which is much simpler than building from source.
sudo apt install ethercat-masterAfter installation, configure the master by editing the configuration file:
sudo nano /etc/ethercat.confSet the MASTER0_DEVICE to the MAC address of your Ethernet port and specify the driver.
MASTER0_DEVICE="b8:27:XX:XX:XX:XX" # Find your MAC with `ip a`
DEVICE_MODULES="generic"
Then, start and enable the EtherCAT master service:
sudo systemctl start ethercat.service
sudo systemctl enable ethercat.serviceCheck the status to ensure it's running correctly:
sudo systemctl status ethercat.service
ethercat slavesThe ethercat slaves command should list all connected and powered EtherCAT slaves.
Install Machinekit from the official repositories. Follow the instructions on the Machinekit website.
A typical installation looks like this:
sudo apt install -y lsb-release
sudo sh -c \
'echo "deb http://deb.machinekit.io/debian $(lsb_release -sc) main" > \
/etc/apt/sources.list.d/machinekit.list'
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 43DDF224
sudo apt-get update
sudo apt-get install -y machinekit-posixNote: machinekit-posix is for simulation and non-realtime use. For a real-time setup, you would typically install machinekit-rt-preempt.
To check the real-time performance of your system, you can use cyclictest.
sudo apt install -y rt-tests
sudo cyclictest -t1 -p 80 -n -i 10000 -l 100000This will show you the minimum, average, and maximum latencies. The maximum latency is the most important value for determining a safe servo thread period for Machinekit. For a 1ms (1000µs) cycle time, the maximum latency should stay well below that, ideally under 100-150µs under full system load.
- 2023-10-27: Complete rewrite for modern Raspberry Pi OS and kernel.
- 2016-06-16: Add info about Machinekit setup.
- 2016-05-30: Initial version.

