| 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 1 |
|---|
| 7 | % byte, the current sensor reading! |
|---|
| 8 | |
|---|
| 9 | RequestLen = 6; |
|---|
| 10 | I2Cdata = hex2dec(['02'; '42']); % Read Measurement Byte 0 (see LEGO Mindstorms NXT |
|---|
| 11 | % Ultrasonic Sensor - I2C Communication Protocol) |
|---|
| 12 | |
|---|
| 13 | NXT_LSWrite(f_sensorport, RequestLen, I2Cdata, 'dontreply') |
|---|
| 14 | |
|---|
| 15 | %---------------------------------------------------------------------- |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | %% Get current processing status of digital sensor |
|---|
| 19 | %---------------------------LSGETSTATUS--------------------------------; |
|---|
| 20 | |
|---|
| 21 | % we keep on asking the NXT when the ultrasonic sensor is ready... |
|---|
| 22 | % note the dangerous potential of an infinite loop in case the sensor |
|---|
| 23 | % should never get ready... |
|---|
| 24 | |
|---|
| 25 | BytesReady = 0; |
|---|
| 26 | while (BytesReady == 0) |
|---|
| 27 | %TODO some sort of timeout in here in case we never get an answer? |
|---|
| 28 | [BytesReady status] = NXT_LSGetStatus(f_sensorport); |
|---|
| 29 | end%if |
|---|
| 30 | |
|---|
| 31 | acc_vector = -1; |
|---|
| 32 | |
|---|
| 33 | %% If ready get object distance (ultrasonic sensor value) |
|---|
| 34 | if ( status == 0 ) && ( BytesReady > 0 ) |
|---|
| 35 | |
|---|
| 36 | %-------------------------LSREAD----------------------------------- |
|---|
| 37 | |
|---|
| 38 | [data BytesRead] = NXT_LSRead(f_sensorport); |
|---|
| 39 | |
|---|
| 40 | if BytesRead > 5 |
|---|
| 41 | if (data(1) > 127) data(1) = data(1) - 256; end |
|---|
| 42 | if (data(2) > 127) data(2) = data(2) - 256; end |
|---|
| 43 | if (data(3) > 127) data(3) = data(3) - 256; end |
|---|
| 44 | acc_vector(1) = data(1) * 4 + data(4); |
|---|
| 45 | acc_vector(2) = data(2) * 4 + data(5); |
|---|
| 46 | acc_vector(3) = data(3) * 4 + data(6); |
|---|
| 47 | end%if |
|---|
| 48 | |
|---|
| 49 | %------------------------------------------------------------------ |
|---|
| 50 | end%if |
|---|
| 51 | |
|---|
| 52 | end%function |
|---|