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

Dependencies:   X_NUCLEO_53L3A2

Committer:
charlesmn
Date:
Thu Oct 15 12:14:48 2020 +0000
Revision:
0:2a67f2b41fd7
Child:
1:9ea3593bac93
A simple example of a ST VL53L3 ToF sensor being used to get range data polling for measurements. Mbed V5.15

Who changed what in which revision?

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