Simple Ranging example, using Polling, with Expansion Board and Satellite Boards.

Dependencies:   X_NUCLEO_53L1A1

Committer:
dmathew
Date:
Thu May 23 13:00:36 2019 +0000
Revision:
1:b7507ca3370f
Parent:
0:6b7696e7df5e
Child:
2:819f8323b02d
Update polling example with NULL checks to allow only one device attached to the nucleo.  Added stop to user button

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 *
dmathew 1:b7507ca3370f 5 * Measured ranges are ouput on the Serial Port, running at 96000 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;
dmathew 1:b7507ca3370f 39 Serial pc(SERIAL_TX, SERIAL_RX);
johnAlexander 0:6b7696e7df5e 40
dmathew 1:b7507ca3370f 41 volatile bool polling_stop = false;
dmathew 1:b7507ca3370f 42
dmathew 1:b7507ca3370f 43 void stop_polling(void)
dmathew 1:b7507ca3370f 44 {
dmathew 1:b7507ca3370f 45 polling_stop = true;
dmathew 1:b7507ca3370f 46 }
johnAlexander 0:6b7696e7df5e 47
johnAlexander 0:6b7696e7df5e 48 /*=================================== Main ==================================
johnAlexander 0:6b7696e7df5e 49 =============================================================================*/
johnAlexander 0:6b7696e7df5e 50 int main()
johnAlexander 0:6b7696e7df5e 51 {
dmathew 1:b7507ca3370f 52 #if USER_BUTTON==PC_13 // we are cross compiling for Nucleo-f401
dmathew 1:b7507ca3370f 53 InterruptIn stop_button(USER_BUTTON);
dmathew 1:b7507ca3370f 54 stop_button.rise(&stop_polling);
dmathew 1:b7507ca3370f 55 #endif
johnAlexander 0:6b7696e7df5e 56 int status = 0;
johnAlexander 0:6b7696e7df5e 57 uint8_t ready_centre = 0;
johnAlexander 0:6b7696e7df5e 58 uint8_t ready_left = 0;
johnAlexander 0:6b7696e7df5e 59 uint8_t ready_right = 0;
johnAlexander 0:6b7696e7df5e 60 uint16_t distance_centre = 0;
johnAlexander 0:6b7696e7df5e 61 uint16_t distance_left = 0;
johnAlexander 0:6b7696e7df5e 62 uint16_t distance_right = 0;
johnAlexander 0:6b7696e7df5e 63
johnAlexander 0:6b7696e7df5e 64 printf("Hello world!\r\n");
johnAlexander 0:6b7696e7df5e 65
johnAlexander 0:6b7696e7df5e 66 vl53L1X_DevI2C *dev_I2C = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL);
johnAlexander 0:6b7696e7df5e 67
johnAlexander 0:6b7696e7df5e 68 printf("I2C device created!\r\n");
johnAlexander 0:6b7696e7df5e 69
johnAlexander 0:6b7696e7df5e 70 /* creates the 53L1A1 expansion board singleton obj */
johnAlexander 0:6b7696e7df5e 71 board = XNucleo53L1A1::instance(dev_I2C, A2, D9, D2);
johnAlexander 0:6b7696e7df5e 72 printf("board created!\r\n");
johnAlexander 0:6b7696e7df5e 73
johnAlexander 0:6b7696e7df5e 74 /* init the 53L1A1 expansion board with default values */
johnAlexander 0:6b7696e7df5e 75 status = board->init_board();
dmathew 1:b7507ca3370f 76 if (status) {
johnAlexander 0:6b7696e7df5e 77 printf("Failed to init board!\r\n");
johnAlexander 0:6b7696e7df5e 78 return status;
johnAlexander 0:6b7696e7df5e 79 }
johnAlexander 0:6b7696e7df5e 80
johnAlexander 0:6b7696e7df5e 81 printf("board initiated! - %d\r\n", status);
johnAlexander 0:6b7696e7df5e 82
johnAlexander 0:6b7696e7df5e 83 /* Start ranging on the centre sensor */
dmathew 1:b7507ca3370f 84 if (board->sensor_centre != NULL) {
dmathew 1:b7507ca3370f 85 status = board->sensor_centre->VL53L1X_StartRanging();
dmathew 1:b7507ca3370f 86 if (status != 0) {
dmathew 1:b7507ca3370f 87 printf("Centre sensor failed to start ranging!\r\n");
dmathew 1:b7507ca3370f 88 return status;
dmathew 1:b7507ca3370f 89 }
johnAlexander 0:6b7696e7df5e 90 }
johnAlexander 0:6b7696e7df5e 91
johnAlexander 0:6b7696e7df5e 92 /* Start ranging on the left sensor */
dmathew 1:b7507ca3370f 93 if (board->sensor_left != NULL) {
dmathew 1:b7507ca3370f 94 status = board->sensor_left->VL53L1X_StartRanging();
dmathew 1:b7507ca3370f 95 if (status != 0) {
dmathew 1:b7507ca3370f 96 printf("Left sensor failed to start ranging!\r\n");
dmathew 1:b7507ca3370f 97 return status;
dmathew 1:b7507ca3370f 98 }
johnAlexander 0:6b7696e7df5e 99 }
johnAlexander 0:6b7696e7df5e 100
johnAlexander 0:6b7696e7df5e 101 /* Start ranging on the right sensor */
dmathew 1:b7507ca3370f 102 if (board->sensor_right != NULL) {
dmathew 1:b7507ca3370f 103 status = board->sensor_right->VL53L1X_StartRanging();
dmathew 1:b7507ca3370f 104 if (status != 0) {
dmathew 1:b7507ca3370f 105 printf("Right sensor failed to start ranging!\r\n");
dmathew 1:b7507ca3370f 106 return status;
dmathew 1:b7507ca3370f 107 }
johnAlexander 0:6b7696e7df5e 108 }
johnAlexander 0:6b7696e7df5e 109
johnAlexander 0:6b7696e7df5e 110 /* Ranging loop
johnAlexander 0:6b7696e7df5e 111 * Checks each sensor for data ready
johnAlexander 0:6b7696e7df5e 112 */
johnAlexander 0:6b7696e7df5e 113 while (1)
johnAlexander 0:6b7696e7df5e 114 {
dmathew 1:b7507ca3370f 115 if (polling_stop)
dmathew 1:b7507ca3370f 116 {
dmathew 1:b7507ca3370f 117 printf("\r\nEnding loop mode \r\n");
dmathew 1:b7507ca3370f 118 break;
dmathew 1:b7507ca3370f 119 }
dmathew 1:b7507ca3370f 120 if (board->sensor_centre != NULL) {
dmathew 1:b7507ca3370f 121 board->sensor_centre->VL53L1X_CheckForDataReady(&ready_centre);
dmathew 1:b7507ca3370f 122 }
dmathew 1:b7507ca3370f 123 if (board->sensor_left != NULL) {
dmathew 1:b7507ca3370f 124 board->sensor_left->VL53L1X_CheckForDataReady(&ready_left);
dmathew 1:b7507ca3370f 125 }
dmathew 1:b7507ca3370f 126 if (board->sensor_right != NULL) {
dmathew 1:b7507ca3370f 127 board->sensor_right->VL53L1X_CheckForDataReady(&ready_right);
dmathew 1:b7507ca3370f 128 }
johnAlexander 0:6b7696e7df5e 129 if (ready_centre) {
johnAlexander 0:6b7696e7df5e 130 board->sensor_centre->VL53L1X_GetRangeStatus(&ready_centre);
johnAlexander 0:6b7696e7df5e 131 board->sensor_centre->VL53L1X_GetDistance(&distance_centre);
johnAlexander 0:6b7696e7df5e 132 }
johnAlexander 0:6b7696e7df5e 133 if (ready_left) {
dmathew 1:b7507ca3370f 134 board->sensor_left->VL53L1X_GetRangeStatus(&ready_left);
dmathew 1:b7507ca3370f 135 board->sensor_left->VL53L1X_GetDistance(&distance_left);
johnAlexander 0:6b7696e7df5e 136 }
johnAlexander 0:6b7696e7df5e 137 if (ready_right) {
dmathew 1:b7507ca3370f 138 board->sensor_right->VL53L1X_GetRangeStatus(&ready_right);
dmathew 1:b7507ca3370f 139 board->sensor_right->VL53L1X_GetDistance(&distance_right);
johnAlexander 0:6b7696e7df5e 140 }
johnAlexander 0:6b7696e7df5e 141
dmathew 1:b7507ca3370f 142 if (board->sensor_centre != NULL) {
dmathew 1:b7507ca3370f 143 printf("%d\t", distance_centre);
dmathew 1:b7507ca3370f 144 }
dmathew 1:b7507ca3370f 145 if (board->sensor_left != NULL) {
dmathew 1:b7507ca3370f 146 printf("%d\t", distance_left);
dmathew 1:b7507ca3370f 147 }
dmathew 1:b7507ca3370f 148 if (board->sensor_right != NULL) {
dmathew 1:b7507ca3370f 149 printf("%d\t", distance_right);
dmathew 1:b7507ca3370f 150 }
johnAlexander 0:6b7696e7df5e 151 }
johnAlexander 0:6b7696e7df5e 152
johnAlexander 0:6b7696e7df5e 153 return status;
johnAlexander 0:6b7696e7df5e 154 }