A sample program getting measurements from a VL53L1 ToF sensor which is directly connected to the STM32F401. Only copes with one sensor. Polls for measurements. VL53L1 is operated in multizone mode. MBed V6.3 but will work with other MBed versions.
Dependencies: X_NUCLEO_53L1A2
Diff: main.cpp
- Revision:
- 1:6bbe4976bc7b
- Parent:
- 0:47d5c900dab9
- Child:
- 2:014f6e21d7b3
--- a/main.cpp Tue Oct 27 09:01:08 2020 +0000 +++ b/main.cpp Sun Nov 08 18:09:34 2020 +0000 @@ -13,23 +13,40 @@ #include <stdio.h> #include "mbed.h" -#include "NoShield53L1A1.h" +//#include "NoShield53L1A1.h" #include "ToF_I2C.h" #include <time.h> +//#include "vl53lx_class.h" +#include "vl53l1_def.h" +#include "vl53l1_platform_user_data.h" +#include "vl53l1x.h" // define the i2c comms pins #define I2C_SDA D14 #define I2C_SCL D15 +#define SENSOR_DEFAULT_ADDRESS 0x52 +#define NEW_SENSOR_CENTRE_ADDRESS 0x56 +#define NEW_SENSOR_LEFT_ADDRESS 0x58 +#define NEW_SENSOR_RIGHT_ADDRESS 0x5a + -static NoShield53L1 *board=NULL; -Serial pc(SERIAL_TX, SERIAL_RX); +//static NoShield53L1 *board=NULL; -void process_interrupt( VL53L1X * sensor,VL53L1_DEV dev ); +// MBed V6.4 has renamed wait_ms and UnbufferedSerial replaces Serial +#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 + +//void process_interrupt( VL53L1X * sensor,VL53L1_DEV dev ); void print_results( int devSpiNumber, VL53L1_MultiRangingData_t *pMultiRangingData ); + VL53L1_Dev_t devCentre; VL53L1_Dev_t devLeft; VL53L1_Dev_t devRight; @@ -43,54 +60,45 @@ int status; VL53L1X * Sensor; uint16_t wordData; + DigitalOut xshutdown(D9); pc.baud(115200); // baud rate is important as printf statements take a lot of time - printf("Polling single multizone\r\n"); + printf("Polling single multizone mbed = %d \r\n",MBED_VERSION); // create i2c interface ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL); /* creates the 53L1A1 expansion board singleton obj */ - board = NoShield53L1::instance(dev_I2C, A2, D8, D2); +// board = XNucleo51L1::instance(dev_I2C, A2, D8, D2); + + Sensor = new VL53L1X(dev_I2C,&xshutdown,A2); printf("board created!\r\n"); /* define the shutdown pins */ - status = board->init_board(D9,D4,D3); + // status = board->init_board(); + status = Sensor->InitSensor(SENSOR_DEFAULT_ADDRESS); if (status) { - printf("Failed to init board!\r\n"); + printf("Failed to init sensor!\r\n"); return 0; } - + wait_ms(250); // wait for the sensor to initialise - printf("board initiated! - %d\r\n", status); + printf("sensor initiated! - %d\r\n", status); // create the sensor controller classes - - - + 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; - Sensor->VL53L1_RdWord(Dev, 0x01, &wordData); printf("VL53L1X: %02X %d\n\r", wordData,Dev->i2c_slave_address); /* Device Initialization and setting */ status = Sensor->vl53L1_DataInit(); status = Sensor->vl53L1_StaticInit(); status = Sensor->vl53L1_SetPresetMode( VL53L1_PRESETMODE_MULTIZONES_SCANNING); - status = Sensor->vl53L1_SetDistanceMode( VL53L1_DISTANCEMODE_LONG); -// status = Sensor->vl53L1_SetMeasurementTimingBudgetMicroSeconds( 100 * 1000); -// status = Sensor->vl53L1_SetInterMeasurementPeriodMilliSeconds( 100); + status = Sensor->vl53L1_SetDistanceMode( VL53L1_DISTANCEMODE_LONG); + status = Sensor->vl53L1_SetMeasurementTimingBudgetMicroSeconds( 50 * 1000); - + // configure the region of interests VL53L1_RoiConfig_t roiConfig; roiConfig.NumberOfRoi =3; roiConfig.UserRois[0].TopLeftX = 0; @@ -108,8 +116,7 @@ status = Sensor->vl53L1_SetROI( &roiConfig); printf("VL53L1_SetROI %d \n",status); - devCentre.i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS; - status = board->sensor_centre->vl53L1_StartMeasurement(); + status = Sensor->vl53L1_StartMeasurement(); // looping polling for results while (1) @@ -118,13 +125,13 @@ VL53L1_MultiRangingData_t *pMultiRangingData = &MultiRangingData; // wait for result - status = board->sensor_centre->vl53L1_WaitMeasurementDataReady(); + status = Sensor->vl53L1_WaitMeasurementDataReady(); // get the result - status = board->sensor_centre->vl53L1_GetMultiRangingData( pMultiRangingData); + status = Sensor->vl53L1_GetMultiRangingData( pMultiRangingData); // if valid, print it if(status==0) { print_results(devCentre.i2c_slave_address, pMultiRangingData ); - status = board->sensor_centre->vl53L1_ClearInterruptAndStartMeasurement(); + status = Sensor->vl53L1_ClearInterruptAndStartMeasurement(); } //if(status==0) else @@ -141,40 +148,43 @@ // prints the range result seen above void print_results( int devSpiNumber, VL53L1_MultiRangingData_t *pMultiRangingData ) { - int no_of_object_found=pMultiRangingData->NumberOfObjectsFound; + int no_of_object_found=pMultiRangingData->NumberOfObjectsFound; - int RoiNumber=pMultiRangingData->RoiNumber; + int RoiNumber=pMultiRangingData->RoiNumber; // int RoiStatus=pMultiRangingData->RoiStatus; - if (( no_of_object_found < 10 ) && ( no_of_object_found != 0)) - { - - // printf("MZI Count=%5d, ", pMultiRangingData->StreamCount); - // printf("RoiNumber%1d, ", RoiNumber); - // printf("RoiStatus=%1d, \n", RoiStatus); - 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 status=%d, \t D=%5dmm, \t Signal=%2.2f Mcps, \t Ambient=%2.2f Mcps \n", - devSpiNumber, - RoiNumber, - pMultiRangingData->RangeData[j].RangeStatus, - pMultiRangingData->RangeData[j].RangeMilliMeter, - pMultiRangingData->RangeData[j].SignalRateRtnMegaCps/65536.0, - pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps/65536.0); - } - else - { + if (( no_of_object_found < 10 ) && ( no_of_object_found != 0)) + { + +// printf("MZI Count=%5d, ", pMultiRangingData->StreamCount); +// printf("RoiNumber%1d, ", RoiNumber); +// printf("RoiStatus=%1d, \n", RoiStatus); + 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 status=%d, \t D=%5dmm, \t Signal=%2.2f Mcps, \t Ambient=%2.2f Mcps \n", + devSpiNumber, + RoiNumber, + pMultiRangingData->RangeData[j].RangeStatus, + pMultiRangingData->RangeData[j].RangeMilliMeter, + pMultiRangingData->RangeData[j].SignalRateRtnMegaCps/65536.0, + pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps/65536.0); + } + 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 (( no_of_object_found < 10 ) && ( no_of_object_found != 0)) + } - \ No newline at end of file + +#if (MBED_VERSION > 60300) +void wait_ms(int ms) + { + thread_sleep_for(ms); + } +#endif \ No newline at end of file