Simple ranging example, using polling, with only the X-Nucleo_53L1A1 expansion board.
Dependencies: X_NUCLEO_53L1A1_mbed
main.cpp@3:a3a863902994, 2019-10-14 (annotated)
- Committer:
- johnAlexander
- Date:
- Mon Oct 14 10:12:25 2019 +0000
- Revision:
- 3:a3a863902994
- Parent:
- 2:819f8323b02d
Simple ranging example, using polling, with only the X-Nucleo-53L1A1 expansion board.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
johnAlexander | 0:6b7696e7df5e | 1 | /* |
johnAlexander | 0:6b7696e7df5e | 2 | * This VL53L1X Expansion board test application performs range measurements |
johnAlexander | 3:a3a863902994 | 3 | * using the onboard embedded centre sensor. |
johnAlexander | 0:6b7696e7df5e | 4 | * |
johnAlexander | 2:819f8323b02d | 5 | * Measured ranges are ouput on the Serial Port, running at 115200 baud. |
johnAlexander | 0:6b7696e7df5e | 6 | * |
johnAlexander | 0:6b7696e7df5e | 7 | * The Black Reset button is used to restart the program. |
johnAlexander | 0:6b7696e7df5e | 8 | * |
johnAlexander | 0:6b7696e7df5e | 9 | * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of |
johnAlexander | 0:6b7696e7df5e | 10 | * the X-NUCELO-53L0A1 expansion board are not made/OFF. |
johnAlexander | 0:6b7696e7df5e | 11 | * These links must be made to allow interrupts from the Satellite boards |
johnAlexander | 0:6b7696e7df5e | 12 | * to be received. |
johnAlexander | 0:6b7696e7df5e | 13 | * U11 and U18 must be made/ON to allow interrupts to be received from the |
johnAlexander | 0:6b7696e7df5e | 14 | * INT_L & INT_R positions; or |
johnAlexander | 0:6b7696e7df5e | 15 | * U10 and U15 must be made/ON to allow interrupts to be received from the |
johnAlexander | 0:6b7696e7df5e | 16 | * Alternate INT_L & INT_R positions. |
johnAlexander | 0:6b7696e7df5e | 17 | * The X_NUCLEO_53L1A1 firmware library defaults to use the INT_L/INT_R |
johnAlexander | 0:6b7696e7df5e | 18 | * positions. |
johnAlexander | 0:6b7696e7df5e | 19 | * INT_L is available on expansion board Arduino Connector CN5, pin 1 as D9. |
johnAlexander | 0:6b7696e7df5e | 20 | * Alternate INT_L is on CN5 Connector pin 2 as D8. |
johnAlexander | 0:6b7696e7df5e | 21 | * INT_R is available on expansion board Arduino Connector CN9, pin 3 as D4. |
johnAlexander | 0:6b7696e7df5e | 22 | * Alternate INT_R is on CN9 Connector pin 5 as D2. |
johnAlexander | 0:6b7696e7df5e | 23 | * The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A1/ |
johnAlexander | 0:6b7696e7df5e | 24 | */ |
johnAlexander | 0:6b7696e7df5e | 25 | |
johnAlexander | 0:6b7696e7df5e | 26 | #include <stdio.h> |
johnAlexander | 0:6b7696e7df5e | 27 | |
johnAlexander | 0:6b7696e7df5e | 28 | #include "mbed.h" |
johnAlexander | 0:6b7696e7df5e | 29 | #include "XNucleo53L1A1.h" |
johnAlexander | 0:6b7696e7df5e | 30 | #include "vl53L1x_I2c.h" |
johnAlexander | 0:6b7696e7df5e | 31 | |
johnAlexander | 0:6b7696e7df5e | 32 | #define VL53L1_I2C_SDA D14 |
johnAlexander | 0:6b7696e7df5e | 33 | #define VL53L1_I2C_SCL D15 |
johnAlexander | 0:6b7696e7df5e | 34 | |
johnAlexander | 0:6b7696e7df5e | 35 | static XNucleo53L1A1 *board=NULL; |
johnAlexander | 0:6b7696e7df5e | 36 | |
dmathew | 1:b7507ca3370f | 37 | volatile bool polling_stop = false; |
dmathew | 1:b7507ca3370f | 38 | |
dmathew | 1:b7507ca3370f | 39 | void stop_polling(void) |
dmathew | 1:b7507ca3370f | 40 | { |
dmathew | 1:b7507ca3370f | 41 | polling_stop = true; |
dmathew | 1:b7507ca3370f | 42 | } |
johnAlexander | 0:6b7696e7df5e | 43 | |
johnAlexander | 0:6b7696e7df5e | 44 | /*=================================== Main ================================== |
johnAlexander | 0:6b7696e7df5e | 45 | =============================================================================*/ |
johnAlexander | 0:6b7696e7df5e | 46 | int main() |
johnAlexander | 0:6b7696e7df5e | 47 | { |
johnAlexander | 0:6b7696e7df5e | 48 | int status = 0; |
johnAlexander | 0:6b7696e7df5e | 49 | uint8_t ready_centre = 0; |
johnAlexander | 0:6b7696e7df5e | 50 | uint16_t distance_centre = 0; |
johnAlexander | 0:6b7696e7df5e | 51 | |
johnAlexander | 0:6b7696e7df5e | 52 | printf("Hello world!\r\n"); |
johnAlexander | 0:6b7696e7df5e | 53 | |
johnAlexander | 0:6b7696e7df5e | 54 | vl53L1X_DevI2C *dev_I2C = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL); |
johnAlexander | 0:6b7696e7df5e | 55 | |
johnAlexander | 0:6b7696e7df5e | 56 | printf("I2C device created!\r\n"); |
johnAlexander | 0:6b7696e7df5e | 57 | |
johnAlexander | 0:6b7696e7df5e | 58 | /* creates the 53L1A1 expansion board singleton obj */ |
johnAlexander | 0:6b7696e7df5e | 59 | board = XNucleo53L1A1::instance(dev_I2C, A2, D9, D2); |
johnAlexander | 0:6b7696e7df5e | 60 | printf("board created!\r\n"); |
johnAlexander | 0:6b7696e7df5e | 61 | |
johnAlexander | 0:6b7696e7df5e | 62 | /* init the 53L1A1 expansion board with default values */ |
johnAlexander | 0:6b7696e7df5e | 63 | status = board->init_board(); |
dmathew | 1:b7507ca3370f | 64 | if (status) { |
johnAlexander | 0:6b7696e7df5e | 65 | printf("Failed to init board!\r\n"); |
johnAlexander | 0:6b7696e7df5e | 66 | return status; |
johnAlexander | 0:6b7696e7df5e | 67 | } |
johnAlexander | 0:6b7696e7df5e | 68 | |
johnAlexander | 0:6b7696e7df5e | 69 | printf("board initiated! - %d\r\n", status); |
johnAlexander | 0:6b7696e7df5e | 70 | |
johnAlexander | 0:6b7696e7df5e | 71 | /* Start ranging on the centre sensor */ |
dmathew | 1:b7507ca3370f | 72 | if (board->sensor_centre != NULL) { |
dmathew | 1:b7507ca3370f | 73 | status = board->sensor_centre->VL53L1X_StartRanging(); |
dmathew | 1:b7507ca3370f | 74 | if (status != 0) { |
dmathew | 1:b7507ca3370f | 75 | printf("Centre sensor failed to start ranging!\r\n"); |
dmathew | 1:b7507ca3370f | 76 | return status; |
dmathew | 1:b7507ca3370f | 77 | } |
johnAlexander | 0:6b7696e7df5e | 78 | } |
johnAlexander | 0:6b7696e7df5e | 79 | |
johnAlexander | 0:6b7696e7df5e | 80 | /* Ranging loop |
johnAlexander | 0:6b7696e7df5e | 81 | * Checks each sensor for data ready |
johnAlexander | 0:6b7696e7df5e | 82 | */ |
johnAlexander | 0:6b7696e7df5e | 83 | while (1) |
johnAlexander | 0:6b7696e7df5e | 84 | { |
dmathew | 1:b7507ca3370f | 85 | if (polling_stop) |
dmathew | 1:b7507ca3370f | 86 | { |
dmathew | 1:b7507ca3370f | 87 | printf("\r\nEnding loop mode \r\n"); |
dmathew | 1:b7507ca3370f | 88 | break; |
dmathew | 1:b7507ca3370f | 89 | } |
dmathew | 1:b7507ca3370f | 90 | if (board->sensor_centre != NULL) { |
dmathew | 1:b7507ca3370f | 91 | board->sensor_centre->VL53L1X_CheckForDataReady(&ready_centre); |
dmathew | 1:b7507ca3370f | 92 | } |
johnAlexander | 0:6b7696e7df5e | 93 | if (ready_centre) { |
johnAlexander | 0:6b7696e7df5e | 94 | board->sensor_centre->VL53L1X_GetRangeStatus(&ready_centre); |
johnAlexander | 0:6b7696e7df5e | 95 | board->sensor_centre->VL53L1X_GetDistance(&distance_centre); |
johnAlexander | 0:6b7696e7df5e | 96 | } |
johnAlexander | 0:6b7696e7df5e | 97 | |
dmathew | 1:b7507ca3370f | 98 | if (board->sensor_centre != NULL) { |
johnAlexander | 3:a3a863902994 | 99 | printf("Distance read by sensor is : %d\r\n", distance_centre); |
dmathew | 1:b7507ca3370f | 100 | } |
johnAlexander | 0:6b7696e7df5e | 101 | } |
johnAlexander | 0:6b7696e7df5e | 102 | |
johnAlexander | 0:6b7696e7df5e | 103 | return status; |
johnAlexander | 0:6b7696e7df5e | 104 | } |