Changeset 275
- Timestamp:
- 09/19/08 16:15:15 (5 years ago)
- Location:
- branches/atorf/RWTHMindstormsNXT
- Files:
-
- 2 modified
-
GetUltrasonic.m (modified) (1 diff)
-
USGetSnapshotResults.m (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/atorf/RWTHMindstormsNXT/GetUltrasonic.m
r273 r275 44 44 45 45 46 % also accept strings as input 47 if ischar(f_sensorport) 48 f_sensorport = str2double(f_sensorport); 49 end%if 50 51 46 52 % The old version looked like this: 47 53 %RequestLen = 1; -
branches/atorf/RWTHMindstormsNXT/USGetSnapshotResults.m
r253 r275 64 64 65 65 66 67 66 % also accept strings as input 68 67 if ischar(f_sensorport) … … 71 70 72 71 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'); 73 78 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)); 83 81 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); 104 85 end%if 105 86 106 %% Harvesting107 108 [data BytesRead]= NXT_LSRead(f_sensorport);109 110 if BytesRead ~= RequestLen111 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%if113 114 if BytesRead < RequestLen115 % if less than expected....116 snaps(1:BytesRead) = data(1:BytesRead);117 else118 % take all we need...119 snaps(1:RequestLen) = data(1:RequestLen);120 end%if121 87 122 88 end
