This article is devoted to explaining how cyclic program calls work in Codesys 2.3 and Codesys 3.5. In this article, we will find the difference in the implementation of program management on these platforms, consider the principle of operation of the task manager, define the types of tasks implemented in these programs, and focus on the "watchdog" function.
In any version of Codesys, there may be situations where one task in execution time may coincide with another. In this case, it is necessary to set the priority, which of the tasks is more important. For example, you have scheduled tasks for a year in advance. One of these tasks is to go to a friend's birthday party. And you also have a daily task - go to the store every day. If you give priority to the store, you may not get to your friend's birthday, although you have been waiting for this event for a whole year.
Single-core processors use a task manager, which is a tool for solving multitasking issues by dividing the tasks performed by time and priority. Typically, we handle inputs and outputs more often, but applications and rendering less often. Thus, we build a run loop for different tasks. So, for example, there is a program for reading inputs, which is processed with a discreteness of 10 milliseconds, there is a program with a control algorithm. These programs are processed sequentially. If the time for processing inputs is over, then proceed to processing the application program. If the time has not yet expired, then, bypassing the processing of the application program, we turn to the block of writing to the outputs. And so in a circle. Everything seems to be simple, but this is as long as there are only two such tasks. When there are more such tasks, when they are busy with serious calculations, then the role of the task manager in executing programs with different frequency increases significantly. We are faced with the need to prioritize program processing.
Periodic differentiation is necessary for a more correct use of the processing power of the CPU (central processing unit of the controller). There is no reason to make the same program calls that can be made less frequently. So, a program using a video processor takes more time than, for example, processing any formulas or logic. Drawing is a resource-intensive process and there is no need to update the picture more often than necessary to implement basic functions.
Let's define the main differences between programming environments.
Priorities in Codesys 2.3 are specified in dimensions from 0 to 15, in Codesys 3.5 they are already in the range from 0 to 31. All of these priorities are related to the allocation of processor time. A program with a lower priority number will have a higher priority and more CPU time to run the associated program.
The type of multitasking in Codesys 2.3 is called “surrogate”, where one task replaces another. The task manager in this version implements the specified logic by priority. The higher priority will be executed.
In Codesys 3.5, the "preemptive" principle of multitasking. If there are two tasks that need to be executed at the same time, the program will implement sequential execution, one task after the other. A higher priority task will be eligible to be completed first, and a lower priority task will also be completed next. If tasks with the same priority are called, the one that waits the most will be executed. This rule is necessary so that a situation does not arise when one task is not performed at all.
Let's consider this difference in the following example. We will create two simple programs on Codesys 2.3 and on Codesys 3.5 in one project. We will make one program with a execution cycle of 10 milliseconds, and another program specially with an error - with an infinite loop. Let's add two tasks in the task manager, dividing them by priority. The second task (with an infinite loop) is given a lower priority. In each program section, add a counter.
What happens when the execution time coincides? What will happen to the controller and how will it behave? After starting Codesys 2.3, if the "watchdog" function was not activated, we will see the controller stop, since it stopped at one of the tasks and could not exit. He went to work on a single process and became unresponsive. This is due to the code execution architecture in Codesys 2.3.
At the same time, in Codesys 3.5, the program does not stop. The values of the counters continue to change. Despite the fact that the program does not have its end, it is nevertheless updated and has time to be processed.
Thus, the significant difference between Codesys 2.3 and Codesys 3.5 is the difference in prioritization. Thus, the priorities in Codesys 2.3 are defined by seniority and the "highest over lowest" priority is clearly implemented. In Codesys 3.5, CPU time is reallocated. Allocating this time for one priority or another is a feature of preemptive multitasking.
Attention! In the case of preemptive multitasking, we are always talking about a processor with one core. Where there are many cores, the processes are more complicated and we will not talk about this in this article.
Let's take a closer look at the types of Codesys tasks.
• Type of tasks "Cyclic".
When the time is right, the program starts executing. It is executed as long as required by the program itself, then the program stops, waits for the next timer operation time and is executed again. When using several cyclical tasks, you should try to set them at different times in order to reduce the number of possible collisions, and set different priorities for everyone.
• Task type "Event"
The task manager keeps track of a bound variable of type "BOOL". When a variable changes its value from "FALSE" to "TRUE", then a single, 1 cycle long, call of the associated program occurs. For example, in this way you can fix a sensor closure, transmit a diagnostic message about an event that has occurred to the SCADA system. For this type of task, it is desirable to set the highest priority, so that it is guaranteed to be given the execution of cyclical or free tasks.
• Type of problems "Free"
From the first cycle of the controller, interrogation of the associated program begins. At the end of the action of this program, it is executed the next time without delays, it just starts immediately. It has no time frame. The program is called with the frequency of its execution, that is, this frequency can change depending on the processor load, on the volume of logical calculations of this program. Accordingly, if we perform such types of tasks, then it is better to set them lower in priority than other types of tasks, otherwise the latter will simply not be completed.
• Task type "Status"
The task will be executed in the same way as the free one, with the minimum possible polling cycle, while the associated variable of the "BOOL" type is in the "TRUE" state. You should reduce the priority of this task, since it acts without delays in execution and can "overwrite" any of the tasks.
The controller has its own separate cycles (clocks), which are not tied to either reading inputs or executing programs. These are the cycles of the operating system.
Now a little more about the "Watchdog" function.
In the task manager there is a function for tracking freezes - "Watchdog". This function is intended to stop the execution of the program if the execution time is exceeded. So, if any of the programs will run longer than the time allotted to it, then when the "Watchdog" is triggered, the controller or task will not hang. It becomes possible to correct programs or reboot the controller.
For correct operation of the Watchdog timer function, there is a “sensitivity” parameter. This parameter determines how many times the time until the “Watchdog” is triggered can be exceeded. For example, if the "Watchdog" is triggered in Codesys 2.3, it will automatically reboot the PLC. This is done in order to exclude or reduce the degree of influence of the frozen PLC on the technological process when an error occurs in the code. There is a chance to return everything to normal, or at least to return all controller outputs to a safe state. If one of the tasks in Codesys 3.5 freezes, then this will not lead to a hangup of the process or a reboot of the PLC. The controller will continue its work, the program will generate diagnostic information about the exclusion of the task from processing.
In Codesys 3.5, we have diagnostics and can see the current status of task execution. Task manager diagnostics (monitoring):
"Status" - shows the current status of the execution. If "Not created" - the task has not been started since the last update. "Created" - the task is recognized in the runtime system, but not in operation. "Valid" - the task is working properly. "Exception" - the task was removed from processing. There is also "Active", which means that this task has problems.
"Counter" - has two fields, IEC and General. It is related to block call types. The total is usually larger.
"Pos. (µs) "- The time of the last measured cycle in microseconds, must be constantly changing.
Aver. cycle time (µs) - The total average time of all cycles for a given task in microseconds.
"Max. cycle time (µs) ”- maximum measured cycle time [µs]. This time can be longer than the specified time if the program does not have time to work out.
“Min. cycle time (µs) ”is the minimum measured cycle time [µs].
Jitter (µs) is the last measured jitter [µs]. Jitter is the difference between the time a task should start and the time the task actually starts.For example, if a task has to start every 1000 µs, but in fact it did not have time and is forced to start after 1100 µs, then the jitter is 100 µs. The task call time can be shifted with each call. This is the difference between the time the task should have started executing and the time when it actually started executing. Jitter, in fact, is essentially a numerical quantitative parameter that measures the magnitude of a given shift. The smaller it is, the better the PLC setting is.
#codesys, #codesys2.3, #codesys3.5, #taskmanager, #multitasking, #watchdog, #jitter, #cycle, #processing