Improvement of embedded operating system μLC/OS-Ⅱ for numerical control system

Publisher:WanderlustGazeLatest update time:2011-08-03 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Abstract: In order to make the embedded operating system μC/OS-Ⅱ more suitable for the application of numerical control system, the task classification and task scheduling of μC/OS-Ⅱ are improved. Tasks are divided into ordinary tasks and preemptive tasks. Ordinary tasks refer to tasks scheduled and run by the operating system scheduler, which are divided into periodic tasks and random tasks. Periodic tasks correspond to tasks that are required to be executed regularly in numerical control systems, and random tasks correspond to tasks triggered and executed by signals in numerical control systems. Preemptive tasks are not scheduled and run by the scheduler, but are directly run in interrupt processing. Preemptive tasks correspond to tasks with high execution frequency and short execution time in numerical control systems. Experiments have shown that the improved μC/OS-Ⅱ is more suitable for adding tasks to numerical control systems.
Keywords: numerical control system; μC/OS-Ⅱ; task classification; task scheduling

The embedded operating system μC/OS-Ⅱ is a scalable, open source, compact, preemptive real-time multitasking kernel, mainly for small and medium-sized embedded systems, with high execution efficiency, small footprint, strong portability, excellent real-time performance and strong scalability. The numerical control system is a typical strong real-time system with determinism. Determinism mainly ensures that the time from the occurrence of a condition to the start (or end) of the action caused by it is within an accurate period of time. In the CNC system, the condition is caused by the operator's instructions (such as emergency stop, moving the x-axis, etc.) or the state of the machine tool (such as tool breakage, etc.). This paper analyzes the characteristics of CNC system tasks, and combines the kernel system of μC/OS-Ⅱ to improve the task classification, task scheduling and interrupt service strategy of μC/OS-Ⅱ, making it more suitable for the application of CNC system.

1 Classification of tasks by μC/OS-Ⅱ
Each task in μC/OS-Ⅱ has 5 states: dormant, ready, running, waiting, and interrupt (ISR). The tasks in the dormant state reside in the memory and have not been used by the kernel; the tasks in the ready state are ready to execute, with a lower priority than the currently executed tasks, and have not obtained the CPU control right; the tasks are in the running state after obtaining the CPU control right; the tasks waiting for events to occur are in the waiting state, and the events can be the completion of I/O operations, the availability of shared resources, the expiration of clock cycles, etc.; the task execution process is interrupted by the interrupt service routine, and the task is in the interrupt state.

2 Improvements of μC/OS-Ⅱ for CNC Systems
2.1 Characteristics of CNC System Tasks
In CNC systems, tasks can be divided into two types: periodic tasks and signal-triggered tasks. The signals mentioned here include both hardware signals and software signals. Periodic tasks include timed signal sampling, timed I/O port scanning, and timed communication. Signal-triggered tasks include interrupt service tasks triggered by interrupt signals (hardware signals), command interpretation tasks triggered by command message signals (software signals), etc. In addition, in CNC systems, some tasks also have the characteristics of short running time, high running frequency, and high real-time requirements, such as signal sampling and interpolation control in CNC machine tools.
2.2 Improved task division
In the improved embedded operating system, tasks are divided into two categories: ordinary tasks and preemptive tasks. Ordinary tasks refer to tasks that are scheduled and run by the operating system scheduler. The scheduling method is shown in Figure 1; preemptive tasks refer to tasks that are not scheduled and run by the scheduler, but are directly run in interrupt processing. The following is a detailed description of these two tasks.
2.2.1 Ordinary tasks
According to the characteristics that most of the tasks in the CNC system are periodic tasks and signal-triggered tasks, ordinary tasks are divided into two types: periodic tasks that run at a fixed time (referred to as periodic tasks) and random tasks that run when triggered by signals (referred to as random tasks). Correspondingly, the task status is divided into 6 types: running state, ready state, waiting state, stopped state, suspended state and interrupt state. Figure 1 is the improved task status switching diagram.

