Ngoc Trong Nghia NGUYEN / Mbed 2 deprecated 6_laser_avec_servo

Dependencies:   mbed vl53l0x_api Servolib

Revision:
0:1806894cdd53
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/capteur_laser.h	Tue Apr 02 13:23:15 2019 +0000
@@ -0,0 +1,100 @@
+#include "vl53l0x_api.h"
+#include "vl53l0x_platform.h"
+#include "vl53l0x_i2c_platform.h"
+
+#define USE_I2C_2V8
+
+VL53L0X_Error WaitMeasurementDataReady(VL53L0X_DEV Dev) {
+    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
+    uint8_t NewDatReady=0;
+    uint32_t LoopNb;
+    
+    if (Status == VL53L0X_ERROR_NONE) {
+        LoopNb = 0;
+        do {
+            Status = VL53L0X_GetMeasurementDataReady(Dev, &NewDatReady);
+            if ((NewDatReady == 0x01) || Status != VL53L0X_ERROR_NONE) {
+                break;
+            }
+            LoopNb = LoopNb + 1;
+            VL53L0X_PollingDelay(Dev);
+        } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP);
+
+        if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) {
+            Status = VL53L0X_ERROR_TIME_OUT;
+        }
+    }
+
+    return Status;
+}
+
+VL53L0X_Error WaitStopCompleted(VL53L0X_DEV Dev) {
+    VL53L0X_Error Status = VL53L0X_ERROR_NONE;
+    uint32_t StopCompleted=0;
+    uint32_t LoopNb;
+
+    if (Status == VL53L0X_ERROR_NONE) {
+        LoopNb = 0;
+        do {
+            Status = VL53L0X_GetStopCompletedStatus(Dev, &StopCompleted);
+            if ((StopCompleted == 0x00) || Status != VL53L0X_ERROR_NONE) {
+                break;
+            }
+            LoopNb = LoopNb + 1;
+            VL53L0X_PollingDelay(Dev);
+        } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP);
+
+        if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) {
+            Status = VL53L0X_ERROR_TIME_OUT;
+        }
+
+    }
+
+    return Status;
+}
+
+VL53L0X_Dev_t MyDevice;
+VL53L0X_Dev_t *pMyDevice = &MyDevice;
+VL53L0X_RangingMeasurementData_t    RangingMeasurementData;
+VL53L0X_RangingMeasurementData_t   *pRangingMeasurementData    = &RangingMeasurementData;
+    
+void laser_init()
+{
+    // Initialize Comms
+    pMyDevice->I2cDevAddr      = 0x52;
+    pMyDevice->comms_type      =  1;
+    pMyDevice->comms_speed_khz =  400;
+        
+        
+    //VL53L0X_ERROR_CONTROL_INTERFACE;
+    VL53L0X_RdWord(&MyDevice, VL53L0X_REG_OSC_CALIBRATE_VAL,0);
+    VL53L0X_DataInit(&MyDevice); // Data initialization
+    //Status = VL53L0X_ERROR_NONE;
+    uint32_t refSpadCount;
+    uint8_t isApertureSpads;
+    uint8_t VhvSettings;
+    uint8_t PhaseCal;
+    
+    VL53L0X_StaticInit(pMyDevice); // Device Initialization
+    VL53L0X_PerformRefSpadManagement(pMyDevice, &refSpadCount, &isApertureSpads); // Device Initialization
+    VL53L0X_PerformRefCalibration(pMyDevice, &VhvSettings, &PhaseCal); // Device Initialization
+    VL53L0X_SetDeviceMode(pMyDevice, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING); // Setup in single ranging mode
+    VL53L0X_StartMeasurement(pMyDevice);
+}
+
+int laser_mesure(int nb_mesure)
+{
+    int measure=0;
+    int sum=0;
+    for(int i=0; i<nb_mesure; i++){
+        WaitMeasurementDataReady(pMyDevice);
+        VL53L0X_GetRangingMeasurementData(pMyDevice, pRangingMeasurementData);
+        measure=pRangingMeasurementData->RangeMilliMeter;
+        //printf("In loop measurement %d\n", mea);
+        sum=sum+measure;
+        // Clear the interrupt
+        VL53L0X_ClearInterruptMask(pMyDevice, VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY);
+        VL53L0X_PollingDelay(pMyDevice);
+    }
+    return sum/nb_mesure;
+}
\ No newline at end of file