## How to implement Extended Kalman Filter’s State Transition Function and Measurement Function for PMSM?

Hello, the TLDR of this post is that I am trying to learn how to implement a sensorless control for a 3 phase PMSM using the EKF Simulink block, but I can’t seem to make it work. I’m not sure if I implemented the State Transition Function or the Measurement Functions correctly.

The EKF Simulink block is set up as such

The equivalent Simulink implementation is as such

I have set the Covariance Matrix Q as NoiseMatrix(1:4) and Covariance Matrix R as NoiseMatrix(5:6) in the EKF block parameter below

StateTransitionFunction

The relevant state space equations of the 3 phase PMSM is derived as such

and I implemented the State Transition function as such

function x_next = pmsmStateTransition_3p(x,u)

% represents the dynaics of the PMSM and how the states of the …

% evolves over time

% x=[i_d; i_q; omega_e; theta_e] (state vector)

% u=[v_alpha; v_beta] (input vector)

% ——– Inputs ——– %

i_d=x(1);

i_q=x(2);

omega_e=x(3);

theta_e=x(4);

v_alpha=u(1);

v_beta=u(2);

% ——– Motor Parameters ——– %

R = 0.007184;

L = 19.5e-6;

pole_pairs = 6; %pole pairs

J = 3.37862;

B = 0;

lambda = 0.0623;

dt = 9e-5; % Ts

% ——– Equations ——– %

lambda_q = i_q*L;

lambda_d = i_d*L+lambda;

T_e = 3/2*pole_pairs*(i_q*(i_d*L + lambda) – i_d*i_q*L);

v_d = cos(theta_e)*v_alpha + sin(theta_e)*v_beta;

v_q = -sin(theta_e)*v_alpha + cos(theta_e)*v_beta;

% State Transition Equations

% Euler Discretization is used to calculate the next states

id_next = i_d + (v_d – R*i_d + omega_e*lambda_q)/L*dt;

iq_next = i_q + (v_q – R*i_q – omega_e*lambda_d)/L*dt;

omega_next = omega_e + (T_e – B*omega_e)/J*dt;

theta_next = theta_e + omega_e*dt;

% ——– Output ——– %

x_next=[id_next; iq_next; omega_next; theta_next];

end

Measurement Function

function y = pmsmMeasurement_3p(x)

% x=[i_d; i_q; omega_e; theta_e] (state vector)

% y=[i_alpha; i_beta] (output vector)

% ——— Inputs ———- %

i_d=x(1);

i_q=x(2);

theta_e=x(4);

% ——– Equations ——– %

i_alpha = i_d*cos(theta_e) – i_q*sin(theta_e);

i_beta = i_d*sin(theta_e) + i_q*cos(theta_e);

% ——— Output ———- %

y=[i_alpha; i_beta];

end

Results

I understand that the covariance matrices determines the results. After I set everything up, I would find the optimal values using the Genetic Algorithm, but even then the results from it don’t differ so much from the estimated we_hat in the bottom figure for results, so I just used a generic placeholder value of 100 for all values of the Noise Covariance Matrices (i.e. Q = [100, 100, 100, 100] and R = [100, 100]).

Have I implemented the functions properly? I would appreciate any help at all to help lessen the amount of things I need to debug. Thank you so much and I look forward to your reply.

I have attached all the relevant files in this post, so please let me know what you need and I can direct you to the relevant file.Hello, the TLDR of this post is that I am trying to learn how to implement a sensorless control for a 3 phase PMSM using the EKF Simulink block, but I can’t seem to make it work. I’m not sure if I implemented the State Transition Function or the Measurement Functions correctly.

The EKF Simulink block is set up as such

The equivalent Simulink implementation is as such

I have set the Covariance Matrix Q as NoiseMatrix(1:4) and Covariance Matrix R as NoiseMatrix(5:6) in the EKF block parameter below

StateTransitionFunction

The relevant state space equations of the 3 phase PMSM is derived as such

and I implemented the State Transition function as such

function x_next = pmsmStateTransition_3p(x,u)

% represents the dynaics of the PMSM and how the states of the …

% evolves over time

% x=[i_d; i_q; omega_e; theta_e] (state vector)

% u=[v_alpha; v_beta] (input vector)

% ——– Inputs ——– %

i_d=x(1);

i_q=x(2);

omega_e=x(3);

theta_e=x(4);

v_alpha=u(1);

v_beta=u(2);

% ——– Motor Parameters ——– %

R = 0.007184;

L = 19.5e-6;

pole_pairs = 6; %pole pairs

J = 3.37862;

B = 0;

lambda = 0.0623;

dt = 9e-5; % Ts

% ——– Equations ——– %

lambda_q = i_q*L;

lambda_d = i_d*L+lambda;

T_e = 3/2*pole_pairs*(i_q*(i_d*L + lambda) – i_d*i_q*L);

v_d = cos(theta_e)*v_alpha + sin(theta_e)*v_beta;

v_q = -sin(theta_e)*v_alpha + cos(theta_e)*v_beta;