a.JPG


Among these six states, the running state, ready state and interrupt state correspond to READY, RUNNING and ISR of μC/OS-Ⅱ; the suspended state is the state in which a task is forced to stop running before the execution is completed due to waiting for an event or resource, waiting for the event or resource to arrive; the waiting state is the state in which a periodic task completes one run and waits for the running cycle to run again; the stopped state is the state in which a random task waits for its trigger signal. The dormant state is removed here, that is, there is no task deletion. Once all tasks are established, they will always exist during the operation of the system. This treatment is because in the application of numerical control systems, all established tasks must be useful, that is, they will be executed during the operation of the system, and useless codes and tasks will not be added. In μC/OS-Ⅱ, it is inappropriate to set each task as an infinite loop, that is, the task function will never return. This operating system allows the task function to return, and after returning, it calls the function OSTaskEndDeal(), which puts the periodic task into the waiting queue and the random task into the stop queue according to the category of the task.
2.2.2 Preemptive tasks
Preemptive tasks are tasks with short execution time and execution frequency higher than the OS system clock frequency (such as signal sampling), or tasks with high real-time requirements (such as interpolation control in CNC machine tools). The scheduling time (mainly the time spent on task switching) is often longer than the time it takes for such tasks to run once, which is obviously unreasonable. Preemptive tasks are designed to solve this unreasonable problem. Preemptive tasks are not scheduled to run through the OS scheduler, nor are they identified by TCB (task control block). Instead, they are directly processed in the interrupt when their interrupt trigger signal arrives, which saves time for scheduling and task switching. However, since preemptive tasks do not have TCBs and corresponding task stacks, special attention should be paid to the use of resources for preemptive tasks: they must use independent resources. This can not only allow preemptive tasks to run normally, but also prevent preemptive tasks from damaging the environment of the interrupted program. The specific methods are as follows:
(1) Dedicated register group. If registers are used in the handler, a dedicated register group is allocated to it, which also saves the time consumption of saving/restoring registers.
(2) Global variables. Because the local variables inside the function are allocated in the stack, the direct processing method does not form a task and does not have its own stack. If local variables are used, their local variables will be allocated in the stack of the interrupted task, so the task should use global variables in this method. Users should have certain restrictions when designing preemptive tasks, otherwise it will affect the response time of the system. The specific restrictions are as follows: First, the number cannot be too many, preferably less than or equal to 3; second, tasks must have short execution time and high execution frequency to be set as preemptive tasks.

