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:
Sun Nov 08 14:05:14 2020 +0000
Revision:
2:ef5e40bad526
Parent:
1:49e2fad70dfe
Child:
3:d1a3d15a06ff
Mbed V6.4 changes

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 0:50b05f035d13 16 #include "XNucleo53L1A1.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 0:50b05f035d13 25
charlesmn 0:50b05f035d13 26
charlesmn 0:50b05f035d13 27 static XNucleo53L1A1 *board=NULL;
charlesmn 2:ef5e40bad526 28 #if (MBED_VERSION > 60100)
charlesmn 2:ef5e40bad526 29 UnbufferedSerial pc(SERIAL_TX, SERIAL_RX);
charlesmn 2:ef5e40bad526 30 extern "C" void wait_ms(int ms);
charlesmn 2:ef5e40bad526 31 #else
charlesmn 0:50b05f035d13 32 Serial pc(SERIAL_TX, SERIAL_RX);
charlesmn 2:ef5e40bad526 33 #endif
charlesmn 0:50b05f035d13 34
charlesmn 0:50b05f035d13 35 void process_interrupt( VL53L1X * sensor,VL53L1_DEV dev );
charlesmn 0:50b05f035d13 36 void print_results( int devSpiNumber, VL53L1_MultiRangingData_t *pMultiRangingData );
charlesmn 0:50b05f035d13 37
charlesmn 0:50b05f035d13 38 VL53L1_Dev_t devCentre;
charlesmn 0:50b05f035d13 39 VL53L1_Dev_t devLeft;
charlesmn 0:50b05f035d13 40 VL53L1_Dev_t devRight;
charlesmn 0:50b05f035d13 41 VL53L1_DEV Dev = &devCentre;
charlesmn 0:50b05f035d13 42
charlesmn 0:50b05f035d13 43
charlesmn 0:50b05f035d13 44 /*=================================== Main ==================================
charlesmn 0:50b05f035d13 45 =============================================================================*/
charlesmn 0:50b05f035d13 46 int main()
charlesmn 0:50b05f035d13 47 {
charlesmn 0:50b05f035d13 48 int status;
charlesmn 0:50b05f035d13 49 VL53L1X * Sensor;
charlesmn 0:50b05f035d13 50 uint16_t wordData;
charlesmn 0:50b05f035d13 51
charlesmn 0:50b05f035d13 52
charlesmn 0:50b05f035d13 53 pc.baud(115200); // baud rate is important as printf statements take a lot of time
charlesmn 2:ef5e40bad526 54 printf("Polling single multizone mbed = %d \r\n",MBED_VERSION);
charlesmn 0:50b05f035d13 55
charlesmn 0:50b05f035d13 56 // create i2c interface
charlesmn 0:50b05f035d13 57 ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL);
charlesmn 0:50b05f035d13 58
charlesmn 0:50b05f035d13 59 /* creates the 53L1A1 expansion board singleton obj */
charlesmn 0:50b05f035d13 60 board = XNucleo53L1A1::instance(dev_I2C, A2, D8, D2);
charlesmn 0:50b05f035d13 61
charlesmn 0:50b05f035d13 62 printf("board created!\r\n");
charlesmn 0:50b05f035d13 63
charlesmn 0:50b05f035d13 64 /* init the 53L1A1 expansion board with default values */
charlesmn 0:50b05f035d13 65 status = board->init_board();
charlesmn 0:50b05f035d13 66 if (status) {
charlesmn 0:50b05f035d13 67 printf("Failed to init board!\r\n");
charlesmn 0:50b05f035d13 68 return 0;
charlesmn 0:50b05f035d13 69 }
charlesmn 0:50b05f035d13 70
charlesmn 0:50b05f035d13 71
charlesmn 0:50b05f035d13 72 printf("board initiated! - %d\r\n", status);
charlesmn 0:50b05f035d13 73 // create the sensor controller classes
charlesmn 0:50b05f035d13 74
charlesmn 0:50b05f035d13 75
charlesmn 0:50b05f035d13 76
charlesmn 0:50b05f035d13 77 Dev=&devCentre;
charlesmn 0:50b05f035d13 78 Sensor=board->sensor_centre;
charlesmn 0:50b05f035d13 79 Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS;
charlesmn 0:50b05f035d13 80 printf("configuring centre channel \n");
charlesmn 0:50b05f035d13 81
charlesmn 0:50b05f035d13 82
charlesmn 0:50b05f035d13 83 // configure the sensors
charlesmn 0:50b05f035d13 84 Dev->comms_speed_khz = 400;
charlesmn 0:50b05f035d13 85
charlesmn 0:50b05f035d13 86 Dev->comms_type = 1;
charlesmn 0:50b05f035d13 87
charlesmn 0:50b05f035d13 88 Sensor->VL53L1_RdWord(Dev, 0x01, &wordData);
charlesmn 0:50b05f035d13 89 printf("VL53L1X: %02X %d\n\r", wordData,Dev->i2c_slave_address);
charlesmn 0:50b05f035d13 90 /* Device Initialization and setting */
charlesmn 0:50b05f035d13 91 status = Sensor->vl53L1_DataInit();
charlesmn 0:50b05f035d13 92 status = Sensor->vl53L1_StaticInit();
charlesmn 0:50b05f035d13 93 status = Sensor->vl53L1_SetPresetMode( VL53L1_PRESETMODE_MULTIZONES_SCANNING);
charlesmn 1:49e2fad70dfe 94 status = Sensor->vl53L1_SetDistanceMode( VL53L1_DISTANCEMODE_LONG);
charlesmn 1:49e2fad70dfe 95
charlesmn 0:50b05f035d13 96
charlesmn 0:50b05f035d13 97 VL53L1_RoiConfig_t roiConfig;
charlesmn 0:50b05f035d13 98 roiConfig.NumberOfRoi =3;
charlesmn 0:50b05f035d13 99 roiConfig.UserRois[0].TopLeftX = 0;
charlesmn 0:50b05f035d13 100 roiConfig.UserRois[0].TopLeftY = 9;
charlesmn 0:50b05f035d13 101 roiConfig.UserRois[0].BotRightX = 4;
charlesmn 0:50b05f035d13 102 roiConfig.UserRois[0].BotRightY = 5;
charlesmn 0:50b05f035d13 103 roiConfig.UserRois[1].TopLeftX = 5;
charlesmn 0:50b05f035d13 104 roiConfig.UserRois[1].TopLeftY = 9;
charlesmn 0:50b05f035d13 105 roiConfig.UserRois[1].BotRightX = 9;
charlesmn 0:50b05f035d13 106 roiConfig.UserRois[1].BotRightY = 4;
charlesmn 0:50b05f035d13 107 roiConfig.UserRois[2].TopLeftX = 11;
charlesmn 0:50b05f035d13 108 roiConfig.UserRois[2].TopLeftY = 9;
charlesmn 0:50b05f035d13 109 roiConfig.UserRois[2].BotRightX = 15;
charlesmn 0:50b05f035d13 110 roiConfig.UserRois[2].BotRightY = 5;
charlesmn 0:50b05f035d13 111 status = Sensor->vl53L1_SetROI( &roiConfig);
charlesmn 0:50b05f035d13 112 printf("VL53L1_SetROI %d \n",status);
charlesmn 0:50b05f035d13 113
charlesmn 0:50b05f035d13 114 devCentre.i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS;
charlesmn 0:50b05f035d13 115 status = board->sensor_centre->vl53L1_StartMeasurement();
charlesmn 0:50b05f035d13 116
charlesmn 0:50b05f035d13 117 // looping polling for results
charlesmn 0:50b05f035d13 118 while (1)
charlesmn 0:50b05f035d13 119 {
charlesmn 0:50b05f035d13 120 VL53L1_MultiRangingData_t MultiRangingData;
charlesmn 0:50b05f035d13 121 VL53L1_MultiRangingData_t *pMultiRangingData = &MultiRangingData;
charlesmn 0:50b05f035d13 122
charlesmn 0:50b05f035d13 123 // wait for result
charlesmn 0:50b05f035d13 124 status = board->sensor_centre->vl53L1_WaitMeasurementDataReady();
charlesmn 0:50b05f035d13 125 // get the result
charlesmn 0:50b05f035d13 126 status = board->sensor_centre->vl53L1_GetMultiRangingData( pMultiRangingData);
charlesmn 0:50b05f035d13 127 // if valid, print it
charlesmn 0:50b05f035d13 128 if(status==0) {
charlesmn 0:50b05f035d13 129 print_results(devCentre.i2c_slave_address, pMultiRangingData );
charlesmn 0:50b05f035d13 130 status = board->sensor_centre->vl53L1_ClearInterruptAndStartMeasurement();
charlesmn 0:50b05f035d13 131
charlesmn 0:50b05f035d13 132 } //if(status==0)
charlesmn 0:50b05f035d13 133 else
charlesmn 0:50b05f035d13 134 {
charlesmn 0:50b05f035d13 135 printf("VL53L1_GetMultiRangingData centre %d \n",status);
charlesmn 0:50b05f035d13 136 }
charlesmn 0:50b05f035d13 137
charlesmn 0:50b05f035d13 138 wait_ms( 1 * 10);
charlesmn 0:50b05f035d13 139 }
charlesmn 0:50b05f035d13 140 }
charlesmn 0:50b05f035d13 141
charlesmn 0:50b05f035d13 142
charlesmn 0:50b05f035d13 143
charlesmn 0:50b05f035d13 144 // prints the range result seen above
charlesmn 0:50b05f035d13 145 void print_results( int devSpiNumber, VL53L1_MultiRangingData_t *pMultiRangingData )
charlesmn 0:50b05f035d13 146 {
charlesmn 0:50b05f035d13 147 int no_of_object_found=pMultiRangingData->NumberOfObjectsFound;
charlesmn 0:50b05f035d13 148
charlesmn 0:50b05f035d13 149 int RoiNumber=pMultiRangingData->RoiNumber;
charlesmn 0:50b05f035d13 150 // int RoiStatus=pMultiRangingData->RoiStatus;
charlesmn 0:50b05f035d13 151
charlesmn 0:50b05f035d13 152 if (( no_of_object_found < 10 ) && ( no_of_object_found != 0))
charlesmn 0:50b05f035d13 153 {
charlesmn 0:50b05f035d13 154
charlesmn 0:50b05f035d13 155 // printf("MZI Count=%5d, ", pMultiRangingData->StreamCount);
charlesmn 0:50b05f035d13 156 // printf("RoiNumber%1d, ", RoiNumber);
charlesmn 0:50b05f035d13 157 // printf("RoiStatus=%1d, \n", RoiStatus);
charlesmn 0:50b05f035d13 158 for(int j=0;j<no_of_object_found;j++){
charlesmn 0:50b05f035d13 159 if ((pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) ||
charlesmn 0:50b05f035d13 160 (pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL))
charlesmn 0:50b05f035d13 161 {
charlesmn 0:50b05f035d13 162 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 0:50b05f035d13 163 devSpiNumber,
charlesmn 0:50b05f035d13 164 RoiNumber,
charlesmn 0:50b05f035d13 165 pMultiRangingData->RangeData[j].RangeStatus,
charlesmn 0:50b05f035d13 166 pMultiRangingData->RangeData[j].RangeMilliMeter,
charlesmn 0:50b05f035d13 167 pMultiRangingData->RangeData[j].SignalRateRtnMegaCps/65536.0,
charlesmn 0:50b05f035d13 168 pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps/65536.0);
charlesmn 0:50b05f035d13 169 }
charlesmn 0:50b05f035d13 170 else
charlesmn 0:50b05f035d13 171 {
charlesmn 0:50b05f035d13 172 // printf("RangeStatus %d %d\n",j, pMultiRangingData->RangeData[j].RangeStatus);
charlesmn 0:50b05f035d13 173 }
charlesmn 0:50b05f035d13 174 }
charlesmn 0:50b05f035d13 175 } // if (( no_of_object_found < 10 ) && ( no_of_object_found != 0))
charlesmn 0:50b05f035d13 176 else
charlesmn 0:50b05f035d13 177 {
charlesmn 0:50b05f035d13 178 // printf("no_of_object_found %d \n",no_of_object_found);
charlesmn 0:50b05f035d13 179 }
charlesmn 0:50b05f035d13 180
charlesmn 2:ef5e40bad526 181 }
charlesmn 2:ef5e40bad526 182
charlesmn 2:ef5e40bad526 183
charlesmn 2:ef5e40bad526 184 #if (MBED_VERSION > 60100)
charlesmn 2:ef5e40bad526 185 extern "C" void wait_ms(int ms)
charlesmn 2:ef5e40bad526 186 {
charlesmn 2:ef5e40bad526 187 thread_sleep_for(ms);
charlesmn 2:ef5e40bad526 188 }
charlesmn 2:ef5e40bad526 189 #endif
charlesmn 0:50b05f035d13 190
charlesmn 0:50b05f035d13 191