source: tags/version-4.04/RWTHMindstormsNXT/GetNXT2Color.m

Last change on this file was 926, checked in by atorf, 9 years ago

/!\ Creating TAG for release version 4.04

File size: 5.8 KB
Line 
1function out = GetNXT2Color( f_sensorport, varargin )
2%  Reads the current value of the color sensor from the NXT 2.0 set
3%
4% Syntax
5%   out = GetNXT2Color(port)
6%
7%   out = GetNXT2Color(port, handle)
8%
9% Description
10%   This functions retrieves the current value of the LEGO NXT 2.0 Color sensor specified by the sensor
11%   port. The value port can be addressed by the symbolic constants
12%   SENSOR_1 , SENSOR_2, SENSOR_3 and SENSOR_4 analog to the labeling on the NXT Brick.
13%   This function is intended for the Color sensor that comes with the NXT 2.0
14%   set. It has the label "RGB" written on the front, 3 LED openings (1 black
15%   empty spot, the light sensor and a clear lens with tiny red, green, blue LEDs behind it).
16%   It is not to be confused with the HiTechnic Color sensors (V1 and V2),
17%   for those please see the functions OpenColor and GetColor.
18%
19%   The sensor has to be opened with OpenNXT2Color() before this function
20%   can be used.
21%   
22%   Depending on the mode the color sensor was opened in,
23%   the return value of this function can have one of the following two
24%   formats
25% * *In full color mode* (sensor was opened with mode = 'FULL'), the return
26%   value will consist of one of the following strings:
27%   'BLACK', 'BLUE', 'GREEN', 'YELLOW', 'RED', 'WHITE'. If an
28%   error occured, the return value may be 'UNKNOWN' (unlikely though).
29% * *In all other modes*, i.e. 'RED', 'GREEN', 'BLUE', 'NONE', the
30%   returned value will be an integer between 0 and 1023, indicating the
31%   amount of reflected / detected light. This is very similar to the
32%   behaviour of GetLight.
33%
34%   The last optional argument can be a valid NXT handle. If none is
35%   specified, the default handle will be used (call COM_SetDefaultNXT to
36%   set one).
37%
38%
39% Limitations
40%   The sensor is influenced by ambient light. It reacts differently on
41%   daylight than on artificial light. The modes 'RED' and 'NONE' are
42%   similar to the Light sensor's modes 'ACTIVE' and 'INACTIVE', but the
43%   sensors are not perfectly compatible.
44%
45%
46% Examples
47%  port = SENSOR_1;
48%  OpenNXT2Color(port, 'FULL');
49%  color = GetNXT2Color(port);
50%  if strcmp(color, 'BLUE')
51%      disp('Blue like the ocean');
52%  else
53%      disp(['The detected color is ' color]);
54%  end%if
55%  CloseSensor(port);
56%
57%  port = SENSOR_2;
58%  OpenNXT2Color(port, 'NONE');
59%  colorVal = GetNXT2Color(port);
60%  if colorVal > 700
61%      disp('It''s quite bright!')
62%  end%if
63%  CloseSensor(port);
64%
65% See also: OpentNXT2Color, CloseSensor, OpenColor, GetColor, OpenLight, GetLight, COM_ReadI2C
66%
67% Signature
68%   Author: Nick Watts, Linus Atorf (see AUTHORS)
69%   Date: 2010/09/21
70%   Copyright: 2007-2010, RWTH Aachen University
71%
72%
73% ***********************************************************************************************
74% *  This file is part of the RWTH - Mindstorms NXT Toolbox.                                    *
75% *                                                                                             *
76% *  The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify  *
77% *  it under the terms of the GNU General Public License as published by the Free Software     *
78% *  Foundation, either version 3 of the License, or (at your option) any later version.        *
79% *                                                                                             *
80% *  The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful,       *
81% *  but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS  *
82% *  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
83% *                                                                                             *
84% *  You should have received a copy of the GNU General Public License along with the           *
85% *  RWTH - Mindstorms NXT Toolbox. If not, see <http://www.gnu.org/licenses/>.                 *
86% ***********************************************************************************************
87
88
89%% check parameters
90
91    if nargin > 1
92        handle = varargin{1};
93    else
94        handle = COM_GetDefaultNXT;
95    end%if
96 
97%% call NXT_GetInputValues
98 
99    in = NXT_GetInputValues( f_sensorport, handle );
100   
101%% check for valid data, re-request if necessary
102    % init timeout-counter
103    startTime = clock();
104    timeOut = 0.5; % in seconds
105    % loop until valid
106    while (~in.Valid) && (etime(clock, startTime) < timeOut)
107        in = NXT_GetInputValues(f_sensorport, handle);
108    end%while
109   
110    % nice check, we could've actually done that everwhere else...
111    if strcmp(in.TypeName,'NO_SENSOR')
112        error('MATLAB:RWTHMindstormsNXT:Sensor:noSensorOpened', 'No sensor configured / opened for this port')
113    end%if
114   
115   
116    % check if everything is ok now...
117    if ~in.Valid
118        warning('MATLAB:RWTHMindstormsNXT:Sensor:invalidData', ...
119               ['Returned sensor data marked invalid! ' ...
120                'Make sure the sensor is properly connected and configured to a supported mode. ' ...
121                'Disable this warning by calling  warning(''off'', ''MATLAB:RWTHMindstormsNXT:Sensor:invalidData'')']);
122    end%if
123   
124
125   
126%% format output
127    % for a list of values see here:
128    % http://bricxcc.sourceforge.net/nbc/nxcdoc/nxcapi/group___input_color_value_constants.html
129    if in.TypeByte == 13 %strcmp(in.TypeName, 'COLORFULL')
130        colors = {'BLACK';'BLUE';'GREEN';'YELLOW';'RED';'WHITE'};
131        try
132            out = colors{ in.ScaledVal };
133        catch
134            out = 'UNKNOWN';
135        end%try
136    else
137        % after various tests it was decided to do the same as for the
138        % light sensor:
139        out = double(in.NormalizedADVal);
140    end%if
141
142end%function
143
Note: See TracBrowser for help on using the repository browser.