Created
May 18, 2011 12:33
-
-
Save bsodmike/978493 to your computer and use it in GitHub Desktop.
Discrete PI Algorithm as a MATLAB S-file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function [sys,x0,str,ts]=pialg2(t,x,u,flag,k,z1,dt,vmin,vmax) | |
%discrete PI algorithm | |
%v(nh)=k*u(nh)-k*z1*u((n-1)h)+v((n-1)h) | |
% dt sampling time, the same as h. | |
if abs(flag)==2 | |
sys(1)=k*(1-z1)*u+x(1); | |
elseif flag==3 | |
v=k*u+x(1); | |
if v>vmax | |
v=vmax; | |
elseif v<vmin | |
v=vmin; | |
else v=v; | |
end | |
sys(1)=v; %output signal | |
elseif flag==4 | |
sys(1)=t+dt; | |
t=sys(1); | |
elseif flag==0 | |
sys(1)=0; %number of continuous states | |
sys(2)=1; %number of discrete states | |
sys(3)=1; %number of outputs | |
sys(4)=1; %number of inputs | |
sys(5)=0; | |
sys(6)=0; | |
sys(7)=1; | |
x0(1)=0; | |
ts=[dt,0]; 2 | |
else | |
sys=[]; | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Found this on a chinese forum for PID control | |
% | |
s-function source code is as follows: | |
function [sys, x0, str, ts] = pidcon (t, x, u, flag, Kp, Ti, Td, T, E, uk) | |
global umax Ki Kd uk_1 ek_1 ek_2 B | |
switch flag | |
case 0,% initialize some | |
sizes = simsizes; | |
sizes.NumContStates = 0; sizes.NumDiscStates = 0; | |
sizes.NumOutputs = 1; sizes.NumInputs = 1; | |
sizes.DirFeedthrough = 1; sizes.NumSampleTimes = 1; | |
sys = simsizes (sizes); str = []; | |
ts = [T 0]; | |
umax = 50; uk_1 = 0; ek_1 = 0; ek_2 = 0; Ki = Kp * T / Ti; Kd = Kp * Td / T% initialize the controller parameters | |
case 3,% controller output calculation | |
ek = u;% for the error | |
if abs (ek) <E | |
B = 1; | |
else | |
B = 0; | |
end | |
uk = uk_1 + Kp * (ek-ek_1) + B * Ki * ek + Kd * (ek-2 * ek_1 + ek_2);% PID control law | |
if uk> umax | |
uk = umax; | |
end | |
if uk <-umax | |
uk =- umax; | |
end | |
uk_1 = uk; ek_2 = ek_1; ek_1 = ek; | |
sys = [uk]; | |
uk = uk_1 + Kp * (ek-ek_1) + B * Ki * ek + Kd * (ek-2 * ek_1 + ek_2) | |
case {1,2,4,9}, | |
sys = []; | |
otherwise | |
error (['Unhandled flag', num2str (flag)]); | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% copied the first file again | |
function [sys,x0,str,ts]=pialg2(t,x,u,flag,k,z1,dt,vmin,vmax) | |
%discrete PI algorithm | |
%v(nh)=k*u(nh)-k*z1*u((n-1)h)+v((n-1)h) | |
% dt sampling time, the same as h. | |
if abs(flag)==2 | |
sys(1)=k*(1-z1)*u+x(1); | |
elseif flag==3 | |
v=k*u+x(1); | |
if v>vmax | |
v=vmax; | |
elseif v<vmin | |
v=vmin; | |
else v=v; | |
end | |
sys(1)=v; %output signal | |
elseif flag==4 | |
sys(1)=t+dt; | |
t=sys(1); | |
elseif flag==0 | |
sys(1)=0; %number of continuous states | |
sys(2)=1; %number of discrete states | |
sys(3)=1; %number of outputs | |
sys(4)=1; %number of inputs | |
sys(5)=0; | |
sys(6)=0; | |
sys(7)=1; | |
x(1)=0; | |
ts=[dt,0]; | |
else | |
sys=[]; | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment