Hi,
I'm trying to use the us-sensor with pbLua, but without success.
The result of reading a byte from sensor is allways 0.
What means at HDK I2C Communication protocol:
Read measurement Byte 0 : 0x02 0x42 R+0x03 (what is R+0x03) ???
Any idea ?
collectgarbage( "setpause", 110 );
function SensorUSInit( port )
nxt.InputSetType(port,2)
nxt.InputSetDir(port,1,1)
nxt.InputSetState(port,1,1)
end
function I2CWait( port )
while true do
local txstatus, rxstatus = nxt.I2CGetStatus( port )
if (0 ~= rxstatus) and (0 ~= txstatus) then
break
end
end
end
-- Set up the ultrasound sensor on port 4
SensorUSInit(4)
-- And start the I2C subsystem
nxt.I2CTimer(1)
nxt.I2CInitPins(4)
-- fill in code to start the US device, continuous mode
nxt.I2CSendData( 4, string.format( "%c%c%c", 0x02, 0x41, 0x02 ),3,0)
I2CWait(4)
-- Here's where you read the actual data - 2 bytes out, 1 byte reply
function ReadUS()
while (true) do
button=nxt.ButtonRead();
if(button==1) then exit() end
-- Read measurement byte 0
nxt.I2CSendData( 4, string.format( "%c%c", 0x02, 0x42 ), 2, 1 )
-- wait around for the US sensor to be done
I2CWait(4)
-- Read the sensor result, and convert to a byte
usSensor = string.byte( nxt.I2CRecvData( 4, 1 ) )
print("US-Sensor: "..usSensor);
end
end
Thanks for help
Bye
Christian
|