VL53L3CX ranging example application, using X-Nucleo-53L3A2 expansion shield and polling, on MbedOS v6.x

Dependencies:   X_NUCLEO_53L3A2

Committer:
johnAlexander
Date:
Fri May 07 15:12:28 2021 +0000
Revision:
13:c0ef2ae9e6a7
Parent:
11:6d4d6e9b3729
Reformat to match mbed coding style.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
charlesmn 0:2a67f2b41fd7 1 /*
johnAlexander 13:c0ef2ae9e6a7 2 * This VL53L3 Expansion board test application performs range measurements
johnAlexander 13:c0ef2ae9e6a7 3 * using the onboard embedded centre sensor, in singleshot, polling mode.
johnAlexander 13:c0ef2ae9e6a7 4 * Measured ranges are ouput on the Serial Port, running at 115200 baud.
johnAlexander 13:c0ef2ae9e6a7 5 *
johnAlexander 13:c0ef2ae9e6a7 6 * The User Blue button stops the current measurement and entire program,
johnAlexander 13:c0ef2ae9e6a7 7 * releasing all resources.
charlesmn 0:2a67f2b41fd7 8 *
johnAlexander 13:c0ef2ae9e6a7 9 * The Reset button can be used to restart the program.
charlesmn 0:2a67f2b41fd7 10 *
johnAlexander 13:c0ef2ae9e6a7 11 * *** Note :
johnAlexander 13:c0ef2ae9e6a7 12 * Default Mbed build system settings disable print floating-point support.
johnAlexander 13:c0ef2ae9e6a7 13 * Offline builds can enable this, again.
johnAlexander 13:c0ef2ae9e6a7 14 * https://github.com/ARMmbed/mbed-os/blob/master/platform/source/minimal-printf/README.md
johnAlexander 13:c0ef2ae9e6a7 15 * .\mbed-os\platform\mbed_lib.json
johnAlexander 13:c0ef2ae9e6a7 16 *
charlesmn 0:2a67f2b41fd7 17 */
johnAlexander 13:c0ef2ae9e6a7 18
charlesmn 0:2a67f2b41fd7 19 #include <stdio.h>
johnAlexander 3:7826b7dbd1b0 20 #include <time.h>
charlesmn 0:2a67f2b41fd7 21
charlesmn 0:2a67f2b41fd7 22 #include "mbed.h"
johnAlexander 13:c0ef2ae9e6a7 23
johnAlexander 4:9fad37a914cd 24 #include "XNucleo53L3A2.h"
charlesmn 1:9ea3593bac93 25 #include "vl53L3_I2c.h"
charlesmn 0:2a67f2b41fd7 26
charlesmn 0:2a67f2b41fd7 27
charlesmn 0:2a67f2b41fd7 28 // define i2c mcu pins
johnAlexander 13:c0ef2ae9e6a7 29 #define I2C_SDA D14
johnAlexander 13:c0ef2ae9e6a7 30 #define I2C_SCL D15
charlesmn 0:2a67f2b41fd7 31
johnAlexander 4:9fad37a914cd 32 static XNucleo53L3A2 *board=NULL;
johnAlexander 8:4f47fb835574 33 #if (MBED_VERSION > 60300)
johnAlexander 13:c0ef2ae9e6a7 34 UnbufferedSerial pc(USBTX, USBRX);
johnAlexander 8:4f47fb835574 35 #else
johnAlexander 13:c0ef2ae9e6a7 36 Serial pc(SERIAL_TX, SERIAL_RX);
johnAlexander 8:4f47fb835574 37 #endif
charlesmn 0:2a67f2b41fd7 38
charlesmn 0:2a67f2b41fd7 39
charlesmn 0:2a67f2b41fd7 40
charlesmn 0:2a67f2b41fd7 41 VL53LX_Dev_t devCentre;
charlesmn 0:2a67f2b41fd7 42 VL53LX_Dev_t devLeft;
charlesmn 0:2a67f2b41fd7 43 VL53LX_Dev_t devRight;
johnAlexander 3:7826b7dbd1b0 44
charlesmn 0:2a67f2b41fd7 45 VL53LX_DEV Dev = &devCentre;
charlesmn 0:2a67f2b41fd7 46
charlesmn 0:2a67f2b41fd7 47
johnAlexander 13:c0ef2ae9e6a7 48
charlesmn 0:2a67f2b41fd7 49 /*=================================== Main ==================================
charlesmn 0:2a67f2b41fd7 50 =============================================================================*/
charlesmn 0:2a67f2b41fd7 51 int main()
johnAlexander 13:c0ef2ae9e6a7 52 {
charlesmn 0:2a67f2b41fd7 53 int status;
johnAlexander 8:4f47fb835574 54 VL53L3 * Sensor;
charlesmn 0:2a67f2b41fd7 55 uint16_t wordData;
charlesmn 0:2a67f2b41fd7 56
charlesmn 0:2a67f2b41fd7 57 static VL53LX_MultiRangingData_t RangingData;
charlesmn 0:2a67f2b41fd7 58
charlesmn 0:2a67f2b41fd7 59
charlesmn 0:2a67f2b41fd7 60 pc.baud(115200); // baud rate is important as printf statements take a lot of time
charlesmn 0:2a67f2b41fd7 61 printf("Hello world!\r\n");
charlesmn 0:2a67f2b41fd7 62
charlesmn 1:9ea3593bac93 63 vl53L3_DevI2C *dev_I2C = new vl53L3_DevI2C(I2C_SDA, I2C_SCL);
johnAlexander 13:c0ef2ae9e6a7 64
charlesmn 0:2a67f2b41fd7 65 /* creates the 53L1A1 expansion board singleton obj */
johnAlexander 4:9fad37a914cd 66 board = XNucleo53L3A2::instance(dev_I2C, A2, D8, D2);
charlesmn 0:2a67f2b41fd7 67 printf("board created!\r\n");
charlesmn 0:2a67f2b41fd7 68
charlesmn 0:2a67f2b41fd7 69
charlesmn 0:2a67f2b41fd7 70 /* init the 53L1A1 expansion board with default values */
charlesmn 0:2a67f2b41fd7 71 status = board->init_board();
charlesmn 0:2a67f2b41fd7 72 if (status) {
charlesmn 0:2a67f2b41fd7 73 printf("Failed to init board!\r\n");
charlesmn 0:2a67f2b41fd7 74 return 0;
charlesmn 0:2a67f2b41fd7 75 }
johnAlexander 13:c0ef2ae9e6a7 76
johnAlexander 13:c0ef2ae9e6a7 77
charlesmn 0:2a67f2b41fd7 78 printf("board initiated! \n");
johnAlexander 13:c0ef2ae9e6a7 79
charlesmn 0:2a67f2b41fd7 80 printf("configuring centre channel \n");
charlesmn 0:2a67f2b41fd7 81 Dev=&devCentre;
charlesmn 0:2a67f2b41fd7 82 Sensor=board->sensor_centre;
charlesmn 0:2a67f2b41fd7 83 Dev->I2cDevAddr = NEW_SENSOR_CENTRE_ADDRESS;
charlesmn 0:2a67f2b41fd7 84 printf("configured centre channel \n");
johnAlexander 13:c0ef2ae9e6a7 85
johnAlexander 13:c0ef2ae9e6a7 86
johnAlexander 13:c0ef2ae9e6a7 87 // configure the i2c connection
charlesmn 0:2a67f2b41fd7 88 Dev->comms_speed_khz = 400;
charlesmn 0:2a67f2b41fd7 89 Dev->comms_type = 1;
charlesmn 0:2a67f2b41fd7 90
johnAlexander 13:c0ef2ae9e6a7 91 /* Device Initialization and setting */
charlesmn 0:2a67f2b41fd7 92
charlesmn 0:2a67f2b41fd7 93 status = Sensor->VL53LX_DataInit();
johnAlexander 13:c0ef2ae9e6a7 94 uint8_t NewDataReady=0;
johnAlexander 13:c0ef2ae9e6a7 95
charlesmn 0:2a67f2b41fd7 96
johnAlexander 13:c0ef2ae9e6a7 97 status = Sensor->VL53LX_StartMeasurement();
johnAlexander 13:c0ef2ae9e6a7 98 printf("VL53LX_StartMeasurement %d \n",status);
johnAlexander 13:c0ef2ae9e6a7 99
johnAlexander 13:c0ef2ae9e6a7 100 while(1) {
charlesmn 0:2a67f2b41fd7 101 status = Sensor->VL53LX_WaitMeasurementDataReady();
charlesmn 0:2a67f2b41fd7 102
johnAlexander 13:c0ef2ae9e6a7 103 if(!status) {
charlesmn 0:2a67f2b41fd7 104
charlesmn 0:2a67f2b41fd7 105 status = Sensor->VL53LX_GetMultiRangingData( &RangingData);
charlesmn 0:2a67f2b41fd7 106
johnAlexander 13:c0ef2ae9e6a7 107 if ( status == 0) {
charlesmn 0:2a67f2b41fd7 108 int no_of_object_found=RangingData.NumberOfObjectsFound;
johnAlexander 13:c0ef2ae9e6a7 109 if ( no_of_object_found < 10 ) {
johnAlexander 13:c0ef2ae9e6a7 110 for(int j=0; j<no_of_object_found; j++) {
johnAlexander 13:c0ef2ae9e6a7 111 if ((RangingData.RangeData[j].RangeStatus == VL53LX_RANGESTATUS_RANGE_VALID) ||
johnAlexander 13:c0ef2ae9e6a7 112 (RangingData.RangeData[j].RangeStatus == VL53LX_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL)) {
johnAlexander 13:c0ef2ae9e6a7 113 // print data
johnAlexander 13:c0ef2ae9e6a7 114 printf("centre \t object %d \t status=%d, \t D=%5dmm, \t Signal=%2.2f Mcps, \t Ambient=%2.2f Mcps \n",
johnAlexander 13:c0ef2ae9e6a7 115 j,
johnAlexander 13:c0ef2ae9e6a7 116 RangingData.RangeData[j].RangeStatus,
johnAlexander 13:c0ef2ae9e6a7 117 RangingData.RangeData[j].RangeMilliMeter,
johnAlexander 13:c0ef2ae9e6a7 118 (RangingData.RangeData[j].SignalRateRtnMegaCps/65535.0),
johnAlexander 13:c0ef2ae9e6a7 119 (RangingData.RangeData[j].AmbientRateRtnMegaCps/65535.0));
johnAlexander 13:c0ef2ae9e6a7 120 } //if
charlesmn 0:2a67f2b41fd7 121 } //for
johnAlexander 13:c0ef2ae9e6a7 122 } // if ( no_of_object_found < 10 )
johnAlexander 13:c0ef2ae9e6a7 123 } // if status VL53LX_GetMultiRangingData
johnAlexander 13:c0ef2ae9e6a7 124
johnAlexander 13:c0ef2ae9e6a7 125 } // if !status VL53LX_WaitMeasurementDataReady
johnAlexander 13:c0ef2ae9e6a7 126 else {
johnAlexander 13:c0ef2ae9e6a7 127 printf("VL53L1_WaitMeasurementDataReady failed %d \n",status);
johnAlexander 13:c0ef2ae9e6a7 128 }
charlesmn 0:2a67f2b41fd7 129
johnAlexander 13:c0ef2ae9e6a7 130 status = Sensor->VL53LX_ClearInterruptAndStartMeasurement();
johnAlexander 13:c0ef2ae9e6a7 131
charlesmn 0:2a67f2b41fd7 132 } // while(1)
johnAlexander 13:c0ef2ae9e6a7 133
johnAlexander 13:c0ef2ae9e6a7 134 // *** will not reach, after infinite loop.
johnAlexander 13:c0ef2ae9e6a7 135 status = Sensor->VL53LX_StopMeasurement();
johnAlexander 13:c0ef2ae9e6a7 136
charlesmn 0:2a67f2b41fd7 137 }
johnAlexander 8:4f47fb835574 138