📄 heat_pid.m
字号:
function heat_pid
% PID control computation for the heater control system
heatglbl % Global definitions
% The 'next-state'is maintained as a separate variable so that the
% initialization canbe handled correctly and so a test can be
% constructed to tell whether the entry function of a state
% should be run. The next-state variable must be static so it
% remembered across function invocations. The 'global' designation
% is the only way Matlab has of making a variable static.
% The default value of the next-state variable is -1, indicating
% that no state change has taken place.
global Sheat_pid_next intvv intv minmc maxmc setprev valueprev
% Next state for this task (other local statics
% can be listed here.
% These aren't really global, but do have to be 'static'
% Matlab doesn't have a 'static' declaration so this will have to do!
if Sheat_pid == 0
% Initialization section - occurs once only
Sheat_pid = 1; % Make sure this section is not executed again!
Sheat_pid_next = 1; % First state.
%The default is -1 which indicates stay in same state
duty_cycle = 0;
return;
end
% This task runs at specific times. If current time has not reached
% the scheduled run time, just return.
if tstep < Theat_pid
return;
end
Theat_pid = Theat_pid + Tpid_delt; % Time for next run -- in this case
% the task runs at regular intervals, but that needn't
% be true all the time.
if Sheat_pid_next ~= -1
% There has been a state change (including self-transition)
[i_audit,trans_trail] = audit(1,Sheat_pid,Sheat_pid_next,...
tstep,trans_trail,i_audit);
% Record this transition
Sheat_pid = Sheat_pid_next;
run_entry = 1;
Sheat_pid_next = -1; % Default - stay in same state
else
run_entry = 0;
end
% This task has only one state -- it produces a command duty cycle as
% a function of time, based on a PID feedback control calculation
% Run the code associated with the current state
if Sheat_pid == 1
% Entry section
if run_entry
value = x(1); % Temperature
valueprev = value;
intvv = 0; % Integrator value
setprev = temp_set;
minmc = 0; % No cooling for this system
maxmc = 1; % Maximum duty cycle
end
% Action section
value = x(1); % Temperature
[mc,intv] = pidcntrl(Tpid_delt,temp_set,setprev,value,valueprev,intvv,...
Kp,Ki,Kd,minmc,maxmc);
duty_cycle = mc;
intvv = intv; % New integrator value
valueprev = value; % Save values for next time
setprev = temp_set;
%Test/exit section
% This task has only one state - no transitions!
else % Check for a state variable value that doesn't
% to a defined state.
error('Task: heat_pid -- unknown state encountered');
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -