| 2 | | |
| | 2 | % Requests and reads sensor data via I2C from a correctly configured digital sensor. |
| | 3 | % |
| | 4 | % Syntax |
| | 5 | % |ReturnBytes = COM_ReadI2C(Port, RequestLen, DeviceAddress, RegisterAddress)| |
| | 6 | % |
| | 7 | % |ReturnBytes = COM_ReadI2C(Port, RequestLen, DeviceAddress, RegisterAddress, handle)| |
| | 8 | % |
| | 9 | % Description |
| | 10 | % This function is used to retrieve data from digital sensors (like the |
| | 11 | % ultrasonic) in a comfortable way. It is designed as a helping function |
| | 12 | % for developers wanting to access new sensors. For already implemented |
| | 13 | % sensors (e.g. ultrasound, as well as HiTechnic's acceleration and |
| | 14 | % infrared sensors), use the provided high-level functions such as |
| | 15 | % |GetUltrasonic|, |GetInfrared|, etc. |
| | 16 | % |
| | 17 | % For I2C communication, usually the |NXT_SetInputMode| command has to |
| | 18 | % be used with the |LOWSPEED_9V| or |LOWSPEED| setting. Afterwards, |
| | 19 | % commands can be send with |NXT_LSWrite|. Once a sensor is correctly |
| | 20 | % working, i.e. has data available, you can use this function to |
| | 21 | % retrieve them. |
| | 22 | % |
| | 23 | % In |COM_ReadI2C(Port, RequestLen, DeviceAddress, RegisterAddress)|, |
| | 24 | % |Port| is the sensor-port the sensor is connected to. |RequestLen| |
| | 25 | % specifies the amount of bytes you want to retrieve. For ultasound, |
| | 26 | % this is 1. |DeviceAddress| is the sensor's address on the I2C bus. |
| | 27 | % This sometimes can be changed, but not for the ultrasonic sensor. |
| | 28 | % Default value is 0x02 (2 in decimal). Finally, |RegisterAddress| is |
| | 29 | % the address where you want to read data from. For the ultrasound and |
| | 30 | % many other sensors, the "data section" starts at 0x24 (66 in decimal). |
| | 31 | % |
| | 32 | % As last argument you can pass a valid NXT-handle to be used from this |
| | 33 | % function. If no handle is passed, the default set by |
| | 34 | % |COM_SetDefaultNXT will be used. |
| | 35 | % |
| | 36 | % *Returns:* |ReturnBytes|, byte-array (column vector) of uint8 |
| | 37 | % This array contains the raw sensor-data you requested. How to |
| | 38 | % interpret them depends on the sensor. If communication failed (even |
| | 39 | % after automatic retransmission) -- e.g. when the sensor get's |
| | 40 | % disconnected while in use -- an empty vector |[]| will be returned. |
| | 41 | % |
| | 42 | % Note |
| | 43 | % Please note that the return values of this function are of type uint8. |
| | 44 | % You have to convert them to double (using |double()|) before |
| | 45 | % performing calculations with them, otherwise you might get unexpected |
| | 46 | % results! |
| | 47 | % |
| | 48 | % The sensor you are addressing with this command has to be correctly |
| | 49 | % opened and initialized of course -- otherwise no valid data can be |
| | 50 | % received. |
| | 51 | % |
| | 52 | % Example |
| | 53 | % This example opens and reads the ultrasonic sensor |
| | 54 | %+ port = SENSOR_1; |
| | 55 | %+ handle = COM_OpenNXT('bluetooth.ini', 'check'); |
| | 56 | %+ |
| | 57 | %+ OpenUltrasonic(port); |
| | 58 | %+ |
| | 59 | %+ % retrieve 1 byte from device 0x02, register 0x42 |
| | 60 | %+ data = COM_ReadI2C(port, 1, uint8(2), uint8(66)); |
| | 61 | %+ |
| | 62 | %+ if isempty(data) |
| | 63 | %+ DistanceCM = -1; |
| | 64 | %+ else |
| | 65 | %+ % don'f forget this double()!!! |
| | 66 | %+ DistanceCM = double(data(1)); |
| | 67 | %+ end%if |
| | 68 | % |
| | 69 | % |
| | 70 | % See also: NXT_LSWrite, NXT_LSRead, NXT_LSGetStatus, NXT_SetInputMode, OpenUltrasonic, |
| | 71 | % GetUltrasonic, SENSOR_1, SENSOR_2, SENSOR_3, SENSOR_4 |
| | 72 | % |
| | 73 | % |
| | 74 | % Signature |
| | 75 | % Author: Linus Atorf (see AUTHORS) |
| | 76 | % Date: 2008/09/23 |
| | 77 | % Copyright: 2007-2008, RWTH Aachen University |
| | 78 | % |