VL53L1CB expansion board example, showing multi-ranges in an autonomous setup & polling mode. Uses the onboard sensor. Targets MbedOS v6.10.0.
Dependencies: X_NUCLEO_53L1A2
Diff: main.cpp
- Revision:
- 4:0ac9998b69ac
- Parent:
- 3:c1e893e6752f
- Child:
- 5:b233bdf91671
--- a/main.cpp Tue May 11 14:10:25 2021 +0000 +++ b/main.cpp Tue May 11 15:33:09 2021 +0000 @@ -52,93 +52,15 @@ Serial pc(SERIAL_TX, SERIAL_RX); #endif -// flags to indicate an interrupt has happened -static int int_centre_result = 0; -static int int_left_result = 0; -static int int_right_result = 0; - -// flags to indicate an interrupt has cleared -static int int_centre_dropped = 0; -static int int_left_dropped = 0; -static int int_right_dropped = 0; - void print_results(int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData ); -class WaitForMeasurement { -public: - - -// this class services the interrupts from the ToF sensors. -// There is a limited amount you can do in an interrupt routine; printfs,mutexes break them among other things. -// We keep things simple by only raising a flag so all the real work is done outside the interrupt. -// This is designed around MBED V2 which doesn't have the RTOS features that would make this work nicely e.g. semaphores/queues. -WaitForMeasurement(): _interrupt(A1) -{ -} - - - // constructor - Sensor is not used and can be removed - WaitForMeasurement(PinName pin,VL53L1_DEV Dev) : _interrupt(pin) // create the InterruptIn on the pin specified to Counter - { - Devlocal = Dev; - pinlocal = pin; - -// #include "mbed.h" - - - _interrupt.rise(callback(this, &WaitForMeasurement::got_interrupt)); // if interrupt happens read data - _interrupt.fall(callback(this, &WaitForMeasurement::linedropped)); // if interupt clears, clear interrupt - - } - - // function is called every time an interupt is cleared. Sets flags to clear the interrupt - void linedropped() - { - - if (Devlocal->i2c_slave_address == NEW_SENSOR_CENTRE_ADDRESS) - int_centre_dropped = 1; //flag to main that interrupt cleared. A flag is raised which allows the main routine to service interupt. - if (Devlocal->i2c_slave_address == NEW_SENSOR_LEFT_ADDRESS) - int_left_dropped = 1; //flag to main that interrupt cleared - if (Devlocal->i2c_slave_address == NEW_SENSOR_RIGHT_ADDRESS) - int_right_dropped = 1; //flag to main that interrupt cleared - - } - - // function is called every time an interupt is seen. A flag is raised which allows the main routine to service the interupt. - void got_interrupt() - { - DigitalIn intp(pinlocal); - - if (Devlocal->i2c_slave_address == NEW_SENSOR_CENTRE_ADDRESS) - int_centre_result = 1; //flag to main that interrupt happened - if (Devlocal->i2c_slave_address == NEW_SENSOR_LEFT_ADDRESS) - int_left_result = 1; //flag to main that interrupt happened7 - if (Devlocal->i2c_slave_address == NEW_SENSOR_RIGHT_ADDRESS) - int_right_result = 1; //flag to main that interrupt happened - } - - - //destructor - ~WaitForMeasurement() - { - printf("WaitForMeasurement destruction \n"); - } - -private: - InterruptIn _interrupt; - PinName pinlocal; - VL53L1_DEV Devlocal; - int status; - -}; - - - VL53L1_Dev_t devCentre; VL53L1_Dev_t devLeft; VL53L1_Dev_t devRight; VL53L1_DEV Dev = &devCentre; +VL53L1 *Sensor; + /* flags that handle interrupt request for sensor and user blue button*/ @@ -156,11 +78,44 @@ int init_sensor() { int status = 0; - /* start the measure on the center sensor */ - if (NULL != board->sensor_centre) { + + Dev=&devCentre; + Sensor=board->sensor_centre; + + // configure the sensors + Dev->comms_speed_khz = 400; + Dev->comms_type = 1; + Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS; + + printf("configuring centre channel \n"); + + /* Device Initialization and setting */ + status = Sensor->vl53L1_DataInit(); + status = Sensor->vl53L1_StaticInit(); + status = Sensor->vl53L1_SetPresetMode(VL53L1_PRESETMODE_AUTONOMOUS); + status = Sensor->vl53L1_SetDistanceMode(VL53L1_DISTANCEMODE_LONG); + status = Sensor->vl53L1_SetMeasurementTimingBudgetMicroSeconds( 200 * 1000); + + + // set the ranging and signal rate filter + VL53L1_DetectionConfig_t thresholdconfig; + + thresholdconfig.DetectionMode = VL53L1_DETECTION_DISTANCE_ONLY; /// type VL53L1_DetectionMode in vl53l1_def.h + thresholdconfig.Distance.CrossMode = VL53L1_THRESHOLD_IN_WINDOW; // type VL53L1_ThresholdMode. ignore if distance outside high and low + thresholdconfig.Distance.High = 300; // high distance in mm + thresholdconfig.Distance.Low = 200; // low distance in mm + thresholdconfig.Rate.CrossMode=0; // type VL53L1_ThresholdMode VL53L1_THRESHOLD_CROSSED_LOW VL53L1_THRESHOLD_CROSSED_HIGH VL53L1_THRESHOLD_OUT_OF_WINDOW VL53L1_THRESHOLD_IN_WINDOW + thresholdconfig.Rate.High = 0; + thresholdconfig.Rate.Low = 0; + thresholdconfig.IntrNoTarget = 0 ;// if 1 produce an interrupt even if there is no target found e.g out of range + + status = Sensor->vl53L1_SetThresholdConfig(&thresholdconfig); + + // create interrupt handler and start measurements + if (board->sensor_centre!= NULL) { status = board->sensor_centre->stop_measurement(); if (status != 0) { - return status; + return status; } status = board->sensor_centre->start_measurement(&sensor_irq); @@ -181,16 +136,15 @@ =============================================================================*/ int main() { +#if (MBED_VERSION < 60000) + #if USER_BUTTON==PC_13 // we are cross compiling for Nucleo-f401 + InterruptIn stop_button(USER_BUTTON); + stop_button.rise(&measuring_stop_irq); + #endif +#endif int status; - VL53L1 * Sensor; - uint8_t ToFSensor = 1; // 0=Left, 1=Center(default), 2=Right - - //mbed compiler claims these are never used but they are. - WaitForMeasurement* int2; - WaitForMeasurement* int1; - WaitForMeasurement* int3; - - + uint16_t distance = 0; + pc.baud(115200); // baud rate is important as printf statements take a lot of time printf("Autonomous Interrupt, mbed = %d \r\n",MBED_VERSION); @@ -220,47 +174,6 @@ return status; } - - Dev=&devCentre; - Sensor=board->sensor_centre; - Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS; - printf("configuring centre channel \n"); - - // configure the sensors - Dev->comms_speed_khz = 400; - Dev->comms_type = 1; - - /* Device Initialization and setting */ - status = Sensor->vl53L1_DataInit(); - status = Sensor->vl53L1_StaticInit(); - status = Sensor->vl53L1_SetPresetMode(VL53L1_PRESETMODE_AUTONOMOUS); - status = Sensor->vl53L1_SetDistanceMode(VL53L1_DISTANCEMODE_LONG); - status = Sensor->vl53L1_SetMeasurementTimingBudgetMicroSeconds( 200 * 1000); - - - // set the ranging and signal rate filter - VL53L1_DetectionConfig_t thresholdconfig; - - thresholdconfig.DetectionMode = VL53L1_DETECTION_DISTANCE_ONLY; /// type VL53L1_DetectionMode in vl53l1_def.h - thresholdconfig.Distance.CrossMode = VL53L1_THRESHOLD_IN_WINDOW; // type VL53L1_ThresholdMode. ignore if distance outside high and low - thresholdconfig.Distance.High = 300; // high distance in mm - thresholdconfig.Distance.Low = 200; // low distance in mm - thresholdconfig.Rate.CrossMode=0; // type VL53L1_ThresholdMode VL53L1_THRESHOLD_CROSSED_LOW VL53L1_THRESHOLD_CROSSED_HIGH VL53L1_THRESHOLD_OUT_OF_WINDOW VL53L1_THRESHOLD_IN_WINDOW - thresholdconfig.Rate.High = 0; - thresholdconfig.Rate.Low = 0; - thresholdconfig.IntrNoTarget = 0 ;// if 1 produce an interrupt even if there is no target found e.g out of range - - status = Sensor->vl53L1_SetThresholdConfig(&thresholdconfig); - - // create interrupt handlers for the three sensors and start measurements - if (board->sensor_centre!= NULL ) - { - printf("starting interrupt centre\n"); - devCentre.i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS; - int1 = new WaitForMeasurement(CentreIntPin,&devCentre); // create interrupt handler - status = board->sensor_centre->vl53L1_StartMeasurement(); - } - printf("loop forever\n"); // loop waiting for interrupts to happen. This is signaled by int_centre_result,int_left_result or int_right_result @@ -297,11 +210,30 @@ } */ + VL53L1_MultiRangingData_t MultiRangingData; + VL53L1_MultiRangingData_t *pMultiRangingData = NULL; + while (true) { + pMultiRangingData = &MultiRangingData; + if (int_sensor) { int_sensor = false; - status = board->sensor_centre->handle_irq(&distance); +/* +// status = board->sensor_centre->handle_irq(&distance); +// status = get_measurement(distance); +// status = board->sensor_centre->VL53L1_GetDistance(&distance); + status = board->sensor_centre->VL53L1_ClearInterrupt(); + board->sensor_centre->enable_interrupt_measure_detection_irq(); printf("distance: %d\r\n", distance); +*/ + status = board->sensor_centre->vl53L1_GetMultiRangingData( pMultiRangingData); + if (status == 0) + { + print_results( devCentre.i2c_slave_address, pMultiRangingData ); + } + status = board->sensor_centre->VL53L1_ClearInterrupt(); +// status = board->sensor_centre->vl53L1_ClearInterruptAndStartMeasurement(); // doesnt run! + board->sensor_centre->enable_interrupt_measure_detection_irq(); } if (int_stop) { @@ -343,6 +275,6 @@ { thread_sleep_for(ms); } - #endif +#endif