Deploying LinuxCNC on RDK X3 (Including Kernel Porting, Compilation Ideas)

LinuxCNC (formerly known as EMC2) is an open-source Computer Numerical Control (CNC) software system used to control CNC machine tools and motion control equipment. Its goal is to combine computers with mechanical control to achieve precise workpiece processing and motion control. The design inspiration for LinuxCNC came from previous EMC projects, but significant development and improvements were made later.

The deployment of LinuxCNC in Ubuntu ARM64 is relatively complex. As a record and sharing, I hope it can help friends in need.

One. Kernel and system image construction

The official image of RDK X3 does not have a real-time kernel (PREEMPT-RT) enabled. We need to build the kernel to enable related functions, and then package our own system image and burn it to the development board.

  1. Virtual Machine Installation Ubuntu 20.04 Cross Compilation Development Environment

Cross compilation refers to developing and building software on a host, and then deploying the built software to run on a development board. Hosts generally have higher performance and memory than development boards, which can accelerate code construction. You can refer to this article for a complete installation of the basic environment:

https://zhuanlan.zhihu.com/p/141033713
  1. Execute the following command to complete the setup of the basic development environment

The relevant operation steps are sourced from the Linux Development Guide section of the official documentation.

sudo apt-get install -y build-essential make cmake libpcre3 libpcre3-dev bc bison flex python-numpy mtd-utils zlib1g-dev debootstrap libdata-hexdumper-perl libncurses5-dev zip qemu-user-static curl git liblz4-tool apt-cacher-ng libssl-dev checkpolicy autoconf android-sdk-libsparse-utils android-sdk-ext4-utils mtools parted dosfstools udev rsync

Download the cross compilation toolchain, decompress and install it. It is recommended to install it in the/opt directory. Usually, writing data to the/opt directory requires sudo permission.

curl -fO http://archive.sunrisepi.tech/toolchain/gcc-ubuntu-9.3.0-2020.03-x86_64-aarch64-linux-gnu.tar.xz
sudo tar -xvf gcc-ubuntu-9.3.0-2020.03-x86_64-aarch64-linux-gnu.tar.xz -C /opt

Install the compilation environment source code (rdk gen), which is used to build a custom operating system image suitable for Horizon RDK X3. It provides an extensible framework that allows users to customize and build the Ubuntu operating system for RDK X3 according to their own needs.

git clone git@github.com:HorizonRDK/rdk-gen.git
cd rdk-gen

Download the source code that corresponds exactly to the official system image version. Currently, it is version 2.0.0. If prompted during the process, simply select “YES”.

./source_sync.sh -t os-image_2.0.0

Download the official pre installed samplefs and deb files

./download_samplefs.sh deb_packages
./download_deb_pkgs.sh deb_packages
  1. Compile kernel, port

Enable real-time kernel (PREEMPT-RT) related configuration in the build_all function in the mk_kernel.sh script.

vim mk_kernel.sh


