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

[复制链接]
 楼主| gaoyang9992006 发表于 2017-12-22 10:32 | 显示全部楼层 |阅读模式
控制HC-SR04超声波传感器
本例展示了如何使用Arduino®硬件的MATLAB®支持包来控制HC-SR04超声波测距模块,该模块能够测量2cm到400cm的距离。
硬件要求
  • Arduino板
  • HC-SR04超声波传感器

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

 楼主| gaoyang9992006 发表于 2017-12-22 10:35 | 显示全部楼层
创建HCSR04对象
使用JRodrigoTech / HCSR04插件创建arduino对象。
a = arduino('COM38', 'Uno', 'Libraries', 'JRodrigoTech/HCSR04')
  1. <font color="#474747" face="Menlo, Monaco, Consolas, Courier New, monospace"><span style="font-size: 12px;">a =

  2.   arduino with properties:

  3.                     Port: 'COM38'
  4.                    Board: 'Uno'
  5.            AvailablePins: {'D2-D13', 'A0-A5'}
  6.                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 | 显示全部楼层
  1. classdef HCSR04 < arduinoio.LibraryBase & matlab.mixin.CustomDisplay
  2.     % HCSR04 Create an HCSR04 device object.
  3.     %   
  4.     % sensor = addon(a,'JRodrigoTech/HCSR04',triggerPin,echoPin) creates a HCSR04 device object.
  5.    
  6.     % Copyright 2016-2017 The MathWorks, Inc.
  7.    
  8.     properties(Access = private, Constant = true)
  9.         CREATE_HCSR04           = hex2dec('01')
  10.         DELETE_HCSR04           = hex2dec('02')
  11.         HCSR04_READ_DISTANCE    = hex2dec('03')
  12.         HCSR04_READ_TIME        = hex2dec('04')
  13.     end

  14.     properties(Access = protected, Constant = true)
  15.         LibraryName = 'JRodrigoTech/HCSR04'
  16.         DependentLibraries = {}
  17.         ArduinoLibraryHeaderFiles = {'Ultrasonic/Ultrasonic.h'}
  18.         CppHeaderFile = fullfile(arduinoio.FilePath(mfilename('fullpath')), 'src', 'HCSR04.h')
  19.         CppClassName = 'HCSR04'
  20.     end
  21.    
  22.     properties(Access = private)
  23.         ResourceOwner = 'JRodrigoTech/HCSR04';
  24.     end
  25.    
  26.     methods(Hidden, Access = public)
  27.         function obj = HCSR04(parentObj, triggerPin, echoPin)
  28.             %   Connect to a Ultrasonic sensor HC-SR04 sensor
  29.             %
  30.             %   Syntax:
  31.             %   sensor = addon(a,'JRodrigoTech/HCSR04',triggerPin,echoPin)
  32.             %
  33.             %   Description:
  34.             %   sensor = addon(a,'JRodrigoTech/HCSR04',triggerPin,echoPin)        Connects to a HC-SR04 sensor
  35.             %
  36.             %   Example:
  37.             %       a = arduino('COM3','Uno','libraries','JRodrigoTech/HCSR04');
  38.             %       sensor = addon(a,'JRodrigoTech/HCSR04','D12','D13')
  39.             %
  40.             %   Input Arguments:
  41.             %   parentObj  - Arduino
  42.             %   triggerPin - Arduino pin to send the signal (character vector)
  43.             %   echoPin    - Arduino pin to receive the echoed back signal (character vector)
  44.             %
  45.             %   Output Arguments:
  46.             %   obj - HCSR04 object

  47.             narginchk(3, 3);
  48.             obj.Parent = parentObj;
  49.             
  50.             try
  51.                 obj.Pins = {triggerPin, echoPin};
  52.                 % Configure pin modes
  53.                 configurePinResource(obj.Parent, triggerPin, obj.ResourceOwner, 'DigitalOutput');
  54.                 configurePinResource(obj.Parent, echoPin, obj.ResourceOwner, 'DigitalInput');
  55.                
  56.                 createUltrasonicSensor(obj);
  57.             catch e
  58.                 throwAsCaller(e);
  59.             end
  60.         end
  61.     end
  62.    
  63.     methods(Access = protected)
  64.         function delete(obj)
  65.             try
  66.                 parentObj = obj.Parent;
  67.                 for iLoop = obj.Pins
  68.                     configurePinResource(parentObj,iLoop{:},obj.ResourceOwner,'Unset');
  69.                 end
  70.                
  71.                 deleteUltrasonicSensor(obj);
  72.             catch
  73.                 % Do not throw errors on destroy.
  74.                 % This may result from an incomplete construction.
  75.             end
  76.         end  
  77.     end
  78.         
  79.     methods(Access = private)
  80.         function createUltrasonicSensor(obj)
  81.             cmdID = obj.CREATE_HCSR04;
  82.             data = getTerminalsFromPins(obj.Parent, obj.Pins);
  83.             sendCommand(obj, obj.LibraryName, cmdID, data);
  84.         end
  85.         
  86.         function deleteUltrasonicSensor(obj)
  87.             cmdID = obj.DELETE_HCSR04;
  88.             sendCommand(obj, obj.LibraryName, cmdID, []);
  89.         end
  90.     end
  91.    
  92.     methods(Access = public)
  93.         function val = readTravelTime(obj)
  94.             %   Get the time for echo pin to receive echoed back signal
  95.             %
  96.             %   Syntax:
  97.             %   readTravelTime(sensor)
  98.             %
  99.             %   Description:
  100.             %   Get the time for echo pin to receive echoed back signal after a signal is sent from send pin
  101.             %
  102.             %   Example:
  103.             %       a = arduino('COM3','Uno','libraries','JRodrigoTech/HCSR04');
  104.             %       sensor = addon(a,'JRodrigoTech/HCSR04','D12','D13');
  105.             %       value = readTravelTime(sensor)
  106.             %
  107.             %   Input Arguments:
  108.             %   obj - HCSR04 device
  109.             %
  110.             %   Output Arguments:
  111.             %   val - Sensed duration that echo pin is high (s)
  112.         
  113.             cmdID = obj.HCSR04_READ_TIME;
  114.             
  115.             try
  116.                 val = sendCommand(obj, obj.LibraryName, cmdID, []);
  117.                 val = double(typecast(uint8(val), 'int32')); % microseconds
  118.                 val = val/1000000; % seconds
  119.             catch e
  120.                 throwAsCaller(e);
  121.             end
  122.         end
  123.         
  124.         function val = readDistance(obj)
  125.             %   Get the sensed distance to the nearest object
  126.             %
  127.             %   Syntax:
  128.             %   readDistance(sensor)
  129.             %
  130.             %   Description:
  131.             %   Get the sensed distance to the nearest object, assuming speed of sound is 340m/s
  132.             %
  133.             %   Example:
  134.             %       a = arduino('COM3','Uno','libraries','JRodrigoTech/HCSR04');
  135.             %       sensor = addon(a,'JRodrigoTech/HCSR04','D12','D13');
  136.             %       value = readDistance(sensor)
  137.             %
  138.             %   Input Arguments:
  139.             %   obj - HCSR04 device
  140.             %
  141.             %   Output Arguments:
  142.             %   val - Sensed distance (m)
  143.             
  144.             cmdID = obj.HCSR04_READ_DISTANCE;
  145.             
  146.             try
  147.                 val = sendCommand(obj, obj.LibraryName, cmdID, []);
  148.                 val = typecast(uint8(val), 'int32'); % in cm
  149.                 val = double(val)/100; % in meter
  150.             catch e
  151.                 throwAsCaller(e);
  152.             end
  153.         end
  154.     end
  155.    
  156.     methods (Access = protected)
  157.         function displayScalarObject(obj)
  158.             header = getHeader(obj);
  159.             disp(header);
  160.                         
  161.             % Display main options
  162.             fprintf('               Pins: ''%s''(Trigger), ''%s''(Echo)\n', obj.Pins{1}, obj.Pins{2});
  163.             fprintf('\n');
  164.                   
  165.             % Allow for the possibility of a footer.
  166.             footer = getFooter(obj);
  167.             if ~isempty(footer)
  168.                 disp(footer);
  169.             end
  170.         end
  171.     end
  172. end
lgl63167048 发表于 2017-12-27 09:05 | 显示全部楼层
下来看看
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:如果你觉得我的分享或者答复还可以,请给我点赞,谢谢。

2048

主题

16367

帖子

221

粉丝
快速回复 在线客服 返回列表 返回顶部