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:
johnAlexander
Date:
Fri May 07 13:45:18 2021 +0000
Revision:
9:0a3e1affe004
Parent:
8:8f6490937346
Child:
10:17d61466185f
Add note on floating-point printf support.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
charlesmn 0:50b05f035d13 1 /*
johnAlexander 8:8f6490937346 2 * This VL53L1CB 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 *
johnAlexander 7:b5a4ad8ba844 7 * This is designed to work with MBed v2, & MBedOS v5 / v6.
charlesmn 0:50b05f035d13 8 *
charlesmn 0:50b05f035d13 9 *
charlesmn 0:50b05f035d13 10 * The Reset button can be used to restart the program.
johnAlexander 9:0a3e1affe004 11 *
johnAlexander 9:0a3e1affe004 12 * *** Note :
johnAlexander 9:0a3e1affe004 13 * Default Mbed build system settings disable print floating-point support.
johnAlexander 9:0a3e1affe004 14 * Offline builds can enable this, again.
johnAlexander 9:0a3e1affe004 15 * https://github.com/ARMmbed/mbed-os/blob/master/platform/source/minimal-printf/README.md
johnAlexander 9:0a3e1affe004 16 * .\mbed-os\platform\mbed_lib.json
johnAlexander 9:0a3e1affe004 17 *
charlesmn 0:50b05f035d13 18 */
charlesmn 0:50b05f035d13 19
charlesmn 0:50b05f035d13 20 #include <stdio.h>
johnAlexander 6:bc6ac1e294f4 21 #include <time.h>
charlesmn 0:50b05f035d13 22
charlesmn 0:50b05f035d13 23 #include "mbed.h"
johnAlexander 4:177d711cc20f 24 #include "XNucleo53L1A2.h"
charlesmn 0:50b05f035d13 25 #include "ToF_I2C.h"
charlesmn 0:50b05f035d13 26
charlesmn 0:50b05f035d13 27
charlesmn 0:50b05f035d13 28 // define the i2c comms pins
charlesmn 0:50b05f035d13 29 #define I2C_SDA D14
charlesmn 0:50b05f035d13 30 #define I2C_SCL D15
charlesmn 0:50b05f035d13 31
charlesmn 0:50b05f035d13 32
charlesmn 0:50b05f035d13 33
johnAlexander 4:177d711cc20f 34 static XNucleo53L1A2 *board=NULL;
charlesmn 3:d1a3d15a06ff 35 #if (MBED_VERSION > 60300)
johnAlexander 7:b5a4ad8ba844 36 UnbufferedSerial pc(USBTX, USBRX);
johnAlexander 6:bc6ac1e294f4 37 extern "C" void wait_ms(int ms);
charlesmn 2:ef5e40bad526 38 #else
johnAlexander 6:bc6ac1e294f4 39 Serial pc(SERIAL_TX, SERIAL_RX);
charlesmn 2:ef5e40bad526 40 #endif
charlesmn 0:50b05f035d13 41
charlesmn 5:5916ff386a94 42 void process_interrupt( VL53L1 * sensor,VL53L1_DEV dev );
charlesmn 0:50b05f035d13 43 void print_results( int devSpiNumber, VL53L1_MultiRangingData_t *pMultiRangingData );
charlesmn 0:50b05f035d13 44
charlesmn 0:50b05f035d13 45 VL53L1_Dev_t devCentre;
charlesmn 0:50b05f035d13 46 VL53L1_Dev_t devLeft;
charlesmn 0:50b05f035d13 47 VL53L1_Dev_t devRight;
charlesmn 0:50b05f035d13 48 VL53L1_DEV Dev = &devCentre;
charlesmn 0:50b05f035d13 49
charlesmn 0:50b05f035d13 50
charlesmn 0:50b05f035d13 51 /*=================================== Main ==================================
charlesmn 0:50b05f035d13 52 =============================================================================*/
charlesmn 0:50b05f035d13 53 int main()
charlesmn 0:50b05f035d13 54 {
charlesmn 0:50b05f035d13 55 int status;
charlesmn 5:5916ff386a94 56 VL53L1 * Sensor;
charlesmn 0:50b05f035d13 57 uint16_t wordData;
charlesmn 0:50b05f035d13 58
charlesmn 0:50b05f035d13 59
charlesmn 0:50b05f035d13 60 pc.baud(115200); // baud rate is important as printf statements take a lot of time
charlesmn 2:ef5e40bad526 61 printf("Polling single multizone mbed = %d \r\n",MBED_VERSION);
charlesmn 0:50b05f035d13 62
charlesmn 0:50b05f035d13 63 // create i2c interface
charlesmn 0:50b05f035d13 64 ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL);
charlesmn 0:50b05f035d13 65
charlesmn 0:50b05f035d13 66 /* creates the 53L1A1 expansion board singleton obj */
johnAlexander 4:177d711cc20f 67 board = XNucleo53L1A2::instance(dev_I2C, A2, D8, D2);
charlesmn 0:50b05f035d13 68
charlesmn 0:50b05f035d13 69 printf("board created!\r\n");
charlesmn 0:50b05f035d13 70
charlesmn 0:50b05f035d13 71 /* init the 53L1A1 expansion board with default values */
charlesmn 0:50b05f035d13 72 status = board->init_board();
charlesmn 0:50b05f035d13 73 if (status) {
charlesmn 0:50b05f035d13 74 printf("Failed to init board!\r\n");
charlesmn 0:50b05f035d13 75 return 0;
charlesmn 0:50b05f035d13 76 }
charlesmn 0:50b05f035d13 77
charlesmn 0:50b05f035d13 78
charlesmn 0:50b05f035d13 79 printf("board initiated! - %d\r\n", status);
charlesmn 0:50b05f035d13 80 // create the sensor controller classes
charlesmn 0:50b05f035d13 81
charlesmn 0:50b05f035d13 82
charlesmn 0:50b05f035d13 83
charlesmn 0:50b05f035d13 84 Dev=&devCentre;
charlesmn 0:50b05f035d13 85 Sensor=board->sensor_centre;
charlesmn 0:50b05f035d13 86 Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS;
charlesmn 0:50b05f035d13 87 printf("configuring centre channel \n");
charlesmn 0:50b05f035d13 88
charlesmn 0:50b05f035d13 89
charlesmn 0:50b05f035d13 90 // configure the sensors
charlesmn 0:50b05f035d13 91 Dev->comms_speed_khz = 400;
charlesmn 0:50b05f035d13 92
charlesmn 0:50b05f035d13 93 Dev->comms_type = 1;
charlesmn 0:50b05f035d13 94
charlesmn 0:50b05f035d13 95 Sensor->VL53L1_RdWord(Dev, 0x01, &wordData);
charlesmn 0:50b05f035d13 96 printf("VL53L1X: %02X %d\n\r", wordData,Dev->i2c_slave_address);
charlesmn 0:50b05f035d13 97 /* Device Initialization and setting */
charlesmn 0:50b05f035d13 98 status = Sensor->vl53L1_DataInit();
charlesmn 0:50b05f035d13 99 status = Sensor->vl53L1_StaticInit();
charlesmn 0:50b05f035d13 100 status = Sensor->vl53L1_SetPresetMode( VL53L1_PRESETMODE_MULTIZONES_SCANNING);
charlesmn 1:49e2fad70dfe 101 status = Sensor->vl53L1_SetDistanceMode( VL53L1_DISTANCEMODE_LONG);
charlesmn 1:49e2fad70dfe 102
charlesmn 0:50b05f035d13 103
charlesmn 0:50b05f035d13 104 VL53L1_RoiConfig_t roiConfig;
charlesmn 0:50b05f035d13 105 roiConfig.NumberOfRoi =3;
charlesmn 0:50b05f035d13 106 roiConfig.UserRois[0].TopLeftX = 0;
charlesmn 0:50b05f035d13 107 roiConfig.UserRois[0].TopLeftY = 9;
charlesmn 0:50b05f035d13 108 roiConfig.UserRois[0].BotRightX = 4;
charlesmn 0:50b05f035d13 109 roiConfig.UserRois[0].BotRightY = 5;
charlesmn 0:50b05f035d13 110 roiConfig.UserRois[1].TopLeftX = 5;
charlesmn 0:50b05f035d13 111 roiConfig.UserRois[1].TopLeftY = 9;
charlesmn 0:50b05f035d13 112 roiConfig.UserRois[1].BotRightX = 9;
charlesmn 0:50b05f035d13 113 roiConfig.UserRois[1].BotRightY = 4;
charlesmn 0:50b05f035d13 114 roiConfig.UserRois[2].TopLeftX = 11;
charlesmn 0:50b05f035d13 115 roiConfig.UserRois[2].TopLeftY = 9;
charlesmn 0:50b05f035d13 116 roiConfig.UserRois[2].BotRightX = 15;
charlesmn 0:50b05f035d13 117 roiConfig.UserRois[2].BotRightY = 5;
charlesmn 0:50b05f035d13 118 status = Sensor->vl53L1_SetROI( &roiConfig);
charlesmn 0:50b05f035d13 119 printf("VL53L1_SetROI %d \n",status);
charlesmn 0:50b05f035d13 120
charlesmn 0:50b05f035d13 121 devCentre.i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS;
charlesmn 0:50b05f035d13 122 status = board->sensor_centre->vl53L1_StartMeasurement();
charlesmn 0:50b05f035d13 123
charlesmn 0:50b05f035d13 124 // looping polling for results
charlesmn 0:50b05f035d13 125 while (1)
charlesmn 0:50b05f035d13 126 {
charlesmn 0:50b05f035d13 127 VL53L1_MultiRangingData_t MultiRangingData;
charlesmn 0:50b05f035d13 128 VL53L1_MultiRangingData_t *pMultiRangingData = &MultiRangingData;
charlesmn 0:50b05f035d13 129
charlesmn 0:50b05f035d13 130 // wait for result
charlesmn 0:50b05f035d13 131 status = board->sensor_centre->vl53L1_WaitMeasurementDataReady();
charlesmn 0:50b05f035d13 132 // get the result
charlesmn 0:50b05f035d13 133 status = board->sensor_centre->vl53L1_GetMultiRangingData( pMultiRangingData);
charlesmn 0:50b05f035d13 134 // if valid, print it
charlesmn 0:50b05f035d13 135 if(status==0) {
charlesmn 0:50b05f035d13 136 print_results(devCentre.i2c_slave_address, pMultiRangingData );
charlesmn 0:50b05f035d13 137 status = board->sensor_centre->vl53L1_ClearInterruptAndStartMeasurement();
charlesmn 0:50b05f035d13 138
charlesmn 0:50b05f035d13 139 } //if(status==0)
charlesmn 0:50b05f035d13 140 else
charlesmn 0:50b05f035d13 141 {
charlesmn 0:50b05f035d13 142 printf("VL53L1_GetMultiRangingData centre %d \n",status);
charlesmn 0:50b05f035d13 143 }
charlesmn 0:50b05f035d13 144
charlesmn 0:50b05f035d13 145 wait_ms( 1 * 10);
charlesmn 0:50b05f035d13 146 }
charlesmn 0:50b05f035d13 147 }
charlesmn 0:50b05f035d13 148
charlesmn 0:50b05f035d13 149
charlesmn 0:50b05f035d13 150
charlesmn 0:50b05f035d13 151 // prints the range result seen above
charlesmn 0:50b05f035d13 152 void print_results( int devSpiNumber, VL53L1_MultiRangingData_t *pMultiRangingData )
charlesmn 0:50b05f035d13 153 {
charlesmn 0:50b05f035d13 154 int no_of_object_found=pMultiRangingData->NumberOfObjectsFound;
charlesmn 0:50b05f035d13 155
charlesmn 0:50b05f035d13 156 int RoiNumber=pMultiRangingData->RoiNumber;
charlesmn 0:50b05f035d13 157 // int RoiStatus=pMultiRangingData->RoiStatus;
charlesmn 0:50b05f035d13 158
charlesmn 0:50b05f035d13 159 if (( no_of_object_found < 10 ) && ( no_of_object_found != 0))
charlesmn 0:50b05f035d13 160 {
charlesmn 0:50b05f035d13 161
charlesmn 0:50b05f035d13 162 // printf("MZI Count=%5d, ", pMultiRangingData->StreamCount);
charlesmn 0:50b05f035d13 163 // printf("RoiNumber%1d, ", RoiNumber);
charlesmn 0:50b05f035d13 164 // printf("RoiStatus=%1d, \n", RoiStatus);
charlesmn 0:50b05f035d13 165 for(int j=0;j<no_of_object_found;j++){
charlesmn 0:50b05f035d13 166 if ((pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) ||
charlesmn 0:50b05f035d13 167 (pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL))
charlesmn 0:50b05f035d13 168 {
johnAlexander 8:8f6490937346 169 printf("*****************\t i2cAddr=0x%x \t RoiNumber=%d \t status=%d, \t D=%5dmm, \t Signal=%2.2f Mcps, \t Ambient=%2.2f Mcps \n",
charlesmn 0:50b05f035d13 170 devSpiNumber,
charlesmn 0:50b05f035d13 171 RoiNumber,
charlesmn 0:50b05f035d13 172 pMultiRangingData->RangeData[j].RangeStatus,
charlesmn 0:50b05f035d13 173 pMultiRangingData->RangeData[j].RangeMilliMeter,
charlesmn 0:50b05f035d13 174 pMultiRangingData->RangeData[j].SignalRateRtnMegaCps/65536.0,
charlesmn 0:50b05f035d13 175 pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps/65536.0);
charlesmn 0:50b05f035d13 176 }
charlesmn 0:50b05f035d13 177 else
charlesmn 0:50b05f035d13 178 {
charlesmn 0:50b05f035d13 179 // printf("RangeStatus %d %d\n",j, pMultiRangingData->RangeData[j].RangeStatus);
charlesmn 0:50b05f035d13 180 }
charlesmn 0:50b05f035d13 181 }
charlesmn 0:50b05f035d13 182 } // if (( no_of_object_found < 10 ) && ( no_of_object_found != 0))
charlesmn 0:50b05f035d13 183 else
charlesmn 0:50b05f035d13 184 {
charlesmn 0:50b05f035d13 185 // printf("no_of_object_found %d \n",no_of_object_found);
charlesmn 0:50b05f035d13 186 }
charlesmn 0:50b05f035d13 187
charlesmn 2:ef5e40bad526 188 }
charlesmn 2:ef5e40bad526 189
charlesmn 2:ef5e40bad526 190
charlesmn 3:d1a3d15a06ff 191 #if (MBED_VERSION > 60300)
charlesmn 2:ef5e40bad526 192 extern "C" void wait_ms(int ms)
charlesmn 2:ef5e40bad526 193 {
charlesmn 2:ef5e40bad526 194 thread_sleep_for(ms);
charlesmn 2:ef5e40bad526 195 }
charlesmn 2:ef5e40bad526 196 #endif
charlesmn 0:50b05f035d13 197
charlesmn 0:50b05f035d13 198