Installing LinuxCNC 2.9 on Raspberry Pi 4B with Preempt-RT kernel

More
13 Dec 2022 19:10 - 08 Jan 2023 09:49 #259347 by alangibson
I've seen a lot of people, including myself, have trouble getting LinuxCNC running on the Raspberry Pi 4. I've just gotten it all working, so I thought I'd share my recipe.

We will be overclocking and disabling power management in order to decrease latency. Make sure your Raspberry Pi has a *very large* heat sink, the kind that basically covers the entire board, before attempting this. A fan wouldn't hurt anything either.

Make sure you are using the fastest SD card you can find. Slow reads and writes will cause latency spikes.

1. On Linux desktop computer

Everything in this section should be run on your Linux desktop computer. These commands were tested on Linux Mint 21, but any reasonably recent Debian distro should work.

1.1 Install Raspberry Pi Imager
sudo apt install rpi-imager

If that doesn't work, you can manually download and install from www.raspberrypi.com/software/

1.2 Build Rasberry Pi OS image with Realtime-RT kernel
sudo apt install git
git clone --depth 1 https://github.com/remusmp/rpi-rt-kernel
cd rpi-rt-kernel
make RASPIOS_IMAGE_NAME=raspios_full_arm64

The script will spit out the name of the OS image file at the end of its run.

    adding: 2022-09-22-raspios-bullseye-arm64-full.img (deflated 65%)

1.3 Burn image to SD card with Rasbperry Pi Imager

Insert the SD card into your computer.

Open Raspberry Pi Imager.

Click "Choose OS" button. Select "Use Custom" from the bottom of the menu. Select the '.img' file you just built in the `rpi-rt-kernel` directory.

Click "Choose Storage" button. Select your SD card.

Click the settings sprocket image button. Set your WLAN credentials, enable ssh, hostname, username/password, and timezone in the settings. I use

    hostname = linuxcnc
    enable ssh = checked
    use password authentication = selected
    set username and password = checked
        username = pi
        password = (password of your choice)
    configure wireless lan = checked
        SSID = (your wifi access point name)
        password = (your wifi password)
        wireless lan country = (your country code)
    set locale settings = checked
        timezone = (your timezone)

Click the "Write" button

1.4 Boot Raspberry Pi

Insert the micro SD card in your RPi 4 and power it up. If you correctly set your WLAN credentials, it should be available on your Wifi network in about a minute. You can watch for it to appear by running `ping`.
ping linuxcnc.local

1.5 Connect to Raspberry Pi

Once the Rasberry Pi is online, `ssh` in to it
ssh username@linuxcnc.local

Enter your password when prompted.

2. On Rasbperrry Pi

The following instructions are based on gnipsel.com/linuxcnc/debian-11-emc.html

2.1 Fix locale setting

First check if you need to set the locale
echo $LC_ALL

If you don't get anything back, then run
echo "export LANGUAGE=$LANG" >> ~/.bashrc
echo "export LC_ALL=$LANG" >> ~/.bashrc
source ~/.bashrc

2.2 Build LinuxCNC
sudo apt install build-essential devscripts autoconf automake \
debhelper dh-python libudev-dev bwidget \
    intltool libboost-python-dev libepoxy-dev libgl1-mesa-dev \
    libglu1-mesa-dev libgtk2.0-dev libgtk-3-dev libmodbus-dev \
    libeditreadline-dev libxmu-dev netcat po4a python3-dev \
    python3-tk python3-xlib tcl8.6-dev tclx tk8.6-dev yapps2 \
    asciidoc docbook-xsl dvipng groff imagemagick \
    python3-lxml source-highlight w3c-linkchecker xsltproc \
    texlive-extra-utils texlive-font-utils texlive-fonts-recommended \
    texlive-lang-cyrillic texlive-lang-french texlive-lang-german \
    texlive-lang-polish texlive-lang-spanish texlive-xetex \
    texlive-latex-recommended dblatex asciidoc-dblatex \
    libusb-1.0-0-dev graphviz inkscape texlive-lang-european
git clone -b 2.9 --depth 1 https://github.com/LinuxCNC/linuxcnc.git build
cd build
./debian/configure no-docs
dpkg-checkbuilddeps
debuild -uc -us

2.3 Install LinuxCNC
sudo apt install libxml2 gir1.2-gtksource-3.0 libxml2-dev libglew2.1 mesa-utils python3-configobj libgtksourceview-3.0-dev tclreadline
cd ..
sudo dpkg -i linuxcnc-uspace_2.9.0~pre1_arm64.deb

2.4 Configure notification service
sudo apt install -y notification-daemon

cat | sudo tee /usr/share/dbus-1/services/org.freedesktop.Notifications.service <<EOF
[D-BUS Service]
Name=org.freedesktop.Notifications
Exec=/usr/lib/notification-daemon/notification-daemon
EOF

