Changeset 905

Show
Ignore:
Timestamp:
09/22/10 00:25:03 (3 years ago)
Author:
atorf
Message:
  • Basic support for NXT 2.0 Color sensor, fixes #38
  • Unfortunately, now raw values can be retrieved right now in full RGB color mode, probably IO Maps are needed for this, see links in #38
  • Adapted see also link and help texts in related functions
Location:
trunk/mfiles
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • trunk/mfiles/GetColor.m

    r904 r905  
    7878%+   CloseSensor(SENSOR_4); 
    7979% 
    80 % See also: OpenColor, CalibrateColor, CloseSensor, COM_ReadI2C 
     80% See also: OpenColor, CalibrateColor, CloseSensor, OpenNXT2Color, GetNXT2Color, COM_ReadI2C 
    8181% 
    8282% Signature 
  • trunk/mfiles/GetNXT2Color.m

    r904 r905  
    11function out = GetNXT2Color( f_sensorport, varargin ) 
    2 %GETNXT2COLOR Summary of this function goes here 
    3 %   Detailed explanation goes here 
     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% *********************************************************************************************** 
     75% *  This file is part of the RWTH - Mindstorms NXT Toolbox.                                    * 
     76% *                                                                                             * 
     77% *  The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify  * 
     78% *  it under the terms of the GNU General Public License as published by the Free Software     * 
     79% *  Foundation, either version 3 of the License, or (at your option) any later version.        * 
     80% *                                                                                             * 
     81% *  The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful,       * 
     82% *  but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS  * 
     83% *  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.             * 
     84% *                                                                                             * 
     85% *  You should have received a copy of the GNU General Public License along with the           * 
     86% *  RWTH - Mindstorms NXT Toolbox. If not, see <http://www.gnu.org/licenses/>.                 * 
     87% *********************************************************************************************** 
     88 
    489 
    590%% check parameters 
    691 
    7   if nargin > 1 
    8     handle = varargin{1}; 
    9   else 
    10     handle = COM_GetDefaultNXT; 
    11   end%if 
     92    if nargin > 1 
     93        handle = varargin{1}; 
     94    else 
     95        handle = COM_GetDefaultNXT; 
     96    end%if 
    1297   
    1398%% call NXT_GetInputValues 
     
    15100    in = NXT_GetInputValues( f_sensorport, handle ); 
    16101     
    17 %% check for valid date, re-request if necessary 
    18    
     102%% check for valid data, re-request if necessary 
     103    % init timeout-counter 
    19104    startTime = clock(); 
    20     timeOut = 0.1; % in seconds 
     105    timeOut = 0.5; % in seconds 
    21106    % loop until valid 
    22107    while (~in.Valid) && (etime(clock, startTime) < timeOut) 
    23       in = NXT_GetInputValues(f_sensorport, handle); 
     108        in = NXT_GetInputValues(f_sensorport, handle); 
    24109    end%while 
    25110     
     111    % nice check, we could've actually done that everwhere else... 
    26112    if strcmp(in.TypeName,'NO_SENSOR') 
    27       error('No sensor set up for that port') 
     113        error('MATLAB:RWTHMindstormsNXT:Sensor:noSensorOpened', 'No sensor configured / opened for this port') 
    28114    end%if 
    29115     
     116     
     117    % check if everything is ok now... 
     118    if ~in.Valid 
     119        warning('MATLAB:RWTHMindstormsNXT:Sensor:invalidData', ... 
     120               ['Returned sensor data marked invalid! ' ... 
     121                'Make sure the sensor is properly connected and configured to a supported mode. ' ... 
     122                'Disable this warning by calling  warning(''off'', ''MATLAB:RWTHMindstormsNXT:Sensor:invalidData'')']); 
     123    end%if 
     124     
     125 
     126     
    30127%% format output 
     128    % for a list of values see here: 
     129    % http://bricxcc.sourceforge.net/nbc/nxcdoc/nxcapi/group___input_color_value_constants.html 
     130    if in.TypeByte == 13 %strcmp(in.TypeName, 'COLORFULL') 
     131        colors = {'BLACK';'BLUE';'GREEN';'YELLOW';'RED';'WHITE'}; 
     132        try 
     133            out = colors{ in.ScaledVal }; 
     134        catch 
     135            out = 'UNKNOWN'; 
     136        end%try 
     137    else 
     138        % after various tests it was decided to do the same as for the 
     139        % light sensor: 
     140        out = double(in.NormalizedADVal); 
     141    end%if 
    31142 
    32     if strcmp(in.TypeName, 'COLORFULL') && strcmp(in.ModeName,'RAWMODE') 
    33       colors = {'BLACK';'BLUE';'GREEN';'YELLOW';'RED';'WHITE'}; 
    34       try 
    35         out = colors{ in.ScaledVal }; 
    36       catch 
    37         out = 'UNKNOWN'; 
    38       end%try 
    39     else 
    40       if strcmp(in.ModeName,'RAWMODE') && ( strcmp(in.TypeName,'COLORRED') || strcmp(in.TypeName,'COLORGREEN') || strcmp(in.TypeName,'COLORBLUE') ) 
    41         out = floor( in.ScaledVal / 1024 * 100 ); 
    42       else 
    43         error('Invalid type/mode combination.  Try using NXT_GetInputValues.') 
    44       end%if 
    45     end 
     143end%function 
    46144 
    47 end 
    48  
  • trunk/mfiles/NXT_SetInputMode.m

    r896 r905  
    4343%                      |LOWSPEED_9V|     (NXT, active digital sensor, e.g. UltraSonic) 
    4444% 
     45%                      |HIGHSPEED|       (NXT, probably digital sensor on highspeed port 4) 
     46% 
    4547%                      |TEMPERATURE|     (old RCX sensor) 
    4648% 
     
    4951%                      |ANGLE|           (old RCX sensor) 
    5052% 
    51 %                      |CUSTOM|          (user defined) 
     53%                      |COLORFULL|       (NXT 2.0 Color sensor, full RGB mode) 
    5254% 
     55%                      |COLORRED|        (NXT 2.0 Color sensor, red LED only) 
     56% 
     57%                      |COLORGREEN|      (NXT 2.0 Color sensor, green LED only) 
     58% 
     59%                      |COLORBLUE|       (NXT 2.0 Color sensor, blue LED only) 
     60% 
     61%                      |COLORNONE|       (NXT 2.0 Color sensor, no LED) 
     62% 
     63% 
     64%FIXME: add type for color nxt 2.0 
    5365% 
    5466%   *|SensorModeDesc|:*  Valid modes are (all strings): 
     
    6072%                      |TRANSITIONCNTMODE|  (count transitions of booleanmode) 
    6173% 
    62 %                      |PERIODCOUNTERMODE|  (counr periods (up and down 
     74%                      |PERIODCOUNTERMODE|  (count periods (up and down 
    6375%                                           transition) of boolean mode) 
    6476% 
  • trunk/mfiles/OpenColor.m

    r904 r905  
    1111%   port. The value |port| can be addressed by the symbolic constants 
    1212%   |SENSOR_1| , |SENSOR_2|, |SENSOR_3| and |SENSOR_4| analog to the labeling on the NXT Brick. 
    13 %   This functions works for both HiTechnic Color sensors V1 and V2. 
     13%   This function works for both HiTechnic Color sensors V1 and V2. 
    1414% 
    1515%   With |GetColor(port)| you can receive color values as RGB or Index. 
     
    3535%+   CloseSensor(SENSOR_2); 
    3636% 
    37 % See also: GetColor, CloseSensor, OpenColorNXT2, GetColorNXT2, COM_ReadI2C 
     37% See also: GetColor, CalibrateColor, CloseSensor, OpenNXT2Color, GetNXT2Color, COM_ReadI2C 
    3838% 
    3939% Signature 
    40 %   Author: Rainer Schnitzler (see AUTHORS) 
     40%   Author: Rainer Schnitzler, Linus Atorf (see AUTHORS) 
    4141%   Date: 2010/09/16 
    4242%   Copyright: 2007-2010, RWTH Aachen University 
  • trunk/mfiles/OpenLight.m

    r896 r905  
    2626%+   CloseSensor(SENSOR_1); 
    2727% 
    28 % See also: NXT_SetInputMode, CloseSensor, GetLight, SENSOR_1, SENSOR_2, SENSOR_3, SENSOR_4 
     28% See also: CloseSensor, GetLight, OpenNXT2Color, GetNXT2Color, NXT_SetInputMode, SENSOR_1, SENSOR_2, SENSOR_3, SENSOR_4 
    2929% 
    3030% Signature 
  • trunk/mfiles/OpenNXT2Color.m

    r904 r905  
    1 function OpenNXT2Color(port, type, varargin) 
    2 %OPENCOLOR Summary of this function goes here 
    3 %   Detailed explanation goes here 
     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% *********************************************************************************************** 
     78% *  This file is part of the RWTH - Mindstorms NXT Toolbox.                                    * 
     79% *                                                                                             * 
     80% *  The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify  * 
     81% *  it under the terms of the GNU General Public License as published by the Free Software     * 
     82% *  Foundation, either version 3 of the License, or (at your option) any later version.        * 
     83% *                                                                                             * 
     84% *  The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful,       * 
     85% *  but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS  * 
     86% *  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.             * 
     87% *                                                                                             * 
     88% *  You should have received a copy of the GNU General Public License along with the           * 
     89% *  RWTH - Mindstorms NXT Toolbox. If not, see <http://www.gnu.org/licenses/>.                 * 
     90% *********************************************************************************************** 
     91 
    492 
    593%% check parameters 
    694 
    7   % check if handle is given; if not use default one 
    8   if nargin > 2 
    9     handle = varargin{1}; 
    10   else 
    11     handle = COM_GetDefaultNXT; 
    12   end%if 
    13    
    14   % also accept strings as input 
    15   if ischar(port) 
    16     port = str2double(port); 
    17   end%if 
    18    
     95    % check if handle is given; if not use default one 
     96    if nargin > 2 
     97        handle = varargin{1}; 
     98    else 
     99        handle = COM_GetDefaultNXT; 
     100    end%if 
     101 
     102    % also accept strings as input 
     103    if ischar(port) 
     104        port = str2double(port); 
     105    end%if 
     106 
    19107%% check and set mode 
    20108 
    21   sensormode = 'RAWMODE'; 
    22   if strcmp(type, 'LIGHTRED') || strcmp(type, 'LIGHT') 
    23     sensortype = 'COLORRED'; 
    24   elseif strcmp(type, 'LIGHTGREEN') 
    25     sensortype = 'COLORGREEN'; 
    26   elseif strcmp(type, 'LIGHTBLUE') 
    27     sensortype = 'COLORBLUE'; 
    28   elseif strcmp(type, 'COLOR') 
    29     sensortype = 'COLORFULL'; 
    30   else 
    31     error('Invalid mode.  Must be LIGHT, LIGHTRED, LIGHTGREEN, LIGHTBLUE or COLOR.') 
    32   end%if 
     109    sensormode = 'RAWMODE'; 
     110    if strcmpi(mode, 'RED') 
     111        sensortype = 'COLORRED'; 
     112    elseif strcmpi(mode, 'GREEN') 
     113        sensortype = 'COLORGREEN'; 
     114    elseif strcmpi(mode, 'BLUE') 
     115        sensortype = 'COLORBLUE'; 
     116    elseif strcmpi(mode, 'FULL') 
     117        sensortype = 'COLORFULL'; 
     118    elseif strcmpi(mode, 'NONE') 
     119        sensortype = 'COLORNONE'; 
     120    else 
     121        error('MATLAB:RWTHMindstormsNXT:Sensor:invalidMode', 'NXT 2.0 Color sensor mode has to be ''FULL'', ''RED'', ''GREEN'', ''BLUE'', or ''NONE'''); 
     122    end%if 
    33123   
    34124%% call NXT_SetInputMode function 
    35125 
    36   NXT_SetInputMode( port, sensortype, sensormode, 'dontreply', handle ); 
     126    NXT_SetInputMode( port, sensortype, sensormode, 'dontreply', handle ); 
    37127   
     128    % it was measured the NXT 2.0 color sensor takes about 170ms until 
     129    % values turn valid. The LEDs light up a tiny moment earlier... 
     130    % in here we wait the biggest amount of time: 
     131    pause(0.130); 
     132    % the rest will be waited inside GetNXT2Color dynamically as needed 
     133     
     134     
    38135end%function 
    39136