3 Kernel data and functions that need to be modified
3.1 Modification of task control block
(1) Modify the meaning of OSTCBDly. In μC/OS-Ⅱ, OSTCBDly represents the number of clock beats of task delay, or the number of timeout clock beats when the task is suspended. If this variable is 0, it means that the task is not delayed, or that there is no limit on the time to wait for the event to occur. After the modification, OSTCBDly describes the number of cycles of task self-running, and its calculation formula is shown in formula (1). If this item is zero, the task is a random task triggered by a signal.
b.JPG
(2) Add an item OSTCBDlyD in TCB as the dynamic value of the task self-running cycle.
INT16UOSTCBDlyD; /*Task self-running cycle dynamic value*/ Among them, OSTCBDlyD is the dynamic value of the task waiting time. If the task is in the waiting state, its initial value is OSTCBD-ly; if the task is in the suspended state, its initial value is the timeout limit value. The system clock processing function OSTimeTick() decrements OSTCBDlyD by 1. When OSTCBDlyD is less than 0, the task is placed in the ready queue.
(3) Increase the value of OSTCBStat. OSTCBStat is the task status word. Since two task states, stop state and wait state, have been added, the value of OSTCBStat needs to be increased. The specific modification is to add the following information in the file μCOS_II.H.
#define OS_STAT_wAIT 0x40 //Task status word is wait state
#define OS_STAT_STOP 0x80 //Task status word is stop state
3.2 Modification of the clock tick function OSTimeTick()
Scan all TCBs through the TCB array and perform different processing according to the value of OSTCBStat. The specific processing is as follows:
For tasks in the ready state, OSTCBDlyD--, if there is a timeout, the timeout processing is performed.
For tasks in the suspended state, OSTCBDlyD--, if there is a timeout, it is inserted into the ready list.
Tasks in waiting state, OSTCBDlyD--, if the time comes, add them to the ready list.
Tasks in running state and stopped state are not processed.
3.3 Add function OSTaskEndDeal()
When the function of the current task is finished running and returns, call function OSTaskEndDeal(), which completes the work: first call OSTaskStkInit() to initialize the stack of the task; then determine whether the task is the lowest priority task. If so, keep the task in the ready state. The purpose of this is to make the lowest priority task always in the ready state, and the ready list will not be empty. If not, decide which queue the task should be inserted into according to OSTCBCUR→OSTCBDly. If the value is equal to zero, the task enters the stopped state;
if it is not equal to zero, the task enters the waiting state. Finally, call OSStart() to select the new task to run.
3.4 Implementation of preemptive tasks
The design of preemptive tasks needs to be processed according to the actual situation. Here, the implementation of preemptive tasks is introduced by taking the interpolation control in the CNC system as an example.
3.4.1 Function of interpolation control task
The interpolation control task is to calculate the displacement and direction of each coordinate axis in real time according to the processing command and the position of the current point. During interpolation, a timer is needed to control the interpolation speed. Each timer interrupt triggers a step of interpolation operation and output. When the interpolation reaches the end point, the timer stops and the interpolation is completed.
3.4.2 Implementation of interpolation control task
(1) Select a hardware timer as a dedicated timer for interpolation, and put the code of the interpolation task into the interrupt service program of the hardware timer.
(2) Set a set of dedicated register groups for the interrupt to reduce the time for saving and restoring the interrupt environment.
(3) Define the current position value and processing command (such as processing curve line shape, end point value) that need to execute the interpolation task as global variables to achieve fast information interaction with other tasks.
(4) Start the interpolation control task. Set the timer initial value according to the interpolation speed, initialize the global variables used for interpolation, and start the timer.

4 Software design of CNC machine tool execution controller based on improved μC/OS-Ⅱ
The CNC machine tool system adopts a two-level structure. The upper computer is composed of a dedicated software IPC, which is used to realize human-computer interaction; the lower computer is composed of an embedded execution controller of C8051020 and its peripheral circuits, which is responsible for real-time and reliable control. The embedded execution controller software is based on the improved μC/OS-Ⅱ design, and mainly includes the following functions: communication with IPC (sending tasks and receiving tasks), command interpretation (command interpretation tasks) and command execution (processing monitoring tasks, interpolation calculation tasks, gap voltage detection and limit switch status detection tasks). Among them, communication has two aspects: sending and receiving; when interpreting commands, the switch control commands are directly executed; during command execution, interpolation calculations, detection of gap voltage and limit switch status, and processing monitoring are required. The specific design of each task is shown in Table 1.

e.JPG



5 Conclusion
Based on the characteristics of CNC system tasks, this paper improves the task division and task scheduling of μC/OS-Ⅱ, making it easier to add CNC system tasks to μC/OS-Ⅱ and also easier for μC/OS-Ⅱ to manage different tasks. Preemptive tasks reduce the time spent on task switching and improve the utilization of hardware resources, but it should be noted that the number of preemptive tasks should not be too large, preferably less than or equal to 3, otherwise it will affect the normal operation of the OS. The improved kernel has been applied to actual projects, and the system is stable and reliable.

Reference address:Improvement of embedded operating system μLC/OS-Ⅱ for numerical control system

Previous article:Embedded quantitative analysis system based on ARM7 chip S3C44BOX
Next article:Research on memory management strategy based on embedded device browser

Latest Industrial Control Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号