Requests and reads sensor data via I2C from a correctly configured digital sensor.
ReturnBytes = COM_ReadI2C(Port, RequestLen, DeviceAddress, RegisterAddress)
ReturnBytes = COM_ReadI2C(Port, RequestLen, DeviceAddress, RegisterAddress, handle)
This function is used to retrieve data from digital sensors (like the ultrasonic) in a comfortable way. It is designed as a helping function for developers wanting to access new sensors. For already implemented sensors (e.g. ultrasound, as well as HiTechnic's acceleration and infrared sensors), use the provided high-level functions such as GetUltrasonic, GetInfrared, etc.
For I2C communication, usually the NXT_SetInputMode command has to be used with the LOWSPEED_9V or LOWSPEED setting. Afterwards, commands can be send with NXT_LSWrite. Once a sensor is correctly working, i.e. has data available, you can use this function to retrieve them.
In COM_ReadI2C(Port, RequestLen, DeviceAddress, RegisterAddress), Port is the sensor-port the sensor is connected to. RequestLen specifies the amount of bytes you want to retrieve. For ultasound, this is 1. DeviceAddress is the sensor's address on the I2C bus. This sometimes can be changed, but not for the ultrasonic sensor. Default value is 0x02 (2 in decimal). Finally, RegisterAddress is the address where you want to read data from. For the ultrasound and many other sensors, the "data section" starts at 0x42 (66 in decimal).
As last argument you can pass a valid NXT-handle to be used by this function. If no handle is passed, the default set by COM_SetDefaultNXT will be used.
Returns: ReturnBytes, byte-array (column vector) of uint8. This array contains the raw sensor-data you requested. How to interpret them depends on the sensor. If communication failed (even after automatic retransmission) -- e.g. when the sensor get's disconnected while in use -- an empty vector  will be returned.
Please note that the return values of this function are of type uint8. You have to convert them to double (using double()) before performing calculations with them, otherwise you might get unexpected results!
The sensor you are addressing with this command has to be correctly opened and initialized of course -- otherwise no valid data can be received.
This example opens and reads the ultrasonic sensor
port = SENSOR_1; handle = COM_OpenNXT('bluetooth.ini'); OpenUltrasonic(port); % retrieve 1 byte from device 0x02, register 0x42 data = COM_ReadI2C(port, 1, uint8(2), uint8(66)); if isempty(data) DistanceCM = -1; else % don'f forget this double()!!! DistanceCM = double(data(1)); end%if
- Author: Linus Atorf (see AUTHORS)
- Date: 2008/09/23
- Copyright: 2007-2010, RWTH Aachen University