Robotique FIP / Mbed 2 deprecated VL6180X_Explorer

Dependencies:   VL6180x mbed Servo

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 #include "Servo.h"
00030 
00031 /*const float GAIN_1    = 1.01;  // Actual ALS Gain of 1.01
00032 const float GAIN_1_25 = 1.28;  // Actual ALS Gain of 1.28
00033 const float GAIN_1_67 = 1.72;  // Actual ALS Gain of 1.72
00034 const float GAIN_2_5  = 2.6;   // Actual ALS Gain of 2.60
00035 const float GAIN_5    = 5.21;  // Actual ALS Gain of 5.21
00036 const float GAIN_10   = 10.32; // Actual ALS Gain of 10.32
00037 const float GAIN_20   = 20;    // Actual ALS Gain of 20
00038 const float GAIN_40   = 40;    // Actual ALS Gain of 40
00039 */
00040 
00041 #define VL6180X_ADDRESS 0x29
00042 
00043 Serial pc(SERIAL_TX, SERIAL_RX);
00044 
00045 DigitalInOut sda_D(PB_9);
00046 DigitalInOut scl_D(PB_8);
00047 
00048 
00049 VL6180xIdentification identification;
00050 // mbed uses 8bit addresses shift address by 1 bit left
00051 VL6180x sensor_D(PB_9, PB_8, VL6180X_ADDRESS<<1);
00052 
00053 void printIdentification(struct VL6180xIdentification *temp){
00054   printf("Model ID = ");
00055   printf("%d\n",temp->idModel);
00056 
00057   printf("Model Rev = ");
00058   printf("%d",temp->idModelRevMajor);
00059   printf(".");
00060   printf("%d\n",temp->idModelRevMinor);
00061 
00062   printf("Module Rev = ");
00063   printf("%d",temp->idModuleRevMajor);
00064   printf(".");
00065   printf("%d\n",temp->idModuleRevMinor);  
00066 
00067   printf("Manufacture Date = ");
00068   printf("%d",((temp->idDate >> 3) & 0x001F));
00069   printf("/");
00070   printf("%d",((temp->idDate >> 8) & 0x000F));
00071   printf("/1");
00072   printf("%d\n",((temp->idDate >> 12) & 0x000F));
00073   printf(" Phase: ");
00074   printf("%d\n",(temp->idDate & 0x0007));
00075 
00076   printf("Manufacture Time (s)= ");
00077   printf("%d\n",(temp->idTime * 2));
00078   printf("\n\n");
00079 }
00080 int main() {
00081 
00082  Servo myservo(D7);                // Create the servo object
00083  float butee_droite=0.1;
00084  float butee_gauche=1;
00085  double position=0.1;
00086  myservo.calibrate(0.00095, 90.0); // Calibrate the servo
00087  
00088  pc.baud(115200);
00089 
00090   wait_ms(100); // delay .1s
00091   sda_D.mode(PullUp);
00092   scl_D.mode(PullUp);
00093   //sensor_D.getIdentification(&identification); // Retrieve manufacture info from device memory
00094   //printIdentification(&identification); // Helper function to print all the Module information
00095 
00096   if(sensor_D.VL6180xInit() != 0){
00097         printf("FAILED TO INITALIZE\n"); //Initialize device and check for errors
00098   }; 
00099 
00100   sensor_D.VL6180xDefautSettings(); //Load default settings to get started.
00101   wait_ms(100); // delay
00102   
00103   while(1) {
00104   
00105         while(position<butee_gauche){
00106             myservo.write(position);
00107             //printf("capteur: %d %f\n", sensor_D.getDistance(), position);
00108             pc.printf("%d\n\r", sensor_D.getDistance());
00109             pc.printf("%f\n\r", position);
00110             wait_ms(10);  
00111             position = position + 0.01;
00112 
00113         }
00114         
00115         myservo.write(butee_droite);
00116         position = 0.1;
00117         wait_ms(10);
00118         
00119       
00120   }
00121 }
00122 
00123 
00124 
00125 
00126