Simple Ranging Example, using Expansion Board Sensor, driven via Interrupts.
Dependencies: mbed X_NUCLEO_53L1A1_mbed
main.cpp@19:92972e54d45c, 2019-05-17 (annotated)
- Committer:
- johnAlexander
- Date:
- Fri May 17 10:05:56 2019 +0000
- Revision:
- 19:92972e54d45c
- Parent:
- 17:e7a6fd6d3c97
Simple Ranging Example, using Expansion Board Sensor, driven via Interrupts.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dmathew | 17:e7a6fd6d3c97 | 1 | /* |
johnAlexander | 19:92972e54d45c | 2 | * This VL53L1X Expansion board sample application performs range measurements |
johnAlexander | 19:92972e54d45c | 3 | * with interrupts enabled to generate a hardware interrupt each time a new |
johnAlexander | 19:92972e54d45c | 4 | * measurement is ready to be read. |
dmathew | 17:e7a6fd6d3c97 | 5 | * |
dmathew | 17:e7a6fd6d3c97 | 6 | * Measured ranges are ouput on the Serial Port, running at 115200 baud. |
dmathew | 17:e7a6fd6d3c97 | 7 | * |
dmathew | 17:e7a6fd6d3c97 | 8 | * The User Blue button stops the current measurement and entire program, |
dmathew | 17:e7a6fd6d3c97 | 9 | * releasing all resources. |
dmathew | 17:e7a6fd6d3c97 | 10 | * |
dmathew | 17:e7a6fd6d3c97 | 11 | * The Black Reset button is used to restart the program. |
dmathew | 17:e7a6fd6d3c97 | 12 | * |
dmathew | 17:e7a6fd6d3c97 | 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 | 17:e7a6fd6d3c97 | 29 | |
dmathew | 17:e7a6fd6d3c97 | 30 | #include <stdio.h> |
dmathew | 17:e7a6fd6d3c97 | 31 | |
johnAlexander | 0:ce8359133ae6 | 32 | #include "mbed.h" |
JerrySzczurak | 14:d3904b05aad6 | 33 | #include "XNucleo53L1A1.h" |
JerrySzczurak | 14:d3904b05aad6 | 34 | #include "vl53L1x_I2c.h" |
johnAlexander | 0:ce8359133ae6 | 35 | |
dmathew | 17:e7a6fd6d3c97 | 36 | #define VL53L1_I2C_SDA D14 |
dmathew | 17:e7a6fd6d3c97 | 37 | #define VL53L1_I2C_SCL D15 |
JerrySzczurak | 14:d3904b05aad6 | 38 | |
JerrySzczurak | 14:d3904b05aad6 | 39 | static XNucleo53L1A1 *board=NULL; |
dmathew | 17:e7a6fd6d3c97 | 40 | Serial pc(SERIAL_TX, SERIAL_RX); |
dmathew | 17:e7a6fd6d3c97 | 41 | |
johnAlexander | 19:92972e54d45c | 42 | /* flags that handle interrupt request for sensor and user blue button*/ |
johnAlexander | 19:92972e54d45c | 43 | volatile bool int_sensor = false; |
johnAlexander | 19:92972e54d45c | 44 | volatile bool int_stop = false; |
johnAlexander | 19:92972e54d45c | 45 | |
johnAlexander | 19:92972e54d45c | 46 | /* ISR callback function of the centre sensor */ |
johnAlexander | 19:92972e54d45c | 47 | void sensor_irq(void) |
dmathew | 17:e7a6fd6d3c97 | 48 | { |
johnAlexander | 19:92972e54d45c | 49 | int_sensor = true; |
johnAlexander | 19:92972e54d45c | 50 | board->sensor_centre->disable_interrupt_measure_detection_irq(); |
johnAlexander | 19:92972e54d45c | 51 | } |
dmathew | 17:e7a6fd6d3c97 | 52 | |
johnAlexander | 19:92972e54d45c | 53 | /* Start the sensor ranging */ |
johnAlexander | 19:92972e54d45c | 54 | int init_sensor() |
johnAlexander | 19:92972e54d45c | 55 | { |
dmathew | 17:e7a6fd6d3c97 | 56 | int status = 0; |
johnAlexander | 19:92972e54d45c | 57 | /* start the measure on the center sensor */ |
johnAlexander | 19:92972e54d45c | 58 | if (NULL != board->sensor_centre) { |
johnAlexander | 19:92972e54d45c | 59 | status = board->sensor_centre->stop_measurement(); |
johnAlexander | 19:92972e54d45c | 60 | if (status != 0) { |
johnAlexander | 19:92972e54d45c | 61 | return status; |
johnAlexander | 19:92972e54d45c | 62 | } |
JerrySzczurak | 14:d3904b05aad6 | 63 | |
johnAlexander | 19:92972e54d45c | 64 | status = board->sensor_centre->start_measurement(&sensor_irq); |
johnAlexander | 19:92972e54d45c | 65 | if (status != 0) { |
johnAlexander | 19:92972e54d45c | 66 | return status; |
johnAlexander | 19:92972e54d45c | 67 | } |
johnAlexander | 19:92972e54d45c | 68 | } |
johnAlexander | 19:92972e54d45c | 69 | return status; |
johnAlexander | 19:92972e54d45c | 70 | } |
johnAlexander | 3:b3f70617a6b3 | 71 | |
johnAlexander | 19:92972e54d45c | 72 | /* ISR callback function of the user blue button to switch measuring sensor. */ |
johnAlexander | 19:92972e54d45c | 73 | void measuring_stop_irq(void) |
johnAlexander | 19:92972e54d45c | 74 | { |
johnAlexander | 19:92972e54d45c | 75 | int_stop = true; |
johnAlexander | 19:92972e54d45c | 76 | } |
JerrySzczurak | 14:d3904b05aad6 | 77 | |
johnAlexander | 19:92972e54d45c | 78 | /* |
johnAlexander | 19:92972e54d45c | 79 | * Main ranging function |
johnAlexander | 19:92972e54d45c | 80 | */ |
johnAlexander | 19:92972e54d45c | 81 | int range_measure(vl53L1X_DevI2C *device_i2c) |
johnAlexander | 19:92972e54d45c | 82 | { |
johnAlexander | 19:92972e54d45c | 83 | int status = 0; |
johnAlexander | 19:92972e54d45c | 84 | uint16_t distance = 0; |
dmathew | 17:e7a6fd6d3c97 | 85 | |
dmathew | 17:e7a6fd6d3c97 | 86 | /* creates the 53L1A1 expansion board singleton obj */ |
johnAlexander | 19:92972e54d45c | 87 | board = XNucleo53L1A1::instance(device_i2c, A2, D9, D2); |
johnAlexander | 3:b3f70617a6b3 | 88 | |
dmathew | 17:e7a6fd6d3c97 | 89 | /* init the 53L1A1 expansion board with default values */ |
johnAlexander | 7:c8087e7333b8 | 90 | status = board->init_board(); |
dmathew | 17:e7a6fd6d3c97 | 91 | if (status != 0) { |
Davidroid | 10:891e10d3b4a6 | 92 | printf("Failed to init board!\r\n"); |
dmathew | 17:e7a6fd6d3c97 | 93 | return status; |
johnAlexander | 7:c8087e7333b8 | 94 | } |
dmathew | 17:e7a6fd6d3c97 | 95 | |
johnAlexander | 19:92972e54d45c | 96 | /* init an array with chars to id the sensors */ |
johnAlexander | 19:92972e54d45c | 97 | status = init_sensor(); |
dmathew | 17:e7a6fd6d3c97 | 98 | if (status != 0) { |
johnAlexander | 19:92972e54d45c | 99 | printf("Failed to init sensors!\r\n"); |
dmathew | 17:e7a6fd6d3c97 | 100 | return status; |
dmathew | 17:e7a6fd6d3c97 | 101 | } |
dmathew | 17:e7a6fd6d3c97 | 102 | |
johnAlexander | 19:92972e54d45c | 103 | printf("Entering loop mode\r\n"); |
johnAlexander | 19:92972e54d45c | 104 | /* Main ranging interrupt loop */ |
johnAlexander | 19:92972e54d45c | 105 | while (true) { |
johnAlexander | 19:92972e54d45c | 106 | if (int_sensor) { |
johnAlexander | 19:92972e54d45c | 107 | int_sensor = false; |
johnAlexander | 19:92972e54d45c | 108 | status = board->sensor_centre->handle_irq(&distance); |
johnAlexander | 19:92972e54d45c | 109 | printf("distance: %d\r\n", distance); |
dmathew | 17:e7a6fd6d3c97 | 110 | } |
dmathew | 17:e7a6fd6d3c97 | 111 | |
johnAlexander | 19:92972e54d45c | 112 | if (int_stop) { |
johnAlexander | 19:92972e54d45c | 113 | printf("\r\nEnding loop mode \r\n"); |
johnAlexander | 19:92972e54d45c | 114 | break; |
johnAlexander | 19:92972e54d45c | 115 | } |
dmathew | 17:e7a6fd6d3c97 | 116 | } |
dmathew | 17:e7a6fd6d3c97 | 117 | |
dmathew | 17:e7a6fd6d3c97 | 118 | return status; |
johnAlexander | 19:92972e54d45c | 119 | |
johnAlexander | 0:ce8359133ae6 | 120 | } |
johnAlexander | 19:92972e54d45c | 121 | |
johnAlexander | 19:92972e54d45c | 122 | /*=================================== Main ================================== |
johnAlexander | 19:92972e54d45c | 123 | =============================================================================*/ |
johnAlexander | 19:92972e54d45c | 124 | int main() |
johnAlexander | 19:92972e54d45c | 125 | { |
johnAlexander | 19:92972e54d45c | 126 | #if USER_BUTTON==PC_13 // we are cross compiling for Nucleo-f401 |
johnAlexander | 19:92972e54d45c | 127 | InterruptIn stop_button(USER_BUTTON); |
johnAlexander | 19:92972e54d45c | 128 | stop_button.rise(&measuring_stop_irq); |
johnAlexander | 19:92972e54d45c | 129 | #endif |
johnAlexander | 19:92972e54d45c | 130 | vl53L1X_DevI2C *device_i2c = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL); |
johnAlexander | 19:92972e54d45c | 131 | range_measure(device_i2c); // start continuous measures |
johnAlexander | 19:92972e54d45c | 132 | } |