John Alexander
/
VL53L3_NoShield_1Sensor_polling_Mb63
Use sensor class
Main.cpp@0:2800b864165a, 2020-10-14 (annotated)
- Committer:
- charlesmn
- Date:
- Wed Oct 14 07:49:01 2020 +0000
- Revision:
- 0:2800b864165a
- Child:
- 1:302ec451d4d5
Simple sample program to control a VL53L3 directly from the MCU without using a Nucleo shield. MBedV6.3.
Who changed what in which revision?
User | Revision | Line number | New 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> |
charlesmn | 0:2800b864165a | 16 | |
charlesmn | 0:2800b864165a | 17 | #include "mbed.h" |
charlesmn | 0:2800b864165a | 18 | #include "NoShield53L3.h" |
charlesmn | 0:2800b864165a | 19 | #include "vl53L1x_I2c.h" |
charlesmn | 0:2800b864165a | 20 | #include <time.h> |
charlesmn | 0:2800b864165a | 21 | |
charlesmn | 0:2800b864165a | 22 | |
charlesmn | 0:2800b864165a | 23 | // define i2c mcu pins |
charlesmn | 0:2800b864165a | 24 | #define VL53L1_I2C_SDA D14 |
charlesmn | 0:2800b864165a | 25 | #define VL53L1_I2C_SCL D15 |
charlesmn | 0:2800b864165a | 26 | |
charlesmn | 0:2800b864165a | 27 | static NoShield53L3 *board=NULL; |
charlesmn | 0:2800b864165a | 28 | Serial pc(SERIAL_TX, SERIAL_RX); |
charlesmn | 0:2800b864165a | 29 | |
charlesmn | 0:2800b864165a | 30 | |
charlesmn | 0:2800b864165a | 31 | |
charlesmn | 0:2800b864165a | 32 | VL53LX_Dev_t devCentre; |
charlesmn | 0:2800b864165a | 33 | VL53LX_Dev_t devLeft; |
charlesmn | 0:2800b864165a | 34 | VL53LX_Dev_t devRight; |
charlesmn | 0:2800b864165a | 35 | VL53LX_DEV Dev = &devCentre; |
charlesmn | 0:2800b864165a | 36 | |
charlesmn | 0:2800b864165a | 37 | |
charlesmn | 0:2800b864165a | 38 | |
charlesmn | 0:2800b864165a | 39 | /*=================================== Main ================================== |
charlesmn | 0:2800b864165a | 40 | =============================================================================*/ |
charlesmn | 0:2800b864165a | 41 | int main() |
charlesmn | 0:2800b864165a | 42 | { |
charlesmn | 0:2800b864165a | 43 | int status; |
charlesmn | 0:2800b864165a | 44 | VL53LX * Sensor; |
charlesmn | 0:2800b864165a | 45 | uint16_t wordData; |
charlesmn | 0:2800b864165a | 46 | |
charlesmn | 0:2800b864165a | 47 | static VL53LX_MultiRangingData_t RangingData; |
charlesmn | 0:2800b864165a | 48 | |
charlesmn | 0:2800b864165a | 49 | |
charlesmn | 0:2800b864165a | 50 | pc.baud(115200); // baud rate is important as printf statements take a lot of time |
charlesmn | 0:2800b864165a | 51 | printf("Hello world!\r\n"); |
charlesmn | 0:2800b864165a | 52 | |
charlesmn | 0:2800b864165a | 53 | vl53L1X_DevI2C *dev_I2C = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL); |
charlesmn | 0:2800b864165a | 54 | |
charlesmn | 0:2800b864165a | 55 | /* no expansion board so don't use stmpe1600 */ |
charlesmn | 0:2800b864165a | 56 | board = NoShield53L3::instance(dev_I2C, A2, D8, D2); |
charlesmn | 0:2800b864165a | 57 | printf("board created!\r\n"); |
charlesmn | 0:2800b864165a | 58 | |
charlesmn | 0:2800b864165a | 59 | |
charlesmn | 0:2800b864165a | 60 | /* define the shutdown pins */ |
charlesmn | 0:2800b864165a | 61 | status = board->init_board(D9,D4,D3); |
charlesmn | 0:2800b864165a | 62 | if (status) { |
charlesmn | 0:2800b864165a | 63 | printf("Failed to init board!\r\n"); |
charlesmn | 0:2800b864165a | 64 | return 0; |
charlesmn | 0:2800b864165a | 65 | } |
charlesmn | 0:2800b864165a | 66 | |
charlesmn | 0:2800b864165a | 67 | |
charlesmn | 0:2800b864165a | 68 | printf("board initiated! \n"); |
charlesmn | 0:2800b864165a | 69 | |
charlesmn | 0:2800b864165a | 70 | printf("configuring centre channel \n"); |
charlesmn | 0:2800b864165a | 71 | Dev=&devCentre; |
charlesmn | 0:2800b864165a | 72 | Sensor=board->sensor_centre; |
charlesmn | 0:2800b864165a | 73 | Dev->I2cDevAddr = NEW_SENSOR_CENTRE_ADDRESS; |
charlesmn | 0:2800b864165a | 74 | printf("configured centre channel \n"); |
charlesmn | 0:2800b864165a | 75 | |
charlesmn | 0:2800b864165a | 76 | |
charlesmn | 0:2800b864165a | 77 | // configure the i2c connection |
charlesmn | 0:2800b864165a | 78 | Dev->comms_speed_khz = 400; |
charlesmn | 0:2800b864165a | 79 | Dev->comms_type = 1; |
charlesmn | 0:2800b864165a | 80 | |
charlesmn | 0:2800b864165a | 81 | /* Device Initialization and setting */ |
charlesmn | 0:2800b864165a | 82 | printf("VL53LX_DataInit \n"); |
charlesmn | 0:2800b864165a | 83 | status = Sensor->VL53LX_DataInit(); |
charlesmn | 0:2800b864165a | 84 | uint8_t NewDataReady=0; |
charlesmn | 0:2800b864165a | 85 | printf("VL53LX_DataInit %d \n",status); |
charlesmn | 0:2800b864165a | 86 | |
charlesmn | 0:2800b864165a | 87 | status = Sensor->VL53LX_StartMeasurement(); |
charlesmn | 0:2800b864165a | 88 | printf("VL53LX_StartMeasurement %d \n",status); |
charlesmn | 0:2800b864165a | 89 | |
charlesmn | 0:2800b864165a | 90 | // loop forever getting measurements |
charlesmn | 0:2800b864165a | 91 | while(1) |
charlesmn | 0:2800b864165a | 92 | { |
charlesmn | 0:2800b864165a | 93 | status = Sensor->VL53LX_WaitMeasurementDataReady(); |
charlesmn | 0:2800b864165a | 94 | |
charlesmn | 0:2800b864165a | 95 | if(!status) |
charlesmn | 0:2800b864165a | 96 | { |
charlesmn | 0:2800b864165a | 97 | |
charlesmn | 0:2800b864165a | 98 | status = Sensor->VL53LX_GetMultiRangingData( &RangingData); |
charlesmn | 0:2800b864165a | 99 | |
charlesmn | 0:2800b864165a | 100 | if ( status == 0) |
charlesmn | 0:2800b864165a | 101 | { |
charlesmn | 0:2800b864165a | 102 | int no_of_object_found=RangingData.NumberOfObjectsFound; |
charlesmn | 0:2800b864165a | 103 | if ( no_of_object_found < 10 ) |
charlesmn | 0:2800b864165a | 104 | { |
charlesmn | 0:2800b864165a | 105 | for(int j=0;j<no_of_object_found;j++){ |
charlesmn | 0:2800b864165a | 106 | if ((RangingData.RangeData[j].RangeStatus == VL53LX_RANGESTATUS_RANGE_VALID) || |
charlesmn | 0:2800b864165a | 107 | (RangingData.RangeData[j].RangeStatus == VL53LX_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL)) |
charlesmn | 0:2800b864165a | 108 | { // print data |
charlesmn | 0:2800b864165a | 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:2800b864165a | 110 | j, |
charlesmn | 0:2800b864165a | 111 | RangingData.RangeData[j].RangeStatus, |
charlesmn | 0:2800b864165a | 112 | RangingData.RangeData[j].RangeMilliMeter, |
charlesmn | 0:2800b864165a | 113 | RangingData.RangeData[j].SignalRateRtnMegaCps/65536.0, |
charlesmn | 0:2800b864165a | 114 | RangingData.RangeData[j].AmbientRateRtnMegaCps/65536.0); |
charlesmn | 0:2800b864165a | 115 | } //if |
charlesmn | 0:2800b864165a | 116 | } //for |
charlesmn | 0:2800b864165a | 117 | } // if ( no_of_object_found < 10 ) |
charlesmn | 0:2800b864165a | 118 | } // if status VL53LX_GetMultiRangingData |
charlesmn | 0:2800b864165a | 119 | |
charlesmn | 0:2800b864165a | 120 | } // if !status VL53LX_WaitMeasurementDataReady |
charlesmn | 0:2800b864165a | 121 | else |
charlesmn | 0:2800b864165a | 122 | { |
charlesmn | 0:2800b864165a | 123 | printf("VL53L1_WaitMeasurementDataReady failed %d \n",status); |
charlesmn | 0:2800b864165a | 124 | } |
charlesmn | 0:2800b864165a | 125 | |
charlesmn | 0:2800b864165a | 126 | status = Sensor->VL53LX_ClearInterruptAndStartMeasurement(); |
charlesmn | 0:2800b864165a | 127 | |
charlesmn | 0:2800b864165a | 128 | } // while(1) |
charlesmn | 0:2800b864165a | 129 | |
charlesmn | 0:2800b864165a | 130 | // status = Sensor->VL53LX_StopMeasurement(); |
charlesmn | 0:2800b864165a | 131 | |
charlesmn | 0:2800b864165a | 132 | } |