Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed X_NUCLEO_53L1A1_mbed
main.cpp@19:27f341756cb6, 2019-05-23 (annotated)
- Committer:
- dmathew
- Date:
- Thu May 23 12:01:43 2019 +0000
- Revision:
- 19:27f341756cb6
- Parent:
- 17:e7a6fd6d3c97
- Child:
- 20:639c4cbcfad9
Added sensor NULL checks to the code to allow only polling example to only use one device. Button stop command added to allow user to stop ranging loop.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dmathew | 17:e7a6fd6d3c97 | 1 | /* |
dmathew | 17:e7a6fd6d3c97 | 2 | * This VL53L1X Expansion board test application performs range measurements |
dmathew | 17:e7a6fd6d3c97 | 3 | * using the onboard embedded centre sensor. |
dmathew | 17:e7a6fd6d3c97 | 4 | * |
dmathew | 19:27f341756cb6 | 5 | * Measured ranges are ouput on the Serial Port, running at 96000 baud. |
dmathew | 17:e7a6fd6d3c97 | 6 | * |
dmathew | 17:e7a6fd6d3c97 | 7 | * The User Blue button stops the current measurement and entire program, |
dmathew | 17:e7a6fd6d3c97 | 8 | * releasing all resources. |
dmathew | 17:e7a6fd6d3c97 | 9 | * |
dmathew | 17:e7a6fd6d3c97 | 10 | * The Black Reset button is used to restart the program. |
dmathew | 17:e7a6fd6d3c97 | 11 | * |
dmathew | 17:e7a6fd6d3c97 | 12 | * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of |
dmathew | 17:e7a6fd6d3c97 | 13 | * the X-NUCELO-53L0A1 expansion board are not made/OFF. |
dmathew | 17:e7a6fd6d3c97 | 14 | * These links must be made to allow interrupts from the Satellite boards |
dmathew | 17:e7a6fd6d3c97 | 15 | * to be received. |
dmathew | 17:e7a6fd6d3c97 | 16 | * U11 and U18 must be made/ON to allow interrupts to be received from the |
dmathew | 17:e7a6fd6d3c97 | 17 | * INT_L & INT_R positions; or |
dmathew | 17:e7a6fd6d3c97 | 18 | * U10 and U15 must be made/ON to allow interrupts to be received from the |
dmathew | 17:e7a6fd6d3c97 | 19 | * Alternate INT_L & INT_R positions. |
dmathew | 17:e7a6fd6d3c97 | 20 | * The X_NUCLEO_53L1A1 firmware library defaults to use the INT_L/INT_R |
dmathew | 17:e7a6fd6d3c97 | 21 | * positions. |
dmathew | 17:e7a6fd6d3c97 | 22 | * INT_L is available on expansion board Arduino Connector CN5, pin 1 as D9. |
dmathew | 17:e7a6fd6d3c97 | 23 | * Alternate INT_L is on CN5 Connector pin 2 as D8. |
dmathew | 17:e7a6fd6d3c97 | 24 | * INT_R is available on expansion board Arduino Connector CN9, pin 3 as D4. |
dmathew | 17:e7a6fd6d3c97 | 25 | * Alternate INT_R is on CN9 Connector pin 5 as D2. |
dmathew | 17:e7a6fd6d3c97 | 26 | * The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A1/ |
dmathew | 17:e7a6fd6d3c97 | 27 | */ |
dmathew | 17:e7a6fd6d3c97 | 28 | |
dmathew | 17:e7a6fd6d3c97 | 29 | #include <stdio.h> |
dmathew | 17:e7a6fd6d3c97 | 30 | |
johnAlexander | 0:ce8359133ae6 | 31 | #include "mbed.h" |
JerrySzczurak | 14:d3904b05aad6 | 32 | #include "XNucleo53L1A1.h" |
JerrySzczurak | 14:d3904b05aad6 | 33 | #include "vl53L1x_I2c.h" |
johnAlexander | 0:ce8359133ae6 | 34 | |
dmathew | 17:e7a6fd6d3c97 | 35 | #define VL53L1_I2C_SDA D14 |
dmathew | 17:e7a6fd6d3c97 | 36 | #define VL53L1_I2C_SCL D15 |
JerrySzczurak | 14:d3904b05aad6 | 37 | |
JerrySzczurak | 14:d3904b05aad6 | 38 | static XNucleo53L1A1 *board=NULL; |
dmathew | 19:27f341756cb6 | 39 | Serial pc(SERIAL_TX, SERIAL_RX); |
dmathew | 17:e7a6fd6d3c97 | 40 | |
dmathew | 19:27f341756cb6 | 41 | volatile bool polling_stop = false; |
dmathew | 19:27f341756cb6 | 42 | |
dmathew | 19:27f341756cb6 | 43 | void stop_polling(void) |
dmathew | 19:27f341756cb6 | 44 | { |
dmathew | 19:27f341756cb6 | 45 | polling_stop = true; |
dmathew | 19:27f341756cb6 | 46 | } |
dmathew | 17:e7a6fd6d3c97 | 47 | |
johnAlexander | 0:ce8359133ae6 | 48 | /*=================================== Main ================================== |
johnAlexander | 0:ce8359133ae6 | 49 | =============================================================================*/ |
johnAlexander | 0:ce8359133ae6 | 50 | int main() |
dmathew | 17:e7a6fd6d3c97 | 51 | { |
dmathew | 19:27f341756cb6 | 52 | #if USER_BUTTON==PC_13 // we are cross compiling for Nucleo-f401 |
dmathew | 19:27f341756cb6 | 53 | InterruptIn stop_button(USER_BUTTON); |
dmathew | 19:27f341756cb6 | 54 | stop_button.rise(&stop_polling); |
dmathew | 19:27f341756cb6 | 55 | #endif |
dmathew | 17:e7a6fd6d3c97 | 56 | int status = 0; |
dmathew | 17:e7a6fd6d3c97 | 57 | uint8_t ready_centre = 0; |
dmathew | 17:e7a6fd6d3c97 | 58 | uint8_t ready_left = 0; |
dmathew | 17:e7a6fd6d3c97 | 59 | uint8_t ready_right = 0; |
dmathew | 17:e7a6fd6d3c97 | 60 | uint16_t distance_centre = 0; |
dmathew | 17:e7a6fd6d3c97 | 61 | uint16_t distance_left = 0; |
dmathew | 17:e7a6fd6d3c97 | 62 | uint16_t distance_right = 0; |
JerrySzczurak | 14:d3904b05aad6 | 63 | |
JerrySzczurak | 14:d3904b05aad6 | 64 | printf("Hello world!\r\n"); |
johnAlexander | 3:b3f70617a6b3 | 65 | |
JerrySzczurak | 14:d3904b05aad6 | 66 | vl53L1X_DevI2C *dev_I2C = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL); |
JerrySzczurak | 14:d3904b05aad6 | 67 | |
JerrySzczurak | 14:d3904b05aad6 | 68 | printf("I2C device created!\r\n"); |
dmathew | 17:e7a6fd6d3c97 | 69 | |
dmathew | 17:e7a6fd6d3c97 | 70 | /* creates the 53L1A1 expansion board singleton obj */ |
dmathew | 17:e7a6fd6d3c97 | 71 | board = XNucleo53L1A1::instance(dev_I2C, A2, D9, D2); |
JerrySzczurak | 14:d3904b05aad6 | 72 | printf("board created!\r\n"); |
johnAlexander | 3:b3f70617a6b3 | 73 | |
dmathew | 17:e7a6fd6d3c97 | 74 | /* init the 53L1A1 expansion board with default values */ |
johnAlexander | 7:c8087e7333b8 | 75 | status = board->init_board(); |
dmathew | 19:27f341756cb6 | 76 | if (status) { |
Davidroid | 10:891e10d3b4a6 | 77 | printf("Failed to init board!\r\n"); |
dmathew | 17:e7a6fd6d3c97 | 78 | return status; |
johnAlexander | 7:c8087e7333b8 | 79 | } |
dmathew | 17:e7a6fd6d3c97 | 80 | |
JerrySzczurak | 14:d3904b05aad6 | 81 | printf("board initiated! - %d\r\n", status); |
dmathew | 17:e7a6fd6d3c97 | 82 | |
dmathew | 17:e7a6fd6d3c97 | 83 | /* Start ranging on the centre sensor */ |
dmathew | 19:27f341756cb6 | 84 | if (board->sensor_centre != NULL) { |
dmathew | 19:27f341756cb6 | 85 | status = board->sensor_centre->VL53L1X_StartRanging(); |
dmathew | 19:27f341756cb6 | 86 | if (status != 0) { |
dmathew | 19:27f341756cb6 | 87 | printf("Centre sensor failed to start ranging!\r\n"); |
dmathew | 19:27f341756cb6 | 88 | return status; |
dmathew | 19:27f341756cb6 | 89 | } |
dmathew | 17:e7a6fd6d3c97 | 90 | } |
dmathew | 17:e7a6fd6d3c97 | 91 | |
dmathew | 17:e7a6fd6d3c97 | 92 | /* Start ranging on the left sensor */ |
dmathew | 19:27f341756cb6 | 93 | if (board->sensor_left != NULL) { |
dmathew | 19:27f341756cb6 | 94 | status = board->sensor_left->VL53L1X_StartRanging(); |
dmathew | 19:27f341756cb6 | 95 | if (status != 0) { |
dmathew | 19:27f341756cb6 | 96 | printf("Left sensor failed to start ranging!\r\n"); |
dmathew | 19:27f341756cb6 | 97 | return status; |
dmathew | 19:27f341756cb6 | 98 | } |
dmathew | 17:e7a6fd6d3c97 | 99 | } |
dmathew | 17:e7a6fd6d3c97 | 100 | |
dmathew | 17:e7a6fd6d3c97 | 101 | /* Start ranging on the right sensor */ |
dmathew | 19:27f341756cb6 | 102 | if (board->sensor_right != NULL) { |
dmathew | 19:27f341756cb6 | 103 | status = board->sensor_right->VL53L1X_StartRanging(); |
dmathew | 19:27f341756cb6 | 104 | if (status != 0) { |
dmathew | 19:27f341756cb6 | 105 | printf("Right sensor failed to start ranging!\r\n"); |
dmathew | 19:27f341756cb6 | 106 | return status; |
dmathew | 19:27f341756cb6 | 107 | } |
dmathew | 17:e7a6fd6d3c97 | 108 | } |
dmathew | 17:e7a6fd6d3c97 | 109 | |
dmathew | 17:e7a6fd6d3c97 | 110 | /* Ranging loop |
dmathew | 17:e7a6fd6d3c97 | 111 | * Checks each sensor for data ready |
dmathew | 17:e7a6fd6d3c97 | 112 | */ |
JerrySzczurak | 14:d3904b05aad6 | 113 | while (1) |
dmathew | 17:e7a6fd6d3c97 | 114 | { |
dmathew | 19:27f341756cb6 | 115 | if (polling_stop) |
dmathew | 19:27f341756cb6 | 116 | { |
dmathew | 19:27f341756cb6 | 117 | printf("\r\nEnding loop mode \r\n"); |
dmathew | 19:27f341756cb6 | 118 | break; |
dmathew | 19:27f341756cb6 | 119 | } |
dmathew | 19:27f341756cb6 | 120 | if (board->sensor_centre != NULL) { |
dmathew | 19:27f341756cb6 | 121 | board->sensor_centre->VL53L1X_CheckForDataReady(&ready_centre); |
dmathew | 19:27f341756cb6 | 122 | } |
dmathew | 19:27f341756cb6 | 123 | if (board->sensor_left != NULL) { |
dmathew | 19:27f341756cb6 | 124 | board->sensor_left->VL53L1X_CheckForDataReady(&ready_left); |
dmathew | 19:27f341756cb6 | 125 | } |
dmathew | 19:27f341756cb6 | 126 | if (board->sensor_right != NULL) { |
dmathew | 19:27f341756cb6 | 127 | board->sensor_right->VL53L1X_CheckForDataReady(&ready_right); |
dmathew | 19:27f341756cb6 | 128 | } |
dmathew | 17:e7a6fd6d3c97 | 129 | if (ready_centre) { |
dmathew | 17:e7a6fd6d3c97 | 130 | board->sensor_centre->VL53L1X_GetRangeStatus(&ready_centre); |
dmathew | 17:e7a6fd6d3c97 | 131 | board->sensor_centre->VL53L1X_GetDistance(&distance_centre); |
dmathew | 17:e7a6fd6d3c97 | 132 | } |
dmathew | 17:e7a6fd6d3c97 | 133 | if (ready_left) { |
dmathew | 17:e7a6fd6d3c97 | 134 | board->sensor_centre->VL53L1X_GetRangeStatus(&ready_left); |
dmathew | 17:e7a6fd6d3c97 | 135 | board->sensor_centre->VL53L1X_GetDistance(&distance_left); |
dmathew | 17:e7a6fd6d3c97 | 136 | } |
dmathew | 17:e7a6fd6d3c97 | 137 | if (ready_right) { |
dmathew | 17:e7a6fd6d3c97 | 138 | board->sensor_centre->VL53L1X_GetRangeStatus(&ready_right); |
dmathew | 17:e7a6fd6d3c97 | 139 | board->sensor_centre->VL53L1X_GetDistance(&distance_right); |
dmathew | 17:e7a6fd6d3c97 | 140 | } |
dmathew | 17:e7a6fd6d3c97 | 141 | |
dmathew | 19:27f341756cb6 | 142 | if (board->sensor_centre != NULL) { |
dmathew | 19:27f341756cb6 | 143 | printf("%d\t", distance_centre); |
dmathew | 19:27f341756cb6 | 144 | } |
dmathew | 19:27f341756cb6 | 145 | if (board->sensor_left != NULL) { |
dmathew | 19:27f341756cb6 | 146 | printf("%d\t", distance_left); |
dmathew | 19:27f341756cb6 | 147 | } |
dmathew | 19:27f341756cb6 | 148 | if (board->sensor_right != NULL) { |
dmathew | 19:27f341756cb6 | 149 | printf("%d\t", distance_right); |
dmathew | 19:27f341756cb6 | 150 | } |
dmathew | 17:e7a6fd6d3c97 | 151 | } |
dmathew | 17:e7a6fd6d3c97 | 152 | |
dmathew | 17:e7a6fd6d3c97 | 153 | return status; |
johnAlexander | 0:ce8359133ae6 | 154 | } |