打印

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

[复制链接]
2261|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
控制HC-SR04超声波传感器
本例展示了如何使用Arduino®硬件的MATLAB®支持包来控制HC-SR04超声波测距模块,该模块能够测量2cm到400cm的距离。
硬件要求
  • Arduino板
  • HC-SR04超声波传感器

硬件设置
  • 将Trig引脚连接到Arduino板上的引脚D12。
  • 将Echo引脚连接到Arduino板上的引脚D13。
  • 将VCC引脚连接到Arduino板上的5V引脚。
  • 将GND引脚连接到Arduino板上的GND引脚。


相关帖子

沙发
gaoyang9992006|  楼主 | 2017-12-22 10:35 | 只看该作者
创建HCSR04对象
使用JRodrigoTech / HCSR04插件创建arduino对象。
a = arduino('COM38', 'Uno', 'Libraries', 'JRodrigoTech/HCSR04')
<font color="#474747" face="Menlo, Monaco, Consolas, Courier New, monospace"><span style="font-size: 12px;">a = 

  arduino with properties:

                    Port: 'COM38'
                   Board: 'Uno'
           AvailablePins: {'D2-D13', 'A0-A5'}
               Libraries: {'JRodrigoTech/HCSR04'}</span></font>


使用触发引脚D12和回波引脚D13创建超声波传感器对象。
sensor = addon(a, 'JRodrigoTech/HCSR04', 'D12', 'D13')
传感器=   HCSR04与属性:               引脚:“D12”(触发),“D13”(回波)
用仪表测量感应距离val = readDistance(sensor);sprintf('Current distance is %.4f meters\n', val)
ans =当前距离是0.0800米
测量与感测时间的距离
距离可以根据检测的时间使用这个公式计算,distance =(time x speedOfSound)/ 2。这里假设音速是340m / s。
t = readTravelTime(传感器);sprintf('当前距离是%.4f米\ n',340 * t / 2)
ans =当前距离为0.0918米
清理clear sensorclear a

使用特权

评论回复
板凳
gaoyang9992006|  楼主 | 2017-12-22 10:36 | 只看该作者
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

使用特权

评论回复
地板
lgl63167048| | 2017-12-27 09:05 | 只看该作者
下来看看

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

认证:西安公路研究院南京院
简介:主要工作从事监控网络与通信网络设计,以及从事基于嵌入式的通信与控制设备研发。擅长单片机嵌入式系统物联网设备开发,音频功放电路开发。

1895

主题

15627

帖子

197

粉丝