Changeset 273

Show
Ignore:
Timestamp:
09/18/08 20:27:12 (5 years ago)
Author:
atorf
Message:

- added new function COM_ReadI2C
- adapted GetUltrasonic? to use COM_ReadI2C
- added new functions OpenAccelerator? and GetAccelerator?

Location:
branches/atorf/RWTHMindstormsNXT
Files:
3 added
1 modified

Legend:

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

    r265 r273  
    4343% *********************************************************************************************** 
    4444 
    45     %----------------------------LSREAD------------------------------------ 
    46     % check for pending reply bytes in the NXT buffers: send 'read bytes', 
    47     % retrieve garbage reply bytes     
    48     
    4945 
    50     %****** Apparently this LSRead is totally unnecessary, that's why I 
    51     % commented it, it seems to work without it... 
     46    % The old version looked like this: 
     47    %RequestLen = 1; 
     48    %I2Cdata = hex2dec(['02'; '42']); % Read Measurement Byte 0 (see LEGO Mindstorms NXT  
     49    %                                 % Ultrasonic Sensor - I2C Communication Protocol) 
     50 
     51    % retrieve 1 byte from device 0x02, register 0x42 
     52    data = COM_ReadI2C(f_sensorport, 1, uint8(2), uint8(66)); 
    5253     
    53     %[bytesRead tmp] = NXT_LSRead(f_sensorport);  
    54      
    55     % especially as we don't use these values from LSRead here...! 
    56      
    57     %---------------------------------------------------------------------- 
    58      
    59 %% Build hex command and send it with NXT_LSWrite 
    60     %-----------------------------LSWRITE---------------------------------- 
    61  
    62     % create this I2C command. basically all we want to do is request 1 
    63     % byte, the current sensor reading! 
    64      
    65     RequestLen = 1; 
    66     I2Cdata = hex2dec(['02'; '42']); % Read Measurement Byte 0 (see LEGO Mindstorms NXT  
    67                                      % Ultrasonic Sensor - I2C Communication Protocol) 
    68      
    69         NXT_LSWrite(f_sensorport, RequestLen, I2Cdata, 'dontreply') 
    70      
    71     %---------------------------------------------------------------------- 
    72      
    73      
    74 %% Get current processing status of digital sensor     
    75     %---------------------------LSGETSTATUS--------------------------------; 
    76      
    77     % we keep on asking the NXT when the ultrasonic sensor is ready... 
    78     % note the dangerous potential of an infinite loop in case the sensor 
    79     % should never get ready... 
    80      
    81     %BytesReady = 0; 
    82     startTime = clock; 
    83     status = -1; 
    84     timeOut = 1; % in seconds 
    85     % we require that some bytes are ready, AND that the current response 
    86     % packet does not contain an error message! additionally we have a 
    87     % timeout - should the sensor hang, we exit with the invalid distance 
    88     % reading -1 
    89  
    90 %     while ((BytesReady == 0) || (status ~= 0)) && etime(clock, startTime) < timeOut 
    91 %         [BytesReady status] = NXT_LSGetStatus(f_sensorport); 
    92 %     end%if 
     54    if isempty(data) 
     55        DistanceCM = -1; 
     56    else 
     57        % this double() is so important!!! 
     58        DistanceCM = double(data(1)); 
     59    end%if 
    9360 
    9461 
    95     BytesRead = 0; 
    96     DistanceCM = -1; 
    97  
    98 %% If ready get object distance (ultrasonic sensor value)     
    99     %CallCounter = 0; 
    100     while ((status ~= 0) || ( BytesRead < 1)) && etime(clock, startTime) < timeOut 
    101          
    102         %-------------------------LSREAD----------------------------------- 
    103          
    104         [data BytesRead status] = NXT_LSRead(f_sensorport); 
    105         %CallCounter = CallCounter + 1; 
    106         %disp(sprintf('Call #%d: bytes read = %d, status = %d', CallCounter, BytesRead, status)); 
    107          
    108         if BytesRead > 0 
    109             % important ti return double!!! 
    110             DistanceCM = double(data(1)); 
    111         end%if 
    112          
    113         %------------------------------------------------------------------ 
    114     end%if 
    115      
    116 %     if CallCounter > 1 
    117 %         disp('twice') 
    118 %     end%if 
    11962     
    12063end%function