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

Dependencies:   VL53L1X_mbed

Committer:
johnAlexander
Date:
Thu Oct 10 09:52:56 2019 +0000
Revision:
3:78fae258d69f
Parent:
2:91088f06f39e
Port Mbed Classic example to MbedOS. Ranging using a (green) VL53L1X Satellite connected directly to the motherboard, without Expansion board support.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnAlexander 0:6b7696e7df5e 1 /*
johnAlexander 3:78fae258d69f 2 * This VL53L1X satellite 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 3:78fae258d69f 6 * Measured ranges are output on the Serial Port, running at 115200 baud.
johnAlexander 0:6b7696e7df5e 7 *
johnAlexander 3:78fae258d69f 8 * On STM32-Nucleo boards :
johnAlexander 3:78fae258d69f 9 * The User Blue button stops the current measurement and entire program,
johnAlexander 3:78fae258d69f 10 * releasing all resources.
johnAlexander 0:6b7696e7df5e 11 *
johnAlexander 3:78fae258d69f 12 * The Black Reset button is used to restart the program.
johnAlexander 1:e5cce6b28b6f 13 *
johnAlexander 3:78fae258d69f 14 * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of
johnAlexander 3:78fae258d69f 15 * the X-NUCELO-53L1A1 expansion board are not made/OFF.
johnAlexander 0:6b7696e7df5e 16 * These links must be made to allow interrupts from the Satellite boards
johnAlexander 0:6b7696e7df5e 17 * to be received.
johnAlexander 0:6b7696e7df5e 18 * U11 and U18 must be made/ON to allow interrupts to be received from the
johnAlexander 0:6b7696e7df5e 19 * INT_L & INT_R positions; or
johnAlexander 0:6b7696e7df5e 20 * U10 and U15 must be made/ON to allow interrupts to be received from the
johnAlexander 0:6b7696e7df5e 21 * Alternate INT_L & INT_R positions.
johnAlexander 0:6b7696e7df5e 22 * The X_NUCLEO_53L1A1 firmware library defaults to use the INT_L/INT_R
johnAlexander 0:6b7696e7df5e 23 * positions.
johnAlexander 0:6b7696e7df5e 24 * INT_L is available on expansion board Arduino Connector CN5, pin 1 as D9.
johnAlexander 0:6b7696e7df5e 25 * Alternate INT_L is on CN5 Connector pin 2 as D8.
johnAlexander 0:6b7696e7df5e 26 * INT_R is available on expansion board Arduino Connector CN9, pin 3 as D4.
johnAlexander 0:6b7696e7df5e 27 * Alternate INT_R is on CN9 Connector pin 5 as D2.
johnAlexander 0:6b7696e7df5e 28 * The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A1/
johnAlexander 0:6b7696e7df5e 29 */
johnAlexander 3:78fae258d69f 30
johnAlexander 0:6b7696e7df5e 31 #include <stdio.h>
johnAlexander 0:6b7696e7df5e 32
johnAlexander 0:6b7696e7df5e 33 #include "mbed.h"
johnAlexander 2:91088f06f39e 34 #include "VL53L1X_I2C.h"
johnAlexander 3:78fae258d69f 35 #include "VL53L1X_Class.h"
johnAlexander 0:6b7696e7df5e 36
johnAlexander 3:78fae258d69f 37 #define VL53L1_I2C_SDA D14
johnAlexander 3:78fae258d69f 38 #define VL53L1_I2C_SCL D15
johnAlexander 1:e5cce6b28b6f 39
johnAlexander 3:78fae258d69f 40 #if TARGET_STM // we are cross compiling for an STM32-Nucleo
johnAlexander 2:91088f06f39e 41 InterruptIn stop_button(USER_BUTTON);
johnAlexander 2:91088f06f39e 42 #endif
johnAlexander 2:91088f06f39e 43 #if TARGET_Freescale // we are cross-compiling for NXP FRDM boards.
johnAlexander 2:91088f06f39e 44 InterruptIn stop_button(SW2);
johnAlexander 1:e5cce6b28b6f 45 #endif
johnAlexander 1:e5cce6b28b6f 46
johnAlexander 3:78fae258d69f 47 static VL53L1X *sensor = NULL;
johnAlexander 3:78fae258d69f 48 //Serial pc(SERIAL_TX, SERIAL_RX);
johnAlexander 0:6b7696e7df5e 49
johnAlexander 3:78fae258d69f 50 /* flags that handle interrupt request for sensor and user blue button*/
johnAlexander 3:78fae258d69f 51 volatile bool int_sensor = false;
johnAlexander 3:78fae258d69f 52 volatile bool int_stop = false;
johnAlexander 1:e5cce6b28b6f 53
johnAlexander 1:e5cce6b28b6f 54
johnAlexander 3:78fae258d69f 55 /* ISR callback function of the sensor */
johnAlexander 3:78fae258d69f 56 void sensor_irq(void)
johnAlexander 1:e5cce6b28b6f 57 {
johnAlexander 3:78fae258d69f 58 int_sensor = true;
johnAlexander 3:78fae258d69f 59 sensor->disable_interrupt_measure_detection_irq();
johnAlexander 1:e5cce6b28b6f 60 }
johnAlexander 1:e5cce6b28b6f 61
johnAlexander 1:e5cce6b28b6f 62 /* ISR callback function of the user blue button to switch measuring sensor. */
johnAlexander 3:78fae258d69f 63 void measuring_stop_irq(void)
johnAlexander 3:78fae258d69f 64 {
johnAlexander 3:78fae258d69f 65 int_stop = true;
johnAlexander 3:78fae258d69f 66 }
johnAlexander 3:78fae258d69f 67
johnAlexander 3:78fae258d69f 68 /* Start the sensor ranging */
johnAlexander 3:78fae258d69f 69 int start_ranging()
johnAlexander 1:e5cce6b28b6f 70 {
johnAlexander 3:78fae258d69f 71 int status = 0;
johnAlexander 3:78fae258d69f 72 /* start the measure on the sensor */
johnAlexander 3:78fae258d69f 73 if (NULL != sensor) {
johnAlexander 3:78fae258d69f 74 status = sensor->stop_measurement();
johnAlexander 3:78fae258d69f 75 if (status != 0) {
johnAlexander 3:78fae258d69f 76 return status;
johnAlexander 3:78fae258d69f 77 }
johnAlexander 3:78fae258d69f 78
johnAlexander 3:78fae258d69f 79 status = sensor->start_measurement(&sensor_irq);
johnAlexander 3:78fae258d69f 80 if (status != 0) {
johnAlexander 3:78fae258d69f 81 return status;
johnAlexander 3:78fae258d69f 82 }
johnAlexander 3:78fae258d69f 83 }
johnAlexander 3:78fae258d69f 84 return status;
johnAlexander 1:e5cce6b28b6f 85 }
johnAlexander 0:6b7696e7df5e 86
johnAlexander 3:78fae258d69f 87 int range_measure(VL53L1X_DevI2C *device_i2c)
johnAlexander 0:6b7696e7df5e 88 {
johnAlexander 3:78fae258d69f 89 int status = 0;
johnAlexander 1:e5cce6b28b6f 90 uint16_t distance = 0;
johnAlexander 3:78fae258d69f 91 /* Create a xshutdown pin */
johnAlexander 3:78fae258d69f 92 DigitalOut xshutdown(D7);
johnAlexander 0:6b7696e7df5e 93
johnAlexander 3:78fae258d69f 94 /* create instance of sensor class */
johnAlexander 3:78fae258d69f 95 sensor = new VL53L1X(device_i2c, &xshutdown, A2);
johnAlexander 3:78fae258d69f 96
johnAlexander 3:78fae258d69f 97 sensor->vl53l1_off();
johnAlexander 3:78fae258d69f 98 /* initialise sensor */
johnAlexander 3:78fae258d69f 99 sensor->init_sensor(0x52);
johnAlexander 3:78fae258d69f 100
johnAlexander 3:78fae258d69f 101 if (status) {
johnAlexander 3:78fae258d69f 102 delete sensor;
johnAlexander 3:78fae258d69f 103 sensor= NULL;
johnAlexander 3:78fae258d69f 104 printf("Sensor centre not present\n\r");
johnAlexander 1:e5cce6b28b6f 105 }
johnAlexander 1:e5cce6b28b6f 106
johnAlexander 3:78fae258d69f 107 /* init an array with chars to id the sensors */
johnAlexander 3:78fae258d69f 108 status = start_ranging();
johnAlexander 3:78fae258d69f 109 if (status != 0) {
johnAlexander 3:78fae258d69f 110 printf("Failed to start ranging!\r\n");
johnAlexander 3:78fae258d69f 111 return status;
johnAlexander 3:78fae258d69f 112 }
johnAlexander 3:78fae258d69f 113
johnAlexander 3:78fae258d69f 114 if (NULL != sensor) {
johnAlexander 3:78fae258d69f 115 printf("Entering loop mode\r\n");
johnAlexander 3:78fae258d69f 116 /* Main ranging interrupt loop */
johnAlexander 3:78fae258d69f 117 while (true) {
johnAlexander 3:78fae258d69f 118 if (int_sensor) {
johnAlexander 3:78fae258d69f 119 int_sensor = false;
johnAlexander 3:78fae258d69f 120 status = sensor->handle_irq(&distance);
johnAlexander 3:78fae258d69f 121 printf("distance: %d\r\n", distance);
johnAlexander 3:78fae258d69f 122 }
johnAlexander 3:78fae258d69f 123
johnAlexander 3:78fae258d69f 124 if (int_stop) {
johnAlexander 3:78fae258d69f 125 printf("\r\nEnding loop mode \r\n");
johnAlexander 3:78fae258d69f 126 break;
johnAlexander 3:78fae258d69f 127 }
johnAlexander 1:e5cce6b28b6f 128 }
johnAlexander 1:e5cce6b28b6f 129 }
johnAlexander 1:e5cce6b28b6f 130
johnAlexander 3:78fae258d69f 131 return status;
johnAlexander 1:e5cce6b28b6f 132
johnAlexander 0:6b7696e7df5e 133 }
johnAlexander 1:e5cce6b28b6f 134
johnAlexander 1:e5cce6b28b6f 135 /*=================================== Main ==================================
johnAlexander 1:e5cce6b28b6f 136 =============================================================================*/
johnAlexander 1:e5cce6b28b6f 137 int main()
johnAlexander 1:e5cce6b28b6f 138 {
johnAlexander 3:78fae258d69f 139 stop_button.rise(&measuring_stop_irq);
johnAlexander 2:91088f06f39e 140
johnAlexander 3:78fae258d69f 141 VL53L1X_DevI2C *device_i2c = new VL53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL);
johnAlexander 3:78fae258d69f 142 range_measure(device_i2c); // start continuous measures
johnAlexander 2:91088f06f39e 143
johnAlexander 2:91088f06f39e 144 return 0;
johnAlexander 1:e5cce6b28b6f 145 }