Simple Ranging with only the Time-of-Flight (ToF) Sensor on the X-NUCLEO-53L1A1 expansion board. Does not use Satellite boards.

Dependencies:   X_NUCLEO_53L1A1

Committer:
johnAlexander
Date:
Fri May 17 12:24:17 2019 +0000
Revision:
1:e5cce6b28b6f
Parent:
0:6b7696e7df5e
Child:
2:46dcd0517f2a
Ranging with Central, Expansion Board, Sensor and Left/Right Satellite Board Sensors, using Interrupts, on Mbed OS 5.x.

Who changed what in which revision?

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