A sample program to control one VL53L1 ToF sensor in multizone mode using polling to find out if a measurement is available. Mbed V6.3 but will run any MBed version by dropping replacing this one. Maint6 release.

Dependencies:   X_NUCLEO_53L1A2

Revision:
11:eafe2b5c130e
Parent:
10:17d61466185f
Child:
12:3dd44d06629c
--- a/main.cpp	Thu May 20 10:48:18 2021 +0000
+++ b/main.cpp	Fri May 21 09:08:32 2021 +0000
@@ -1,19 +1,32 @@
 /*
  * This VL53L1CB Expansion board test application performs range measurements
- * using the onboard embedded sensor, in multizone, polling mode.
- * Measured ranges are ouput on the Serial Port, running at 115200 baud.
+ * using the onboard embedded sensor, in polling mode.
+ * Measured ranges are output on the Serial Port, running at 115200 baud.
  *
- *
- * This is designed to work with MBed v2, & MBedOS v5 / v6.
- *
+ * This is designed to work with MBed v2.x, & MBedOS v5.x / v6.x.
  *
  * The Reset button can be used to restart the program.
  *
- * *** Note :
- * Default Mbed build system settings disable print floating-point support.
- * Offline builds can enable this, again.
- * https://github.com/ARMmbed/mbed-os/blob/master/platform/source/minimal-printf/README.md
- * .\mbed-os\platform\mbed_lib.json
+ * *** NOTE :
+ *     Default Mbed build system settings disable printf() floating-point support.
+ *     Offline builds can enable this, again.
+ *     https://github.com/ARMmbed/mbed-os/blob/master/platform/source/minimal-printf/README.md
+ *     .\mbed-os\platform\mbed_lib.json
+ *
+ * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of
+ *            the X-NUCELO-53L1A1 expansion board are not made/OFF.
+ *            These links must be made to allow interrupts from the Satellite boards
+ *            to be received.
+ *            U11 and U18 must be made/ON to allow interrupts to be received from the
+ *            INT_L & INT_R positions; or
+ *            U10 and U15 must be made/ON to allow interrupts to be received from the
+ *            Alternate INT_L & INT_R positions.
+ *            The X_NUCLEO_53L1A2 library defaults to use the INT_L/INT_R positions.
+ *            INT_L is available on expansion board Arduino Connector CN5, pin 1 as D8.
+ *            Alternate INT_L is on CN5 Connector pin 2 as D9.
+ *            INT_R is available on expansion board Arduino Connector CN9, pin 3 as D2.
+ *            Alternate INT_R is on CN9 Connector pin 5 as D4.
+ *            The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A2/
  *
  */
 
@@ -21,17 +34,30 @@
 #include <time.h>
 
 #include "mbed.h"
+
 #include "XNucleo53L1A2.h"
 #include "ToF_I2C.h"
 
-
-// define the i2c comms pins
+// i2c comms port pins
 #define I2C_SDA   D14
 #define I2C_SCL   D15
 
 
+#define NUM_SENSORS 3
+
+// define interrupt pins
+PinName CentreIntPin = A2;
+// the satellite pins depend on solder blobs on the back of the shield.
+// they may not exist or may be one of two sets.
+// the centre pin always exists
+//PinName LeftIntPin = D8;
+PinName RightIntPin = D2;
+// alternate set
+PinName LeftIntPin = D9;
+//PinName RightIntPin = D4;
 
 static XNucleo53L1A2 *board=NULL;
+
 #if (MBED_VERSION  > 60300)
 UnbufferedSerial  pc(USBTX, USBRX);
 extern "C" void wait_ms(int ms);
@@ -39,32 +65,109 @@
 Serial pc(SERIAL_TX, SERIAL_RX);
 #endif
 
