📄 heatctrl.m
字号:
% Simulation of heater control using a PWM actuation
% This system uses minimum latency scheduling
% File: heatcntrl.m
% Created 8/4/95 by DM Auslander
% Based on pwm_samp.m
heatglbl % Read in global definitions -- because each function
%in Matlab requires a new file, this is a more compact
%way to share variables
% System setup and initialization
% Put initial values for all variables here
% Simulation parameter values (time units are seconds)
tfinal = 20;
%del_t = 0.02; % Use del_t=0.01 for pwm_period=0.5; 0.02 for 1.0
del_t = 0.0115; % For calibrated time
del_tsim = 0.06;
ept = del_t / 100; % Used to check for round-off errors
% Use this to control the number of output values
t_outint = del_t * 5; % Time interval for outputs
% This is particularly important for the student edition!
nouts = ceil(tfinal / t_outint);
yout = zeros(nouts + 1,4); % Set this up for proper output dimension
tout = zeros(nouts + 1,1);
t_outnext = 0; % Time at which next output will be copied
iout = 1; % Index for outputs
tstep = 0; % Running time
% Set up the options for the differential equation solver
% (if one is being used). The lines given here use a modified
% version of "Odesuite"
ode_opts = odeset('refine',0,'reltol',1.e-3); % Differential solver options
% Make up a task list using the string-matrix function, str2mat()
% Note that this can only take 11 strings in each call, so it must be called
% multiple times for more than 11 tasks
% Each name represents a function (file) for that task
%t1 = str2mat('heat_pid','heat_sup'); % Continuous tasks
%t1 = str2mat('heat_pid','heat_sup','heat_pwm'); % Continuous tasks
t1 = str2mat('heat_pid','heat_sup','dummy1','dummy1','dummy1'); % Continuous tasks
tasks_con = str2mat(t1);
nncon = size(tasks_con);
ntasks_con = nncon(1); % Number of rows
t1 = str2mat('heat_pwm'); % Intermittent tasks
tasks_int = str2mat(t1);
nnint = size(tasks_int);
ntasks_int = nnint(1); % Number of rows
%ntasks_int = 0;
% Initial variable values
duty_cycle = 0;
pwm_out = 0;
Tpid_delt = 1; % How often the PID control task should run
Theat_pid = 0; % Next run time for command
Kp = 3.5; % Controller gains
Ki = 4.0;
Kd = 0.0;
temp_soak = 0.5; % Processing temperature
temp_set = 0; % Initialization
soak_time = 5.0;
pwm_period = 1; % Two periods are used for this sample: 0.5 and 1
pwm_out = 0;
duty_cycle = 0;
% Variables associated with measuring pwm duty cycle
kdc = 0;
start_pwm = -1;
meas_duty = [];
start_off = -100;
% Set initial states
Sheat_pid = 0; % A state of 0 is used as a a flag to the
Spwm_gen = 0; % state to do whatever initialization it needs
Sheat_sup = 0;
% Target system parameters
Kin = 2; % Conversion from pwm_out
Kout = 1.5; % Heat loss coeffiecient
Csys = 10; % Target system total heat capacity
temp_amb = 0; % Ambient temperature
temp_init = 0.3;
x(1) = temp_init; % Initial temperature
dummy1;
% Initialize all the tasks
for j = 1:ntasks_con % Continuous tasks
tsk = tasks_con(j,:); % name of j-th task
feval(tsk); % No arguments - task takes care of state
% and when it runs internally.
end
for j = 1:ntasks_int % Intemittent tasks
tsk = tasks_int(j,:); % name of j-th task
feval(tsk); % No arguments - task takes care of state
% and when it runs internally. ('Completion' isn't
% relevant here)
end
i_audit = 1; % Index for transition audit trail
trans_trail = []; % Initialize transition autid trail
tic; % Start the real-time clock
tstep = 0;
tbegin = get_time; % Time at the beginning of simulation step
% Step out the solution
while tstep <= tfinal
% Copy output values to the output array
if (tstep + ept) >= t_outnext
t_outnext = t_outnext + t_outint; % Time for next output
yout(iout,1) = duty_cycle;
yout(iout,2) = pwm_out;
yout(iout,3) = x(1);
yout(iout,4) = temp_set;
tout(iout) = tstep; % Record current time
iout = iout + 1;
end
% Run the control tasks
for j = 1:ntasks_con
tsk = tasks_con(j,:); % name of j-th task
feval(tsk); % No arguments - task takes care of state
% and when it runs internally.
inc_time; % Increment time
% Simulation stuff
del = get_time - tbegin; % Time since last simulation
if del >= del_tsim % If true, do a simulation step
tbegin = get_time;
[tt,xx,stats] = odeeul('heat_sys',del,x,ode_opts);
x = xx; % Update the state vector
end
for k = 1:ntasks_int
tsk = tasks_int(k,:); % name of k-th task
del = get_time - tbegin; % Time since last simulation
if del >= del_tsim % If true, do a simulation step
tbegin = get_time;
[tt,xx,stats] = odeeul('heat_sys',del,x,ode_opts);
x = xx; % Update the state vector
end
flag = 0; % Completion flag
while ~flag
flag = feval(tsk);
% Loop until task signals completion
inc_time; % Increment time
del = get_time - tbegin; % Time since last simulation
if del >= del_tsim % If true, do a simulation step
tbegin = get_time;
[tt,xx,stats] = odeeul('heat_sys',del,x,ode_opts);
x = xx; % Update the state vector
end
end
end % k-loop
end % j-loop
tstep = get_time; % Update time
end
% Record the final output values
yout(iout,1) = duty_cycle;
yout(iout,2) = pwm_out;
yout(iout,3) = x(1);
yout(iout,4) = temp_set;
tout(iout) = tstep;
%Trim the output arrays in case there are extra rows
% (which can happen in real time where the time increment is
% not known)
nry = size(yout,1);
if nry > iout
yout = yout(1:iout,:);
tout = tout(1:iout);
end
% Plot results
subplot(2,1,1); % Top subplot
plot(tout,yout(:,1),'k--');
ylabel('Duty Cycle');
subplot(2,1,2); % Bottom subplot
plot(tout,yout(:,3),'r',tout,yout(:,4),'k--');
ylabel('Temperature');
xlabel('Time');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -