Range with a (green) VL53L1X Satellite board connected directly to the motherboard, without Expansion board support.

Dependencies:   X_NUCLEO_53L1A1_mbed

Committer:
johnAlexander
Date:
Wed Jul 24 14:26:03 2019 +0000
Revision:
2:91088f06f39e
Parent:
1:e5cce6b28b6f
Child:
3:78fae258d69f
Update for non-ST boards.

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