www.gusucode.com > Arduino_Engineering_Kit_Hardware_Support_18b 工具箱matlab源码程序 > Arduino_Engineering_Kit_Hardware_Support_18b/simulink/+motorcarrier/+blocks/mkrmotorcarrier_DCMotor.m

    classdef mkrmotorcarrier_DCMotor < motorcarrier.blocks.mkrmotorcarrier_base ...
        & matlab.system.mixin.internal.CustomIcon
    %
    % MKR Motor Carrier DC Motor 
    %
    
    % Copyright 2018 The MathWorks, Inc.
    
    %#codegen
    %#ok<*EMCA>
    
    properties (Nontunable)
        %MotorNumber Motor port        
        MotorNumber = 'M1';
        %MotorCarrierPWMFreq Motor carrier PWM frequency
        MotorCarrierPWMFreq = 100;                       
    end
    
    properties(Constant, Hidden)
        MotorNumberSet = matlab.system.StringSet({'M1', 'M2'});
        
        % --------------Commands for DC motor at D11---------------------
        SET_PWM_DUTY_CYCLE_DC_MOTOR      = 5   ;
        SET_PWM_FREQUENCY_DC_MOTOR       = 6   ; 
    end

    properties (Dependent,Hidden)
        MotorNumberIdx
    end   
            
    methods
        % Constructor
        function obj = mkrmotorcarrier_DCMotor(varargin)
            coder.allowpcode('plain'); 
        end 
        
        function idx = get.MotorNumberIdx(obj)
            switch obj.MotorNumber
                case 'M1'
                    idx = 0;
                case 'M2'
                    idx = 1;
            end
        end
    end
    
    methods (Access = protected)        
        function DCmotor_setDuty(obj, value)          
            mkrmotorcarrier_setData(obj, uint8(obj.SET_PWM_DUTY_CYCLE_DC_MOTOR),...
                uint8(obj.MotorNumberIdx), int32(value));
        end
        
        function DCmotor_setFrequency(obj)            
            mkrmotorcarrier_setData(obj, uint8(obj.SET_PWM_FREQUENCY_DC_MOTOR),...
                uint8(obj.MotorNumberIdx), int32(obj.MotorCarrierPWMFreq));
        end
    end
    
    methods (Access=protected)
        function setupImpl(obj)            
            if coder.target('Rtw')
                mkrmotorcarrier_init(obj);
                DCmotor_setFrequency(obj);
            else
                % Do nothing
            end
        end

        function stepImpl(obj, In1)
            if coder.target('Rtw')
                DCmotor_setDuty(obj, int32(In1));
            else
                % Do nothing
            end
        end

        function releaseImpl(obj)
            % Release resources, such as file handles
            if coder.target('Rtw')
                DCmotor_setDuty(obj, int32(0));
            end
        end
        
    end
    
    methods (Access=protected)
        %% Define input properties
        function num = getNumInputsImpl(~)
            num = 1;
        end
        
        function num = getNumOutputsImpl(~)
            num = 0;
        end
        
        function varargout = getInputNamesImpl(~)
            varargout{1} = 'Duty cycle';
        end 
        
        function varargout = isInputSizeLockedImpl(~,~)
            varargout{1} = true;
        end
        
        function varargout = isInputFixedSizeImpl(~,~)           
            varargout{1} = true;
        end
        
        function varargout = isInputComplexityLockedImpl(~,~)
            varargout{1} = true;
        end
        
        function maskDisplayCmds = getMaskDisplayImpl(obj)
            MotorIndex = ['Motor: ' obj.MotorNumber];
            maskDisplayCmds = [ ...
                ['color(''white'');',newline]...
                ['plot([100,100,100,100]*1,[100,100,100,100]*1);',newline]...
                ['plot([100,100,100,100]*0,[100,100,100,100]*0);',newline]...
                ['sppkgroot = strrep(motorcarrier.internal.getSpPkgRootDir(),''\'',''/'');',newline]...
                ['image(fullfile(sppkgroot,''resources'',''dcmotor.png''),''center'')',newline]...
                ['color(''blue'');',newline] ...
                ['text(99, 92, ''ARDUINO'', ''horizontalAlignment'', ''right'');', newline] ...
                ['color(''black'');',newline]...
                ['text(52,12,' [''' ' MotorIndex ''',''horizontalAlignment'',''center'');' newline]]   ...
                ['color(''black'');',newline]...
                ];
            
        end
    end
    
    methods (Static, Access=protected)
        function simMode = getSimulateUsingImpl(~)
            simMode = 'Interpreted execution';
        end
        
        function isVisible = showSimulateUsingImpl
            isVisible = false;
        end
        
        function [groups, PropertyList] = getPropertyGroupsImpl(~)
            % MKR Motor Carrier Base property list
            [~, PropertyListOut] = motorcarrier.blocks.mkrmotorcarrier_base.getPropertyGroupsImpl;
            MotorNumberProp = matlab.system.display.internal.Property(...
                'MotorNumber', 'Description', 'Motor port');
            MotorCarrierPWMFreqProp = matlab.system.display.internal.Property(...
                'MotorCarrierPWMFreq', 'Description', 'PWM frequency','IsGraphical', false);
            
            % Add to property list
            PropertyListOut{end+1} = MotorNumberProp;
            PropertyListOut{end+1} = MotorCarrierPWMFreqProp;
            
            Group = matlab.system.display.Section(...
               'PropertyList',PropertyListOut);
            
            groups = Group;
            
            % Output property list if requested
            if nargout > 1
                PropertyList = PropertyListOut;
            end
        end
        
        % Note that this is ignored for the mask-on-mask
        function header = getHeaderImpl
            %getHeaderImpl Create mask header
            %   This only has an effect on the base mask.
            header = matlab.system.display.Header(mfilename('class'), ...
                'Title', 'DC Motor', ...
                'Text', sprintf('Set the speed and direction of the DC motor on the selected port. \n\nThe block accepts an input between 100 to -100:\n* 100 = Full speed forward\n* 0 = Stop\n* -100 = Full speed reverse'), ...
                'ShowSourceLink', false);
        end
    end
    
    methods (Static)
        function name = getDescriptiveName()
            name = 'MKR Motor Carrier DC Motor';
        end
        
        function b = isSupportedContext(context)
            b = context.isCodeGenTarget('rtw');
        end
        
        function updateBuildInfo(buildInfo, context)
             if context.isCodeGenTarget('rtw')
                codertarget.arduinobase.internal.arduinoI2CWrite.updateBuildInfo(buildInfo, context);
             end
        end
    end

end