Example of interrupt ranging using VL53L1X satellite sensor board

Dependencies:   mbed X_NUCLEO_53L1A1_mbed

Files at this revision

API Documentation at this revision

Comitter:
dmathew
Date:
Thu May 30 11:39:36 2019 +0000
Parent:
20:56355fae478b
Commit message:
Added ranging mode to allow choice between polling and interrupt ranging

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 56355fae478b -r a225fcaf0228 main.cpp
--- a/main.cpp	Tue May 28 08:16:57 2019 +0000
+++ b/main.cpp	Thu May 30 11:39:36 2019 +0000
@@ -36,6 +36,9 @@
 #define VL53L1_I2C_SDA   D14
 #define VL53L1_I2C_SCL   D15
 
+#define INTERRUPT_MODE 0
+#define POLLING_MODE   1
+
 static VL53L1X *sensor = NULL;
 Serial pc(SERIAL_TX, SERIAL_RX);
 
@@ -76,7 +79,7 @@
     return status;
 }
 
-int range_measure(vl53L1X_DevI2C *device_i2c)
+int interrupt_range_measure(vl53L1X_DevI2C *device_i2c)
 {
     int status = 0;
     uint16_t distance = 0;
@@ -86,7 +89,6 @@
     /* create instance of sensor class */
     sensor = new VL53L1X(device_i2c, &xshutdown, A2);
 
-    sensor->VL53L1_Off();
     /* initialise sensor */
     sensor->InitSensor(0x52);
 
@@ -124,15 +126,76 @@
 
 }
 
+int polling_range_measure(vl53L1X_DevI2C *device_i2c)
+{
+    int status = 0;
+    uint8_t ready = 0;
+    uint16_t distance = 0;
+    /* Create a xshutdown pin */
+    DigitalOut xshutdown(D7);
+
+    /* create instance of sensor class */
+    sensor = new VL53L1X(device_i2c, &xshutdown, A2);
+
+    /* initialise sensor */
+    sensor->InitSensor(0x52);
+
+    if (status) {
+        delete sensor;
+        sensor= NULL;
+        printf("Sensor centre not present\n\r");
+    }
+
+    /* init an array with chars to id the sensors */
+    status = start_ranging();
+    if (status != 0) {
+        printf("Failed to start ranging!\r\n");
+        return status;
+    }
+
+    if (NULL != sensor) {
+        printf("Entering loop mode\r\n");
+        /* Main ranging interrupt loop */
+        while (true) {
+                status = sensor->VL53L1X_CheckForDataReady(&ready);
+                if (ready) {
+                    sensor->VL53L1X_GetDistance(&distance);
+                    printf("distance: %d\r\n", distance);
+             }
+
+            if (int_stop) {
+                printf("\r\nEnding loop mode \r\n");
+                break;
+            }
+        }
+    }
+
+    return status;
+}
 
 /*=================================== Main ==================================
 =============================================================================*/
 int main()
 {
+    int range_mode = 0;
 #if USER_BUTTON==PC_13  // we are cross compiling for Nucleo-f401
     InterruptIn stop_button(USER_BUTTON);
     stop_button.rise(&measuring_stop_irq);
 #endif
     vl53L1X_DevI2C *device_i2c = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL);
-    range_measure(device_i2c);  // start continuous measures
+    
+    /* set range_mode to either polling or interrupt */
+    range_mode = POLLING_MODE;
+    
+    switch (range_mode) {
+        case INTERRUPT_MODE:
+            interrupt_range_measure(device_i2c);  // start continuous measures
+            break;
+        case POLLING_MODE:
+            polling_range_measure(device_i2c);
+            break;
+        default:
+            printf("Invalid range mode\n\r");
+            break;
+    }
 }