Why doesn't this code work?

1 vue (au cours des 30 derniers jours)
채린
채린 le 16 Avr 2025
Commenté : Walter Roberson le 16 Avr 2025
function y = fcn(u)
%#codegen
u = n_psi_av
% initial waypoint
Xi = wp1(1,1);
Yi = wp1(1,2);
% final waypoint
Xf = wp2(1,1);
Yf = wp2(1,2);
% initial heading direction
Xav = 0;
Yav = 0;
n_Xav = 0;
n_Yav = 0;
% design variable(Kint <= 1.0)
Kint = 0.5
n_LOS = sqrt((Xf-n_Xav)^2 + (Yf-n_Yav)^2);
n_psi_leg = atan2(Xf-Xi,Yf-Yi);
n_psi_LOS = atan2(Xf-n_Xav,Yf-n_Yav);
zeta = atan2(Yf-Yi,Xf-Xi);
eta = (n_psi_LOS - n_psi_leg);
Op = n_LOS*(1-Kint);
Lp = Op*cos(eta);
Xvir = Xf-Lp*cos(zeta);
Yvir = Yf-Lp*sin(zeta);
n_psi_vLOS = atan2(Xvir-n_Xav,Yvir-n_Yav);
n_LOSv = sqrt((Xvir-n_Xav)^2 + (Yvir-n_Yav)^2);
n_psi_CMD = (n_psi_vLOS - n_psi_av);
n_XTRK = abs(n_LOSv*sin(n_psi_LOS-n_psi_leg))*sign(n_psi_LOSv-n_psi_av);
The code above is the code I wrote. I created Autopilot using simulink, and I wanted to connect the psi from it to the matlab function to obtain the psi command using the LOS guidance law. For this, I used the matlab function block, and the code I put in this block is the code above.
1. Can't I specify variables in matlab function?
2. If so, what should I change to make this code run smoothly?
  2 commentaires
Steven Lord
Steven Lord le 16 Avr 2025
What makes you think this code doesn't work in this context?
  • Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
  • Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
  • Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support so we can investigate.
At a quick scan, the first two lines of executable code in your function smells funny.
function y = fcn(u)
%#codegen
u = n_psi_av
Why does your function accept an input argument only to immediately overwrite that input argument with something else?
Walter Roberson
Walter Roberson le 16 Avr 2025
u = n_psi_av
% initial waypoint
Xi = wp1(1,1);
Yi = wp1(1,2);
n_psi_av and wp1 and wp2 look like uninitialized variables at that point. If you want to use the values of those variables then you should have them as input signals (possibly created with Constant blocks)

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Simulink Coder dans Help Center et File Exchange

Produits


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by