超声波matlab,MATLAB玩转Arduino——控制HC-SR04超声波传感器

classdef HCSR04 < arduinoio.LibraryBase & matlab.mixin.CustomDisplay

% HCSR04 Create an HCSR04 device object.

%

% sensor = addon(a,'JRodrigoTech/HCSR04',triggerPin,echoPin) creates a HCSR04 device object.

% Copyright 2016-2017 The MathWorks, Inc.

properties(Access = private, Constant = true)

CREATE_HCSR04           = hex2dec('01')

DELETE_HCSR04           = hex2dec('02')

HCSR04_READ_DISTANCE    = hex2dec('03')

HCSR04_READ_TIME        = hex2dec('04')

end

properties(Access = protected, Constant = true)

LibraryName = 'JRodrigoTech/HCSR04'

DependentLibraries = {}

ArduinoLibraryHeaderFiles = {'Ultrasonic/Ultrasonic.h'}

CppHeaderFile = fullfile(arduinoio.FilePath(mfilename('fullpath')), 'src', 'HCSR04.h')

CppClassName = 'HCSR04'

end

properties(Access = private)

ResourceOwner = 'JRodrigoTech/HCSR04';

end

methods(Hidden, Access = public)

function obj = HCSR04(parentObj, triggerPin, echoPin)

%   Connect to a Ultrasonic sensor HC-SR04 sensor

%

%   Syntax:

%   sensor = addon(a,'JRodrigoTech/HCSR04',triggerPin,echoPin)

%

%   Description:

%   sensor = addon(a,'JRodrigoTech/HCSR04',triggerPin,echoPin)        Connects to a HC-SR04 sensor

%

%   Example:

%       a = arduino('COM3','Uno','libraries','JRodrigoTech/HCSR04');

%       sensor = addon(a,'JRodrigoTech/HCSR04','D12','D13')

%

%   Input Arguments:

%   parentObj  - Arduino

%   triggerPin - Arduino pin to send the signal (character vector)

%   echoPin    - Arduino pin to receive the echoed back signal (character vector)

%

%   Output Arguments:

%   obj - HCSR04 object

narginchk(3, 3);

obj.Parent = parentObj;

try

obj.Pins = {triggerPin, echoPin};

% Configure pin modes

configurePinResource(obj.Parent, triggerPin, obj.ResourceOwner, 'DigitalOutput');

configurePinResource(obj.Parent, echoPin, obj.ResourceOwner, 'DigitalInput');

createUltrasonicSensor(obj);

catch e

throwAsCaller(e);

end

end

end

methods(Access = protected)

function delete(obj)

try

parentObj = obj.Parent;

for iLoop = obj.Pins

configurePinResource(parentObj,iLoop{:},obj.ResourceOwner,'Unset');

end

deleteUltrasonicSensor(obj);

catch

% Do not throw errors on destroy.

% This may result from an incomplete construction.

end

end

end

methods(Access = private)

function createUltrasonicSensor(obj)

cmdID = obj.CREATE_HCSR04;

data = getTerminalsFromPins(obj.Parent, obj.Pins);

sendCommand(obj, obj.LibraryName, cmdID, data);

end

function deleteUltrasonicSensor(obj)

cmdID = obj.DELETE_HCSR04;

sendCommand(obj, obj.LibraryName, cmdID, []);

end

end

methods(Access = public)

function val = readTravelTime(obj)

%   Get the time for echo pin to receive echoed back signal

%

%   Syntax:

%   readTravelTime(sensor)

%

%   Description:

%   Get the time for echo pin to receive echoed back signal after a signal is sent from send pin

%

%   Example:

%       a = arduino('COM3','Uno','libraries','JRodrigoTech/HCSR04');

%       sensor = addon(a,'JRodrigoTech/HCSR04','D12','D13');

%       value = readTravelTime(sensor)

%

%   Input Arguments:

%   obj - HCSR04 device

%

%   Output Arguments:

%   val - Sensed duration that echo pin is high (s)

cmdID = obj.HCSR04_READ_TIME;

try

val = sendCommand(obj, obj.LibraryName, cmdID, []);

val = double(typecast(uint8(val), 'int32')); % microseconds

val = val/1000000; % seconds

catch e

throwAsCaller(e);

end

end

function val = readDistance(obj)

%   Get the sensed distance to the nearest object

%

%   Syntax:

%   readDistance(sensor)

%

%   Description:

%   Get the sensed distance to the nearest object, assuming speed of sound is 340m/s

%

%   Example:

%       a = arduino('COM3','Uno','libraries','JRodrigoTech/HCSR04');

%       sensor = addon(a,'JRodrigoTech/HCSR04','D12','D13');

%       value = readDistance(sensor)

%

%   Input Arguments:

%   obj - HCSR04 device

%

%   Output Arguments:

%   val - Sensed distance (m)

cmdID = obj.HCSR04_READ_DISTANCE;

try

val = sendCommand(obj, obj.LibraryName, cmdID, []);

val = typecast(uint8(val), 'int32'); % in cm

val = double(val)/100; % in meter

catch e

throwAsCaller(e);

end

end

end

methods (Access = protected)

function displayScalarObject(obj)

header = getHeader(obj);

disp(header);

% Display main options

fprintf('               Pins: ''%s''(Trigger), ''%s''(Echo)\n', obj.Pins{1}, obj.Pins{2});

fprintf('\n');

% Allow for the possibility of a footer.

footer = getFooter(obj);

if ~isempty(footer)

disp(footer);

end

end

end

end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值