Changeset 299

Show
Ignore:
Timestamp:
09/25/08 15:56:39 (5 years ago)
Author:
atorf
Message:
  • Added optional NXT handle paramet to Get* functions
  • Updated documentation for some functions
Location:
branches/atorf/RWTHMindstormsNXT
Files:
11 modified

Legend:

Unmodified
Added
Removed
  • branches/atorf/RWTHMindstormsNXT/CalibrateCompass.m

    r298 r299  
    1 function [ok] = CalibrateCompass(port, f_start) 
    2 % Calibrate compass sensor 
     1function [ok] = CalibrateCompass(port, f_start, varargin) 
     2% Enables or calibration mode of the HiTechnic compass sensor 
    33% 
    44% Syntax 
    55%  |CalibrateCompass(port, f_start)| 
    66% 
     7%  |CalibrateCompass(port, f_start, handle)| 
     8% 
    79% Description 
    8 %   Calibrate compass to reduce influence of motor and brick on compass 
    9 %   values. You have to calibrate a robotor only once until new building 
    10 %   conditions. While calibration the motor very slowly should make two full rotations. 
    11 %   Compass sensor have to be opened before execution. 
    12 %   |f_start| = true starts calibration, |f_start| = false stopps it. 
     10%   Calibrate the compass to reduce influence of metallic objects, 
     11%   especially of the NXT motor and brick on compass values. 
     12%   You have to calibrate a roboter only once until the design changes. 
     13%   During calibration the compass should make two full rotations very slowly. 
     14%   The compass sensor has to be opened (using |OpenCompass|) before execution. 
     15%  
     16%   Set |f_start = true| to start calibration mode, and |f_start = false| to stop it. 
     17%   In between those commands, the calibration (compass rotation) should occur. 
     18% 
    1319%   The given |port| number specifies the connection port. The value |port| can be 
    1420%   addressed by the symbolic constants |SENSOR_1| , |SENSOR_2|, |SENSOR_3| and |SENSOR_4| 
    1521%   analog to the labeling on the NXT Brick.  
    16 %    
     22% 
     23%   The last optional argument can be a valid NXT handle. If none is 
     24%   specified, the default handle will be used (call |COM_SetDefaultNXT| to 
     25%   set one). 
     26% 
    1727% Example 
     28%  
     29%+     % enable calibration mode 
     30%+     CalibrateCompass(SENSOR_2, true); 
     31%+ 
     32%+     % compass is attached to motor A, rotate 2 full turns  
    1833%+     SetMotor(MOTOR_A); 
    19 %+     SetPower(5); 
    20 %+     SetAngleLimit(720); 
     34%+         SetPower(5); 
     35%+         SetAngleLimit(720); 
    2136%+     SendMotorSettings; 
    22 %+     CalibrateCompass(SENSOR_2, true); 
     37%+ 
    2338%+     WaitForMotor(MOTOR_A); 
     39%+ 
     40%+     % calibration should now be complete! 
    2441%+     CalibrateCompass(SENSOR_2, false); 
    2542% 
     
    2744% 
    2845% Signature 
    29 %   Author: Rainer Schnitzler (see AUTHORS) 
     46%   Author: Rainer Schnitzler, Linus Atorf (see AUTHORS) 
    3047%   Date: 2008/08/01 
    3148%   Copyright: 2007-2008, RWTH Aachen University 
     
    5067%% Check Parameter 
    5168    if nargin < 2 
    52         error('MATLAB:RWTHMindstormsNXT:Sensor:invalidParameter', 'Two function parameter are required, port and start/stop mode'); 
     69        error('MATLAB:RWTHMindstormsNXT:notEnoughInputArguments', 'Two function parameter are required, port and start/stop mode'); 
    5370    end 
     71     
     72     
     73    % check if handle is given; if not use default one 
     74    if nargin > 2 
     75        handle = varargin{1}; 
     76    else 
     77        handle = COM_GetDefaultNXT; 
     78    end%if 
    5479     
    5580%% create this I2C command to start callibration 
     
    5782    if f_start 
    5883      I2Cdata = hex2dec(['02'; '41'; '43']); % Command (41): Start callibration (43)  
    59       NXT_LSWrite(port, 0, I2Cdata, 'dontreply');  
     84      NXT_LSWrite(port, 0, I2Cdata, 'dontreply', handle);  
    6085      ok = true; 
    6186    else 
    6287      % set back to 0 
    6388      I2Cdata = hex2dec(['02'; '41'; '00']); % Command (41): Stop callibration (00)       
    64       NXT_LSWrite(port, 0, I2Cdata, 'dontreply'); 
     89      NXT_LSWrite(port, 0, I2Cdata, 'dontreply', handle); 
    6590       
    66       % wait for compass sensor to get ready and write a 2 in case calibration failed 
    67       pause(0.2); % 200ms, just a guess, less should be ok? 
     91      % wait for compass sensor to get ready and decide wether calibration 
     92      % was successful (it writes a 2 in case calibration failed) 
     93      pause(0.2); % 200ms, just a guess... enough or too long? 
    6894       
    6995      % retrieve 1 byte from device 0x02, register 0x41     
    70       data = COM_ReadI2C(port, 1, uint8(2), uint8(65));      % calibration ok ? 
     96      data = COM_ReadI2C(port, 1, uint8(2), uint8(65), handle);      % calibration ok ? 
    7197      if isempty(data) || (data == 2) 
    7298         ok = false; 
  • branches/atorf/RWTHMindstormsNXT/CloseSensor.m

    r123 r299  
    1 function [] = CloseSensor(f_sensorport) 
     1function CloseSensor(port, varargin) 
    22% Closes a specified sensor port (e.g. turns off the active light mode of the NXT light sensor) 
    33% 
    44% Syntax 
    55%   |CloseSensor(port)| 
     6% 
     7%   |CloseSensor(port, handle)| 
    68% 
    79% Description 
     
    1113%   mode (the red light is turned off), closing the Ultrasonic sensor stops sending out ultrasound. 
    1214% 
     15%   The last optional argument can be a valid NXT handle. If none is 
     16%   specified, the default handle will be used (call |COM_SetDefaultNXT| to 
     17%   set one). 
    1318% 
    1419% Examples 
     
    4348 
    4449%% check parameters 
    45     % also accept strings as input 
    46     if ischar(f_sensorport) 
    47         f_sensorport = str2double(f_sensorport); 
     50 
     51    % check if handle is given; if not use default one 
     52    if nargin > 1 
     53        handle = varargin{1}; 
     54    else 
     55        handle = COM_GetDefaultNXT; 
    4856    end%if 
    4957 
     58 
     59    % also accept strings as input 
     60    if ischar(port) 
     61        port = str2double(port); 
     62    end%if 
     63 
     64     
    5065 
    5166%% Set correct variables 
     
    5671 
    5772%% Call NXT_SetInputMode function        
    58     NXT_SetInputMode(f_sensorport, sensortype, sensormode, 'dontreply');  
     73    NXT_SetInputMode(port, sensortype, sensormode, 'dontreply', handle);  
    5974 
    6075end%function 
  • branches/atorf/RWTHMindstormsNXT/GetAccelerator.m

    r291 r299  
    1 function [acc_vector] = GetAccelerator(port) 
    2 % Reads the current value of the NXT accelerator sensor 
     1function [acc_vector] = GetAccelerator(port, varargin) 
     2% Reads the current value of the HiTechnic acceleration sensor 
    33% 
    44% Syntax 
    55%   |acc_vector = GetAccelerator(port)| 
    66% 
     7%   |acc_vector = GetAccelerator(port, handle)| 
     8% 
    79% Description 
    8 %   |acc_vector = GetAccelerator(port)| returns the current 1x3 accelerator vector |acc_vector| of the NXT 
    9 %   accelerator sensor. |distance| represents the measured distance in cm. 
     10%   |acc_vector = GetAccelerator(port)| returns the current 1x3 accelerator vector |acc_vector| of  
     11%   the HiTechnic acceleration sensor. The column vector contains the 
     12%   readings of the x, y, and z-axis, respectively. A reading of 200 is 
     13%   equal to the acceleration of 1g. Maximum range is -2g to +2g. 
    1014%   The given |port| number specifies the connection port. The value |port| can be 
    1115%   addressed by the symbolic constants |SENSOR_1| , |SENSOR_2|, |SENSOR_3| and |SENSOR_4| analog to 
    1216%   the labeling on the NXT Brick. 
    1317% 
     18%   The last optional argument can be a valid NXT handle. If none is 
     19%   specified, the default handle will be used (call |COM_SetDefaultNXT| to 
     20%   set one). 
    1421% 
    1522% Example 
     
    6168%      
    6269 
    63      
     70    % check if handle is given; if not use default one 
     71    if nargin > 1 
     72        handle = varargin{1}; 
     73    else 
     74        handle = COM_GetDefaultNXT; 
     75    end%if     
     76 
     77 
    6478    % initialize 
    6579    acc_vector = [NaN; NaN; NaN]; 
     
    6882     
    6983    % retrieve 6 bytes from device 0x02, register 0x42 
    70     data = COM_ReadI2C(port, 6, uint8(2), uint8(66)); 
     84    data = COM_ReadI2C(port, 6, uint8(2), uint8(66), handle); 
    7185     
    7286    if ~isempty(data) 
  • branches/atorf/RWTHMindstormsNXT/GetCompass.m

    r298 r299  
    1 function [degree] = GetCompass(port) 
    2 % Reads the current value of the NXT compass sensor 
     1function [degree] = GetCompass(port, varargin) 
     2% Reads the current value of the HiTechnic compass sensor 
    33% 
    44% Syntax 
    55%   |degree = GetCompass(port)| 
    66% 
     7%   |degree = GetCompass(port, handle)| 
     8% 
    79% Description 
    8 %   |degree = GetCompass(port)| returns the current heading value NXT 
    9 %   compass sensor ranging from 0 to 360 where 0 is north and 
     10%   |degree = GetCompass(port)| returns the current heading value of the 
     11%   HiTechnic magnetic compass sensor ranging from 0 to 360 where 0 is north and 
    1012%   counterclockwise (90 = west etc.). 
    1113%   The given |port| number specifies the connection port. The value |port| can be addressed by the 
     
    1517%   For more complex settings the functions |NXT_LSRead| and |NXT_LSWrite| can be used. 
    1618% 
     19%   The last optional argument can be a valid NXT handle. If none is 
     20%   specified, the default handle will be used (call |COM_SetDefaultNXT| to 
     21%   set one). 
     22% 
    1723% Example 
    1824%+   OpenCompass(SENSOR_4); 
     
    2026%+   CloseSensor(SENSOR_4); 
    2127% 
    22 % See also: OpenCompass, CloseSensor, COM_ReadI2C 
     28% See also: OpenCompass, CalibrateCompass CloseSensor, COM_ReadI2C 
    2329% 
    2430% Signature 
     
    4349% *  RWTH - Mindstorms NXT Toolbox. If not, see <http://www.gnu.org/licenses/>.                 * 
    4450% *********************************************************************************************** 
    45   
     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 
    4659    % initialize 
    4760    degree = NaN; 
     
    5164    % see http://www.hitechnic.com for more register information 
    5265    % retrieve 2 bytes from device 0x02, register 0x44     
    53     data = COM_ReadI2C(port, 2, uint8(2), uint8(68)); 
     66    data = COM_ReadI2C(port, 2, uint8(2), uint8(68), handle); 
    5467 
    5568    if ~isempty(data) 
  • branches/atorf/RWTHMindstormsNXT/GetInfrared.m

    r291 r299  
    1 function [direction rawData] = GetInfrared(port) 
    2 % Reads the current value of the NXT infrared sensor (infrared seeker) 
     1function [direction rawData] = GetInfrared(port, varargin) 
     2% Reads the current value of the Hitechnic infrared sensor (infrared seeker) 
    33% 
    44% Syntax 
    55%   |[direction rawData] = GetInfrared(port)| 
     6% 
     7%   |[direction rawData] = GetInfrared(port, handle)| 
    68% 
    79% Description 
     
    911%   detected infrared signal. |direction| represents the main direction (1-9) calculated based on 
    1012%   the measured raw data given in the |rawData| vector (1x5). Five sensors are provided by the 
    11 %   infrared seeker. 
     13%   infrared seeker. For more information see http://www.hitechnic.com 
     14% 
    1215%   The given |port| number specifies the connection port. The value |port| can be 
    1316%   addressed by the symbolic constants |SENSOR_1| , |SENSOR_2|, |SENSOR_3| and |SENSOR_4| analog to 
    1417%   the labeling on the NXT Brick. 
    1518% 
     19%   The last optional argument can be a valid NXT handle. If none is 
     20%   specified, the default handle will be used (call |COM_SetDefaultNXT| to 
     21%   set one). 
    1622% 
    1723% Example 
     
    4450% *********************************************************************************************** 
    4551 
     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 
    4659    % initialize 
    4760    direction = NaN; 
     
    4962 
    5063 
    51      
    5264    % retrieve 6 bytes from device 0x02, register 0x42 
    53     data = COM_ReadI2C(port, 6, uint8(2), uint8(66)); 
     65    data = COM_ReadI2C(port, 6, uint8(2), uint8(66), handle); 
    5466     
    5567    if ~isempty(data) 
     
    5870         
    5971        direction = data(1); 
    60         rawData = data(2:6); 
    61          
     72        rawData = data(2:6);     
    6273 
    6374    end%if 
  • branches/atorf/RWTHMindstormsNXT/GetLight.m

    r123 r299  
    1 function light = GetLight(f_sensorport) 
     1function light = GetLight(port, varargin) 
    22% Reads the current value of the NXT light sensor 
    33% 
    44% Syntax 
    55%   |light = GetLight(port)| 
     6% 
     7%   |light = GetLight(port, handle)| 
    68% 
    79% Description 
     
    1214%   addressed by the symbolic constants |SENSOR_1| , |SENSOR_2|, |SENSOR_3| and |SENSOR_4| analog to 
    1315%   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). 
    1420% 
    1521%   For more complex settings the function |NXT_GetInputValues| can be used. 
     
    4450% *********************************************************************************************** 
    4551 
     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     
    4659%% Call NXT_GetInputValues function 
    47     in = NXT_GetInputValues(f_sensorport); 
     60    in = NXT_GetInputValues(port, handle); 
    4861 
    4962%% Return normalized sound value (0...1023 / 10 Bit)     
  • branches/atorf/RWTHMindstormsNXT/GetMemoryCount.m

    r123 r299  
    1 function memory = GetMemoryCount(number) 
     1function memory = GetMemoryCount(number, varargin) 
    22% Gets the internal NXT memory counter (manual mapping replica) 
    33 
    44% Syntax 
    55%   |memory = GetMemoryCount(port)|  
     6% 
     7%   |memory = GetMemoryCount(port, handle)|  
    68% 
    79% Description 
     
    1113%   |memory| contains the value of the NXT memory counter (maunal mapping replica). 
    1214% 
     15%   The last optional argument can be a valid NXT handle. If none is 
     16%   specified, the default handle will be used (call |COM_SetDefaultNXT| to 
     17%   set one). 
     18% 
     19% 
    1320% Note: 
    1421%   This function is *recommened for the advanced user*. 
     
    1724%+   memory = GetMemoryCount(MOTOR_A); 
    1825% 
    19 % See also: SetMemoryCounter, MOTOR_A, MOTOR_B, MOTOR_C 
     26% See also: SetMemoryCount, MOTOR_A, MOTOR_B, MOTOR_C 
    2027% 
    2128% Signature 
     
    4148% *********************************************************************************************** 
    4249 
    43 % get default handle & motorstate 
    44 h = COM_GetDefaultNXT(); 
     50% check if handle is given; if not use default one 
     51if nargin > 1 
     52    h = varargin{1}; 
     53else 
     54    h = COM_GetDefaultNXT; 
     55end%if 
     56 
     57% get motorstate 
    4558NXTMOTOR_State = h.NXTMOTOR_getState(); 
    4659 
  • branches/atorf/RWTHMindstormsNXT/GetMotorSettings.m

    r289 r299  
    1 function out = GetMotorSettings(whatmotor) 
     1function out = GetMotorSettings(whatmotor, varargin) 
    22% Returns the current motor data / settings (e.g. position, speed, etc.) from the specified motor 
    33 
    44% Syntax 
    55%   |data = GetMotorSettings(port)|  
     6% 
     7%   |data = GetMotorSettings(port, handle)|  
    68% 
    79% Description 
     
    1012%   |MOTOR_C| analog to the labeling on the NXT Brick. The return value |data| is a struct variable. 
    1113%   It contains several motor settings and information. 
     14% 
     15%   The last optional argument can be a valid NXT handle. If none is 
     16%   specified, the default handle will be used (call |COM_SetDefaultNXT| to 
     17%   set one). 
    1218% 
    1319% Output: 
     
    6672 
    6773 
     74% check if handle is given; if not use default one 
     75if nargin > 1 
     76    h = varargin{1}; 
     77else 
     78    h = COM_GetDefaultNXT; 
     79end%if 
     80 
    6881if whatmotor < 0 || whatmotor > 2 
    6982    error('MATLAB:RWTHMindstormsNXT:Motor:invalidPort', 'Input argument for motor must be 0, 1, or 2') 
    7083end%if 
    7184 
    72 % get default handle & motorstate 
    73 h = COM_GetDefaultNXT(); 
     85% get motorstate 
    7486NXTMOTOR_State = h.NXTMOTOR_getState(); 
    7587 
    7688 
    77 data = NXT_GetOutputState(whatmotor); 
     89data = NXT_GetOutputState(whatmotor, h); 
    7890 
    7991 
     
    129141NXTMOTOR_State(whatmotor + 1).TachoCount = data.TachoCount; 
    130142 
     143% TODO the whole concept of storing this freshly retrieved information back 
     144% to the handle might not be that useful anymore... as soon as the data 
     145% arrives, it's basically out of date. So why would we want to keep it? 
     146 
    131147% save motor state back to handle 
    132148h.NXTMOTOR_setState(NXTMOTOR_State); 
  • branches/atorf/RWTHMindstormsNXT/GetSound.m

    r289 r299  
    1 function out = GetSound(f_sensorport) 
     1function out = GetSound(f_sensorport, varargin) 
    22% Reads the current value of the NXT sound sensor 
    33% 
    44% Syntax 
    55%   |sound = GetSound(port)| 
     6% 
     7%   |sound = GetSound(port, handle)| 
    68% 
    79% Description 
     
    1214%   addressed by the symbolic constants |SENSOR_1|, |SENSOR_2|, |SENSOR_3| and |SENSOR_4| analog to 
    1315%   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). 
    1420% 
    1521%   For more complex settings the function |NXT_GetInputMode| can be used. 
     
    4450% *********************************************************************************************** 
    4551     
     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 
    4659%% Call NXT_GetInputValues function 
    47     in = NXT_GetInputValues(f_sensorport); 
     60    in = NXT_GetInputValues(f_sensorport, handle); 
    4861     
    4962%% Return normalized sound value (0...1023 / 10 Bit) 
  • branches/atorf/RWTHMindstormsNXT/GetSwitch.m

    r290 r299  
    1 function out = GetSwitch(f_sensorport) 
     1function out = GetSwitch(f_sensorport, varargin) 
    22% Reads the current value of the NXT switch / touch sensor 
    33% 
    44% Syntax 
    55%   |switch = GetSwitch(port)| 
     6% 
     7%   |switch = GetSwitch(port, handle)| 
    68% 
    79% Description 
     
    1315%   the labeling on the NXT Brick. 
    1416% 
     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% 
    1521%   For more complex settings the function |NXT_GetInputValues| can be used. 
     22% 
    1623% 
    1724% Example 
     
    5865% 
    5966 
     67%% check if handle is given; if not use default one 
     68    if nargin > 1 
     69        handle = varargin{1}; 
     70    else 
     71        handle = COM_GetDefaultNXT; 
     72    end%if 
     73 
    6074 
    6175%% Call NXT_GetInputValues function 
    62     in = NXT_GetInputValues(f_sensorport); 
     76    in = NXT_GetInputValues(f_sensorport, handle); 
    6377     
    6478%% Do threshold decision 
  • branches/atorf/RWTHMindstormsNXT/GetUltrasonic.m

    r296 r299  
    1 function [DistanceCM] = GetUltrasonic(port) 
     1function [DistanceCM] = GetUltrasonic(port, varargin) 
    22% Reads the current value of the NXT ultrasonic sensor 
    33% 
    44% Syntax 
    55%   |distance = GetUltrasonic(port)| 
     6% 
     7%   |distance = GetUltrasonic(port, handle)| 
    68% 
    79% Description 
     
    1214%   the labeling on the NXT Brick. 
    1315% 
    14 %   For more complex settings the functions |NXT_LSRead| and |NXT_LSWrite| can be used. 
     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% 
    1528% 
    1629% Example 
     
    4356% *********************************************************************************************** 
    4457 
     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 
    4564 
    4665    % also accept strings as input 
     
    5675 
    5776    % retrieve 1 byte from device 0x02, register 0x42 
    58     data = COM_ReadI2C(port, 1, uint8(2), uint8(66)); 
     77    data = COM_ReadI2C(port, 1, uint8(2), uint8(66), handle); 
    5978     
    6079    if isempty(data)