Design and Implementation of Embedded PLC Based on RT-Linux
2026-04-06 04:30:23··#1
Introduction In CNC machine tools, programmable logic controllers (PLCs) are typically used to control the machine tool's switching signals. PLCs offer high reliability and ease of use. However, in most CNC machine tools, especially economical ones, the required number of input/output points is relatively small, usually below 60. Therefore, to reduce the cost of CNC machine tools, in industrial PC-based CNC systems, switching I/O boards with external relays can be used, along with the host software, to control the machine tool's switches. However, if the PC uses a single-task operating system (such as DOS), all tasks of the CNC system are placed in a single overall message loop, resulting in poor software modularity and maintainability, a relatively concentrated risk of system failure, and inefficient use of PC system resources. Furthermore, when using a non-real-time multi-task operating system (such as Windows), the Win32 API design does not consider the development needs of a real-time environment, leading to inefficient system calls that cannot meet the real-time requirements of PLC control in CNC systems. Therefore, this paper proposes an embedded PLC based on the RT-Linux operating system. Utilizing the openness, modularity, and scalability of RT-Linux's system architecture and its multi-threaded/multi-tasking environment, real-time performance is ensured while mitigating fault risks. Hardware Structure of the Embedded PLC for CNC Systems The CNC system hardware is built upon the open architecture of a general-purpose industrial PC. The embedded PLC hardware includes: an industrial PC and its peripherals, a digital input/output interface card based on the ISA bus, an opto-isolation module, and a relay output module. Its structure is shown in Figure 1. The industrial PC uses the Red Hat Linux 810 + RT Linux 311 operating system. The CNC system's human-machine interface, CNC code processing, trajectory planning, parameter management, and PLC control are all implemented through software on the industrial PC, eliminating the need for a separate PLC controller. This reduces the CNC system's dependence on hardware and improves system openness. I/O input/output information is exchanged between the host and servo interface modules and I/O interface modules via the PC I/O interface card, which is based on the ISA or PCI bus. RT- Linux is a 32-bit hard real-time operating system based on Linux and capable of running on various hardware platforms. It inherits the design philosophy of the MERT system, providing both strictly real-time services and all standard POSIX services within a single operating system based on a general-purpose operating system. RT-Linux's source code is open-source and easy to modify, reducing system costs. The open source code frees CNC system development from dependence on foreign software companies, contributing to the localization of CNC software. RT-Linux is a multi-tasking real-time operating system based on Linux and capable of running on various hardware platforms. By modifying the Linux kernel's hardware layer and employing interrupt emulation technology, a small and efficient real-time kernel is implemented between the kernel and the hardware. A small real-time system is formed on top of this real-time kernel, with the Linux kernel running only as the lowest priority task in the real-time system. For ordinary x86 hardware architectures, RT-Linux exhibits excellent real-time performance and stability, with a maximum interrupt latency of no more than 15μs and a maximum task switching error of no more than 35μs. These real-time parameters are independent of system load but depend on the computer hardware. For example, on a typical PC with a PII350 and 64MB of memory, the maximum system latency is no more than 1μs. RT-Linux is divided into real-time and non-real-time domains based on real-time requirements, as shown in Figure 2. The real-time domain follows the design principles of real-time operating systems, namely transparency, modularity, and scalability. The RT-Linux real-time kernel consists of a core part and several optional parts. The core part is only responsible for high-speed interrupt handling, supports SMP operations, and is not delayed or re-entered by underlying synchronization or interrupt routines. Other functions are extended by dynamically loadable modules. RT-Linux leaves operations that do not affect system real-time performance (i.e., non-real-time domain operations) to the non-real-time Linux system. Linux, based on a multi-tasking environment, provides rich system resources for software development, such as various inter-process communication mechanisms and flexible memory management mechanisms. Embedded PLC Design and Implementation The PLC control module of an embedded PLC system has high real-time requirements and therefore must run within the system's real-time domain. Based on the requirements of PLC control in general CNC systems and the modular design of CNC system software, the PLC control module of the CNC system is designated as one of the real-time tasks of the RT-Linux system. Its priority and calling cycle depend on the real-time requirements of each task in the CNC system and the response time of the control requirements. The PLC control module mainly completes the logic control of the CNC system, while the controlled inputs and outputs, i.e., I/O inputs and outputs, are handled by the PC I/O interface card input/output module. Therefore, completing the PLC control of the CNC system requires two RT-Linux real-time tasks, as shown in Figure 3. These two tasks are RT-Task1 (hereinafter referred to as "adapter card input/output") and RT-Task2 (hereinafter referred to as "PLC control"). Figure 3 is a diagram of the embedded PLC real-time task relationship based on the RT-Linux system. The adapter card input/output mainly completes the inputs and outputs of the CNC system, i.e., the output of position control commands for each axis, I/O outputs, I/O inputs, and position feedback inputs. It is essentially the device driver module of the CNC system control card, and its priority is the highest among the real-time tasks of the CNC system. Based on its hardware characteristics and motion control requirements, its response period is 100μs, generated by a hardware timer on the PC I/O interface card. According to the RT-Linux system's hardware interrupt response mechanism, the real-time performance of input/output control tasks is guaranteed, a fact verified in our CNC system. In Figure 3, the PLC control primarily performs the PLC control functions of the CNC system. Its task priority is lower than the adapter card's input/output, and also lower than the CNC system's real-time interpolation and position servo tasks. Based on the PLC control requirements of a general CNC system, its response period is determined to be 5ms, generated by the RT-Linux software timer. According to the RT-Linux system's real-time multi-task scheduling mechanism, the real-time performance of the PLC control tasks is guaranteed, a fact also verified in practical applications. In embedded PLCs, the real-time task module facilitates data communication between two real-time tasks controlled by the PLC in a CNC system. Since the amount of input/output data required (typically 64 inputs and 64 outputs, but expandable as needed) is relatively small, a shared memory communication method is used. Two shared memory blocks are allocated between the adapter card's input/output and the PLC's control of the two real-time tasks. One block is used for the adapter card to transmit I/O port status information to the PLC control, and the other block is used for the PLC control to transmit control information processed by the PLC logic to the adapter card's input/output task. The reason for not using an RT-FIFO for communication between the two real-time tasks is that the amount of data exchanged is not large, while the two tasks have significantly different execution cycles. Using an RT-FIFO would require an additional coordination mechanism to avoid FIFO congestion, which might not be as effective as using shared memory, especially since shared memory offers faster read/write speeds than FIFO. Implementation of Real-Time Tasks in Embedded PLCs The adapter card's input/output modules are dynamically loadable modules. Each module (task) uses a 100μs hardware timer interrupt to complete the output of position control commands and I/O for each axis, as well as the input of position feedback values and I/O for each axis. The adapter card's output values come from the position servo task and the PLC control task, while the input values come from the adapter card's input interface. The PLC control module (task) is also a dynamically loadable module. It periodically reads control information (such as M, S, and T instructions) from the RT-FIFO it communicates with the main control module at a 5ms soft timer. Simultaneously, it reads I/O information from the shared memory it communicates with the adapter card's input/output modules, performs logical processing, and finally writes the results back to the shared memory for the adapter card's input/output modules to read and output. Conclusion This embedded PLC module has been successfully applied to the THHP-III CNC system (based on Red Hat Linux 8.0 + RT Linux 3.1) of the Manufacturing Engineering Research Institute, Department of Precision Instruments, Tsinghua University. This module can meet the PLC control requirements of ordinary CNC systems and machining centers.