在连续系统的数字PID控制仿真-2的基础上,利用S函数实现PID离散控制器的Simulink仿真。
在S函数中,采用初始化函数、更新函数和输出函数,即 mdlInitializeSizes函数、mdIUpdates函数和mdlOutputs函数。在初始化中采用sizes 结构,选择1个输出、2个输入。其中一个输入为误差信号,另一个输入为误差信号上一时刻的值。S函数嵌入在Simulink程序中,采样时间为1ms。仿真结果如图所示。
仿真图:
PID控制器程序:chap1_8s.m
function [sys,x0,str,ts]=exp_pidf(t,x,u,flag)
switch flag,
case 0 % initializations
[sys,x0,str,ts] =mdllnitializeSizes;
case 2 % discrete states updates
sys = mdIUpdates(x,u);
case 3 %computation of control signal
% sys = mdlOutputs(t,x,u,kp,ki,kd,MTab);
sys=mdlOutputs(t,x,u);
case{1,4,9} %unused flag values
sys= [ ];
otherwise %error handling
error(['Unhandled flag=',num2str(flag)]);
end;
% when flag=0, perform system initialization.%
function [sys,x0,str,ts] =mdllnitializeSizes
sizes = simsizes; % read default control variables
sizes.NumContStates= 0: % no continuous states
sizes.NumDiscStates = 3; % 3 states and assume they are the P/I/Dcomponents
sizes.NumOutputs=1; % 2 output variables: control u(t) and state x(3)
sizes.NumInputs = 2; % 4 input signals
sizes.DirFeedthrough=1: % input reflected directly in output
sizes.NumSampleTimes=1; % single sampling period
sys = simsizes(sizes);
x0=[0; 0;0]; % zero initial states
str =[];
ts=[-1 0]; % sampling period
% when flag=2,updates the discrete states%
function sys = mdlUpdates(x,u)
T=0.001;
sys=[ u(1);
x(2)+u(1)*T;
(u(1)-u(2)/T];
% when flag-3, computates the output signals
function sys = mdlOutputs(t,x,u,kp,ki,kd,MTab)
kp=1.5;ki=2.0;kd=0.05;
%sys=[kp,ki,kd]*x;
sys=kp*x(1)+ki*x(2)+kd*x(3);
作图程序:chap1_8plot.m
close all;
plot(t,y(:,1),r',ty(:,2),k:',linewidth’,2);
xlabel('time(s)');ylabel('yd,y';
legend('Ideal position signal',Position tracking');