% State Transition Equations

% Euler Discretization is used to calculate the next states

id_next = i_d + (v_d – R*i_d + omega_e*lambda_q)/L*dt;

iq_next = i_q + (v_q – R*i_q – omega_e*lambda_d)/L*dt;

omega_next = omega_e + (T_e – B*omega_e)/J*dt;

theta_next = theta_e + omega_e*dt;

% ——– Output ——– %

x_next=[id_next; iq_next; omega_next; theta_next];

end

Measurement Function

function y = pmsmMeasurement_3p(x)

% x=[i_d; i_q; omega_e; theta_e] (state vector)

% y=[i_alpha; i_beta] (output vector)

% ——— Inputs ———- %

i_d=x(1);

i_q=x(2);

theta_e=x(4);

% ——– Equations ——– %

i_alpha = i_d*cos(theta_e) – i_q*sin(theta_e);

i_beta = i_d*sin(theta_e) + i_q*cos(theta_e);

% ——— Output ———- %

y=[i_alpha; i_beta];

end

Results

I understand that the covariance matrices determines the results. After I set everything up, I would find the optimal values using the Genetic Algorithm, but even then the results from it don’t differ so much from the estimated we_hat in the bottom figure for results, so I just used a generic placeholder value of 100 for all values of the Noise Covariance Matrices (i.e. Q = [100, 100, 100, 100] and R = [100, 100]).

Have I implemented the functions properly? I would appreciate any help at all to help lessen the amount of things I need to debug. Thank you so much and I look forward to your reply.

I have attached all the relevant files in this post, so please let me know what you need and I can direct you to the relevant file. Hello, the TLDR of this post is that I am trying to learn how to implement a sensorless control for a 3 phase PMSM using the EKF Simulink block, but I can’t seem to make it work. I’m not sure if I implemented the State Transition Function or the Measurement Functions correctly.

The EKF Simulink block is set up as such

The equivalent Simulink implementation is as such

I have set the Covariance Matrix Q as NoiseMatrix(1:4) and Covariance Matrix R as NoiseMatrix(5:6) in the EKF block parameter below

StateTransitionFunction

The relevant state space equations of the 3 phase PMSM is derived as such

and I implemented the State Transition function as such

function x_next = pmsmStateTransition_3p(x,u)

% represents the dynaics of the PMSM and how the states of the …

% evolves over time

% x=[i_d; i_q; omega_e; theta_e] (state vector)

% u=[v_alpha; v_beta] (input vector)

% ——– Inputs ——– %

i_d=x(1);

i_q=x(2);

omega_e=x(3);

theta_e=x(4);

v_alpha=u(1);

v_beta=u(2);

% ——– Motor Parameters ——– %

R = 0.007184;

L = 19.5e-6;

pole_pairs = 6; %pole pairs

J = 3.37862;

B = 0;

lambda = 0.0623;

dt = 9e-5; % Ts

% ——– Equations ——– %

lambda_q = i_q*L;

lambda_d = i_d*L+lambda;

T_e = 3/2*pole_pairs*(i_q*(i_d*L + lambda) – i_d*i_q*L);

v_d = cos(theta_e)*v_alpha + sin(theta_e)*v_beta;

v_q = -sin(theta_e)*v_alpha + cos(theta_e)*v_beta;

% State Transition Equations

% Euler Discretization is used to calculate the next states

id_next = i_d + (v_d – R*i_d + omega_e*lambda_q)/L*dt;

iq_next = i_q + (v_q – R*i_q – omega_e*lambda_d)/L*dt;

omega_next = omega_e + (T_e – B*omega_e)/J*dt;

theta_next = theta_e + omega_e*dt;

% ——– Output ——– %

x_next=[id_next; iq_next; omega_next; theta_next];

end

Measurement Function

function y = pmsmMeasurement_3p(x)

% x=[i_d; i_q; omega_e; theta_e] (state vector)

% y=[i_alpha; i_beta] (output vector)

% ——— Inputs ———- %

i_d=x(1);

i_q=x(2);

theta_e=x(4);

% ——– Equations ——– %

i_alpha = i_d*cos(theta_e) – i_q*sin(theta_e);

i_beta = i_d*sin(theta_e) + i_q*cos(theta_e);

% ——— Output ———- %

y=[i_alpha; i_beta];

end

Results

I understand that the covariance matrices determines the results. After I set everything up, I would find the optimal values using the Genetic Algorithm, but even then the results from it don’t differ so much from the estimated we_hat in the bottom figure for results, so I just used a generic placeholder value of 100 for all values of the Noise Covariance Matrices (i.e. Q = [100, 100, 100, 100] and R = [100, 100]).

Have I implemented the functions properly? I would appreciate any help at all to help lessen the amount of things I need to debug. Thank you so much and I look forward to your reply.

I have attached all the relevant files in this post, so please let me know what you need and I can direct you to the relevant file. ekf, foc, pmsm, sensorless, state transition function, measurement function, simulink, r2023a MATLAB Answers — New Questions