www.gusucode.com > Arduino_Engineering_Kit_Hardware_Support_18b 工具箱matlab源码程序 > Arduino_Engineering_Kit_Hardware_Support_18b/matlab/MATLABAddon/+arduinoioaddons/+arduino/MKRServo.m

    classdef MKRServo < arduinoio.MotorBase & matlab.mixin.CustomDisplay
    %MKRServo Create a Servo object.
    
    % Copyright 2018 The MathWorks, Inc.
    
    properties(Hidden)
        PWMFrequency = 50
        Angle
    end
    
    
    
    properties(Access = private)
        ResourceOwner = 'MKRMotorCarrier\Servo';
    end
    
    properties(Access = private, Constant = true)
        % MATLAB defined command IDs
        CREATE_SERVO_MOTOR     = hex2dec('0F')
        CLEAR_SERVO_MOTOR      = hex2dec('10')
        READ_SERVO_POSITION    = hex2dec('11')
        WRITE_SERVO_POSITION   = hex2dec('12')
        
    end
   
    properties(Access = private, Constant = true)
        MaxServoMotors = 4
        ResourceMode = 'MKRMotorCarrier\Servo';
    end
    
    %% Constructor
    methods(Hidden, Access = public)
        function obj = MKRServo(parentObj, motorNumber, varargin)
            obj.Pins = [];
            obj.Parent = parentObj;
            arduinoObj = parentObj.Parent;
            
            try
                if (nargin < 2)
                    obj.localizedError('MATLAB:minrhs');
                end
            catch e
                throwAsCaller(e);
            end
            
            motorNumber = arduinoio.internal.validateIntParameterRanged(...
                [obj.ResourceOwner 'MotorNumber'], ...
                motorNumber, ...
                1, ...
                obj.MaxServoMotors);
            
            try
                servos = getSharedResourceProperty(arduinoObj, obj.ResourceOwner, 'servos');
            catch 
                locSC = 1; %#ok<NASGU>
                servos = [parentObj.I2CAddress zeros(1, obj.MaxServoMotors)];
            end
            shieldSCAddresses = servos(:, 1);
            [~, locSC] = ismember(parentObj.I2CAddress, shieldSCAddresses);
            if locSC == 0
                servos = [servos; parentObj.I2CAddress zeros(1, obj.MaxServoMotors)];
                locSC = size(servos, 1);
            end
            
            % Check for resource conflict with Servo Motors
            if servos(locSC, motorNumber+1)
                error('Arduino:MKRMotorCarrier:conflictServoMKR', 'ArduinoMKRMotorCarrier\\\\Servo ''%d'' is already in use', motorNumber);
            end
       
            % No clonflicts
            servos(locSC, motorNumber+1) = 1;
            setSharedResourceProperty(arduinoObj, obj.ResourceOwner, 'servos', servos);
            obj.MotorNumber = motorNumber;
            
            try
                p = inputParser;
                addParameter(p, 'PWMFrequency', 50);
                parse(p, varargin{:});
            catch
                obj.localizedError('MATLAB:arduinoio:general:invalidNVPropertyName',...
                    obj.ResourceOwner, ...
                    arduinoio.internal.renderCellArrayOfCharVectorsToCharVector(p.Parameters, ', '));
            end
            
            obj.PWMFrequency = p.Results.PWMFrequency;            
            createServoMotor(obj);
        end
    end
    
    %%
    methods
        function writePosition(obj,value)
            try
                if (nargin < 2)
                    obj.localizedError('MATLAB:minrhs');
                end            
                arduinoio.internal.validateDoubleParameterRanged('position', value, 0, 1);
                obj.Angle = uint8(180*value);
                writeServoPosition(obj);
            catch e
                throwAsCaller(e);
            end
        end
   end
    
	%% Destructor
    methods (Access=protected)
        function delete(obj)
            originalState = warning('off','MATLAB:class:DestructorError');
            try
                parentObj = obj.Parent;
                arduinoObj = parentObj.Parent;
                
                % Clear the Servo Motor
                if ~isempty(obj.MotorNumber)
                    clearServoMotor(obj);
                end
                servos = getSharedResourceProperty(arduinoObj, obj.ResourceOwner, 'servos');
                shieldSCAddresses = servos(:, 1);
                [~, locSC] = ismember(parentObj.I2CAddress, shieldSCAddresses);
                servos(locSC, obj.MotorNumber+1) = 0;
                setSharedResourceProperty(arduinoObj, obj.ResourceOwner, 'servos', servos);                

            catch
                % Do not throw errors on destroy.
                % This may result from an incomplete construction.
            end
            warning(originalState.state, 'MATLAB:class:DestructorError');
        end
    end
    
    %% Private methods
    methods (Access = private)
        function createServoMotor(obj)
             commandID = obj.CREATE_SERVO_MOTOR;
             params = [obj.PWMFrequency];
             sendCommand(obj, commandID, params);
        end
        
        function writeServoPosition(obj)
            commandID = obj.WRITE_SERVO_POSITION;
            params = [obj.Angle];
            sendCommand(obj, commandID, params);
        end
        
        function angle = readServoPosition(obj)
            commandID = obj.READ_SERVO_POSITION;
             params = [];
             angle = sendCommand(obj, commandID, params);
        end
        
        function clearServoMotor(obj)
            commandID = obj.CLEAR_SERVO_MOTOR;
            params = [];
            sendCommand(obj, commandID, params);
        end
    end
    
    
    %% Protected methods
    methods(Access = protected)    
        function output = sendCommand(obj, commandID, params)
            params = [obj.MotorNumber - 1; params]; 
            output = sendCarrierCommand(obj.Parent, commandID, params);
        end
    end
        
    %% Protected methods
    methods (Access = protected)
        function displayScalarObject(obj)
            header = getHeader(obj);
            disp(header);
            
            % Display main options
            fprintf('    Servo: %d\n', obj.MotorNumber);
            fprintf('\n');
                  
            % Allow for the possibility of a footer.
            footer = getFooter(obj);
            if ~isempty(footer)
                disp(footer);
            end
        end
    end
end