📄 s_xmp3.m
字号:
function [sys,x0,str,ts] = s_xmp3(t,x,u,flag,Kp,Ki,Kd, ... L_upper,L_lower,T_samp)% S-file example 3% MATLAB S-file implementation of discrete PID controller.%% Based on sfuntmpl.m, supplied with SIMULINK% Copyright (c) 1990-96 by The MathWorks, Inc.%M=1;m=1;l=1;switch flag, case 0, % Initialization [sys,x0,str,ts]=mdlInitializeSizes(T_samp) ; case 1, % Compute derivatives of continuous states sys=mdlDerivatives(t,x,u) ; case 2, sys=mdlUpdate(t,x,u,L_upper,L_lower,T_samp); case 3, sys=mdlOutputs(t,x,u,Kp,Ki,Kd,T_samp); % Compute output vector case 4, % Compute time of next sample sys=mdlGetTimeOfNextVarHit(t,x,u); case 9, % Finished. Do any needed sys=mdlTerminate(t,x,u); otherwise % Invalid input error(['Unhandled flag = ',num2str(flag)]);end%********************************************************%* mdlInitializeSizes *%********************************************************function [sys,x0,str,ts]=mdlInitializeSizes(T_samp)% Return the sizes of the system vectors, initial % conditions, and the sample times and offets.sizes = simsizes; % Create the sizes structuresizes.NumContStates = 0;sizes.NumDiscStates = 1;sizes.NumOutputs = 1;sizes.NumInputs = 1;sizes.DirFeedthrough = 1;sizes.NumSampleTimes = 1; sys = simsizes(sizes); % load sys with sizes structurex0 = [0]; % Specify initial conditions for all states str = []; % str is always an empty matrixts = [T_samp 0]; %initialize the array of sample timesset_param(gcb,'UserData',0) ;%********************************************************%* mdlDerivatives *%********************************************************function sys=mdlDerivatives(t,x,u,M,m,l)% Compute derivatives of continuous statessys = [];%********************************************************%* mdlUpdate *%********************************************************function sys=mdlUpdate(t,x,u,L_upper,L_lower,T_samp)% Compute update for discrete states. If necessary, check % for sample time hits.x_new = [x(1) + T_samp*u(1)] ;x_new = min(max(x_new,L_lower),L_upper) ; % Prevent wind-upsys = x_new ;%********************************************************%* mdlOutputs *%********************************************************function sys=mdlOutputs(t,x,u,Kp,Ki,Kd,T_samp)% Compute output vector given current state, time, inputu_prev = get_param(gcb,'UserData') ;set_param(gcb,'UserData',u(1)) ; % Get input at % previous sampleu_deriv = (u(1) - u_prev)/T_samp ; % Save current inputsys = Kp*u(1) + Ki*x(1) + Kd*u_deriv ;%********************************************************%* mdlGetTimeOfNextVarHit *%********************************************************function sys=mdlGetTimeOfNextVarHit(t,x,u)% Return the time of the next hit for this block. sys = [] ;%********************************************************%* mdlTerminate *%********************************************************function sys=mdlTerminate(t,x,u)% Perform any necessary tasks at the end of the simulationsys = [];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -