VL53L3CX ranging example application, using X-Nucleo-53L3A2 expansion shield and polling, on MbedOS v6.x
Dependencies: X_NUCLEO_53L3A2
main.cpp@10:6145f8f6527c, 2021-04-30 (annotated)
- Committer:
- johnAlexander
- Date:
- Fri Apr 30 09:38:57 2021 +0000
- Revision:
- 10:6145f8f6527c
- Parent:
- 8:4f47fb835574
- Child:
- 11:6d4d6e9b3729
Updated comments, to report correct, default baud rate.
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. |
johnAlexander | 10:6145f8f6527c | 4 | * Measured ranges are ouput on the Serial Port, running at 115200 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; |
johnAlexander | 8:4f47fb835574 | 27 | #if (MBED_VERSION > 60300) |
johnAlexander | 8:4f47fb835574 | 28 | UnbufferedSerial pc(SERIAL_TX, SERIAL_RX); |
johnAlexander | 8:4f47fb835574 | 29 | #else |
johnAlexander | 8:4f47fb835574 | 30 | Serial pc(SERIAL_TX, SERIAL_RX); |
johnAlexander | 8:4f47fb835574 | 31 | #endif |
charlesmn | 0:2a67f2b41fd7 | 32 | |
charlesmn | 0:2a67f2b41fd7 | 33 | |
charlesmn | 0:2a67f2b41fd7 | 34 | |
charlesmn | 0:2a67f2b41fd7 | 35 | VL53LX_Dev_t devCentre; |
charlesmn | 0:2a67f2b41fd7 | 36 | VL53LX_Dev_t devLeft; |
charlesmn | 0:2a67f2b41fd7 | 37 | VL53LX_Dev_t devRight; |
johnAlexander | 3:7826b7dbd1b0 | 38 | |
charlesmn | 0:2a67f2b41fd7 | 39 | VL53LX_DEV Dev = &devCentre; |
charlesmn | 0:2a67f2b41fd7 | 40 | |
charlesmn | 0:2a67f2b41fd7 | 41 | |
charlesmn | 0:2a67f2b41fd7 | 42 | |
charlesmn | 0:2a67f2b41fd7 | 43 | /*=================================== Main ================================== |
charlesmn | 0:2a67f2b41fd7 | 44 | =============================================================================*/ |
charlesmn | 0:2a67f2b41fd7 | 45 | int main() |
charlesmn | 0:2a67f2b41fd7 | 46 | { |
charlesmn | 0:2a67f2b41fd7 | 47 | int status; |
johnAlexander | 8:4f47fb835574 | 48 | VL53L3 * Sensor; |
charlesmn | 0:2a67f2b41fd7 | 49 | uint16_t wordData; |
charlesmn | 0:2a67f2b41fd7 | 50 | |
charlesmn | 0:2a67f2b41fd7 | 51 | static VL53LX_MultiRangingData_t RangingData; |
charlesmn | 0:2a67f2b41fd7 | 52 | |
charlesmn | 0:2a67f2b41fd7 | 53 | |
charlesmn | 0:2a67f2b41fd7 | 54 | pc.baud(115200); // baud rate is important as printf statements take a lot of time |
charlesmn | 0:2a67f2b41fd7 | 55 | printf("Hello world!\r\n"); |
charlesmn | 0:2a67f2b41fd7 | 56 | |
charlesmn | 1:9ea3593bac93 | 57 | vl53L3_DevI2C *dev_I2C = new vl53L3_DevI2C(I2C_SDA, I2C_SCL); |
charlesmn | 0:2a67f2b41fd7 | 58 | |
charlesmn | 0:2a67f2b41fd7 | 59 | /* creates the 53L1A1 expansion board singleton obj */ |
johnAlexander | 4:9fad37a914cd | 60 | board = XNucleo53L3A2::instance(dev_I2C, A2, D8, D2); |
charlesmn | 0:2a67f2b41fd7 | 61 | printf("board created!\r\n"); |
charlesmn | 0:2a67f2b41fd7 | 62 | |
charlesmn | 0:2a67f2b41fd7 | 63 | |
charlesmn | 0:2a67f2b41fd7 | 64 | /* init the 53L1A1 expansion board with default values */ |
charlesmn | 0:2a67f2b41fd7 | 65 | status = board->init_board(); |
charlesmn | 0:2a67f2b41fd7 | 66 | if (status) { |
charlesmn | 0:2a67f2b41fd7 | 67 | printf("Failed to init board!\r\n"); |
charlesmn | 0:2a67f2b41fd7 | 68 | return 0; |
charlesmn | 0:2a67f2b41fd7 | 69 | } |
charlesmn | 0:2a67f2b41fd7 | 70 | |
charlesmn | 0:2a67f2b41fd7 | 71 | |
charlesmn | 0:2a67f2b41fd7 | 72 | printf("board initiated! \n"); |
charlesmn | 0:2a67f2b41fd7 | 73 | |
charlesmn | 0:2a67f2b41fd7 | 74 | printf("configuring centre channel \n"); |
charlesmn | 0:2a67f2b41fd7 | 75 | Dev=&devCentre; |
charlesmn | 0:2a67f2b41fd7 | 76 | Sensor=board->sensor_centre; |
charlesmn | 0:2a67f2b41fd7 | 77 | Dev->I2cDevAddr = NEW_SENSOR_CENTRE_ADDRESS; |
charlesmn | 0:2a67f2b41fd7 | 78 | printf("configured centre channel \n"); |
charlesmn | 0:2a67f2b41fd7 | 79 | |
charlesmn | 0:2a67f2b41fd7 | 80 | |
charlesmn | 0:2a67f2b41fd7 | 81 | // configure the i2c connection |
charlesmn | 0:2a67f2b41fd7 | 82 | Dev->comms_speed_khz = 400; |
charlesmn | 0:2a67f2b41fd7 | 83 | Dev->comms_type = 1; |
charlesmn | 0:2a67f2b41fd7 | 84 | |
charlesmn | 0:2a67f2b41fd7 | 85 | /* Device Initialization and setting */ |
charlesmn | 0:2a67f2b41fd7 | 86 | |
charlesmn | 0:2a67f2b41fd7 | 87 | status = Sensor->VL53LX_DataInit(); |
charlesmn | 0:2a67f2b41fd7 | 88 | uint8_t NewDataReady=0; |
charlesmn | 0:2a67f2b41fd7 | 89 | |
charlesmn | 0:2a67f2b41fd7 | 90 | |
charlesmn | 0:2a67f2b41fd7 | 91 | status = Sensor->VL53LX_StartMeasurement(); |
charlesmn | 0:2a67f2b41fd7 | 92 | printf("VL53LX_StartMeasurement %d \n",status); |
charlesmn | 0:2a67f2b41fd7 | 93 | |
charlesmn | 0:2a67f2b41fd7 | 94 | while(1) |
charlesmn | 0:2a67f2b41fd7 | 95 | { |
charlesmn | 0:2a67f2b41fd7 | 96 | status = Sensor->VL53LX_WaitMeasurementDataReady(); |
charlesmn | 0:2a67f2b41fd7 | 97 | |
charlesmn | 0:2a67f2b41fd7 | 98 | if(!status) |
charlesmn | 0:2a67f2b41fd7 | 99 | { |
charlesmn | 0:2a67f2b41fd7 | 100 | |
charlesmn | 0:2a67f2b41fd7 | 101 | status = Sensor->VL53LX_GetMultiRangingData( &RangingData); |
charlesmn | 0:2a67f2b41fd7 | 102 | |
charlesmn | 0:2a67f2b41fd7 | 103 | if ( status == 0) |
charlesmn | 0:2a67f2b41fd7 | 104 | { |
charlesmn | 0:2a67f2b41fd7 | 105 | int no_of_object_found=RangingData.NumberOfObjectsFound; |
charlesmn | 0:2a67f2b41fd7 | 106 | if ( no_of_object_found < 10 ) |
charlesmn | 0:2a67f2b41fd7 | 107 | { |
charlesmn | 0:2a67f2b41fd7 | 108 | for(int j=0;j<no_of_object_found;j++){ |
charlesmn | 0:2a67f2b41fd7 | 109 | if ((RangingData.RangeData[j].RangeStatus == VL53LX_RANGESTATUS_RANGE_VALID) || |
charlesmn | 0:2a67f2b41fd7 | 110 | (RangingData.RangeData[j].RangeStatus == VL53LX_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL)) |
charlesmn | 0:2a67f2b41fd7 | 111 | { // print data |
charlesmn | 0:2a67f2b41fd7 | 112 | 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 | 113 | j, |
charlesmn | 0:2a67f2b41fd7 | 114 | RangingData.RangeData[j].RangeStatus, |
charlesmn | 0:2a67f2b41fd7 | 115 | RangingData.RangeData[j].RangeMilliMeter, |
charlesmn | 0:2a67f2b41fd7 | 116 | RangingData.RangeData[j].SignalRateRtnMegaCps/65536.0, |
charlesmn | 0:2a67f2b41fd7 | 117 | RangingData.RangeData[j].AmbientRateRtnMegaCps/65536.0); |
charlesmn | 0:2a67f2b41fd7 | 118 | } //if |
charlesmn | 0:2a67f2b41fd7 | 119 | } //for |
charlesmn | 0:2a67f2b41fd7 | 120 | } // if ( no_of_object_found < 10 ) |
charlesmn | 0:2a67f2b41fd7 | 121 | } // if status VL53LX_GetMultiRangingData |
charlesmn | 0:2a67f2b41fd7 | 122 | |
charlesmn | 0:2a67f2b41fd7 | 123 | } // if !status VL53LX_WaitMeasurementDataReady |
charlesmn | 0:2a67f2b41fd7 | 124 | else |
charlesmn | 0:2a67f2b41fd7 | 125 | { |
charlesmn | 0:2a67f2b41fd7 | 126 | printf("VL53L1_WaitMeasurementDataReady failed %d \n",status); |
charlesmn | 0:2a67f2b41fd7 | 127 | } |
charlesmn | 0:2a67f2b41fd7 | 128 | |
charlesmn | 0:2a67f2b41fd7 | 129 | status = Sensor->VL53LX_ClearInterruptAndStartMeasurement(); |
charlesmn | 0:2a67f2b41fd7 | 130 | |
charlesmn | 0:2a67f2b41fd7 | 131 | } // while(1) |
charlesmn | 0:2a67f2b41fd7 | 132 | |
charlesmn | 0:2a67f2b41fd7 | 133 | // status = Sensor->VL53LX_StopMeasurement(); |
charlesmn | 0:2a67f2b41fd7 | 134 | |
charlesmn | 0:2a67f2b41fd7 | 135 | } |
johnAlexander | 8:4f47fb835574 | 136 |