Ngoc Trong Nghia NGUYEN / Mbed 2 deprecated 6_laser_avec_servo

Dependencies:   mbed vl53l0x_api Servolib

Committer:
nguyentony
Date:
Tue Apr 02 13:23:15 2019 +0000
Revision:
0:1806894cdd53
contournement;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nguyentony 0:1806894cdd53 1 #include "vl53l0x_api.h"
nguyentony 0:1806894cdd53 2 #include "vl53l0x_platform.h"
nguyentony 0:1806894cdd53 3 #include "vl53l0x_i2c_platform.h"
nguyentony 0:1806894cdd53 4
nguyentony 0:1806894cdd53 5 #define USE_I2C_2V8
nguyentony 0:1806894cdd53 6
nguyentony 0:1806894cdd53 7 VL53L0X_Error WaitMeasurementDataReady(VL53L0X_DEV Dev) {
nguyentony 0:1806894cdd53 8 VL53L0X_Error Status = VL53L0X_ERROR_NONE;
nguyentony 0:1806894cdd53 9 uint8_t NewDatReady=0;
nguyentony 0:1806894cdd53 10 uint32_t LoopNb;
nguyentony 0:1806894cdd53 11
nguyentony 0:1806894cdd53 12 if (Status == VL53L0X_ERROR_NONE) {
nguyentony 0:1806894cdd53 13 LoopNb = 0;
nguyentony 0:1806894cdd53 14 do {
nguyentony 0:1806894cdd53 15 Status = VL53L0X_GetMeasurementDataReady(Dev, &NewDatReady);
nguyentony 0:1806894cdd53 16 if ((NewDatReady == 0x01) || Status != VL53L0X_ERROR_NONE) {
nguyentony 0:1806894cdd53 17 break;
nguyentony 0:1806894cdd53 18 }
nguyentony 0:1806894cdd53 19 LoopNb = LoopNb + 1;
nguyentony 0:1806894cdd53 20 VL53L0X_PollingDelay(Dev);
nguyentony 0:1806894cdd53 21 } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP);
nguyentony 0:1806894cdd53 22
nguyentony 0:1806894cdd53 23 if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) {
nguyentony 0:1806894cdd53 24 Status = VL53L0X_ERROR_TIME_OUT;
nguyentony 0:1806894cdd53 25 }
nguyentony 0:1806894cdd53 26 }
nguyentony 0:1806894cdd53 27
nguyentony 0:1806894cdd53 28 return Status;
nguyentony 0:1806894cdd53 29 }
nguyentony 0:1806894cdd53 30
nguyentony 0:1806894cdd53 31 VL53L0X_Error WaitStopCompleted(VL53L0X_DEV Dev) {
nguyentony 0:1806894cdd53 32 VL53L0X_Error Status = VL53L0X_ERROR_NONE;
nguyentony 0:1806894cdd53 33 uint32_t StopCompleted=0;
nguyentony 0:1806894cdd53 34 uint32_t LoopNb;
nguyentony 0:1806894cdd53 35
nguyentony 0:1806894cdd53 36 if (Status == VL53L0X_ERROR_NONE) {
nguyentony 0:1806894cdd53 37 LoopNb = 0;
nguyentony 0:1806894cdd53 38 do {
nguyentony 0:1806894cdd53 39 Status = VL53L0X_GetStopCompletedStatus(Dev, &StopCompleted);
nguyentony 0:1806894cdd53 40 if ((StopCompleted == 0x00) || Status != VL53L0X_ERROR_NONE) {
nguyentony 0:1806894cdd53 41 break;
nguyentony 0:1806894cdd53 42 }
nguyentony 0:1806894cdd53 43 LoopNb = LoopNb + 1;
nguyentony 0:1806894cdd53 44 VL53L0X_PollingDelay(Dev);
nguyentony 0:1806894cdd53 45 } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP);
nguyentony 0:1806894cdd53 46
nguyentony 0:1806894cdd53 47 if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) {
nguyentony 0:1806894cdd53 48 Status = VL53L0X_ERROR_TIME_OUT;
nguyentony 0:1806894cdd53 49 }
nguyentony 0:1806894cdd53 50
nguyentony 0:1806894cdd53 51 }
nguyentony 0:1806894cdd53 52
nguyentony 0:1806894cdd53 53 return Status;
nguyentony 0:1806894cdd53 54 }
nguyentony 0:1806894cdd53 55
nguyentony 0:1806894cdd53 56 VL53L0X_Dev_t MyDevice;
nguyentony 0:1806894cdd53 57 VL53L0X_Dev_t *pMyDevice = &MyDevice;
nguyentony 0:1806894cdd53 58 VL53L0X_RangingMeasurementData_t RangingMeasurementData;
nguyentony 0:1806894cdd53 59 VL53L0X_RangingMeasurementData_t *pRangingMeasurementData = &RangingMeasurementData;
nguyentony 0:1806894cdd53 60
nguyentony 0:1806894cdd53 61 void laser_init()
nguyentony 0:1806894cdd53 62 {
nguyentony 0:1806894cdd53 63 // Initialize Comms
nguyentony 0:1806894cdd53 64 pMyDevice->I2cDevAddr = 0x52;
nguyentony 0:1806894cdd53 65 pMyDevice->comms_type = 1;
nguyentony 0:1806894cdd53 66 pMyDevice->comms_speed_khz = 400;
nguyentony 0:1806894cdd53 67
nguyentony 0:1806894cdd53 68
nguyentony 0:1806894cdd53 69 //VL53L0X_ERROR_CONTROL_INTERFACE;
nguyentony 0:1806894cdd53 70 VL53L0X_RdWord(&MyDevice, VL53L0X_REG_OSC_CALIBRATE_VAL,0);
nguyentony 0:1806894cdd53 71 VL53L0X_DataInit(&MyDevice); // Data initialization
nguyentony 0:1806894cdd53 72 //Status = VL53L0X_ERROR_NONE;
nguyentony 0:1806894cdd53 73 uint32_t refSpadCount;
nguyentony 0:1806894cdd53 74 uint8_t isApertureSpads;
nguyentony 0:1806894cdd53 75 uint8_t VhvSettings;
nguyentony 0:1806894cdd53 76 uint8_t PhaseCal;
nguyentony 0:1806894cdd53 77
nguyentony 0:1806894cdd53 78 VL53L0X_StaticInit(pMyDevice); // Device Initialization
nguyentony 0:1806894cdd53 79 VL53L0X_PerformRefSpadManagement(pMyDevice, &refSpadCount, &isApertureSpads); // Device Initialization
nguyentony 0:1806894cdd53 80 VL53L0X_PerformRefCalibration(pMyDevice, &VhvSettings, &PhaseCal); // Device Initialization
nguyentony 0:1806894cdd53 81 VL53L0X_SetDeviceMode(pMyDevice, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING); // Setup in single ranging mode
nguyentony 0:1806894cdd53 82 VL53L0X_StartMeasurement(pMyDevice);
nguyentony 0:1806894cdd53 83 }
nguyentony 0:1806894cdd53 84
nguyentony 0:1806894cdd53 85 int laser_mesure(int nb_mesure)
nguyentony 0:1806894cdd53 86 {
nguyentony 0:1806894cdd53 87 int measure=0;
nguyentony 0:1806894cdd53 88 int sum=0;
nguyentony 0:1806894cdd53 89 for(int i=0; i<nb_mesure; i++){
nguyentony 0:1806894cdd53 90 WaitMeasurementDataReady(pMyDevice);
nguyentony 0:1806894cdd53 91 VL53L0X_GetRangingMeasurementData(pMyDevice, pRangingMeasurementData);
nguyentony 0:1806894cdd53 92 measure=pRangingMeasurementData->RangeMilliMeter;
nguyentony 0:1806894cdd53 93 //printf("In loop measurement %d\n", mea);
nguyentony 0:1806894cdd53 94 sum=sum+measure;
nguyentony 0:1806894cdd53 95 // Clear the interrupt
nguyentony 0:1806894cdd53 96 VL53L0X_ClearInterruptMask(pMyDevice, VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY);
nguyentony 0:1806894cdd53 97 VL53L0X_PollingDelay(pMyDevice);
nguyentony 0:1806894cdd53 98 }
nguyentony 0:1806894cdd53 99 return sum/nb_mesure;
nguyentony 0:1806894cdd53 100 }