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

    classdef mkrmotorcarrier_ServoMotor < matlab.System ...
        & coder.ExternalDependency ...
        & matlab.system.mixin.Propagates ...
        & matlab.system.mixin.internal.CustomIcon
    %
    % Servo Motor 
    %
    
    % Copyright 2018 The MathWorks, Inc.
    
    %#codegen
    %#ok<*EMCA>
    
    properties (Nontunable)
        %MotorNumber Motor port        
        MotorNumber = '1';
        %MotorCarrierPWMFreq Motor carrier PWM frequency
        MotorCarrierPWMFreq = 100;       
        % SlaveAddress_ - I2C Slave Address
        SlaveAddress_ = hex2dec('66');
    end
    
    properties(Constant, Hidden)
        MotorNumberSet = matlab.system.StringSet({'1', '2', '3', '4'});
         % --------------Commands for DC motor at D11---------------------
        SET_PWM_DUTY_CYCLE_SERVO      = 3   ;
        SET_PWM_FREQUENCY_SERVO       = 4   ; 
        
        % -------------- Angle Mapping ----------------- %
        in_min = int32(0);
        in_max = int32(180);
        out_min = int32(7);
        out_max = int32(28);
    end

    properties (Dependent,Hidden)
        MotorNumberIdx
    end
        
    methods
        % Constructor
        function obj = mkrmotorcarrier_ServoMotor(varargin)
            coder.allowpcode('plain');
            setProperties(obj,nargin,varargin{:});
        end 
        
        function idx = get.MotorNumberIdx(obj)
            switch obj.MotorNumber
                case '1'
                    idx = 0;
                case '2'
                    idx = 1;
                case '3'
                    idx = 2;
                case '4'
                    idx = 3;
            end
        end
    end

    methods (Access = protected)
       % Set data in D11
        function D11_setData(obj,command, target, data) 
            % Convert int32 data to uint8 in Little Endian byte order
            data_uint8 = typecast(int32(data), 'uint8');
            
            final_data_to_write = [uint8(command), uint8(target), data_uint8];
            if coder.target('Rtw')% done only for code gen
                coder.cinclude('MW_arduinoI2C.h');
                coder.ceval('MW_i2cWriteLoop', uint8(obj.SlaveAddress_), 0, 0, ...
                         coder.rref(final_data_to_write), 6);
            elseif ( coder.target('RtwForRapid') || coder.target('RtwForSfun') )
            end
        end
        
        function ServoMotor_setDuty(obj, value)
            % wmath map - Equivalent function
            value = int32((value - obj.in_min) * (obj.out_max - obj.out_min) / (obj.in_max - obj.in_min) + obj.out_min);
            %value = (value - int32(0)) * int32(28 - 7) / int32(180 - 0) + 7 ; 
            D11_setData(obj, uint8(obj.SET_PWM_DUTY_CYCLE_SERVO),...
                uint8(obj.MotorNumberIdx), int32(value));
        end
        
        function ServoMotor_setFrequency(obj)
            D11_setData(obj, uint8(obj.SET_PWM_FREQUENCY_SERVO),...
                uint8(obj.MotorNumberIdx), int32(obj.MotorCarrierPWMFreq));
        end
    end
    
    methods (Access=protected)
        function setupImpl(obj)            
            if coder.target('Rtw')
               ServoMotor_setFrequency(obj);
            else
                % Do nothing
            end
        end

        function releaseImpl(obj)
            % Release resources, such as file handles
            if coder.target('Rtw')
               ServoMotor_setDuty(obj, int32(0));
            else
                % Do nothing
            end
        end
        
        function stepImpl(obj, In1)
            if coder.target('Rtw')
               ServoMotor_setDuty(obj, int32(In1));
            else
                % Do nothing
            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 = ['Servo 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'',''servowrite.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 = getPropertyGroupsImpl(~)
            % Define section for properties in System block dialog box.
            MotorNumberProp = matlab.system.display.internal.Property(...
                'MotorNumber', 'Description', 'Motor port');
            MotorCarrierPWMFreqProp = matlab.system.display.internal.Property(...
                'MotorCarrierPWMFreq', 'Description', 'PWM frequency', 'IsGraphical', false);
            SlaveAddressProp = matlab.system.display.internal.Property(...
                'SlaveAddress_', 'IsGraphical', false);                       
            
            Group = matlab.system.display.Section(...
                'Title', 'Parameters', 'PropertyList', ...
                {MotorNumberProp, MotorCarrierPWMFreqProp, ...
                SlaveAddressProp, ...
                });
            
            groups = Group;
        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', 'Servo Motor', ...
                'Text', sprintf('Set the shaft position of the standard servo motor.\n\nThe block accepts an input between 0 and 180. The value of 0 positions the shaft at 0 degrees, while the value of 180 positions the shaft at 180 degrees.'), ...
                'ShowSourceLink', false);
        end
    end
    
    methods (Static)
        function name = getDescriptiveName()
            name = 'MKR Motor Carrier Servo 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