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