source: tags/version-4.04/RWTHMindstormsNXT/OpenNXT2Color.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.5 KB
Line 
1function OpenNXT2Color(port, mode, varargin)
2% Initializes the LEGO color sensor from the NXT 2.0 set, sets correct sensor mode
3%
4% Syntax
5%   OpenNXT2Color(port, mode)
6%
7%   OpenNXT2Color(port, mode, handle)
8%
9% Description
10%   This function initializes the input mode 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%   With GetNXT2Color(port) you can receive the detected brightness or color.
20%   
21%   mode specifies the operating mode of the sensor, the following values
22%   are allowed:
23% * 'FULL' - The red, green, and blue LEDs are constantly on (actually
24%   flashing at a high frequency), and the sensor will try to detect one of
25%   6 predefined colors.
26% * 'RED' - The red LED is constantly on, the sensor outputs reflected
27%   light / brightness. This is similar to the LEGO Light sensor mode
28%   'ACTIVE'. See OpenLight.
29% * 'GREEN' - The green LED is constantly on, the sensor outputs reflected
30%   light / brightness.
31% * 'BLUE' - The blue LED is constantly on, the sensor outputs reflected
32%   light / brightness.
33% * 'NONE' - All LEDs are constantly off, the sensor outputs ambient
34%   light / brightness. This is similar to the LEGO Light sensor mode
35%   'INACTIVE'. See OpenLight.
36%
37%   The last optional argument can be a valid NXT handle. If none is
38%   specified, the default handle will be used (call COM_SetDefaultNXT to
39%   set one).
40%
41%
42% Limitations
43%   The sensor is influenced by ambient light. It reacts differently on
44%   daylight than on artificial light. The modes 'RED' and 'NONE' are
45%   similar to the Light sensor's modes 'ACTIVE' and 'INACTIVE', but the
46%   sensors are not perfectly compatible.
47%
48%
49% Examples
50%  port = SENSOR_1;
51%  OpenNXT2Color(port, 'FULL');
52%  color = GetNXT2Color(port);
53%  if strcmp(color, 'BLUE')
54%      disp('Blue like the ocean');
55%  else
56%      disp(['The detected color is ' color]);
57%  end%if
58%  CloseSensor(port);
59%
60%  port = SENSOR_2;
61%  OpenNXT2Color(port, 'NONE');
62%  colorVal = GetNXT2Color(port);
63%  if colorVal > 700
64%      disp('It''s quite bright!')
65%  end%if
66%  CloseSensor(port);
67%
68% See also: GetNXT2Color, CloseSensor, OpenColor, GetColor, OpenLight, GetLight, COM_ReadI2C
69%
70% Signature
71%   Author: Nick Watts, Linus Atorf (see AUTHORS)
72%   Date: 2010/09/21
73%   Copyright: 2007-2010, RWTH Aachen University
74%
75%
76% ***********************************************************************************************
77% *  This file is part of the RWTH - Mindstorms NXT Toolbox.                                    *
78% *                                                                                             *
79% *  The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify  *
80% *  it under the terms of the GNU General Public License as published by the Free Software     *
81% *  Foundation, either version 3 of the License, or (at your option) any later version.        *
82% *                                                                                             *
83% *  The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful,       *
84% *  but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS  *
85% *  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
86% *                                                                                             *
87% *  You should have received a copy of the GNU General Public License along with the           *
88% *  RWTH - Mindstorms NXT Toolbox. If not, see <http://www.gnu.org/licenses/>.                 *
89% ***********************************************************************************************
90
91
92%% check parameters
93
94    % check if handle is given; if not use default one
95    if nargin > 2
96        handle = varargin{1};
97    else
98        handle = COM_GetDefaultNXT;
99    end%if
100
101    % also accept strings as input
102    if ischar(port)
103        port = str2double(port);
104    end%if
105
106%% check and set mode
107
108    sensormode = 'RAWMODE';
109    if strcmpi(mode, 'RED')
110        sensortype = 'COLORRED';
111    elseif strcmpi(mode, 'GREEN')
112        sensortype = 'COLORGREEN';
113    elseif strcmpi(mode, 'BLUE')
114        sensortype = 'COLORBLUE';
115    elseif strcmpi(mode, 'FULL')
116        sensortype = 'COLORFULL';
117    elseif strcmpi(mode, 'NONE')
118        sensortype = 'COLORNONE';
119    else
120        error('MATLAB:RWTHMindstormsNXT:Sensor:invalidMode', 'NXT 2.0 Color sensor mode has to be ''FULL'', ''RED'', ''GREEN'', ''BLUE'', or ''NONE''');
121    end%if
122 
123%% call NXT_SetInputMode function
124
125    NXT_SetInputMode( port, sensortype, sensormode, 'dontreply', handle );
126 
127    % it was measured the NXT 2.0 color sensor takes about 170ms until
128    % values turn valid. The LEDs light up a tiny moment earlier...
129    % in here we wait the biggest amount of time:
130    pause(0.130);
131    % the rest will be waited inside GetNXT2Color dynamically as needed
132   
133   
134end%function
135
Note: See TracBrowser for help on using the repository browser.