文档

为MATLAB系统块系统对象指定采样时间

这个例子展示了如何使用System object™方法控制MATLAB系统块的采样时间。

在类定义中,使用matlab.system.mixin.SampleTime方法配置采样时间,并根据当前仿真时间修改系统对象行为。

指定采样时间

要指定采样时间,请实现getSampleTimeImpl方法。在本例中,是属性SampleTimeTypeProp以根据不同的属性值分配示例时间。的getSampleTimeImpl方法创建示例时间规范SampleTimeTypeProp财产。的getSampleTimeImpl方法返回示例时间规范对象sts设置采样时间规格。

函数sts = getSampleTimeImpl(obj)切换obj。SampleTimeTypePropcase 'Inherited sample time' sts = createSampleTime(obj,'Type','Inherited'); case 'Inherited not controllable sample time' sts = createSampleTime(obj,'Type','Inherited',... 'Disallow','Controllable'); case 'Fixed In Minor Step sample time' sts = createSampleTime(obj,'Type','Fixed In Minor Step'); case 'Discrete sample time' sts = createSampleTime(obj,'Type','Discrete',... 'SampleTime',obj.SampleTime, ... 'OffsetTime',obj.OffsetTime); case 'Controllable sample time' sts = createSampleTime(obj,'Type','Controllable',... 'TickTime',obj.TickTime); end end

查询模拟时间和采样时间

使用getSampleTimegetCurrentTime方法分别查询MATLAB系统块的当前采样时间和仿真时间。getSampleTime返回一个示例时间规范对象,该对象具有描述示例时间设置的属性。

函数[y,ct,st] = stepImpl(obj,u) y = obj。Count + u;obj。Count = y;ct = getCurrentTime(obj);sts = getSampleTime(obj);如果比较字符串(sts)。类型,“可控”) setNumTicksUntilNextHit (obj obj.Count);结束st = sts.SampleTime;结束

完整的类定义

类型CountTime.m
classdef CountTime < matlab。System & matlab. System .mix . sampletime %计数命中数和时间属性(不可调)SampleTimeTypeProp = '离散采样时间';%样本时间类型SampleTime = 1.4;%样本时间偏移= 0.2;%偏移时间TickTime = 0.1;结束属性(离散状态)计数结束属性(常量,隐藏)SampleTimeTypePropSet = matlab.system.StringSet(…){'继承样本时间',…“遗传不可控采样时间”,……“固定在小步骤采样时间”,…离散采样时间,… 'Controllable sample time'}); end methods(Access = protected) function sts = getSampleTimeImpl(obj) switch obj.SampleTimeTypeProp case 'Inherited sample time' sts = createSampleTime(obj,'Type','Inherited'); case 'Inherited not controllable sample time' sts = createSampleTime(obj,'Type','Inherited',... 'Disallow','Controllable'); case 'Fixed In Minor Step sample time' sts = createSampleTime(obj,'Type','Fixed In Minor Step'); case 'Discrete sample time' sts = createSampleTime(obj,'Type','Discrete',... 'SampleTime',obj.SampleTime, ... 'OffsetTime',obj.OffsetTime); case 'Controllable sample time' sts = createSampleTime(obj,'Type','Controllable',... 'TickTime',obj.TickTime); end end function [Count, Time, SampleTime] = stepImpl(obj,u) Count = obj.Count + u; obj.Count = Count; Time = getCurrentTime(obj); sts = getSampleTime(obj); if strcmp(sts.Type,'Controllable') setNumTicksUntilNextHit(obj,obj.Count); end SampleTime = sts.SampleTime; end function setupImpl(obj) obj.Count = 0; end function resetImpl(obj) % Initialize / reset discrete-state properties obj.Count = 0; end function flag = isInactivePropertyImpl(obj,prop) flag = false; switch obj.SampleTimeTypeProp case {'Inherited sample time', ... 'Inherited not controllable sample time', ... 'Fixed In Minor Step sample time'} if any(strcmp(prop,{'SampleTime','OffsetTime','TickTime'})) flag = true; end case 'discrete' if any(strcmp(prop,{'TickTime'})) flag = true; end case 'controllable' if any(strcmp(prop,{'SampleTime','OffsetTime'})) flag = true; end end end end end

将此System对象包含在MATLAB系统块中。

模型=“CountTime_model”;open_system(模型);sim(模型);

在作用域中,您可以看到样例时间更改对块的影响。

open_system([模型/范围的]);

另请参阅

|||||

相关的话题

这个话题有帮助吗?