![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
my new gear...
sensor/lidar_lite.cpp@3:a9b4b2565a23, 2022-03-27 (annotated)
- Committer:
- yootee
- Date:
- Sun Mar 27 04:51:16 2022 +0000
- Revision:
- 3:a9b4b2565a23
my new gear...
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yootee | 3:a9b4b2565a23 | 1 | #include "lidar_lite.h" |
yootee | 3:a9b4b2565a23 | 2 | |
yootee | 3:a9b4b2565a23 | 3 | LIDARLite::LIDARLite(I2C &i2c):i2c_(i2c){} |
yootee | 3:a9b4b2565a23 | 4 | |
yootee | 3:a9b4b2565a23 | 5 | /*------------------------------------------------------------------------------ |
yootee | 3:a9b4b2565a23 | 6 | Begin |
yootee | 3:a9b4b2565a23 | 7 | Starts the sensor and I2C. |
yootee | 3:a9b4b2565a23 | 8 | Parameters |
yootee | 3:a9b4b2565a23 | 9 | ------------------------------------------------------------------------------ |
yootee | 3:a9b4b2565a23 | 10 | configuration: Default 0. Selects one of several preset configurations. |
yootee | 3:a9b4b2565a23 | 11 | lidarliteAddress: Default 0x62. Fill in new address here if changed. See |
yootee | 3:a9b4b2565a23 | 12 | operating manual for instructions. |
yootee | 3:a9b4b2565a23 | 13 | ------------------------------------------------------------------------------*/ |
yootee | 3:a9b4b2565a23 | 14 | void LIDARLite::begin(int configuration, char lidarliteAddress) |
yootee | 3:a9b4b2565a23 | 15 | { |
yootee | 3:a9b4b2565a23 | 16 | configure(configuration, lidarliteAddress); // Configuration settings |
yootee | 3:a9b4b2565a23 | 17 | } /* LIDARLite::begin */ |
yootee | 3:a9b4b2565a23 | 18 | |
yootee | 3:a9b4b2565a23 | 19 | /*------------------------------------------------------------------------------ |
yootee | 3:a9b4b2565a23 | 20 | Configure |
yootee | 3:a9b4b2565a23 | 21 | Selects one of several preset configurations. |
yootee | 3:a9b4b2565a23 | 22 | Parameters |
yootee | 3:a9b4b2565a23 | 23 | ------------------------------------------------------------------------------ |
yootee | 3:a9b4b2565a23 | 24 | configuration: Default 0. |
yootee | 3:a9b4b2565a23 | 25 | 0: Default mode, balanced performance. |
yootee | 3:a9b4b2565a23 | 26 | 1: Short range, high speed. Uses 0x1d maximum acquisition count. |
yootee | 3:a9b4b2565a23 | 27 | 2: Default range, higher speed short range. Turns on quick termination |
yootee | 3:a9b4b2565a23 | 28 | detection for faster measurements at short range (with decreased |
yootee | 3:a9b4b2565a23 | 29 | accuracy) |
yootee | 3:a9b4b2565a23 | 30 | 3: Maximum range. Uses 0xff maximum acquisition count. |
yootee | 3:a9b4b2565a23 | 31 | 4: High sensitivity detection. Overrides default valid measurement detection |
yootee | 3:a9b4b2565a23 | 32 | algorithm, and uses a threshold value for high sensitivity and noise. |
yootee | 3:a9b4b2565a23 | 33 | 5: Low sensitivity detection. Overrides default valid measurement detection |
yootee | 3:a9b4b2565a23 | 34 | algorithm, and uses a threshold value for low sensitivity and noise. |
yootee | 3:a9b4b2565a23 | 35 | lidarliteAddress: Default 0x62. Fill in new address here if changed. See |
yootee | 3:a9b4b2565a23 | 36 | operating manual for instructions. |
yootee | 3:a9b4b2565a23 | 37 | ------------------------------------------------------------------------------*/ |
yootee | 3:a9b4b2565a23 | 38 | void LIDARLite::configure(int configuration, char lidarliteAddress) |
yootee | 3:a9b4b2565a23 | 39 | { |
yootee | 3:a9b4b2565a23 | 40 | switch (configuration) |
yootee | 3:a9b4b2565a23 | 41 | { |
yootee | 3:a9b4b2565a23 | 42 | case 0: // Default mode, balanced performance |
yootee | 3:a9b4b2565a23 | 43 | write(0x02,0x80,lidarliteAddress); // Default |
yootee | 3:a9b4b2565a23 | 44 | write(0x04,0x08,lidarliteAddress); // Default |
yootee | 3:a9b4b2565a23 | 45 | write(0x1c,0x00,lidarliteAddress); // Default |
yootee | 3:a9b4b2565a23 | 46 | break; |
yootee | 3:a9b4b2565a23 | 47 | |
yootee | 3:a9b4b2565a23 | 48 | case 1: // Short range, high speed |
yootee | 3:a9b4b2565a23 | 49 | write(0x02,0x1d,lidarliteAddress); |
yootee | 3:a9b4b2565a23 | 50 | write(0x04,0x08,lidarliteAddress); // Default |
yootee | 3:a9b4b2565a23 | 51 | write(0x1c,0x00,lidarliteAddress); // Default |
yootee | 3:a9b4b2565a23 | 52 | break; |
yootee | 3:a9b4b2565a23 | 53 | |
yootee | 3:a9b4b2565a23 | 54 | case 2: // Default range, higher speed short range |
yootee | 3:a9b4b2565a23 | 55 | write(0x02,0x80,lidarliteAddress); // Default |
yootee | 3:a9b4b2565a23 | 56 | write(0x04,0x00,lidarliteAddress); |
yootee | 3:a9b4b2565a23 | 57 | write(0x1c,0x00,lidarliteAddress); // Default |
yootee | 3:a9b4b2565a23 | 58 | break; |
yootee | 3:a9b4b2565a23 | 59 | |
yootee | 3:a9b4b2565a23 | 60 | case 3: // Maximum range |
yootee | 3:a9b4b2565a23 | 61 | write(0x02,0xff,lidarliteAddress); |
yootee | 3:a9b4b2565a23 | 62 | write(0x04,0x08,lidarliteAddress); // Default |
yootee | 3:a9b4b2565a23 | 63 | write(0x1c,0x00,lidarliteAddress); // Default |
yootee | 3:a9b4b2565a23 | 64 | break; |
yootee | 3:a9b4b2565a23 | 65 | |
yootee | 3:a9b4b2565a23 | 66 | case 4: // High sensitivity detection, high erroneous measurements |
yootee | 3:a9b4b2565a23 | 67 | write(0x02,0x80,lidarliteAddress); // Default |
yootee | 3:a9b4b2565a23 | 68 | write(0x04,0x08,lidarliteAddress); // Default |
yootee | 3:a9b4b2565a23 | 69 | write(0x1c,0x80,lidarliteAddress); |
yootee | 3:a9b4b2565a23 | 70 | break; |
yootee | 3:a9b4b2565a23 | 71 | |
yootee | 3:a9b4b2565a23 | 72 | case 5: // Low sensitivity detection, low erroneous measurements |
yootee | 3:a9b4b2565a23 | 73 | write(0x02,0x80,lidarliteAddress); // Default |
yootee | 3:a9b4b2565a23 | 74 | write(0x04,0x08,lidarliteAddress); // Default |
yootee | 3:a9b4b2565a23 | 75 | write(0x1c,0xb0,lidarliteAddress); |
yootee | 3:a9b4b2565a23 | 76 | break; |
yootee | 3:a9b4b2565a23 | 77 | } |
yootee | 3:a9b4b2565a23 | 78 | } /* LIDARLite::configure */ |
yootee | 3:a9b4b2565a23 | 79 | |
yootee | 3:a9b4b2565a23 | 80 | /*------------------------------------------------------------------------------ |
yootee | 3:a9b4b2565a23 | 81 | Set I2C Address |
yootee | 3:a9b4b2565a23 | 82 | Set Alternate I2C Device Address. See Operation Manual for additional info. |
yootee | 3:a9b4b2565a23 | 83 | Parameters |
yootee | 3:a9b4b2565a23 | 84 | ------------------------------------------------------------------------------ |
yootee | 3:a9b4b2565a23 | 85 | newAddress: desired secondary I2C device address |
yootee | 3:a9b4b2565a23 | 86 | disableDefault: a non-zero value here means the default 0x62 I2C device |
yootee | 3:a9b4b2565a23 | 87 | address will be disabled. |
yootee | 3:a9b4b2565a23 | 88 | lidarliteAddress: Default 0x62. Fill in new address here if changed. See |
yootee | 3:a9b4b2565a23 | 89 | operating manual for instructions. |
yootee | 3:a9b4b2565a23 | 90 | ------------------------------------------------------------------------------*/ |
yootee | 3:a9b4b2565a23 | 91 | void LIDARLite::setI2Caddr(char newAddress, bool disableDefault, char lidarliteAddress) |
yootee | 3:a9b4b2565a23 | 92 | { |
yootee | 3:a9b4b2565a23 | 93 | char dataBytes[2]; |
yootee | 3:a9b4b2565a23 | 94 | |
yootee | 3:a9b4b2565a23 | 95 | // Read UNIT_ID serial number bytes and write them into I2C_ID byte locations |
yootee | 3:a9b4b2565a23 | 96 | read ((0x16 | 0x80), 2, dataBytes, false, lidarliteAddress); |
yootee | 3:a9b4b2565a23 | 97 | write(0x18, dataBytes[0], lidarliteAddress); |
yootee | 3:a9b4b2565a23 | 98 | write(0x19, dataBytes[1], lidarliteAddress); |
yootee | 3:a9b4b2565a23 | 99 | |
yootee | 3:a9b4b2565a23 | 100 | // Write the new I2C device address to registers |
yootee | 3:a9b4b2565a23 | 101 | dataBytes[0] = newAddress; |
yootee | 3:a9b4b2565a23 | 102 | write(0x1a, dataBytes[0], lidarliteAddress); |
yootee | 3:a9b4b2565a23 | 103 | |
yootee | 3:a9b4b2565a23 | 104 | // Enable the new I2C device address using the default I2C device address |
yootee | 3:a9b4b2565a23 | 105 | dataBytes[0] = 0; |
yootee | 3:a9b4b2565a23 | 106 | write(0x1e, dataBytes[0], lidarliteAddress); |
yootee | 3:a9b4b2565a23 | 107 | |
yootee | 3:a9b4b2565a23 | 108 | // If desired, disable default I2C device address (using the new I2C device address) |
yootee | 3:a9b4b2565a23 | 109 | if (disableDefault) |
yootee | 3:a9b4b2565a23 | 110 | { |
yootee | 3:a9b4b2565a23 | 111 | dataBytes[0] = (1 << 3); // set bit to disable default address |
yootee | 3:a9b4b2565a23 | 112 | write(0x1e, dataBytes[0], newAddress); |
yootee | 3:a9b4b2565a23 | 113 | } |
yootee | 3:a9b4b2565a23 | 114 | } /* LIDARLite::setI2Caddr */ |
yootee | 3:a9b4b2565a23 | 115 | |
yootee | 3:a9b4b2565a23 | 116 | /*------------------------------------------------------------------------------ |
yootee | 3:a9b4b2565a23 | 117 | Reset |
yootee | 3:a9b4b2565a23 | 118 | Reset device. The device reloads default register settings, including the |
yootee | 3:a9b4b2565a23 | 119 | default I2C address. Re-initialization takes approximately 22ms. |
yootee | 3:a9b4b2565a23 | 120 | Parameters |
yootee | 3:a9b4b2565a23 | 121 | ------------------------------------------------------------------------------ |
yootee | 3:a9b4b2565a23 | 122 | lidarliteAddress: Default 0x62. Fill in new address here if changed. See |
yootee | 3:a9b4b2565a23 | 123 | operating manual for instructions. |
yootee | 3:a9b4b2565a23 | 124 | ------------------------------------------------------------------------------*/ |
yootee | 3:a9b4b2565a23 | 125 | void LIDARLite::reset( char lidarliteAddress) |
yootee | 3:a9b4b2565a23 | 126 | { |
yootee | 3:a9b4b2565a23 | 127 | write(0x00,0x00,lidarliteAddress); |
yootee | 3:a9b4b2565a23 | 128 | } /* LIDARLite::reset */ |
yootee | 3:a9b4b2565a23 | 129 | |
yootee | 3:a9b4b2565a23 | 130 | /*------------------------------------------------------------------------------ |
yootee | 3:a9b4b2565a23 | 131 | Distance |
yootee | 3:a9b4b2565a23 | 132 | Take a distance measurement and read the result. |
yootee | 3:a9b4b2565a23 | 133 | Process |
yootee | 3:a9b4b2565a23 | 134 | ------------------------------------------------------------------------------ |
yootee | 3:a9b4b2565a23 | 135 | 1. Write 0x04 or 0x03 to register 0x00 to initiate an aquisition. |
yootee | 3:a9b4b2565a23 | 136 | 2. Read register 0x01 (this is handled in the read() command) |
yootee | 3:a9b4b2565a23 | 137 | - if the first bit is "1" then the sensor is busy, loop until the first |
yootee | 3:a9b4b2565a23 | 138 | bit is "0" |
yootee | 3:a9b4b2565a23 | 139 | - if the first bit is "0" then the sensor is ready |
yootee | 3:a9b4b2565a23 | 140 | 3. Read two bytes from register 0x8f and save |
yootee | 3:a9b4b2565a23 | 141 | 4. Shift the first value from 0x8f << 8 and add to second value from 0x8f. |
yootee | 3:a9b4b2565a23 | 142 | The result is the measured distance in centimeters. |
yootee | 3:a9b4b2565a23 | 143 | Parameters |
yootee | 3:a9b4b2565a23 | 144 | ------------------------------------------------------------------------------ |
yootee | 3:a9b4b2565a23 | 145 | biasCorrection: Default true. Take aquisition with receiver bias |
yootee | 3:a9b4b2565a23 | 146 | correction. If set to false measurements will be faster. Receiver bias |
yootee | 3:a9b4b2565a23 | 147 | correction must be performed periodically. (e.g. 1 out of every 100 |
yootee | 3:a9b4b2565a23 | 148 | readings). |
yootee | 3:a9b4b2565a23 | 149 | lidarliteAddress: Default 0x62. Fill in new address here if changed. See |
yootee | 3:a9b4b2565a23 | 150 | operating manual for instructions. |
yootee | 3:a9b4b2565a23 | 151 | ------------------------------------------------------------------------------*/ |
yootee | 3:a9b4b2565a23 | 152 | int LIDARLite::distance(bool biasCorrection, char lidarliteAddress) |
yootee | 3:a9b4b2565a23 | 153 | { |
yootee | 3:a9b4b2565a23 | 154 | if(biasCorrection) |
yootee | 3:a9b4b2565a23 | 155 | { |
yootee | 3:a9b4b2565a23 | 156 | // Take acquisition & correlation processing with receiver bias correction |
yootee | 3:a9b4b2565a23 | 157 | write(0x00,0x04,lidarliteAddress); |
yootee | 3:a9b4b2565a23 | 158 | } |
yootee | 3:a9b4b2565a23 | 159 | else |
yootee | 3:a9b4b2565a23 | 160 | { |
yootee | 3:a9b4b2565a23 | 161 | // Take acquisition & correlation processing without receiver bias correction |
yootee | 3:a9b4b2565a23 | 162 | write(0x00,0x03,lidarliteAddress); |
yootee | 3:a9b4b2565a23 | 163 | } |
yootee | 3:a9b4b2565a23 | 164 | // Array to store high and low bytes of distance |
yootee | 3:a9b4b2565a23 | 165 | char distanceArray[2]; |
yootee | 3:a9b4b2565a23 | 166 | // Read two bytes from register 0x8f (autoincrement for reading 0x0f and 0x10) |
yootee | 3:a9b4b2565a23 | 167 | read(0x8f,2,distanceArray,true,lidarliteAddress);//0x8f |
yootee | 3:a9b4b2565a23 | 168 | // Shift high byte and add to low byte |
yootee | 3:a9b4b2565a23 | 169 | int distance = (distanceArray[0] << 8) + distanceArray[1]; |
yootee | 3:a9b4b2565a23 | 170 | return(distance); |
yootee | 3:a9b4b2565a23 | 171 | } /* LIDARLite::distance */ |
yootee | 3:a9b4b2565a23 | 172 | |
yootee | 3:a9b4b2565a23 | 173 | /*------------------------------------------------------------------------------ |
yootee | 3:a9b4b2565a23 | 174 | Write |
yootee | 3:a9b4b2565a23 | 175 | Perform I2C write to device. |
yootee | 3:a9b4b2565a23 | 176 | Parameters |
yootee | 3:a9b4b2565a23 | 177 | ------------------------------------------------------------------------------ |
yootee | 3:a9b4b2565a23 | 178 | myAddress: register address to write to. |
yootee | 3:a9b4b2565a23 | 179 | myValue: value to write. |
yootee | 3:a9b4b2565a23 | 180 | lidarliteAddress: Default 0x62. Fill in new address here if changed. See |
yootee | 3:a9b4b2565a23 | 181 | operating manual for instructions. |
yootee | 3:a9b4b2565a23 | 182 | ------------------------------------------------------------------------------*/ |
yootee | 3:a9b4b2565a23 | 183 | void LIDARLite::write(char myAddress, char myValue, char lidarliteAddress) |
yootee | 3:a9b4b2565a23 | 184 | { |
yootee | 3:a9b4b2565a23 | 185 | char data[2] = {myAddress,myValue};//register value |
yootee | 3:a9b4b2565a23 | 186 | int nackCatcher = i2c_.write((int)lidarliteAddress,data,2); |
yootee | 3:a9b4b2565a23 | 187 | // A nack means the device is not responding, report the error over serial |
yootee | 3:a9b4b2565a23 | 188 | /* |
yootee | 3:a9b4b2565a23 | 189 | if(nackCatcher != 0) |
yootee | 3:a9b4b2565a23 | 190 | { |
yootee | 3:a9b4b2565a23 | 191 | //printf("> nack"); |
yootee | 3:a9b4b2565a23 | 192 | }*/ |
yootee | 3:a9b4b2565a23 | 193 | |
yootee | 3:a9b4b2565a23 | 194 | //wait_ms(1); // 1 ms delay for robustness with successive reads and writes |
yootee | 3:a9b4b2565a23 | 195 | } /* LIDARLite::write */ |
yootee | 3:a9b4b2565a23 | 196 | |
yootee | 3:a9b4b2565a23 | 197 | /*------------------------------------------------------------------------------ |
yootee | 3:a9b4b2565a23 | 198 | Read |
yootee | 3:a9b4b2565a23 | 199 | Perform I2C read from device. Will detect an unresponsive device and report |
yootee | 3:a9b4b2565a23 | 200 | the error over serial. The optional busy flag monitoring |
yootee | 3:a9b4b2565a23 | 201 | can be used to read registers that are updated at the end of a distance |
yootee | 3:a9b4b2565a23 | 202 | measurement to obtain the new data. |
yootee | 3:a9b4b2565a23 | 203 | Parameters |
yootee | 3:a9b4b2565a23 | 204 | ------------------------------------------------------------------------------ |
yootee | 3:a9b4b2565a23 | 205 | myAddress: register address to read from. |
yootee | 3:a9b4b2565a23 | 206 | numOfBytes: numbers of bytes to read. Can be 1 or 2. |
yootee | 3:a9b4b2565a23 | 207 | arrayToSave: an array to store the read values. |
yootee | 3:a9b4b2565a23 | 208 | monitorBusyFlag: if true, the routine will repeatedly read the status |
yootee | 3:a9b4b2565a23 | 209 | register until the busy flag (LSB) is 0. |
yootee | 3:a9b4b2565a23 | 210 | ------------------------------------------------------------------------------*/ |
yootee | 3:a9b4b2565a23 | 211 | void LIDARLite::read(char myAddress, int numOfBytes, char arrayToSave[2], bool monitorBusyFlag, char lidarliteAddress) |
yootee | 3:a9b4b2565a23 | 212 | { |
yootee | 3:a9b4b2565a23 | 213 | char data; |
yootee | 3:a9b4b2565a23 | 214 | int busyFlag = 0; // busyFlag monitors when the device is done with a measurement |
yootee | 3:a9b4b2565a23 | 215 | bool errer_flag =false; |
yootee | 3:a9b4b2565a23 | 216 | if(monitorBusyFlag) |
yootee | 3:a9b4b2565a23 | 217 | { |
yootee | 3:a9b4b2565a23 | 218 | busyFlag = 1; // Begin read immediately if not monitoring busy flag |
yootee | 3:a9b4b2565a23 | 219 | } |
yootee | 3:a9b4b2565a23 | 220 | int busyCounter = 0; // busyCounter counts number of times busy flag is checked, for timeout |
yootee | 3:a9b4b2565a23 | 221 | |
yootee | 3:a9b4b2565a23 | 222 | while(busyFlag != 0) // Loop until device is not busy |
yootee | 3:a9b4b2565a23 | 223 | { |
yootee | 3:a9b4b2565a23 | 224 | // Read status register to check busy flag |
yootee | 3:a9b4b2565a23 | 225 | data = 0x01;//Set the status register to be read |
yootee | 3:a9b4b2565a23 | 226 | int nackCatcher = i2c_.write(lidarliteAddress,&data,1,true); |
yootee | 3:a9b4b2565a23 | 227 | // A nack means the device is not responding, report the error over serial |
yootee | 3:a9b4b2565a23 | 228 | if(nackCatcher != 0) |
yootee | 3:a9b4b2565a23 | 229 | { |
yootee | 3:a9b4b2565a23 | 230 | //printf("> nack"); |
yootee | 3:a9b4b2565a23 | 231 | } |
yootee | 3:a9b4b2565a23 | 232 | |
yootee | 3:a9b4b2565a23 | 233 | i2c_.read(lidarliteAddress,&data,1); // Read register 0x01 |
yootee | 3:a9b4b2565a23 | 234 | busyFlag = data & 0x1; // Assign the LSB of the status register to busyFlag |
yootee | 3:a9b4b2565a23 | 235 | |
yootee | 3:a9b4b2565a23 | 236 | busyCounter++; // Increment busyCounter for timeout |
yootee | 3:a9b4b2565a23 | 237 | |
yootee | 3:a9b4b2565a23 | 238 | // Handle timeout condition, exit while loop and goto bailout |
yootee | 3:a9b4b2565a23 | 239 | if(busyCounter > 9999) |
yootee | 3:a9b4b2565a23 | 240 | { |
yootee | 3:a9b4b2565a23 | 241 | errer_flag = true; |
yootee | 3:a9b4b2565a23 | 242 | break; |
yootee | 3:a9b4b2565a23 | 243 | } |
yootee | 3:a9b4b2565a23 | 244 | } |
yootee | 3:a9b4b2565a23 | 245 | |
yootee | 3:a9b4b2565a23 | 246 | // Device is not busy, begin read |
yootee | 3:a9b4b2565a23 | 247 | if(busyFlag == 0 && !errer_flag) |
yootee | 3:a9b4b2565a23 | 248 | { |
yootee | 3:a9b4b2565a23 | 249 | data = myAddress;// Set the register to be read |
yootee | 3:a9b4b2565a23 | 250 | int nackCatcher = i2c_.write(lidarliteAddress,&data,1,true); |
yootee | 3:a9b4b2565a23 | 251 | // A nack means the device is not responding, report the error over serial |
yootee | 3:a9b4b2565a23 | 252 | if(nackCatcher != 0) |
yootee | 3:a9b4b2565a23 | 253 | { |
yootee | 3:a9b4b2565a23 | 254 | //printf("> nack"); |
yootee | 3:a9b4b2565a23 | 255 | } |
yootee | 3:a9b4b2565a23 | 256 | |
yootee | 3:a9b4b2565a23 | 257 | // Perform read of 1 or 2 bytes, save in arrayToSave |
yootee | 3:a9b4b2565a23 | 258 | i2c_.read(lidarliteAddress, arrayToSave, numOfBytes); |
yootee | 3:a9b4b2565a23 | 259 | } |
yootee | 3:a9b4b2565a23 | 260 | |
yootee | 3:a9b4b2565a23 | 261 | // bailout reports error over serial |
yootee | 3:a9b4b2565a23 | 262 | if(busyCounter > 9999 || errer_flag) |
yootee | 3:a9b4b2565a23 | 263 | { |
yootee | 3:a9b4b2565a23 | 264 | busyCounter = 0; |
yootee | 3:a9b4b2565a23 | 265 | //printf("> read failed"); |
yootee | 3:a9b4b2565a23 | 266 | } |
yootee | 3:a9b4b2565a23 | 267 | } /* LIDARLite::read */ |
yootee | 3:a9b4b2565a23 | 268 | |
yootee | 3:a9b4b2565a23 | 269 | /*------------------------------------------------------------------------------ |
yootee | 3:a9b4b2565a23 | 270 | Correlation Record To Serial |
yootee | 3:a9b4b2565a23 | 271 | The correlation record used to calculate distance can be read from the device. |
yootee | 3:a9b4b2565a23 | 272 | It has a bipolar wave shape, transitioning from a positive going portion to a |
yootee | 3:a9b4b2565a23 | 273 | roughly symmetrical negative going pulse. The point where the signal crosses |
yootee | 3:a9b4b2565a23 | 274 | zero represents the effective delay for the reference and return signals. |
yootee | 3:a9b4b2565a23 | 275 | Process |
yootee | 3:a9b4b2565a23 | 276 | ------------------------------------------------------------------------------ |
yootee | 3:a9b4b2565a23 | 277 | 1. Take a distance reading (there is no correlation record without at least |
yootee | 3:a9b4b2565a23 | 278 | one distance reading being taken) |
yootee | 3:a9b4b2565a23 | 279 | 2. Select memory bank by writing 0xc0 to register 0x5d |
yootee | 3:a9b4b2565a23 | 280 | 3. Set test mode select by writing 0x07 to register 0x40 |
yootee | 3:a9b4b2565a23 | 281 | 4. For as many readings as you want to take (max is 1024) |
yootee | 3:a9b4b2565a23 | 282 | 1. Read two bytes from 0xd2 |
yootee | 3:a9b4b2565a23 | 283 | 2. The Low byte is the value from the record |
yootee | 3:a9b4b2565a23 | 284 | 3. The high byte is the sign from the record |
yootee | 3:a9b4b2565a23 | 285 | Parameters |
yootee | 3:a9b4b2565a23 | 286 | ------------------------------------------------------------------------------ |
yootee | 3:a9b4b2565a23 | 287 | separator: the separator between serial data words |
yootee | 3:a9b4b2565a23 | 288 | numberOfReadings: Default: 256. Maximum of 1024 |
yootee | 3:a9b4b2565a23 | 289 | lidarliteAddress: Default 0x62. Fill in new address here if changed. See |
yootee | 3:a9b4b2565a23 | 290 | operating manual for instructions. |
yootee | 3:a9b4b2565a23 | 291 | ------------------------------------------------------------------------------*/ |
yootee | 3:a9b4b2565a23 | 292 | void LIDARLite::correlationRecordToSerial(char separator, int numberOfReadings, char lidarliteAddress) |
yootee | 3:a9b4b2565a23 | 293 | { |
yootee | 3:a9b4b2565a23 | 294 | |
yootee | 3:a9b4b2565a23 | 295 | // Array to store read values |
yootee | 3:a9b4b2565a23 | 296 | char correlationArray[2]; |
yootee | 3:a9b4b2565a23 | 297 | // Var to store value of correlation record |
yootee | 3:a9b4b2565a23 | 298 | int correlationValue = 0; |
yootee | 3:a9b4b2565a23 | 299 | // Selects memory bank |
yootee | 3:a9b4b2565a23 | 300 | write(0x5d,0xc0,lidarliteAddress); |
yootee | 3:a9b4b2565a23 | 301 | // Test mode enable |
yootee | 3:a9b4b2565a23 | 302 | write(0x40, 0x07,lidarliteAddress); |
yootee | 3:a9b4b2565a23 | 303 | for(int i = 0; i<numberOfReadings; i++){ |
yootee | 3:a9b4b2565a23 | 304 | // Select single byte |
yootee | 3:a9b4b2565a23 | 305 | read(0xd2,2,correlationArray,false,lidarliteAddress); |
yootee | 3:a9b4b2565a23 | 306 | // Low byte is the value of the correlation record |
yootee | 3:a9b4b2565a23 | 307 | correlationValue = correlationArray[0]; |
yootee | 3:a9b4b2565a23 | 308 | // if upper byte lsb is set, the value is negative |
yootee | 3:a9b4b2565a23 | 309 | if((int)correlationArray[1] == 1){ |
yootee | 3:a9b4b2565a23 | 310 | correlationValue |= 0xff00; |
yootee | 3:a9b4b2565a23 | 311 | } |
yootee | 3:a9b4b2565a23 | 312 | //printf((int)correlationValue); |
yootee | 3:a9b4b2565a23 | 313 | //printf(separator); |
yootee | 3:a9b4b2565a23 | 314 | } |
yootee | 3:a9b4b2565a23 | 315 | // test mode disable |
yootee | 3:a9b4b2565a23 | 316 | write(0x40,0x00,lidarliteAddress); |
yootee | 3:a9b4b2565a23 | 317 | } /* LIDARLite::correlationRecordToSerial */ |