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