![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
baca lidar aja
main.cpp@3:03963277337b, 2019-01-17 (annotated)
- Committer:
- 315_josh
- Date:
- Thu Jan 17 12:51:00 2019 +0000
- Revision:
- 3:03963277337b
- Parent:
- 2:8ac50bd2c7a1
baca lidar
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mjarvisal | 0:b6867e1a23fa | 1 | #include "mbed.h" |
mjarvisal | 0:b6867e1a23fa | 2 | #include "vl53l0x_api.h" |
mjarvisal | 0:b6867e1a23fa | 3 | #include "vl53l0x_platform.h" |
mjarvisal | 0:b6867e1a23fa | 4 | #include "vl53l0x_i2c_platform.h" |
mjarvisal | 0:b6867e1a23fa | 5 | |
315_josh | 3:03963277337b | 6 | #define USE_I2C_2V8 |
315_josh | 3:03963277337b | 7 | |
andcor02 | 2:8ac50bd2c7a1 | 8 | Serial pc(USBTX, USBRX); |
andcor02 | 2:8ac50bd2c7a1 | 9 | DigitalOut led(LED1); |
mjarvisal | 0:b6867e1a23fa | 10 | |
mjarvisal | 0:b6867e1a23fa | 11 | |
mjarvisal | 0:b6867e1a23fa | 12 | VL53L0X_Error WaitMeasurementDataReady(VL53L0X_DEV Dev) { |
mjarvisal | 0:b6867e1a23fa | 13 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; |
mjarvisal | 0:b6867e1a23fa | 14 | uint8_t NewDatReady=0; |
mjarvisal | 0:b6867e1a23fa | 15 | uint32_t LoopNb; |
315_josh | 3:03963277337b | 16 | |
mjarvisal | 0:b6867e1a23fa | 17 | if (Status == VL53L0X_ERROR_NONE) { |
mjarvisal | 0:b6867e1a23fa | 18 | LoopNb = 0; |
mjarvisal | 0:b6867e1a23fa | 19 | do { |
mjarvisal | 0:b6867e1a23fa | 20 | Status = VL53L0X_GetMeasurementDataReady(Dev, &NewDatReady); |
mjarvisal | 0:b6867e1a23fa | 21 | if ((NewDatReady == 0x01) || Status != VL53L0X_ERROR_NONE) { |
mjarvisal | 0:b6867e1a23fa | 22 | break; |
mjarvisal | 0:b6867e1a23fa | 23 | } |
mjarvisal | 0:b6867e1a23fa | 24 | LoopNb = LoopNb + 1; |
mjarvisal | 0:b6867e1a23fa | 25 | VL53L0X_PollingDelay(Dev); |
mjarvisal | 0:b6867e1a23fa | 26 | } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP); |
mjarvisal | 0:b6867e1a23fa | 27 | |
mjarvisal | 0:b6867e1a23fa | 28 | if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) { |
mjarvisal | 0:b6867e1a23fa | 29 | Status = VL53L0X_ERROR_TIME_OUT; |
mjarvisal | 0:b6867e1a23fa | 30 | } |
mjarvisal | 0:b6867e1a23fa | 31 | } |
mjarvisal | 0:b6867e1a23fa | 32 | |
mjarvisal | 0:b6867e1a23fa | 33 | return Status; |
mjarvisal | 0:b6867e1a23fa | 34 | } |
mjarvisal | 0:b6867e1a23fa | 35 | |
mjarvisal | 0:b6867e1a23fa | 36 | VL53L0X_Error WaitStopCompleted(VL53L0X_DEV Dev) { |
mjarvisal | 0:b6867e1a23fa | 37 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; |
mjarvisal | 0:b6867e1a23fa | 38 | uint32_t StopCompleted=0; |
mjarvisal | 0:b6867e1a23fa | 39 | uint32_t LoopNb; |
mjarvisal | 0:b6867e1a23fa | 40 | |
mjarvisal | 0:b6867e1a23fa | 41 | if (Status == VL53L0X_ERROR_NONE) { |
mjarvisal | 0:b6867e1a23fa | 42 | LoopNb = 0; |
mjarvisal | 0:b6867e1a23fa | 43 | do { |
mjarvisal | 0:b6867e1a23fa | 44 | Status = VL53L0X_GetStopCompletedStatus(Dev, &StopCompleted); |
mjarvisal | 0:b6867e1a23fa | 45 | if ((StopCompleted == 0x00) || Status != VL53L0X_ERROR_NONE) { |
mjarvisal | 0:b6867e1a23fa | 46 | break; |
mjarvisal | 0:b6867e1a23fa | 47 | } |
mjarvisal | 0:b6867e1a23fa | 48 | LoopNb = LoopNb + 1; |
mjarvisal | 0:b6867e1a23fa | 49 | VL53L0X_PollingDelay(Dev); |
mjarvisal | 0:b6867e1a23fa | 50 | } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP); |
mjarvisal | 0:b6867e1a23fa | 51 | |
mjarvisal | 0:b6867e1a23fa | 52 | if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) { |
mjarvisal | 0:b6867e1a23fa | 53 | Status = VL53L0X_ERROR_TIME_OUT; |
mjarvisal | 0:b6867e1a23fa | 54 | } |
mjarvisal | 0:b6867e1a23fa | 55 | |
mjarvisal | 0:b6867e1a23fa | 56 | } |
mjarvisal | 0:b6867e1a23fa | 57 | |
mjarvisal | 0:b6867e1a23fa | 58 | return Status; |
mjarvisal | 0:b6867e1a23fa | 59 | } |
mjarvisal | 0:b6867e1a23fa | 60 | |
mjarvisal | 0:b6867e1a23fa | 61 | |
315_josh | 3:03963277337b | 62 | |
mjarvisal | 0:b6867e1a23fa | 63 | int main() |
mjarvisal | 0:b6867e1a23fa | 64 | { |
315_josh | 3:03963277337b | 65 | int x=1, measure=0; |
315_josh | 3:03963277337b | 66 | int ave=0, sum=0; |
mjarvisal | 0:b6867e1a23fa | 67 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; |
mjarvisal | 0:b6867e1a23fa | 68 | VL53L0X_Dev_t MyDevice; |
mjarvisal | 0:b6867e1a23fa | 69 | VL53L0X_Dev_t *pMyDevice = &MyDevice; |
315_josh | 3:03963277337b | 70 | VL53L0X_RangingMeasurementData_t RangingMeasurementData; |
315_josh | 3:03963277337b | 71 | VL53L0X_RangingMeasurementData_t *pRangingMeasurementData = &RangingMeasurementData; |
mjarvisal | 0:b6867e1a23fa | 72 | VL53L0X_Version_t Version; |
mjarvisal | 0:b6867e1a23fa | 73 | |
mjarvisal | 0:b6867e1a23fa | 74 | // Initialize Comms |
mjarvisal | 0:b6867e1a23fa | 75 | pMyDevice->I2cDevAddr = 0x52; |
mjarvisal | 0:b6867e1a23fa | 76 | pMyDevice->comms_type = 1; |
mjarvisal | 0:b6867e1a23fa | 77 | pMyDevice->comms_speed_khz = 400; |
mjarvisal | 0:b6867e1a23fa | 78 | |
315_josh | 3:03963277337b | 79 | |
315_josh | 3:03963277337b | 80 | VL53L0X_ERROR_CONTROL_INTERFACE; |
315_josh | 3:03963277337b | 81 | VL53L0X_RdWord(&MyDevice, VL53L0X_REG_OSC_CALIBRATE_VAL,0); |
315_josh | 3:03963277337b | 82 | VL53L0X_DataInit(&MyDevice); // Data initialization |
315_josh | 3:03963277337b | 83 | Status = VL53L0X_ERROR_NONE; |
315_josh | 3:03963277337b | 84 | uint32_t refSpadCount; |
315_josh | 3:03963277337b | 85 | uint8_t isApertureSpads; |
315_josh | 3:03963277337b | 86 | uint8_t VhvSettings; |
315_josh | 3:03963277337b | 87 | uint8_t PhaseCal; |
mjarvisal | 0:b6867e1a23fa | 88 | |
315_josh | 3:03963277337b | 89 | VL53L0X_StaticInit(pMyDevice); // Device Initialization |
315_josh | 3:03963277337b | 90 | VL53L0X_PerformRefSpadManagement(pMyDevice, &refSpadCount, &isApertureSpads); // Device Initialization |
315_josh | 3:03963277337b | 91 | VL53L0X_PerformRefCalibration(pMyDevice, &VhvSettings, &PhaseCal); // Device Initialization |
315_josh | 3:03963277337b | 92 | VL53L0X_SetDeviceMode(pMyDevice, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING); // Setup in single ranging mode |
315_josh | 3:03963277337b | 93 | VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, (FixPoint1616_t)(0.25*65536)); //High Accuracy mode, see API PDF |
315_josh | 3:03963277337b | 94 | VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, (FixPoint1616_t)(18*65536)); //High Accuracy mode, see API PDF |
315_josh | 3:03963277337b | 95 | VL53L0X_SetMeasurementTimingBudgetMicroSeconds(pMyDevice, 200000); //High Accuracy mode, see API PDF |
315_josh | 3:03963277337b | 96 | VL53L0X_StartMeasurement(pMyDevice); |
mjarvisal | 0:b6867e1a23fa | 97 | |
315_josh | 3:03963277337b | 98 | while (1){ |
315_josh | 3:03963277337b | 99 | while(x<=50){ |
315_josh | 3:03963277337b | 100 | WaitMeasurementDataReady(pMyDevice); |
315_josh | 3:03963277337b | 101 | VL53L0X_GetRangingMeasurementData(pMyDevice, pRangingMeasurementData); |
315_josh | 3:03963277337b | 102 | measure=pRangingMeasurementData->RangeMilliMeter; |
315_josh | 3:03963277337b | 103 | //printf("In loop measurement %d\n", mea); |
315_josh | 3:03963277337b | 104 | sum=sum+measure; |
315_josh | 3:03963277337b | 105 | led=!led; |
315_josh | 3:03963277337b | 106 | // Clear the interrupt |
315_josh | 3:03963277337b | 107 | VL53L0X_ClearInterruptMask(pMyDevice, VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY); |
315_josh | 3:03963277337b | 108 | VL53L0X_PollingDelay(pMyDevice); |
315_josh | 3:03963277337b | 109 | x++; |
mjarvisal | 0:b6867e1a23fa | 110 | } |
315_josh | 3:03963277337b | 111 | ave=sum/x; |
315_josh | 3:03963277337b | 112 | printf("Average is %d\n", ave); |
315_josh | 3:03963277337b | 113 | x=1; |
315_josh | 3:03963277337b | 114 | sum=0; |
315_josh | 3:03963277337b | 115 | wait(20); |
mjarvisal | 0:b6867e1a23fa | 116 | } |
mjarvisal | 0:b6867e1a23fa | 117 | |
315_josh | 3:03963277337b | 118 | VL53L0X_StopMeasurement(pMyDevice); |
315_josh | 3:03963277337b | 119 | WaitStopCompleted(pMyDevice); |
315_josh | 3:03963277337b | 120 | VL53L0X_ClearInterruptMask(pMyDevice,VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY); |
315_josh | 3:03963277337b | 121 | |
mjarvisal | 0:b6867e1a23fa | 122 | } |
mjarvisal | 0:b6867e1a23fa | 123 |