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