Range with a (green) VL53L1X Satellite board connected directly to the motherboard, without Expansion board support.
Dependencies: X_NUCLEO_53L1A1_mbed
Diff: main.cpp
- Revision:
- 3:78fae258d69f
- Parent:
- 2:91088f06f39e
--- a/main.cpp Wed Jul 24 14:26:03 2019 +0000 +++ b/main.cpp Thu Oct 10 09:52:56 2019 +0000 @@ -1,20 +1,18 @@ /* - * This VL53L1X Expansion board sample application performs range measurements + * This VL53L1X satellite board sample application performs range measurements * with interrupts enabled to generate a hardware interrupt each time a new * measurement is ready to be read. * - * Measured ranges are output on the Serial Port, running at 115200 baud. - * - * The application supports the centre, on-board, sensor and up to two satellite boards. + * Measured ranges are output on the Serial Port, running at 115200 baud. * - * On STM32-Nucleo boards : - * The User Blue button stops the current measurement and entire program, - * releasing all resources. + * On STM32-Nucleo boards : + * The User Blue button stops the current measurement and entire program, + * releasing all resources. * - * The Black Reset button is used to restart the program. + * The Black Reset button is used to restart the program. * - * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of - * the X-NUCELO-53L0A1 expansion board are not made/OFF. + * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of + * the X-NUCELO-53L1A1 expansion board are not made/OFF. * These links must be made to allow interrupts from the Satellite boards * to be received. * U11 and U18 must be made/ON to allow interrupts to be received from the @@ -29,212 +27,119 @@ * Alternate INT_R is on CN9 Connector pin 5 as D2. * The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A1/ */ - + #include <stdio.h> #include "mbed.h" -#include "XNucleo53L1A1.h" #include "VL53L1X_I2C.h" +#include "VL53L1X_Class.h" -#define VL53L1_I2C_SDA D14 -#define VL53L1_I2C_SCL D15 +#define VL53L1_I2C_SDA D14 +#define VL53L1_I2C_SCL D15 -#if TARGET_STM // we are cross compiling for an STM32-Nucleo +#if TARGET_STM // we are cross compiling for an STM32-Nucleo InterruptIn stop_button(USER_BUTTON); #endif #if TARGET_Freescale // we are cross-compiling for NXP FRDM boards. InterruptIn stop_button(SW2); #endif -/* Installed sensors count */ -int sensorCnt = 0; - -/* installed sensors prefixes */ -char installedSensors[3]; - -static XNucleo53L1A1 *board=NULL; +static VL53L1X *sensor = NULL; +//Serial pc(SERIAL_TX, SERIAL_RX); -/* interrupt requests */ -volatile bool centerSensor = false; -volatile bool leftSensor = false; -volatile bool rightSensor = false; -volatile bool int_measuring_stop = false; -/* Current sensor number*/ -volatile int currentSensor = 0; - -/* current displayed sensor change IRQ */ -volatile bool switchChanged = false; +/* flags that handle interrupt request for sensor and user blue button*/ +volatile bool int_sensor = false; +volatile bool int_stop = false; -/* ISR callback function of the centre sensor */ -void sensor_centre_irq(void) -{ - centerSensor = true; - board->sensor_centre->disable_interrupt_measure_detection_irq(); -} -/* ISR callback function of the left sensor */ -void sensor_left_irq(void) +/* ISR callback function of the sensor */ +void sensor_irq(void) { - leftSensor = true; - board->sensor_left->disable_interrupt_measure_detection_irq(); -} - -/* ISR callback function of the right sensor */ -void sensor_right_irq(void) -{ - rightSensor = true; - board->sensor_right->disable_interrupt_measure_detection_irq(); + int_sensor = true; + sensor->disable_interrupt_measure_detection_irq(); } /* ISR callback function of the user blue button to switch measuring sensor. */ -void switch_measuring_sensor_irq(void) +void measuring_stop_irq(void) +{ + int_stop = true; +} + +/* Start the sensor ranging */ +int start_ranging() { - stop_button.disable_irq(); - switchChanged = true; + int status = 0; + /* start the measure on the sensor */ + if (NULL != sensor) { + status = sensor->stop_measurement(); + if (status != 0) { + return status; + } + + status = sensor->start_measurement(&sensor_irq); + if (status != 0) { + return status; + } + } + return status; } -/* - * This function calls the interrupt handler for each sensor - * and outputs the range - */ -inline void measure_sensors() +int range_measure(VL53L1X_DevI2C *device_i2c) { - bool current = false; + int status = 0; uint16_t distance = 0; + /* Create a xshutdown pin */ + DigitalOut xshutdown(D7); - /* Handle the interrupt and output the range from the centre sensor */ - if (centerSensor) { - centerSensor = false; - board->sensor_centre->handle_irq(&distance); - current = (currentSensor == 0); - if (current) { - printf("Centre: %d\r\n", distance); - } + /* create instance of sensor class */ + sensor = new VL53L1X(device_i2c, &xshutdown, A2); + + sensor->vl53l1_off(); + /* initialise sensor */ + sensor->init_sensor(0x52); + + if (status) { + delete sensor; + sensor= NULL; + printf("Sensor centre not present\n\r"); } - /* Handle the interrupt and output the range from the left sensor */ - if (leftSensor) { - leftSensor = false; - board->sensor_left->handle_irq(&distance); - current = (installedSensors[currentSensor] == 'L'); - if (current) { - printf("Left: %d\r\n", distance); + /* init an array with chars to id the sensors */ + status = start_ranging(); + if (status != 0) { + printf("Failed to start ranging!\r\n"); + return status; + } + + if (NULL != sensor) { + printf("Entering loop mode\r\n"); + /* Main ranging interrupt loop */ + while (true) { + if (int_sensor) { + int_sensor = false; + status = sensor->handle_irq(&distance); + printf("distance: %d\r\n", distance); + } + + if (int_stop) { + printf("\r\nEnding loop mode \r\n"); + break; + } } } - /* Handle the interrupt and output the range from the right sensor */ - if (rightSensor) { - rightSensor = false; - board->sensor_right->handle_irq(&distance); - current = (installedSensors[currentSensor] == 'R'); - if (current) { - printf("Right: %d\r\n", distance); - } - } -} + return status; -/* - * Add to an array a character that represents the sensor and start ranging - */ -int init_sensors_array() -{ - int status = 0; - sensorCnt = 0; - /* start the measure on the center sensor */ - if (NULL != board->sensor_centre) { - installedSensors[sensorCnt] = 'C'; - status = board->sensor_centre->stop_measurement(); - if (status != 0) { - return status; - } - status = board->sensor_centre->start_measurement(&sensor_centre_irq); - if (status != 0) { - return status; - } - ++sensorCnt; - } - /* start the measure on the left sensor */ - if (NULL != board->sensor_left) { - installedSensors[sensorCnt] = 'L'; - status = board->sensor_left->stop_measurement(); - if (status != 0) { - return status; - } - status = board->sensor_left->start_measurement(&sensor_left_irq); - if (status != 0) { - return status; - } - ++sensorCnt; - } - /* start the measure on the right sensor */ - if (NULL != board->sensor_right) { - installedSensors[sensorCnt] = 'R'; - status = board->sensor_right->stop_measurement(); - if (status != 0) { - return status; - } - status = board->sensor_right->start_measurement(&sensor_right_irq); - if (status != 0) { - return status; - } - ++sensorCnt; - } - currentSensor = 0; - return status; -} - -/* - * Main ranging function - */ -int range_measure(VL53L1X_DevI2C *device_i2c) -{ - int status = 0; - - /* creates the 53L1A1 expansion board singleton obj */ - board = XNucleo53L1A1::instance(device_i2c, A2, D9, D2); - - /* init the 53L1A1 expansion board with default values */ - status = board->init_board(); - if (status != 0) { - printf("Failed to init board!\r\n"); - return status; - } - - /* init an array with chars to id the sensors */ - status = init_sensors_array(); - if (status != 0) { - printf("Failed to init sensors!\r\n"); - return status; - } - - printf("Entering loop mode\r\n"); - - /* Main ranging interrupt loop */ - while (true) { - measure_sensors(); - if (switchChanged) { - ++currentSensor; - if (currentSensor == sensorCnt) - currentSensor = 0; - printf("Sensor changed to %c\r\n", installedSensors[currentSensor]); - switchChanged = false; - stop_button.enable_irq(); - } - } - delete board; - return status; } /*=================================== Main ================================== =============================================================================*/ int main() { - stop_button.rise(&switch_measuring_sensor_irq); - stop_button.enable_irq(); + stop_button.rise(&measuring_stop_irq); - VL53L1X_DevI2C *dev_I2C = new VL53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL); - range_measure(dev_I2C); // start continuous measures + VL53L1X_DevI2C *device_i2c = new VL53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL); + range_measure(device_i2c); // start continuous measures return 0; } -