function build_all()
{
    # 生成内核配置.config
    make $kernel_config_file || {
        echo "make $config failed"
        exit 1
    }
    # 开启实时内核(PREEMPT-RT)相关配置
    echo "CONFIG_PREEMPT_RT_BASE=y" >> .config
    echo "CONFIG_PREEMPT_LAZY=y" >> .config
    echo "CONFIG_PREEMPT_RT_FULL=y" >> .config
    ...省略其他内容

Start compiling the kernel, and when prompted, simply enter “Y” and press enter

export CROSS_COMPILE=/opt/gcc-ubuntu-9.3.0-2020.03-x86_64-aarch64-linux-gnu/bin/aarch64-linux-gnu-
export LD_LIBRARY_PATH=/opt/gcc-ubuntu-9.3.0-2020.03-x86_64-aarch64-linux-gnu/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH
export PATH=$PATH:/opt/gcc-ubuntu-9.3.0-2020.03-x86_64-aarch64-linux-gnu/bin/
export ARCH=arm64
./mk_kernel.sh
  1. Compile and port hobot boot

    ./mk_debs.sh hobot-boot

Back up the existing hobot boot package in the deb_packages directory, and remember the name of the original hobot boot package, for example, hobot boot-2.0.1-20230619192956-arm64.deb.

cd deb_packages
mv hobot-boot_2.0.1-20230619192956_arm64.deb hobot-boot_2.0.1-20230619192956_arm64.deb_bak
cd ../

Copy the newly compiled hobot boot to the deb_packages directory and rename it to the name of the original hobot boot package.

cp deploy/deb_pkgs/hobot-boot_2.0.0-20230724221932_arm64.deb deb_packages/hobot-boot_2.0.1-20230619192956_arm64.deb
  1. Compile system image

    sudo ./pack_image.sh test

The system image is stored in the deploy directory, for example: ubuntu pre installed desktop arm64.img, which can be burned to the development board according to the official documentation instructions.

  1. Verify if real-time kernel (PREEMPT-RT) is enabled

SSH login to the development board equipped with the new image system to verify whether the real-time kernel (PREEMPT-RT) is enabled.

uname -a

If the real-time kernel (PREEMPT-RT) is enabled, the feedback message will include “PREEMPT RT” as follows:

Linux ubuntu 4.14.87 #1 SMP PREEMPT RT Mon Jul 24 18:55:46 CST 2023 aarch64 aarch64 aarch64 GNU/Linux

Two Compile and install LinuxCNC

  1. Install the required dependency packages for compilation.

    sudo apt install -y automake libudev-dev libusb-1.0-0-dev libgtk-3-dev psmisc yapps2 intltool tk-dev bwidget tclx python3-tk libglu1-mesa-dev libxmu-dev asciidoc debhelper dh-python imagemagick libboost-python-dev libtirpc-dev netcat-traditional netcat-openbsd po4a python3-xlib

  2. Download the LinuxCNC source code.

Recommend using version 2.9 and above, versions below 2.9 have not been validated.

git clone git@github.com:LinuxCNC/linuxcnc.git
cd linuxcnc/src
  1. Compile LinuxCNC

    ./autogen.sh
    ./configure --with-realtime=uspace

If the configuration package script is successful, you will see the following information:

######################################################################
#                LinuxCNC - Enhanced Machine Controller              #
######################################################################
#                                                                    #
#   LinuxCNC is a software system for computer control of machine    #
#   tools such as milling machines. LinuxCNC is released under the   #
#   GPL.  Check out http://www.linuxcnc.org/ for more details.       #
#                                                                    #
#                                                                    #
#   It seems that ./configure completed successfully.                #
#   This means that RT is properly installed                         #
#   If things don't work check config.log for errors & warnings      #
#                                                                    #
#   Next compile by typing                                           #
#         make                                                       #
#         sudo make setuid                                           #
#          (if realtime behavior and hardware access are required)   #
#                                                                    #
#   Before running the software, set the environment:                #
#         . (top dir)/scripts/rip-environment                        #
#                                                                    #
#   To run the software type                                         #
#         linuxcnc                                                   #
#                                                                    #
######################################################################
  1. Continue compiling, this step will take a long time. Please be patient as there may be some warning messages during the process. Don’t worry, as long as there are no errors, it will be okay.

    make

If the compilation is successful, you will see the following information:

...
Linking ../rtlib/motmod.so
Compiling realtime emc/motion/homemod.c
Compiling realtime emc/motion/homing.c
Linking ../rtlib/homemod.so
Compiling realtime emc/tp/tpmod.c
Compiling realtime emc/tp/tc.c
Compiling realtime emc/tp/tcq.c
Compiling realtime emc/tp/tp.c
Compiling realtime emc/tp/spherical_arc.c
Compiling realtime emc/tp/blendmath.c
Compiling realtime emc/nml_intf/emcpose.c
Linking ../rtlib/tpmod.so
config.status: creating ../scripts/setup_designer
You now need to run 'sudo make setuid' in order to run in place with access to hardware.
  1. Set LinuxCNC executable file permissions:

    sudo make setuid