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

Committer:
johnAlexander
Date:
Wed Jul 24 12:53:28 2019 +0000
Revision:
22:49949975c59b
Parent:
21:bb3dcf713446
Refactored init-sensor.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dmathew 17:e7a6fd6d3c97 1 /*
johnAlexander 19:29699fbc39b8 2 * This VL53L1X Expansion board sample application performs range measurements
johnAlexander 19:29699fbc39b8 3 * with interrupts enabled to generate a hardware interrupt each time a new
johnAlexander 19:29699fbc39b8 4 * measurement is ready to be read.
dmathew 17:e7a6fd6d3c97 5 *
johnAlexander 19:29699fbc39b8 6 * Measured ranges are output on the Serial Port, running at 115200 baud.
dmathew 17:e7a6fd6d3c97 7 *
johnAlexander 19:29699fbc39b8 8 * The application supports the centre, on-board, sensor and up to two satellite boards.
dmathew 17:e7a6fd6d3c97 9 *
johnAlexander 21:bb3dcf713446 10 * On STM32-Nucleo boards :
johnAlexander 21:bb3dcf713446 11 * The User Blue button stops the current measurement and entire program,
johnAlexander 21:bb3dcf713446 12 * releasing all resources.
dmathew 17:e7a6fd6d3c97 13 *
johnAlexander 21:bb3dcf713446 14 * The Black Reset button is used to restart the program.
johnAlexander 19:29699fbc39b8 15 *
johnAlexander 19:29699fbc39b8 16 * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of
johnAlexander 21:bb3dcf713446 17 * the X-NUCELO-53L1A1 expansion board are not made/OFF.
dmathew 17:e7a6fd6d3c97 18 * These links must be made to allow interrupts from the Satellite boards
dmathew 17:e7a6fd6d3c97 19 * to be received.
dmathew 17:e7a6fd6d3c97 20 * U11 and U18 must be made/ON to allow interrupts to be received from the
dmathew 17:e7a6fd6d3c97 21 * INT_L & INT_R positions; or
dmathew 17:e7a6fd6d3c97 22 * U10 and U15 must be made/ON to allow interrupts to be received from the
dmathew 17:e7a6fd6d3c97 23 * Alternate INT_L & INT_R positions.
dmathew 17:e7a6fd6d3c97 24 * The X_NUCLEO_53L1A1 firmware library defaults to use the INT_L/INT_R
dmathew 17:e7a6fd6d3c97 25 * positions.
dmathew 17:e7a6fd6d3c97 26 * INT_L is available on expansion board Arduino Connector CN5, pin 1 as D9.
dmathew 17:e7a6fd6d3c97 27 * Alternate INT_L is on CN5 Connector pin 2 as D8.
dmathew 17:e7a6fd6d3c97 28 * INT_R is available on expansion board Arduino Connector CN9, pin 3 as D4.
dmathew 17:e7a6fd6d3c97 29 * Alternate INT_R is on CN9 Connector pin 5 as D2.
dmathew 17:e7a6fd6d3c97 30 * The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A1/
dmathew 17:e7a6fd6d3c97 31 */
johnAlexander 19:29699fbc39b8 32
dmathew 17:e7a6fd6d3c97 33 #include <stdio.h>
dmathew 17:e7a6fd6d3c97 34
johnAlexander 0:ce8359133ae6 35 #include "mbed.h"
JerrySzczurak 14:d3904b05aad6 36 #include "XNucleo53L1A1.h"
johnAlexander 22:49949975c59b 37 #include "VL53L1X_I2C.h"
johnAlexander 0:ce8359133ae6 38
johnAlexander 19:29699fbc39b8 39 #define VL53L1_I2C_SDA D14
johnAlexander 19:29699fbc39b8 40 #define VL53L1_I2C_SCL D15
johnAlexander 19:29699fbc39b8 41
johnAlexander 21:bb3dcf713446 42 #if TARGET_STM // we are cross compiling for an STM32-Nucleo
johnAlexander 21:bb3dcf713446 43 InterruptIn stop_button(USER_BUTTON);
johnAlexander 21:bb3dcf713446 44 #endif
johnAlexander 21:bb3dcf713446 45 #if TARGET_Freescale // we are cross-compiling for NXP FRDM boards.
johnAlexander 21:bb3dcf713446 46 InterruptIn stop_button(SW2);
johnAlexander 19:29699fbc39b8 47 #endif
johnAlexander 19:29699fbc39b8 48
johnAlexander 19:29699fbc39b8 49 /* Installed sensors count */
johnAlexander 19:29699fbc39b8 50 int sensorCnt = 0;
johnAlexander 19:29699fbc39b8 51
johnAlexander 19:29699fbc39b8 52 /* installed sensors prefixes */
johnAlexander 19:29699fbc39b8 53 char installedSensors[3];
JerrySzczurak 14:d3904b05aad6 54
JerrySzczurak 14:d3904b05aad6 55 static XNucleo53L1A1 *board=NULL;
johnAlexander 20:e3c04690aee2 56 //Serial pc(SERIAL_TX, SERIAL_RX);
dmathew 17:e7a6fd6d3c97 57
johnAlexander 19:29699fbc39b8 58 /* interrupt requests */
johnAlexander 19:29699fbc39b8 59 volatile bool centerSensor = false;
johnAlexander 19:29699fbc39b8 60 volatile bool leftSensor = false;
johnAlexander 19:29699fbc39b8 61 volatile bool rightSensor = false;
johnAlexander 19:29699fbc39b8 62 volatile bool int_measuring_stop = false;
johnAlexander 19:29699fbc39b8 63 /* Current sensor number*/
johnAlexander 19:29699fbc39b8 64 volatile int currentSensor = 0;
johnAlexander 19:29699fbc39b8 65
johnAlexander 19:29699fbc39b8 66 /* current displayed sensor change IRQ */
johnAlexander 19:29699fbc39b8 67 volatile bool switchChanged = false;
johnAlexander 19:29699fbc39b8 68
johnAlexander 19:29699fbc39b8 69 /* ISR callback function of the centre sensor */
johnAlexander 19:29699fbc39b8 70 void sensor_centre_irq(void)
johnAlexander 19:29699fbc39b8 71 {
johnAlexander 19:29699fbc39b8 72 centerSensor = true;
johnAlexander 19:29699fbc39b8 73 board->sensor_centre->disable_interrupt_measure_detection_irq();
johnAlexander 19:29699fbc39b8 74 }
johnAlexander 19:29699fbc39b8 75
johnAlexander 19:29699fbc39b8 76 /* ISR callback function of the left sensor */
johnAlexander 19:29699fbc39b8 77 void sensor_left_irq(void)
johnAlexander 19:29699fbc39b8 78 {
johnAlexander 19:29699fbc39b8 79 leftSensor = true;
johnAlexander 19:29699fbc39b8 80 board->sensor_left->disable_interrupt_measure_detection_irq();
johnAlexander 19:29699fbc39b8 81 }
johnAlexander 19:29699fbc39b8 82
johnAlexander 19:29699fbc39b8 83 /* ISR callback function of the right sensor */
johnAlexander 19:29699fbc39b8 84 void sensor_right_irq(void)
johnAlexander 19:29699fbc39b8 85 {
johnAlexander 19:29699fbc39b8 86 rightSensor = true;
johnAlexander 19:29699fbc39b8 87 board->sensor_right->disable_interrupt_measure_detection_irq();
johnAlexander 19:29699fbc39b8 88 }
johnAlexander 19:29699fbc39b8 89
johnAlexander 19:29699fbc39b8 90 /* ISR callback function of the user blue button to switch measuring sensor. */
johnAlexander 19:29699fbc39b8 91 void switch_measuring_sensor_irq(void)
johnAlexander 19:29699fbc39b8 92 {
johnAlexander 19:29699fbc39b8 93 stop_button.disable_irq();
johnAlexander 19:29699fbc39b8 94 switchChanged = true;
johnAlexander 19:29699fbc39b8 95 }
dmathew 17:e7a6fd6d3c97 96
johnAlexander 19:29699fbc39b8 97 /*
johnAlexander 19:29699fbc39b8 98 * This function calls the interrupt handler for each sensor
johnAlexander 19:29699fbc39b8 99 * and outputs the range
johnAlexander 19:29699fbc39b8 100 */
johnAlexander 19:29699fbc39b8 101 inline void measure_sensors()
dmathew 17:e7a6fd6d3c97 102 {
johnAlexander 19:29699fbc39b8 103 bool current = false;
johnAlexander 19:29699fbc39b8 104 uint16_t distance = 0;
dmathew 17:e7a6fd6d3c97 105
johnAlexander 19:29699fbc39b8 106 /* Handle the interrupt and output the range from the centre sensor */
johnAlexander 19:29699fbc39b8 107 if (centerSensor) {
johnAlexander 19:29699fbc39b8 108 centerSensor = false;
johnAlexander 19:29699fbc39b8 109 board->sensor_centre->handle_irq(&distance);
johnAlexander 19:29699fbc39b8 110 current = (currentSensor == 0);
johnAlexander 19:29699fbc39b8 111 if (current) {
johnAlexander 19:29699fbc39b8 112 printf("Centre: %d\r\n", distance);
johnAlexander 19:29699fbc39b8 113 }
johnAlexander 19:29699fbc39b8 114 }
johnAlexander 19:29699fbc39b8 115
johnAlexander 19:29699fbc39b8 116 /* Handle the interrupt and output the range from the left sensor */
johnAlexander 19:29699fbc39b8 117 if (leftSensor) {
johnAlexander 19:29699fbc39b8 118 leftSensor = false;
johnAlexander 19:29699fbc39b8 119 board->sensor_left->handle_irq(&distance);
johnAlexander 19:29699fbc39b8 120 current = (installedSensors[currentSensor] == 'L');
johnAlexander 19:29699fbc39b8 121 if (current) {
johnAlexander 19:29699fbc39b8 122 printf("Left: %d\r\n", distance);
johnAlexander 19:29699fbc39b8 123 }
johnAlexander 19:29699fbc39b8 124 }
johnAlexander 19:29699fbc39b8 125
johnAlexander 19:29699fbc39b8 126 /* Handle the interrupt and output the range from the right sensor */
johnAlexander 19:29699fbc39b8 127 if (rightSensor) {
johnAlexander 19:29699fbc39b8 128 rightSensor = false;
johnAlexander 19:29699fbc39b8 129 board->sensor_right->handle_irq(&distance);
johnAlexander 19:29699fbc39b8 130 current = (installedSensors[currentSensor] == 'R');
johnAlexander 19:29699fbc39b8 131 if (current) {
johnAlexander 19:29699fbc39b8 132 printf("Right: %d\r\n", distance);
johnAlexander 19:29699fbc39b8 133 }
johnAlexander 19:29699fbc39b8 134 }
johnAlexander 19:29699fbc39b8 135 }
johnAlexander 19:29699fbc39b8 136
johnAlexander 19:29699fbc39b8 137 /*
johnAlexander 19:29699fbc39b8 138 * Add to an array a character that represents the sensor and start ranging
johnAlexander 19:29699fbc39b8 139 */
johnAlexander 19:29699fbc39b8 140 int init_sensors_array()
johnAlexander 19:29699fbc39b8 141 {
dmathew 17:e7a6fd6d3c97 142 int status = 0;
johnAlexander 19:29699fbc39b8 143 sensorCnt = 0;
johnAlexander 19:29699fbc39b8 144 /* start the measure on the center sensor */
johnAlexander 19:29699fbc39b8 145 if (NULL != board->sensor_centre) {
johnAlexander 19:29699fbc39b8 146 installedSensors[sensorCnt] = 'C';
johnAlexander 19:29699fbc39b8 147 status = board->sensor_centre->stop_measurement();
johnAlexander 19:29699fbc39b8 148 if (status != 0) {
johnAlexander 19:29699fbc39b8 149 return status;
johnAlexander 19:29699fbc39b8 150 }
johnAlexander 19:29699fbc39b8 151 status = board->sensor_centre->start_measurement(&sensor_centre_irq);
johnAlexander 19:29699fbc39b8 152 if (status != 0) {
johnAlexander 19:29699fbc39b8 153 return status;
johnAlexander 19:29699fbc39b8 154 }
johnAlexander 19:29699fbc39b8 155 ++sensorCnt;
johnAlexander 19:29699fbc39b8 156 }
johnAlexander 19:29699fbc39b8 157 /* start the measure on the left sensor */
johnAlexander 19:29699fbc39b8 158 if (NULL != board->sensor_left) {
johnAlexander 19:29699fbc39b8 159 installedSensors[sensorCnt] = 'L';
johnAlexander 19:29699fbc39b8 160 status = board->sensor_left->stop_measurement();
johnAlexander 19:29699fbc39b8 161 if (status != 0) {
johnAlexander 19:29699fbc39b8 162 return status;
johnAlexander 19:29699fbc39b8 163 }
johnAlexander 19:29699fbc39b8 164 status = board->sensor_left->start_measurement(&sensor_left_irq);
johnAlexander 19:29699fbc39b8 165 if (status != 0) {
johnAlexander 19:29699fbc39b8 166 return status;
johnAlexander 19:29699fbc39b8 167 }
johnAlexander 19:29699fbc39b8 168 ++sensorCnt;
johnAlexander 19:29699fbc39b8 169 }
johnAlexander 19:29699fbc39b8 170 /* start the measure on the right sensor */
johnAlexander 19:29699fbc39b8 171 if (NULL != board->sensor_right) {
johnAlexander 19:29699fbc39b8 172 installedSensors[sensorCnt] = 'R';
johnAlexander 19:29699fbc39b8 173 status = board->sensor_right->stop_measurement();
johnAlexander 19:29699fbc39b8 174 if (status != 0) {
johnAlexander 19:29699fbc39b8 175 return status;
johnAlexander 19:29699fbc39b8 176 }
johnAlexander 19:29699fbc39b8 177 status = board->sensor_right->start_measurement(&sensor_right_irq);
johnAlexander 19:29699fbc39b8 178 if (status != 0) {
johnAlexander 19:29699fbc39b8 179 return status;
johnAlexander 19:29699fbc39b8 180 }
johnAlexander 19:29699fbc39b8 181 ++sensorCnt;
johnAlexander 19:29699fbc39b8 182 }
johnAlexander 19:29699fbc39b8 183 currentSensor = 0;
johnAlexander 19:29699fbc39b8 184 return status;
johnAlexander 19:29699fbc39b8 185 }
JerrySzczurak 14:d3904b05aad6 186
johnAlexander 19:29699fbc39b8 187 /*
johnAlexander 19:29699fbc39b8 188 * Main ranging function
johnAlexander 19:29699fbc39b8 189 */
johnAlexander 22:49949975c59b 190 void range_measure(VL53L1X_DevI2C *device_i2c)
johnAlexander 19:29699fbc39b8 191 {
johnAlexander 19:29699fbc39b8 192 int status = 0;
dmathew 17:e7a6fd6d3c97 193
dmathew 17:e7a6fd6d3c97 194 /* creates the 53L1A1 expansion board singleton obj */
johnAlexander 20:e3c04690aee2 195 board = XNucleo53L1A1::instance(device_i2c, A2, D9, D2);
johnAlexander 3:b3f70617a6b3 196
dmathew 17:e7a6fd6d3c97 197 /* init the 53L1A1 expansion board with default values */
johnAlexander 7:c8087e7333b8 198 status = board->init_board();
dmathew 17:e7a6fd6d3c97 199 if (status != 0) {
Davidroid 10:891e10d3b4a6 200 printf("Failed to init board!\r\n");
johnAlexander 21:bb3dcf713446 201 return;
johnAlexander 7:c8087e7333b8 202 }
dmathew 17:e7a6fd6d3c97 203
johnAlexander 19:29699fbc39b8 204 /* init an array with chars to id the sensors */
johnAlexander 19:29699fbc39b8 205 status = init_sensors_array();
dmathew 17:e7a6fd6d3c97 206 if (status != 0) {
johnAlexander 19:29699fbc39b8 207 printf("Failed to init sensors!\r\n");
johnAlexander 21:bb3dcf713446 208 return;
dmathew 17:e7a6fd6d3c97 209 }
dmathew 17:e7a6fd6d3c97 210
johnAlexander 19:29699fbc39b8 211 printf("Entering loop mode\r\n");
johnAlexander 4:c8932fb926d6 212
johnAlexander 19:29699fbc39b8 213 /* Main ranging interrupt loop */
johnAlexander 19:29699fbc39b8 214 while (true) {
johnAlexander 19:29699fbc39b8 215 measure_sensors();
johnAlexander 19:29699fbc39b8 216 if (switchChanged) {
johnAlexander 19:29699fbc39b8 217 ++currentSensor;
johnAlexander 19:29699fbc39b8 218 if (currentSensor == sensorCnt)
johnAlexander 19:29699fbc39b8 219 currentSensor = 0;
johnAlexander 19:29699fbc39b8 220 printf("Sensor changed to %c\r\n", installedSensors[currentSensor]);
johnAlexander 19:29699fbc39b8 221 switchChanged = false;
johnAlexander 19:29699fbc39b8 222 stop_button.enable_irq();
dmathew 17:e7a6fd6d3c97 223 }
dmathew 17:e7a6fd6d3c97 224 }
johnAlexander 0:ce8359133ae6 225 }
johnAlexander 19:29699fbc39b8 226
johnAlexander 19:29699fbc39b8 227 /*=================================== Main ==================================
johnAlexander 19:29699fbc39b8 228 =============================================================================*/
johnAlexander 19:29699fbc39b8 229 int main()
johnAlexander 19:29699fbc39b8 230 {
johnAlexander 19:29699fbc39b8 231 stop_button.rise(&switch_measuring_sensor_irq);
johnAlexander 19:29699fbc39b8 232 stop_button.enable_irq();
johnAlexander 21:bb3dcf713446 233
johnAlexander 22:49949975c59b 234 VL53L1X_DevI2C *dev_I2C = new VL53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL);
johnAlexander 19:29699fbc39b8 235 range_measure(dev_I2C); // start continuous measures
johnAlexander 21:bb3dcf713446 236
johnAlexander 21:bb3dcf713446 237 return 0;
johnAlexander 19:29699fbc39b8 238 }
johnAlexander 19:29699fbc39b8 239