Include a new condition to solve a system using ODE45

์กฐํšŒ ์ˆ˜: 6 (์ตœ๊ทผ 30์ผ)
Mikel
Mikel 2023๋…„ 5์›” 4์ผ
ํŽธ์ง‘: Sam Chak 2023๋…„ 5์›” 4์ผ
Hello,
I'm programming a SMC control algorithm for a DC motor where it has to track a given trajectory. I'm using ODE45 to solve the equation system, but I have an issue, I dont know how can I include a step perturbation in the system for example in the second 4 of the loop. Here is my code:
Is it possible to do so?
clear variables;
close all;
clc;
%% Control of a DC motor using SMC control
% kirchoff
% ๐‘…๐‘– + L ( d๐‘– / ๐‘‘๐‘ก) = ๐‘ฃ โˆ’ ๐พ๐‘’m
%newton second law
% ๐ฝ๐œ”ฬ‡ + ๐‘๐œ” = ๐พ๐‘ก๐‘–
%expressed as space state
% dx1 = -b/j * x1 + Kt/j * x2
% dx2 = -Ke/L * x1 - R/L *x2 + 1/L *u
% y = x1
%% Init variables
% space state definition
A11 = -5.5;
A21 = -0.01852;
A12 = 37.5;
A22 = -0.1481;
A= [A11 A21; A21 A22];
B11 = 0;
B21 = 0.3704;
B = [B11; B21];
C = [1 0];
D = 0;
%convert to transfer function
[a,b]=ss2tf(A,B,C,D);
%% Controller design
% ๐œ”ฬˆ + 5.648๐œ”ฬ‡ + 1.509๐œ” = 13.89๐‘ฃ
% x1 = w
% x2 = dw
% u = v
% dx1 = x2
% dx2 = -5.648๐œ”ฬ‡ - 1.509๐œ” + 13.89u
%sliding variable
%s = ce + de
%where e = wd - w
%% CONTROL
% solving the ode
t0=0;
tfinal=8;
tspan = linspace(t0, tfinal, 8e4);
x0 = [0 0 0];
[t, x] = ode45(@(t,x) SMCf(t,x), tspan, x0);
s = zeros(numel(t),1);
u = s;
wd = s;
dwd = s;
for i = 1:numel(t)
[~, s(i),u(i),wd(i),dwd(i)] = SMCf(t(i), x(i,:));
end
%compute tracking error
y = x(:,1);
e = wd - y;
%% PID
sys=ss(A,B,C,D);
[gains,info] = pidtune(sys,'PID',10);
closedLoop = feedback(sys*gains,1);
%% FUNCTION
% describing the ode
function [dot,s,u,wd,dwd,ddwd] = SMCf(t, x)
%variables
c=5;
r=10;
% perturbation
f = step function
% yc, desired track
wd = 2*cos(t);
% compute desired velocity and acceleration
% dyc = gradient(yc,t);
dwd = -2*sin(t);
ddwd = -2*cos(t);
%sliding variable
s= dwd+c*wd-c*x(1)-x(2);
%control law U to satisfy ss'<0
u = r*tanh(s/0.01);
x1dot = x(2);
x2dot = -5.648*x(2)-1.609*x(1)+13.89*u;
% somehow include this step in the 4th second so:
x2dot = (-5.648*x(2)-1.609*x(1)+13.89*u) + f;
x3dot = x(1);
dot = [x1dot x2dot x3dot]';
end

๋‹ต๋ณ€ (1๊ฐœ)

Sam Chak
Sam Chak 2023๋…„ 5์›” 4์ผ
ํŽธ์ง‘: Sam Chak 2023๋…„ 5์›” 4์ผ
You can try the two approaches in the code.
However, the step value should not exceed fs = 130, or else your designed SMC cannot track well (unless you increase r).
% solving the ode
t0 = 0;
tfinal = 20;
tspan = linspace(t0, tfinal, 20001);
x0 = [0 0 0];
[t, x] = ode45(@(t, x) SMCf(t, x), tspan, x0);
% plotting the output
plot(t, 2*cos(t), t, x(:,1)), grid on
xlabel('t')
legend('\omega_{d}(t)', 'y(t)')
% describing the ode
function [dot,s,u,wd,dwd,ddwd] = SMCf(t, x)
%variables
c = 5;
r = 10;
% perturbation
ts = 4; % step time
fs = 139/1.08; % step output value (shouldn't exceed 130 or it cannot track)
f = fs*(t > ts); % step function (not a math expression, but a program syntax)
% f = fs*heaviside(t - ts); % Heaviside function (an acceptable math function for step)
% yc, desired track
wd = 2*cos(t);
% compute desired velocity and acceleration
% dyc = gradient(yc,t);
dwd = - 2*sin(t);
ddwd = - 2*cos(t);
%sliding variable
s = dwd + c*wd - c*x(1) - x(2);
%control law U to satisfy ss'<0
u = r*tanh(s/0.01);
x1dot = x(2);
x2dot = - 5.648*x(2) - 1.609*x(1) + 13.89*u + f;
x3dot = x(1);
dot = [x1dot x2dot x3dot]';
end

์นดํ…Œ๊ณ ๋ฆฌ

Help Center ๋ฐ File Exchange์—์„œ Ordinary Differential Equations์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ

ํƒœ๊ทธ

์ œํ’ˆ


๋ฆด๋ฆฌ์Šค

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by