A simple example of a ST VL53L3 ToF sensor being used to get range data polling for measurements. Mbed V5.15

Dependencies:   X_NUCLEO_53L3A2

Committer:
charlesmn
Date:
Mon Oct 19 09:19:26 2020 +0000
Revision:
1:9ea3593bac93
Parent:
0:2a67f2b41fd7
Child:
2:f57af818d5ac
only name changes

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 1:9ea3593bac93 17 #include "XNucleo53LX.h"
charlesmn 1:9ea3593bac93 18 #include "vl53L3_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 1:9ea3593bac93 23 #define I2C_SDA D14
charlesmn 1:9ea3593bac93 24 #define I2C_SCL D15
charlesmn 0:2a67f2b41fd7 25
charlesmn 1:9ea3593bac93 26 static XNucleo53LX *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 1:9ea3593bac93 52 vl53L3_DevI2C *dev_I2C = new vl53L3_DevI2C(I2C_SDA, I2C_SCL);
charlesmn 0:2a67f2b41fd7 53
charlesmn 0:2a67f2b41fd7 54 /* creates the 53L1A1 expansion board singleton obj */
charlesmn 1:9ea3593bac93 55 board = XNucleo53LX::instance(dev_I2C, A2, D8, D2);
charlesmn 0:2a67f2b41fd7 56 printf("board created!\r\n");
charlesmn 0:2a67f2b41fd7 57
charlesmn 0:2a67f2b41fd7 58
charlesmn 0:2a67f2b41fd7 59 /* init the 53L1A1 expansion board with default values */
charlesmn 0:2a67f2b41fd7 60 status = board->init_board();
charlesmn 0:2a67f2b41fd7 61 if (status) {
charlesmn 0:2a67f2b41fd7 62 printf("Failed to init board!\r\n");
charlesmn 0:2a67f2b41fd7 63 return 0;
charlesmn 0:2a67f2b41fd7 64 }
charlesmn 0:2a67f2b41fd7 65
charlesmn 0:2a67f2b41fd7 66
charlesmn 0:2a67f2b41fd7 67 printf("board initiated! \n");
charlesmn 0:2a67f2b41fd7 68
charlesmn 0:2a67f2b41fd7 69 printf("configuring centre channel \n");
charlesmn 0:2a67f2b41fd7 70 Dev=&devCentre;
charlesmn 0:2a67f2b41fd7 71 Sensor=board->sensor_centre;
charlesmn 0:2a67f2b41fd7 72 Dev->I2cDevAddr = NEW_SENSOR_CENTRE_ADDRESS;
charlesmn 0:2a67f2b41fd7 73 printf("configured centre channel \n");
charlesmn 0:2a67f2b41fd7 74
charlesmn 0:2a67f2b41fd7 75
charlesmn 0:2a67f2b41fd7 76 // configure the i2c connection
charlesmn 0:2a67f2b41fd7 77 Dev->comms_speed_khz = 400;
charlesmn 0:2a67f2b41fd7 78 Dev->comms_type = 1;
charlesmn 0:2a67f2b41fd7 79
charlesmn 0:2a67f2b41fd7 80 /* Device Initialization and setting */
charlesmn 0:2a67f2b41fd7 81
charlesmn 0:2a67f2b41fd7 82 status = Sensor->VL53LX_DataInit();
charlesmn 0:2a67f2b41fd7 83 uint8_t NewDataReady=0;
charlesmn 0:2a67f2b41fd7 84
charlesmn 0:2a67f2b41fd7 85
charlesmn 0:2a67f2b41fd7 86 status = Sensor->VL53LX_StartMeasurement();
charlesmn 0:2a67f2b41fd7 87 printf("VL53LX_StartMeasurement %d \n",status);
charlesmn 0:2a67f2b41fd7 88
charlesmn 0:2a67f2b41fd7 89 while(1)
charlesmn 0:2a67f2b41fd7 90 {
charlesmn 0:2a67f2b41fd7 91 status = Sensor->VL53LX_WaitMeasurementDataReady();
charlesmn 0:2a67f2b41fd7 92
charlesmn 0:2a67f2b41fd7 93 if(!status)
charlesmn 0:2a67f2b41fd7 94 {
charlesmn 0:2a67f2b41fd7 95
charlesmn 0:2a67f2b41fd7 96 status = Sensor->VL53LX_GetMultiRangingData( &RangingData);
charlesmn 0:2a67f2b41fd7 97
charlesmn 0:2a67f2b41fd7 98 if ( status == 0)
charlesmn 0:2a67f2b41fd7 99 {
charlesmn 0:2a67f2b41fd7 100 int no_of_object_found=RangingData.NumberOfObjectsFound;
charlesmn 0:2a67f2b41fd7 101 if ( no_of_object_found < 10 )
charlesmn 0:2a67f2b41fd7 102 {
charlesmn 0:2a67f2b41fd7 103 for(int j=0;j<no_of_object_found;j++){
charlesmn 0:2a67f2b41fd7 104 if ((RangingData.RangeData[j].RangeStatus == VL53LX_RANGESTATUS_RANGE_VALID) ||
charlesmn 0:2a67f2b41fd7 105 (RangingData.RangeData[j].RangeStatus == VL53LX_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL))
charlesmn 0:2a67f2b41fd7 106 { // print data
charlesmn 0:2a67f2b41fd7 107 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 108 j,
charlesmn 0:2a67f2b41fd7 109 RangingData.RangeData[j].RangeStatus,
charlesmn 0:2a67f2b41fd7 110 RangingData.RangeData[j].RangeMilliMeter,
charlesmn 0:2a67f2b41fd7 111 RangingData.RangeData[j].SignalRateRtnMegaCps/65536.0,
charlesmn 0:2a67f2b41fd7 112 RangingData.RangeData[j].AmbientRateRtnMegaCps/65536.0);
charlesmn 0:2a67f2b41fd7 113 } //if
charlesmn 0:2a67f2b41fd7 114 } //for
charlesmn 0:2a67f2b41fd7 115 } // if ( no_of_object_found < 10 )
charlesmn 0:2a67f2b41fd7 116 } // if status VL53LX_GetMultiRangingData
charlesmn 0:2a67f2b41fd7 117
charlesmn 0:2a67f2b41fd7 118 } // if !status VL53LX_WaitMeasurementDataReady
charlesmn 0:2a67f2b41fd7 119 else
charlesmn 0:2a67f2b41fd7 120 {
charlesmn 0:2a67f2b41fd7 121 printf("VL53L1_WaitMeasurementDataReady failed %d \n",status);
charlesmn 0:2a67f2b41fd7 122 }
charlesmn 0:2a67f2b41fd7 123
charlesmn 0:2a67f2b41fd7 124 status = Sensor->VL53LX_ClearInterruptAndStartMeasurement();
charlesmn 0:2a67f2b41fd7 125
charlesmn 0:2a67f2b41fd7 126 } // while(1)
charlesmn 0:2a67f2b41fd7 127
charlesmn 0:2a67f2b41fd7 128 // status = Sensor->VL53LX_StopMeasurement();
charlesmn 0:2a67f2b41fd7 129
charlesmn 0:2a67f2b41fd7 130 }