as per name
Dependencies: VL53L1CB
main.cpp@5:c59b6c183c8c, 2021-06-24 (annotated)
- Committer:
- johnAlexander
- Date:
- Thu Jun 24 12:36:04 2021 +0000
- Revision:
- 5:c59b6c183c8c
- Parent:
- 4:64f1d9db74fa
Use default sensor constructor. Add delay after power-on, before init sequence.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
charlesmn | 0:86d5438023b9 | 1 | /* |
johnAlexander | 4:64f1d9db74fa | 2 | * This VL53L1CB test application performs range measurements, using a satellite |
johnAlexander | 4:64f1d9db74fa | 3 | * board plugged directly in to the motherboard. |
charlesmn | 0:86d5438023b9 | 4 | * Measured ranges are ouput on the Serial Port, running at 115200 baud. |
charlesmn | 0:86d5438023b9 | 5 | * |
charlesmn | 0:86d5438023b9 | 6 | * This is designed to work with MBed v2.x, & MBedOS v5.x / v6.x. |
charlesmn | 0:86d5438023b9 | 7 | * |
johnAlexander | 4:64f1d9db74fa | 8 | * The satellite board is expected to be connected as follows : |
johnAlexander | 4:64f1d9db74fa | 9 | * |
johnAlexander | 4:64f1d9db74fa | 10 | * I2C_SDA D14 / Arduino Connector CN5, pin 9 |
johnAlexander | 4:64f1d9db74fa | 11 | * I2C_SCL D15 / Arduino Connector CN5, pin 10 |
johnAlexander | 4:64f1d9db74fa | 12 | * Sensor xShutdown D07 / Arduino Connector CN9, pin 8 |
johnAlexander | 4:64f1d9db74fa | 13 | * Sensor Interrupt A02 / Arduino Connector CN8, pin 3 |
johnAlexander | 4:64f1d9db74fa | 14 | * |
charlesmn | 0:86d5438023b9 | 15 | * The Reset button can be used to restart the program. |
charlesmn | 0:86d5438023b9 | 16 | * |
charlesmn | 0:86d5438023b9 | 17 | * *** NOTE : |
charlesmn | 0:86d5438023b9 | 18 | * Default Mbed build system settings disable printf() floating-point support. |
charlesmn | 0:86d5438023b9 | 19 | * Offline builds can enable this, again. |
charlesmn | 0:86d5438023b9 | 20 | * https://github.com/ARMmbed/mbed-os/blob/master/platform/source/minimal-printf/README.md |
charlesmn | 0:86d5438023b9 | 21 | * .\mbed-os\platform\mbed_lib.json |
charlesmn | 0:86d5438023b9 | 22 | * |
charlesmn | 0:86d5438023b9 | 23 | * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of |
charlesmn | 0:86d5438023b9 | 24 | * the X-NUCELO-53L1A1 expansion board are not made/OFF. |
charlesmn | 0:86d5438023b9 | 25 | * These links must be made to allow interrupts from the Satellite boards |
charlesmn | 0:86d5438023b9 | 26 | * to be received. |
charlesmn | 0:86d5438023b9 | 27 | * U11 and U18 must be made/ON to allow interrupts to be received from the |
charlesmn | 0:86d5438023b9 | 28 | * INT_L & INT_R positions; or |
charlesmn | 0:86d5438023b9 | 29 | * U10 and U15 must be made/ON to allow interrupts to be received from the |
charlesmn | 0:86d5438023b9 | 30 | * Alternate INT_L & INT_R positions. |
charlesmn | 0:86d5438023b9 | 31 | * The X_NUCLEO_53L1A2 library defaults to use the INT_L/INT_R positions. |
charlesmn | 0:86d5438023b9 | 32 | * INT_L is available on expansion board Arduino Connector CN5, pin 1 as D8. |
charlesmn | 0:86d5438023b9 | 33 | * Alternate INT_L is on CN5 Connector pin 2 as D9. |
charlesmn | 0:86d5438023b9 | 34 | * INT_R is available on expansion board Arduino Connector CN9, pin 3 as D2. |
charlesmn | 0:86d5438023b9 | 35 | * Alternate INT_R is on CN9 Connector pin 5 as D4. |
charlesmn | 0:86d5438023b9 | 36 | * The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A2/ |
charlesmn | 0:86d5438023b9 | 37 | * |
charlesmn | 0:86d5438023b9 | 38 | */ |
charlesmn | 0:86d5438023b9 | 39 | |
charlesmn | 0:86d5438023b9 | 40 | #include <stdio.h> |
charlesmn | 0:86d5438023b9 | 41 | #include <time.h> |
charlesmn | 0:86d5438023b9 | 42 | |
charlesmn | 0:86d5438023b9 | 43 | #include "mbed.h" |
charlesmn | 0:86d5438023b9 | 44 | |
johnAlexander | 4:64f1d9db74fa | 45 | #include "VL53L1CB.h" |
charlesmn | 0:86d5438023b9 | 46 | #include "ToF_I2C.h" |
charlesmn | 0:86d5438023b9 | 47 | |
charlesmn | 0:86d5438023b9 | 48 | // i2c comms port pins |
charlesmn | 0:86d5438023b9 | 49 | #define I2C_SDA D14 |
charlesmn | 0:86d5438023b9 | 50 | #define I2C_SCL D15 |
charlesmn | 0:86d5438023b9 | 51 | |
charlesmn | 0:86d5438023b9 | 52 | |
johnAlexander | 5:c59b6c183c8c | 53 | #define NEW_SENSOR_CENTRE_ADDRESS 0x52 |
johnAlexander | 5:c59b6c183c8c | 54 | |
charlesmn | 0:86d5438023b9 | 55 | |
charlesmn | 0:86d5438023b9 | 56 | // define interrupt pins |
charlesmn | 0:86d5438023b9 | 57 | PinName CentreIntPin = A2; |
charlesmn | 0:86d5438023b9 | 58 | // the satellite pins depend on solder blobs on the back of the shield. |
charlesmn | 0:86d5438023b9 | 59 | // they may not exist or may be one of two sets. |
charlesmn | 0:86d5438023b9 | 60 | // the centre pin always exists |
charlesmn | 0:86d5438023b9 | 61 | //PinName LeftIntPin = D8; |
charlesmn | 0:86d5438023b9 | 62 | PinName RightIntPin = D2; |
charlesmn | 0:86d5438023b9 | 63 | // alternate set |
charlesmn | 0:86d5438023b9 | 64 | PinName LeftIntPin = D9; |
charlesmn | 0:86d5438023b9 | 65 | //PinName RightIntPin = D4; |
charlesmn | 0:86d5438023b9 | 66 | |
charlesmn | 0:86d5438023b9 | 67 | |
charlesmn | 0:86d5438023b9 | 68 | #if (MBED_VERSION > 60300) |
charlesmn | 0:86d5438023b9 | 69 | UnbufferedSerial pc(USBTX, USBRX); |
charlesmn | 0:86d5438023b9 | 70 | extern "C" void wait_ms(int ms); |
charlesmn | 0:86d5438023b9 | 71 | #else |
charlesmn | 0:86d5438023b9 | 72 | Serial pc(SERIAL_TX, SERIAL_RX); |
charlesmn | 0:86d5438023b9 | 73 | #endif |
charlesmn | 0:86d5438023b9 | 74 | |
charlesmn | 0:86d5438023b9 | 75 | void print_results(int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData ); |
charlesmn | 0:86d5438023b9 | 76 | |
charlesmn | 0:86d5438023b9 | 77 | |
johnAlexander | 5:c59b6c183c8c | 78 | VL53L1CB *Sensor; |
charlesmn | 0:86d5438023b9 | 79 | |
charlesmn | 0:86d5438023b9 | 80 | |
charlesmn | 0:86d5438023b9 | 81 | /* Start the sensor ranging */ |
charlesmn | 0:86d5438023b9 | 82 | int init_sensor() |
charlesmn | 0:86d5438023b9 | 83 | { |
charlesmn | 0:86d5438023b9 | 84 | int status = 0; |
charlesmn | 0:86d5438023b9 | 85 | |
johnAlexander | 5:c59b6c183c8c | 86 | printf("configuring sensor\n"); |
charlesmn | 0:86d5438023b9 | 87 | |
charlesmn | 0:86d5438023b9 | 88 | /* Device Initialization and setting */ |
charlesmn | 0:86d5438023b9 | 89 | status = Sensor->VL53L1CB_DataInit(); |
charlesmn | 0:86d5438023b9 | 90 | status = Sensor->VL53L1CB_StaticInit(); |
charlesmn | 0:86d5438023b9 | 91 | |
charlesmn | 0:86d5438023b9 | 92 | status = Sensor->VL53L1CB_SetPresetMode(VL53L1_PRESETMODE_AUTONOMOUS); |
charlesmn | 0:86d5438023b9 | 93 | status = Sensor->VL53L1CB_SetDistanceMode(VL53L1_DISTANCEMODE_LONG); |
charlesmn | 0:86d5438023b9 | 94 | |
charlesmn | 0:86d5438023b9 | 95 | status = Sensor->VL53L1CB_SetInterMeasurementPeriodMilliSeconds(500); |
charlesmn | 0:86d5438023b9 | 96 | |
charlesmn | 0:86d5438023b9 | 97 | status = Sensor->VL53L1CB_SetMeasurementTimingBudgetMicroSeconds(45000); |
charlesmn | 0:86d5438023b9 | 98 | |
charlesmn | 0:86d5438023b9 | 99 | status = Sensor->VL53L1CB_SetSequenceStepEnable(VL53L1_SEQUENCESTEP_MM1, 0); |
charlesmn | 0:86d5438023b9 | 100 | status = Sensor->VL53L1CB_SetSequenceStepEnable(VL53L1_SEQUENCESTEP_MM2, 0); |
charlesmn | 0:86d5438023b9 | 101 | |
johnAlexander | 5:c59b6c183c8c | 102 | |
charlesmn | 0:86d5438023b9 | 103 | // create interrupt handler and start measurements |
johnAlexander | 5:c59b6c183c8c | 104 | if (Sensor != NULL) { |
johnAlexander | 4:64f1d9db74fa | 105 | status = Sensor->stop_measurement(); |
charlesmn | 0:86d5438023b9 | 106 | if (status != 0) { |
charlesmn | 0:86d5438023b9 | 107 | return status; |
charlesmn | 0:86d5438023b9 | 108 | } |
charlesmn | 0:86d5438023b9 | 109 | |
charlesmn | 0:86d5438023b9 | 110 | status = Sensor->VL53L1CB_StartMeasurement(); |
charlesmn | 0:86d5438023b9 | 111 | if (status != 0) { |
charlesmn | 0:86d5438023b9 | 112 | return status; |
charlesmn | 0:86d5438023b9 | 113 | } |
charlesmn | 0:86d5438023b9 | 114 | } |
charlesmn | 0:86d5438023b9 | 115 | return status; |
charlesmn | 0:86d5438023b9 | 116 | } |
charlesmn | 0:86d5438023b9 | 117 | |
charlesmn | 0:86d5438023b9 | 118 | |
charlesmn | 0:86d5438023b9 | 119 | /*=================================== Main ================================== |
charlesmn | 0:86d5438023b9 | 120 | =============================================================================*/ |
charlesmn | 0:86d5438023b9 | 121 | int main() |
charlesmn | 0:86d5438023b9 | 122 | { |
charlesmn | 0:86d5438023b9 | 123 | int status; |
johnAlexander | 4:64f1d9db74fa | 124 | DigitalOut xshutdown(D7); |
charlesmn | 0:86d5438023b9 | 125 | |
johnAlexander | 5:c59b6c183c8c | 126 | |
charlesmn | 0:86d5438023b9 | 127 | pc.baud(115200); // baud rate is important as printf statements take a lot of time |
charlesmn | 0:86d5438023b9 | 128 | |
charlesmn | 0:86d5438023b9 | 129 | printf("mbed version : %d \r\n",MBED_VERSION); |
charlesmn | 0:86d5438023b9 | 130 | |
johnAlexander | 5:c59b6c183c8c | 131 | // create i2c interface |
charlesmn | 0:86d5438023b9 | 132 | ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL); |
johnAlexander | 5:c59b6c183c8c | 133 | |
johnAlexander | 4:64f1d9db74fa | 134 | Sensor = new VL53L1CB(dev_I2C, &xshutdown, A2); |
charlesmn | 0:86d5438023b9 | 135 | |
johnAlexander | 4:64f1d9db74fa | 136 | printf("sensor created!\r\n"); |
charlesmn | 0:86d5438023b9 | 137 | |
johnAlexander | 5:c59b6c183c8c | 138 | /* init the 53L1A1 expansion board with default values */ |
johnAlexander | 5:c59b6c183c8c | 139 | Sensor->VL53L1CB_Off(); |
johnAlexander | 5:c59b6c183c8c | 140 | |
johnAlexander | 5:c59b6c183c8c | 141 | Sensor->VL53L1CB_On(); |
johnAlexander | 5:c59b6c183c8c | 142 | status = Sensor->InitSensor(NEW_SENSOR_CENTRE_ADDRESS); |
charlesmn | 0:86d5438023b9 | 143 | if (status) { |
johnAlexander | 5:c59b6c183c8c | 144 | delete Sensor; |
johnAlexander | 5:c59b6c183c8c | 145 | Sensor = NULL; |
johnAlexander | 5:c59b6c183c8c | 146 | printf("Sensor not present\n\r"); |
johnAlexander | 5:c59b6c183c8c | 147 | } else { |
johnAlexander | 5:c59b6c183c8c | 148 | printf("Sensor present\n\r"); |
charlesmn | 0:86d5438023b9 | 149 | } |
charlesmn | 0:86d5438023b9 | 150 | |
johnAlexander | 5:c59b6c183c8c | 151 | #if (MBED_VERSION > 60300) |
johnAlexander | 5:c59b6c183c8c | 152 | thread_sleep_for(100); |
johnAlexander | 5:c59b6c183c8c | 153 | #else |
johnAlexander | 5:c59b6c183c8c | 154 | wait_ms(100); // NEEDS A DELAY BETWEEN SENSORS |
johnAlexander | 5:c59b6c183c8c | 155 | #endif |
johnAlexander | 5:c59b6c183c8c | 156 | |
johnAlexander | 5:c59b6c183c8c | 157 | printf("board initialised! - %d\r\n", status); |
charlesmn | 0:86d5438023b9 | 158 | |
charlesmn | 0:86d5438023b9 | 159 | /* init an array with chars to id the sensors */ |
charlesmn | 0:86d5438023b9 | 160 | status = init_sensor(); |
charlesmn | 0:86d5438023b9 | 161 | if (status != 0) { |
johnAlexander | 5:c59b6c183c8c | 162 | printf("Failed to init sensors!\r\n"); |
charlesmn | 0:86d5438023b9 | 163 | return status; |
charlesmn | 0:86d5438023b9 | 164 | } |
charlesmn | 0:86d5438023b9 | 165 | |
charlesmn | 0:86d5438023b9 | 166 | printf("loop forever\n"); |
charlesmn | 0:86d5438023b9 | 167 | |
charlesmn | 0:86d5438023b9 | 168 | VL53L1_MultiRangingData_t MultiRangingData; |
charlesmn | 0:86d5438023b9 | 169 | VL53L1_MultiRangingData_t *pMultiRangingData = NULL; |
charlesmn | 0:86d5438023b9 | 170 | |
charlesmn | 0:86d5438023b9 | 171 | while (true) { |
charlesmn | 0:86d5438023b9 | 172 | pMultiRangingData = &MultiRangingData; |
charlesmn | 0:86d5438023b9 | 173 | |
johnAlexander | 4:64f1d9db74fa | 174 | status = Sensor->VL53L1CB_WaitMeasurementDataReady(); |
johnAlexander | 5:c59b6c183c8c | 175 | |
johnAlexander | 5:c59b6c183c8c | 176 | status = Sensor->VL53L1CB_GetMultiRangingData( pMultiRangingData); |
johnAlexander | 5:c59b6c183c8c | 177 | |
johnAlexander | 5:c59b6c183c8c | 178 | print_results(NEW_SENSOR_CENTRE_ADDRESS, pMultiRangingData ); |
johnAlexander | 5:c59b6c183c8c | 179 | |
johnAlexander | 4:64f1d9db74fa | 180 | status = Sensor->VL53L1CB_ClearInterrupt(); |
charlesmn | 0:86d5438023b9 | 181 | } |
charlesmn | 0:86d5438023b9 | 182 | |
charlesmn | 0:86d5438023b9 | 183 | printf("Terminating.\n"); |
charlesmn | 0:86d5438023b9 | 184 | } |
charlesmn | 0:86d5438023b9 | 185 | |
charlesmn | 0:86d5438023b9 | 186 | |
charlesmn | 0:86d5438023b9 | 187 | // print what ever results are required |
charlesmn | 0:86d5438023b9 | 188 | void print_results( int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData ) |
charlesmn | 0:86d5438023b9 | 189 | { |
charlesmn | 0:86d5438023b9 | 190 | int no_of_object_found = pMultiRangingData->NumberOfObjectsFound; |
charlesmn | 0:86d5438023b9 | 191 | int signal_rate = 0; |
charlesmn | 0:86d5438023b9 | 192 | int ambient_rate = 0; |
charlesmn | 0:86d5438023b9 | 193 | |
charlesmn | 0:86d5438023b9 | 194 | int RoiNumber = pMultiRangingData->RoiNumber; |
charlesmn | 0:86d5438023b9 | 195 | |
charlesmn | 0:86d5438023b9 | 196 | if (( no_of_object_found < 10 ) && ( no_of_object_found != 0)) { |
charlesmn | 0:86d5438023b9 | 197 | for(int j=0; j<no_of_object_found; j++) { |
charlesmn | 0:86d5438023b9 | 198 | if ((pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) || |
charlesmn | 0:86d5438023b9 | 199 | (pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL)) { |
charlesmn | 0:86d5438023b9 | 200 | signal_rate = pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65535; |
charlesmn | 0:86d5438023b9 | 201 | ambient_rate = pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65535; |
charlesmn | 0:86d5438023b9 | 202 | printf("\t i2cAddr=%d \t RoiNumber=%d \t status=%d, \t D=%5dmm, \t Signal=%d Mcps, \t Ambient=%d Mcps \n", |
charlesmn | 0:86d5438023b9 | 203 | devNumber, RoiNumber, |
charlesmn | 0:86d5438023b9 | 204 | pMultiRangingData->RangeData[j].RangeStatus, |
charlesmn | 0:86d5438023b9 | 205 | pMultiRangingData->RangeData[j].RangeMilliMeter, |
charlesmn | 0:86d5438023b9 | 206 | signal_rate, |
johnAlexander | 5:c59b6c183c8c | 207 | ambient_rate); |
johnAlexander | 5:c59b6c183c8c | 208 | /* |
johnAlexander | 5:c59b6c183c8c | 209 | // online compiler disables printf() / floating-point support, for code-size reasons. |
johnAlexander | 5:c59b6c183c8c | 210 | // offline compiler can switch it on. |
johnAlexander | 5:c59b6c183c8c | 211 | printf("\t i2cAddr=%d \t RoiNumber=%d \t status=%d, \t D=%5dmm, \t Signal=%2.2f Mcps, \t Ambient=%2.2f Mcps \n", |
johnAlexander | 5:c59b6c183c8c | 212 | devNumber, RoiNumber, |
johnAlexander | 5:c59b6c183c8c | 213 | pMultiRangingData->RangeData[j].RangeStatus, |
johnAlexander | 5:c59b6c183c8c | 214 | pMultiRangingData->RangeData[j].RangeMilliMeter, |
johnAlexander | 5:c59b6c183c8c | 215 | pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65535.0, |
johnAlexander | 5:c59b6c183c8c | 216 | pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65535.0); |
johnAlexander | 5:c59b6c183c8c | 217 | */ |
charlesmn | 0:86d5438023b9 | 218 | } |
charlesmn | 0:86d5438023b9 | 219 | } |
charlesmn | 0:86d5438023b9 | 220 | } // if (( no_of_object_found < 10 ) && ( no_of_object_found != 0)) |
charlesmn | 0:86d5438023b9 | 221 | } |
charlesmn | 0:86d5438023b9 | 222 | |
johnAlexander | 5:c59b6c183c8c | 223 | |
charlesmn | 0:86d5438023b9 | 224 | #if (MBED_VERSION > 60300) |
charlesmn | 0:86d5438023b9 | 225 | extern "C" void wait_ms(int ms) |
charlesmn | 0:86d5438023b9 | 226 | { |
charlesmn | 0:86d5438023b9 | 227 | thread_sleep_for(ms); |
charlesmn | 0:86d5438023b9 | 228 | } |
charlesmn | 0:86d5438023b9 | 229 | #endif |
charlesmn | 0:86d5438023b9 | 230 | |
charlesmn | 0:86d5438023b9 | 231 |