📄 tpl_seq.m
字号:
% Template for Matlab mechatronics control programs
% This template is for a single-thread, sequential scheduler
% File:tpl_seq.m
% Created 8/1/95 by DM Auslander
%glbl_var % 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
tfinal = 30;
del_t = 0.2; % Simulates the minimal increment of computing time
ept = del_t / 100; % Used to check for round-off errors
del_tsim = 0.6; % Simulation discretization interval. Must be >= del_t
% Use this to control the number of output values
t_outint = 0.5; % Time interval for outputs. Usually greater than del_t
% 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).
% Several simulation options are possible. Odeeul is a function
% that does a one-step Euler integration. It is very efficient, but very
% dangerous mathematically because it has absolutely no error control
% Another good option is to use the functions in Odesuite, a new
% Matlab integration package. The version used here has been modified
% with a parameter 'hfirst' so it can be used more efficiently in this
% hybrid (discrete-time/continuous-time) environment.
ode_opts = odeset('refine',0,'reltol',1.e-3); % Differential solver options
% 'refine' = 0 causes the ode solver to output only at the end
% 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('task1','task2','task3');
tasks = str2mat(t1);
nn = size(tasks);
ntasks = nn(1); % Number of rows in 'tasks'
% Set initial states
global S_task1 S_task2 S_task3
S_task1 = 0; % A state of 0 is used as a a flag to the
S_task2 = 0; % state to do whatever initialization it needs
S_task3 = 0;
% Also set up the state vector for ode simulation here if one is being used
% Initialize all the tasks
for j = 1:ntasks
tsk = tasks(j,:); % name of j-th task
feval(tsk); % No arguments - task takes care of state
% and when it runs internally.
end
i_audit = 1; % Index for transition audit trail
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) = out1;
yout(iout,2) = out2;
yout(iout,3) = out3;
yout(iout,4) = out4;
tout(iout) = tstep; % Record current time
iout = iout + 1;
end
% Run the control tasks
for j = 1:ntasks
tsk = tasks(j,:); % name of j-th task
feval(tsk); % No arguments - task takes care of state
% and when it runs internally.
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] = odeeul('proc_sys',del,x,ode_opts);
x = xx; % Update the state vector
end
end
tstep = get_time; % Update time
end
% Record the final output values
yout(iout,1) = out1;
yout(iout,2) = out2;
yout(iout,3) = out3;
yout(iout,4) = out4;
tout(iout) = tstep; % Last recorded time
%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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -