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