VL53L3CX ranging example application, using X-Nucleo-53L3A2 expansion shield and polling, on MbedOS v6.x
Dependencies: X_NUCLEO_53L3A2
Main.cpp@4:9fad37a914cd, 2020-11-04 (annotated)
- Committer:
- johnAlexander
- Date:
- Wed Nov 04 10:03:01 2020 +0000
- Revision:
- 4:9fad37a914cd
- Parent:
- 3:7826b7dbd1b0
Renamed board lib
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
charlesmn | 0:2a67f2b41fd7 | 1 | /* |
johnAlexander | 4:9fad37a914cd | 2 | * This VL53L3 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> |
johnAlexander | 3:7826b7dbd1b0 | 15 | #include <time.h> |
charlesmn | 0:2a67f2b41fd7 | 16 | |
charlesmn | 0:2a67f2b41fd7 | 17 | #include "mbed.h" |
johnAlexander | 4:9fad37a914cd | 18 | #include "XNucleo53L3A2.h" |
charlesmn | 1:9ea3593bac93 | 19 | #include "vl53L3_I2c.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 | |
johnAlexander | 4:9fad37a914cd | 26 | static XNucleo53L3A2 *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; |
johnAlexander | 3:7826b7dbd1b0 | 34 | |
charlesmn | 0:2a67f2b41fd7 | 35 | VL53LX_DEV Dev = &devCentre; |
charlesmn | 0:2a67f2b41fd7 | 36 | |
charlesmn | 0:2a67f2b41fd7 | 37 | |
charlesmn | 0:2a67f2b41fd7 | 38 | |
charlesmn | 0:2a67f2b41fd7 | 39 | /*=================================== Main ================================== |
charlesmn | 0:2a67f2b41fd7 | 40 | =============================================================================*/ |
charlesmn | 0:2a67f2b41fd7 | 41 | int main() |
charlesmn | 0:2a67f2b41fd7 | 42 | { |
charlesmn | 0:2a67f2b41fd7 | 43 | int status; |
charlesmn | 0:2a67f2b41fd7 | 44 | VL53LX * Sensor; |
charlesmn | 0:2a67f2b41fd7 | 45 | uint16_t wordData; |
charlesmn | 0:2a67f2b41fd7 | 46 | |
charlesmn | 0:2a67f2b41fd7 | 47 | static VL53LX_MultiRangingData_t RangingData; |
charlesmn | 0:2a67f2b41fd7 | 48 | |
charlesmn | 0:2a67f2b41fd7 | 49 | |
charlesmn | 0:2a67f2b41fd7 | 50 | pc.baud(115200); // baud rate is important as printf statements take a lot of time |
charlesmn | 0:2a67f2b41fd7 | 51 | printf("Hello world!\r\n"); |
charlesmn | 0:2a67f2b41fd7 | 52 | |
charlesmn | 1:9ea3593bac93 | 53 | vl53L3_DevI2C *dev_I2C = new vl53L3_DevI2C(I2C_SDA, I2C_SCL); |
charlesmn | 0:2a67f2b41fd7 | 54 | |
charlesmn | 0:2a67f2b41fd7 | 55 | /* creates the 53L1A1 expansion board singleton obj */ |
johnAlexander | 4:9fad37a914cd | 56 | board = XNucleo53L3A2::instance(dev_I2C, A2, D8, D2); |
charlesmn | 0:2a67f2b41fd7 | 57 | printf("board created!\r\n"); |
charlesmn | 0:2a67f2b41fd7 | 58 | |
charlesmn | 0:2a67f2b41fd7 | 59 | |
charlesmn | 0:2a67f2b41fd7 | 60 | /* init the 53L1A1 expansion board with default values */ |
charlesmn | 0:2a67f2b41fd7 | 61 | status = board->init_board(); |
charlesmn | 0:2a67f2b41fd7 | 62 | if (status) { |
charlesmn | 0:2a67f2b41fd7 | 63 | printf("Failed to init board!\r\n"); |
charlesmn | 0:2a67f2b41fd7 | 64 | return 0; |
charlesmn | 0:2a67f2b41fd7 | 65 | } |
charlesmn | 0:2a67f2b41fd7 | 66 | |
charlesmn | 0:2a67f2b41fd7 | 67 | |
charlesmn | 0:2a67f2b41fd7 | 68 | printf("board initiated! \n"); |
charlesmn | 0:2a67f2b41fd7 | 69 | |
charlesmn | 0:2a67f2b41fd7 | 70 | printf("configuring centre channel \n"); |
charlesmn | 0:2a67f2b41fd7 | 71 | Dev=&devCentre; |
charlesmn | 0:2a67f2b41fd7 | 72 | Sensor=board->sensor_centre; |
charlesmn | 0:2a67f2b41fd7 | 73 | Dev->I2cDevAddr = NEW_SENSOR_CENTRE_ADDRESS; |
charlesmn | 0:2a67f2b41fd7 | 74 | printf("configured centre channel \n"); |
charlesmn | 0:2a67f2b41fd7 | 75 | |
charlesmn | 0:2a67f2b41fd7 | 76 | |
charlesmn | 0:2a67f2b41fd7 | 77 | // configure the i2c connection |
charlesmn | 0:2a67f2b41fd7 | 78 | Dev->comms_speed_khz = 400; |
charlesmn | 0:2a67f2b41fd7 | 79 | Dev->comms_type = 1; |
charlesmn | 0:2a67f2b41fd7 | 80 | |
charlesmn | 0:2a67f2b41fd7 | 81 | /* Device Initialization and setting */ |
charlesmn | 0:2a67f2b41fd7 | 82 | |
charlesmn | 0:2a67f2b41fd7 | 83 | status = Sensor->VL53LX_DataInit(); |
charlesmn | 0:2a67f2b41fd7 | 84 | uint8_t NewDataReady=0; |
charlesmn | 0:2a67f2b41fd7 | 85 | |
charlesmn | 0:2a67f2b41fd7 | 86 | |
charlesmn | 0:2a67f2b41fd7 | 87 | status = Sensor->VL53LX_StartMeasurement(); |
charlesmn | 0:2a67f2b41fd7 | 88 | printf("VL53LX_StartMeasurement %d \n",status); |
charlesmn | 0:2a67f2b41fd7 | 89 | |
charlesmn | 0:2a67f2b41fd7 | 90 | while(1) |
charlesmn | 0:2a67f2b41fd7 | 91 | { |
charlesmn | 0:2a67f2b41fd7 | 92 | status = Sensor->VL53LX_WaitMeasurementDataReady(); |
charlesmn | 0:2a67f2b41fd7 | 93 | |
charlesmn | 0:2a67f2b41fd7 | 94 | if(!status) |
charlesmn | 0:2a67f2b41fd7 | 95 | { |
charlesmn | 0:2a67f2b41fd7 | 96 | |
charlesmn | 0:2a67f2b41fd7 | 97 | status = Sensor->VL53LX_GetMultiRangingData( &RangingData); |
charlesmn | 0:2a67f2b41fd7 | 98 | |
charlesmn | 0:2a67f2b41fd7 | 99 | if ( status == 0) |
charlesmn | 0:2a67f2b41fd7 | 100 | { |
charlesmn | 0:2a67f2b41fd7 | 101 | int no_of_object_found=RangingData.NumberOfObjectsFound; |
charlesmn | 0:2a67f2b41fd7 | 102 | if ( no_of_object_found < 10 ) |
charlesmn | 0:2a67f2b41fd7 | 103 | { |
charlesmn | 0:2a67f2b41fd7 | 104 | for(int j=0;j<no_of_object_found;j++){ |
charlesmn | 0:2a67f2b41fd7 | 105 | if ((RangingData.RangeData[j].RangeStatus == VL53LX_RANGESTATUS_RANGE_VALID) || |
charlesmn | 0:2a67f2b41fd7 | 106 | (RangingData.RangeData[j].RangeStatus == VL53LX_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL)) |
charlesmn | 0:2a67f2b41fd7 | 107 | { // print data |
charlesmn | 0:2a67f2b41fd7 | 108 | 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 | 109 | j, |
charlesmn | 0:2a67f2b41fd7 | 110 | RangingData.RangeData[j].RangeStatus, |
charlesmn | 0:2a67f2b41fd7 | 111 | RangingData.RangeData[j].RangeMilliMeter, |
charlesmn | 0:2a67f2b41fd7 | 112 | RangingData.RangeData[j].SignalRateRtnMegaCps/65536.0, |
charlesmn | 0:2a67f2b41fd7 | 113 | RangingData.RangeData[j].AmbientRateRtnMegaCps/65536.0); |
charlesmn | 0:2a67f2b41fd7 | 114 | } //if |
charlesmn | 0:2a67f2b41fd7 | 115 | } //for |
charlesmn | 0:2a67f2b41fd7 | 116 | } // if ( no_of_object_found < 10 ) |
charlesmn | 0:2a67f2b41fd7 | 117 | } // if status VL53LX_GetMultiRangingData |
charlesmn | 0:2a67f2b41fd7 | 118 | |
charlesmn | 0:2a67f2b41fd7 | 119 | } // if !status VL53LX_WaitMeasurementDataReady |
charlesmn | 0:2a67f2b41fd7 | 120 | else |
charlesmn | 0:2a67f2b41fd7 | 121 | { |
charlesmn | 0:2a67f2b41fd7 | 122 | printf("VL53L1_WaitMeasurementDataReady failed %d \n",status); |
charlesmn | 0:2a67f2b41fd7 | 123 | } |
charlesmn | 0:2a67f2b41fd7 | 124 | |
charlesmn | 0:2a67f2b41fd7 | 125 | status = Sensor->VL53LX_ClearInterruptAndStartMeasurement(); |
charlesmn | 0:2a67f2b41fd7 | 126 | |
charlesmn | 0:2a67f2b41fd7 | 127 | } // while(1) |
charlesmn | 0:2a67f2b41fd7 | 128 | |
charlesmn | 0:2a67f2b41fd7 | 129 | // status = Sensor->VL53LX_StopMeasurement(); |
charlesmn | 0:2a67f2b41fd7 | 130 | |
charlesmn | 0:2a67f2b41fd7 | 131 | } |