root/branches/atorf/RWTHMindstormsNXT/GetUltrasonic.m @ 296

Revision 296, 3.0 KB (checked in by behrens, 5 years ago)

replaced f_sensorport to port

Line 
1function [DistanceCM] = GetUltrasonic(port)
2% Reads the current value of the NXT ultrasonic sensor
3%
4% Syntax
5%   |distance = GetUltrasonic(port)|
6%
7% Description
8%   |distance = GetUltraSonic(port)| returns the current measurement value |distance| of the NXT
9%   ultrasonic sensor. |distance| represents the measured distance in cm.
10%   The given |port| number specifies the connection port. The value |port| can be
11%   addressed by the symbolic constants |SENSOR_1| , |SENSOR_2|, |SENSOR_3| and |SENSOR_4| analog to
12%   the labeling on the NXT Brick.
13%
14%   For more complex settings the functions |NXT_LSRead| and |NXT_LSWrite| can be used.
15%
16% Example
17%+   OpenUltrasonic(SENSOR_4);
18%+   distance = GetUltrasonic(SENSOR_4);
19%+   CloseSensor(SENSOR_4);
20%
21% See also: OpenUltrasonic, USMakeSnapshot, USGetSnapshotResults, CloseSensor, NXT_LSRead, NXT_LSWrite
22%
23% Signature
24%   Author: Linus Atorf, Alexander Behrens (see AUTHORS)
25%   Date: 2008/01/15
26%   Copyright: 2007-2008, RWTH Aachen University
27%
28;
29%
30% ***********************************************************************************************
31% *  This file is part of the RWTH - Mindstorms NXT Toolbox.                                    *
32% *                                                                                             *
33% *  The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify  *
34% *  it under the terms of the GNU General Public License as published by the Free Software     *
35% *  Foundation, either version 3 of the License, or (at your option) any later version.        *
36% *                                                                                             *
37% *  The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful,       *
38% *  but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS  *
39% *  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
40% *                                                                                             *
41% *  You should have received a copy of the GNU General Public License along with the           *
42% *  RWTH - Mindstorms NXT Toolbox. If not, see <http://www.gnu.org/licenses/>.                 *
43% ***********************************************************************************************
44
45
46    % also accept strings as input
47    if ischar(port)
48        port = str2double(port);
49    end%if
50
51
52    % The old version looked like this:
53    %RequestLen = 1;
54    %I2Cdata = hex2dec(['02'; '42']); % Read Measurement Byte 0 (see LEGO Mindstorms NXT
55    %                                 % Ultrasonic Sensor - I2C Communication Protocol)
56
57    % retrieve 1 byte from device 0x02, register 0x42
58    data = COM_ReadI2C(port, 1, uint8(2), uint8(66));
59   
60    if isempty(data)
61        DistanceCM = -1;
62    else
63        % this double() is so important!!!
64        DistanceCM = double(data(1));
65    end%if
66
67
68   
69end%function
Note: See TracBrowser for help on using the browser.