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:
lugandc
Date:
Thu Jul 22 11:44:20 2021 +0200
Revision:
22:a16cc1505e61
Parent:
13:2cf61951ea62
Cleanup Example and update X_NUCLEO_53L1A2 lib

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 11:eafe2b5c130e 3 * using the onboard embedded sensor, in polling mode.
johnAlexander 11:eafe2b5c130e 4 * Measured ranges are output on the Serial Port, running at 115200 baud.
charlesmn 0:50b05f035d13 5 *
johnAlexander 11:eafe2b5c130e 6 * This is designed to work with MBed v2.x, & MBedOS v5.x / v6.x.
charlesmn 0:50b05f035d13 7 *
johnAlexander 10:17d61466185f 8 * The Reset button can be used to restart the program.
johnAlexander 9:0a3e1affe004 9 *
johnAlexander 11:eafe2b5c130e 10 * *** NOTE :
johnAlexander 11:eafe2b5c130e 11 * Default Mbed build system settings disable printf() floating-point support.
johnAlexander 11:eafe2b5c130e 12 * Offline builds can enable this, again.
johnAlexander 11:eafe2b5c130e 13 * https://github.com/ARMmbed/mbed-os/blob/master/platform/source/minimal-printf/README.md
johnAlexander 11:eafe2b5c130e 14 * .\mbed-os\platform\mbed_lib.json
johnAlexander 11:eafe2b5c130e 15 *
johnAlexander 11:eafe2b5c130e 16 * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of
johnAlexander 11:eafe2b5c130e 17 * the X-NUCELO-53L1A1 expansion board are not made/OFF.
johnAlexander 11:eafe2b5c130e 18 * These links must be made to allow interrupts from the Satellite boards
johnAlexander 11:eafe2b5c130e 19 * to be received.
johnAlexander 11:eafe2b5c130e 20 * U11 and U18 must be made/ON to allow interrupts to be received from the
johnAlexander 11:eafe2b5c130e 21 * INT_L & INT_R positions; or
johnAlexander 11:eafe2b5c130e 22 * U10 and U15 must be made/ON to allow interrupts to be received from the
johnAlexander 11:eafe2b5c130e 23 * Alternate INT_L & INT_R positions.
johnAlexander 11:eafe2b5c130e 24 * The X_NUCLEO_53L1A2 library defaults to use the INT_L/INT_R positions.
johnAlexander 11:eafe2b5c130e 25 * INT_L is available on expansion board Arduino Connector CN5, pin 1 as D8.
johnAlexander 11:eafe2b5c130e 26 * Alternate INT_L is on CN5 Connector pin 2 as D9.
johnAlexander 11:eafe2b5c130e 27 * INT_R is available on expansion board Arduino Connector CN9, pin 3 as D2.
johnAlexander 11:eafe2b5c130e 28 * Alternate INT_R is on CN9 Connector pin 5 as D4.
johnAlexander 11:eafe2b5c130e 29 * The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A2/
johnAlexander 9:0a3e1affe004 30 *
charlesmn 0:50b05f035d13 31 */
johnAlexander 10:17d61466185f 32
charlesmn 0:50b05f035d13 33 #include <stdio.h>
johnAlexander 6:bc6ac1e294f4 34 #include <time.h>
charlesmn 0:50b05f035d13 35
charlesmn 0:50b05f035d13 36 #include "mbed.h"
johnAlexander 11:eafe2b5c130e 37
johnAlexander 4:177d711cc20f 38 #include "XNucleo53L1A2.h"
charlesmn 0:50b05f035d13 39 #include "ToF_I2C.h"
charlesmn 0:50b05f035d13 40
johnAlexander 11:eafe2b5c130e 41 // i2c comms port pins
johnAlexander 10:17d61466185f 42 #define I2C_SDA D14
johnAlexander 10:17d61466185f 43 #define I2C_SCL D15
charlesmn 0:50b05f035d13 44
charlesmn 0:50b05f035d13 45
johnAlexander 11:eafe2b5c130e 46 #define NUM_SENSORS 3
johnAlexander 11:eafe2b5c130e 47
johnAlexander 11:eafe2b5c130e 48 // define interrupt pins
johnAlexander 11:eafe2b5c130e 49 PinName CentreIntPin = A2;
johnAlexander 11:eafe2b5c130e 50 // the satellite pins depend on solder blobs on the back of the shield.
johnAlexander 11:eafe2b5c130e 51 // they may not exist or may be one of two sets.
johnAlexander 11:eafe2b5c130e 52 // the centre pin always exists
johnAlexander 11:eafe2b5c130e 53 //PinName LeftIntPin = D8;
johnAlexander 11:eafe2b5c130e 54 PinName RightIntPin = D2;
johnAlexander 11:eafe2b5c130e 55 // alternate set
johnAlexander 11:eafe2b5c130e 56 PinName LeftIntPin = D9;
johnAlexander 11:eafe2b5c130e 57 //PinName RightIntPin = D4;
charlesmn 0:50b05f035d13 58
johnAlexander 4:177d711cc20f 59 static XNucleo53L1A2 *board=NULL;
johnAlexander 11:eafe2b5c130e 60
johnAlexander 10:17d61466185f 61 #if (MBED_VERSION > 60300)
johnAlexander 10:17d61466185f 62 UnbufferedSerial pc(USBTX, USBRX);
charlesmn 2:ef5e40bad526 63 #else
johnAlexander 10:17d61466185f 64 Serial pc(SERIAL_TX, SERIAL_RX);
charlesmn 2:ef5e40bad526 65 #endif
charlesmn 0:50b05f035d13 66
johnAlexander 11:eafe2b5c130e 67 void print_results(int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData );
johnAlexander 11:eafe2b5c130e 68
johnAlexander 11:eafe2b5c130e 69
johnAlexander 11:eafe2b5c130e 70 VL53L1_Dev_t devCentre;
johnAlexander 11:eafe2b5c130e 71 VL53L1_DEV Dev = &devCentre;
johnAlexander 11:eafe2b5c130e 72
Charles MacNeill 12:3dd44d06629c 73 VL53L1CB *Sensor;
johnAlexander 11:eafe2b5c130e 74
johnAlexander 11:eafe2b5c130e 75
johnAlexander 11:eafe2b5c130e 76
johnAlexander 11:eafe2b5c130e 77 /* flags that handle interrupt request for sensor and user blue button*/
johnAlexander 11:eafe2b5c130e 78 volatile bool int_sensor = false;
johnAlexander 11:eafe2b5c130e 79 volatile bool int_stop = false;
johnAlexander 11:eafe2b5c130e 80
johnAlexander 11:eafe2b5c130e 81 /* ISR callback function of the centre sensor */
johnAlexander 11:eafe2b5c130e 82 void sensor_irq(void)
johnAlexander 11:eafe2b5c130e 83 {
johnAlexander 11:eafe2b5c130e 84 int_sensor = true;
johnAlexander 11:eafe2b5c130e 85 board->sensor_centre->disable_interrupt_measure_detection_irq();
johnAlexander 11:eafe2b5c130e 86 }
johnAlexander 11:eafe2b5c130e 87
johnAlexander 11:eafe2b5c130e 88 /* Start the sensor ranging */
lugandc 22:a16cc1505e61 89 int configure_sensor()
johnAlexander 11:eafe2b5c130e 90 {
johnAlexander 11:eafe2b5c130e 91 int status = 0;
lugandc 22:a16cc1505e61 92 VL53L1_DeviceInfo_t device_info;
lugandc 22:a16cc1505e61 93 VL53L1_RoiConfig_t RoiConfig;
johnAlexander 11:eafe2b5c130e 94
lugandc 22:a16cc1505e61 95 Dev = &devCentre;
lugandc 22:a16cc1505e61 96 Sensor = board->sensor_centre;
lugandc 22:a16cc1505e61 97
lugandc 22:a16cc1505e61 98 if (Sensor == NULL)
lugandc 22:a16cc1505e61 99 return -1;
johnAlexander 11:eafe2b5c130e 100
johnAlexander 11:eafe2b5c130e 101 printf("configuring centre channel \n");
johnAlexander 11:eafe2b5c130e 102
lugandc 22:a16cc1505e61 103 status = Sensor->VL53L1CB_GetDeviceInfo(&device_info);
lugandc 22:a16cc1505e61 104 if (status != 0) {
lugandc 22:a16cc1505e61 105 return status;
lugandc 22:a16cc1505e61 106 }
lugandc 22:a16cc1505e61 107 printf("device name %s \n",device_info.Name);
lugandc 22:a16cc1505e61 108 printf("device type %s \n",device_info.Type);
lugandc 22:a16cc1505e61 109 printf("device productID %s \n",device_info.ProductId);
lugandc 22:a16cc1505e61 110 printf("device productType %x \n",device_info.ProductType);
johnAlexander 11:eafe2b5c130e 111
johnAlexander 11:eafe2b5c130e 112 RoiConfig.NumberOfRoi = 3;
charlesmn 0:50b05f035d13 113
johnAlexander 11:eafe2b5c130e 114 RoiConfig.UserRois[0].TopLeftX = 3;
johnAlexander 11:eafe2b5c130e 115 RoiConfig.UserRois[0].TopLeftY = 10;
johnAlexander 11:eafe2b5c130e 116 RoiConfig.UserRois[0].BotRightX = 10;
johnAlexander 11:eafe2b5c130e 117 RoiConfig.UserRois[0].BotRightY = 3;
johnAlexander 11:eafe2b5c130e 118
johnAlexander 11:eafe2b5c130e 119 RoiConfig.UserRois[1].TopLeftX = 5;
johnAlexander 11:eafe2b5c130e 120 RoiConfig.UserRois[1].TopLeftY = 12;
johnAlexander 11:eafe2b5c130e 121 RoiConfig.UserRois[1].BotRightX = 12;
johnAlexander 11:eafe2b5c130e 122 RoiConfig.UserRois[1].BotRightY = 5;
johnAlexander 11:eafe2b5c130e 123
johnAlexander 11:eafe2b5c130e 124 RoiConfig.UserRois[2].TopLeftX = 6;
johnAlexander 11:eafe2b5c130e 125 RoiConfig.UserRois[2].TopLeftY = 13;
johnAlexander 11:eafe2b5c130e 126 RoiConfig.UserRois[2].BotRightX = 13;
johnAlexander 11:eafe2b5c130e 127 RoiConfig.UserRois[2].BotRightY = 6;
johnAlexander 11:eafe2b5c130e 128
Charles MacNeill 12:3dd44d06629c 129 status = Sensor->VL53L1CB_SetPresetMode(VL53L1_PRESETMODE_MULTIZONES_SCANNING);
lugandc 22:a16cc1505e61 130 if (status != 0) {
lugandc 22:a16cc1505e61 131 return status;
lugandc 22:a16cc1505e61 132 }
lugandc 22:a16cc1505e61 133
Charles MacNeill 12:3dd44d06629c 134 status = Sensor->VL53L1CB_SetDistanceMode(VL53L1_DISTANCEMODE_LONG);
lugandc 22:a16cc1505e61 135 if (status != 0) {
lugandc 22:a16cc1505e61 136 return status;
lugandc 22:a16cc1505e61 137 }
johnAlexander 11:eafe2b5c130e 138
Charles MacNeill 12:3dd44d06629c 139 status = Sensor->VL53L1CB_SetROI(&RoiConfig);
lugandc 22:a16cc1505e61 140 if (status != 0) {
lugandc 22:a16cc1505e61 141 return status;
lugandc 22:a16cc1505e61 142 }
johnAlexander 10:17d61466185f 143
lugandc 22:a16cc1505e61 144 status = Sensor->VL53L1CB_SetMeasurementTimingBudgetMicroSeconds(60000);
lugandc 22:a16cc1505e61 145 if (status != 0) {
lugandc 22:a16cc1505e61 146 return status;
lugandc 22:a16cc1505e61 147 }
johnAlexander 11:eafe2b5c130e 148
johnAlexander 11:eafe2b5c130e 149 return status;
johnAlexander 11:eafe2b5c130e 150 }
johnAlexander 11:eafe2b5c130e 151
johnAlexander 11:eafe2b5c130e 152 /* ISR callback function of the user blue button to switch measuring sensor. */
johnAlexander 11:eafe2b5c130e 153 void measuring_stop_irq(void)
johnAlexander 11:eafe2b5c130e 154 {
johnAlexander 11:eafe2b5c130e 155 int_stop = true;
johnAlexander 11:eafe2b5c130e 156 }
johnAlexander 11:eafe2b5c130e 157
charlesmn 0:50b05f035d13 158 /*=================================== Main ==================================
charlesmn 0:50b05f035d13 159 =============================================================================*/
charlesmn 0:50b05f035d13 160 int main()
johnAlexander 10:17d61466185f 161 {
charlesmn 0:50b05f035d13 162 int status;
charlesmn 0:50b05f035d13 163
charlesmn 0:50b05f035d13 164 pc.baud(115200); // baud rate is important as printf statements take a lot of time
johnAlexander 11:eafe2b5c130e 165
johnAlexander 11:eafe2b5c130e 166 printf("mbed version : %d \r\n",MBED_VERSION);
charlesmn 0:50b05f035d13 167
charlesmn 0:50b05f035d13 168 // create i2c interface
charlesmn 0:50b05f035d13 169 ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL);
johnAlexander 11:eafe2b5c130e 170 /* creates the 53L1A2 expansion board singleton obj */
lugandc 22:a16cc1505e61 171 board = XNucleo53L1A2::instance(dev_I2C, CentreIntPin, LeftIntPin, RightIntPin);
johnAlexander 10:17d61466185f 172
charlesmn 0:50b05f035d13 173 printf("board created!\r\n");
charlesmn 0:50b05f035d13 174
charlesmn 0:50b05f035d13 175 /* init the 53L1A1 expansion board with default values */
charlesmn 0:50b05f035d13 176 status = board->init_board();
charlesmn 0:50b05f035d13 177 if (status) {
charlesmn 0:50b05f035d13 178 printf("Failed to init board!\r\n");
johnAlexander 11:eafe2b5c130e 179 return status;
charlesmn 0:50b05f035d13 180 }
johnAlexander 10:17d61466185f 181
charlesmn 0:50b05f035d13 182 printf("board initiated! - %d\r\n", status);
charlesmn 0:50b05f035d13 183
johnAlexander 11:eafe2b5c130e 184 /* init an array with chars to id the sensors */
lugandc 22:a16cc1505e61 185 status = configure_sensor();
johnAlexander 11:eafe2b5c130e 186 if (status != 0) {
johnAlexander 11:eafe2b5c130e 187 printf("Failed to init sensors!\r\n");
johnAlexander 11:eafe2b5c130e 188 return status;
johnAlexander 11:eafe2b5c130e 189 }
charlesmn 0:50b05f035d13 190
lugandc 22:a16cc1505e61 191 // start measurements
lugandc 22:a16cc1505e61 192 status = board->sensor_centre->VL53L1CB_StartMeasurement();
lugandc 22:a16cc1505e61 193 if (status != 0) {
lugandc 22:a16cc1505e61 194 return status;
lugandc 22:a16cc1505e61 195 }
lugandc 22:a16cc1505e61 196
johnAlexander 11:eafe2b5c130e 197 printf("loop forever\n");
johnAlexander 10:17d61466185f 198
johnAlexander 11:eafe2b5c130e 199 VL53L1_MultiRangingData_t MultiRangingData;
lugandc 22:a16cc1505e61 200 VL53L1_MultiRangingData_t *pMultiRangingData = &MultiRangingData;
johnAlexander 10:17d61466185f 201
johnAlexander 11:eafe2b5c130e 202 while (true) {
johnAlexander 10:17d61466185f 203
Charles MacNeill 12:3dd44d06629c 204 status = board->sensor_centre->VL53L1CB_WaitMeasurementDataReady();
Charles MacNeill 12:3dd44d06629c 205 status = board->sensor_centre->VL53L1CB_GetMultiRangingData( pMultiRangingData);
charlesmn 0:50b05f035d13 206
johnAlexander 11:eafe2b5c130e 207 print_results( devCentre.i2c_slave_address, pMultiRangingData );
johnAlexander 10:17d61466185f 208
lugandc 22:a16cc1505e61 209 status = board->sensor_centre->VL53L1CB_ClearInterruptAndStartMeasurement();
johnAlexander 11:eafe2b5c130e 210 }
johnAlexander 10:17d61466185f 211
johnAlexander 11:eafe2b5c130e 212 printf("Terminating.\n");
charlesmn 0:50b05f035d13 213 }
johnAlexander 10:17d61466185f 214
johnAlexander 10:17d61466185f 215
johnAlexander 11:eafe2b5c130e 216 // print what ever results are required
johnAlexander 11:eafe2b5c130e 217 void print_results( int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData )
charlesmn 0:50b05f035d13 218 {
johnAlexander 11:eafe2b5c130e 219 int no_of_object_found = pMultiRangingData->NumberOfObjectsFound;
lugandc 22:a16cc1505e61 220 int signal_kcps = 0;
lugandc 22:a16cc1505e61 221 int ambient_kcps = 0;
johnAlexander 10:17d61466185f 222
johnAlexander 11:eafe2b5c130e 223 int RoiNumber = pMultiRangingData->RoiNumber;
johnAlexander 10:17d61466185f 224
johnAlexander 11:eafe2b5c130e 225 if (no_of_object_found <= 1)
johnAlexander 11:eafe2b5c130e 226 no_of_object_found = 1;
lugandc 22:a16cc1505e61 227 printf("i2cAddr=%d\tRoiNumber=%d", devNumber, RoiNumber);
johnAlexander 11:eafe2b5c130e 228 for(int j=0; j<no_of_object_found; j++) {
lugandc 22:a16cc1505e61 229 signal_kcps = 1000*(pMultiRangingData->RangeData[j].SignalRateRtnMegaCps) / 65536;
lugandc 22:a16cc1505e61 230 ambient_kcps = 1000*(pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps) / 65536;
lugandc 22:a16cc1505e61 231 if (j > 0)
lugandc 22:a16cc1505e61 232 printf("\t\t\t");
lugandc 22:a16cc1505e61 233 printf("\trange[%d] status=%d, \t D=%5dmm, \t Signal=%d Kcps, \t Ambient=%d Kcps \n",
lugandc 22:a16cc1505e61 234 j,
lugandc 22:a16cc1505e61 235 pMultiRangingData->RangeData[j].RangeStatus,
lugandc 22:a16cc1505e61 236 pMultiRangingData->RangeData[j].RangeMilliMeter,
lugandc 22:a16cc1505e61 237 signal_kcps,
lugandc 22:a16cc1505e61 238 ambient_kcps);
johnAlexander 10:17d61466185f 239 }
johnAlexander 10:17d61466185f 240 }
charlesmn 2:ef5e40bad526 241