2.5 Install Qt Designer
sudo sed -i 's/x86_64-linux-gnu/aarch64-linux-gnu/' /usr/lib/python3/dist-packages/qtvcp/designer/install_script
/usr/lib/python3/dist-packages/qtvcp/designer/install_script

2.6 Install Mesaflash
git clone https://github.com/LinuxCNC/mesaflash.git
sudo apt install libpci-dev libmd-dev pkg-config
cd mesaflash
sudo make install

2.7 Install Mesa Configuration Tool
cd ~
sudo apt install python3-packaging
curl -L -O https://github.com/jethornton/mesact/raw/master/mesact_1.1.2_arm64.deb
sudo dpkg -i mesact_1.1.2_arm64.deb

2.8 Improve latency
# Stop and disable services we don't need
sudo systemctl stop alsa-restore apache2 bluetooth hciuart cron cups-browsed cups triggerhappy upower packagekit
sudo systemctl disable alsa-restore apache2 bluetooth hciuart cron cups-browsed cups triggerhappy upower packagekit
# Overclock CPU
echo 'over_voltage=6' | sudo tee -a /boot/config.txt
echo 'arm_freq=2000' | sudo tee -a /boot/config.txt
echo -n ' isolcpus=1,2,3 idle=poll processor.max_cstate=1' | sudo tee -a /boot/cmdline.txt
echo -n 'performance' | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
# Overclock SD card
echo 'dtparam=sd_overclock=100' | sudo tee -a /boot/config.txt
# Disable audio
sudo sed -i 's/dtparam=audio=on/# dtparam=audio=on/' /boot/config.txt

2.9 Fix virtual keyboard
sudo apt-get install onboard
pip install pygst
sudo sed -i 's/stderr=subprocess.PIPE,/stderr=subprocess.PIPE, text=True,/' /usr/lib/python3/dist-packages/gladevcp/hal_filechooser.py

2.10 (Optional) Fix USB ports on Raspberry Pi 4 Compute Module
echo -n ' dtoverlay=dwc2,dr_mode=host' | sudo tee -a /boot/cmdline.txt

2.11 (Optional) Improve latency by stopping unnecessary networking

This will disable Wifi, automatic IP address assignment, remote shell access, and local DNS naming. Make sure you have a monitor and keyboard connected before doing this.
sudo systemctl stop avahi-daemon ssh wpa_supplicant dhcpcd ModemManager
sudo systemctl disable avahi-daemon ssh wpa_supplicant dhcpcd ModemManager

And that's about it.

With 7 glxgears and a ping flood to my 7i96S running, base thread jitter is 25us. That's good enough for slow-ish software stepping according to the Wiki.
Last edit: 08 Jan 2023 09:49 by alangibson.
The following user(s) said Thank You: tommylight, elovalvo, Donno, DrKnow65, mor, gene_weber, RMS

Please Log in or Create an account to join the conversation.

More
13 Dec 2022 19:28 #259348 by tommylight
You should also mention the version of the RPI4, by now there are several versions with different hardware so all this might not work on all of them, as can be noted from complaints about the official image not working on some new RPI4.
Thank you.

Please Log in or Create an account to join the conversation.

More
13 Dec 2022 20:07 #259351 by rodw

You should also mention the version of the RPI4, by now there are several versions with different hardware so all this might not work on all of them, as can be noted from complaints about the official image not working on some new RPI4.
Thank you.

I don't think the version matters as from the git files Pi 1 to Pi 4/4b are supported.

Please Log in or Create an account to join the conversation.

More
13 Dec 2022 20:28 #259359 by tommylight
It does, new versions had issues with the official Raspbian, it did not work several months back.
I do not nor do i intend to use RPI4 in the foreseeable future, but i read everything on this forum and there are several topic with that subject.

Please Log in or Create an account to join the conversation.

More
25 Dec 2022 17:08 #260283 by RMS
Alangibson,
I have tested your recipe on raspberry Pi, compute module CM4108016, 8G RAM + 16G eMMc installed on mother board “Compute Module 4 IO Board” and mesa 7i96S
This is the cleanest OS 64 installation for linuxcnc 2.9 preRT with kernel

uname -a
Linux linuxcnc29 5.15.84-rt54-v8+ #1 SMP PREEMPT_RT Fri Dec 23 20:39:48 CET 2022 aarch64 GNU/Linux

Note: compute module carrier board IO must have
in the file /boot/config.txt

dtoverlay=dwc2,dr_mode=host

to enable usb PORTS.

The only hiccup I have experienced was with setting on the “locale”.

for users experiencing the issue
VERIFY IF YOU HAVE THOSE LINES
export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"