-void process_interrupt( VL53L1 * sensor,VL53L1_DEV dev );
-void print_results( int devSpiNumber, VL53L1_MultiRangingData_t *pMultiRangingData );
+void print_results(int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData );
+
+
+VL53L1_Dev_t devCentre;
+VL53L1_DEV Dev = &devCentre;
+
+VL53L1 *Sensor;
+
+
+
+/* flags that handle interrupt request for sensor and user blue button*/
+volatile bool int_sensor = false;
+volatile bool int_stop = false;
+
+/* ISR callback function of the centre sensor */
+void sensor_irq(void)
+{
+    int_sensor = true;
+    board->sensor_centre->disable_interrupt_measure_detection_irq();
+}
+
+/* Start the sensor ranging */
+int init_sensor()
+{
+    int status = 0;
+
+    Dev=&devCentre;
+    Sensor=board->sensor_centre;
+
+    // configure the sensors
+    Dev->comms_speed_khz = 400;
+    Dev->comms_type = 1;
+    Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS;
+
+    printf("configuring centre channel \n");
+
+    /* Device Initialization and setting */
+    status = Sensor->vl53L1_DataInit();
+    status = Sensor->vl53L1_StaticInit();
+
+    VL53L1_RoiConfig_t RoiConfig;
+
+    RoiConfig.NumberOfRoi = 3;
 
-VL53L1_Dev_t                   devCentre;
-VL53L1_Dev_t                   devLeft;
-VL53L1_Dev_t                   devRight;
-VL53L1_DEV                     Dev = &devCentre;
+    RoiConfig.UserRois[0].TopLeftX = 3;
+    RoiConfig.UserRois[0].TopLeftY = 10;
+    RoiConfig.UserRois[0].BotRightX = 10;
+    RoiConfig.UserRois[0].BotRightY = 3;
+
+    RoiConfig.UserRois[1].TopLeftX = 5;
+    RoiConfig.UserRois[1].TopLeftY = 12;
+    RoiConfig.UserRois[1].BotRightX = 12;
+    RoiConfig.UserRois[1].BotRightY = 5;
+
+    RoiConfig.UserRois[2].TopLeftX = 6;
+    RoiConfig.UserRois[2].TopLeftY = 13;
+    RoiConfig.UserRois[2].BotRightX = 13;
+    RoiConfig.UserRois[2].BotRightY = 6;
+
+    status = Sensor->vl53L1_SetPresetMode(VL53L1_PRESETMODE_MULTIZONES_SCANNING);
+    status = Sensor->vl53L1_SetDistanceMode(VL53L1_DISTANCEMODE_LONG);
+
+    status = Sensor->vl53L1_SetROI(&RoiConfig);
+
+    status = Sensor->vl53L1_SetMeasurementTimingBudgetMicroSeconds(17000);
 
 
+    // create interrupt handler and start measurements
+    if (board->sensor_centre!= NULL) {
+        status = board->sensor_centre->stop_measurement();
+        if (status != 0) {
+            return status;
+        }
+
+        status = Sensor->vl53L1_StartMeasurement();
+        if (status != 0) {
+            return status;
+        }
+    }
+    return status;
+}
+
+/* ISR callback function of the user blue button to switch measuring sensor. */
+void measuring_stop_irq(void)
+{
+    int_stop = true;
+}
+
 /*=================================== Main ==================================
 =============================================================================*/
 int main()
 {
     int status;
-    VL53L1 * Sensor;
-    uint16_t wordData;
-
+    uint16_t distance = 0;
 
     pc.baud(115200);  // baud rate is important as printf statements take a lot of time
-    printf("Polling single multizone mbed = %d \r\n",MBED_VERSION);
+
+    printf("mbed version : %d \r\n",MBED_VERSION);
 
 // create i2c interface
     ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL);
-
-    /* creates the 53L1A1 expansion board singleton obj */
-    board = XNucleo53L1A2::instance(dev_I2C, A2, D8, D2);
+    /* creates the 53L1A2 expansion board singleton obj */
+    board = XNucleo53L1A2::instance(dev_I2C, CentreIntPin, LeftIntPin, RightIntPin);    
 
     printf("board created!\r\n");
 
@@ -72,113 +175,69 @@
     status = board->init_board();
     if (status) {
         printf("Failed to init board!\r\n");
-        return 0;
+        return status;
     }
 
     printf("board initiated! - %d\r\n", status);
 
-    // create the sensor controller classes
-    Dev=&devCentre;
-    Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS;
-    // configure the sensors
-    Dev->comms_speed_khz = 400;
-    Dev->comms_type = 1;
-
-    Sensor=board->sensor_centre;
-
-    printf("configuring centre channel \n");
-
+    /* init an array with chars to id the sensors */
+    status = init_sensor();
+    if (status != 0) {
+        printf("Failed to init sensors!\r\n");
+        return status;
+    }
 
-    Sensor->VL53L1_RdWord(Dev, 0x01, &wordData);
-    printf("VL53L1X: %02X   %d\n\r", wordData,Dev->i2c_slave_address);
-    /* Device Initialization and setting */
-    status = Sensor->vl53L1_DataInit();
-    status = Sensor->vl53L1_StaticInit();
-
-    status = Sensor->vl53L1_SetPresetMode(VL53L1_PRESETMODE_MULTIZONES_SCANNING);
-
-    status = Sensor->vl53L1_SetDistanceMode(VL53L1_DISTANCEMODE_LONG);
+    printf("loop forever\n");
 
