Trial project, for use with VL53L3 board and sensor libs, and mbed-cli tools.
Dependencies: 53L13ExpansionBoard
main.cpp@0:3adb6e4b8113, 2020-10-29 (annotated)
- Committer:
- johnAlexander
- Date:
- Thu Oct 29 14:29:27 2020 +0000
- Revision:
- 0:3adb6e4b8113
trial project, for use offline, with mbed-cli import
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
johnAlexander | 0:3adb6e4b8113 | 1 | /* |
johnAlexander | 0:3adb6e4b8113 | 2 | * This VL53L1X Expansion board test application performs range measurements |
johnAlexander | 0:3adb6e4b8113 | 3 | * using the onboard embedded centre sensor, in singleshot, polling mode. |
johnAlexander | 0:3adb6e4b8113 | 4 | * Measured ranges are ouput on the Serial Port, running at 9600 baud. |
johnAlexander | 0:3adb6e4b8113 | 5 | * |
johnAlexander | 0:3adb6e4b8113 | 6 | * The User Blue button stops the current measurement and entire program, |
johnAlexander | 0:3adb6e4b8113 | 7 | * releasing all resources. |
johnAlexander | 0:3adb6e4b8113 | 8 | * |
johnAlexander | 0:3adb6e4b8113 | 9 | * The Reset button can be used to restart the program. |
johnAlexander | 0:3adb6e4b8113 | 10 | */ |
johnAlexander | 0:3adb6e4b8113 | 11 | |
johnAlexander | 0:3adb6e4b8113 | 12 | //Main_single_polling.h |
johnAlexander | 0:3adb6e4b8113 | 13 | |
johnAlexander | 0:3adb6e4b8113 | 14 | #include <stdio.h> |
johnAlexander | 0:3adb6e4b8113 | 15 | |
johnAlexander | 0:3adb6e4b8113 | 16 | #include "mbed.h" |
johnAlexander | 0:3adb6e4b8113 | 17 | #include "XNucleo53L1A1.h" |
johnAlexander | 0:3adb6e4b8113 | 18 | #include "vl53L1x_I2c.h" |
johnAlexander | 0:3adb6e4b8113 | 19 | #include <time.h> |
johnAlexander | 0:3adb6e4b8113 | 20 | |
johnAlexander | 0:3adb6e4b8113 | 21 | |
johnAlexander | 0:3adb6e4b8113 | 22 | // define i2c mcu pins |
johnAlexander | 0:3adb6e4b8113 | 23 | #define VL53L1_I2C_SDA D14 |
johnAlexander | 0:3adb6e4b8113 | 24 | #define VL53L1_I2C_SCL D15 |
johnAlexander | 0:3adb6e4b8113 | 25 | |
johnAlexander | 0:3adb6e4b8113 | 26 | static XNucleo53L1A1 *board=NULL; |
johnAlexander | 0:3adb6e4b8113 | 27 | Serial pc(SERIAL_TX, SERIAL_RX); |
johnAlexander | 0:3adb6e4b8113 | 28 | |
johnAlexander | 0:3adb6e4b8113 | 29 | |
johnAlexander | 0:3adb6e4b8113 | 30 | |
johnAlexander | 0:3adb6e4b8113 | 31 | VL53LX_Dev_t devCentre; |
johnAlexander | 0:3adb6e4b8113 | 32 | VL53LX_Dev_t devLeft; |
johnAlexander | 0:3adb6e4b8113 | 33 | VL53LX_Dev_t devRight; |
johnAlexander | 0:3adb6e4b8113 | 34 | VL53LX_DEV Dev = &devCentre; |
johnAlexander | 0:3adb6e4b8113 | 35 | |
johnAlexander | 0:3adb6e4b8113 | 36 | |
johnAlexander | 0:3adb6e4b8113 | 37 | |
johnAlexander | 0:3adb6e4b8113 | 38 | /*=================================== Main ================================== |
johnAlexander | 0:3adb6e4b8113 | 39 | =============================================================================*/ |
johnAlexander | 0:3adb6e4b8113 | 40 | int main() |
johnAlexander | 0:3adb6e4b8113 | 41 | { |
johnAlexander | 0:3adb6e4b8113 | 42 | int status; |
johnAlexander | 0:3adb6e4b8113 | 43 | VL53LX * Sensor; |
johnAlexander | 0:3adb6e4b8113 | 44 | uint16_t wordData; |
johnAlexander | 0:3adb6e4b8113 | 45 | |
johnAlexander | 0:3adb6e4b8113 | 46 | static VL53LX_MultiRangingData_t RangingData; |
johnAlexander | 0:3adb6e4b8113 | 47 | |
johnAlexander | 0:3adb6e4b8113 | 48 | |
johnAlexander | 0:3adb6e4b8113 | 49 | pc.baud(115200); // baud rate is important as printf statements take a lot of time |
johnAlexander | 0:3adb6e4b8113 | 50 | printf("Hello world!\r\n"); |
johnAlexander | 0:3adb6e4b8113 | 51 | |
johnAlexander | 0:3adb6e4b8113 | 52 | vl53L1X_DevI2C *dev_I2C = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL); |
johnAlexander | 0:3adb6e4b8113 | 53 | |
johnAlexander | 0:3adb6e4b8113 | 54 | // printf("I2C device created! %d %d\r\n",dev_I2C,*dev_I2C); |
johnAlexander | 0:3adb6e4b8113 | 55 | |
johnAlexander | 0:3adb6e4b8113 | 56 | /* creates the 53L1A1 expansion board singleton obj */ |
johnAlexander | 0:3adb6e4b8113 | 57 | board = XNucleo53L1A1::instance(dev_I2C, A2, D8, D2); |
johnAlexander | 0:3adb6e4b8113 | 58 | printf("board created!\r\n"); |
johnAlexander | 0:3adb6e4b8113 | 59 | |
johnAlexander | 0:3adb6e4b8113 | 60 | |
johnAlexander | 0:3adb6e4b8113 | 61 | /* init the 53L1A1 expansion board with default values */ |
johnAlexander | 0:3adb6e4b8113 | 62 | status = board->init_board(); |
johnAlexander | 0:3adb6e4b8113 | 63 | if (status) { |
johnAlexander | 0:3adb6e4b8113 | 64 | printf("Failed to init board!\r\n"); |
johnAlexander | 0:3adb6e4b8113 | 65 | return 0; |
johnAlexander | 0:3adb6e4b8113 | 66 | } |
johnAlexander | 0:3adb6e4b8113 | 67 | |
johnAlexander | 0:3adb6e4b8113 | 68 | |
johnAlexander | 0:3adb6e4b8113 | 69 | printf("board initiated! \n"); |
johnAlexander | 0:3adb6e4b8113 | 70 | |
johnAlexander | 0:3adb6e4b8113 | 71 | printf("configuring centre channel \n"); |
johnAlexander | 0:3adb6e4b8113 | 72 | Dev=&devCentre; |
johnAlexander | 0:3adb6e4b8113 | 73 | Sensor=board->sensor_centre; |
johnAlexander | 0:3adb6e4b8113 | 74 | Dev->I2cDevAddr = NEW_SENSOR_CENTRE_ADDRESS; |
johnAlexander | 0:3adb6e4b8113 | 75 | printf("configured centre channel \n"); |
johnAlexander | 0:3adb6e4b8113 | 76 | |
johnAlexander | 0:3adb6e4b8113 | 77 | |
johnAlexander | 0:3adb6e4b8113 | 78 | // configure the i2c connection |
johnAlexander | 0:3adb6e4b8113 | 79 | Dev->comms_speed_khz = 400; |
johnAlexander | 0:3adb6e4b8113 | 80 | Dev->comms_type = 1; |
johnAlexander | 0:3adb6e4b8113 | 81 | |
johnAlexander | 0:3adb6e4b8113 | 82 | /* Device Initialization and setting */ |
johnAlexander | 0:3adb6e4b8113 | 83 | |
johnAlexander | 0:3adb6e4b8113 | 84 | status = Sensor->VL53LX_DataInit(); |
johnAlexander | 0:3adb6e4b8113 | 85 | uint8_t NewDataReady=0; |
johnAlexander | 0:3adb6e4b8113 | 86 | |
johnAlexander | 0:3adb6e4b8113 | 87 | |
johnAlexander | 0:3adb6e4b8113 | 88 | status = Sensor->VL53LX_StartMeasurement(); |
johnAlexander | 0:3adb6e4b8113 | 89 | printf("VL53LX_StartMeasurement %d \n",status); |
johnAlexander | 0:3adb6e4b8113 | 90 | |
johnAlexander | 0:3adb6e4b8113 | 91 | while(1) |
johnAlexander | 0:3adb6e4b8113 | 92 | { |
johnAlexander | 0:3adb6e4b8113 | 93 | status = Sensor->VL53LX_WaitMeasurementDataReady(); |
johnAlexander | 0:3adb6e4b8113 | 94 | |
johnAlexander | 0:3adb6e4b8113 | 95 | if(!status) |
johnAlexander | 0:3adb6e4b8113 | 96 | { |
johnAlexander | 0:3adb6e4b8113 | 97 | |
johnAlexander | 0:3adb6e4b8113 | 98 | status = Sensor->VL53LX_GetMultiRangingData( &RangingData); |
johnAlexander | 0:3adb6e4b8113 | 99 | |
johnAlexander | 0:3adb6e4b8113 | 100 | if ( status == 0) |
johnAlexander | 0:3adb6e4b8113 | 101 | { |
johnAlexander | 0:3adb6e4b8113 | 102 | int no_of_object_found=RangingData.NumberOfObjectsFound; |
johnAlexander | 0:3adb6e4b8113 | 103 | if ( no_of_object_found < 10 ) |
johnAlexander | 0:3adb6e4b8113 | 104 | { |
johnAlexander | 0:3adb6e4b8113 | 105 | for(int j=0;j<no_of_object_found;j++){ |
johnAlexander | 0:3adb6e4b8113 | 106 | if ((RangingData.RangeData[j].RangeStatus == VL53LX_RANGESTATUS_RANGE_VALID) || |
johnAlexander | 0:3adb6e4b8113 | 107 | (RangingData.RangeData[j].RangeStatus == VL53LX_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL)) |
johnAlexander | 0:3adb6e4b8113 | 108 | { // print data |
johnAlexander | 0:3adb6e4b8113 | 109 | printf("centre \t object %d \t status=%d, \t D=%5dmm, \t Signal=%2.2f Mcps, \t Ambient=%2.2f Mcps \n", |
johnAlexander | 0:3adb6e4b8113 | 110 | j, |
johnAlexander | 0:3adb6e4b8113 | 111 | RangingData.RangeData[j].RangeStatus, |
johnAlexander | 0:3adb6e4b8113 | 112 | RangingData.RangeData[j].RangeMilliMeter, |
johnAlexander | 0:3adb6e4b8113 | 113 | RangingData.RangeData[j].SignalRateRtnMegaCps/65536.0, |
johnAlexander | 0:3adb6e4b8113 | 114 | RangingData.RangeData[j].AmbientRateRtnMegaCps/65536.0); |
johnAlexander | 0:3adb6e4b8113 | 115 | } //if |
johnAlexander | 0:3adb6e4b8113 | 116 | } //for |
johnAlexander | 0:3adb6e4b8113 | 117 | } // if ( no_of_object_found < 10 ) |
johnAlexander | 0:3adb6e4b8113 | 118 | } // if status VL53LX_GetMultiRangingData |
johnAlexander | 0:3adb6e4b8113 | 119 | |
johnAlexander | 0:3adb6e4b8113 | 120 | } // if !status VL53LX_WaitMeasurementDataReady |
johnAlexander | 0:3adb6e4b8113 | 121 | else |
johnAlexander | 0:3adb6e4b8113 | 122 | { |
johnAlexander | 0:3adb6e4b8113 | 123 | printf("VL53L1_WaitMeasurementDataReady failed %d \n",status); |
johnAlexander | 0:3adb6e4b8113 | 124 | } |
johnAlexander | 0:3adb6e4b8113 | 125 | |
johnAlexander | 0:3adb6e4b8113 | 126 | status = Sensor->VL53LX_ClearInterruptAndStartMeasurement(); |
johnAlexander | 0:3adb6e4b8113 | 127 | |
johnAlexander | 0:3adb6e4b8113 | 128 | } // while(1) |
johnAlexander | 0:3adb6e4b8113 | 129 | |
johnAlexander | 0:3adb6e4b8113 | 130 | // status = Sensor->VL53LX_StopMeasurement(); |
johnAlexander | 0:3adb6e4b8113 | 131 | |
johnAlexander | 0:3adb6e4b8113 | 132 | } |