A sample program to control one VL53L1 ToF sensor in multizone mode using polling to find out if a measurement is available. Mbed V6.3 but will run any MBed version by dropping replacing this one. Maint6 release.

Dependencies:   X_NUCLEO_53L1A2

Committer:
charlesmn
Date:
Wed Jun 16 14:24:47 2021 +0000
Revision:
18:2d1f16e92a8e
Parent:
16:9fd42eb225c5
Tidy up. Remove commented code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
charlesmn 0:50b05f035d13 1 /*
charlesmn 0:50b05f035d13 2 * This VL53L1X Expansion board test application performs range measurements
charlesmn 0:50b05f035d13 3 * using the onboard embedded centre sensor, in multizone, polling mode.
charlesmn 0:50b05f035d13 4 * Measured ranges are ouput on the Serial Port, running at 115200 baud.
charlesmn 0:50b05f035d13 5 *
charlesmn 0:50b05f035d13 6 *
charlesmn 0:50b05f035d13 7 * This is designed to work with MBed V2 ,MBed V5 and MBed V6.
charlesmn 0:50b05f035d13 8 *
charlesmn 0:50b05f035d13 9 *
charlesmn 0:50b05f035d13 10 * The Reset button can be used to restart the program.
charlesmn 0:50b05f035d13 11 */
charlesmn 0:50b05f035d13 12
charlesmn 0:50b05f035d13 13 #include <stdio.h>
charlesmn 0:50b05f035d13 14
charlesmn 0:50b05f035d13 15 #include "mbed.h"
charlesmn 15:ce041cbc8727 16 #include "XNucleo53L1A2.h"
charlesmn 0:50b05f035d13 17 #include "ToF_I2C.h"
charlesmn 0:50b05f035d13 18 #include <time.h>
charlesmn 0:50b05f035d13 19
charlesmn 0:50b05f035d13 20
charlesmn 0:50b05f035d13 21 // define the i2c comms pins
charlesmn 0:50b05f035d13 22 #define I2C_SDA D14
charlesmn 0:50b05f035d13 23 #define I2C_SCL D15
charlesmn 0:50b05f035d13 24
charlesmn 15:ce041cbc8727 25 // define interrupt pins
charlesmn 15:ce041cbc8727 26 PinName CentreIntPin = A2;
charlesmn 15:ce041cbc8727 27 // the satellite pins depend on solder blobs on the back of the shield.
charlesmn 15:ce041cbc8727 28 // they may not exist or may be one of two sets.
charlesmn 15:ce041cbc8727 29 // the centre pin always exists
charlesmn 15:ce041cbc8727 30 PinName LeftIntPin = D9;
charlesmn 15:ce041cbc8727 31 PinName RightIntPin = D4;
charlesmn 15:ce041cbc8727 32 // alternate set
charlesmn 15:ce041cbc8727 33 //PinName LeftIntPin = D8;
charlesmn 15:ce041cbc8727 34 //PinName RightIntPin = D2;
charlesmn 0:50b05f035d13 35
charlesmn 0:50b05f035d13 36
charlesmn 15:ce041cbc8727 37
charlesmn 15:ce041cbc8727 38
charlesmn 15:ce041cbc8727 39 static XNucleo53L1A2 *board=NULL;
charlesmn 18:2d1f16e92a8e 40
charlesmn 18:2d1f16e92a8e 41
charlesmn 3:d1a3d15a06ff 42 #if (MBED_VERSION > 60300)
charlesmn 16:9fd42eb225c5 43 UnbufferedSerial pc(USBTX, USBRX);
charlesmn 2:ef5e40bad526 44 extern "C" void wait_ms(int ms);
charlesmn 2:ef5e40bad526 45 #else
charlesmn 0:50b05f035d13 46 Serial pc(SERIAL_TX, SERIAL_RX);
charlesmn 2:ef5e40bad526 47 #endif
charlesmn 0:50b05f035d13 48
charlesmn 16:9fd42eb225c5 49 void process_interrupt( VL53L1CB * sensor,VL53L1_DEV dev );
charlesmn 0:50b05f035d13 50 void print_results( int devSpiNumber, VL53L1_MultiRangingData_t *pMultiRangingData );
charlesmn 0:50b05f035d13 51
charlesmn 0:50b05f035d13 52 VL53L1_Dev_t devCentre;
charlesmn 0:50b05f035d13 53 VL53L1_Dev_t devLeft;
charlesmn 0:50b05f035d13 54 VL53L1_Dev_t devRight;
charlesmn 0:50b05f035d13 55 VL53L1_DEV Dev = &devCentre;
charlesmn 0:50b05f035d13 56
charlesmn 0:50b05f035d13 57
charlesmn 0:50b05f035d13 58 /*=================================== Main ==================================
charlesmn 0:50b05f035d13 59 =============================================================================*/
charlesmn 0:50b05f035d13 60 int main()
charlesmn 0:50b05f035d13 61 {
charlesmn 0:50b05f035d13 62 int status;
charlesmn 16:9fd42eb225c5 63 VL53L1CB * Sensor;
charlesmn 0:50b05f035d13 64 uint16_t wordData;
charlesmn 0:50b05f035d13 65
charlesmn 0:50b05f035d13 66 pc.baud(115200); // baud rate is important as printf statements take a lot of time
charlesmn 2:ef5e40bad526 67 printf("Polling single multizone mbed = %d \r\n",MBED_VERSION);
charlesmn 0:50b05f035d13 68
charlesmn 0:50b05f035d13 69 // create i2c interface
charlesmn 0:50b05f035d13 70 ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL);
charlesmn 0:50b05f035d13 71
charlesmn 15:ce041cbc8727 72 /* creates the 53L1A2 expansion board singleton obj */
charlesmn 15:ce041cbc8727 73 board = XNucleo53L1A2::instance(dev_I2C, CentreIntPin, LeftIntPin, RightIntPin);
charlesmn 0:50b05f035d13 74
charlesmn 0:50b05f035d13 75 printf("board created!\r\n");
charlesmn 0:50b05f035d13 76
charlesmn 0:50b05f035d13 77 /* init the 53L1A1 expansion board with default values */
charlesmn 0:50b05f035d13 78 status = board->init_board();
charlesmn 0:50b05f035d13 79 if (status) {
charlesmn 0:50b05f035d13 80 printf("Failed to init board!\r\n");
charlesmn 0:50b05f035d13 81 return 0;
charlesmn 0:50b05f035d13 82 }
charlesmn 0:50b05f035d13 83
charlesmn 0:50b05f035d13 84 printf("board initiated! - %d\r\n", status);
charlesmn 18:2d1f16e92a8e 85
charlesmn 0:50b05f035d13 86 // create the sensor controller classes
charlesmn 0:50b05f035d13 87 Dev=&devCentre;
charlesmn 0:50b05f035d13 88 Sensor=board->sensor_centre;
charlesmn 0:50b05f035d13 89 Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS;
charlesmn 0:50b05f035d13 90 printf("configuring centre channel \n");
charlesmn 0:50b05f035d13 91
charlesmn 0:50b05f035d13 92
charlesmn 0:50b05f035d13 93 // configure the sensors
charlesmn 0:50b05f035d13 94 Dev->comms_speed_khz = 400;
charlesmn 0:50b05f035d13 95
charlesmn 0:50b05f035d13 96 Dev->comms_type = 1;
charlesmn 0:50b05f035d13 97
charlesmn 16:9fd42eb225c5 98 Sensor->VL53L1CB_RdWord(Dev, 0x01, &wordData);
charlesmn 0:50b05f035d13 99 printf("VL53L1X: %02X %d\n\r", wordData,Dev->i2c_slave_address);
charlesmn 0:50b05f035d13 100 /* Device Initialization and setting */
charlesmn 16:9fd42eb225c5 101 status = Sensor->VL53L1CB_DataInit();
charlesmn 16:9fd42eb225c5 102 status = Sensor->VL53L1CB_StaticInit();
charlesmn 16:9fd42eb225c5 103 status = Sensor->VL53L1CB_SetPresetMode( VL53L1_PRESETMODE_MULTIZONES_SCANNING);
charlesmn 16:9fd42eb225c5 104 status = Sensor->VL53L1CB_SetDistanceMode( VL53L1_DISTANCEMODE_LONG);
charlesmn 1:49e2fad70dfe 105
charlesmn 0:50b05f035d13 106
charlesmn 0:50b05f035d13 107 VL53L1_RoiConfig_t roiConfig;
charlesmn 0:50b05f035d13 108 roiConfig.NumberOfRoi =3;
charlesmn 0:50b05f035d13 109 roiConfig.UserRois[0].TopLeftX = 0;
charlesmn 0:50b05f035d13 110 roiConfig.UserRois[0].TopLeftY = 9;
charlesmn 0:50b05f035d13 111 roiConfig.UserRois[0].BotRightX = 4;
charlesmn 0:50b05f035d13 112 roiConfig.UserRois[0].BotRightY = 5;
charlesmn 0:50b05f035d13 113 roiConfig.UserRois[1].TopLeftX = 5;
charlesmn 0:50b05f035d13 114 roiConfig.UserRois[1].TopLeftY = 9;
charlesmn 0:50b05f035d13 115 roiConfig.UserRois[1].BotRightX = 9;
charlesmn 0:50b05f035d13 116 roiConfig.UserRois[1].BotRightY = 4;
charlesmn 0:50b05f035d13 117 roiConfig.UserRois[2].TopLeftX = 11;
charlesmn 0:50b05f035d13 118 roiConfig.UserRois[2].TopLeftY = 9;
charlesmn 0:50b05f035d13 119 roiConfig.UserRois[2].BotRightX = 15;
charlesmn 0:50b05f035d13 120 roiConfig.UserRois[2].BotRightY = 5;
charlesmn 16:9fd42eb225c5 121 status = Sensor->VL53L1CB_SetROI( &roiConfig);
charlesmn 0:50b05f035d13 122 printf("VL53L1_SetROI %d \n",status);
charlesmn 0:50b05f035d13 123
charlesmn 0:50b05f035d13 124 devCentre.i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS;
charlesmn 16:9fd42eb225c5 125 status = board->sensor_centre->VL53L1CB_StartMeasurement();
charlesmn 0:50b05f035d13 126
charlesmn 0:50b05f035d13 127 // looping polling for results
charlesmn 0:50b05f035d13 128 while (1)
charlesmn 0:50b05f035d13 129 {
charlesmn 0:50b05f035d13 130 VL53L1_MultiRangingData_t MultiRangingData;
charlesmn 0:50b05f035d13 131 VL53L1_MultiRangingData_t *pMultiRangingData = &MultiRangingData;
charlesmn 0:50b05f035d13 132
charlesmn 0:50b05f035d13 133 // wait for result
charlesmn 16:9fd42eb225c5 134 status = board->sensor_centre->VL53L1CB_WaitMeasurementDataReady();
charlesmn 0:50b05f035d13 135 // get the result
charlesmn 16:9fd42eb225c5 136 status = board->sensor_centre->VL53L1CB_GetMultiRangingData( pMultiRangingData);
charlesmn 0:50b05f035d13 137 // if valid, print it
charlesmn 0:50b05f035d13 138 if(status==0) {
charlesmn 0:50b05f035d13 139 print_results(devCentre.i2c_slave_address, pMultiRangingData );
charlesmn 16:9fd42eb225c5 140 status = board->sensor_centre->VL53L1CB_ClearInterruptAndStartMeasurement();
charlesmn 0:50b05f035d13 141
charlesmn 0:50b05f035d13 142 } //if(status==0)
charlesmn 0:50b05f035d13 143 else
charlesmn 0:50b05f035d13 144 {
charlesmn 0:50b05f035d13 145 printf("VL53L1_GetMultiRangingData centre %d \n",status);
charlesmn 0:50b05f035d13 146 }
charlesmn 0:50b05f035d13 147
charlesmn 0:50b05f035d13 148 wait_ms( 1 * 10);
charlesmn 0:50b05f035d13 149 }
charlesmn 0:50b05f035d13 150 }
charlesmn 0:50b05f035d13 151
charlesmn 0:50b05f035d13 152
charlesmn 0:50b05f035d13 153
charlesmn 0:50b05f035d13 154 // prints the range result seen above
charlesmn 0:50b05f035d13 155 void print_results( int devSpiNumber, VL53L1_MultiRangingData_t *pMultiRangingData )
charlesmn 0:50b05f035d13 156 {
charlesmn 18:2d1f16e92a8e 157 int no_of_object_found=pMultiRangingData->NumberOfObjectsFound;
charlesmn 18:2d1f16e92a8e 158
charlesmn 18:2d1f16e92a8e 159 int RoiNumber=pMultiRangingData->RoiNumber;
charlesmn 0:50b05f035d13 160
charlesmn 18:2d1f16e92a8e 161 if (( no_of_object_found < 10 ) && ( no_of_object_found != 0))
charlesmn 18:2d1f16e92a8e 162 {
charlesmn 18:2d1f16e92a8e 163 for(int j=0;j<no_of_object_found;j++){
charlesmn 18:2d1f16e92a8e 164 if ((pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) ||
charlesmn 18:2d1f16e92a8e 165 (pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL))
charlesmn 0:50b05f035d13 166 {
charlesmn 18:2d1f16e92a8e 167 printf("*****************\t spiAddr=%d \t RoiNumber=%d \t status=%d, \t D=%5dmm, \t Signal=%2.2f Mcps, \t Ambient=%2.2f Mcps \n",
charlesmn 18:2d1f16e92a8e 168 devSpiNumber,
charlesmn 18:2d1f16e92a8e 169 RoiNumber,
charlesmn 18:2d1f16e92a8e 170 pMultiRangingData->RangeData[j].RangeStatus,
charlesmn 18:2d1f16e92a8e 171 pMultiRangingData->RangeData[j].RangeMilliMeter,
charlesmn 18:2d1f16e92a8e 172 pMultiRangingData->RangeData[j].SignalRateRtnMegaCps/65536.0,
charlesmn 18:2d1f16e92a8e 173 pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps/65536.0);
charlesmn 18:2d1f16e92a8e 174 }
charlesmn 0:50b05f035d13 175 else
charlesmn 0:50b05f035d13 176 {
charlesmn 18:2d1f16e92a8e 177
charlesmn 18:2d1f16e92a8e 178 }
charlesmn 18:2d1f16e92a8e 179 }
charlesmn 18:2d1f16e92a8e 180 } // if (( no_of_object_found < 10 ) && ( no_of_object_found != 0))
charlesmn 18:2d1f16e92a8e 181 else
charlesmn 18:2d1f16e92a8e 182 {
charlesmn 0:50b05f035d13 183 // printf("no_of_object_found %d \n",no_of_object_found);
charlesmn 18:2d1f16e92a8e 184 }
charlesmn 0:50b05f035d13 185
charlesmn 2:ef5e40bad526 186 }
charlesmn 2:ef5e40bad526 187
charlesmn 2:ef5e40bad526 188
charlesmn 3:d1a3d15a06ff 189 #if (MBED_VERSION > 60300)
charlesmn 2:ef5e40bad526 190 extern "C" void wait_ms(int ms)
charlesmn 2:ef5e40bad526 191 {
charlesmn 2:ef5e40bad526 192 thread_sleep_for(ms);
charlesmn 2:ef5e40bad526 193 }
charlesmn 2:ef5e40bad526 194 #endif
charlesmn 0:50b05f035d13 195
charlesmn 0:50b05f035d13 196