Simulink Code Generation - Variable names changes of MATLAB function blocks in C++ generated code
2 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Mark McBroom
on 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
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!