Use sensor class

Dependencies:   VL53L3CX_mbed

Committer:
johnAlexander
Date:
Fri Apr 30 15:01:09 2021 +0000
Revision:
3:1f36ecdad3c4
Parent:
2:6cc14a15a98c
Align with MbedOS v6.10

Who changed what in which revision?

UserRevisionLine numberNew contents of line
charlesmn 0:2800b864165a 1 /*
charlesmn 0:2800b864165a 2 * no_shield_single_polling.h
charlesmn 0:2800b864165a 3 * This pogram is a simple sample program which demsonstrates the VL53L3 being used without
charlesmn 0:2800b864165a 4 * a nucleo sheild. The shutdown pins are directly connected to the MCU pins D9,D4 and D3
charlesmn 0:2800b864165a 5 * as defined by this line:
charlesmn 0:2800b864165a 6 * status = board->init_board(D9,D4,D3);
charlesmn 0:2800b864165a 7 * the interrupt pins are defined in
charlesmn 0:2800b864165a 8 * board = NoShield53L3::instance(dev_I2C, A2, D8, D2);
charlesmn 0:2800b864165a 9 *
charlesmn 0:2800b864165a 10 * Measured ranges are ouput on the Serial Port, running at 115200 baud.
charlesmn 0:2800b864165a 11 *
charlesmn 0:2800b864165a 12 * The Reset button can be used to restart the program.
charlesmn 0:2800b864165a 13 */
charlesmn 0:2800b864165a 14
charlesmn 0:2800b864165a 15 #include <stdio.h>
johnAlexander 3:1f36ecdad3c4 16 #include <time.h>
charlesmn 0:2800b864165a 17
charlesmn 0:2800b864165a 18 #include "mbed.h"
johnAlexander 2:6cc14a15a98c 19 //#include "NoShield53L3.h"
johnAlexander 3:1f36ecdad3c4 20 #include "vl53l3_class.h"
charlesmn 1:302ec451d4d5 21 #include "vl53L3_I2c.h"
charlesmn 0:2800b864165a 22
charlesmn 0:2800b864165a 23
charlesmn 0:2800b864165a 24 // define i2c mcu pins
charlesmn 1:302ec451d4d5 25 #define I2C_SDA D14
charlesmn 1:302ec451d4d5 26 #define I2C_SCL D15
charlesmn 0:2800b864165a 27
johnAlexander 2:6cc14a15a98c 28 //static NoShield53L3 *board=NULL;
johnAlexander 3:1f36ecdad3c4 29 static VL53L3 *sensor = NULL;
johnAlexander 3:1f36ecdad3c4 30 #if (MBED_VERSION > 60300)
johnAlexander 3:1f36ecdad3c4 31 UnbufferedSerial pc(USBTX, USBRX);
johnAlexander 3:1f36ecdad3c4 32 #else
johnAlexander 3:1f36ecdad3c4 33 Serial pc(SERIAL_TX, SERIAL_RX);
johnAlexander 3:1f36ecdad3c4 34 #endif
charlesmn 0:2800b864165a 35
charlesmn 0:2800b864165a 36
charlesmn 0:2800b864165a 37
charlesmn 0:2800b864165a 38 VL53LX_Dev_t devCentre;
charlesmn 0:2800b864165a 39 VL53LX_Dev_t devLeft;
charlesmn 0:2800b864165a 40 VL53LX_Dev_t devRight;
charlesmn 0:2800b864165a 41 VL53LX_DEV Dev = &devCentre;
charlesmn 0:2800b864165a 42
charlesmn 0:2800b864165a 43
charlesmn 0:2800b864165a 44
charlesmn 0:2800b864165a 45 /*=================================== Main ==================================
charlesmn 0:2800b864165a 46 =============================================================================*/
charlesmn 0:2800b864165a 47 int main()
charlesmn 0:2800b864165a 48 {
charlesmn 0:2800b864165a 49 int status;
charlesmn 0:2800b864165a 50 uint16_t wordData;
johnAlexander 2:6cc14a15a98c 51
johnAlexander 3:1f36ecdad3c4 52 DigitalOut xshutdown(D8);
johnAlexander 2:6cc14a15a98c 53
charlesmn 0:2800b864165a 54
charlesmn 0:2800b864165a 55 static VL53LX_MultiRangingData_t RangingData;
charlesmn 0:2800b864165a 56
charlesmn 0:2800b864165a 57
charlesmn 0:2800b864165a 58 pc.baud(115200); // baud rate is important as printf statements take a lot of time
charlesmn 0:2800b864165a 59 printf("Hello world!\r\n");
charlesmn 0:2800b864165a 60
charlesmn 1:302ec451d4d5 61 vl53L3_DevI2C *dev_I2C = new vl53L3_DevI2C(I2C_SDA, I2C_SCL);
charlesmn 0:2800b864165a 62
charlesmn 0:2800b864165a 63 /* no expansion board so don't use stmpe1600 */
johnAlexander 2:6cc14a15a98c 64 // board = NoShield53L3::instance(dev_I2C, A2, D8, D2);
johnAlexander 3:1f36ecdad3c4 65 sensor = new VL53L3(dev_I2C, &xshutdown, A2);
johnAlexander 2:6cc14a15a98c 66
johnAlexander 2:6cc14a15a98c 67 printf("sensor created!\r\n");
charlesmn 0:2800b864165a 68
johnAlexander 2:6cc14a15a98c 69 status = sensor->InitSensor(0x52);
charlesmn 0:2800b864165a 70 if (status) {
johnAlexander 2:6cc14a15a98c 71 printf("Failed to init sensor!\r\n");
charlesmn 0:2800b864165a 72 return 0;
johnAlexander 2:6cc14a15a98c 73 }
johnAlexander 2:6cc14a15a98c 74
johnAlexander 2:6cc14a15a98c 75 printf("sensor initialised! \n");
charlesmn 0:2800b864165a 76
charlesmn 0:2800b864165a 77 uint8_t NewDataReady=0;
charlesmn 0:2800b864165a 78
johnAlexander 2:6cc14a15a98c 79 status = sensor->VL53LX_StartMeasurement();
charlesmn 0:2800b864165a 80 printf("VL53LX_StartMeasurement %d \n",status);
charlesmn 0:2800b864165a 81
charlesmn 0:2800b864165a 82 // loop forever getting measurements
charlesmn 0:2800b864165a 83 while(1)
charlesmn 0:2800b864165a 84 {
johnAlexander 2:6cc14a15a98c 85 status = sensor->VL53LX_WaitMeasurementDataReady();
charlesmn 0:2800b864165a 86
charlesmn 0:2800b864165a 87 if(!status)
charlesmn 0:2800b864165a 88 {
charlesmn 0:2800b864165a 89
johnAlexander 2:6cc14a15a98c 90 status = sensor->VL53LX_GetMultiRangingData(&RangingData);
charlesmn 0:2800b864165a 91
johnAlexander 2:6cc14a15a98c 92 if (status == 0)
charlesmn 0:2800b864165a 93 {
johnAlexander 2:6cc14a15a98c 94 int no_of_object_found = RangingData.NumberOfObjectsFound;
charlesmn 0:2800b864165a 95 if ( no_of_object_found < 10 )
charlesmn 0:2800b864165a 96 {
johnAlexander 2:6cc14a15a98c 97 for(int j=0;j<no_of_object_found;j++)
johnAlexander 2:6cc14a15a98c 98 {
charlesmn 0:2800b864165a 99 if ((RangingData.RangeData[j].RangeStatus == VL53LX_RANGESTATUS_RANGE_VALID) ||
charlesmn 0:2800b864165a 100 (RangingData.RangeData[j].RangeStatus == VL53LX_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL))
johnAlexander 2:6cc14a15a98c 101 { // print data
johnAlexander 2:6cc14a15a98c 102 printf("centre \t object %d \t status=%d, \t D=%5dmm, \t Signal=%2.2f Mcps, \t Ambient=%2.2f Mcps \n",
charlesmn 0:2800b864165a 103 j,
charlesmn 0:2800b864165a 104 RangingData.RangeData[j].RangeStatus,
charlesmn 0:2800b864165a 105 RangingData.RangeData[j].RangeMilliMeter,
charlesmn 0:2800b864165a 106 RangingData.RangeData[j].SignalRateRtnMegaCps/65536.0,
charlesmn 0:2800b864165a 107 RangingData.RangeData[j].AmbientRateRtnMegaCps/65536.0);
johnAlexander 2:6cc14a15a98c 108 } //if
charlesmn 0:2800b864165a 109 } //for
charlesmn 0:2800b864165a 110 } // if ( no_of_object_found < 10 )
johnAlexander 2:6cc14a15a98c 111 } // if status VL53LX_GetMultiRangingData
johnAlexander 2:6cc14a15a98c 112 } // if !status VL53LX_WaitMeasurementDataReady
johnAlexander 2:6cc14a15a98c 113 else
johnAlexander 2:6cc14a15a98c 114 {
johnAlexander 2:6cc14a15a98c 115 printf("VL53L1_WaitMeasurementDataReady failed %d \n",status);
johnAlexander 2:6cc14a15a98c 116 }
charlesmn 0:2800b864165a 117
johnAlexander 2:6cc14a15a98c 118 status = sensor->VL53LX_ClearInterruptAndStartMeasurement();
charlesmn 0:2800b864165a 119
charlesmn 0:2800b864165a 120 } // while(1)
charlesmn 0:2800b864165a 121
charlesmn 0:2800b864165a 122 }