VL53L1CB expansion board example, showing multi-ranges in an autonomous setup & polling mode. Uses the onboard sensor. Targets MbedOS v6.10.0.
Dependencies: X_NUCLEO_53L1A2
main.cpp@3:c1e893e6752f, 2021-05-11 (annotated)
- Committer:
- johnAlexander
- Date:
- Tue May 11 14:10:25 2021 +0000
- Revision:
- 3:c1e893e6752f
- Parent:
- 2:f0ec92af4b5f
- Child:
- 4:0ac9998b69ac
shield, 1 sensor, interrupt, autonomous.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
charlesmn | 0:020912dfa221 | 1 | /* |
johnAlexander | 2:f0ec92af4b5f | 2 | * This VL53L1X Expansion board test application performs range measurements |
johnAlexander | 2:f0ec92af4b5f | 3 | * using the onboard embedded centre sensor and two satelites, in autonomous, interrupt mode. |
johnAlexander | 2:f0ec92af4b5f | 4 | * Measured ranges are ouput on the Serial Port, running at 115200 baud. |
charlesmn | 0:020912dfa221 | 5 | * |
charlesmn | 0:020912dfa221 | 6 | * This is designed to work with MBed V2 , MBed V5 and MBed V6. |
charlesmn | 0:020912dfa221 | 7 | * |
johnAlexander | 2:f0ec92af4b5f | 8 | * The Reset button can be used to restart the program. |
charlesmn | 0:020912dfa221 | 9 | * |
johnAlexander | 2:f0ec92af4b5f | 10 | * *** Note : |
johnAlexander | 2:f0ec92af4b5f | 11 | * Default Mbed build system settings disable print floating-point support. |
johnAlexander | 2:f0ec92af4b5f | 12 | * Offline builds can enable this, again. |
johnAlexander | 2:f0ec92af4b5f | 13 | * https://github.com/ARMmbed/mbed-os/blob/master/platform/source/minimal-printf/README.md |
johnAlexander | 2:f0ec92af4b5f | 14 | * .\mbed-os\platform\mbed_lib.json |
johnAlexander | 2:f0ec92af4b5f | 15 | * |
charlesmn | 0:020912dfa221 | 16 | */ |
charlesmn | 0:020912dfa221 | 17 | |
charlesmn | 0:020912dfa221 | 18 | #include <stdio.h> |
johnAlexander | 2:f0ec92af4b5f | 19 | #include <time.h> |
charlesmn | 0:020912dfa221 | 20 | |
charlesmn | 0:020912dfa221 | 21 | #include "mbed.h" |
johnAlexander | 2:f0ec92af4b5f | 22 | |
charlesmn | 1:ff48a20de191 | 23 | #include "XNucleo53L1A2.h" |
charlesmn | 0:020912dfa221 | 24 | #include "ToF_I2C.h" |
charlesmn | 0:020912dfa221 | 25 | |
charlesmn | 0:020912dfa221 | 26 | // i2c comms port pins |
charlesmn | 0:020912dfa221 | 27 | #define I2C_SDA D14 |
charlesmn | 0:020912dfa221 | 28 | #define I2C_SCL D15 |
charlesmn | 0:020912dfa221 | 29 | |
charlesmn | 0:020912dfa221 | 30 | |
charlesmn | 0:020912dfa221 | 31 | #define NUM_SENSORS 3 |
charlesmn | 0:020912dfa221 | 32 | |
charlesmn | 1:ff48a20de191 | 33 | // define the interrupt pins |
charlesmn | 0:020912dfa221 | 34 | PinName CentreIntPin = A2; |
charlesmn | 0:020912dfa221 | 35 | // the satellite pins depend on solder blobs on the back of the shield. |
charlesmn | 0:020912dfa221 | 36 | // they may not exist or may be one of two sets. |
charlesmn | 0:020912dfa221 | 37 | // the centre pin always exists |
charlesmn | 0:020912dfa221 | 38 | PinName LeftIntPin = D9; |
charlesmn | 0:020912dfa221 | 39 | PinName RightIntPin = D4; |
charlesmn | 0:020912dfa221 | 40 | // alternate set |
charlesmn | 0:020912dfa221 | 41 | //PinName LeftIntPin = D8; |
charlesmn | 0:020912dfa221 | 42 | //PinName RightIntPin = D2; |
charlesmn | 0:020912dfa221 | 43 | |
charlesmn | 0:020912dfa221 | 44 | |
charlesmn | 0:020912dfa221 | 45 | |
charlesmn | 1:ff48a20de191 | 46 | static XNucleo53L1A2 *board=NULL; |
charlesmn | 0:020912dfa221 | 47 | |
charlesmn | 0:020912dfa221 | 48 | #if (MBED_VERSION > 60300) |
johnAlexander | 2:f0ec92af4b5f | 49 | UnbufferedSerial pc(USBTX, USBRX); |
johnAlexander | 2:f0ec92af4b5f | 50 | extern "C" void wait_ms(int ms); |
charlesmn | 0:020912dfa221 | 51 | #else |
johnAlexander | 2:f0ec92af4b5f | 52 | Serial pc(SERIAL_TX, SERIAL_RX); |
charlesmn | 0:020912dfa221 | 53 | #endif |
charlesmn | 0:020912dfa221 | 54 | |
charlesmn | 0:020912dfa221 | 55 | // flags to indicate an interrupt has happened |
charlesmn | 0:020912dfa221 | 56 | static int int_centre_result = 0; |
charlesmn | 0:020912dfa221 | 57 | static int int_left_result = 0; |
charlesmn | 0:020912dfa221 | 58 | static int int_right_result = 0; |
charlesmn | 0:020912dfa221 | 59 | |
charlesmn | 0:020912dfa221 | 60 | // flags to indicate an interrupt has cleared |
charlesmn | 0:020912dfa221 | 61 | static int int_centre_dropped = 0; |
charlesmn | 0:020912dfa221 | 62 | static int int_left_dropped = 0; |
charlesmn | 0:020912dfa221 | 63 | static int int_right_dropped = 0; |
charlesmn | 0:020912dfa221 | 64 | |
johnAlexander | 3:c1e893e6752f | 65 | void print_results(int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData ); |
charlesmn | 0:020912dfa221 | 66 | |
charlesmn | 0:020912dfa221 | 67 | class WaitForMeasurement { |
charlesmn | 0:020912dfa221 | 68 | public: |
charlesmn | 0:020912dfa221 | 69 | |
charlesmn | 0:020912dfa221 | 70 | |
charlesmn | 0:020912dfa221 | 71 | // this class services the interrupts from the ToF sensors. |
charlesmn | 0:020912dfa221 | 72 | // There is a limited amount you can do in an interrupt routine; printfs,mutexes break them among other things. |
charlesmn | 0:020912dfa221 | 73 | // We keep things simple by only raising a flag so all the real work is done outside the interrupt. |
charlesmn | 0:020912dfa221 | 74 | // This is designed around MBED V2 which doesn't have the RTOS features that would make this work nicely e.g. semaphores/queues. |
charlesmn | 0:020912dfa221 | 75 | WaitForMeasurement(): _interrupt(A1) |
charlesmn | 0:020912dfa221 | 76 | { |
charlesmn | 0:020912dfa221 | 77 | } |
charlesmn | 0:020912dfa221 | 78 | |
charlesmn | 0:020912dfa221 | 79 | |
charlesmn | 0:020912dfa221 | 80 | // constructor - Sensor is not used and can be removed |
charlesmn | 0:020912dfa221 | 81 | WaitForMeasurement(PinName pin,VL53L1_DEV Dev) : _interrupt(pin) // create the InterruptIn on the pin specified to Counter |
charlesmn | 0:020912dfa221 | 82 | { |
charlesmn | 0:020912dfa221 | 83 | Devlocal = Dev; |
charlesmn | 0:020912dfa221 | 84 | pinlocal = pin; |
charlesmn | 0:020912dfa221 | 85 | |
johnAlexander | 2:f0ec92af4b5f | 86 | // #include "mbed.h" |
charlesmn | 0:020912dfa221 | 87 | |
charlesmn | 0:020912dfa221 | 88 | |
charlesmn | 0:020912dfa221 | 89 | _interrupt.rise(callback(this, &WaitForMeasurement::got_interrupt)); // if interrupt happens read data |
charlesmn | 0:020912dfa221 | 90 | _interrupt.fall(callback(this, &WaitForMeasurement::linedropped)); // if interupt clears, clear interrupt |
charlesmn | 0:020912dfa221 | 91 | |
charlesmn | 0:020912dfa221 | 92 | } |
charlesmn | 0:020912dfa221 | 93 | |
charlesmn | 0:020912dfa221 | 94 | // function is called every time an interupt is cleared. Sets flags to clear the interrupt |
charlesmn | 0:020912dfa221 | 95 | void linedropped() |
charlesmn | 0:020912dfa221 | 96 | { |
charlesmn | 0:020912dfa221 | 97 | |
charlesmn | 0:020912dfa221 | 98 | if (Devlocal->i2c_slave_address == NEW_SENSOR_CENTRE_ADDRESS) |
charlesmn | 0:020912dfa221 | 99 | int_centre_dropped = 1; //flag to main that interrupt cleared. A flag is raised which allows the main routine to service interupt. |
charlesmn | 0:020912dfa221 | 100 | if (Devlocal->i2c_slave_address == NEW_SENSOR_LEFT_ADDRESS) |
charlesmn | 0:020912dfa221 | 101 | int_left_dropped = 1; //flag to main that interrupt cleared |
charlesmn | 0:020912dfa221 | 102 | if (Devlocal->i2c_slave_address == NEW_SENSOR_RIGHT_ADDRESS) |
charlesmn | 0:020912dfa221 | 103 | int_right_dropped = 1; //flag to main that interrupt cleared |
charlesmn | 0:020912dfa221 | 104 | |
charlesmn | 0:020912dfa221 | 105 | } |
charlesmn | 0:020912dfa221 | 106 | |
charlesmn | 0:020912dfa221 | 107 | // function is called every time an interupt is seen. A flag is raised which allows the main routine to service the interupt. |
charlesmn | 0:020912dfa221 | 108 | void got_interrupt() |
charlesmn | 0:020912dfa221 | 109 | { |
charlesmn | 0:020912dfa221 | 110 | DigitalIn intp(pinlocal); |
charlesmn | 0:020912dfa221 | 111 | |
charlesmn | 0:020912dfa221 | 112 | if (Devlocal->i2c_slave_address == NEW_SENSOR_CENTRE_ADDRESS) |
charlesmn | 0:020912dfa221 | 113 | int_centre_result = 1; //flag to main that interrupt happened |
charlesmn | 0:020912dfa221 | 114 | if (Devlocal->i2c_slave_address == NEW_SENSOR_LEFT_ADDRESS) |
charlesmn | 0:020912dfa221 | 115 | int_left_result = 1; //flag to main that interrupt happened7 |
charlesmn | 0:020912dfa221 | 116 | if (Devlocal->i2c_slave_address == NEW_SENSOR_RIGHT_ADDRESS) |
charlesmn | 0:020912dfa221 | 117 | int_right_result = 1; //flag to main that interrupt happened |
charlesmn | 0:020912dfa221 | 118 | } |
charlesmn | 0:020912dfa221 | 119 | |
charlesmn | 0:020912dfa221 | 120 | |
charlesmn | 0:020912dfa221 | 121 | //destructor |
charlesmn | 0:020912dfa221 | 122 | ~WaitForMeasurement() |
charlesmn | 0:020912dfa221 | 123 | { |
charlesmn | 0:020912dfa221 | 124 | printf("WaitForMeasurement destruction \n"); |
charlesmn | 0:020912dfa221 | 125 | } |
charlesmn | 0:020912dfa221 | 126 | |
charlesmn | 0:020912dfa221 | 127 | private: |
charlesmn | 0:020912dfa221 | 128 | InterruptIn _interrupt; |
charlesmn | 0:020912dfa221 | 129 | PinName pinlocal; |
charlesmn | 0:020912dfa221 | 130 | VL53L1_DEV Devlocal; |
charlesmn | 0:020912dfa221 | 131 | int status; |
charlesmn | 0:020912dfa221 | 132 | |
charlesmn | 0:020912dfa221 | 133 | }; |
charlesmn | 0:020912dfa221 | 134 | |
charlesmn | 0:020912dfa221 | 135 | |
charlesmn | 0:020912dfa221 | 136 | |
johnAlexander | 3:c1e893e6752f | 137 | |
johnAlexander | 3:c1e893e6752f | 138 | VL53L1_Dev_t devCentre; |
johnAlexander | 3:c1e893e6752f | 139 | VL53L1_Dev_t devLeft; |
johnAlexander | 3:c1e893e6752f | 140 | VL53L1_Dev_t devRight; |
johnAlexander | 3:c1e893e6752f | 141 | VL53L1_DEV Dev = &devCentre; |
charlesmn | 0:020912dfa221 | 142 | |
charlesmn | 0:020912dfa221 | 143 | |
johnAlexander | 3:c1e893e6752f | 144 | /* flags that handle interrupt request for sensor and user blue button*/ |
johnAlexander | 3:c1e893e6752f | 145 | volatile bool int_sensor = false; |
johnAlexander | 3:c1e893e6752f | 146 | volatile bool int_stop = false; |
johnAlexander | 3:c1e893e6752f | 147 | |
johnAlexander | 3:c1e893e6752f | 148 | /* ISR callback function of the centre sensor */ |
johnAlexander | 3:c1e893e6752f | 149 | void sensor_irq(void) |
johnAlexander | 3:c1e893e6752f | 150 | { |
johnAlexander | 3:c1e893e6752f | 151 | int_sensor = true; |
johnAlexander | 3:c1e893e6752f | 152 | board->sensor_centre->disable_interrupt_measure_detection_irq(); |
johnAlexander | 3:c1e893e6752f | 153 | } |
johnAlexander | 3:c1e893e6752f | 154 | |
johnAlexander | 3:c1e893e6752f | 155 | /* Start the sensor ranging */ |
johnAlexander | 3:c1e893e6752f | 156 | int init_sensor() |
johnAlexander | 3:c1e893e6752f | 157 | { |
johnAlexander | 3:c1e893e6752f | 158 | int status = 0; |
johnAlexander | 3:c1e893e6752f | 159 | /* start the measure on the center sensor */ |
johnAlexander | 3:c1e893e6752f | 160 | if (NULL != board->sensor_centre) { |
johnAlexander | 3:c1e893e6752f | 161 | status = board->sensor_centre->stop_measurement(); |
johnAlexander | 3:c1e893e6752f | 162 | if (status != 0) { |
johnAlexander | 3:c1e893e6752f | 163 | return status; |
johnAlexander | 3:c1e893e6752f | 164 | } |
johnAlexander | 3:c1e893e6752f | 165 | |
johnAlexander | 3:c1e893e6752f | 166 | status = board->sensor_centre->start_measurement(&sensor_irq); |
johnAlexander | 3:c1e893e6752f | 167 | if (status != 0) { |
johnAlexander | 3:c1e893e6752f | 168 | return status; |
johnAlexander | 3:c1e893e6752f | 169 | } |
johnAlexander | 3:c1e893e6752f | 170 | } |
johnAlexander | 3:c1e893e6752f | 171 | return status; |
johnAlexander | 3:c1e893e6752f | 172 | } |
johnAlexander | 3:c1e893e6752f | 173 | |
johnAlexander | 3:c1e893e6752f | 174 | /* ISR callback function of the user blue button to switch measuring sensor. */ |
johnAlexander | 3:c1e893e6752f | 175 | void measuring_stop_irq(void) |
johnAlexander | 3:c1e893e6752f | 176 | { |
johnAlexander | 3:c1e893e6752f | 177 | int_stop = true; |
johnAlexander | 3:c1e893e6752f | 178 | } |
johnAlexander | 3:c1e893e6752f | 179 | |
charlesmn | 0:020912dfa221 | 180 | /*=================================== Main ================================== |
charlesmn | 0:020912dfa221 | 181 | =============================================================================*/ |
charlesmn | 0:020912dfa221 | 182 | int main() |
charlesmn | 0:020912dfa221 | 183 | { |
charlesmn | 0:020912dfa221 | 184 | int status; |
charlesmn | 1:ff48a20de191 | 185 | VL53L1 * Sensor; |
charlesmn | 0:020912dfa221 | 186 | uint8_t ToFSensor = 1; // 0=Left, 1=Center(default), 2=Right |
charlesmn | 0:020912dfa221 | 187 | |
charlesmn | 0:020912dfa221 | 188 | //mbed compiler claims these are never used but they are. |
charlesmn | 0:020912dfa221 | 189 | WaitForMeasurement* int2; |
charlesmn | 0:020912dfa221 | 190 | WaitForMeasurement* int1; |
charlesmn | 0:020912dfa221 | 191 | WaitForMeasurement* int3; |
charlesmn | 0:020912dfa221 | 192 | |
charlesmn | 0:020912dfa221 | 193 | |
charlesmn | 0:020912dfa221 | 194 | pc.baud(115200); // baud rate is important as printf statements take a lot of time |
charlesmn | 0:020912dfa221 | 195 | |
johnAlexander | 3:c1e893e6752f | 196 | printf("Autonomous Interrupt, mbed = %d \r\n",MBED_VERSION); |
charlesmn | 0:020912dfa221 | 197 | |
charlesmn | 0:020912dfa221 | 198 | // create i2c interface |
charlesmn | 0:020912dfa221 | 199 | ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL); |
johnAlexander | 3:c1e893e6752f | 200 | /* creates the 53L1A2 expansion board singleton obj */ |
johnAlexander | 3:c1e893e6752f | 201 | board = XNucleo53L1A2::instance(dev_I2C, CentreIntPin, LeftIntPin, RightIntPin); |
charlesmn | 0:020912dfa221 | 202 | |
charlesmn | 0:020912dfa221 | 203 | dev_I2C->frequency(400000); //also needs doing in spi_interface.c |
johnAlexander | 3:c1e893e6752f | 204 | |
charlesmn | 0:020912dfa221 | 205 | printf("board created!\r\n"); |
charlesmn | 0:020912dfa221 | 206 | |
charlesmn | 0:020912dfa221 | 207 | /* init the 53L1A1 expansion board with default values */ |
charlesmn | 0:020912dfa221 | 208 | status = board->init_board(); |
charlesmn | 0:020912dfa221 | 209 | if (status) { |
charlesmn | 0:020912dfa221 | 210 | printf("Failed to init board!\r\n"); |
charlesmn | 0:020912dfa221 | 211 | return 0; |
charlesmn | 0:020912dfa221 | 212 | } |
johnAlexander | 3:c1e893e6752f | 213 | |
charlesmn | 0:020912dfa221 | 214 | printf("board initiated! - %d\r\n", status); |
johnAlexander | 3:c1e893e6752f | 215 | |
johnAlexander | 3:c1e893e6752f | 216 | /* init an array with chars to id the sensors */ |
johnAlexander | 3:c1e893e6752f | 217 | status = init_sensor(); |
johnAlexander | 3:c1e893e6752f | 218 | if (status != 0) { |
johnAlexander | 3:c1e893e6752f | 219 | printf("Failed to init sensors!\r\n"); |
johnAlexander | 3:c1e893e6752f | 220 | return status; |
johnAlexander | 3:c1e893e6752f | 221 | } |
johnAlexander | 3:c1e893e6752f | 222 | |
charlesmn | 0:020912dfa221 | 223 | |
johnAlexander | 3:c1e893e6752f | 224 | Dev=&devCentre; |
johnAlexander | 3:c1e893e6752f | 225 | Sensor=board->sensor_centre; |
johnAlexander | 3:c1e893e6752f | 226 | Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS; |
johnAlexander | 3:c1e893e6752f | 227 | printf("configuring centre channel \n"); |
johnAlexander | 3:c1e893e6752f | 228 | |
johnAlexander | 3:c1e893e6752f | 229 | // configure the sensors |
johnAlexander | 3:c1e893e6752f | 230 | Dev->comms_speed_khz = 400; |
johnAlexander | 3:c1e893e6752f | 231 | Dev->comms_type = 1; |
charlesmn | 0:020912dfa221 | 232 | |
johnAlexander | 3:c1e893e6752f | 233 | /* Device Initialization and setting */ |
johnAlexander | 3:c1e893e6752f | 234 | status = Sensor->vl53L1_DataInit(); |
johnAlexander | 3:c1e893e6752f | 235 | status = Sensor->vl53L1_StaticInit(); |
johnAlexander | 3:c1e893e6752f | 236 | status = Sensor->vl53L1_SetPresetMode(VL53L1_PRESETMODE_AUTONOMOUS); |
johnAlexander | 3:c1e893e6752f | 237 | status = Sensor->vl53L1_SetDistanceMode(VL53L1_DISTANCEMODE_LONG); |
johnAlexander | 3:c1e893e6752f | 238 | status = Sensor->vl53L1_SetMeasurementTimingBudgetMicroSeconds( 200 * 1000); |
charlesmn | 0:020912dfa221 | 239 | |
charlesmn | 0:020912dfa221 | 240 | |
johnAlexander | 3:c1e893e6752f | 241 | // set the ranging and signal rate filter |
johnAlexander | 3:c1e893e6752f | 242 | VL53L1_DetectionConfig_t thresholdconfig; |
johnAlexander | 3:c1e893e6752f | 243 | |
johnAlexander | 3:c1e893e6752f | 244 | thresholdconfig.DetectionMode = VL53L1_DETECTION_DISTANCE_ONLY; /// type VL53L1_DetectionMode in vl53l1_def.h |
johnAlexander | 3:c1e893e6752f | 245 | thresholdconfig.Distance.CrossMode = VL53L1_THRESHOLD_IN_WINDOW; // type VL53L1_ThresholdMode. ignore if distance outside high and low |
johnAlexander | 3:c1e893e6752f | 246 | thresholdconfig.Distance.High = 300; // high distance in mm |
johnAlexander | 3:c1e893e6752f | 247 | thresholdconfig.Distance.Low = 200; // low distance in mm |
johnAlexander | 3:c1e893e6752f | 248 | thresholdconfig.Rate.CrossMode=0; // type VL53L1_ThresholdMode VL53L1_THRESHOLD_CROSSED_LOW VL53L1_THRESHOLD_CROSSED_HIGH VL53L1_THRESHOLD_OUT_OF_WINDOW VL53L1_THRESHOLD_IN_WINDOW |
johnAlexander | 3:c1e893e6752f | 249 | thresholdconfig.Rate.High = 0; |
johnAlexander | 3:c1e893e6752f | 250 | thresholdconfig.Rate.Low = 0; |
johnAlexander | 3:c1e893e6752f | 251 | thresholdconfig.IntrNoTarget = 0 ;// if 1 produce an interrupt even if there is no target found e.g out of range |
johnAlexander | 3:c1e893e6752f | 252 | |
johnAlexander | 3:c1e893e6752f | 253 | status = Sensor->vl53L1_SetThresholdConfig(&thresholdconfig); |
charlesmn | 0:020912dfa221 | 254 | |
johnAlexander | 3:c1e893e6752f | 255 | // create interrupt handlers for the three sensors and start measurements |
johnAlexander | 3:c1e893e6752f | 256 | if (board->sensor_centre!= NULL ) |
johnAlexander | 3:c1e893e6752f | 257 | { |
johnAlexander | 3:c1e893e6752f | 258 | printf("starting interrupt centre\n"); |
johnAlexander | 3:c1e893e6752f | 259 | devCentre.i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS; |
johnAlexander | 3:c1e893e6752f | 260 | int1 = new WaitForMeasurement(CentreIntPin,&devCentre); // create interrupt handler |
johnAlexander | 3:c1e893e6752f | 261 | status = board->sensor_centre->vl53L1_StartMeasurement(); |
johnAlexander | 3:c1e893e6752f | 262 | } |
johnAlexander | 3:c1e893e6752f | 263 | |
johnAlexander | 3:c1e893e6752f | 264 | printf("loop forever\n"); |
johnAlexander | 3:c1e893e6752f | 265 | |
johnAlexander | 3:c1e893e6752f | 266 | // loop waiting for interrupts to happen. This is signaled by int_centre_result,int_left_result or int_right_result |
johnAlexander | 3:c1e893e6752f | 267 | // being non zero. When the interrupts clear this is signaled by int_centre_dropped,int_left_dropped and int_right_dropped. |
johnAlexander | 3:c1e893e6752f | 268 | // These are set back to zero when processing is completed |
johnAlexander | 3:c1e893e6752f | 269 | /* while (1) |
johnAlexander | 3:c1e893e6752f | 270 | { |
johnAlexander | 3:c1e893e6752f | 271 | VL53L1_MultiRangingData_t MultiRangingData; |
johnAlexander | 3:c1e893e6752f | 272 | VL53L1_MultiRangingData_t *pMultiRangingData = &MultiRangingData; |
johnAlexander | 3:c1e893e6752f | 273 | |
johnAlexander | 3:c1e893e6752f | 274 | if ( int_left_dropped || int_centre_dropped || int_right_dropped ) |
johnAlexander | 3:c1e893e6752f | 275 | wait_ms(30); |
johnAlexander | 3:c1e893e6752f | 276 | |
johnAlexander | 3:c1e893e6752f | 277 | // when the interrupt pin goes loww start new measurement |
johnAlexander | 3:c1e893e6752f | 278 | if (int_centre_dropped != 0) |
charlesmn | 0:020912dfa221 | 279 | { |
johnAlexander | 3:c1e893e6752f | 280 | int_centre_dropped = 0; |
johnAlexander | 3:c1e893e6752f | 281 | status = board->sensor_centre->vl53L1_ClearInterruptAndStartMeasurement(); |
charlesmn | 0:020912dfa221 | 282 | } |
johnAlexander | 3:c1e893e6752f | 283 | |
johnAlexander | 3:c1e893e6752f | 284 | if (int_centre_result != 0) |
charlesmn | 0:020912dfa221 | 285 | { |
johnAlexander | 3:c1e893e6752f | 286 | status = board->sensor_centre->vl53L1_GetMultiRangingData( pMultiRangingData); |
johnAlexander | 3:c1e893e6752f | 287 | if (status == 0) |
johnAlexander | 3:c1e893e6752f | 288 | { |
johnAlexander | 3:c1e893e6752f | 289 | print_results( devCentre.i2c_slave_address, pMultiRangingData ); |
johnAlexander | 3:c1e893e6752f | 290 | } |
johnAlexander | 3:c1e893e6752f | 291 | |
johnAlexander | 3:c1e893e6752f | 292 | // clear interrupt flag |
johnAlexander | 3:c1e893e6752f | 293 | int_centre_result = 0; |
johnAlexander | 3:c1e893e6752f | 294 | |
johnAlexander | 3:c1e893e6752f | 295 | } |
johnAlexander | 3:c1e893e6752f | 296 | wait_ms( 1 * 10); |
johnAlexander | 3:c1e893e6752f | 297 | } |
johnAlexander | 3:c1e893e6752f | 298 | */ |
johnAlexander | 3:c1e893e6752f | 299 | |
johnAlexander | 3:c1e893e6752f | 300 | while (true) { |
johnAlexander | 3:c1e893e6752f | 301 | if (int_sensor) { |
johnAlexander | 3:c1e893e6752f | 302 | int_sensor = false; |
johnAlexander | 3:c1e893e6752f | 303 | status = board->sensor_centre->handle_irq(&distance); |
johnAlexander | 3:c1e893e6752f | 304 | printf("distance: %d\r\n", distance); |
charlesmn | 0:020912dfa221 | 305 | } |
charlesmn | 0:020912dfa221 | 306 | |
johnAlexander | 3:c1e893e6752f | 307 | if (int_stop) { |
johnAlexander | 3:c1e893e6752f | 308 | printf("\r\nEnding loop mode \r\n"); |
johnAlexander | 3:c1e893e6752f | 309 | break; |
charlesmn | 0:020912dfa221 | 310 | } |
charlesmn | 0:020912dfa221 | 311 | } |
charlesmn | 0:020912dfa221 | 312 | |
johnAlexander | 3:c1e893e6752f | 313 | } |
johnAlexander | 3:c1e893e6752f | 314 | |
charlesmn | 0:020912dfa221 | 315 | |
charlesmn | 0:020912dfa221 | 316 | // print what ever results are required |
johnAlexander | 2:f0ec92af4b5f | 317 | void print_results( int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData ) |
charlesmn | 0:020912dfa221 | 318 | { |
johnAlexander | 2:f0ec92af4b5f | 319 | int no_of_object_found=pMultiRangingData->NumberOfObjectsFound; |
charlesmn | 0:020912dfa221 | 320 | |
johnAlexander | 2:f0ec92af4b5f | 321 | int RoiNumber=pMultiRangingData->RoiNumber; |
charlesmn | 0:020912dfa221 | 322 | |
johnAlexander | 2:f0ec92af4b5f | 323 | if (( no_of_object_found < 10 ) && ( no_of_object_found != 0)) |
johnAlexander | 2:f0ec92af4b5f | 324 | { |
johnAlexander | 2:f0ec92af4b5f | 325 | for(int j=0;j<no_of_object_found;j++){ |
johnAlexander | 2:f0ec92af4b5f | 326 | if ((pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) || |
johnAlexander | 2:f0ec92af4b5f | 327 | (pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL)) |
charlesmn | 0:020912dfa221 | 328 | { |
johnAlexander | 2:f0ec92af4b5f | 329 | 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 | 2:f0ec92af4b5f | 330 | devNumber, RoiNumber, |
johnAlexander | 2:f0ec92af4b5f | 331 | pMultiRangingData->RangeData[j].RangeStatus, |
johnAlexander | 2:f0ec92af4b5f | 332 | pMultiRangingData->RangeData[j].RangeMilliMeter, |
johnAlexander | 2:f0ec92af4b5f | 333 | pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65535.0, |
johnAlexander | 2:f0ec92af4b5f | 334 | pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65535.0); |
johnAlexander | 2:f0ec92af4b5f | 335 | } |
johnAlexander | 2:f0ec92af4b5f | 336 | } |
johnAlexander | 2:f0ec92af4b5f | 337 | } // if (( no_of_object_found < 10 ) && ( no_of_object_found != 0)) |
charlesmn | 0:020912dfa221 | 338 | } |
charlesmn | 0:020912dfa221 | 339 | |
charlesmn | 0:020912dfa221 | 340 | |
charlesmn | 0:020912dfa221 | 341 | #if (MBED_VERSION > 60300) |
charlesmn | 0:020912dfa221 | 342 | extern "C" void wait_ms(int ms) |
charlesmn | 0:020912dfa221 | 343 | { |
charlesmn | 0:020912dfa221 | 344 | thread_sleep_for(ms); |
charlesmn | 0:020912dfa221 | 345 | } |
charlesmn | 0:020912dfa221 | 346 | #endif |
charlesmn | 0:020912dfa221 | 347 | |
charlesmn | 0:020912dfa221 | 348 |