⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tpl_min.m

📁 Matlab学习课件
💻 M
字号:
% Template for Matlab mechatronics control programs
% This template is for a single-thread, minimum-latency scheduler
% File:tpl_min.m
% Created 8/1/95 by DM Auslander
%  modified from the sequential scheduler, 8/2/95, DMA

% There are three changes needed to go from the sequential scheduler to
% the minimum latency scheduler:
% 1) An additional task list is set up
% 2) A loop is added in the execution section to run the intermittent tasks
% 3) A return value is added to the task function to indicate competion
%	This return value is ignored for tasks in the continuous-type list

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.2;	% 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,'rtol',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

% List of continuous tasks:
t1 = str2mat('task1','task2','task3');
tasks_con = str2mat(t1);
ncon = size(tasks_con);
ntasks_con = ncon(1);		% Number of rows in 'tasks'

% List of intermittent tasks:
% (If there are no intermittent tasks set ntasks_int=0 )
t1 = str2mat('task4','task5');
tasks_int = str2mat(t1);
nint = size(tasks_int);
ntasks_int = nint(1);		% Number of rows in 'tasks'

% Set initial states
Stask1 = 0;	% A state of 0 is used as a a flag to the
Stask2 = 0;	% state to do whatever initialization it needs
Stask3 = 0;
Stask4 = 0;
Stask5 = 0;
% Also set up the state vector for ode simulation here if one is being used

% 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
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_con
		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
		% Run the intermittent tasks - they all run for each scan
		for k = 1:ntasks_int
			tsk = tasks_int(k,:);	% name of k-th task
			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] = odeeul('proc_sys',del,x,ode_opts); 
					x = xx;	% Update the state vector
				end
			end  % of while ~flag
		end  % k-loop
	end % j-loop
	tstep = get_time; % Update time
end % i-loop
% 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 + -