Ported from sparkfun library
Dependents: tc_agent Line_Trace_No_collision_Light_control_longer_dis
VL6180X.cpp@0:8aca38272614, 2016-07-19 (annotated)
- Committer:
- maruta
- Date:
- Tue Jul 19 03:49:54 2016 +0000
- Revision:
- 0:8aca38272614
Ported to mbed;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maruta | 0:8aca38272614 | 1 | /****************************************************************************** |
maruta | 0:8aca38272614 | 2 | * SparkFun_VL6180x.cpp |
maruta | 0:8aca38272614 | 3 | * Library for VL6180x time of flight range finder. |
maruta | 0:8aca38272614 | 4 | * Casey Kuhns @ SparkFun Electronics |
maruta | 0:8aca38272614 | 5 | * 10/29/2014 |
maruta | 0:8aca38272614 | 6 | * https://github.com/sparkfun/SparkFun_ToF_Range_Finder-VL6180_Arduino_Library |
maruta | 0:8aca38272614 | 7 | * |
maruta | 0:8aca38272614 | 8 | * The VL6180x by ST micro is a time of flight range finder that |
maruta | 0:8aca38272614 | 9 | * uses pulsed IR light to determine distances from object at close |
maruta | 0:8aca38272614 | 10 | * range. The average range of a sensor is between 0-200mm |
maruta | 0:8aca38272614 | 11 | * |
maruta | 0:8aca38272614 | 12 | * In this file are the functions in the VL6180x class |
maruta | 0:8aca38272614 | 13 | * |
maruta | 0:8aca38272614 | 14 | * Resources: |
maruta | 0:8aca38272614 | 15 | * This library uses the Arduino Wire.h to complete I2C transactions. |
maruta | 0:8aca38272614 | 16 | * |
maruta | 0:8aca38272614 | 17 | * Development environment specifics: |
maruta | 0:8aca38272614 | 18 | * IDE: Arduino 1.0.5 |
maruta | 0:8aca38272614 | 19 | * Hardware Platform: Arduino Pro 3.3V/8MHz |
maruta | 0:8aca38272614 | 20 | * VL6180x Breakout Version: 1.0 |
maruta | 0:8aca38272614 | 21 | * **Updated for Arduino 1.6.4 5/2015** |
maruta | 0:8aca38272614 | 22 | * |
maruta | 0:8aca38272614 | 23 | * This code is beerware. If you see me (or any other SparkFun employee) at the |
maruta | 0:8aca38272614 | 24 | * local pub, and you've found our code helpful, please buy us a round! |
maruta | 0:8aca38272614 | 25 | * |
maruta | 0:8aca38272614 | 26 | * Distributed as-is; no warranty is given. |
maruta | 0:8aca38272614 | 27 | ******************************************************************************/ |
maruta | 0:8aca38272614 | 28 | |
maruta | 0:8aca38272614 | 29 | #include "VL6180X.h" |
maruta | 0:8aca38272614 | 30 | #include "mbed.h" |
maruta | 0:8aca38272614 | 31 | |
maruta | 0:8aca38272614 | 32 | VL6180x::VL6180x(PinName sda, PinName scl) : _i2c(sda, scl) { |
maruta | 0:8aca38272614 | 33 | } |
maruta | 0:8aca38272614 | 34 | |
maruta | 0:8aca38272614 | 35 | |
maruta | 0:8aca38272614 | 36 | uint8_t VL6180x::VL6180xInit(void){ |
maruta | 0:8aca38272614 | 37 | uint8_t data; //for temp data storage |
maruta | 0:8aca38272614 | 38 | |
maruta | 0:8aca38272614 | 39 | data = VL6180x_getRegister(VL6180X_SYSTEM_FRESH_OUT_OF_RESET); |
maruta | 0:8aca38272614 | 40 | |
maruta | 0:8aca38272614 | 41 | if(data != 1) return VL6180x_FAILURE_RESET; |
maruta | 0:8aca38272614 | 42 | |
maruta | 0:8aca38272614 | 43 | //Required by datasheet |
maruta | 0:8aca38272614 | 44 | //http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf |
maruta | 0:8aca38272614 | 45 | VL6180x_setRegister(0x0207, 0x01); |
maruta | 0:8aca38272614 | 46 | VL6180x_setRegister(0x0208, 0x01); |
maruta | 0:8aca38272614 | 47 | VL6180x_setRegister(0x0096, 0x00); |
maruta | 0:8aca38272614 | 48 | VL6180x_setRegister(0x0097, 0xfd); |
maruta | 0:8aca38272614 | 49 | VL6180x_setRegister(0x00e3, 0x00); |
maruta | 0:8aca38272614 | 50 | VL6180x_setRegister(0x00e4, 0x04); |
maruta | 0:8aca38272614 | 51 | VL6180x_setRegister(0x00e5, 0x02); |
maruta | 0:8aca38272614 | 52 | VL6180x_setRegister(0x00e6, 0x01); |
maruta | 0:8aca38272614 | 53 | VL6180x_setRegister(0x00e7, 0x03); |
maruta | 0:8aca38272614 | 54 | VL6180x_setRegister(0x00f5, 0x02); |
maruta | 0:8aca38272614 | 55 | VL6180x_setRegister(0x00d9, 0x05); |
maruta | 0:8aca38272614 | 56 | VL6180x_setRegister(0x00db, 0xce); |
maruta | 0:8aca38272614 | 57 | VL6180x_setRegister(0x00dc, 0x03); |
maruta | 0:8aca38272614 | 58 | VL6180x_setRegister(0x00dd, 0xf8); |
maruta | 0:8aca38272614 | 59 | VL6180x_setRegister(0x009f, 0x00); |
maruta | 0:8aca38272614 | 60 | VL6180x_setRegister(0x00a3, 0x3c); |
maruta | 0:8aca38272614 | 61 | VL6180x_setRegister(0x00b7, 0x00); |
maruta | 0:8aca38272614 | 62 | VL6180x_setRegister(0x00bb, 0x3c); |
maruta | 0:8aca38272614 | 63 | VL6180x_setRegister(0x00b2, 0x09); |
maruta | 0:8aca38272614 | 64 | VL6180x_setRegister(0x00ca, 0x09); |
maruta | 0:8aca38272614 | 65 | VL6180x_setRegister(0x0198, 0x01); |
maruta | 0:8aca38272614 | 66 | VL6180x_setRegister(0x01b0, 0x17); |
maruta | 0:8aca38272614 | 67 | VL6180x_setRegister(0x01ad, 0x00); |
maruta | 0:8aca38272614 | 68 | VL6180x_setRegister(0x00ff, 0x05); |
maruta | 0:8aca38272614 | 69 | VL6180x_setRegister(0x0100, 0x05); |
maruta | 0:8aca38272614 | 70 | VL6180x_setRegister(0x0199, 0x05); |
maruta | 0:8aca38272614 | 71 | VL6180x_setRegister(0x01a6, 0x1b); |
maruta | 0:8aca38272614 | 72 | VL6180x_setRegister(0x01ac, 0x3e); |
maruta | 0:8aca38272614 | 73 | VL6180x_setRegister(0x01a7, 0x1f); |
maruta | 0:8aca38272614 | 74 | VL6180x_setRegister(0x0030, 0x00); |
maruta | 0:8aca38272614 | 75 | |
maruta | 0:8aca38272614 | 76 | return 0; |
maruta | 0:8aca38272614 | 77 | } |
maruta | 0:8aca38272614 | 78 | |
maruta | 0:8aca38272614 | 79 | void VL6180x::VL6180xDefautSettings(void){ |
maruta | 0:8aca38272614 | 80 | //Recommended settings from datasheet |
maruta | 0:8aca38272614 | 81 | //http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf |
maruta | 0:8aca38272614 | 82 | |
maruta | 0:8aca38272614 | 83 | //Enable Interrupts on Conversion Complete (any source) |
maruta | 0:8aca38272614 | 84 | VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, (4 << 3)|(4) ); // Set GPIO1 high when sample complete |
maruta | 0:8aca38272614 | 85 | |
maruta | 0:8aca38272614 | 86 | |
maruta | 0:8aca38272614 | 87 | VL6180x_setRegister(VL6180X_SYSTEM_MODE_GPIO1, 0x10); // Set GPIO1 high when sample complete |
maruta | 0:8aca38272614 | 88 | VL6180x_setRegister(VL6180X_READOUT_AVERAGING_SAMPLE_PERIOD, 0x30); //Set Avg sample period |
maruta | 0:8aca38272614 | 89 | VL6180x_setRegister(VL6180X_SYSALS_ANALOGUE_GAIN, 0x46); // Set the ALS gain |
maruta | 0:8aca38272614 | 90 | VL6180x_setRegister(VL6180X_SYSRANGE_VHV_REPEAT_RATE, 0xFF); // Set auto calibration period (Max = 255)/(OFF = 0) |
maruta | 0:8aca38272614 | 91 | VL6180x_setRegister(VL6180X_SYSALS_INTEGRATION_PERIOD, 0x63); // Set ALS integration time to 100ms |
maruta | 0:8aca38272614 | 92 | VL6180x_setRegister(VL6180X_SYSRANGE_VHV_RECALIBRATE, 0x01); // perform a single temperature calibration |
maruta | 0:8aca38272614 | 93 | //Optional settings from datasheet |
maruta | 0:8aca38272614 | 94 | //http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00122600.pdf |
maruta | 0:8aca38272614 | 95 | VL6180x_setRegister(VL6180X_SYSRANGE_INTERMEASUREMENT_PERIOD, 0x09); // Set default ranging inter-measurement period to 100ms |
maruta | 0:8aca38272614 | 96 | VL6180x_setRegister(VL6180X_SYSALS_INTERMEASUREMENT_PERIOD, 0x0A); // Set default ALS inter-measurement period to 100ms |
maruta | 0:8aca38272614 | 97 | VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CONFIG_GPIO, 0x24); // Configures interrupt on ‘New Sample Ready threshold event’ |
maruta | 0:8aca38272614 | 98 | //Additional settings defaults from community |
maruta | 0:8aca38272614 | 99 | VL6180x_setRegister(VL6180X_SYSRANGE_MAX_CONVERGENCE_TIME, 0x32); |
maruta | 0:8aca38272614 | 100 | VL6180x_setRegister(VL6180X_SYSRANGE_RANGE_CHECK_ENABLES, 0x10 | 0x01); |
maruta | 0:8aca38272614 | 101 | VL6180x_setRegister16bit(VL6180X_SYSRANGE_EARLY_CONVERGENCE_ESTIMATE, 0x7B ); |
maruta | 0:8aca38272614 | 102 | VL6180x_setRegister16bit(VL6180X_SYSALS_INTEGRATION_PERIOD, 0x64); |
maruta | 0:8aca38272614 | 103 | |
maruta | 0:8aca38272614 | 104 | VL6180x_setRegister(VL6180X_READOUT_AVERAGING_SAMPLE_PERIOD,0x30); |
maruta | 0:8aca38272614 | 105 | VL6180x_setRegister(VL6180X_SYSALS_ANALOGUE_GAIN,0x40); |
maruta | 0:8aca38272614 | 106 | VL6180x_setRegister(VL6180X_FIRMWARE_RESULT_SCALER,0x01); |
maruta | 0:8aca38272614 | 107 | } |
maruta | 0:8aca38272614 | 108 | void VL6180x::getIdentification(struct VL6180xIdentification *temp){ |
maruta | 0:8aca38272614 | 109 | |
maruta | 0:8aca38272614 | 110 | temp->idModel = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_ID); |
maruta | 0:8aca38272614 | 111 | temp->idModelRevMajor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_REV_MAJOR); |
maruta | 0:8aca38272614 | 112 | temp->idModelRevMinor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODEL_REV_MINOR); |
maruta | 0:8aca38272614 | 113 | temp->idModuleRevMajor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODULE_REV_MAJOR); |
maruta | 0:8aca38272614 | 114 | temp->idModuleRevMinor = VL6180x_getRegister(VL6180X_IDENTIFICATION_MODULE_REV_MINOR); |
maruta | 0:8aca38272614 | 115 | |
maruta | 0:8aca38272614 | 116 | temp->idDate = VL6180x_getRegister16bit(VL6180X_IDENTIFICATION_DATE); |
maruta | 0:8aca38272614 | 117 | temp->idTime = VL6180x_getRegister16bit(VL6180X_IDENTIFICATION_TIME); |
maruta | 0:8aca38272614 | 118 | } |
maruta | 0:8aca38272614 | 119 | |
maruta | 0:8aca38272614 | 120 | |
maruta | 0:8aca38272614 | 121 | uint8_t VL6180x::changeAddress(uint8_t old_address, uint8_t new_address){ |
maruta | 0:8aca38272614 | 122 | |
maruta | 0:8aca38272614 | 123 | //NOTICE: IT APPEARS THAT CHANGING THE ADDRESS IS NOT STORED IN NON-VOLATILE MEMORY |
maruta | 0:8aca38272614 | 124 | // POWER CYCLING THE DEVICE REVERTS ADDRESS BACK TO 0X29 |
maruta | 0:8aca38272614 | 125 | |
maruta | 0:8aca38272614 | 126 | if( old_address == new_address) return old_address; |
maruta | 0:8aca38272614 | 127 | if( new_address > 127) return old_address; |
maruta | 0:8aca38272614 | 128 | |
maruta | 0:8aca38272614 | 129 | VL6180x_setRegister(VL6180X_I2C_SLAVE_DEVICE_ADDRESS, new_address); |
maruta | 0:8aca38272614 | 130 | |
maruta | 0:8aca38272614 | 131 | return VL6180x_getRegister(VL6180X_I2C_SLAVE_DEVICE_ADDRESS); |
maruta | 0:8aca38272614 | 132 | } |
maruta | 0:8aca38272614 | 133 | |
maruta | 0:8aca38272614 | 134 | |
maruta | 0:8aca38272614 | 135 | |
maruta | 0:8aca38272614 | 136 | void VL6180x::triggerDistance() |
maruta | 0:8aca38272614 | 137 | { |
maruta | 0:8aca38272614 | 138 | VL6180x_setRegister(VL6180X_SYSRANGE_START, 0x01); //Start Single shot mode |
maruta | 0:8aca38272614 | 139 | } |
maruta | 0:8aca38272614 | 140 | |
maruta | 0:8aca38272614 | 141 | uint8_t VL6180x::pollDistance() |
maruta | 0:8aca38272614 | 142 | { |
maruta | 0:8aca38272614 | 143 | uint8_t status; |
maruta | 0:8aca38272614 | 144 | do{ |
maruta | 0:8aca38272614 | 145 | status = VL6180x_getRegister(VL6180X_RESULT_INTERRUPT_STATUS_GPIO); |
maruta | 0:8aca38272614 | 146 | }while((status & 0x7) != 4); |
maruta | 0:8aca38272614 | 147 | return VL6180x_getRegister(VL6180X_RESULT_RANGE_VAL); |
maruta | 0:8aca38272614 | 148 | } |
maruta | 0:8aca38272614 | 149 | |
maruta | 0:8aca38272614 | 150 | uint8_t VL6180x::getDistance() |
maruta | 0:8aca38272614 | 151 | { |
maruta | 0:8aca38272614 | 152 | VL6180x_setRegister(VL6180X_SYSRANGE_START, 0x01); //Start Single shot mode |
maruta | 0:8aca38272614 | 153 | wait_ms(10); |
maruta | 0:8aca38272614 | 154 | return VL6180x_getRegister(VL6180X_RESULT_RANGE_VAL); |
maruta | 0:8aca38272614 | 155 | // VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07); |
maruta | 0:8aca38272614 | 156 | // return distance; |
maruta | 0:8aca38272614 | 157 | } |
maruta | 0:8aca38272614 | 158 | |
maruta | 0:8aca38272614 | 159 | float VL6180x::getAmbientLight(vl6180x_als_gain VL6180X_ALS_GAIN) |
maruta | 0:8aca38272614 | 160 | { |
maruta | 0:8aca38272614 | 161 | //First load in Gain we are using, do it everytime incase someone changes it on us. |
maruta | 0:8aca38272614 | 162 | //Note: Upper nibble shoudl be set to 0x4 i.e. for ALS gain of 1.0 write 0x46 |
maruta | 0:8aca38272614 | 163 | VL6180x_setRegister(VL6180X_SYSALS_ANALOGUE_GAIN, (0x40 | VL6180X_ALS_GAIN)); // Set the ALS gain |
maruta | 0:8aca38272614 | 164 | |
maruta | 0:8aca38272614 | 165 | //Start ALS Measurement |
maruta | 0:8aca38272614 | 166 | VL6180x_setRegister(VL6180X_SYSALS_START, 0x01); |
maruta | 0:8aca38272614 | 167 | |
maruta | 0:8aca38272614 | 168 | wait_ms(100); //give it time... |
maruta | 0:8aca38272614 | 169 | |
maruta | 0:8aca38272614 | 170 | VL6180x_setRegister(VL6180X_SYSTEM_INTERRUPT_CLEAR, 0x07); |
maruta | 0:8aca38272614 | 171 | |
maruta | 0:8aca38272614 | 172 | //Retrieve the Raw ALS value from the sensoe |
maruta | 0:8aca38272614 | 173 | unsigned int alsRaw = VL6180x_getRegister16bit(VL6180X_RESULT_ALS_VAL); |
maruta | 0:8aca38272614 | 174 | |
maruta | 0:8aca38272614 | 175 | //Get Integration Period for calculation, we do this everytime incase someone changes it on us. |
maruta | 0:8aca38272614 | 176 | unsigned int alsIntegrationPeriodRaw = VL6180x_getRegister16bit(VL6180X_SYSALS_INTEGRATION_PERIOD); |
maruta | 0:8aca38272614 | 177 | |
maruta | 0:8aca38272614 | 178 | float alsIntegrationPeriod = 100.0 / alsIntegrationPeriodRaw ; |
maruta | 0:8aca38272614 | 179 | |
maruta | 0:8aca38272614 | 180 | //Calculate actual LUX from Appnotes |
maruta | 0:8aca38272614 | 181 | |
maruta | 0:8aca38272614 | 182 | float alsGain = 0.0; |
maruta | 0:8aca38272614 | 183 | |
maruta | 0:8aca38272614 | 184 | switch (VL6180X_ALS_GAIN){ |
maruta | 0:8aca38272614 | 185 | case GAIN_20: alsGain = 20.0; break; |
maruta | 0:8aca38272614 | 186 | case GAIN_10: alsGain = 10.32; break; |
maruta | 0:8aca38272614 | 187 | case GAIN_5: alsGain = 5.21; break; |
maruta | 0:8aca38272614 | 188 | case GAIN_2_5: alsGain = 2.60; break; |
maruta | 0:8aca38272614 | 189 | case GAIN_1_67: alsGain = 1.72; break; |
maruta | 0:8aca38272614 | 190 | case GAIN_1_25: alsGain = 1.28; break; |
maruta | 0:8aca38272614 | 191 | case GAIN_1: alsGain = 1.01; break; |
maruta | 0:8aca38272614 | 192 | case GAIN_40: alsGain = 40.0; break; |
maruta | 0:8aca38272614 | 193 | } |
maruta | 0:8aca38272614 | 194 | |
maruta | 0:8aca38272614 | 195 | //Calculate LUX from formula in AppNotes |
maruta | 0:8aca38272614 | 196 | |
maruta | 0:8aca38272614 | 197 | float alsCalculated = (float)0.32 * ((float)alsRaw / alsGain) * alsIntegrationPeriod; |
maruta | 0:8aca38272614 | 198 | |
maruta | 0:8aca38272614 | 199 | return alsCalculated; |
maruta | 0:8aca38272614 | 200 | } |
maruta | 0:8aca38272614 | 201 | |
maruta | 0:8aca38272614 | 202 | // --- Private Functions --- // |
maruta | 0:8aca38272614 | 203 | |
maruta | 0:8aca38272614 | 204 | uint8_t VL6180x::VL6180x_getRegister(uint16_t reg) |
maruta | 0:8aca38272614 | 205 | { |
maruta | 0:8aca38272614 | 206 | uint8_t dataout[2]; |
maruta | 0:8aca38272614 | 207 | uint8_t datain[1]; |
maruta | 0:8aca38272614 | 208 | |
maruta | 0:8aca38272614 | 209 | dataout[0] = (reg >> 8) & 0xFF; |
maruta | 0:8aca38272614 | 210 | dataout[1] = reg & 0xFF; |
maruta | 0:8aca38272614 | 211 | |
maruta | 0:8aca38272614 | 212 | _i2c.write(_addr, (char *) dataout, 2); |
maruta | 0:8aca38272614 | 213 | _i2c.read(_addr, (char *) datain, 1); |
maruta | 0:8aca38272614 | 214 | |
maruta | 0:8aca38272614 | 215 | return datain[0]; |
maruta | 0:8aca38272614 | 216 | } |
maruta | 0:8aca38272614 | 217 | |
maruta | 0:8aca38272614 | 218 | uint16_t VL6180x::VL6180x_getRegister16bit(uint16_t reg) |
maruta | 0:8aca38272614 | 219 | { |
maruta | 0:8aca38272614 | 220 | uint8_t dataout[2],datain[2]; |
maruta | 0:8aca38272614 | 221 | |
maruta | 0:8aca38272614 | 222 | dataout[0] = (reg >> 8) & 0xFF; |
maruta | 0:8aca38272614 | 223 | dataout[1] = reg & 0xFF; |
maruta | 0:8aca38272614 | 224 | |
maruta | 0:8aca38272614 | 225 | uint16_t data; |
maruta | 0:8aca38272614 | 226 | |
maruta | 0:8aca38272614 | 227 | _i2c.write(_addr, (char *) dataout, 2); |
maruta | 0:8aca38272614 | 228 | _i2c.read(_addr, (char *) datain,2); |
maruta | 0:8aca38272614 | 229 | |
maruta | 0:8aca38272614 | 230 | data = (datain[0] << 8)|datain[1]; |
maruta | 0:8aca38272614 | 231 | |
maruta | 0:8aca38272614 | 232 | return data; |
maruta | 0:8aca38272614 | 233 | } |
maruta | 0:8aca38272614 | 234 | |
maruta | 0:8aca38272614 | 235 | void VL6180x::VL6180x_setRegister(uint16_t reg, uint8_t data) |
maruta | 0:8aca38272614 | 236 | { |
maruta | 0:8aca38272614 | 237 | uint8_t dataout[3]; |
maruta | 0:8aca38272614 | 238 | |
maruta | 0:8aca38272614 | 239 | dataout[0] = (reg >> 8) & 0xFF; |
maruta | 0:8aca38272614 | 240 | dataout[1] = reg & 0xFF; |
maruta | 0:8aca38272614 | 241 | dataout[2] = data & 0xFF; |
maruta | 0:8aca38272614 | 242 | |
maruta | 0:8aca38272614 | 243 | _i2c.write(_addr, (char *) dataout, 3); |
maruta | 0:8aca38272614 | 244 | } |
maruta | 0:8aca38272614 | 245 | |
maruta | 0:8aca38272614 | 246 | void VL6180x::VL6180x_setRegister16bit(uint16_t reg, uint16_t data) |
maruta | 0:8aca38272614 | 247 | { |
maruta | 0:8aca38272614 | 248 | uint8_t dataout[4]; |
maruta | 0:8aca38272614 | 249 | dataout[0] = (reg >> 8) & 0xFF; |
maruta | 0:8aca38272614 | 250 | dataout[1] = reg & 0xFF; |
maruta | 0:8aca38272614 | 251 | dataout[2] = (data >> 8) & 0xff; |
maruta | 0:8aca38272614 | 252 | dataout[3] = data & 0xFF; |
maruta | 0:8aca38272614 | 253 | _i2c.write(_addr, (char *) dataout, 4); |
maruta | 0:8aca38272614 | 254 | } |