VL53L1CB expansion board example, showing multi-ranges in an autonomous setup & polling mode. Uses the onboard sensor. Targets MbedOS v6.10.0.

Dependencies:   X_NUCLEO_53L1A2

Committer:
lugandc
Date:
Mon Jul 26 16:22:00 2021 +0000
Revision:
15:ed4d4b4d379c
Parent:
10:2da1507fa8c2
Cleanup Example and update X_NUCLEO_53L1A2 lib

Who changed what in which revision?

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