Changeset 275

Show
Ignore:
Timestamp:
09/19/08 16:15:15 (5 years ago)
Author:
atorf
Message:

Updated USGetSnapshotResults to use new COM_ReadI2C function

Location:
branches/atorf/RWTHMindstormsNXT
Files:
2 modified

Legend:

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

    r273 r275  
    4444 
    4545 
     46    % also accept strings as input 
     47    if ischar(f_sensorport) 
     48        f_sensorport = str2double(f_sensorport); 
     49    end%if 
     50 
     51 
    4652    % The old version looked like this: 
    4753    %RequestLen = 1; 
  • branches/atorf/RWTHMindstormsNXT/USGetSnapshotResults.m

    r253 r275  
    6464 
    6565 
    66  
    6766    % also accept strings as input 
    6867    if ischar(f_sensorport) 
     
    7170 
    7271 
     72    snaps = ones(8, 1) * -1; % initialize 8 results 
     73     
     74    % old command: 
     75    %I2Cdata(1) = hex2dec('02'); % the default I2C address for a port.  
     76    %I2Cdata(2) = hex2dec('42'); % READ! 
     77    %NXT_LSWrite(f_sensorport, RequestLen, I2Cdata, 'dontreply'); 
    7378 
    74  
    75 %% Build hex command and send it with NXT_LSWrite    
    76     %---------------------------------------------------------------------- 
    77     % (see LEGO Mindstorms NXT Ultrasonic Sensor - I2C Communication Protocol) 
    78          
    79     
    80     RequestLen = 8; % -> we want 8 bytes 
    81  
    82     snaps = zeros(RequestLen, 1); %preallocate 
     79    % retrieve 8 bytes from device 0x02, register 0x42 
     80    data = COM_ReadI2C(f_sensorport, 8, uint8(2), uint8(66)); 
    8381     
    84     I2Cdata(1) = hex2dec('02'); % the default I2C address for a port.  
    85     I2Cdata(2) = hex2dec('42'); % READ! 
    86     %wrong: I2Cdata(2) = hex2dec('03'); % start here 
    87      
    88     NXT_LSWrite(f_sensorport, RequestLen, I2Cdata, 'dontreply'); 
    89  
    90     %-------------------------------------------------------------------- 
    91      
    92      
    93 %% Wait for the reply  
    94     BytesReady = 0; 
    95     startTime = clock; 
    96     status = -1; 
    97     timeOut = 2; % in seconds 
    98     % we require that some bytes are ready, AND that the current response 
    99     % packet does not contain an error message! additionally we have a 
    100     % timeout - should the sensor hang, we exit with the invalid distance 
    101     % reading -1 
    102     while ((BytesReady < RequestLen) || (status ~= 0)) && etime(clock, startTime) < timeOut 
    103         [BytesReady status] = NXT_LSGetStatus(f_sensorport); 
     82    if ~isempty(data) 
     83        % important to convert to double!!! 
     84        snaps = double(data); 
    10485    end%if 
    10586     
    106 %% Harvesting 
    107      
    108     [data BytesRead]= NXT_LSRead(f_sensorport); 
    109      
    110     if BytesRead ~= RequestLen 
    111         warning('MATLAB:RWTHMindstormsNXT:I2C:notEnoughBytesAvailable', 'I2C: We didn''t get the amount of bytes we requested in LSWrite, but we defenitely should have!') 
    112     end%if 
    113      
    114     if BytesRead < RequestLen 
    115         % if less than expected.... 
    116         snaps(1:BytesRead) = data(1:BytesRead); 
    117     else 
    118         % take all we need... 
    119         snaps(1:RequestLen) = data(1:RequestLen); 
    120     end%if 
    12187     
    12288end