| 1 | function [acc_vector] = GetAccelerator(f_sensorport) |
|---|
| 2 | |
|---|
| 3 | %% Build hex command and send it with NXT_LSWrite |
|---|
| 4 | %-----------------------------LSWRITE---------------------------------- |
|---|
| 5 | |
|---|
| 6 | % create this I2C command. basically all we want to do is request 6 |
|---|
| 7 | % bytes, the current sensor reading! |
|---|
| 8 | |
|---|
| 9 | RequestLen = 6; |
|---|
| 10 | I2Cdata = hex2dec(['02'; '42']); |
|---|
| 11 | |
|---|
| 12 | NXT_LSWrite(f_sensorport, RequestLen, I2Cdata, 'dontreply') |
|---|
| 13 | |
|---|
| 14 | %---------------------------------------------------------------------- |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | %% Get current processing status of digital sensor |
|---|
| 18 | %---------------------------LSGETSTATUS--------------------------------; |
|---|
| 19 | |
|---|
| 20 | % we keep on asking the NXT when the ultrasonic sensor is ready... |
|---|
| 21 | % note the dangerous potential of an infinite loop in case the sensor |
|---|
| 22 | % should never get ready... |
|---|
| 23 | |
|---|
| 24 | %instead of "wasting time" in the idle loop below, waiting for the |
|---|
| 25 | %sensor to get ready, we could also just wait some time here and |
|---|
| 26 | %collect the sensor bytes then... |
|---|
| 27 | %pause(0.015); |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | BytesReady = 0; |
|---|
| 31 | ticID = 34; %fixme randomly, dangerous |
|---|
| 32 | timeout = 2; % in s |
|---|
| 33 | tictic(ticID); |
|---|
| 34 | status = -1; |
|---|
| 35 | %idlecount = 0; |
|---|
| 36 | while ((BytesReady < 6) || (status ~= 0)) && (toctoc(ticID) < timeout) |
|---|
| 37 | |
|---|
| 38 | [BytesReady status] = NXT_LSGetStatus(f_sensorport); |
|---|
| 39 | if status == 221 % communication bus error |
|---|
| 40 | % recursive! |
|---|
| 41 | acc_vector = GetAccelerator(f_sensorport); |
|---|
| 42 | return |
|---|
| 43 | end%if |
|---|
| 44 | %idlecount = idlecount + 1; |
|---|
| 45 | end%if |
|---|
| 46 | |
|---|
| 47 | %fprintf('idlecount = %d \n', idlecount) |
|---|
| 48 | % if (status ~= 0) || (BytesReady < 6) |
|---|
| 49 | % disp('whoa') |
|---|
| 50 | % end%if |
|---|
| 51 | |
|---|
| 52 | acc_vector = [NaN NaN NaN]; |
|---|
| 53 | |
|---|
| 54 | %% If ready get object distance (ultrasonic sensor value) |
|---|
| 55 | if ( status == 0 ) && ( BytesReady > 0 ) |
|---|
| 56 | |
|---|
| 57 | %-------------------------LSREAD----------------------------------- |
|---|
| 58 | |
|---|
| 59 | [data BytesRead] = NXT_LSRead(f_sensorport); |
|---|
| 60 | |
|---|
| 61 | % IMPORTANT, DON'T FORGET!!! |
|---|
| 62 | % or change in NXT_LSRead |
|---|
| 63 | data = double(data); |
|---|
| 64 | |
|---|
| 65 | if BytesRead > 5 |
|---|
| 66 | % as seen in the hitechnic example code... |
|---|
| 67 | % this is just a byte shift and could be done better, but |
|---|
| 68 | % should work |
|---|
| 69 | if (data(1) > 127) |
|---|
| 70 | data(1) = data(1) - 256; |
|---|
| 71 | end |
|---|
| 72 | if (data(2) > 127) data(2) = data(2) - 256; end |
|---|
| 73 | if (data(3) > 127) data(3) = data(3) - 256; end |
|---|
| 74 | acc_vector(1) = data(1) * 4 + data(4); |
|---|
| 75 | acc_vector(2) = data(2) * 4 + data(5); |
|---|
| 76 | acc_vector(3) = data(3) * 4 + data(6); |
|---|
| 77 | end%if |
|---|
| 78 | |
|---|
| 79 | %------------------------------------------------------------------ |
|---|
| 80 | else |
|---|
| 81 | [tmp msg] = checkStatusByte(status); |
|---|
| 82 | fprintf('Invalid sensor reading, %d bytes received, status %d: %s\n', BytesReady, status, msg); |
|---|
| 83 | |
|---|
| 84 | end%if |
|---|
| 85 | |
|---|
| 86 | end%function |
|---|