Example of interrupt ranging using VL53L1X satellite sensor board
Dependencies: mbed X_NUCLEO_53L1A1_mbed
main.cpp@21:a225fcaf0228, 2019-05-30 (annotated)
- Committer:
- dmathew
- Date:
- Thu May 30 11:39:36 2019 +0000
- Revision:
- 21:a225fcaf0228
- Parent:
- 20:56355fae478b
Added ranging mode to allow choice between polling and interrupt ranging
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 | 21:a225fcaf0228 | 39 | #define INTERRUPT_MODE 0 |
dmathew | 21:a225fcaf0228 | 40 | #define POLLING_MODE 1 |
dmathew | 21:a225fcaf0228 | 41 | |
dmathew | 20:56355fae478b | 42 | static VL53L1X *sensor = NULL; |
dmathew | 20:56355fae478b | 43 | Serial pc(SERIAL_TX, SERIAL_RX); |
dmathew | 17:e7a6fd6d3c97 | 44 | |
dmathew | 20:56355fae478b | 45 | /* flags that handle interrupt request for sensor and user blue button*/ |
dmathew | 20:56355fae478b | 46 | volatile bool int_sensor = false; |
dmathew | 20:56355fae478b | 47 | volatile bool int_stop = false; |
johnAlexander | 19:29699fbc39b8 | 48 | |
johnAlexander | 19:29699fbc39b8 | 49 | |
dmathew | 20:56355fae478b | 50 | /* ISR callback function of the sensor */ |
dmathew | 20:56355fae478b | 51 | void sensor_irq(void) |
johnAlexander | 19:29699fbc39b8 | 52 | { |
dmathew | 20:56355fae478b | 53 | int_sensor = true; |
dmathew | 20:56355fae478b | 54 | sensor->disable_interrupt_measure_detection_irq(); |
johnAlexander | 19:29699fbc39b8 | 55 | } |
johnAlexander | 19:29699fbc39b8 | 56 | |
johnAlexander | 19:29699fbc39b8 | 57 | /* ISR callback function of the user blue button to switch measuring sensor. */ |
dmathew | 20:56355fae478b | 58 | void measuring_stop_irq(void) |
dmathew | 20:56355fae478b | 59 | { |
dmathew | 20:56355fae478b | 60 | int_stop = true; |
dmathew | 20:56355fae478b | 61 | } |
dmathew | 20:56355fae478b | 62 | |
dmathew | 20:56355fae478b | 63 | /* Start the sensor ranging */ |
dmathew | 20:56355fae478b | 64 | int start_ranging() |
johnAlexander | 19:29699fbc39b8 | 65 | { |
dmathew | 20:56355fae478b | 66 | int status = 0; |
dmathew | 20:56355fae478b | 67 | /* start the measure on the sensor */ |
dmathew | 20:56355fae478b | 68 | if (NULL != sensor) { |
dmathew | 20:56355fae478b | 69 | status = sensor->stop_measurement(); |
dmathew | 20:56355fae478b | 70 | if (status != 0) { |
dmathew | 20:56355fae478b | 71 | return status; |
dmathew | 20:56355fae478b | 72 | } |
dmathew | 20:56355fae478b | 73 | |
dmathew | 20:56355fae478b | 74 | status = sensor->start_measurement(&sensor_irq); |
dmathew | 20:56355fae478b | 75 | if (status != 0) { |
dmathew | 20:56355fae478b | 76 | return status; |
dmathew | 20:56355fae478b | 77 | } |
dmathew | 20:56355fae478b | 78 | } |
dmathew | 20:56355fae478b | 79 | return status; |
johnAlexander | 19:29699fbc39b8 | 80 | } |
dmathew | 17:e7a6fd6d3c97 | 81 | |
dmathew | 21:a225fcaf0228 | 82 | int interrupt_range_measure(vl53L1X_DevI2C *device_i2c) |
dmathew | 17:e7a6fd6d3c97 | 83 | { |
dmathew | 20:56355fae478b | 84 | int status = 0; |
johnAlexander | 19:29699fbc39b8 | 85 | uint16_t distance = 0; |
dmathew | 20:56355fae478b | 86 | /* Create a xshutdown pin */ |
dmathew | 20:56355fae478b | 87 | DigitalOut xshutdown(D7); |
dmathew | 17:e7a6fd6d3c97 | 88 | |
dmathew | 20:56355fae478b | 89 | /* create instance of sensor class */ |
dmathew | 20:56355fae478b | 90 | sensor = new VL53L1X(device_i2c, &xshutdown, A2); |
dmathew | 20:56355fae478b | 91 | |
dmathew | 20:56355fae478b | 92 | /* initialise sensor */ |
dmathew | 20:56355fae478b | 93 | sensor->InitSensor(0x52); |
dmathew | 20:56355fae478b | 94 | |
dmathew | 20:56355fae478b | 95 | if (status) { |
dmathew | 20:56355fae478b | 96 | delete sensor; |
dmathew | 20:56355fae478b | 97 | sensor= NULL; |
dmathew | 20:56355fae478b | 98 | printf("Sensor centre not present\n\r"); |
johnAlexander | 19:29699fbc39b8 | 99 | } |
johnAlexander | 19:29699fbc39b8 | 100 | |
dmathew | 20:56355fae478b | 101 | /* init an array with chars to id the sensors */ |
dmathew | 20:56355fae478b | 102 | status = start_ranging(); |
dmathew | 20:56355fae478b | 103 | if (status != 0) { |
dmathew | 20:56355fae478b | 104 | printf("Failed to start ranging!\r\n"); |
dmathew | 20:56355fae478b | 105 | return status; |
dmathew | 20:56355fae478b | 106 | } |
dmathew | 20:56355fae478b | 107 | |
dmathew | 20:56355fae478b | 108 | if (NULL != sensor) { |
dmathew | 20:56355fae478b | 109 | printf("Entering loop mode\r\n"); |
dmathew | 20:56355fae478b | 110 | /* Main ranging interrupt loop */ |
dmathew | 20:56355fae478b | 111 | while (true) { |
dmathew | 20:56355fae478b | 112 | if (int_sensor) { |
dmathew | 20:56355fae478b | 113 | int_sensor = false; |
dmathew | 20:56355fae478b | 114 | status = sensor->handle_irq(&distance); |
dmathew | 20:56355fae478b | 115 | printf("distance: %d\r\n", distance); |
dmathew | 20:56355fae478b | 116 | } |
dmathew | 20:56355fae478b | 117 | |
dmathew | 20:56355fae478b | 118 | if (int_stop) { |
dmathew | 20:56355fae478b | 119 | printf("\r\nEnding loop mode \r\n"); |
dmathew | 20:56355fae478b | 120 | break; |
dmathew | 20:56355fae478b | 121 | } |
johnAlexander | 19:29699fbc39b8 | 122 | } |
johnAlexander | 19:29699fbc39b8 | 123 | } |
johnAlexander | 19:29699fbc39b8 | 124 | |
dmathew | 20:56355fae478b | 125 | return status; |
dmathew | 20:56355fae478b | 126 | |
johnAlexander | 19:29699fbc39b8 | 127 | } |
johnAlexander | 19:29699fbc39b8 | 128 | |
dmathew | 21:a225fcaf0228 | 129 | int polling_range_measure(vl53L1X_DevI2C *device_i2c) |
dmathew | 21:a225fcaf0228 | 130 | { |
dmathew | 21:a225fcaf0228 | 131 | int status = 0; |
dmathew | 21:a225fcaf0228 | 132 | uint8_t ready = 0; |
dmathew | 21:a225fcaf0228 | 133 | uint16_t distance = 0; |
dmathew | 21:a225fcaf0228 | 134 | /* Create a xshutdown pin */ |
dmathew | 21:a225fcaf0228 | 135 | DigitalOut xshutdown(D7); |
dmathew | 21:a225fcaf0228 | 136 | |
dmathew | 21:a225fcaf0228 | 137 | /* create instance of sensor class */ |
dmathew | 21:a225fcaf0228 | 138 | sensor = new VL53L1X(device_i2c, &xshutdown, A2); |
dmathew | 21:a225fcaf0228 | 139 | |
dmathew | 21:a225fcaf0228 | 140 | /* initialise sensor */ |
dmathew | 21:a225fcaf0228 | 141 | sensor->InitSensor(0x52); |
dmathew | 21:a225fcaf0228 | 142 | |
dmathew | 21:a225fcaf0228 | 143 | if (status) { |
dmathew | 21:a225fcaf0228 | 144 | delete sensor; |
dmathew | 21:a225fcaf0228 | 145 | sensor= NULL; |
dmathew | 21:a225fcaf0228 | 146 | printf("Sensor centre not present\n\r"); |
dmathew | 21:a225fcaf0228 | 147 | } |
dmathew | 21:a225fcaf0228 | 148 | |
dmathew | 21:a225fcaf0228 | 149 | /* init an array with chars to id the sensors */ |
dmathew | 21:a225fcaf0228 | 150 | status = start_ranging(); |
dmathew | 21:a225fcaf0228 | 151 | if (status != 0) { |
dmathew | 21:a225fcaf0228 | 152 | printf("Failed to start ranging!\r\n"); |
dmathew | 21:a225fcaf0228 | 153 | return status; |
dmathew | 21:a225fcaf0228 | 154 | } |
dmathew | 21:a225fcaf0228 | 155 | |
dmathew | 21:a225fcaf0228 | 156 | if (NULL != sensor) { |
dmathew | 21:a225fcaf0228 | 157 | printf("Entering loop mode\r\n"); |
dmathew | 21:a225fcaf0228 | 158 | /* Main ranging interrupt loop */ |
dmathew | 21:a225fcaf0228 | 159 | while (true) { |
dmathew | 21:a225fcaf0228 | 160 | status = sensor->VL53L1X_CheckForDataReady(&ready); |
dmathew | 21:a225fcaf0228 | 161 | if (ready) { |
dmathew | 21:a225fcaf0228 | 162 | sensor->VL53L1X_GetDistance(&distance); |
dmathew | 21:a225fcaf0228 | 163 | printf("distance: %d\r\n", distance); |
dmathew | 21:a225fcaf0228 | 164 | } |
dmathew | 21:a225fcaf0228 | 165 | |
dmathew | 21:a225fcaf0228 | 166 | if (int_stop) { |
dmathew | 21:a225fcaf0228 | 167 | printf("\r\nEnding loop mode \r\n"); |
dmathew | 21:a225fcaf0228 | 168 | break; |
dmathew | 21:a225fcaf0228 | 169 | } |
dmathew | 21:a225fcaf0228 | 170 | } |
dmathew | 21:a225fcaf0228 | 171 | } |
dmathew | 21:a225fcaf0228 | 172 | |
dmathew | 21:a225fcaf0228 | 173 | return status; |
dmathew | 21:a225fcaf0228 | 174 | } |
johnAlexander | 19:29699fbc39b8 | 175 | |
johnAlexander | 19:29699fbc39b8 | 176 | /*=================================== Main ================================== |
johnAlexander | 19:29699fbc39b8 | 177 | =============================================================================*/ |
johnAlexander | 19:29699fbc39b8 | 178 | int main() |
johnAlexander | 19:29699fbc39b8 | 179 | { |
dmathew | 21:a225fcaf0228 | 180 | int range_mode = 0; |
johnAlexander | 19:29699fbc39b8 | 181 | #if USER_BUTTON==PC_13 // we are cross compiling for Nucleo-f401 |
dmathew | 20:56355fae478b | 182 | InterruptIn stop_button(USER_BUTTON); |
dmathew | 20:56355fae478b | 183 | stop_button.rise(&measuring_stop_irq); |
johnAlexander | 19:29699fbc39b8 | 184 | #endif |
dmathew | 20:56355fae478b | 185 | vl53L1X_DevI2C *device_i2c = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL); |
dmathew | 21:a225fcaf0228 | 186 | |
dmathew | 21:a225fcaf0228 | 187 | /* set range_mode to either polling or interrupt */ |
dmathew | 21:a225fcaf0228 | 188 | range_mode = POLLING_MODE; |
dmathew | 21:a225fcaf0228 | 189 | |
dmathew | 21:a225fcaf0228 | 190 | switch (range_mode) { |
dmathew | 21:a225fcaf0228 | 191 | case INTERRUPT_MODE: |
dmathew | 21:a225fcaf0228 | 192 | interrupt_range_measure(device_i2c); // start continuous measures |
dmathew | 21:a225fcaf0228 | 193 | break; |
dmathew | 21:a225fcaf0228 | 194 | case POLLING_MODE: |
dmathew | 21:a225fcaf0228 | 195 | polling_range_measure(device_i2c); |
dmathew | 21:a225fcaf0228 | 196 | break; |
dmathew | 21:a225fcaf0228 | 197 | default: |
dmathew | 21:a225fcaf0228 | 198 | printf("Invalid range mode\n\r"); |
dmathew | 21:a225fcaf0228 | 199 | break; |
dmathew | 21:a225fcaf0228 | 200 | } |
johnAlexander | 19:29699fbc39b8 | 201 | } |