VL6180x lib under optimization (add longer ranging and multiple devices on the same I2C)

Dependents:   Coupe Robotique FIP Coupe-Robotique-FIP-Main

Committer:
julientiron
Date:
Tue Jul 07 20:36:43 2015 +0000
Revision:
0:84fb42fedc5b
add I2C internal pull up

Who changed what in which revision?

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