Use a command only once
Show older comments
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 Comments
Walter Roberson
on 19 Sep 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
on 19 Sep 2022
Accepted Answer
More Answers (1)
KSSV
on 19 Sep 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;
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!