Simulink Code Generation - Variable names changes of MATLAB function blocks in C++ generated code

2 vues (au cours des 30 derniers jours)
Good evening, I'm generating C++ code from a model in Simulink using the Embedded Coder toolbox, in which are present some MATLAB functions. In the generated code I saw that the variable names present in the Matlab function blocks of the model are changed with other variable names. For example the next are the lines of MATLAB code I used in one of the MATLAB function blocks:
function K_ec_bar = K_control(T, ec_bar_d, ec_bar, Ac_CT, Bc_CT, mu_K)
eig_des_K_control = exp(mu_K*T); % mu_K is a 2x1 vector
Ac_DT=Ac_CT*T+eye(1);
Bc_DT=Bc_CT*T;
poly_K=conv([1 -eig_des_K_control(1)],[1 -eig_des_K_control(2)]);
kp=(poly_K(2)+Ac_DT+1)/Bc_DT;
ki=(Bc_DT*kp-Ac_DT+poly_K(3))/Bc_DT;
K_ec_bar=kp*ec_bar_d+ki*ec_bar;
end
The generated C++ code for this part is:
eig_des_K_control_idx_0 = std::exp(U.mu_K[0] * U.T);
b2 = std::exp(U.mu_K[1] * U.T);
Ac_DT = -14.014686154499964 * U.T + 1.0;
Bc_DT = 11.449906988970557 * U.T;
kp = (((-eig_des_K_control_idx_0 + -b2) + Ac_DT) + 1.0) / Bc_DT;
Ac_DT = ((Bc_DT * kp - Ac_DT) + -b2 * -eig_des_K_control_idx_0) / Bc_DT * DW_l.ec_bar_Delay_DSTATE + kp * ec_bar_d;
In this case the variable "eig_des_control" (a 2x1 vector) is splitted in 2 variables, the code generator called them as "eig_des_K_control_idx_0" and "b2": the last one is called with another variable name I've used in another part of the model.
This is not the only case, I saw many of this changes in my generated code.
I want to know if there are code generation settings or naming rules for these function blocks to avoid this mixing of names, and possibly select what name give to the variables in the final code.
Thanks in advance

Réponse acceptée

Mark McBroom
Mark McBroom le 22 Mar 2020
Try adjusting the "Loop unrolling threshold" to 0 or 1. What you are encountering is an optimization. It is usually more efficient to operate on each element of a small vector rather than using a for loop.https://www.mathworks.com/help/releases/R2020a/rtw/ref/optimization-pane-general.html
  1 commentaire
Luca Nanu
Luca Nanu le 27 Mar 2020
I tried lowering the "Loop unrolling threshold" setting value and I've seen that the result is better (the variables name are not mixed), but the code becomes very long and sometimes some variables definition and initialization are repeated.
In my case, even with high optimization and after many tries of real implementation, the code seems to work well: I think that I can accept the optimized code with some variable names changed, to avoid too much lines of code.
Thanks for the answer anyway.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by