So far i have no issues with other programs residing in raspberrian.

My project is mobile robotic analytical platform with linuxcnc as the backbone for sample collection.
This is my third setup. The other systems were for big CNC 4x6 ft table with encoders in the loop using 7i96 + 7i85 raspberry pi 4M
and Grizzly CNC with Jetson TX1 and 7i95

Thank you and the other contributors.
RMS
The following user(s) said Thank You: tommylight

Please Log in or Create an account to join the conversation.

More
26 Dec 2022 10:17 - 26 Dec 2022 10:40 #260304 by voltaicsca
Similar problem, but different enough to chime in. My tack was admittedly a bit hacky, but it worked.
My goal was to create a "fresh" rpi image (ie, modify the .img file, BEFORE flashing, rather than after, since after first boot, things change) with the rt preempt kernel, but based on the rpi images supplied by linuxcnc, rather than the vanilla raspbian. This was difficult, because the released images from linuxcnc don't seem to support the RPi 4*B*, which is what I'm running, and the raspbian image with a new kernel didn't seem to want to install linuxcnc. Both sets of software had something I wanted, but neither had everything, hence the deep-dive.

I found a way that worked for me, and have some (poorly documented) code here: github.com/InternetofAwesome/pm-940-cnc/...misc/distro-build.sh

Basically, the script is going to want a "fresh" image of the 2.8.1 build from linuxcnc for rpi, and the files from /boot from a more recent raspbian build.
Those are in the top of the script in a clearly marked config section, and named IMAGE_PATH and BOOTLOADER_PATH, respectively.

Once the script runs, you should have a ready-to-flash base OS image. From there, the apt repos are setup to grab from the mainline linuxcnc sources, and worked fine for me.

I didn't need/want to build LCNC from source, so the mainline releases were fine for me.
Last edit: 26 Dec 2022 10:40 by voltaicsca. Reason: Misread the original post, and edited the reply.

Please Log in or Create an account to join the conversation.

More
05 Jan 2023 18:04 - 05 Jan 2023 18:21 #261012 by RMS
Dear All,

With ongoing extensive testing, of this kernel
Linux linuxcnc29 5.15.84-rt54-v8+ #1 SMP PREEMPT_RT Fri Dec 23 20:39:48 CET 2022 aarch64 GNU/Linux

Unfortunately, I have found few problems.
1. the virtual keyboard was not found by gmoccapy.

The solution was the installation of onboard and pygst
$sudo apt-get install onboard
$pip install pygst

Note:
The matchbox-keyboard in this kernel is defective

2. In gmoccapy when loading png the error box pops up:

Found an error!
The following information may be usefull in troubleshooting
File "/usr/lib/python3/dist-packages/gladevcp/hal_filechooser.py", line 56, in update
m=progress_re.match(stderr_line)
TypeError: cannot use a string pattern on bytes-like object

Solution:
TBD , we need to ask developer of gmoccapy.
However, the “axis” imports and processes the png’s and py’s OK

Thanks,
RMS
Last edit: 05 Jan 2023 18:21 by RMS.

Please Log in or Create an account to join the conversation.

More
06 Jan 2023 09:26 - 06 Jan 2023 09:27 #261070 by alangibson
Thanks for the report

1. I'll add that to the instructions

2. There may be a quick fix for that. Try this:

- Open file /usr/lib/python3/dist-packages/gladevcp/hal_filechooser.py
- Change this line
    stderr=subprocess.PIPE,
to
    stderr=subprocess.PIPE, text=True,
Last edit: 06 Jan 2023 09:27 by alangibson.
The following user(s) said Thank You: RMS

Please Log in or Create an account to join the conversation.

More
07 Jan 2023 15:31 - 07 Jan 2023 15:37 #261171 by RMS
Alan
it works thanks
RMS

P.S.1.      With the overclocking I suggest being cautious.  I was experiencing stability problem on CM4 and had to revert and clean cmdline.txt and config.txt from overclocking entries.
2.      While loading button macros with custom ‘png” graphics i am getting error. File “/usr/bin/gmoccapy”, line 5728, in <module>  app = gmoccapy(sys.arg)
...
File “/usr/bin/gmocappy”, line 1397, in_check_macro_for_image Image=line.split(“.”)[1] IndexError: list index out of range

Any clues? 
Last edit: 07 Jan 2023 15:37 by RMS. Reason: add on info

Please Log in or Create an account to join the conversation.

More
08 Jan 2023 09:51 #261249 by alangibson
Good to hear.

1. I've added a note about needing an very large heat sink. I've been thrashing my RPi for a week straight now without problems.

2. I'll take a look and get back to you
The following user(s) said Thank You: RMS

Please Log in or Create an account to join the conversation.

Time to create page: 0.444 seconds
Powered by Kunena Forum