Include a new condition to solve a system using ODE45

6 views (last 30 days)
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

Answers (1)

Sam Chak
Sam Chak on 4 May 2023
Edited: Sam Chak on 4 May 2023
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

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!