Robotique FIP / Mbed 2 deprecated VL6180X

Dependencies:   VL6180x mbed

Fork of VL6180X_Explorer by Ian Kilburn

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /******************************************************************************
00002  * Developed from
00003  * VL6180X_demo.ino
00004  * Example Sketch for VL6180x time of flight range finder.
00005  * Casey Kuhns @ SparkFun Electronics
00006  * 10/29/2014
00007  * https://github.com/sparkfun/ToF_Range_Finder-VL6180_Library
00008  * 
00009  * The VL6180x by ST micro is a time of flight range finder that
00010  * uses pulsed IR light to determine distances from object at close
00011  * range.  The average range of a sensor is between 0-200mm
00012  * 
00013  * Resources:
00014  * This library uses the Arduino Wire.h to complete I2C transactions.
00015  * 
00016  * Development environment specifics:
00017  *  IDE: Arduino 1.0.5
00018  *  Hardware Platform: Arduino Pro 3.3V/8MHz
00019  *  VL6180x Breakout Version: 1.0
00020  * 
00021  * 
00022  * This code is beerware. If you see me (or any other SparkFun employee) at the
00023  * local pub, and you've found our code helpful, please buy us a round!
00024  * 
00025  * Distributed as-is; no warranty is given.
00026  ******************************************************************************/
00027 #include "mbed.h"
00028 #include <VL6180x.h>
00029 
00030 /*const float GAIN_1    = 1.01;  // Actual ALS Gain of 1.01
00031 const float GAIN_1_25 = 1.28;  // Actual ALS Gain of 1.28
00032 const float GAIN_1_67 = 1.72;  // Actual ALS Gain of 1.72
00033 const float GAIN_2_5  = 2.6;   // Actual ALS Gain of 2.60
00034 const float GAIN_5    = 5.21;  // Actual ALS Gain of 5.21
00035 const float GAIN_10   = 10.32; // Actual ALS Gain of 10.32
00036 const float GAIN_20   = 20;    // Actual ALS Gain of 20
00037 const float GAIN_40   = 40;    // Actual ALS Gain of 40
00038 */
00039 
00040 #define VL6180X_ADDRESS 0x29
00041 
00042 VL6180xIdentification identification;
00043 // mbed uses 8bit addresses shift address by 1 bit left
00044 VL6180x sensor(D14, D15, VL6180X_ADDRESS<<1);
00045 
00046 void printIdentification(struct VL6180xIdentification *temp){
00047   printf("Model ID = ");
00048   printf("%d\n",temp->idModel);
00049 
00050   printf("Model Rev = ");
00051   printf("%d",temp->idModelRevMajor);
00052   printf(".");
00053   printf("%d\n",temp->idModelRevMinor);
00054 
00055   printf("Module Rev = ");
00056   printf("%d",temp->idModuleRevMajor);
00057   printf(".");
00058   printf("%d\n",temp->idModuleRevMinor);  
00059 
00060   printf("Manufacture Date = ");
00061   printf("%d",((temp->idDate >> 3) & 0x001F));
00062   printf("/");
00063   printf("%d",((temp->idDate >> 8) & 0x000F));
00064   printf("/1");
00065   printf("%d\n",((temp->idDate >> 12) & 0x000F));
00066   printf(" Phase: ");
00067   printf("%d\n",(temp->idDate & 0x0007));
00068 
00069   printf("Manufacture Time (s)= ");
00070   printf("%d\n",(temp->idTime * 2));
00071   printf("\n\n");
00072 }
00073 int main() {
00074 
00075   wait_ms(100); // delay .1s
00076 
00077   sensor.getIdentification(&identification); // Retrieve manufacture info from device memory
00078   printIdentification(&identification); // Helper function to print all the Module information
00079 
00080   if(sensor.VL6180xInit() != 0){
00081         printf("FAILED TO INITALIZE\n"); //Initialize device and check for errors
00082   }; 
00083 
00084   sensor.VL6180xDefautSettings(); //Load default settings to get started.
00085   
00086     wait_ms(1000); // delay 1s
00087 
00088     
00089     
00090     while(1) {
00091   //Get Ambient Light level and report in LUX
00092       printf("Ambient Light Level (Lux) = ");
00093   
00094   //Input GAIN for light levels, 
00095   // GAIN_20     // Actual ALS Gain of 20
00096   // GAIN_10     // Actual ALS Gain of 10.32
00097   // GAIN_5      // Actual ALS Gain of 5.21
00098   // GAIN_2_5    // Actual ALS Gain of 2.60
00099   // GAIN_1_67   // Actual ALS Gain of 1.72
00100   // GAIN_1_25   // Actual ALS Gain of 1.28
00101   // GAIN_1      // Actual ALS Gain of 1.01
00102   // GAIN_40     // Actual ALS Gain of 40
00103   
00104       printf("%f\n",sensor.getAmbientLight(GAIN_1) );
00105 
00106   //Get Distance and report in mm
00107       printf("Distance measured (mm) = ");
00108       printf("%d\n", sensor.getDistance() ); 
00109 
00110       wait_ms(500);  
00111     }
00112 }
00113 
00114 
00115 
00116 
00117