#1

Dependencies:   mbed SB1602E

Fork of mbed-cloud-connect-sensor-laser-distance by Andrea Corrado

Committer:
KStefan
Date:
Tue May 01 19:16:01 2018 +0000
Revision:
8:0f74264cc38a
Parent:
5:1fca2683ae6f
Child:
9:f2bdb2a79528
#1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andcor02 0:80032665d37e 1 #include "mbed.h"
KStefan 8:0f74264cc38a 2 #include "SB1602E.h"
andcor02 5:1fca2683ae6f 3 #include "vl53l0x_api.h"
andcor02 5:1fca2683ae6f 4 #include "vl53l0x_platform.h"
andcor02 5:1fca2683ae6f 5 #include "vl53l0x_i2c_platform.h"
andcor02 5:1fca2683ae6f 6
andcor02 5:1fca2683ae6f 7 #define USE_I2C_2V8
andcor02 0:80032665d37e 8
KStefan 8:0f74264cc38a 9 DigitalOut myled(P0_7); // 7=rot, 8=gruen
KStefan 8:0f74264cc38a 10
KStefan 8:0f74264cc38a 11 I2C i2c_disp(P0_5, P0_4); // SDA, SCL auf NXP U24
KStefan 8:0f74264cc38a 12 SB1602E lcd( i2c_disp );
andcor02 0:80032665d37e 13
andcor02 5:1fca2683ae6f 14 VL53L0X_Error WaitMeasurementDataReady(VL53L0X_DEV Dev) {
andcor02 5:1fca2683ae6f 15 VL53L0X_Error Status = VL53L0X_ERROR_NONE;
andcor02 5:1fca2683ae6f 16 uint8_t NewDatReady=0;
andcor02 5:1fca2683ae6f 17 uint32_t LoopNb;
andcor02 5:1fca2683ae6f 18
andcor02 5:1fca2683ae6f 19 if (Status == VL53L0X_ERROR_NONE) {
andcor02 5:1fca2683ae6f 20 LoopNb = 0;
andcor02 5:1fca2683ae6f 21 do {
andcor02 5:1fca2683ae6f 22 Status = VL53L0X_GetMeasurementDataReady(Dev, &NewDatReady);
andcor02 5:1fca2683ae6f 23 if ((NewDatReady == 0x01) || Status != VL53L0X_ERROR_NONE) {
andcor02 5:1fca2683ae6f 24 break;
andcor02 5:1fca2683ae6f 25 }
andcor02 5:1fca2683ae6f 26 LoopNb = LoopNb + 1;
andcor02 5:1fca2683ae6f 27 VL53L0X_PollingDelay(Dev);
andcor02 5:1fca2683ae6f 28 } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP);
andcor02 5:1fca2683ae6f 29
andcor02 5:1fca2683ae6f 30 if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) {
andcor02 5:1fca2683ae6f 31 Status = VL53L0X_ERROR_TIME_OUT;
andcor02 5:1fca2683ae6f 32 }
andcor02 5:1fca2683ae6f 33 }
andcor02 5:1fca2683ae6f 34
andcor02 5:1fca2683ae6f 35 return Status;
andcor02 5:1fca2683ae6f 36 }
andcor02 5:1fca2683ae6f 37
andcor02 5:1fca2683ae6f 38 VL53L0X_Error WaitStopCompleted(VL53L0X_DEV Dev) {
andcor02 5:1fca2683ae6f 39 VL53L0X_Error Status = VL53L0X_ERROR_NONE;
andcor02 5:1fca2683ae6f 40 uint32_t StopCompleted=0;
andcor02 5:1fca2683ae6f 41 uint32_t LoopNb;
andcor02 5:1fca2683ae6f 42
andcor02 5:1fca2683ae6f 43 if (Status == VL53L0X_ERROR_NONE) {
andcor02 5:1fca2683ae6f 44 LoopNb = 0;
andcor02 5:1fca2683ae6f 45 do {
andcor02 5:1fca2683ae6f 46 Status = VL53L0X_GetStopCompletedStatus(Dev, &StopCompleted);
andcor02 5:1fca2683ae6f 47 if ((StopCompleted == 0x00) || Status != VL53L0X_ERROR_NONE) {
andcor02 5:1fca2683ae6f 48 break;
andcor02 5:1fca2683ae6f 49 }
andcor02 5:1fca2683ae6f 50 LoopNb = LoopNb + 1;
andcor02 5:1fca2683ae6f 51 VL53L0X_PollingDelay(Dev);
andcor02 5:1fca2683ae6f 52 } while (LoopNb < VL53L0X_DEFAULT_MAX_LOOP);
andcor02 5:1fca2683ae6f 53
andcor02 5:1fca2683ae6f 54 if (LoopNb >= VL53L0X_DEFAULT_MAX_LOOP) {
andcor02 5:1fca2683ae6f 55 Status = VL53L0X_ERROR_TIME_OUT;
andcor02 5:1fca2683ae6f 56 }
andcor02 5:1fca2683ae6f 57
andcor02 5:1fca2683ae6f 58 }
andcor02 5:1fca2683ae6f 59
andcor02 5:1fca2683ae6f 60 return Status;
andcor02 5:1fca2683ae6f 61 }
andcor02 0:80032665d37e 62
andcor02 0:80032665d37e 63 int main()
andcor02 0:80032665d37e 64 {
andcor02 5:1fca2683ae6f 65
andcor02 5:1fca2683ae6f 66 //Setup laser
andcor02 5:1fca2683ae6f 67 int var=1, measure=0;
andcor02 5:1fca2683ae6f 68 int ave=0, sum=0;
andcor02 5:1fca2683ae6f 69 VL53L0X_Dev_t MyDevice;
andcor02 5:1fca2683ae6f 70 VL53L0X_Dev_t *pMyDevice = &MyDevice;
andcor02 5:1fca2683ae6f 71 VL53L0X_RangingMeasurementData_t RangingMeasurementData;
andcor02 5:1fca2683ae6f 72 VL53L0X_RangingMeasurementData_t *pRangingMeasurementData = &RangingMeasurementData;
andcor02 5:1fca2683ae6f 73
andcor02 5:1fca2683ae6f 74 // Initialize Comms laster
andcor02 5:1fca2683ae6f 75 pMyDevice->I2cDevAddr = 0x52;
andcor02 5:1fca2683ae6f 76 pMyDevice->comms_type = 1;
andcor02 5:1fca2683ae6f 77 pMyDevice->comms_speed_khz = 400;
andcor02 5:1fca2683ae6f 78
andcor02 5:1fca2683ae6f 79
andcor02 5:1fca2683ae6f 80 VL53L0X_RdWord(&MyDevice, VL53L0X_REG_OSC_CALIBRATE_VAL,0);
andcor02 5:1fca2683ae6f 81 VL53L0X_DataInit(&MyDevice);
andcor02 5:1fca2683ae6f 82 uint32_t refSpadCount;
andcor02 5:1fca2683ae6f 83 uint8_t isApertureSpads;
andcor02 5:1fca2683ae6f 84 uint8_t VhvSettings;
andcor02 5:1fca2683ae6f 85 uint8_t PhaseCal;
andcor02 5:1fca2683ae6f 86
andcor02 5:1fca2683ae6f 87 VL53L0X_StaticInit(pMyDevice);
andcor02 5:1fca2683ae6f 88 VL53L0X_PerformRefSpadManagement(pMyDevice, &refSpadCount, &isApertureSpads); // Device Initialization
andcor02 5:1fca2683ae6f 89 VL53L0X_PerformRefCalibration(pMyDevice, &VhvSettings, &PhaseCal); // Device Initialization
andcor02 5:1fca2683ae6f 90 VL53L0X_SetDeviceMode(pMyDevice, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING); // Setup in single ranging mode
KStefan 8:0f74264cc38a 91 //VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, (FixPoint1616_t)(0.1*65536)); //Long Range modus
KStefan 8:0f74264cc38a 92 //VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, (FixPoint1616_t)(60*65536)); // Long Range
KStefan 8:0f74264cc38a 93 //VL53L0X_SetMeasurementTimingBudgetMicroSeconds(pMyDevice, 33000); // Long Range
KStefan 8:0f74264cc38a 94 //VL53L0X_SetVcselPulsePeriod(pMyDevice, VL53L0X_VCSEL_PERIOD_PRE_RANGE, 18); // Long Range
KStefan 8:0f74264cc38a 95 //VL53L0X_SetVcselPulsePeriod(pMyDevice, VL53L0X_VCSEL_PERIOD_FINAL_RANGE, 14); // Long Range
KStefan 8:0f74264cc38a 96
andcor02 5:1fca2683ae6f 97 VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, (FixPoint1616_t)(0.25*65536)); //High Accuracy mode, see API PDF
andcor02 5:1fca2683ae6f 98 VL53L0X_SetLimitCheckValue(pMyDevice, VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, (FixPoint1616_t)(18*65536)); //High Accuracy mode, see API PDF
andcor02 5:1fca2683ae6f 99 VL53L0X_SetMeasurementTimingBudgetMicroSeconds(pMyDevice, 200000); //High Accuracy mode, see API PDF
andcor02 5:1fca2683ae6f 100 VL53L0X_StartMeasurement(pMyDevice);
andcor02 5:1fca2683ae6f 101
andcor02 0:80032665d37e 102 while(1) {
KStefan 8:0f74264cc38a 103 //lcd.cls();
KStefan 8:0f74264cc38a 104 //lcd.locate(0,3);
KStefan 8:0f74264cc38a 105 //lcd.printf("[DISTANCE]");
KStefan 8:0f74264cc38a 106
KStefan 8:0f74264cc38a 107 lcd.printf( 0, "Laser-Messung:\r" ); // line# (0 or 1), string
KStefan 8:0f74264cc38a 108
KStefan 8:0f74264cc38a 109 myled = !myled;
KStefan 8:0f74264cc38a 110 while(var<=5){
andcor02 5:1fca2683ae6f 111 WaitMeasurementDataReady(pMyDevice);
andcor02 5:1fca2683ae6f 112 VL53L0X_GetRangingMeasurementData(pMyDevice, pRangingMeasurementData);
andcor02 5:1fca2683ae6f 113 measure=pRangingMeasurementData->RangeMilliMeter;
andcor02 5:1fca2683ae6f 114 sum=sum+measure;
andcor02 5:1fca2683ae6f 115 VL53L0X_ClearInterruptMask(pMyDevice, VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY);
andcor02 5:1fca2683ae6f 116 VL53L0X_PollingDelay(pMyDevice);
andcor02 5:1fca2683ae6f 117 var++;
andcor02 5:1fca2683ae6f 118 }
andcor02 5:1fca2683ae6f 119 ave=sum/var;
andcor02 5:1fca2683ae6f 120 var=1;
andcor02 5:1fca2683ae6f 121 sum=0;
KStefan 8:0f74264cc38a 122 //lcd.locate(0,15);
KStefan 8:0f74264cc38a 123 //lcd.printf("%dmm", ave); // Print to LCD values
KStefan 8:0f74264cc38a 124 lcd.printf( 1,1, " ");
KStefan 8:0f74264cc38a 125 lcd.printf( 1,1, "%dmm", ave); // line# (0 or 1), string
KStefan 8:0f74264cc38a 126 wait(0.2);
andcor02 0:80032665d37e 127 }
andcor02 2:587b4d7444d1 128 }