KPM RITM: real-time control¶
This example examines model execution on a RITM CPM with task execution time (TET) control: the TET variable is plotted interactively, the same values are analysed from the profiling file, and, in addition, the RITM's responses to model calculation step overflows are tested.
Introduction¶
Once the RITM real-time machine and its working environment have been set up, it is possible to proceed directly to the application of rapid prototyping and/or semi-natural modelling techniques. These and other approaches are based on real-time testing.
Real-time testing is the study of the functioning of a system (or its components) under real operating conditions - at a given sampling rate, with the effects of real equipment and interfaces, taking into account the synchronisation of various running tasks. Real-time testing is used in the technologies of rapid prototyping of a control algorithm (or Rapid Prototyping), semi-natural modelling of a control object (also known as HIL, or Hardware-in-the-Loop), and in applications to data acquisition and signal processing.
As it follows from this definition, real-time testing is ensured by the compliance of calculations with a given sampling period - the step of the model calculation. Accordingly, during modelling on the RITM CPM, it is necessary to estimate and control the time of task execution (TET, or Task Execution Time), as well as to control the model execution on the RITM in case of non-compliance with the real-time testing conditions.
To solve these problems, Engee has several features and tools, which we use further in the current example.
Model overview¶
The model of this example is based on the model from the example CPM RITM: Quick Start. The difference is the block РИТМ Время выполнения
, which can be found in the block library: РИТМ -> RITM Utilities -> RITM Task Execution Time -> RITM-TET
.
Below is the example model with the block RITM-TET
.
The signal tet
at the output of the block RITM-TET
will be recorded for analysis in the graph window Engee.
Modelling in Engee of this system was considered in the previous example, so in the current example we will consider modelling only on the RITM CPM in the interactive execution mode.
Calculation of lead times¶
After switching the calculation space from Engee to RITM KPM, open model settings and enable TET calculation:
Also let's switch on profiling and set the number of profiling points - this will allow us to save TET values to a text file in the RITM memory. The model runtime is $2\ c$, the calculation step is $1\cdot 10^{-3}$. In order to save all TET values during the modelling process, it is necessary to set 2000 profiling points.
Modelling results¶
After running the model execution on the RITM CPM in interactive mode, the following signal change can be observed at the output of the RITM-TET
block:
The Engee graph builder has the necessary functionality for initial graph analysis. For a more detailed, accurate and visual analysis of task execution time, we can go to the RITM CPM command shell and read the model profiling file ritm_dcm_get_tet_profile.txt
. Let's save the model profiling file and analyse the runtime data using the julia code below.
Analysing the model runtime¶
Let's connect the script with the auxiliary function of reading .txt files into the vector variable:
# Подключаем скрипт
include("$(@__DIR__)/txt_to_vector.jl");
# Получаем вектор значений из файла
tet = TxtToVec("$(@__DIR__)/ritm_dcm_get_tet_profile.txt");
# Шаг расчёта модели
StepTime = 1e3; # мкс
Install libraries and import modules for data analysis and display.
Pkg.add(["StatsBase", "Printf"])
import StatsBase.mean, Printf.@printf
Display messages with data about the analysed sample, minimum, average and maximum values, as well as the percentage of model runtime and model calculation step.
@printf "Количество точек профилирования: %d\n\n" length(tet)
@printf "Минимальное TET: %.3f мкс\n" tet_min = (minimum(tet)/1000)
@printf "Среднее TET: %.3f мкс\n" tet_mean = (mean(tet)/1000)
@printf "Максимальное TET: %.3f мкс\n\n" tet_max = (maximum(tet)/1000)
print("Диапазон TET относительно шага расчёта модели, %: ")
@printf "%.1f - %.1f" tet_min/StepTime*100 tet_max/StepTime*100
As it was defined earlier, we have received a given number of profiling points.
For clarity, we can also present the obtained data graphically:
gr(legend=:right, title="Время выполнения модели\n на КПМ РИТМ",
xlabel="Время модели, мс", ylabel="Время выполнения, мкс")
plot(tet./1000; label="TET")
plot!([0, 2000], [tet_min, tet_min]; label="TET_MIN", color=:green)
plot!([0, 2000], [tet_mean, tet_mean]; label="TET_MEAN", color=:purple)
plot!([0, 2000], [tet_max, tet_max]; label="TET_MAX", color=:red)
As a result of the analysis we have established that the execution time of the model does not exceed its calculation step. However, with a smaller step - e.g. $53 мкс$ or, even more so, $10 мкс$, the model calculation on the RITM CPM will partially or completely fail to comply with the real-time testing principle.
The RITM.Real-Time and Engee Real-Time Operating System (RTOS)1 and Engee have tools to monitor and control the model calculation in real time.
Calculation step overflow¶
To test the real-time test control capabilities, let's set the calculation step in the example model to $1\cdot 10^{-6} с$.
To control overflow of the calculation step in Model Settings in the RITM section, let's enable detection of overflow of the calculation step. First, we define the action when a calculation step overflow occurs as "Notify". When a calculation step overflow occurs, the RITM.Monitor GUI in the Application Log window will display the corresponding messages - Overrun detected: rate #0, model time ...!
, but this will not prevent RITM from executing the model up to the final simulation time.
In case of setting the action on overflow of calculation step "Finish" and specifying the number of allowed overflows, the model execution on RITM will be automatically stopped when the specified number of overflows is reached. RITM.Monitor in the Application Log window for this case will display messages about reaching the maximum number of allowed overflows - Maximum number of allowed overruns reached, stopping model execution!
and stopping the model.
Conclusion¶
In the example, we have looked at the available tools and features for controlling task execution time in RITM.Real Time, the block library and the Engee settings for RITM. These functionalities allow better selection of model settings and/or optimisation of the model itself for real-time execution.