Simple Ranging example, using Polling, with Expansion Board and Satellite Boards.
Dependencies: X_NUCLEO_53L1A1_mbed
main.cpp@2:819f8323b02d, 2019-10-14 (annotated)
- Committer:
- johnAlexander
- Date:
- Mon Oct 14 10:02:40 2019 +0000
- Revision:
- 2:819f8323b02d
- Parent:
- 1:b7507ca3370f
Simple ranging example, using polling, with expansion board sensor and satellite boards.
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 | 2:819f8323b02d | 3 | * using the onboard embedded centre sensor, and 2 Satellite boards. |
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 | uint8_t ready_left = 0; |
johnAlexander | 0:6b7696e7df5e | 51 | uint8_t ready_right = 0; |
johnAlexander | 0:6b7696e7df5e | 52 | uint16_t distance_centre = 0; |
johnAlexander | 0:6b7696e7df5e | 53 | uint16_t distance_left = 0; |
johnAlexander | 0:6b7696e7df5e | 54 | uint16_t distance_right = 0; |
johnAlexander | 0:6b7696e7df5e | 55 | |
johnAlexander | 0:6b7696e7df5e | 56 | printf("Hello world!\r\n"); |
johnAlexander | 0:6b7696e7df5e | 57 | |
johnAlexander | 0:6b7696e7df5e | 58 | vl53L1X_DevI2C *dev_I2C = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL); |
johnAlexander | 0:6b7696e7df5e | 59 | |
johnAlexander | 0:6b7696e7df5e | 60 | printf("I2C device created!\r\n"); |
johnAlexander | 0:6b7696e7df5e | 61 | |
johnAlexander | 0:6b7696e7df5e | 62 | /* creates the 53L1A1 expansion board singleton obj */ |
johnAlexander | 0:6b7696e7df5e | 63 | board = XNucleo53L1A1::instance(dev_I2C, A2, D9, D2); |
johnAlexander | 0:6b7696e7df5e | 64 | printf("board created!\r\n"); |
johnAlexander | 0:6b7696e7df5e | 65 | |
johnAlexander | 0:6b7696e7df5e | 66 | /* init the 53L1A1 expansion board with default values */ |
johnAlexander | 0:6b7696e7df5e | 67 | status = board->init_board(); |
dmathew | 1:b7507ca3370f | 68 | if (status) { |
johnAlexander | 0:6b7696e7df5e | 69 | printf("Failed to init board!\r\n"); |
johnAlexander | 0:6b7696e7df5e | 70 | return status; |
johnAlexander | 0:6b7696e7df5e | 71 | } |
johnAlexander | 0:6b7696e7df5e | 72 | |
johnAlexander | 0:6b7696e7df5e | 73 | printf("board initiated! - %d\r\n", status); |
johnAlexander | 0:6b7696e7df5e | 74 | |
johnAlexander | 0:6b7696e7df5e | 75 | /* Start ranging on the centre sensor */ |
dmathew | 1:b7507ca3370f | 76 | if (board->sensor_centre != NULL) { |
dmathew | 1:b7507ca3370f | 77 | status = board->sensor_centre->VL53L1X_StartRanging(); |
dmathew | 1:b7507ca3370f | 78 | if (status != 0) { |
dmathew | 1:b7507ca3370f | 79 | printf("Centre sensor failed to start ranging!\r\n"); |
dmathew | 1:b7507ca3370f | 80 | return status; |
dmathew | 1:b7507ca3370f | 81 | } |
johnAlexander | 0:6b7696e7df5e | 82 | } |
johnAlexander | 0:6b7696e7df5e | 83 | |
johnAlexander | 0:6b7696e7df5e | 84 | /* Start ranging on the left sensor */ |
dmathew | 1:b7507ca3370f | 85 | if (board->sensor_left != NULL) { |
dmathew | 1:b7507ca3370f | 86 | status = board->sensor_left->VL53L1X_StartRanging(); |
dmathew | 1:b7507ca3370f | 87 | if (status != 0) { |
dmathew | 1:b7507ca3370f | 88 | printf("Left sensor failed to start ranging!\r\n"); |
dmathew | 1:b7507ca3370f | 89 | return status; |
dmathew | 1:b7507ca3370f | 90 | } |
johnAlexander | 0:6b7696e7df5e | 91 | } |
johnAlexander | 0:6b7696e7df5e | 92 | |
johnAlexander | 0:6b7696e7df5e | 93 | /* Start ranging on the right sensor */ |
dmathew | 1:b7507ca3370f | 94 | if (board->sensor_right != NULL) { |
dmathew | 1:b7507ca3370f | 95 | status = board->sensor_right->VL53L1X_StartRanging(); |
dmathew | 1:b7507ca3370f | 96 | if (status != 0) { |
dmathew | 1:b7507ca3370f | 97 | printf("Right sensor failed to start ranging!\r\n"); |
dmathew | 1:b7507ca3370f | 98 | return status; |
dmathew | 1:b7507ca3370f | 99 | } |
johnAlexander | 0:6b7696e7df5e | 100 | } |
johnAlexander | 0:6b7696e7df5e | 101 | |
johnAlexander | 0:6b7696e7df5e | 102 | /* Ranging loop |
johnAlexander | 0:6b7696e7df5e | 103 | * Checks each sensor for data ready |
johnAlexander | 0:6b7696e7df5e | 104 | */ |
johnAlexander | 0:6b7696e7df5e | 105 | while (1) |
johnAlexander | 0:6b7696e7df5e | 106 | { |
dmathew | 1:b7507ca3370f | 107 | if (polling_stop) |
dmathew | 1:b7507ca3370f | 108 | { |
dmathew | 1:b7507ca3370f | 109 | printf("\r\nEnding loop mode \r\n"); |
dmathew | 1:b7507ca3370f | 110 | break; |
dmathew | 1:b7507ca3370f | 111 | } |
dmathew | 1:b7507ca3370f | 112 | if (board->sensor_centre != NULL) { |
dmathew | 1:b7507ca3370f | 113 | board->sensor_centre->VL53L1X_CheckForDataReady(&ready_centre); |
dmathew | 1:b7507ca3370f | 114 | } |
dmathew | 1:b7507ca3370f | 115 | if (board->sensor_left != NULL) { |
dmathew | 1:b7507ca3370f | 116 | board->sensor_left->VL53L1X_CheckForDataReady(&ready_left); |
dmathew | 1:b7507ca3370f | 117 | } |
dmathew | 1:b7507ca3370f | 118 | if (board->sensor_right != NULL) { |
dmathew | 1:b7507ca3370f | 119 | board->sensor_right->VL53L1X_CheckForDataReady(&ready_right); |
dmathew | 1:b7507ca3370f | 120 | } |
johnAlexander | 0:6b7696e7df5e | 121 | if (ready_centre) { |
johnAlexander | 0:6b7696e7df5e | 122 | board->sensor_centre->VL53L1X_GetRangeStatus(&ready_centre); |
johnAlexander | 0:6b7696e7df5e | 123 | board->sensor_centre->VL53L1X_GetDistance(&distance_centre); |
johnAlexander | 0:6b7696e7df5e | 124 | } |
johnAlexander | 0:6b7696e7df5e | 125 | if (ready_left) { |
dmathew | 1:b7507ca3370f | 126 | board->sensor_left->VL53L1X_GetRangeStatus(&ready_left); |
dmathew | 1:b7507ca3370f | 127 | board->sensor_left->VL53L1X_GetDistance(&distance_left); |
johnAlexander | 0:6b7696e7df5e | 128 | } |
johnAlexander | 0:6b7696e7df5e | 129 | if (ready_right) { |
dmathew | 1:b7507ca3370f | 130 | board->sensor_right->VL53L1X_GetRangeStatus(&ready_right); |
dmathew | 1:b7507ca3370f | 131 | board->sensor_right->VL53L1X_GetDistance(&distance_right); |
johnAlexander | 0:6b7696e7df5e | 132 | } |
johnAlexander | 0:6b7696e7df5e | 133 | |
dmathew | 1:b7507ca3370f | 134 | if (board->sensor_centre != NULL) { |
johnAlexander | 2:819f8323b02d | 135 | printf("Distance read by centre sensor is : %d\r\n", distance_centre); |
dmathew | 1:b7507ca3370f | 136 | } |
dmathew | 1:b7507ca3370f | 137 | if (board->sensor_left != NULL) { |
johnAlexander | 2:819f8323b02d | 138 | printf("Distance read by left satellite sensor is : %d\r\n", distance_left); |
dmathew | 1:b7507ca3370f | 139 | } |
dmathew | 1:b7507ca3370f | 140 | if (board->sensor_right != NULL) { |
johnAlexander | 2:819f8323b02d | 141 | printf("Distance read by right satellite sensor is : %d\r\n", distance_right); |
dmathew | 1:b7507ca3370f | 142 | } |
johnAlexander | 0:6b7696e7df5e | 143 | } |
johnAlexander | 0:6b7696e7df5e | 144 | |
johnAlexander | 0:6b7696e7df5e | 145 | return status; |
johnAlexander | 0:6b7696e7df5e | 146 | } |