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

조회 수: 9 (최근 30일)
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

채택된 답변

Mark McBroom
Mark McBroom 2020년 3월 22일
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
Luca Nanu
Luca Nanu 2020년 3월 27일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulink Coder에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by