How to access members of a Class from the C code generated by the Embedded coder

1 visualización (últimos 30 días)
Hello,
I have a class like this:
classdef MyClass < matlab.System
properties
K = 0.1
end
...
end
This class is used in my model and I set a default value of 0.2 (Matlab System component).
The generated C++ code for the step function looks like:
if (rtDW.obj_d.K != 0.2) {
rtDW.obj_d.K = 0.2;
}
What is the purpose of this code? I would like to be able to access K from the C code and possibly change the value of K at runtime. Is there a mechanism to do that?
Thanks

Respuestas (1)

Shrey Tripathi
Shrey Tripathi el 27 de Jun. de 2023
The purpose of the generated C++ code you provided is to ensure that the value of the K property in the MyClass class matches the default value set in the Simulink model. If the current value of K is different from the default value (0.2 in your case), it will be updated to match the default value.
Regarding your question to be able to access K from the C code and possibly change the value of K at runtime:
To access and modify the value of K at runtime from the generated C code, you can first define a global variable in your C code to hold the value of K. For example:
double myClass_K = 0.1; // Initial value
Then, in the generated C code, instead of using rtDW.obj_d.K, you can use the global variable myClass_K:
if (myClass_K != 0.2) {
myClass_K = 0.2;
}
Now, you can access and modify the value of K at runtime by manipulating the myClass_K global variable in your C code.
Point to be noted: modifying the value of K at runtime from C code might have implications for the behavior of your Simulink model. Ensure that any changes to K are consistent with the intended behavior of your system.
Additionally, keep in mind that when using Embedded Coder, the generated code is typically intended for deployment on an embedded target. Modifying properties of a MATLAB class at runtime from the generated C code might not be the best practice in embedded systems. It's recommended to carefully design and test your system to ensure that it meets the desired requirements.
  2 comentarios
Pascal
Pascal el 27 de Jun. de 2023
Let's assume that changing a member variable of a class is safe because this is an atomic operation (writing a single or an int). We can make it completely safe by doing the change before calling the step function that has to be called every X ms.
The global variable will work if I have one only one instance of MyClass, but I have few instances on MyClass, so this is not a good solution.
The property K is tunable because it does not have the Nontunable attribute, but how can I tune K if it is reset to the default value at every step?
Pascal
Pascal el 27 de Jun. de 2023
I think I have found the answer. There is a parameter that controls that:
  • Default parameter behavior needs to be set tu tunable as opposed to inlined.
I will experiment with that.

Iniciar sesión para comentar.

Categorías

Más información sobre Simulink Coder en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by