| 1 | |
|---|
| 2 | <!DOCTYPE html |
|---|
| 3 | PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
|---|
| 4 | <html> |
|---|
| 5 | <head> |
|---|
| 6 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
|---|
| 7 | |
|---|
| 8 | <!-- |
|---|
| 9 | This HTML is auto-generated from an M-file. |
|---|
| 10 | To make changes, update the M-file and republish this document. |
|---|
| 11 | --> |
|---|
| 12 | <title>COM_ReadI2C</title> |
|---|
| 13 | <meta name="generator" content="MATLAB 7.7"> |
|---|
| 14 | <meta name="date" content="2008-11-05"> |
|---|
| 15 | <meta name="m-file" content="script_COM_ReadI2C"> |
|---|
| 16 | <link type="text/css" rel="stylesheet" href="../../style.css"> |
|---|
| 17 | </head> |
|---|
| 18 | <body> |
|---|
| 19 | <p class="header">RWTH - Mindstorms NXT Toolbox</p> |
|---|
| 20 | <div class="content"> |
|---|
| 21 | <h1>COM_ReadI2C</h1> |
|---|
| 22 | <!--introduction--> |
|---|
| 23 | <p>Requests and reads sensor data via I2C from a correctly configured digital sensor.</p> |
|---|
| 24 | <!--/introduction--> |
|---|
| 25 | <h2>Contents</h2> |
|---|
| 26 | <div> |
|---|
| 27 | <ul> |
|---|
| 28 | <li><a href="#2">Syntax</a></li> |
|---|
| 29 | <li><a href="#5">Description</a></li> |
|---|
| 30 | <li><a href="#17">Example</a></li> |
|---|
| 31 | <li><a href="#20">See also</a></li> |
|---|
| 32 | <li><a href="#23">Signature</a></li> |
|---|
| 33 | </ul> |
|---|
| 34 | </div> |
|---|
| 35 | <h2>Syntax<a name="2"></a></h2> |
|---|
| 36 | <p><tt>ReturnBytes = COM_ReadI2C(Port, RequestLen, DeviceAddress, RegisterAddress)</tt></p> |
|---|
| 37 | <p><tt>ReturnBytes = COM_ReadI2C(Port, RequestLen, DeviceAddress, RegisterAddress, handle)</tt></p> |
|---|
| 38 | <h2>Description<a name="5"></a></h2> |
|---|
| 39 | <p>This function is used to retrieve data from digital sensors (like the ultrasonic) in a comfortable way. It is designed as |
|---|
| 40 | a helping function for developers wanting to access new sensors. For already implemented sensors (e.g. ultrasound, as well |
|---|
| 41 | as HiTechnic's acceleration and infrared sensors), use the provided high-level functions such as <tt>GetUltrasonic</tt>, <tt>GetInfrared</tt>, etc. |
|---|
| 42 | </p> |
|---|
| 43 | <p>For I2C communication, usually the <tt>NXT_SetInputMode</tt> command has to be used with the <tt>LOWSPEED_9V</tt> or <tt>LOWSPEED</tt> setting. Afterwards, commands can be send with <tt>NXT_LSWrite</tt>. Once a sensor is correctly working, i.e. has data available, you can use this function to retrieve them. |
|---|
| 44 | </p> |
|---|
| 45 | <p>In <tt>COM_ReadI2C(Port, RequestLen, DeviceAddress, RegisterAddress)</tt>, <tt>Port</tt> is the sensor-port the sensor is connected to. <tt>RequestLen</tt> specifies the amount of bytes you want to retrieve. For ultasound, this is 1. <tt>DeviceAddress</tt> is the sensor's address on the I2C bus. This sometimes can be changed, but not for the ultrasonic sensor. Default value is |
|---|
| 46 | 0x02 (2 in decimal). Finally, <tt>RegisterAddress</tt> is the address where you want to read data from. For the ultrasound and many other sensors, the "data section" starts at |
|---|
| 47 | 0x42 (66 in decimal). |
|---|
| 48 | </p> |
|---|
| 49 | <p>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 <tt>COM_SetDefaultNXT</tt> will be used. |
|---|
| 50 | </p> |
|---|
| 51 | <p><b>Returns:</b> <tt>ReturnBytes</tt>, byte-array (column vector) of uint8. This array contains the raw sensor-data you requested. How to interpret them depends |
|---|
| 52 | on the sensor. If communication failed (even after automatic retransmission) -- e.g. when the sensor get's disconnected while |
|---|
| 53 | in use -- an empty vector <tt>[]</tt> will be returned. |
|---|
| 54 | </p> |
|---|
| 55 | <p><b>Note:</b></p> |
|---|
| 56 | <p>Please note that the return values of this function are of type uint8. You have to convert them to double (using <tt>double()</tt>) before performing calculations with them, otherwise you might get unexpected results! |
|---|
| 57 | </p> |
|---|
| 58 | <p>The sensor you are addressing with this command has to be correctly opened and initialized of course -- otherwise no valid |
|---|
| 59 | data can be received. |
|---|
| 60 | </p> |
|---|
| 61 | <h2>Example<a name="17"></a></h2> |
|---|
| 62 | <p>This example opens and reads the ultrasonic sensor</p><pre class="codeinput"> port = SENSOR_1; |
|---|
| 63 | handle = COM_OpenNXT(<span class="string">'bluetooth.ini'</span>, <span class="string">'check'</span>); |
|---|
| 64 | |
|---|
| 65 | OpenUltrasonic(port); |
|---|
| 66 | |
|---|
| 67 | <span class="comment">% retrieve 1 byte from device 0x02, register 0x42</span> |
|---|
| 68 | data = COM_ReadI2C(port, 1, uint8(2), uint8(66)); |
|---|
| 69 | |
|---|
| 70 | <span class="keyword">if</span> isempty(data) |
|---|
| 71 | DistanceCM = -1; |
|---|
| 72 | <span class="keyword">else</span> |
|---|
| 73 | <span class="comment">% don'f forget this double()!!!</span> |
|---|
| 74 | DistanceCM = double(data(1)); |
|---|
| 75 | <span class="keyword">end</span><span class="comment">%if</span> |
|---|
| 76 | </pre><h2>See also<a name="20"></a></h2> |
|---|
| 77 | <p><a href="NXT_LSWrite.html">NXT_LSWrite</a>, <a href="NXT_LSRead.html">NXT_LSRead</a>, <a href="NXT_LSGetStatus.html">NXT_LSGetStatus</a>, <a href="NXT_SetInputMode.html">NXT_SetInputMode</a>, <a href="OpenUltrasonic.html">OpenUltrasonic</a>, <a href="GetUltrasonic.html">GetUltrasonic</a>, <a href="SENSOR_1.html">SENSOR_1</a>, <a href="SENSOR_2.html">SENSOR_2</a>, <a href="SENSOR_3.html">SENSOR_3</a>, <a href="SENSOR_4.html">SENSOR_4</a></p> |
|---|
| 78 | <h2>Signature<a name="23"></a></h2> |
|---|
| 79 | <div> |
|---|
| 80 | <ul> |
|---|
| 81 | <li><b>Author:</b> Linus Atorf (see AUTHORS) |
|---|
| 82 | </li> |
|---|
| 83 | <li><b>Date:</b> 2008/09/23 |
|---|
| 84 | </li> |
|---|
| 85 | <li><b>Copyright:</b> 2007-2008, RWTH Aachen University |
|---|
| 86 | </li> |
|---|
| 87 | </ul> |
|---|
| 88 | </div> |
|---|
| 89 | <p class="footer"><br> |
|---|
| 90 | Published with wg_publish; V1.0<br></p> |
|---|
| 91 | </div> |
|---|
| 92 | <!-- |
|---|
| 93 | ##### SOURCE BEGIN ##### |
|---|
| 94 | %% COM_ReadI2C |
|---|
| 95 | % Requests and reads sensor data via I2C from a correctly configured digital sensor. |
|---|
| 96 | %% |
|---|
| 97 | %% Syntax |
|---|
| 98 | % |ReturnBytes = COM_ReadI2C(Port, RequestLen, DeviceAddress, RegisterAddress)| |
|---|
| 99 | %% |
|---|
| 100 | % |ReturnBytes = COM_ReadI2C(Port, RequestLen, DeviceAddress, RegisterAddress, handle)| |
|---|
| 101 | %% |
|---|
| 102 | %% Description |
|---|
| 103 | % This function is used to retrieve data from digital sensors (like the |
|---|
| 104 | % ultrasonic) in a comfortable way. It is designed as a helping function |
|---|
| 105 | % for developers wanting to access new sensors. For already implemented |
|---|
| 106 | % sensors (e.g. ultrasound, as well as HiTechnic's acceleration and |
|---|
| 107 | % infrared sensors), use the provided high-level functions such as |
|---|
| 108 | % |GetUltrasonic|, |GetInfrared|, etc. |
|---|
| 109 | %% |
|---|
| 110 | % For I2C communication, usually the |NXT_SetInputMode| command has to |
|---|
| 111 | % be used with the |LOWSPEED_9V| or |LOWSPEED| setting. Afterwards, |
|---|
| 112 | % commands can be send with |NXT_LSWrite|. Once a sensor is correctly |
|---|
| 113 | % working, i.e. has data available, you can use this function to |
|---|
| 114 | % retrieve them. |
|---|
| 115 | %% |
|---|
| 116 | % In |COM_ReadI2C(Port, RequestLen, DeviceAddress, RegisterAddress)|, |
|---|
| 117 | % |Port| is the sensor-port the sensor is connected to. |RequestLen| |
|---|
| 118 | % specifies the amount of bytes you want to retrieve. For ultasound, |
|---|
| 119 | % this is 1. |DeviceAddress| is the sensor's address on the I2C bus. |
|---|
| 120 | % This sometimes can be changed, but not for the ultrasonic sensor. |
|---|
| 121 | % Default value is 0x02 (2 in decimal). Finally, |RegisterAddress| is |
|---|
| 122 | % the address where you want to read data from. For the ultrasound and |
|---|
| 123 | % many other sensors, the "data section" starts at 0x42 (66 in decimal). |
|---|
| 124 | %% |
|---|
| 125 | % As last argument you can pass a valid NXT-handle to be used by this |
|---|
| 126 | % function. If no handle is passed, the default set by |
|---|
| 127 | % |COM_SetDefaultNXT| will be used. |
|---|
| 128 | %% |
|---|
| 129 | %% |
|---|
| 130 | % *Returns:* |ReturnBytes|, byte-array (column vector) of uint8. |
|---|
| 131 | % This array contains the raw sensor-data you requested. How to |
|---|
| 132 | % interpret them depends on the sensor. If communication failed (even |
|---|
| 133 | % after automatic retransmission) REPLACE_WITH_DASH_DASH e.g. when the sensor get's |
|---|
| 134 | % disconnected while in use REPLACE_WITH_DASH_DASH an empty vector |[]| will be returned. |
|---|
| 135 | %% |
|---|
| 136 | %% |
|---|
| 137 | % *Note:* |
|---|
| 138 | %% |
|---|
| 139 | %% |
|---|
| 140 | % Please note that the return values of this function are of type uint8. |
|---|
| 141 | % You have to convert them to double (using |double()|) before |
|---|
| 142 | % performing calculations with them, otherwise you might get unexpected |
|---|
| 143 | % results! |
|---|
| 144 | %% |
|---|
| 145 | % The sensor you are addressing with this command has to be correctly |
|---|
| 146 | % opened and initialized of course REPLACE_WITH_DASH_DASH otherwise no valid data can be |
|---|
| 147 | % received. |
|---|
| 148 | %% |
|---|
| 149 | %% Example |
|---|
| 150 | % This example opens and reads the ultrasonic sensor |
|---|
| 151 | port = SENSOR_1; |
|---|
| 152 | handle = COM_OpenNXT('bluetooth.ini', 'check'); |
|---|
| 153 | |
|---|
| 154 | OpenUltrasonic(port); |
|---|
| 155 | |
|---|
| 156 | % retrieve 1 byte from device 0x02, register 0x42 |
|---|
| 157 | data = COM_ReadI2C(port, 1, uint8(2), uint8(66)); |
|---|
| 158 | |
|---|
| 159 | if isempty(data) |
|---|
| 160 | DistanceCM = -1; |
|---|
| 161 | else |
|---|
| 162 | % don'f forget this double()!!! |
|---|
| 163 | DistanceCM = double(data(1)); |
|---|
| 164 | end%if |
|---|
| 165 | %% |
|---|
| 166 | %% |
|---|
| 167 | %% See also |
|---|
| 168 | % NXT_LSWrite, NXT_LSRead, NXT_LSGetStatus, NXT_SetInputMode, OpenUltrasonic, |
|---|
| 169 | % GetUltrasonic, SENSOR_1, SENSOR_2, SENSOR_3, SENSOR_4 |
|---|
| 170 | %% |
|---|
| 171 | %% |
|---|
| 172 | %% Signature |
|---|
| 173 | %% |
|---|
| 174 | % * *Author:* Linus Atorf (see AUTHORS) |
|---|
| 175 | % * *Date:* 2008/09/23 |
|---|
| 176 | % * *Copyright:* 2007-2008, RWTH Aachen University |
|---|
| 177 | % |
|---|
| 178 | |
|---|
| 179 | ##### SOURCE END ##### |
|---|
| 180 | --> |
|---|
| 181 | </body> |
|---|
| 182 | </html> |
|---|