-
-    VL53L1_RoiConfig_t roiConfig;
-
-    roiConfig.NumberOfRoi =3;
-
-    roiConfig.UserRois[0].TopLeftX = 3;
-    roiConfig.UserRois[0].TopLeftY = 10;
-    roiConfig.UserRois[0].BotRightX = 10;
-    roiConfig.UserRois[0].BotRightY = 3;
+    VL53L1_MultiRangingData_t MultiRangingData;
+    VL53L1_MultiRangingData_t *pMultiRangingData = NULL;
 
-    roiConfig.UserRois[1].TopLeftX = 5;
-    roiConfig.UserRois[1].TopLeftY = 12;
-    roiConfig.UserRois[1].BotRightX = 12;
-    roiConfig.UserRois[1].BotRightY = 5;
+    while (true) {
+        pMultiRangingData = &MultiRangingData;
 
-    roiConfig.UserRois[2].TopLeftX = 6;
-    roiConfig.UserRois[2].TopLeftY = 13;
-    roiConfig.UserRois[2].BotRightX = 13;
-    roiConfig.UserRois[2].BotRightY = 6;
-
-    status = Sensor->vl53L1_SetROI( &roiConfig);
-
-    printf("VL53L1_SetROI %d \n",status);
-
-    status = board->sensor_centre->vl53L1_StartMeasurement();
+        status = board->sensor_centre->vl53L1_WaitMeasurementDataReady();
+        status = board->sensor_centre->vl53L1_GetMultiRangingData( pMultiRangingData);
 
-    // looping polling for results
-    while (true) {
-        VL53L1_MultiRangingData_t MultiRangingData;
-        VL53L1_MultiRangingData_t *pMultiRangingData = &MultiRangingData;
+        print_results( devCentre.i2c_slave_address, pMultiRangingData );
 
-        // wait for result
-        status = board->sensor_centre->vl53L1_WaitMeasurementDataReady();
-        // get the result
-        status = board->sensor_centre->vl53L1_GetMultiRangingData( pMultiRangingData);
-        // if valid, print it
-        if (status == 0)
-            print_results(devCentre.i2c_slave_address, pMultiRangingData );
-        else
-            printf("VL53L1_GetMultiRangingData centre %d \n",status);
+        status = board->sensor_centre->VL53L1_ClearInterrupt();
+    }
 
-        if (status == 0)
-            status = board->sensor_centre->vl53L1_ClearInterruptAndStartMeasurement();
-    }
+    printf("Terminating.\n");
 }
 
 
-
-// prints the range result seen above
-void print_results( int devSpiNumber, VL53L1_MultiRangingData_t *pMultiRangingData )
+// print what ever results are required
+void print_results( int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData )
 {
-    int no_of_object_found=pMultiRangingData->NumberOfObjectsFound;
+    int no_of_object_found = pMultiRangingData->NumberOfObjectsFound;
+    int signal_rate = 0;
+    int ambient_rate = 0;
 
-    int RoiNumber=pMultiRangingData->RoiNumber;
-//            int RoiStatus=pMultiRangingData->RoiStatus;
-
-    if (( no_of_object_found < 10 ) &&  ( no_of_object_found != 0)) {
+    int RoiNumber = pMultiRangingData->RoiNumber;
 
-        //              printf("MZI Count=%5d, ", pMultiRangingData->StreamCount);
-        //             printf("RoiNumber%1d, ", RoiNumber);
-        //               printf("RoiStatus=%1d, \n", RoiStatus);
-        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)) {
-                printf("*****************\t i2cAddr=0x%x \t RoiNumber=%d   \t status=%d, \t D=%5dmm, \t Signal=%2.2f Mcps, \t Ambient=%2.2f Mcps \n",
-                       devSpiNumber,
-                       RoiNumber,
-                       pMultiRangingData->RangeData[j].RangeStatus,
-                       pMultiRangingData->RangeData[j].RangeMilliMeter,
-                       pMultiRangingData->RangeData[j].SignalRateRtnMegaCps/65536.0,
-                       pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps/65536.0);
-            } else {
-//                               printf("RangeStatus %d %d\n",j, pMultiRangingData->RangeData[j].RangeStatus);
-            }
-        }
-    } // if (( no_of_object_found < 10 ) &&  ( no_of_object_found != 0))
-    else {
-//                       printf("no_of_object_found %d \n",no_of_object_found);
+    if (no_of_object_found <= 1)
+        no_of_object_found = 1;
+    for(int j=0; j<no_of_object_found; j++) {
+            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);
+*/                       
     }
-
 }