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:
Charles MacNeill
Date:
Fri Jun 11 17:22:59 2021 +0100
Revision:
13:2cf61951ea62
Parent:
12:3dd44d06629c
Child:
22:a16cc1505e61
Change case of VL53L1CB.*

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);
johnAlexander 10:17d61466185f 63 extern "C" void wait_ms(int ms);
charlesmn 2:ef5e40bad526 64 #else
johnAlexander 10:17d61466185f 65 Serial pc(SERIAL_TX, SERIAL_RX);
charlesmn 2:ef5e40bad526 66 #endif
charlesmn 0:50b05f035d13 67
johnAlexander 11:eafe2b5c130e 68 void print_results(int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData );
johnAlexander 11:eafe2b5c130e 69
johnAlexander 11:eafe2b5c130e 70
johnAlexander 11:eafe2b5c130e 71 VL53L1_Dev_t devCentre;
johnAlexander 11:eafe2b5c130e 72 VL53L1_DEV Dev = &devCentre;
johnAlexander 11:eafe2b5c130e 73
Charles MacNeill 12:3dd44d06629c 74 VL53L1CB *Sensor;
johnAlexander 11:eafe2b5c130e 75
johnAlexander 11:eafe2b5c130e 76
johnAlexander 11:eafe2b5c130e 77
johnAlexander 11:eafe2b5c130e 78 /* flags that handle interrupt request for sensor and user blue button*/
johnAlexander 11:eafe2b5c130e 79 volatile bool int_sensor = false;
johnAlexander 11:eafe2b5c130e 80 volatile bool int_stop = false;
johnAlexander 11:eafe2b5c130e 81
johnAlexander 11:eafe2b5c130e 82 /* ISR callback function of the centre sensor */
johnAlexander 11:eafe2b5c130e 83 void sensor_irq(void)
johnAlexander 11:eafe2b5c130e 84 {
johnAlexander 11:eafe2b5c130e 85 int_sensor = true;
johnAlexander 11:eafe2b5c130e 86 board->sensor_centre->disable_interrupt_measure_detection_irq();
johnAlexander 11:eafe2b5c130e 87 }
johnAlexander 11:eafe2b5c130e 88
johnAlexander 11:eafe2b5c130e 89 /* Start the sensor ranging */
johnAlexander 11:eafe2b5c130e 90 int init_sensor()
johnAlexander 11:eafe2b5c130e 91 {
johnAlexander 11:eafe2b5c130e 92 int status = 0;
johnAlexander 11:eafe2b5c130e 93
johnAlexander 11:eafe2b5c130e 94 Dev=&devCentre;
johnAlexander 11:eafe2b5c130e 95 Sensor=board->sensor_centre;
johnAlexander 11:eafe2b5c130e 96
johnAlexander 11:eafe2b5c130e 97 // configure the sensors
johnAlexander 11:eafe2b5c130e 98 Dev->comms_speed_khz = 400;
johnAlexander 11:eafe2b5c130e 99 Dev->comms_type = 1;
johnAlexander 11:eafe2b5c130e 100 Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS;
johnAlexander 11:eafe2b5c130e 101
johnAlexander 11:eafe2b5c130e 102 printf("configuring centre channel \n");
johnAlexander 11:eafe2b5c130e 103
johnAlexander 11:eafe2b5c130e 104 /* Device Initialization and setting */
Charles MacNeill 12:3dd44d06629c 105 status = Sensor->VL53L1CB_DataInit();
Charles MacNeill 12:3dd44d06629c 106 status = Sensor->VL53L1CB_StaticInit();
johnAlexander 11:eafe2b5c130e 107
johnAlexander 11:eafe2b5c130e 108 VL53L1_RoiConfig_t RoiConfig;
johnAlexander 11:eafe2b5c130e 109
johnAlexander 11:eafe2b5c130e 110 RoiConfig.NumberOfRoi = 3;
charlesmn 0:50b05f035d13 111
johnAlexander 11:eafe2b5c130e 112 RoiConfig.UserRois[0].TopLeftX = 3;
johnAlexander 11:eafe2b5c130e 113 RoiConfig.UserRois[0].TopLeftY = 10;
johnAlexander 11:eafe2b5c130e 114 RoiConfig.UserRois[0].BotRightX = 10;
johnAlexander 11:eafe2b5c130e 115 RoiConfig.UserRois[0].BotRightY = 3;
johnAlexander 11:eafe2b5c130e 116
johnAlexander 11:eafe2b5c130e 117 RoiConfig.UserRois[1].TopLeftX = 5;
johnAlexander 11:eafe2b5c130e 118 RoiConfig.UserRois[1].TopLeftY = 12;
johnAlexander 11:eafe2b5c130e 119 RoiConfig.UserRois[1].BotRightX = 12;
johnAlexander 11:eafe2b5c130e 120 RoiConfig.UserRois[1].BotRightY = 5;
johnAlexander 11:eafe2b5c130e 121
johnAlexander 11:eafe2b5c130e 122 RoiConfig.UserRois[2].TopLeftX = 6;
johnAlexander 11:eafe2b5c130e 123 RoiConfig.UserRois[2].TopLeftY = 13;
johnAlexander 11:eafe2b5c130e 124 RoiConfig.UserRois[2].BotRightX = 13;
johnAlexander 11:eafe2b5c130e 125 RoiConfig.UserRois[2].BotRightY = 6;
johnAlexander 11:eafe2b5c130e 126
Charles MacNeill 12:3dd44d06629c 127 status = Sensor->VL53L1CB_SetPresetMode(VL53L1_PRESETMODE_MULTIZONES_SCANNING);
Charles MacNeill 12:3dd44d06629c 128 status = Sensor->VL53L1CB_SetDistanceMode(VL53L1_DISTANCEMODE_LONG);
johnAlexander 11:eafe2b5c130e 129
Charles MacNeill 12:3dd44d06629c 130 status = Sensor->VL53L1CB_SetROI(&RoiConfig);
johnAlexander 11:eafe2b5c130e 131
Charles MacNeill 12:3dd44d06629c 132 status = Sensor->VL53L1CB_SetMeasurementTimingBudgetMicroSeconds(17000);
Charles MacNeill 13:2cf61951ea62 133
Charles MacNeill 13:2cf61951ea62 134
Charles MacNeill 13:2cf61951ea62 135 VL53L1_DeviceInfo_t device_info;
Charles MacNeill 13:2cf61951ea62 136
Charles MacNeill 13:2cf61951ea62 137 status = Sensor->VL53L1CB_GetDeviceInfo(&device_info);
Charles MacNeill 13:2cf61951ea62 138 printf("device name %s \n",device_info.Name);
Charles MacNeill 13:2cf61951ea62 139 printf("device type %s \n",device_info.Type);
Charles MacNeill 13:2cf61951ea62 140 printf("device productID %s \n",device_info.ProductId);
Charles MacNeill 13:2cf61951ea62 141 printf("device productType %x \n",device_info.ProductType);
charlesmn 0:50b05f035d13 142
johnAlexander 10:17d61466185f 143
johnAlexander 11:eafe2b5c130e 144 // create interrupt handler and start measurements
johnAlexander 11:eafe2b5c130e 145 if (board->sensor_centre!= NULL) {
johnAlexander 11:eafe2b5c130e 146 status = board->sensor_centre->stop_measurement();
johnAlexander 11:eafe2b5c130e 147 if (status != 0) {
johnAlexander 11:eafe2b5c130e 148 return status;
johnAlexander 11:eafe2b5c130e 149 }
johnAlexander 11:eafe2b5c130e 150
Charles MacNeill 12:3dd44d06629c 151 status = Sensor->VL53L1CB_StartMeasurement();
johnAlexander 11:eafe2b5c130e 152 if (status != 0) {
johnAlexander 11:eafe2b5c130e 153 return status;
johnAlexander 11:eafe2b5c130e 154 }
johnAlexander 11:eafe2b5c130e 155 }
johnAlexander 11:eafe2b5c130e 156 return status;
johnAlexander 11:eafe2b5c130e 157 }
johnAlexander 11:eafe2b5c130e 158
johnAlexander 11:eafe2b5c130e 159 /* ISR callback function of the user blue button to switch measuring sensor. */
johnAlexander 11:eafe2b5c130e 160 void measuring_stop_irq(void)
johnAlexander 11:eafe2b5c130e 161 {
johnAlexander 11:eafe2b5c130e 162 int_stop = true;
johnAlexander 11:eafe2b5c130e 163 }
johnAlexander 11:eafe2b5c130e 164
charlesmn 0:50b05f035d13 165 /*=================================== Main ==================================
charlesmn 0:50b05f035d13 166 =============================================================================*/
charlesmn 0:50b05f035d13 167 int main()
johnAlexander 10:17d61466185f 168 {
charlesmn 0:50b05f035d13 169 int status;
johnAlexander 11:eafe2b5c130e 170 uint16_t distance = 0;
charlesmn 0:50b05f035d13 171
charlesmn 0:50b05f035d13 172 pc.baud(115200); // baud rate is important as printf statements take a lot of time
johnAlexander 11:eafe2b5c130e 173
johnAlexander 11:eafe2b5c130e 174 printf("mbed version : %d \r\n",MBED_VERSION);
charlesmn 0:50b05f035d13 175
charlesmn 0:50b05f035d13 176 // create i2c interface
charlesmn 0:50b05f035d13 177 ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL);
johnAlexander 11:eafe2b5c130e 178 /* creates the 53L1A2 expansion board singleton obj */
johnAlexander 11:eafe2b5c130e 179 board = XNucleo53L1A2::instance(dev_I2C, CentreIntPin, LeftIntPin, RightIntPin);
johnAlexander 10:17d61466185f 180
charlesmn 0:50b05f035d13 181 printf("board created!\r\n");
charlesmn 0:50b05f035d13 182
charlesmn 0:50b05f035d13 183 /* init the 53L1A1 expansion board with default values */
charlesmn 0:50b05f035d13 184 status = board->init_board();
charlesmn 0:50b05f035d13 185 if (status) {
charlesmn 0:50b05f035d13 186 printf("Failed to init board!\r\n");
johnAlexander 11:eafe2b5c130e 187 return status;
charlesmn 0:50b05f035d13 188 }
johnAlexander 10:17d61466185f 189
charlesmn 0:50b05f035d13 190 printf("board initiated! - %d\r\n", status);
charlesmn 0:50b05f035d13 191
johnAlexander 11:eafe2b5c130e 192 /* init an array with chars to id the sensors */
johnAlexander 11:eafe2b5c130e 193 status = init_sensor();
johnAlexander 11:eafe2b5c130e 194 if (status != 0) {
johnAlexander 11:eafe2b5c130e 195 printf("Failed to init sensors!\r\n");
johnAlexander 11:eafe2b5c130e 196 return status;
johnAlexander 11:eafe2b5c130e 197 }
charlesmn 0:50b05f035d13 198
johnAlexander 11:eafe2b5c130e 199 printf("loop forever\n");
johnAlexander 10:17d61466185f 200
johnAlexander 11:eafe2b5c130e 201 VL53L1_MultiRangingData_t MultiRangingData;
johnAlexander 11:eafe2b5c130e 202 VL53L1_MultiRangingData_t *pMultiRangingData = NULL;
johnAlexander 10:17d61466185f 203
johnAlexander 11:eafe2b5c130e 204 while (true) {
johnAlexander 11:eafe2b5c130e 205 pMultiRangingData = &MultiRangingData;
johnAlexander 10:17d61466185f 206
Charles MacNeill 12:3dd44d06629c 207 status = board->sensor_centre->VL53L1CB_WaitMeasurementDataReady();
Charles MacNeill 12:3dd44d06629c 208 status = board->sensor_centre->VL53L1CB_GetMultiRangingData( pMultiRangingData);
charlesmn 0:50b05f035d13 209
johnAlexander 11:eafe2b5c130e 210 print_results( devCentre.i2c_slave_address, pMultiRangingData );
johnAlexander 10:17d61466185f 211
Charles MacNeill 12:3dd44d06629c 212 status = board->sensor_centre->VL53L1CB_ClearInterrupt();
johnAlexander 11:eafe2b5c130e 213 }
johnAlexander 10:17d61466185f 214
johnAlexander 11:eafe2b5c130e 215 printf("Terminating.\n");
charlesmn 0:50b05f035d13 216 }
johnAlexander 10:17d61466185f 217
johnAlexander 10:17d61466185f 218
johnAlexander 11:eafe2b5c130e 219 // print what ever results are required
johnAlexander 11:eafe2b5c130e 220 void print_results( int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData )
charlesmn 0:50b05f035d13 221 {
johnAlexander 11:eafe2b5c130e 222 int no_of_object_found = pMultiRangingData->NumberOfObjectsFound;
johnAlexander 11:eafe2b5c130e 223 int signal_rate = 0;
johnAlexander 11:eafe2b5c130e 224 int ambient_rate = 0;
johnAlexander 10:17d61466185f 225
johnAlexander 11:eafe2b5c130e 226 int RoiNumber = pMultiRangingData->RoiNumber;
johnAlexander 10:17d61466185f 227
johnAlexander 11:eafe2b5c130e 228 if (no_of_object_found <= 1)
johnAlexander 11:eafe2b5c130e 229 no_of_object_found = 1;
johnAlexander 11:eafe2b5c130e 230 for(int j=0; j<no_of_object_found; j++) {
johnAlexander 11:eafe2b5c130e 231 signal_rate = pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65535;
johnAlexander 11:eafe2b5c130e 232 ambient_rate = pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65535;
johnAlexander 11:eafe2b5c130e 233 printf("\t i2cAddr=%d \t RoiNumber=%d \t status=%d, \t D=%5dmm, \t Signal=%d Mcps, \t Ambient=%d Mcps \n",
johnAlexander 11:eafe2b5c130e 234 devNumber, RoiNumber,
johnAlexander 11:eafe2b5c130e 235 pMultiRangingData->RangeData[j].RangeStatus,
johnAlexander 11:eafe2b5c130e 236 pMultiRangingData->RangeData[j].RangeMilliMeter,
johnAlexander 11:eafe2b5c130e 237 signal_rate,
johnAlexander 11:eafe2b5c130e 238 ambient_rate);
johnAlexander 11:eafe2b5c130e 239 /*
johnAlexander 11:eafe2b5c130e 240 // online compiler disables printf() / floating-point support, for code-size reasons.
johnAlexander 11:eafe2b5c130e 241 // offline compiler can switch it on.
johnAlexander 11:eafe2b5c130e 242 printf("\t i2cAddr=%d \t RoiNumber=%d \t status=%d, \t D=%5dmm, \t Signal=%2.2f Mcps, \t Ambient=%2.2f Mcps \n",
johnAlexander 11:eafe2b5c130e 243 devNumber, RoiNumber,
johnAlexander 11:eafe2b5c130e 244 pMultiRangingData->RangeData[j].RangeStatus,
johnAlexander 11:eafe2b5c130e 245 pMultiRangingData->RangeData[j].RangeMilliMeter,
johnAlexander 11:eafe2b5c130e 246 pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65535.0,
johnAlexander 11:eafe2b5c130e 247 pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65535.0);
johnAlexander 11:eafe2b5c130e 248 */
johnAlexander 10:17d61466185f 249 }
johnAlexander 10:17d61466185f 250 }
charlesmn 2:ef5e40bad526 251
johnAlexander 10:17d61466185f 252
charlesmn 3:d1a3d15a06ff 253 #if (MBED_VERSION > 60300)
charlesmn 2:ef5e40bad526 254 extern "C" void wait_ms(int ms)
johnAlexander 10:17d61466185f 255 {
charlesmn 2:ef5e40bad526 256 thread_sleep_for(ms);
johnAlexander 10:17d61466185f 257 }
johnAlexander 10:17d61466185f 258 #endif
johnAlexander 10:17d61466185f 259
johnAlexander 10:17d61466185f 260