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@22:eda6c35d4226, 2019-07-24 (annotated)
- Committer:
- johnAlexander
- Date:
- Wed Jul 24 12:42:54 2019 +0000
- Revision:
- 22:eda6c35d4226
- Parent:
- 21:b4af3bca077e
Refactored init_sensor
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 | * |
johnAlexander | 21:b4af3bca077e | 8 | * On STM32-Nucleo boards : |
johnAlexander | 21:b4af3bca077e | 9 | * The User Blue button stops the current measurement and entire program, |
johnAlexander | 21:b4af3bca077e | 10 | * releasing all resources. |
dmathew | 17:e7a6fd6d3c97 | 11 | * |
johnAlexander | 21:b4af3bca077e | 12 | * The Black Reset button is used to restart the program. |
johnAlexander | 19:29699fbc39b8 | 13 | * |
dmathew | 20:56355fae478b | 14 | * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of |
johnAlexander | 21:b4af3bca077e | 15 | * the X-NUCELO-53L1A1 expansion board are not made/OFF. |
dmathew | 17:e7a6fd6d3c97 | 16 | * These links must be made to allow interrupts from the Satellite boards |
dmathew | 17:e7a6fd6d3c97 | 17 | * to be received. |
dmathew | 17:e7a6fd6d3c97 | 18 | * U11 and U18 must be made/ON to allow interrupts to be received from the |
dmathew | 17:e7a6fd6d3c97 | 19 | * INT_L & INT_R positions; or |
dmathew | 17:e7a6fd6d3c97 | 20 | * U10 and U15 must be made/ON to allow interrupts to be received from the |
dmathew | 17:e7a6fd6d3c97 | 21 | * Alternate INT_L & INT_R positions. |
dmathew | 17:e7a6fd6d3c97 | 22 | * The X_NUCLEO_53L1A1 firmware library defaults to use the INT_L/INT_R |
dmathew | 17:e7a6fd6d3c97 | 23 | * positions. |
dmathew | 17:e7a6fd6d3c97 | 24 | * INT_L is available on expansion board Arduino Connector CN5, pin 1 as D9. |
dmathew | 17:e7a6fd6d3c97 | 25 | * Alternate INT_L is on CN5 Connector pin 2 as D8. |
dmathew | 17:e7a6fd6d3c97 | 26 | * INT_R is available on expansion board Arduino Connector CN9, pin 3 as D4. |
dmathew | 17:e7a6fd6d3c97 | 27 | * Alternate INT_R is on CN9 Connector pin 5 as D2. |
dmathew | 17:e7a6fd6d3c97 | 28 | * The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A1/ |
dmathew | 17:e7a6fd6d3c97 | 29 | */ |
dmathew | 20:56355fae478b | 30 | |
dmathew | 17:e7a6fd6d3c97 | 31 | #include <stdio.h> |
dmathew | 17:e7a6fd6d3c97 | 32 | |
johnAlexander | 0:ce8359133ae6 | 33 | #include "mbed.h" |
johnAlexander | 22:eda6c35d4226 | 34 | #include "VL53L1X_I2C.h" |
johnAlexander | 22:eda6c35d4226 | 35 | #include "VL53L1X_Class.h" |
johnAlexander | 19:29699fbc39b8 | 36 | |
dmathew | 20:56355fae478b | 37 | #define VL53L1_I2C_SDA D14 |
dmathew | 20:56355fae478b | 38 | #define VL53L1_I2C_SCL D15 |
johnAlexander | 19:29699fbc39b8 | 39 | |
dmathew | 20:56355fae478b | 40 | static VL53L1X *sensor = NULL; |
johnAlexander | 21:b4af3bca077e | 41 | //Serial pc(SERIAL_TX, SERIAL_RX); |
dmathew | 17:e7a6fd6d3c97 | 42 | |
dmathew | 20:56355fae478b | 43 | /* flags that handle interrupt request for sensor and user blue button*/ |
dmathew | 20:56355fae478b | 44 | volatile bool int_sensor = false; |
dmathew | 20:56355fae478b | 45 | volatile bool int_stop = false; |
johnAlexander | 19:29699fbc39b8 | 46 | |
johnAlexander | 19:29699fbc39b8 | 47 | |
dmathew | 20:56355fae478b | 48 | /* ISR callback function of the sensor */ |
dmathew | 20:56355fae478b | 49 | void sensor_irq(void) |
johnAlexander | 19:29699fbc39b8 | 50 | { |
dmathew | 20:56355fae478b | 51 | int_sensor = true; |
dmathew | 20:56355fae478b | 52 | sensor->disable_interrupt_measure_detection_irq(); |
johnAlexander | 19:29699fbc39b8 | 53 | } |
johnAlexander | 19:29699fbc39b8 | 54 | |
johnAlexander | 19:29699fbc39b8 | 55 | /* ISR callback function of the user blue button to switch measuring sensor. */ |
dmathew | 20:56355fae478b | 56 | void measuring_stop_irq(void) |
dmathew | 20:56355fae478b | 57 | { |
dmathew | 20:56355fae478b | 58 | int_stop = true; |
dmathew | 20:56355fae478b | 59 | } |
dmathew | 20:56355fae478b | 60 | |
dmathew | 20:56355fae478b | 61 | /* Start the sensor ranging */ |
dmathew | 20:56355fae478b | 62 | int start_ranging() |
johnAlexander | 19:29699fbc39b8 | 63 | { |
dmathew | 20:56355fae478b | 64 | int status = 0; |
dmathew | 20:56355fae478b | 65 | /* start the measure on the sensor */ |
dmathew | 20:56355fae478b | 66 | if (NULL != sensor) { |
dmathew | 20:56355fae478b | 67 | status = sensor->stop_measurement(); |
dmathew | 20:56355fae478b | 68 | if (status != 0) { |
dmathew | 20:56355fae478b | 69 | return status; |
dmathew | 20:56355fae478b | 70 | } |
dmathew | 20:56355fae478b | 71 | |
dmathew | 20:56355fae478b | 72 | status = sensor->start_measurement(&sensor_irq); |
dmathew | 20:56355fae478b | 73 | if (status != 0) { |
dmathew | 20:56355fae478b | 74 | return status; |
dmathew | 20:56355fae478b | 75 | } |
dmathew | 20:56355fae478b | 76 | } |
dmathew | 20:56355fae478b | 77 | return status; |
johnAlexander | 19:29699fbc39b8 | 78 | } |
dmathew | 17:e7a6fd6d3c97 | 79 | |
johnAlexander | 22:eda6c35d4226 | 80 | int range_measure(VL53L1X_DevI2C *device_i2c) |
dmathew | 17:e7a6fd6d3c97 | 81 | { |
dmathew | 20:56355fae478b | 82 | int status = 0; |
johnAlexander | 19:29699fbc39b8 | 83 | uint16_t distance = 0; |
dmathew | 20:56355fae478b | 84 | /* Create a xshutdown pin */ |
dmathew | 20:56355fae478b | 85 | DigitalOut xshutdown(D7); |
dmathew | 17:e7a6fd6d3c97 | 86 | |
dmathew | 20:56355fae478b | 87 | /* create instance of sensor class */ |
dmathew | 20:56355fae478b | 88 | sensor = new VL53L1X(device_i2c, &xshutdown, A2); |
dmathew | 20:56355fae478b | 89 | |
johnAlexander | 22:eda6c35d4226 | 90 | sensor->vl53l1_off(); |
dmathew | 20:56355fae478b | 91 | /* initialise sensor */ |
johnAlexander | 22:eda6c35d4226 | 92 | sensor->init_sensor(0x52); |
dmathew | 20:56355fae478b | 93 | |
dmathew | 20:56355fae478b | 94 | if (status) { |
dmathew | 20:56355fae478b | 95 | delete sensor; |
dmathew | 20:56355fae478b | 96 | sensor= NULL; |
dmathew | 20:56355fae478b | 97 | printf("Sensor centre not present\n\r"); |
johnAlexander | 19:29699fbc39b8 | 98 | } |
johnAlexander | 19:29699fbc39b8 | 99 | |
dmathew | 20:56355fae478b | 100 | /* init an array with chars to id the sensors */ |
dmathew | 20:56355fae478b | 101 | status = start_ranging(); |
dmathew | 20:56355fae478b | 102 | if (status != 0) { |
dmathew | 20:56355fae478b | 103 | printf("Failed to start ranging!\r\n"); |
dmathew | 20:56355fae478b | 104 | return status; |
dmathew | 20:56355fae478b | 105 | } |
dmathew | 20:56355fae478b | 106 | |
dmathew | 20:56355fae478b | 107 | if (NULL != sensor) { |
dmathew | 20:56355fae478b | 108 | printf("Entering loop mode\r\n"); |
dmathew | 20:56355fae478b | 109 | /* Main ranging interrupt loop */ |
dmathew | 20:56355fae478b | 110 | while (true) { |
dmathew | 20:56355fae478b | 111 | if (int_sensor) { |
dmathew | 20:56355fae478b | 112 | int_sensor = false; |
dmathew | 20:56355fae478b | 113 | status = sensor->handle_irq(&distance); |
dmathew | 20:56355fae478b | 114 | printf("distance: %d\r\n", distance); |
dmathew | 20:56355fae478b | 115 | } |
dmathew | 20:56355fae478b | 116 | |
dmathew | 20:56355fae478b | 117 | if (int_stop) { |
dmathew | 20:56355fae478b | 118 | printf("\r\nEnding loop mode \r\n"); |
dmathew | 20:56355fae478b | 119 | break; |
dmathew | 20:56355fae478b | 120 | } |
johnAlexander | 19:29699fbc39b8 | 121 | } |
johnAlexander | 19:29699fbc39b8 | 122 | } |
johnAlexander | 19:29699fbc39b8 | 123 | |
dmathew | 20:56355fae478b | 124 | return status; |
dmathew | 20:56355fae478b | 125 | |
johnAlexander | 19:29699fbc39b8 | 126 | } |
johnAlexander | 19:29699fbc39b8 | 127 | |
johnAlexander | 19:29699fbc39b8 | 128 | |
johnAlexander | 19:29699fbc39b8 | 129 | /*=================================== Main ================================== |
johnAlexander | 19:29699fbc39b8 | 130 | =============================================================================*/ |
johnAlexander | 19:29699fbc39b8 | 131 | int main() |
johnAlexander | 19:29699fbc39b8 | 132 | { |
johnAlexander | 21:b4af3bca077e | 133 | #if TARGET_STM // we are cross compiling for an STM32-Nucleo |
dmathew | 20:56355fae478b | 134 | InterruptIn stop_button(USER_BUTTON); |
dmathew | 20:56355fae478b | 135 | stop_button.rise(&measuring_stop_irq); |
johnAlexander | 19:29699fbc39b8 | 136 | #endif |
johnAlexander | 21:b4af3bca077e | 137 | #if TARGET_Freescale // we are cross-compiling for NXP FRDM boards. |
johnAlexander | 21:b4af3bca077e | 138 | InterruptIn stop_button(SW2); |
johnAlexander | 21:b4af3bca077e | 139 | stop_button.rise(&measuring_stop_irq); |
johnAlexander | 21:b4af3bca077e | 140 | #endif |
johnAlexander | 22:eda6c35d4226 | 141 | VL53L1X_DevI2C *device_i2c = new VL53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL); |
dmathew | 20:56355fae478b | 142 | range_measure(device_i2c); // start continuous measures |
johnAlexander | 19:29699fbc39b8 | 143 | } |