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:
Thu May 20 10:48:18 2021 +0000
Revision:
10:17d61466185f
Parent:
9:0a3e1affe004
Child:
11:eafe2b5c130e
Run through astyle for formatting.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
charlesmn 0:50b05f035d13 1 /*
johnAlexander 10:17d61466185f 2 * This VL53L1CB Expansion board test application performs range measurements
johnAlexander 10:17d61466185f 3 * using the onboard embedded sensor, in multizone, polling mode.
johnAlexander 10:17d61466185f 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 *
johnAlexander 10:17d61466185f 10 * The Reset button can be used to restart the program.
johnAlexander 9:0a3e1affe004 11 *
johnAlexander 10:17d61466185f 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 */
johnAlexander 10:17d61466185f 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
johnAlexander 10:17d61466185f 29 #define I2C_SDA D14
johnAlexander 10:17d61466185f 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;
johnAlexander 10:17d61466185f 35 #if (MBED_VERSION > 60300)
johnAlexander 10:17d61466185f 36 UnbufferedSerial pc(USBTX, USBRX);
johnAlexander 10:17d61466185f 37 extern "C" void wait_ms(int ms);
charlesmn 2:ef5e40bad526 38 #else
johnAlexander 10:17d61466185f 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
johnAlexander 10:17d61466185f 50
charlesmn 0:50b05f035d13 51 /*=================================== Main ==================================
charlesmn 0:50b05f035d13 52 =============================================================================*/
charlesmn 0:50b05f035d13 53 int main()
johnAlexander 10:17d61466185f 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);
johnAlexander 10:17d61466185f 65
charlesmn 0:50b05f035d13 66 /* creates the 53L1A1 expansion board singleton obj */
johnAlexander 10:17d61466185f 67 board = XNucleo53L1A2::instance(dev_I2C, A2, D8, D2);
johnAlexander 10:17d61466185f 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 }
johnAlexander 10:17d61466185f 77
charlesmn 0:50b05f035d13 78 printf("board initiated! - %d\r\n", status);
charlesmn 0:50b05f035d13 79
johnAlexander 10:17d61466185f 80 // create the sensor controller classes
charlesmn 0:50b05f035d13 81 Dev=&devCentre;
johnAlexander 10:17d61466185f 82 Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS;
johnAlexander 10:17d61466185f 83 // configure the sensors
johnAlexander 10:17d61466185f 84 Dev->comms_speed_khz = 400;
johnAlexander 10:17d61466185f 85 Dev->comms_type = 1;
johnAlexander 10:17d61466185f 86
charlesmn 0:50b05f035d13 87 Sensor=board->sensor_centre;
johnAlexander 10:17d61466185f 88
charlesmn 0:50b05f035d13 89 printf("configuring centre channel \n");
charlesmn 0:50b05f035d13 90
charlesmn 0:50b05f035d13 91
charlesmn 0:50b05f035d13 92 Sensor->VL53L1_RdWord(Dev, 0x01, &wordData);
charlesmn 0:50b05f035d13 93 printf("VL53L1X: %02X %d\n\r", wordData,Dev->i2c_slave_address);
johnAlexander 10:17d61466185f 94 /* Device Initialization and setting */
charlesmn 0:50b05f035d13 95 status = Sensor->vl53L1_DataInit();
charlesmn 0:50b05f035d13 96 status = Sensor->vl53L1_StaticInit();
johnAlexander 10:17d61466185f 97
johnAlexander 10:17d61466185f 98 status = Sensor->vl53L1_SetPresetMode(VL53L1_PRESETMODE_MULTIZONES_SCANNING);
charlesmn 1:49e2fad70dfe 99
johnAlexander 10:17d61466185f 100 status = Sensor->vl53L1_SetDistanceMode(VL53L1_DISTANCEMODE_LONG);
johnAlexander 10:17d61466185f 101
johnAlexander 10:17d61466185f 102
charlesmn 0:50b05f035d13 103 VL53L1_RoiConfig_t roiConfig;
johnAlexander 10:17d61466185f 104
charlesmn 0:50b05f035d13 105 roiConfig.NumberOfRoi =3;
johnAlexander 10:17d61466185f 106
johnAlexander 10:17d61466185f 107 roiConfig.UserRois[0].TopLeftX = 3;
johnAlexander 10:17d61466185f 108 roiConfig.UserRois[0].TopLeftY = 10;
johnAlexander 10:17d61466185f 109 roiConfig.UserRois[0].BotRightX = 10;
johnAlexander 10:17d61466185f 110 roiConfig.UserRois[0].BotRightY = 3;
johnAlexander 10:17d61466185f 111
charlesmn 0:50b05f035d13 112 roiConfig.UserRois[1].TopLeftX = 5;
johnAlexander 10:17d61466185f 113 roiConfig.UserRois[1].TopLeftY = 12;
johnAlexander 10:17d61466185f 114 roiConfig.UserRois[1].BotRightX = 12;
johnAlexander 10:17d61466185f 115 roiConfig.UserRois[1].BotRightY = 5;
johnAlexander 10:17d61466185f 116
johnAlexander 10:17d61466185f 117 roiConfig.UserRois[2].TopLeftX = 6;
johnAlexander 10:17d61466185f 118 roiConfig.UserRois[2].TopLeftY = 13;
johnAlexander 10:17d61466185f 119 roiConfig.UserRois[2].BotRightX = 13;
johnAlexander 10:17d61466185f 120 roiConfig.UserRois[2].BotRightY = 6;
johnAlexander 10:17d61466185f 121
charlesmn 0:50b05f035d13 122 status = Sensor->vl53L1_SetROI( &roiConfig);
johnAlexander 10:17d61466185f 123
charlesmn 0:50b05f035d13 124 printf("VL53L1_SetROI %d \n",status);
charlesmn 0:50b05f035d13 125
johnAlexander 10:17d61466185f 126 status = board->sensor_centre->vl53L1_StartMeasurement();
charlesmn 0:50b05f035d13 127
johnAlexander 10:17d61466185f 128 // looping polling for results
johnAlexander 10:17d61466185f 129 while (true) {
charlesmn 0:50b05f035d13 130 VL53L1_MultiRangingData_t MultiRangingData;
johnAlexander 10:17d61466185f 131 VL53L1_MultiRangingData_t *pMultiRangingData = &MultiRangingData;
johnAlexander 10:17d61466185f 132
charlesmn 0:50b05f035d13 133 // wait for result
johnAlexander 10:17d61466185f 134 status = board->sensor_centre->vl53L1_WaitMeasurementDataReady();
charlesmn 0:50b05f035d13 135 // get the result
charlesmn 0:50b05f035d13 136 status = board->sensor_centre->vl53L1_GetMultiRangingData( pMultiRangingData);
charlesmn 0:50b05f035d13 137 // if valid, print it
johnAlexander 10:17d61466185f 138 if (status == 0)
charlesmn 0:50b05f035d13 139 print_results(devCentre.i2c_slave_address, pMultiRangingData );
charlesmn 0:50b05f035d13 140 else
charlesmn 0:50b05f035d13 141 printf("VL53L1_GetMultiRangingData centre %d \n",status);
johnAlexander 10:17d61466185f 142
johnAlexander 10:17d61466185f 143 if (status == 0)
johnAlexander 10:17d61466185f 144 status = board->sensor_centre->vl53L1_ClearInterruptAndStartMeasurement();
charlesmn 0:50b05f035d13 145 }
charlesmn 0:50b05f035d13 146 }
johnAlexander 10:17d61466185f 147
johnAlexander 10:17d61466185f 148
johnAlexander 10:17d61466185f 149
johnAlexander 10:17d61466185f 150 // prints the range result seen above
charlesmn 0:50b05f035d13 151 void print_results( int devSpiNumber, VL53L1_MultiRangingData_t *pMultiRangingData )
charlesmn 0:50b05f035d13 152 {
johnAlexander 10:17d61466185f 153 int no_of_object_found=pMultiRangingData->NumberOfObjectsFound;
johnAlexander 10:17d61466185f 154
johnAlexander 10:17d61466185f 155 int RoiNumber=pMultiRangingData->RoiNumber;
charlesmn 0:50b05f035d13 156 // int RoiStatus=pMultiRangingData->RoiStatus;
charlesmn 0:50b05f035d13 157
johnAlexander 10:17d61466185f 158 if (( no_of_object_found < 10 ) && ( no_of_object_found != 0)) {
johnAlexander 10:17d61466185f 159
johnAlexander 10:17d61466185f 160 // printf("MZI Count=%5d, ", pMultiRangingData->StreamCount);
johnAlexander 10:17d61466185f 161 // printf("RoiNumber%1d, ", RoiNumber);
johnAlexander 10:17d61466185f 162 // printf("RoiStatus=%1d, \n", RoiStatus);
johnAlexander 10:17d61466185f 163 for(int j=0; j<no_of_object_found; j++) {
johnAlexander 10:17d61466185f 164 if ((pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) ||
johnAlexander 10:17d61466185f 165 (pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL)) {
johnAlexander 10:17d61466185f 166 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",
johnAlexander 10:17d61466185f 167 devSpiNumber,
johnAlexander 10:17d61466185f 168 RoiNumber,
johnAlexander 10:17d61466185f 169 pMultiRangingData->RangeData[j].RangeStatus,
johnAlexander 10:17d61466185f 170 pMultiRangingData->RangeData[j].RangeMilliMeter,
johnAlexander 10:17d61466185f 171 pMultiRangingData->RangeData[j].SignalRateRtnMegaCps/65536.0,
johnAlexander 10:17d61466185f 172 pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps/65536.0);
johnAlexander 10:17d61466185f 173 } else {
charlesmn 0:50b05f035d13 174 // printf("RangeStatus %d %d\n",j, pMultiRangingData->RangeData[j].RangeStatus);
johnAlexander 10:17d61466185f 175 }
johnAlexander 10:17d61466185f 176 }
johnAlexander 10:17d61466185f 177 } // if (( no_of_object_found < 10 ) && ( no_of_object_found != 0))
johnAlexander 10:17d61466185f 178 else {
charlesmn 0:50b05f035d13 179 // printf("no_of_object_found %d \n",no_of_object_found);
johnAlexander 10:17d61466185f 180 }
charlesmn 0:50b05f035d13 181
johnAlexander 10:17d61466185f 182 }
charlesmn 2:ef5e40bad526 183
johnAlexander 10:17d61466185f 184
charlesmn 3:d1a3d15a06ff 185 #if (MBED_VERSION > 60300)
charlesmn 2:ef5e40bad526 186 extern "C" void wait_ms(int ms)
johnAlexander 10:17d61466185f 187 {
charlesmn 2:ef5e40bad526 188 thread_sleep_for(ms);
johnAlexander 10:17d61466185f 189 }
johnAlexander 10:17d61466185f 190 #endif
johnAlexander 10:17d61466185f 191
johnAlexander 10:17d61466185f 192