0 Introduction
In the field of CNC automatic control, motion control refers to the real-time control and management of parameters such as position, speed, and acceleration of moving parts of machine tool mechanical devices, enabling them to move according to the expected trajectory and specified motion parameters. Due to the powerful and comprehensive functions of industrial personal computers (IPCs), motion controllers based on the PC standard bus have been widely used in CNC system control. However, because PC standard bus-based motion controllers use board-based hardware designs, this type of motion controller is not suitable for long-term operation in harsh industrial environments (with vibration, dust, oil, etc.). Therefore, in recent years, open-source motion controllers have become a research hotspot in the field of CNC machine tool industrial control.
Linux is a POSIX (Portable Operating System Interface) standard-compliant, multi-user, multi-tasking, and advanced network-enabled open-source operating system that facilitates the development of software with independent intellectual property rights. However, Linux is not a "hard" real-time operating system; its kernel is non-preemptive, meaning real-time tasks cannot be guaranteed. Motion controllers , on the other hand, are highly complex technologies with significant real-time control, real-time interaction, and real-time monitoring characteristics. This paper studies the implementation principles of RTAI (Real-time Application Interface) and analyzes the architecture of a real-time embedded Linux motion controller based on the Mpe5200, and implements its application in a CNC system.
1. Implementation of Real-Time Linux
As an embedded system, a motion controller , in addition to a high-performance embedded microprocessor, should also have an embedded operating system that works in conjunction with the processor. Besides the most basic functions of a general operating system, such as task scheduling, synchronization mechanisms, interrupt handling, and file functionality, an embedded operating system should also possess the following characteristics: portability, customizability, real-time performance, strong stability, and good graphics support. Linux, a fully multitasking 32-bit open operating system, can provide motion controllers with advantages such as scalability, portability, high reliability, and good compatibility, making it very suitable as a development platform for open motion controllers.
To date, there are two main Linux-based systems with hard real-time capabilities developed globally: Linux and RTAI. RTAI originated as a research project at the New Mexico Institute of Technology in the United States. Its design concept was proposed by Victor Yodaiken, and its implementation was by Michael Bambanov. RTAI (Real-Time Application Interface) is a GNU-compliant open-source project initiated by the Department of Aerospace Engineering at the Polytechnic University of Milan in Italy. RTAI supports processors such as the 1386, PowerPC, ARM, MIPS, and M68k-nommu, making it one of the Linux real-time solutions with the widest processor support.
The implementation mechanism of RTAI is very similar to that of RTLinux. The only difference is that RTAI defines a real-time hardware abstraction layer (RTHAL) on Linux. Furthermore, LXRT (Linux.RT) was developed specifically for RTAI, allowing it to call Linux's own system call functions, as shown in Figure 1.
Figure 1. Linux kernel structure for loading RTAI
RTAI modifies architecture-related code in linux/arch/i386 to form the RTHAL layer. RTHAL's role is to allow RTAI to interrupt Linux whenever a real-time task needs to run. With RTAI introduced, Linux's functionality remains unchanged, but it runs as a lowest-priority task and only executes when there are no real-time tasks. The advantage of this is that it minimizes direct modifications to the Linux kernel, making the work of porting RTAI to the Linux kernel minimal.
The main modules implementing RTAI include the RTAI module, SCHED module, FIFO module, SHM module, and LXRT module, which can be dynamically loaded and unloaded. User-defined real-time tasks are loaded after RTAI is loaded. The RTAI module and user-defined real-time modules can be loaded into the Linux kernel at system startup by editing the `/etc/rc.sysinit` file. Once the real-time modules are added to the kernel, threads and functions created within them can access the system's underlying resources.
2. Motion Controller System Design
The motion controller consists of two main parts: a hardware system and a software system. Following a modular design approach, the motion controller's architecture is divided into a user layer, a kernel layer, and a device layer (as shown in Figure 2). The kernel layer and device layer together form the open platform of the motion controller. This independent hierarchical structure makes it easy to implement open applications on CNC systems.
Figure 2 Schematic diagram of motion controller platform system
The user layer (within the dashed box) is the user program space of the open motion controller. The user software of the motion controller is specially designed according to the application object and can be flexibly replaced. The design concept of the open motion controller is: without changing the underlying software/hardware modules, different modular user software can be designed according to the characteristics of the controlled object to meet different motion control tasks, reflecting the open nature of the motion controller. "Modular user software" is the biggest feature of the open motion controller, and "modular user software" is also a mechanism provided by Linux: the Linux kernel and user programs are completely separated, and user programs can operate hardware devices by calling device operation interfaces.
The kernel layer design unfolds along two independent lines: porting the Linux operating system and developing device drivers. As shown in Figure 2, the kernel layer design of the motion controller is the most complex. This part of the design is closely related to the Linux kernel mechanism. A large number of hardware devices in the motion controller rely on Linux device drivers to provide user software device operation interfaces. The "motion control interface" and "field communication interface" in Figure 2 are the most important device interfaces for the open motion controller. Because the device drivers reside at the lowest level of the Linux kernel, they are also referred to as low-level software relative to the upper-level user software.
Figure 3 Functional block diagram of MPC5200 microprocessor
The device layer is the hardware layer of the motion controller. This part of the design process requires the completion of the motion controller's hardware and circuit design. The hardware design of embedded motion controllers differs significantly from traditional PC-based motion controller designs. Considering the application characteristics of specific fields, embedded systems cannot utilize the abundant hardware resources of desktop systems. Therefore, standard PC hardware cannot be directly applied to embedded motion controllers. These factors determine the unique characteristics of the hardware design of open motion controllers.
2.1 Hardware Structure Design
The motion controller microprocessor uses Motorola's new PowerPC MPC5200, a promising chip launched in the second half of 2003. It employs a dual-processor architecture, including a dedicated coprocessor for communication, demonstrating a professional data communication framework. It communicates with the main processor via dual-port memory (an extremely fast data exchange method), significantly reducing the workload on the main processor. The chip also contains a wealth of practical peripherals.
The MPC5200, based on the PowerPC core, is a low-power embedded processor with a processing speed of 760 MIPS. It supports Ethernet, USB, PCI, ATA, I2S, I2C, SPI, serial interfaces, J1850, and CAN. It also supports DDR memory and integrates a double-precision floating-point unit (FPU), as shown in Figure 3.
The MPC5200 motion controller is ideally suited for industrial control applications. Its open architecture allows it to be used on a variety of mechanical equipment, and the fieldbus motion control system built via CAN bus interconnection represents a novel distributed control system. Therefore, the MPC5200 motion controller fully meets the demands of modern CNC systems for high speed, intelligence, networking, integration, and openness.
2.2 Software Structure Design
In real-time Linux, to ensure the immediate response of real-time tasks in the CNC system, all real-time related tasks must be placed at the kernel level. Each task is executed by an independent kernel process, and the real-time process is implemented by calling the rt-task-init() function of RTAI. Non-real-time tasks, on the other hand, are placed at the user level and will not interrupt the execution of real-time tasks at any time; they will only be executed after the real-time tasks have finished.
The basic functions of embedded CNC are implemented by various functional modules. The RTAI real-time kernel is integrated on an open platform with Linux as the operating system. Through a modular design approach, the entire control system can be divided into two main modules based on real-time requirements: a user layer module and a kernel layer real-time module. These two main modules are further divided into sub-modules based on different functions, such as a code interpretation module, a human-machine interaction and tool simulation graphics module, a file management module, a position control module, an interpolation calculation module, and a status monitoring module, as shown in Figure 4.
Figure 4 System software structure diagram
Communication between processes in the kernel and user layers primarily relies on the real-time nFO provided by RTAI, which uses the rlf_create() function to create pipes for data transmission. Since FIFO communication is unidirectional, at least two pipes must be created for information exchange between the user and kernel layers.
3. Software Implementation of Real-Time Control
Based on the analysis of the operating mechanism of the open motion controller, corresponding timer operating modes, priorities, and communication queues are set for each real-time task module.
The real-time function module is implemented as follows: the position control task `moor()` determines the motor control accuracy. It calculates the actual coordinate increment for the next cycle and outputs it to the servo unit to drive the stepper motor. Therefore, it is designed as a periodic RTAI thread with a running cycle of 4ms and a priority level of 1.
The function control task `control()` uses a FIFO to transmit control commands from the user layer to the kernel layer and controls the I/O ports. This task employs a real-time interrupt strategy; upon receiving a control command, the kernel suspends the control task.
The status monitoring task `monitor()` is used to monitor the status in real time. Therefore, this task is set to run for 10ms with a priority level of 3. All real-time tasks and functions of the motion controller software are contained in the real-time module `main_program.o`. This module is loaded into the Linux kernel at system startup by compiling the `/etc/rc.sysinit` file. Once the real-time module is added to the kernel, threads and functions created within it can access the system's low-level resources.
In Linux, the loading and unloading of real-time task modules are implemented by the two functions initmodule() and cleanup-module(). These two functions can also be used to allocate and reclaim resources, as well as create threads and processing functions.
4. Conclusion
This paper, based on an in-depth study of the Linux real-time operating system, demonstrates how to port the Linux operating system to the MPC5200 embedded microprocessor, a practically valuable component in CNC machine tools. This proves that embedded CNC systems can achieve a certain degree of openness, and that porting Linux is a good solution for developing embedded CNC motion controller operating platforms. It exhibits significant advancements and practicality.
Through a series of experimental analyses and applied research, the MPC5200 motion controller has been successfully applied to a self-developed embedded wire harness machine control system based on the CAN bus. This series of machine tools has passed project achievement appraisal and is currently undergoing industrialization promotion. It is hoped that this paper can provide some reference and assistance for the development and promotion of embedded Linux motion control systems with independent intellectual property rights in my country.