VL53L1CB expansion board example, showing multi-ranges in an autonomous setup & polling mode. Uses the onboard sensor. Targets MbedOS v6.10.0.

Dependencies:   X_NUCLEO_53L1A2

Revision:
15:ed4d4b4d379c
Parent:
10:2da1507fa8c2
diff -r ea0cff180cd1 -r ed4d4b4d379c main.cpp
--- a/main.cpp	Thu Jun 24 12:47:27 2021 +0000
+++ b/main.cpp	Mon Jul 26 16:22:00 2021 +0000
@@ -60,7 +60,6 @@
 
 #if (MBED_VERSION  > 60300)
 UnbufferedSerial  pc(USBTX, USBRX);
-extern "C" void wait_ms(int ms);
 #else
 Serial pc(SERIAL_TX, SERIAL_RX);
 #endif
@@ -87,23 +86,27 @@
 }
 
 /* Start the sensor ranging */
-int init_sensor()
+int configure_sensor()
 {
     int status = 0;
-
-    Dev=&devCentre;
-    Sensor=board->sensor_centre;
+    VL53L1_DeviceInfo_t device_info;
 
-    // configure the sensors
-    Dev->comms_speed_khz = 400;
-    Dev->comms_type = 1;
-    Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS;
+    Dev = &devCentre;
+    Sensor = board->sensor_centre;
+
+    if (Sensor == NULL)
+        return -1;
 
     printf("configuring centre channel \n");
 
-    /* Device Initialization and setting */
-    status = Sensor->VL53L1CB_DataInit();
-    status = Sensor->VL53L1CB_StaticInit();
+    status = Sensor->VL53L1CB_GetDeviceInfo(&device_info);
+    if (status != 0) {
+        return status;
+    }
+    printf("device name %s \n",device_info.Name);
+    printf("device type %s \n",device_info.Type);
+    printf("device productID %s \n",device_info.ProductId);
+    printf("device productType %x \n",device_info.ProductType);
 
     status = Sensor->VL53L1CB_SetPresetMode(VL53L1_PRESETMODE_AUTONOMOUS);
     status = Sensor->VL53L1CB_SetDistanceMode(VL53L1_DISTANCEMODE_LONG);
@@ -115,19 +118,6 @@
     status = Sensor->VL53L1CB_SetSequenceStepEnable(VL53L1_SEQUENCESTEP_MM1, 0);
     status = Sensor->VL53L1CB_SetSequenceStepEnable(VL53L1_SEQUENCESTEP_MM2, 0);
 
-
-    // create interrupt handler and start measurements
-    if (board->sensor_centre!= NULL) {
-        status = board->sensor_centre->stop_measurement();
-        if (status != 0) {
-            return status;
-        }
-
-        status = Sensor->VL53L1CB_StartMeasurement();
-        if (status != 0) {
-            return status;
-        }
-    }
     return status;
 }
 
@@ -163,31 +153,30 @@
 
     printf("board initiated! - %d\r\n", status);
 
-    /* init an array with chars to id the sensors */
-    status = init_sensor();
+    status = configure_sensor();
     if (status != 0) {
         printf("Failed to init sensors!\r\n");
         return status;
     }
 
+    // start measurements
+    status = board->sensor_centre->VL53L1CB_StartMeasurement();
+    if (status != 0) {
+        return status;
+    }
+
     printf("loop forever\n");
 
     VL53L1_MultiRangingData_t MultiRangingData;
-    VL53L1_MultiRangingData_t *pMultiRangingData = NULL;
+    VL53L1_MultiRangingData_t *pMultiRangingData = &MultiRangingData;
 
     while (true) {
-        pMultiRangingData = &MultiRangingData;
+        status = board->sensor_centre->VL53L1CB_WaitMeasurementDataReady();
+        status = board->sensor_centre->VL53L1CB_GetMultiRangingData(pMultiRangingData);
 
-        status = board->sensor_centre->VL53L1CB_WaitMeasurementDataReady();
-//        if (int_sensor) {
-//            int_sensor = false;
-        status = board->sensor_centre->VL53L1CB_GetMultiRangingData( pMultiRangingData);
+        print_results(devCentre.i2c_slave_address, pMultiRangingData);
 
-        print_results( devCentre.i2c_slave_address, pMultiRangingData );
-
-        status = board->sensor_centre->VL53L1CB_ClearInterrupt();
-//                board->sensor_centre->enable_interrupt_measure_detection_irq();
-//        }
+        status = board->sensor_centre->VL53L1CB_ClearInterruptAndStartMeasurement();
     }
 
     printf("Terminating.\n");
@@ -198,44 +187,26 @@
 void print_results( int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData )
 {
     int no_of_object_found = pMultiRangingData->NumberOfObjectsFound;
-    int signal_rate = 0;
-    int ambient_rate = 0;
+    int signal_kcps = 0;
+    int ambient_kcps = 0;
 
     int RoiNumber = pMultiRangingData->RoiNumber;
 
-    if (( no_of_object_found < 10 ) &&  ( no_of_object_found != 0)) {
-        for(int j=0; j<no_of_object_found; j++) {
-            if ((pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) ||
-                    (pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL)) {
-                signal_rate = pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65535;
-                ambient_rate = pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65535;
-                printf("\t i2cAddr=%d \t RoiNumber=%d   \t status=%d, \t D=%5dmm, \t Signal=%d Mcps, \t Ambient=%d Mcps \n",
-                       devNumber, RoiNumber,
-                       pMultiRangingData->RangeData[j].RangeStatus,
-                       pMultiRangingData->RangeData[j].RangeMilliMeter,
-                       signal_rate,
-                       ambient_rate);
-/*
-// online compiler disables printf() / floating-point support, for code-size reasons.                        
-// offline compiler can switch it on.
-                printf("\t i2cAddr=%d \t RoiNumber=%d   \t status=%d, \t D=%5dmm, \t Signal=%2.2f Mcps, \t Ambient=%2.2f Mcps \n",
-                       devNumber, RoiNumber,
-                       pMultiRangingData->RangeData[j].RangeStatus,
-                       pMultiRangingData->RangeData[j].RangeMilliMeter,
-                       pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65535.0,
-                       pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65535.0);
-*/                       
-            }
-        }
-    } // if (( no_of_object_found < 10 ) &&  ( no_of_object_found != 0))
+    if (no_of_object_found <= 1)
+        no_of_object_found = 1;
+    for(int j=0; j<no_of_object_found; j++) {
+        signal_kcps = 1000*(pMultiRangingData->RangeData[j].SignalRateRtnMegaCps) / 65536;
+        ambient_kcps = 1000*(pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps) / 65536;
+        if (j > 0)
+            printf("\t\t\t");
+        printf("\trange[%d] status=%d, \t D=%5dmm, \t Signal=%d Kcps, \t Ambient=%d Kcps \n",
+               j,
+               pMultiRangingData->RangeData[j].RangeStatus,
+               pMultiRangingData->RangeData[j].RangeMilliMeter,
+               signal_kcps,
+               ambient_kcps);
+    }
 }
 
 
-#if (MBED_VERSION  > 60300)
-extern "C" void wait_ms(int ms)
-{
-    thread_sleep_for(ms);
-}
-#endif
 
-