主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

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

此示例显示如何使用 System object™ 方法控制 MATLAB 系统模块的采样时间。

在类定义中,使用 System object 采样时间方法配置采样时间并根据当前仿真时间修改 System object 行为。如果您想使用继承的采样时间,则不需要在 System object 定义中指定采样时间。

指定采样时间

要指定采样时间,请实现getSampleTimeImpl方法并使用createSampleTime创建采样时间规范对象。

在这个示例中,创建了一个属性 SampleTimeTypeProp,用于根据不同的属性值分配采样时间。getSampleTimeImpl 方法根据 SampleTimeTypeProp 属性创建采样时间规范。getSampleTimeImpl 方法返回 createSampleTime 创建的采样时间规范对象 sts

18        methods(Access = protected)
19            function sts = getSampleTimeImpl(obj)
20                switch char(obj.SampleTimeTypeProp)
21                    case 'Inherited'
22                        sts = createSampleTime(obj,'Type','Inherited');
23                    case 'InheritedNotControllable'
24                        sts = createSampleTime(obj,'Type','Inherited',...
25                            'AlternatePropagation','Controllable');
26                    case 'InheritedErrorConstant'
27                        sts = createSampleTime(obj,'Type','Inherited',...
28                            'ErrorOnPropagation','Constant');
29                    case 'FixedInMinorStep'
30                        sts = createSampleTime(obj,'Type','Fixed In Minor Step');
31                    case 'Discrete'
32                        sts = createSampleTime(obj,'Type','Discrete',...
33                          'SampleTime',obj.SampleTime, ...
34                          'OffsetTime',obj.OffsetTime);
35                    case 'Controllable'
36                        sts = createSampleTime(obj,'Type','Controllable',...
37                            'TickTime',obj.TickTime);
38                end
39            end

查询仿真时间和采样时间

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

40            
41            function [Count, Time, SampleTime] = stepImpl(obj,u)
42                Count = obj.Count + u;
43                obj.Count = Count;
44                Time = getCurrentTime(obj);
45                sts = getSampleTime(obj);
46                if strcmp(sts.Type,'Controllable')
47                   setNumTicksUntilNextHit(obj,obj.Count);
48                end
49                SampleTime = sts.SampleTime;
50            end

Simulink 中的行为

将此 System object 包含在 MATLAB 系统模块中。

在范围内,您可以看到采样时间变化对模块的影响。

完整类定义

CountTime System object 的完整类定义。

classdef CountTime < matlab.System
    % Counts Hits and Time
    
    properties(Nontunable)
        SampleTime = 1.4; % Sample Time
        OffsetTime = 0.2; % Offset Time
        TickTime = 0.1;
        SampleTimeTypeProp (1,:) char {mustBeMember(SampleTimeTypeProp, ...
            {'Discrete','FixedInMinorStep','Controllable',...
            'Inherited','InheritedNotControllable',...
            'InheritedErrorConstant'})} = 'Discrete'
    end
    
    properties(DiscreteState)
        Count
    end
    
    methods(Access = protected)
        function sts = getSampleTimeImpl(obj)
            switch char(obj.SampleTimeTypeProp)
                case 'Inherited'
                    sts = createSampleTime(obj,'Type','Inherited');
                case 'InheritedNotControllable'
                    sts = createSampleTime(obj,'Type','Inherited',...
                        'AlternatePropagation','Controllable');
                case 'InheritedErrorConstant'
                    sts = createSampleTime(obj,'Type','Inherited',...
                        'ErrorOnPropagation','Constant');
                case 'FixedInMinorStep'
                    sts = createSampleTime(obj,'Type','Fixed In Minor Step');
                case 'Discrete'
                    sts = createSampleTime(obj,'Type','Discrete',...
                      'SampleTime',obj.SampleTime, ...
                      'OffsetTime',obj.OffsetTime);
                case 'Controllable'
                    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 char(obj.SampleTimeTypeProp)
                case {'Inherited', ...
                        'InheritedNotControllable', ...
                        'FixedInMinorStep'}
                    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

另请参阅

函数

主题