SCHEDULING POLICIES IN EMBEDDED CONTROL SYSTEMS Lect.univ. LUMINITA GIURGIU ABSTRACT This paper demonstrates the effect of the scheduling policy on the global control performance in the case of three tasks running concurrently on the same CPU and controlling three different dynamic systems. The simulation is realised in the TrueTime MatLab toolbox environment. The advances obtained in microelectronics have led to the situation where almost all control algorithms are realized by computers. More than that, the controllers are often implemented as one or several tasks on a microprocessor with a real time operating system. The operating system uses multiprogramming to multiplex the execution of the various tasks. This is why embedded control systems are most often subject to limited computer resources that can be viewed as shared resources for witch the tasks compete. Tasks interfere with each other through preemption and blocking when waiting for common resources. I. Computer based control implementation The basic structure of a computer based control system is shown in Figure no. 1. Fig. no. 1 Diagram of a computer controlled system The continuous process output is sampled at regular time intervals and converted to digital form by an A/D converter. The control algorithm reads the sampled process output and computes a control signal that is converted back to analog form by a D/A converter. The D/A conversion is usually performed by keeping the output constant between conversions, so called zero order hold. A periodic control loop is implemented by the pseudo code in Listing1. The reading of inputs and writing of output signals correspond to direct calls to external A/D and D/A conversion interfaces. It is also possible to have the sampling and actuation being performed by dedicated tasks, when buffers are used to communicate the values between the tasks. In the case of a networked control system the reading and writing of signals also involve communication with other nodes in the network. In order to minimize the input output delay, the control algorithm is divided into two parts, where the first part computes the control signal based on current measurements and previous states, and the second part then updates the internal states of the controller for the next sample. Listing 1 t = currentTime(); LOOP Read Inputs; Control Calculation; Write Outputs; Update Internal States; t = t + h; waitUntil(t) END II. Real-time scheduling Real time scheduling theory is concerned with the problem of, given a set of tasks, finding an execution order that guarantees that all tasks meet their timing constraints. Real time scheduling algorithms fall in two basic categories: static and dynamic scheduling. Static scheduling is an offline approach, where an optimized execution order is determined once and for all before the system is commissioned. This execution order is then repeated cyclically at runtime. In dynamic scheduling schemes, the decision of which task to run is taken at runtime. The schedulability theory is based on a task model where all tasks are periodic and where each task, i, is characterized by the following parameters: a fixed period, Ti, a hard deadline, Di, and a fixed and known worst case execution time (WCET), Ci. Preemptive fixed priority scheduling is the mechanism supported by all major commercial real time operating systems. Using this approach, each task is assigned a fixed priority value. During runtime, the ready task with the highest priority gets access to the CPU. If a task with a lower priority is currently running, this task is preempted by the higher priority task. For control tasks it is natural to assume that the relative deadlines, Di, of the tasks are equal to their periods, Ti. In this case the most common priority assignment is the rate monotonic assignment, where the priorities are set according to the periods of the tasks: the shorter the period, the higher the priority. Assuming a set of n tasks, a sufficient condition for schedulability using the rate monotonic priority assignment is that the utilization factor U satisfies the condition: n 1 C U = ∑ i ≤ n(2 n − 1) T i =1 i (1) In the more general case where Di < Ti, deadline monotonic priority assignment is optimal [Liu and Layland, 1973]. Here the priorities are assigned according to the relative deadlines of the tasks. For any fixed priority scheduling assignment, an exact schedulability analysis may be performed by computing the worst case response times, Ri, for each task, see [Joseph and Pandya, 1986]. Using fixed priority assignment the priorities of the tasks are static and not changed during runtime. An alternative approach is earliest dead line first (EDF) scheduling which exploits dynamic priority assignment based on the absolute deadlines of the tasks. At any point in time, the task with the shortest remaining time to its deadline will get access to the CPU. EDF is more resource effective than rate monotonic scheduling and a necessary and sufficient condition for schedulability (given Di = Ti) is that the utilization factor is below one: n C U = ∑ i ≤1 T i =1 i (2) A benefit of dead line based scheduling over priority based scheduling is that it is more intuitive to assign deadlines to tasks than to assign priorities. Global information about the relative importance of all tasks in the system is needed in order to assign priorities, which is not required to assign deadlines. The main drawback with EDF is that it offers no guarantees at all during overload. In that case all tasks will miss their deadline, which is known as the domino effect [Stankovic et al., 1998]. For hard real time systems this may be fatal. However, the result during overload under EDF is that the effective periods of the tasks will be scaled in such a way that the utilization of the system is still 100 per cent [Cervin et al., 2002]. Under reasonable overload, this fair distribution of resources will, for most control systems, still give reasonable performance for all loops. III. True time simulation tool There are numerous tools that support simulation of control systems or simulation of real time scheduling, but very few tools support co-simulation of control systems and real time scheduling. The True Time simulator is a complete co simulation tool based on MATLAB/Simulink and in its current version it supports task scheduling by arbitrary scheduling policies, network simulation by standard MAC layer protocols, and a variety of real time primitives used for experimentation with flexible scheduling and compensation schemes. The case under study takes three PID tasks running concurrently on the same CPU, controlling three different systems. The TrueTime blocks are connected with ordinary Simulink blocks to form a real time control system, see Figure no. 2. Fig. no. 2 The control system The three systems are controlled by controller tasks implemented in a TrueTime kernel block. Before a simulation can be run, it is necessary to initialize kernel blocks and to create tasks for the simulation. The execution of tasks is defined by code functions. A code function is further divided into code segments according to the execution model. All execution of user code is done in the beginning of each code segment. The execution time of each segment should be returned by the code function. The kernel is initialized by the initialization script, providing the number of inputs and outputs, the scheduling policy and creating periodic tasks that uses defined code functions. In the initialization script the rate-monotonic scheduling is specified by the function ttinitkerenel and the task are created with ttCreatePeriodicTask function who specifies the name of the associated code function. The code function is given in Listing 2. Listing 2 function [exectime, data] = code_f(seg, data) switch seg, case 1, r = ttAnalogIn(data.rChan); % read reference y=ttAnalogIn(data.yChan); % read process output data = calc(data, r, y); % calculate PID action exectime = 0.002; case 2, ttAnalogOut(data.uChan, data.u); % output control signal exectime = -1; end Running the simulation of the control system, seven graphics are obtained: three graphics for the control signals, three graphics for the reference and outputs signals and the schedule graphic of the tasks during the simulation. Studying the computer schedule and the control performance it is noticed that task1 will miss all its deadlines (see Figure no. 3) and the corresponding control loop is unstable (see Figure no. 4). Fig. no. 3 Computer schedule (RM case): task1 (blue), task2 (green), task3 (red) By changing the scheduling policy to earliest-deadline-first and running a new simulation it is noticed that after an initial transient all task will miss their deadlines (see Figure no. 5), but still the overall control performance is satisfactory (see Figure no. 6). Fig. no. 4 Control loop associated with task 1 (reference blue, output signal green) Fig. no. 5 Computer schedule (EDF case): task1 (blue), task2 (green), task3 (red) Fig. no. 6 Stable control loop associated with task 1 IV. Conclusions A priority based approach will favor high priority tasks over low priority tasks, with the possible consequence that low priority tasks may not execute at all, being starved. Using dead line based scheduling, the available CPU time will be distributed among the tasks in a more fair way and better results concerning the stability of the control loops will be obtained. Depending on the application this may or may not be a desired feature. The major drawback with dead line based scheduling is the lack of commercial products supporting it. BIBLIOGRAPHY 1. Åström, B. Wittenmark, Computer Controlled Systems, Prentice Hall, Upper Saddle River, N.J., 1997 2. Årzén, K.-E., Real-Time Control Systems, Department of Automatic Control, Lund, Sweden, Lund Institute of Technology, 2001