Ranging using a VL53L1X Time-of-Flight (ToF) sensor Satellite board wired directly to an Host microcontroller board (eg, an STM32-Nucleo). No X-NUCLEO-53L1A1 expansion board required.
Dependencies: mbed X_NUCLEO_53L1A1_mbed
main.cpp@20:56355fae478b, 2019-05-28 (annotated)
- Committer:
- dmathew
- Date:
- Tue May 28 08:16:57 2019 +0000
- Revision:
- 20:56355fae478b
- Parent:
- 19:29699fbc39b8
- Child:
- 21:b4af3bca077e
Example of interrupt ranging using only VL53L1X satellite board
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dmathew | 17:e7a6fd6d3c97 | 1 | /* |
dmathew | 20:56355fae478b | 2 | * This VL53L1X satellite board sample application performs range measurements |
johnAlexander | 19:29699fbc39b8 | 3 | * with interrupts enabled to generate a hardware interrupt each time a new |
johnAlexander | 19:29699fbc39b8 | 4 | * measurement is ready to be read. |
dmathew | 17:e7a6fd6d3c97 | 5 | * |
dmathew | 20:56355fae478b | 6 | * Measured ranges are output on the Serial Port, running at 115200 baud. |
dmathew | 17:e7a6fd6d3c97 | 7 | * |
dmathew | 20:56355fae478b | 8 | * The User Blue button stops the current measurement and entire program, |
dmathew | 20:56355fae478b | 9 | * releasing all resources. |
dmathew | 17:e7a6fd6d3c97 | 10 | * |
dmathew | 20:56355fae478b | 11 | * The Black Reset button is used to restart the program. |
johnAlexander | 19:29699fbc39b8 | 12 | * |
dmathew | 20:56355fae478b | 13 | * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of |
dmathew | 17:e7a6fd6d3c97 | 14 | * the X-NUCELO-53L0A1 expansion board are not made/OFF. |
dmathew | 17:e7a6fd6d3c97 | 15 | * These links must be made to allow interrupts from the Satellite boards |
dmathew | 17:e7a6fd6d3c97 | 16 | * to be received. |
dmathew | 17:e7a6fd6d3c97 | 17 | * U11 and U18 must be made/ON to allow interrupts to be received from the |
dmathew | 17:e7a6fd6d3c97 | 18 | * INT_L & INT_R positions; or |
dmathew | 17:e7a6fd6d3c97 | 19 | * U10 and U15 must be made/ON to allow interrupts to be received from the |
dmathew | 17:e7a6fd6d3c97 | 20 | * Alternate INT_L & INT_R positions. |
dmathew | 17:e7a6fd6d3c97 | 21 | * The X_NUCLEO_53L1A1 firmware library defaults to use the INT_L/INT_R |
dmathew | 17:e7a6fd6d3c97 | 22 | * positions. |
dmathew | 17:e7a6fd6d3c97 | 23 | * INT_L is available on expansion board Arduino Connector CN5, pin 1 as D9. |
dmathew | 17:e7a6fd6d3c97 | 24 | * Alternate INT_L is on CN5 Connector pin 2 as D8. |
dmathew | 17:e7a6fd6d3c97 | 25 | * INT_R is available on expansion board Arduino Connector CN9, pin 3 as D4. |
dmathew | 17:e7a6fd6d3c97 | 26 | * Alternate INT_R is on CN9 Connector pin 5 as D2. |
dmathew | 17:e7a6fd6d3c97 | 27 | * The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A1/ |
dmathew | 17:e7a6fd6d3c97 | 28 | */ |
dmathew | 20:56355fae478b | 29 | |
dmathew | 17:e7a6fd6d3c97 | 30 | #include <stdio.h> |
dmathew | 17:e7a6fd6d3c97 | 31 | |
johnAlexander | 0:ce8359133ae6 | 32 | #include "mbed.h" |
JerrySzczurak | 14:d3904b05aad6 | 33 | #include "vl53L1x_I2c.h" |
dmathew | 20:56355fae478b | 34 | #include "vl53l1x_class.h" |
johnAlexander | 19:29699fbc39b8 | 35 | |
dmathew | 20:56355fae478b | 36 | #define VL53L1_I2C_SDA D14 |
dmathew | 20:56355fae478b | 37 | #define VL53L1_I2C_SCL D15 |
johnAlexander | 19:29699fbc39b8 | 38 | |
dmathew | 20:56355fae478b | 39 | static VL53L1X *sensor = NULL; |
dmathew | 20:56355fae478b | 40 | Serial pc(SERIAL_TX, SERIAL_RX); |
dmathew | 17:e7a6fd6d3c97 | 41 | |
dmathew | 20:56355fae478b | 42 | /* flags that handle interrupt request for sensor and user blue button*/ |
dmathew | 20:56355fae478b | 43 | volatile bool int_sensor = false; |
dmathew | 20:56355fae478b | 44 | volatile bool int_stop = false; |
johnAlexander | 19:29699fbc39b8 | 45 | |
johnAlexander | 19:29699fbc39b8 | 46 | |
dmathew | 20:56355fae478b | 47 | /* ISR callback function of the sensor */ |
dmathew | 20:56355fae478b | 48 | void sensor_irq(void) |
johnAlexander | 19:29699fbc39b8 | 49 | { |
dmathew | 20:56355fae478b | 50 | int_sensor = true; |
dmathew | 20:56355fae478b | 51 | sensor->disable_interrupt_measure_detection_irq(); |
johnAlexander | 19:29699fbc39b8 | 52 | } |
johnAlexander | 19:29699fbc39b8 | 53 | |
johnAlexander | 19:29699fbc39b8 | 54 | /* ISR callback function of the user blue button to switch measuring sensor. */ |
dmathew | 20:56355fae478b | 55 | void measuring_stop_irq(void) |
dmathew | 20:56355fae478b | 56 | { |
dmathew | 20:56355fae478b | 57 | int_stop = true; |
dmathew | 20:56355fae478b | 58 | } |
dmathew | 20:56355fae478b | 59 | |
dmathew | 20:56355fae478b | 60 | /* Start the sensor ranging */ |
dmathew | 20:56355fae478b | 61 | int start_ranging() |
johnAlexander | 19:29699fbc39b8 | 62 | { |
dmathew | 20:56355fae478b | 63 | int status = 0; |
dmathew | 20:56355fae478b | 64 | /* start the measure on the sensor */ |
dmathew | 20:56355fae478b | 65 | if (NULL != sensor) { |
dmathew | 20:56355fae478b | 66 | status = sensor->stop_measurement(); |
dmathew | 20:56355fae478b | 67 | if (status != 0) { |
dmathew | 20:56355fae478b | 68 | return status; |
dmathew | 20:56355fae478b | 69 | } |
dmathew | 20:56355fae478b | 70 | |
dmathew | 20:56355fae478b | 71 | status = sensor->start_measurement(&sensor_irq); |
dmathew | 20:56355fae478b | 72 | if (status != 0) { |
dmathew | 20:56355fae478b | 73 | return status; |
dmathew | 20:56355fae478b | 74 | } |
dmathew | 20:56355fae478b | 75 | } |
dmathew | 20:56355fae478b | 76 | return status; |
johnAlexander | 19:29699fbc39b8 | 77 | } |
dmathew | 17:e7a6fd6d3c97 | 78 | |
dmathew | 20:56355fae478b | 79 | int range_measure(vl53L1X_DevI2C *device_i2c) |
dmathew | 17:e7a6fd6d3c97 | 80 | { |
dmathew | 20:56355fae478b | 81 | int status = 0; |
johnAlexander | 19:29699fbc39b8 | 82 | uint16_t distance = 0; |
dmathew | 20:56355fae478b | 83 | /* Create a xshutdown pin */ |
dmathew | 20:56355fae478b | 84 | DigitalOut xshutdown(D7); |
dmathew | 17:e7a6fd6d3c97 | 85 | |
dmathew | 20:56355fae478b | 86 | /* create instance of sensor class */ |
dmathew | 20:56355fae478b | 87 | sensor = new VL53L1X(device_i2c, &xshutdown, A2); |
dmathew | 20:56355fae478b | 88 | |
dmathew | 20:56355fae478b | 89 | sensor->VL53L1_Off(); |
dmathew | 20:56355fae478b | 90 | /* initialise sensor */ |
dmathew | 20:56355fae478b | 91 | sensor->InitSensor(0x52); |
dmathew | 20:56355fae478b | 92 | |
dmathew | 20:56355fae478b | 93 | if (status) { |
dmathew | 20:56355fae478b | 94 | delete sensor; |
dmathew | 20:56355fae478b | 95 | sensor= NULL; |
dmathew | 20:56355fae478b | 96 | printf("Sensor centre not present\n\r"); |
johnAlexander | 19:29699fbc39b8 | 97 | } |
johnAlexander | 19:29699fbc39b8 | 98 | |
dmathew | 20:56355fae478b | 99 | /* init an array with chars to id the sensors */ |
dmathew | 20:56355fae478b | 100 | status = start_ranging(); |
dmathew | 20:56355fae478b | 101 | if (status != 0) { |
dmathew | 20:56355fae478b | 102 | printf("Failed to start ranging!\r\n"); |
dmathew | 20:56355fae478b | 103 | return status; |
dmathew | 20:56355fae478b | 104 | } |
dmathew | 20:56355fae478b | 105 | |
dmathew | 20:56355fae478b | 106 | if (NULL != sensor) { |
dmathew | 20:56355fae478b | 107 | printf("Entering loop mode\r\n"); |
dmathew | 20:56355fae478b | 108 | /* Main ranging interrupt loop */ |
dmathew | 20:56355fae478b | 109 | while (true) { |
dmathew | 20:56355fae478b | 110 | if (int_sensor) { |
dmathew | 20:56355fae478b | 111 | int_sensor = false; |
dmathew | 20:56355fae478b | 112 | status = sensor->handle_irq(&distance); |
dmathew | 20:56355fae478b | 113 | printf("distance: %d\r\n", distance); |
dmathew | 20:56355fae478b | 114 | } |
dmathew | 20:56355fae478b | 115 | |
dmathew | 20:56355fae478b | 116 | if (int_stop) { |
dmathew | 20:56355fae478b | 117 | printf("\r\nEnding loop mode \r\n"); |
dmathew | 20:56355fae478b | 118 | break; |
dmathew | 20:56355fae478b | 119 | } |
johnAlexander | 19:29699fbc39b8 | 120 | } |
johnAlexander | 19:29699fbc39b8 | 121 | } |
johnAlexander | 19:29699fbc39b8 | 122 | |
dmathew | 20:56355fae478b | 123 | return status; |
dmathew | 20:56355fae478b | 124 | |
johnAlexander | 19:29699fbc39b8 | 125 | } |
johnAlexander | 19:29699fbc39b8 | 126 | |
johnAlexander | 19:29699fbc39b8 | 127 | |
johnAlexander | 19:29699fbc39b8 | 128 | /*=================================== Main ================================== |
johnAlexander | 19:29699fbc39b8 | 129 | =============================================================================*/ |
johnAlexander | 19:29699fbc39b8 | 130 | int main() |
johnAlexander | 19:29699fbc39b8 | 131 | { |
johnAlexander | 19:29699fbc39b8 | 132 | #if USER_BUTTON==PC_13 // we are cross compiling for Nucleo-f401 |
dmathew | 20:56355fae478b | 133 | InterruptIn stop_button(USER_BUTTON); |
dmathew | 20:56355fae478b | 134 | stop_button.rise(&measuring_stop_irq); |
johnAlexander | 19:29699fbc39b8 | 135 | #endif |
dmathew | 20:56355fae478b | 136 | vl53L1X_DevI2C *device_i2c = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL); |
dmathew | 20:56355fae478b | 137 | range_measure(device_i2c); // start continuous measures |
johnAlexander | 19:29699fbc39b8 | 138 | } |