Use a command only once
Mostra commenti meno recenti
I use a code to search for an accurate rotor position. I used an initial value for the position (theta_b=0) as shown below and then the final position is theta_ij. I want to use the initial position theta_b=0 (line 5) only once after that I use the final position theta_ij as the initial position (theta_b=theta_ij) for the next search (next sample time). How can I do that?
% Inputs are reference voltage Vdr and Vqr,reference current ,Idr and Iqr
% and measured currents (ia, ib)
function theta = fcn(alpha_r, beta_r, d_est, q_est)
% initial rotor position and initial error
theta_b=0;
err_in=beta_r*d_est-alpha_r*q_est;
for i=0:7
theta_delta=(pi/4)*2^-i;
for j=0:7
theta_ij=theta_b+theta_delta*(j-4);
alpha_est=d_est*cos(theta_ij)-q_est*sin(theta_ij);
beta_est=d_est*sin(theta_ij)+q_est*cos(theta_ij);
% the cost function
err=beta_r*alpha_est-alpha_r*beta_est;
if (err < err_in)
theta_b=theta_ij;
err_in=err;
end
end
end
theta=theta_b;
2 Commenti
Walter Roberson
il 19 Set 2022
Is the "next sample time" the next call to fcn(), or is it within the same call?
If it is the next call to fcn(), are you willing to add an additional parameter for the first call, to set the initial value, with the additional calls not passing in the parameter? If you are not willing to use that arrangement, then how would you like the code to signal that it wants to start over at 0 (such as for another run of the program) ?
saleh shlimet
il 19 Set 2022
Risposta accettata
Più risposte (1)
KSSV
il 19 Set 2022
Make theta_b also a input variable.
% Inputs are reference voltage Vdr and Vqr,reference current ,Idr and Iqr
% and measured currents (ia, ib)
function theta = fcn(theta_b,alpha_r, beta_r, d_est, q_est)
% initial rotor position and initial error
err_in=beta_r*d_est-alpha_r*q_est;
for i=0:7
theta_delta=(pi/4)*2^-i;
for j=0:7
theta_ij=theta_b+theta_delta*(j-4);
alpha_est=d_est*cos(theta_ij)-q_est*sin(theta_ij);
beta_est=d_est*sin(theta_ij)+q_est*cos(theta_ij);
% the cost function
err=beta_r*alpha_est-alpha_r*beta_est;
if (err < err_in)
theta_b=theta_ij;
err_in=err;
end
end
end
theta=theta_b;
Categorie
Scopri di più su MATLAB in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!