Share this

Application design of multi-threading technology to solve the real-time performance problem of open CNC systems

2026-04-06 04:13:26 · · #1
1 Introduction Real-time performance is an important performance indicator of CNC system. On the open CNC system development platform composed of IPC (Industrial Personal Computer) + motion controller, although this master-slave structure ensures that the motion control command is executed at high speed and in real time in the motion controller, many tasks such as real-time display, preprocessing calculation and system status monitoring still need to be completed on the PC. In order to ensure the real-time performance of the system, multi-threading technology is proposed to improve the real-time performance of the system by multi-task parallel processing. This development platform adopts an open CNC system of IPC + motion controller mode. The main motion control is completed by the GT400-SV general motion controller of Googol Technology. It provides the C language function library GT400sv.lib and the Windows dynamic link library GT400.dll, which can realize complex control functions [2]. The development of CNC system is to integrate these control functions with the application modules such as data processing, interface display and user interface required by the control system to build a control system that meets the specific application requirements. 2 Process and Thread and Multi-threading Technology The Windows operating system supports both multi-process and multi-threading. A process is an instance of an application. An execution process is a program loaded into memory to be executed, including the execution code of the currently executing application and some environment information related to program execution. Each process has the resources of the entire computer and does not need to know the information of other processes in the computer. Usually, each process has at least one thread executing the code in its own address space, which is called the main thread. If the main thread finishes running, the system will automatically clean up the process and other address spaces. A thread is the execution path inside a process, the basic entity for the operating system to allocate CPU time, and the smallest unit of program execution. Each process starts the execution of the application from the main thread. A thread consists of a stack, the state of the CPU registers, and an entry point in the system call list. Each process can contain more than one thread, which can execute the code in the process address space independently and simultaneously, sharing all the resources in the process. The smallest unit of processor time allocated by the Windows system is the thread, and the system constantly switches between threads. In a PC, only one thread runs at a time. Usually, the time slice allocated to each thread by the system is very small (m/s), which ensures the real-time performance of the fast system [3]. To achieve multi-threaded programming, worker threads and user interface threads can be established. Worker threads are mainly used to execute CNC programs, coordinate displays, dynamic simulations and data preprocessing; user interface threads are used to process user input and respond to user-generated events and messages. 3 Real-time analysis of CNC system 3.1 Real-time performance of threads The CNC system has many tasks to complete, and the priority requirements of these tasks are different. Accordingly, the multi-tasking, preemptive and multi-threading features of the Windows system can be used to assign each task to different threads and give each thread a different priority. When a high-priority thread is executed, that is, when a task with high real-time requirements needs to be executed, the work of other threads can be automatically terminated and this thread can be executed [4]. Through this method, the real-time performance required by the CNC system can be achieved. 3.2 Creation of worker threads The worker threads created in this development system can be roughly divided as follows: (1) Coordinate display thread The coordinates of the three motion axes X, Y and Z can be displayed in real time in the manual pulse panel, electric control panel and incremental control panel. This allows the operator to see the actual coordinates of the three axes intuitively. (2) Graphics Display Thread The graphics display thread is used to execute graphics drawing instructions in the dynamic simulation panel. Through graphics display, the operator can operate the human-machine interface while the dynamic simulation is in progress. This thread has low real-time requirements and is rated as: Blow Normal. (3) IO Status Control Thread This thread is used to detect the discrete quantities input by the system and the instructions obtained from the CNC program to output the status of the discrete quantities of the machine tool. This thread has a higher priority than the previous two threads and is rated as: Normal. (4) Data Preprocessing Thread The data preprocessing thread is mainly responsible for executing motion control data preprocessing functions such as encoding form conversion, tool length compensation, tool radius compensation, and metric-imperial conversion. It is rated as: Normal. (5) Motion Control Thread This thread is mainly used for the motion controller to execute CNC code functions. It is responsible for inputting motion control commands to the buffer, clearing the buffer, and opening and closing the buffer. It is rated as: Above Normal. (6) Emergency Control Thread This thread handles situations where the machine tool needs to react immediately, such as emergency stops. It has the highest priority, with a level of: Highest. The auxiliary threads created in this system can be roughly divided as shown in Table 1 below. Table 1 Thread Creation and Priority Settings 4 Implementation of Multithreading In the Windows operating system, implementing multithreading requires calling a series of API functions, such as CreateThread and ResumeThread, which is relatively troublesome and prone to errors. Using the TThread class in the new generation RAD development tool C++ Builder, multithreaded programming can be easily implemented, especially for Windows series operating systems where the system development language is C, it has unparalleled advantages over other programming languages. 4.1 Thread Creation Although the concept of a thread is illustrated using the TThread object in C++ Builder, the TThread object itself is not complete. It is necessary to create a subclass under TThread and override Execute to use the thread object. In the C++ Builder IDE environment, select File|New from the menu, select Thread Object in the New tab, and click OK. In the pop-up dialog box, enter the name of the TThread object subclass, CoordinateDisplyThread, which automatically creates a TThread subclass of CoordinateDisply. Simultaneously, a unit named CoordinateDisplyThread is created in the editor. 4.2 Thread Implementation In the created code, the Execute() function is where the code for the task to be implemented in the thread is located. The original Unit1.cpp code includes the CoordinateDisplayThread.h file. When using it, a TCoordinateDisplay object is dynamically created, and the specific code executed is the code of the overloaded Execute() method. Since the function to be executed by the thread added in Execute() calls the VCL component, and VCL objects are not thread-safe, their characteristics and methods can only be accessed in the main thread, the Synchronize() function is used to wrap the coordinate display function. The coordinate display function needs to be declared as follows: void_fastcall Function(). The following uses the implementation steps of the coordinate display thread, i.e., CoordinateDisplayThread, as an example to illustrate the specific method of thread implementation. The implementation of other threads needs to be modified according to specific circumstances. Add the following statement to the `CoordinateDisplayThread::Execute()` function in the `CoordinateDisplayThread.cpp` file to ensure consistency in the X, Y, and Z coordinate display function calls. First, use a switch statement to determine which axis's coordinate position has changed in the single-axis motion: After completing the above preparations, add commands to start and suspend threads at appropriate locations in the main unit. The code is shown below: 4.3 About Thread Synchronization Thread synchronization is very important in programming. When one thread accesses a process object, if another thread tries to modify that object, it may produce incorrect results. In this application development example, using API functions, we can directly use critical or mutual exclusion to achieve synchronization. To improve the reliability and flexibility of synchronization, both flag variables and critical mechanisms are used. Simply declare a variable `Sect1` of type `TRTLCriticalSection` in the program and initialize it in the main thread's constructor. Then, within a specific thread, the corresponding code can be marked as a critical section. When `EnterCriticalSection()` is called in a thread and `Sect1` is passed, multiple data members are set to indicate that the critical section has entered an active state. If another thread wants to call its own critical section, the `EnterCriticalSection()` function will find that a critical section is in use, and will put the second thread into a sleep state until the first thread exits the critical section. 5. Conclusion This paper applies C++ Builder multithreading technology to the software design of an open CNC system, effectively solving the thread synchronization problem, ensuring the real-time requirements of the CNC software system, and achieving good application results.
Read next

CATDOLL 146CM A-CUP Alisa (TPE Body with Hard Silicone Head)

Height: 146cm A-cup Weight: 26kg Shoulder Width: 32cm Bust/Waist/Hip: 64/54/74cm Oral Depth: 3-5cm Vaginal Depth: 3-15c...

Articles 2026-02-22