| 1 | function light = GetLight(port, varargin) |
|---|
| 2 | % Reads the current value of the NXT light sensor |
|---|
| 3 | % |
|---|
| 4 | % Syntax |
|---|
| 5 | % |light = GetLight(port)| |
|---|
| 6 | % |
|---|
| 7 | % |light = GetLight(port, handle)| |
|---|
| 8 | % |
|---|
| 9 | % Description |
|---|
| 10 | % |light = GetLight(port)| returns the current light value |light| of the NXT light |
|---|
| 11 | % sensor. The measurement value |light| represents the normalized (default) sound value (0..1023 / |
|---|
| 12 | % 10 Bit). The normalized value mode is set per default by the function |OpenLight|. |
|---|
| 13 | % The given |port| number specifies the connection port. The value |port| can be |
|---|
| 14 | % addressed by the symbolic constants |SENSOR_1| , |SENSOR_2|, |SENSOR_3| and |SENSOR_4| analog to |
|---|
| 15 | % the labeling on the NXT Brick. |
|---|
| 16 | % |
|---|
| 17 | % The last optional argument can be a valid NXT handle. If none is |
|---|
| 18 | % specified, the default handle will be used (call |COM_SetDefaultNXT| to |
|---|
| 19 | % set one). |
|---|
| 20 | % |
|---|
| 21 | % For more complex settings the function |NXT_GetInputValues| can be used. |
|---|
| 22 | % |
|---|
| 23 | % Examples |
|---|
| 24 | %+ OpenLight(SENSOR_1, 'ACTIVE'); |
|---|
| 25 | %+ light = GetLight(SENSOR_1); |
|---|
| 26 | %+ CloseSensor(SENSOR_1); |
|---|
| 27 | % |
|---|
| 28 | % See also: NXT_GetInputValues, OpenLight, CloseSensor, SENSOR_1, SENSOR_2, SENSOR_3, SENSOR_4 |
|---|
| 29 | % |
|---|
| 30 | % Signature |
|---|
| 31 | % Author: Linus Atorf, Alexander Behrens (see AUTHORS) |
|---|
| 32 | % Date: 2008/12/05 |
|---|
| 33 | % Copyright: 2007-2008, RWTH Aachen University |
|---|
| 34 | % |
|---|
| 35 | ; |
|---|
| 36 | % |
|---|
| 37 | % *********************************************************************************************** |
|---|
| 38 | % * This file is part of the RWTH - Mindstorms NXT Toolbox. * |
|---|
| 39 | % * * |
|---|
| 40 | % * The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify * |
|---|
| 41 | % * it under the terms of the GNU General Public License as published by the Free Software * |
|---|
| 42 | % * Foundation, either version 3 of the License, or (at your option) any later version. * |
|---|
| 43 | % * * |
|---|
| 44 | % * The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful, * |
|---|
| 45 | % * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * |
|---|
| 46 | % * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
|---|
| 47 | % * * |
|---|
| 48 | % * You should have received a copy of the GNU General Public License along with the * |
|---|
| 49 | % * RWTH - Mindstorms NXT Toolbox. If not, see <http: |
|---|
| 50 | % *********************************************************************************************** |
|---|
| 51 | |
|---|
| 52 | %% check if handle is given; if not use default one |
|---|
| 53 | if nargin > 1 |
|---|
| 54 | handle = varargin{1}; |
|---|
| 55 | else |
|---|
| 56 | handle = COM_GetDefaultNXT; |
|---|
| 57 | end%if |
|---|
| 58 | |
|---|
| 59 | %% Call NXT_GetInputValues function |
|---|
| 60 | in = NXT_GetInputValues(port, handle); |
|---|
| 61 | |
|---|
| 62 | %% Check valid-flag, re-request data if necessary |
|---|
| 63 | if ~in.Valid |
|---|
| 64 | % init timeout-counter |
|---|
| 65 | startTime = clock(); |
|---|
| 66 | timeOut = 0.1; % in seconds |
|---|
| 67 | % loop until valid |
|---|
| 68 | while (~in.Valid) && (etime(clock, startTime) < timeOut) |
|---|
| 69 | in = NXT_GetInputValues(port, handle); |
|---|
| 70 | end%while |
|---|
| 71 | %TODO please note that we DO NOT warn or do anything else if this |
|---|
| 72 | %procedure above times out -- that seems ok, since we didn't do |
|---|
| 73 | %that before (old ver. up to 2.02) either... |
|---|
| 74 | % in that case, the returned value will probably be invalid... |
|---|
| 75 | % dangerous. |
|---|
| 76 | % solutions: either WARN, or return NaN... |
|---|
| 77 | end%if |
|---|
| 78 | |
|---|
| 79 | %% Return normalized sound value (0...1023 / 10 Bit) |
|---|
| 80 | light = double(in.NormalizedADVal); |
|---|
| 81 | |
|---|
| 82 | end |
|---|
| 83 | |
|---|