Multi-thread technology to solve the application design of real-time performance of open CNC system
How to complete the real-time control of multi-tasks is a key issue that must be solved in the development of numerical control systems. This article introduces the basic concept of multithreading technology, analyzes the application of multithreading technology in C++ builder environment, and gives the application of multithreading technology in realizing the real-time response of numerical control system through development examples. 1 Introduction Table thread creation and priority setting 4 multi-threaded implementation After doing the above preparations, you need to add the commands to start the thread and suspend the thread in the appropriate location in the main unit. The code looks like this: 4.3 About thread synchronization Changzhou Jinxin ARTS & CRAFTS Co.,Ltd , https://www.jxpartyconfetti.com
Real-time performance is an important performance indicator of CNC systems.
Although this master-slave structure ensures that the motion control commands are executed in the motion controller at high speed and in real time on an open CNC system development platform composed of an IPC (Industrial Personal Computer) and a motion controller, it is on the PC. However, many tasks such as real-time display, pre-processing calculations, and system status monitoring are still required. In order to ensure the real-time performance of the system, multi-thread technology is proposed to improve the real-time performance of the system through multi-task parallel processing.
The development platform adopts an open CNC system with IPC+ motion controller mode, and the main motion control is completed by the GT400-SV universal motion controller of Googol. It provides C language function library GT400sv.lib and Windows dynamic link library GT400.dll, which can implement complex control functions. The development of numerical control system is to integrate these control functions with application program modules such as data processing, interface display, and user interface required by its own control system to build a control system that meets the requirements of specific applications.
2 Processes and Threads and Multithreading 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 transferred to memory for execution, including execution code of the currently executed 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 code in its own address space. This thread is called the main thread. If the main thread finishes running, the system will automatically clear the process and other address spaces.
A thread is a path executed internally by a process. It is a basic entity that the operating system allocates CPU time to, and is the smallest unit of program execution. Each process is started by the main thread for application execution. A thread consists of a stack, the state of CPU registers, and an entry in the system call list. Each process can contain more than one thread. These threads can execute the code in the process address space independently and share all the resources in the process.
The minimum unit for allocating processor time in a Windows system is a thread, and the system constantly switches between threads. In a PC, only one thread is running at the same time. Usually the time slice of the system for each thread is very small (ms level), so that the real-time nature of the fast system is guaranteed.
To implement multithreaded programming, worker Threads and User Interface Threads can be established. The auxiliary thread is mainly used to execute NC programs, coordinate display, dynamic simulation, and data preprocessing; the user interface thread is used to process user input and respond to user generated events and messages.
3 Numerical control system real-time analysis
3.1 real-time thread
There are many tasks that the numerical control system needs to accomplish. The priorities of these tasks are different. Based on this, it is possible to use the multi-tasking, preemptive, and multithreading techniques of the Windows system to assign each task to different threads and assign different priorities to each thread. When a high-priority thread executes, that is, real-time requirements. When a high task needs to be executed, it can automatically terminate the work of other threads and execute this thread instead. By this method, the real-time performance required by the numerical control system can be achieved.
3.2 Auxiliary thread creation
The helper threads created in this development system can be roughly divided as follows:
(1) Coordinate display thread
In manual pulse panel, electric control panel and incremental control panel, the coordinates of the three motion axes X, Y, and Z can be displayed in real time. This allows the operator to visually see the actual coordinates of the three axes. Real-time requirements are low, so use the lowest priority: Lowest Normal.
(2) graphics display thread
The image display thread is used to execute graphics drawing instructions in the dynamic simulation panel. Through the graphic display, the operator can operate the man-machine interface while dynamically simulating. This thread has lower real-time requirements and is rated as Blow Normal.
(3) IO state control thread
This thread is used to detect the various discrete quantities input by the system, as well as the commands from the NC program to output the status of each discrete quantity of the machine tool. This thread has a higher priority than the first two threads. The level is: Normal.
(4) data preprocessing threads
The data preprocessing thread is mainly responsible for performing the execution of the motion control data preprocessing functions such as coding form conversion, tool length compensation, tool radius compensation, and metric conversion. The level is: Normal.
(5) Motion Control Threads
This thread is mainly used by the motion controller to execute the NC code function. Responsible for inputting motion control commands to the buffer, clearing buffers, and opening and closing buffers. Higher level: Above Normal.
(6) emergency control thread
This thread processes some time that requires the machine to react immediately, such as an emergency stop of the machine tool. The highest priority is: Highest.
The helper threads created in this system can be roughly divided as shown in the following table.
In the Windows operating system, multi-threaded implementation needs to call a series of API functions, such as CreateThread, ResumeThread, etc., which are bothersome and error-prone. Using the TThread class in the next-generation RAD development tool C++ Builder, you can easily implement multi-threaded programming, especially for the Windows operating system whose system development language is C. It has unparalleled advantages over other programming languages.
4.1 Thread Creation
Although the TThread object in C + + Builder illustrates the concept of the thread, but the TThread object itself is not complete, you need to create its subclass under TThread, and overload Execute to use the thread object.
In the C + + Builder IDE environment, select the menu File | New, select the Thread Object in the New column, press OK, in the pop-up dialog box, enter the name of the TThread object subclass CoordinateDisplyThread, automatically create a TThread subclass CoordinateDisply. At the same time, a unit named CoordinateDisplyThread is created in the editor.
4.2 Thread implementation
The Execute() function in the created code is where the code of the task to be implemented in the thread resides. The CoordinateDisplayThread.h file is included in the original Unit1.cpp code. When used, dynamically create a TCoordinateDisplay object, the specific implementation of the code is Execute () method overloaded code.
Since the functions that the threads added in Execute() need to run when calling the VCL component and the VCL objects are not thread-safe, their features and methods can only be accessed in the main thread, so use the Synchronize() function to coordinate Display functions for packaging. The coordinate display function needs to declare: void_fastcall Function().
The following shows the implementation steps of coordinate display thread CoordinateDisplayThread as an example to illustrate the specific method of thread implementation. The implementation of other threads needs to be corrected according to the specific situation.
Add the following statement in the CoordinateDisplayThread::Execute() function in the CoordinateDisplayThread.cpp file to achieve the consistency of the X, Y, and Z coordinates of the function call.
First use the switch statement to determine which axis's coordinate position in the single-axis movement has changed:
Thread synchronization is very important in programming techniques. When a thread accesses a process object, if another thread wants to change the object, it may produce erroneous results. In the development and application of this example, using API functions, you can use critical or mutex directly to achieve the purpose of synchronization. In order to improve the reliability and flexibility of synchronization, flag variables and critical mechanisms are used at the same time. Just declare a variable Sect1 of type TRTLCriticalSection in the program and initialize it in the main thread's constructor. Later in a thread, the corresponding code can be marked as a critical section. When EnterCriticalSection() is called in one thread and Sect1 is passed, multiple data members are set to indicate that the critical section is active. If another thread wants to call its own critical section, the function EnterCriticalSection() will find that a critical section is in use, leaving the second thread to sleep until the first thread exits the critical section.
5 Concluding remarks
This paper applies the C++ builder multithreading technology to the software design of the open CNC system, effectively solves the thread synchronization problem, guarantees the real-time requirements of the numerical control software system, and achieves good application results.