Sample MBed program for use with ST XNucleo VL53L1CB board. Uses interrupts to cope with up to 3 sensors in multizone mode. Includes MBed V6.4
Dependencies: X_NUCLEO_53L1A2
Diff: main.cpp
- Revision:
- 0:e91189a84ad9
- Child:
- 1:c67af60ec906
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Nov 08 15:49:23 2020 +0000 @@ -0,0 +1,388 @@ +/* + * This VL53L1X Expansion board test application performs range measurements + * using the onboard embedded centre sensor and two satelites, in multizone, interrupt mode. + * Measured ranges are ouput on the Serial Port, running at 115200 baud. + * + * + * This is designed to work with MBed V2 , MBed V5 and MBed V6. + * + * + * The Reset button can be used to restart the program. + */ + +#include <stdio.h> + +#include "mbed.h" +#include "XNucleo53L1A1.h" +#include "ToF_I2C.h" +#include <time.h> + + +// define the i2c comms pins +#define I2C_SDA D14 +#define I2C_SCL D15 + +#define NUM_SENSORS 3 + +PinName CentreIntPin = A2; +// the satellite pins depend on solder blobs on the back of the shield. +// they may not exist or may be one of two sets. +// the centre pin always exists +PinName LeftIntPin = D9; +PinName RightIntPin = D4; +// alternate set +//PinName LeftIntPin = D8; +//PinName RightIntPin = D2; + + +static XNucleo53L1A1 *board=NULL; + +#if (MBED_VERSION > 60300) +UnbufferedSerial pc(SERIAL_TX, SERIAL_RX); +extern "C" void wait_ms(int ms); +#else +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; + +// timing for debug purposes +uint32_t centre_polling_time_ms; +uint32_t centre_start_time_ms; +uint32_t centre_end_time_ms; + +void process_interrupt( VL53L1X * sensor,VL53L1_DEV dev ); +void print_results( int devSpiNumber, VL53L1_MultiRangingData_t *pMultiRangingData ); + + + +// 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. +class WaitForMeasurement { +public: + + +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" + + printf("WaitForMeasurement %d \n",Dev->i2c_slave_address); + _interrupt.rise(callback(this, &WaitForMeasurement::got_interrupt)); // if interrupt happens read data + _interrupt.fall(callback(this, &WaitForMeasurement::interruptdropped)); // if interupt clears, start next reading + + } + + // function is called every time an interupt is cleared. causes + void interruptdropped() + { + + 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); + _count++; + + 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; + volatile int _count; + VL53L1_DEV Devlocal; + int status; + +}; + + + +VL53L1_Dev_t devCentre; +VL53L1_Dev_t devLeft; +VL53L1_Dev_t devRight; +VL53L1_DEV Dev = &devCentre; + + +/*=================================== Main ================================== +=============================================================================*/ +int main() +{ + int status; + VL53L1X * Sensor; + uint16_t wordData; + uint8_t ToFSensor = 1; // 0=Left, 1=Center(default), 2=Right + + + WaitForMeasurement* int2; + WaitForMeasurement* int1; + WaitForMeasurement* int3; + + pc.baud(115200); // baud rate is important as printf statements take a lot of time + + printf("main_interrupt_multizone \r\n"); + +// create i2c interface + ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL); + + dev_I2C->frequency(400000); //also needs doing in spi_interface.c + + /* creates the 53L1A1 expansion board singleton obj */ + board = XNucleo53L1A1::instance(dev_I2C, CentreIntPin, RightIntPin, D2); + + printf("board created!\r\n"); + + /* init the 53L1A1 expansion board with default values */ + status = board->init_board(); + if (status) { + printf("Failed to init board!\r\n"); + return 0; + } + + + printf("board initiated! - %d\r\n", status); + // create the sensor controller classes for each sensor and initialise the sensors + for (ToFSensor=0;ToFSensor< NUM_SENSORS;ToFSensor++){ + switch(ToFSensor){ + case 0: + if (board->sensor_centre== NULL ) continue; // don't create if sensor not detected + Dev=&devCentre; + Sensor=board->sensor_centre; + Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS; + printf("configuring centre channel \n"); + break; + case 1: + if (board->sensor_left== NULL ) continue; // don't create if sensor not detected + Dev=&devLeft; + Sensor=board->sensor_left; + Dev->i2c_slave_address = NEW_SENSOR_LEFT_ADDRESS; + printf("configuring left channel \n"); + break; + case 2: + if (board->sensor_right== NULL ) continue; // don't create if sensor not detected + Dev=&devRight; + Sensor=board->sensor_right; + Dev->i2c_slave_address = NEW_SENSOR_RIGHT_ADDRESS; + printf("configuring right channel \n"); + break; + default: + printf(" error in switch, invalid ToF sensor \n"); + } //end of switch + +// 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_MULTIZONES_SCANNING); + + + + //configure the regions of interest for each sensor + VL53L1_RoiConfig_t roiConfig; + roiConfig.NumberOfRoi =3; + roiConfig.UserRois[0].TopLeftX = 0; + roiConfig.UserRois[0].TopLeftY = 9; + roiConfig.UserRois[0].BotRightX = 4; + roiConfig.UserRois[0].BotRightY = 5; + roiConfig.UserRois[1].TopLeftX = 5; + roiConfig.UserRois[1].TopLeftY = 9; + roiConfig.UserRois[1].BotRightX = 9; + roiConfig.UserRois[1].BotRightY = 4; + roiConfig.UserRois[2].TopLeftX = 11; + roiConfig.UserRois[2].TopLeftY = 9; + roiConfig.UserRois[2].BotRightX = 15; + roiConfig.UserRois[2].BotRightY = 5; + status = Sensor->vl53L1_SetROI( &roiConfig); + + + status = Sensor->vl53L1_SetDistanceMode( VL53L1_DISTANCEMODE_LONG); + // status = Sensor->VL53L1_SetMeasurementTimingBudgetMicroSeconds( 100 * 500); // error -21 is because of this. Don't know why + } + + // create interrupt handlers for the sensors that exist + if (board->sensor_centre!= NULL ) + { + printf("starting interrupt centre\n"); + devCentre.i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS; + int1 = new WaitForMeasurement(CentreIntPin,&devCentre); // create a interrupt class for this interrupt pin + status = board->sensor_centre->vl53L1_StartMeasurement(); //start the sensor measuring + } + + if (board->sensor_left!= NULL ) + { + printf("starting interrupt left\n"); + devLeft.i2c_slave_address = NEW_SENSOR_LEFT_ADDRESS; + int2 = new WaitForMeasurement(LeftIntPin,&devLeft); // create a interrupt class for this interrupt pin + status = board->sensor_left->vl53L1_StartMeasurement(); //start the sensor measuring + } + + if (board->sensor_right!= NULL ) + { + printf("starting interrupt right\n"); + devRight.i2c_slave_address = NEW_SENSOR_RIGHT_ADDRESS; + int3 = new WaitForMeasurement(RightIntPin,&devRight); // create a interrupt class for this interrupt pin + status = board->sensor_right->vl53L1_StartMeasurement();//start the sensor measuring + } + + + + + // loop waiting for interrupts to happen. This is signaled by int_centre_result,int_left_result or int_right_result + // being non zero. When the interrupts clear this is signaled by int_centre_dropped,int_left_dropped and int_right_dropped. + // These are set back to zero when processing is completed + while (1) + { + VL53L1_MultiRangingData_t MultiRangingData; + VL53L1_MultiRangingData_t *pMultiRangingData = &MultiRangingData; + + if ( int_left_dropped || int_centre_dropped || int_right_result) + wait_ms(20); + + + // when the interrupt pin goes low start new measurement + if ( int_centre_dropped != 0) + { + int_centre_dropped = 0; + status = board->sensor_centre->vl53L1_ClearInterruptAndStartMeasurement();// get next measurement + } + + if ( int_left_dropped != 0) + { + int_left_dropped = 0; + status = board->sensor_left->vl53L1_ClearInterruptAndStartMeasurement(); + } + + if ( int_right_dropped != 0) + { + int_right_dropped = 0; + status = board->sensor_right->vl53L1_ClearInterruptAndStartMeasurement(); + } + + if (int_right_result != 0) // interrupt seen on right sensor + { + status = board->sensor_right->vl53L1_GetMultiRangingData( pMultiRangingData); + if ( status == 0) + { + print_results( devRight.i2c_slave_address, pMultiRangingData ); + } + + // clear interrupt flag + int_right_result = 0; + } + + + if (int_left_result != 0) // interrupt seen on left sensor + { + status = board->sensor_left->vl53L1_GetMultiRangingData(pMultiRangingData); + if ( status == 0) + { + print_results( devLeft.i2c_slave_address, pMultiRangingData ); + } + + // clear interrupt flag + int_left_result = 0; + } + + if (int_centre_result != 0) + { + status = board->sensor_centre->vl53L1_GetMultiRangingData( pMultiRangingData); + if(status==0) { + print_results(devCentre.i2c_slave_address, pMultiRangingData ); + } //if(status==0) + + // clear interrupt flag + int_centre_result = 0; + + } + + wait_ms( 1 * 5); + + } +} + + + + // print out what data is required +void print_results( int devSpiNumber, VL53L1_MultiRangingData_t *pMultiRangingData ) +{ + int no_of_object_found=pMultiRangingData->NumberOfObjectsFound; + + int RoiNumber=pMultiRangingData->RoiNumber; + int RoiStatus=pMultiRangingData->RoiStatus; + + if (( no_of_object_found < 10 ) && ( no_of_object_found != 0)) + { + for(int j=0;j<no_of_object_found;j++){ + if ((pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) || + (pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL)) + { + printf("\t spiAddr=%d \t RoiNumber=%d \t D=%5dmm \n", + devSpiNumber, + RoiNumber, + pMultiRangingData->RangeData[j].RangeMilliMeter); + } + else + { +// printf("RangeStatus %d %d\n",j, pMultiRangingData->RangeData[j].RangeStatus); + } + } + } // if (( no_of_object_found < 10 ) && ( no_of_object_found != 0)) + else + { +// printf("no_of_object_found %d \n",no_of_object_found); + } + +} + + + +#if (MBED_VERSION > 60300) +void wait_ms(int ms) + { + thread_sleep_for(ms); + } + #endif +