Ranging with the Time-of-Flight (ToF) sensors on the X-NUCLEO-53L1A1 expansion board, and 2 Satellite boards connected to the expansion board.
Dependencies: mbed X_NUCLEO_53L1A1_mbed
main.cpp@17:e7a6fd6d3c97, 2019-05-17 (annotated)
- Committer:
- dmathew
- Date:
- Fri May 17 09:14:06 2019 +0000
- Revision:
- 17:e7a6fd6d3c97
- Parent:
- 14:d3904b05aad6
- Child:
- 19:29699fbc39b8
Comment cleanup
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 | 17:e7a6fd6d3c97 | 5 | * Measured ranges are ouput on the Serial Port, running at 115200 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 | 17:e7a6fd6d3c97 | 39 | |
dmathew | 17:e7a6fd6d3c97 | 40 | Serial pc(SERIAL_TX, SERIAL_RX); |
dmathew | 17:e7a6fd6d3c97 | 41 | |
johnAlexander | 0:ce8359133ae6 | 42 | /*=================================== Main ================================== |
johnAlexander | 0:ce8359133ae6 | 43 | =============================================================================*/ |
johnAlexander | 0:ce8359133ae6 | 44 | int main() |
dmathew | 17:e7a6fd6d3c97 | 45 | { |
dmathew | 17:e7a6fd6d3c97 | 46 | |
dmathew | 17:e7a6fd6d3c97 | 47 | int status = 0; |
dmathew | 17:e7a6fd6d3c97 | 48 | uint8_t ready_centre = 0; |
dmathew | 17:e7a6fd6d3c97 | 49 | uint8_t ready_left = 0; |
dmathew | 17:e7a6fd6d3c97 | 50 | uint8_t ready_right = 0; |
dmathew | 17:e7a6fd6d3c97 | 51 | uint16_t distance_centre = 0; |
dmathew | 17:e7a6fd6d3c97 | 52 | uint16_t distance_left = 0; |
dmathew | 17:e7a6fd6d3c97 | 53 | uint16_t distance_right = 0; |
JerrySzczurak | 14:d3904b05aad6 | 54 | |
JerrySzczurak | 14:d3904b05aad6 | 55 | printf("Hello world!\r\n"); |
johnAlexander | 3:b3f70617a6b3 | 56 | |
JerrySzczurak | 14:d3904b05aad6 | 57 | vl53L1X_DevI2C *dev_I2C = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL); |
JerrySzczurak | 14:d3904b05aad6 | 58 | |
JerrySzczurak | 14:d3904b05aad6 | 59 | printf("I2C device created!\r\n"); |
dmathew | 17:e7a6fd6d3c97 | 60 | |
dmathew | 17:e7a6fd6d3c97 | 61 | /* creates the 53L1A1 expansion board singleton obj */ |
dmathew | 17:e7a6fd6d3c97 | 62 | board = XNucleo53L1A1::instance(dev_I2C, A2, D9, D2); |
JerrySzczurak | 14:d3904b05aad6 | 63 | printf("board created!\r\n"); |
johnAlexander | 3:b3f70617a6b3 | 64 | |
dmathew | 17:e7a6fd6d3c97 | 65 | /* init the 53L1A1 expansion board with default values */ |
johnAlexander | 7:c8087e7333b8 | 66 | status = board->init_board(); |
dmathew | 17:e7a6fd6d3c97 | 67 | if (status != 0) { |
Davidroid | 10:891e10d3b4a6 | 68 | printf("Failed to init board!\r\n"); |
dmathew | 17:e7a6fd6d3c97 | 69 | return status; |
johnAlexander | 7:c8087e7333b8 | 70 | } |
dmathew | 17:e7a6fd6d3c97 | 71 | |
JerrySzczurak | 14:d3904b05aad6 | 72 | printf("board initiated! - %d\r\n", status); |
dmathew | 17:e7a6fd6d3c97 | 73 | |
dmathew | 17:e7a6fd6d3c97 | 74 | /* Start ranging on the centre sensor */ |
JerrySzczurak | 14:d3904b05aad6 | 75 | status = board->sensor_centre->VL53L1X_StartRanging(); |
dmathew | 17:e7a6fd6d3c97 | 76 | if (status != 0) { |
dmathew | 17:e7a6fd6d3c97 | 77 | printf("Centre sensor failed to start ranging!\r\n"); |
dmathew | 17:e7a6fd6d3c97 | 78 | return status; |
dmathew | 17:e7a6fd6d3c97 | 79 | } |
dmathew | 17:e7a6fd6d3c97 | 80 | |
dmathew | 17:e7a6fd6d3c97 | 81 | /* Start ranging on the left sensor */ |
dmathew | 17:e7a6fd6d3c97 | 82 | status = board->sensor_left->VL53L1X_StartRanging(); |
dmathew | 17:e7a6fd6d3c97 | 83 | if (status != 0) { |
dmathew | 17:e7a6fd6d3c97 | 84 | printf("Left sensor failed to start ranging!\r\n"); |
dmathew | 17:e7a6fd6d3c97 | 85 | return status; |
dmathew | 17:e7a6fd6d3c97 | 86 | } |
dmathew | 17:e7a6fd6d3c97 | 87 | |
dmathew | 17:e7a6fd6d3c97 | 88 | /* Start ranging on the right sensor */ |
dmathew | 17:e7a6fd6d3c97 | 89 | status = board->sensor_right->VL53L1X_StartRanging(); |
dmathew | 17:e7a6fd6d3c97 | 90 | if (status != 0) { |
dmathew | 17:e7a6fd6d3c97 | 91 | printf("Right sensor failed to start ranging!\r\n"); |
dmathew | 17:e7a6fd6d3c97 | 92 | return status; |
dmathew | 17:e7a6fd6d3c97 | 93 | } |
dmathew | 17:e7a6fd6d3c97 | 94 | |
dmathew | 17:e7a6fd6d3c97 | 95 | /* Ranging loop |
dmathew | 17:e7a6fd6d3c97 | 96 | * Checks each sensor for data ready |
dmathew | 17:e7a6fd6d3c97 | 97 | */ |
JerrySzczurak | 14:d3904b05aad6 | 98 | while (1) |
dmathew | 17:e7a6fd6d3c97 | 99 | { |
dmathew | 17:e7a6fd6d3c97 | 100 | board->sensor_centre->VL53L1X_CheckForDataReady(&ready_centre); |
dmathew | 17:e7a6fd6d3c97 | 101 | board->sensor_left->VL53L1X_CheckForDataReady(&ready_left); |
dmathew | 17:e7a6fd6d3c97 | 102 | board->sensor_right->VL53L1X_CheckForDataReady(&ready_right); |
johnAlexander | 4:c8932fb926d6 | 103 | |
dmathew | 17:e7a6fd6d3c97 | 104 | if (ready_centre) { |
dmathew | 17:e7a6fd6d3c97 | 105 | board->sensor_centre->VL53L1X_GetRangeStatus(&ready_centre); |
dmathew | 17:e7a6fd6d3c97 | 106 | board->sensor_centre->VL53L1X_GetDistance(&distance_centre); |
dmathew | 17:e7a6fd6d3c97 | 107 | } |
dmathew | 17:e7a6fd6d3c97 | 108 | if (ready_left) { |
dmathew | 17:e7a6fd6d3c97 | 109 | board->sensor_centre->VL53L1X_GetRangeStatus(&ready_left); |
dmathew | 17:e7a6fd6d3c97 | 110 | board->sensor_centre->VL53L1X_GetDistance(&distance_left); |
dmathew | 17:e7a6fd6d3c97 | 111 | } |
dmathew | 17:e7a6fd6d3c97 | 112 | if (ready_right) { |
dmathew | 17:e7a6fd6d3c97 | 113 | board->sensor_centre->VL53L1X_GetRangeStatus(&ready_right); |
dmathew | 17:e7a6fd6d3c97 | 114 | board->sensor_centre->VL53L1X_GetDistance(&distance_right); |
dmathew | 17:e7a6fd6d3c97 | 115 | } |
dmathew | 17:e7a6fd6d3c97 | 116 | |
JerrySzczurak | 14:d3904b05aad6 | 117 | printf("%d\t", distance_centre); |
dmathew | 17:e7a6fd6d3c97 | 118 | printf("%d\t", distance_left); |
dmathew | 17:e7a6fd6d3c97 | 119 | printf("%d\t", distance_right); |
dmathew | 17:e7a6fd6d3c97 | 120 | } |
dmathew | 17:e7a6fd6d3c97 | 121 | |
dmathew | 17:e7a6fd6d3c97 | 122 | return status; |
johnAlexander | 0:ce8359133ae6 | 123 | } |