RDK X3部署LinuxCNC(附内核移植、编译思路)

LinuxCNC(先前称为EMC2)是一个开源的数控(Computer Numerical Control,CNC)软件系统,用于控制数控机床和运动控制设备。它的目标是将计算机与机械控制相结合,以实现精密的工件加工和运动控制。LinuxCNC的设计灵感来自于先前的EMC项目,但后来进行了重大的开发和改进。

LinuxCNC在Ubuntu ARM64中的部署相对复杂,文章作为记录和分享,希望可以帮助到有需要的朋友们。

一、内核及系统镜像构建

RDK X3官方镜像中并没有启动实时内核(PREEMPT-RT),我们需要通过内核的构建来开启相关的功能,然后打包自己的系统镜像烧录至开发板。

1、虚拟机安装Ubuntu 20.04交叉编译开发环境

交叉编译是指在主机上开发和构建软件,然后把构建的软件部署到开发板上运行。主机一般拥有比开发板更高的性能和内存,可以加速代码的构建。可以参照这篇文章完整基础环境的安装:

https://zhuanlan.zhihu.com/p/141033713
2、执行下面命令完成基础开发环境的搭建

相关操作步骤来源于官方文档的Linux开发指南部分。

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

下载交叉编译工具链,解压并安装,建议安装到/opt目录下,通常向/opt目录写数据需要sudo权限。

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

安装编译环境源码(rdk-gen),rdk-gen用于构建适用于地平线RDK X3的定制操作系统镜像。它提供了一个可扩展的框架,允许用户根据自己的需求定制和构建RDK X3的Ubuntu操作系统。

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

下载官方系统镜像版本完全对应的源码,目前是2.0.0版本,过程中的提示直接选择"YES"。

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

下载官方预装的samplefs和deb文件

./download_samplefs.sh deb_packages
./download_deb_pkgs.sh deb_packages
3、编译kernel内核、移植

在mk_kernel.sh脚本中的build_all函数中开启实时内核(PREEMPT-RT)相关配置。

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
    ...省略其他内容

开始编译kernel,遇到提示直接输入"Y"和回车

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
4、编译并移植hobot-boot
./mk_debs.sh hobot-boot

备份deb_packages目中原有的hobot-boot包,记住原有hobot-boot包的名称,例如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 ../

将新编译的hobot-boot复制到deb_packages目录并改名为原有hobot-boot包的名称。

cp deploy/deb_pkgs/hobot-boot_2.0.0-20230724221932_arm64.deb deb_packages/hobot-boot_2.0.1-20230619192956_arm64.deb
5、编译系统镜像
sudo ./pack_image.sh test

系统镜像存放于deploy目录,例如:ubuntu-preinstalled-desktop-arm64.img,可按照官方文档的说明烧录至开发板。

6、验证是否开启实时内核(PREEMPT-RT)

ssh登录搭载新镜像系统的开发板验证是否开启实时内核(PREEMPT-RT)。

uname -a

如果已开启开启实时内核(PREEMPT-RT),则在反馈信息中会包含"PREEMPT RT",如下所示:

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

二、编译安装LinuxCNC

1、安装编译所需的依赖包。

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、下载LinuxCNC源码。

推荐使用2.9及以上版本,低于2.9的版本未验证。

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

3、编译LinuxCNC

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

如果配置软件包脚本成功会看到以下信息:

######################################################################
#                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                                                   #
#                                                                    #
######################################################################

4、继续编译,这一步耗时较久,请耐心等待,过程中会出现一些警告信息,不用在意,只要不出错就OK。

make

如果编译成功你会看到如下信息:

...
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.

5、设置 LinuxCNC 可执行文件权限:

sudo make setuid

6、运行测试:

source ../scripts/rip-environment
linuxcnc

编译CNC代码时,是在主机上编译的,还是地平线板子上?

本文是X3上编译