Simple ranging example, using expansion and satellite board sensors, with mbed OS 5.x

Dependencies:   X_NUCLEO_53L1A1_mbed

Committer:
johnAlexander
Date:
Fri May 17 10:44:38 2019 +0000
Revision:
0:6b7696e7df5e
Child:
1:b7507ca3370f
Simple ranging example using Mbed OS 5.x

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnAlexander 0:6b7696e7df5e 1 /*
johnAlexander 0:6b7696e7df5e 2 * This VL53L1X Expansion board test application performs range measurements
johnAlexander 0:6b7696e7df5e 3 * using the onboard embedded centre sensor.
johnAlexander 0:6b7696e7df5e 4 *
johnAlexander 0:6b7696e7df5e 5 * Measured ranges are ouput on the Serial Port, running at 115200 baud.
johnAlexander 0:6b7696e7df5e 6 *
johnAlexander 0:6b7696e7df5e 7 * The User Blue button stops the current measurement and entire program,
johnAlexander 0:6b7696e7df5e 8 * releasing all resources.
johnAlexander 0:6b7696e7df5e 9 *
johnAlexander 0:6b7696e7df5e 10 * The Black Reset button is used to restart the program.
johnAlexander 0:6b7696e7df5e 11 *
johnAlexander 0:6b7696e7df5e 12 * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of
johnAlexander 0:6b7696e7df5e 13 * the X-NUCELO-53L0A1 expansion board are not made/OFF.
johnAlexander 0:6b7696e7df5e 14 * These links must be made to allow interrupts from the Satellite boards
johnAlexander 0:6b7696e7df5e 15 * to be received.
johnAlexander 0:6b7696e7df5e 16 * U11 and U18 must be made/ON to allow interrupts to be received from the
johnAlexander 0:6b7696e7df5e 17 * INT_L & INT_R positions; or
johnAlexander 0:6b7696e7df5e 18 * U10 and U15 must be made/ON to allow interrupts to be received from the
johnAlexander 0:6b7696e7df5e 19 * Alternate INT_L & INT_R positions.
johnAlexander 0:6b7696e7df5e 20 * The X_NUCLEO_53L1A1 firmware library defaults to use the INT_L/INT_R
johnAlexander 0:6b7696e7df5e 21 * positions.
johnAlexander 0:6b7696e7df5e 22 * INT_L is available on expansion board Arduino Connector CN5, pin 1 as D9.
johnAlexander 0:6b7696e7df5e 23 * Alternate INT_L is on CN5 Connector pin 2 as D8.
johnAlexander 0:6b7696e7df5e 24 * INT_R is available on expansion board Arduino Connector CN9, pin 3 as D4.
johnAlexander 0:6b7696e7df5e 25 * Alternate INT_R is on CN9 Connector pin 5 as D2.
johnAlexander 0:6b7696e7df5e 26 * The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A1/
johnAlexander 0:6b7696e7df5e 27 */
johnAlexander 0:6b7696e7df5e 28
johnAlexander 0:6b7696e7df5e 29 #include <stdio.h>
johnAlexander 0:6b7696e7df5e 30
johnAlexander 0:6b7696e7df5e 31 #include "mbed.h"
johnAlexander 0:6b7696e7df5e 32 #include "XNucleo53L1A1.h"
johnAlexander 0:6b7696e7df5e 33 #include "vl53L1x_I2c.h"
johnAlexander 0:6b7696e7df5e 34
johnAlexander 0:6b7696e7df5e 35 #define VL53L1_I2C_SDA D14
johnAlexander 0:6b7696e7df5e 36 #define VL53L1_I2C_SCL D15
johnAlexander 0:6b7696e7df5e 37
johnAlexander 0:6b7696e7df5e 38 static XNucleo53L1A1 *board=NULL;
johnAlexander 0:6b7696e7df5e 39
johnAlexander 0:6b7696e7df5e 40 Serial pc(SERIAL_TX, SERIAL_RX);
johnAlexander 0:6b7696e7df5e 41
johnAlexander 0:6b7696e7df5e 42 /*=================================== Main ==================================
johnAlexander 0:6b7696e7df5e 43 =============================================================================*/
johnAlexander 0:6b7696e7df5e 44 int main()
johnAlexander 0:6b7696e7df5e 45 {
johnAlexander 0:6b7696e7df5e 46
johnAlexander 0:6b7696e7df5e 47 int status = 0;
johnAlexander 0:6b7696e7df5e 48 uint8_t ready_centre = 0;
johnAlexander 0:6b7696e7df5e 49 uint8_t ready_left = 0;
johnAlexander 0:6b7696e7df5e 50 uint8_t ready_right = 0;
johnAlexander 0:6b7696e7df5e 51 uint16_t distance_centre = 0;
johnAlexander 0:6b7696e7df5e 52 uint16_t distance_left = 0;
johnAlexander 0:6b7696e7df5e 53 uint16_t distance_right = 0;
johnAlexander 0:6b7696e7df5e 54
johnAlexander 0:6b7696e7df5e 55 printf("Hello world!\r\n");
johnAlexander 0:6b7696e7df5e 56
johnAlexander 0:6b7696e7df5e 57 vl53L1X_DevI2C *dev_I2C = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL);
johnAlexander 0:6b7696e7df5e 58
johnAlexander 0:6b7696e7df5e 59 printf("I2C device created!\r\n");
johnAlexander 0:6b7696e7df5e 60
johnAlexander 0:6b7696e7df5e 61 /* creates the 53L1A1 expansion board singleton obj */
johnAlexander 0:6b7696e7df5e 62 board = XNucleo53L1A1::instance(dev_I2C, A2, D9, D2);
johnAlexander 0:6b7696e7df5e 63 printf("board created!\r\n");
johnAlexander 0:6b7696e7df5e 64
johnAlexander 0:6b7696e7df5e 65 /* init the 53L1A1 expansion board with default values */
johnAlexander 0:6b7696e7df5e 66 status = board->init_board();
johnAlexander 0:6b7696e7df5e 67 if (status != 0) {
johnAlexander 0:6b7696e7df5e 68 printf("Failed to init board!\r\n");
johnAlexander 0:6b7696e7df5e 69 return status;
johnAlexander 0:6b7696e7df5e 70 }
johnAlexander 0:6b7696e7df5e 71
johnAlexander 0:6b7696e7df5e 72 printf("board initiated! - %d\r\n", status);
johnAlexander 0:6b7696e7df5e 73
johnAlexander 0:6b7696e7df5e 74 /* Start ranging on the centre sensor */
johnAlexander 0:6b7696e7df5e 75 status = board->sensor_centre->VL53L1X_StartRanging();
johnAlexander 0:6b7696e7df5e 76 if (status != 0) {
johnAlexander 0:6b7696e7df5e 77 printf("Centre sensor failed to start ranging!\r\n");
johnAlexander 0:6b7696e7df5e 78 return status;
johnAlexander 0:6b7696e7df5e 79 }
johnAlexander 0:6b7696e7df5e 80
johnAlexander 0:6b7696e7df5e 81 /* Start ranging on the left sensor */
johnAlexander 0:6b7696e7df5e 82 status = board->sensor_left->VL53L1X_StartRanging();
johnAlexander 0:6b7696e7df5e 83 if (status != 0) {
johnAlexander 0:6b7696e7df5e 84 printf("Left sensor failed to start ranging!\r\n");
johnAlexander 0:6b7696e7df5e 85 return status;
johnAlexander 0:6b7696e7df5e 86 }
johnAlexander 0:6b7696e7df5e 87
johnAlexander 0:6b7696e7df5e 88 /* Start ranging on the right sensor */
johnAlexander 0:6b7696e7df5e 89 status = board->sensor_right->VL53L1X_StartRanging();
johnAlexander 0:6b7696e7df5e 90 if (status != 0) {
johnAlexander 0:6b7696e7df5e 91 printf("Right sensor failed to start ranging!\r\n");
johnAlexander 0:6b7696e7df5e 92 return status;
johnAlexander 0:6b7696e7df5e 93 }
johnAlexander 0:6b7696e7df5e 94
johnAlexander 0:6b7696e7df5e 95 /* Ranging loop
johnAlexander 0:6b7696e7df5e 96 * Checks each sensor for data ready
johnAlexander 0:6b7696e7df5e 97 */
johnAlexander 0:6b7696e7df5e 98 while (1)
johnAlexander 0:6b7696e7df5e 99 {
johnAlexander 0:6b7696e7df5e 100 board->sensor_centre->VL53L1X_CheckForDataReady(&ready_centre);
johnAlexander 0:6b7696e7df5e 101 board->sensor_left->VL53L1X_CheckForDataReady(&ready_left);
johnAlexander 0:6b7696e7df5e 102 board->sensor_right->VL53L1X_CheckForDataReady(&ready_right);
johnAlexander 0:6b7696e7df5e 103
johnAlexander 0:6b7696e7df5e 104 if (ready_centre) {
johnAlexander 0:6b7696e7df5e 105 board->sensor_centre->VL53L1X_GetRangeStatus(&ready_centre);
johnAlexander 0:6b7696e7df5e 106 board->sensor_centre->VL53L1X_GetDistance(&distance_centre);
johnAlexander 0:6b7696e7df5e 107 }
johnAlexander 0:6b7696e7df5e 108 if (ready_left) {
johnAlexander 0:6b7696e7df5e 109 board->sensor_centre->VL53L1X_GetRangeStatus(&ready_left);
johnAlexander 0:6b7696e7df5e 110 board->sensor_centre->VL53L1X_GetDistance(&distance_left);
johnAlexander 0:6b7696e7df5e 111 }
johnAlexander 0:6b7696e7df5e 112 if (ready_right) {
johnAlexander 0:6b7696e7df5e 113 board->sensor_centre->VL53L1X_GetRangeStatus(&ready_right);
johnAlexander 0:6b7696e7df5e 114 board->sensor_centre->VL53L1X_GetDistance(&distance_right);
johnAlexander 0:6b7696e7df5e 115 }
johnAlexander 0:6b7696e7df5e 116
johnAlexander 0:6b7696e7df5e 117 printf("%d\t", distance_centre);
johnAlexander 0:6b7696e7df5e 118 printf("%d\t", distance_left);
johnAlexander 0:6b7696e7df5e 119 printf("%d\t", distance_right);
johnAlexander 0:6b7696e7df5e 120 }
johnAlexander 0:6b7696e7df5e 121
johnAlexander 0:6b7696e7df5e 122 return status;
johnAlexander 0:6b7696e7df5e 123 }