Trial program, to test 3 sensors together, in polling / autonomous mode.
Dependencies: X_NUCLEO_53L1A2
main.cpp@2:f0ec92af4b5f, 2021-05-07 (annotated)
- Committer:
- johnAlexander
- Date:
- Fri May 07 14:15:27 2021 +0000
- Revision:
- 2:f0ec92af4b5f
- Parent:
- 1:ff48a20de191
- Child:
- 3:c1e893e6752f
Use latest board lib.; Add comment about printf floating point config
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 | 2:f0ec92af4b5f | 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 | |
charlesmn | 0:020912dfa221 | 137 | VL53L1_Dev_t devCentre; |
charlesmn | 0:020912dfa221 | 138 | VL53L1_Dev_t devLeft; |
charlesmn | 0:020912dfa221 | 139 | VL53L1_Dev_t devRight; |
charlesmn | 0:020912dfa221 | 140 | VL53L1_DEV Dev = &devCentre; |
charlesmn | 0:020912dfa221 | 141 | |
charlesmn | 0:020912dfa221 | 142 | |
charlesmn | 0:020912dfa221 | 143 | /*=================================== Main ================================== |
charlesmn | 0:020912dfa221 | 144 | =============================================================================*/ |
charlesmn | 0:020912dfa221 | 145 | int main() |
charlesmn | 0:020912dfa221 | 146 | { |
charlesmn | 0:020912dfa221 | 147 | int status; |
charlesmn | 1:ff48a20de191 | 148 | VL53L1 * Sensor; |
charlesmn | 0:020912dfa221 | 149 | uint8_t ToFSensor = 1; // 0=Left, 1=Center(default), 2=Right |
charlesmn | 0:020912dfa221 | 150 | |
charlesmn | 0:020912dfa221 | 151 | //mbed compiler claims these are never used but they are. |
charlesmn | 0:020912dfa221 | 152 | WaitForMeasurement* int2; |
charlesmn | 0:020912dfa221 | 153 | WaitForMeasurement* int1; |
charlesmn | 0:020912dfa221 | 154 | WaitForMeasurement* int3; |
charlesmn | 0:020912dfa221 | 155 | |
charlesmn | 0:020912dfa221 | 156 | |
charlesmn | 0:020912dfa221 | 157 | pc.baud(115200); // baud rate is important as printf statements take a lot of time |
charlesmn | 0:020912dfa221 | 158 | |
charlesmn | 0:020912dfa221 | 159 | printf("Autonomous Interruptmbed = %d \r\n",MBED_VERSION); |
charlesmn | 0:020912dfa221 | 160 | |
charlesmn | 0:020912dfa221 | 161 | // create i2c interface |
charlesmn | 0:020912dfa221 | 162 | ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL); |
charlesmn | 0:020912dfa221 | 163 | |
charlesmn | 0:020912dfa221 | 164 | dev_I2C->frequency(400000); //also needs doing in spi_interface.c |
charlesmn | 0:020912dfa221 | 165 | |
charlesmn | 1:ff48a20de191 | 166 | /* creates the 53L1A2 expansion board singleton obj */ |
charlesmn | 1:ff48a20de191 | 167 | board = XNucleo53L1A2::instance(dev_I2C, CentreIntPin, LeftIntPin, RightIntPin); |
charlesmn | 0:020912dfa221 | 168 | |
charlesmn | 0:020912dfa221 | 169 | printf("board created!\r\n"); |
charlesmn | 0:020912dfa221 | 170 | |
charlesmn | 0:020912dfa221 | 171 | /* init the 53L1A1 expansion board with default values */ |
charlesmn | 0:020912dfa221 | 172 | status = board->init_board(); |
charlesmn | 0:020912dfa221 | 173 | if (status) { |
charlesmn | 0:020912dfa221 | 174 | printf("Failed to init board!\r\n"); |
charlesmn | 0:020912dfa221 | 175 | return 0; |
charlesmn | 0:020912dfa221 | 176 | } |
charlesmn | 0:020912dfa221 | 177 | |
charlesmn | 0:020912dfa221 | 178 | |
charlesmn | 0:020912dfa221 | 179 | printf("board initiated! - %d\r\n", status); |
charlesmn | 0:020912dfa221 | 180 | // create sensor controller classes for each vl53l1 and configure each vl53l1 |
charlesmn | 0:020912dfa221 | 181 | for (ToFSensor=0;ToFSensor < NUM_SENSORS;ToFSensor++){ |
charlesmn | 0:020912dfa221 | 182 | switch(ToFSensor){ |
charlesmn | 0:020912dfa221 | 183 | case 0: |
charlesmn | 0:020912dfa221 | 184 | if (board->sensor_centre== NULL ) continue; // don't create if sensor not detected |
charlesmn | 0:020912dfa221 | 185 | Dev=&devCentre; |
charlesmn | 0:020912dfa221 | 186 | Sensor=board->sensor_centre; |
charlesmn | 0:020912dfa221 | 187 | Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS; |
charlesmn | 0:020912dfa221 | 188 | printf("configuring centre channel \n"); |
charlesmn | 0:020912dfa221 | 189 | break; |
charlesmn | 0:020912dfa221 | 190 | case 1: |
charlesmn | 0:020912dfa221 | 191 | if (board->sensor_left== NULL ) continue; |
charlesmn | 0:020912dfa221 | 192 | Dev=&devLeft; |
charlesmn | 0:020912dfa221 | 193 | Sensor=board->sensor_left; |
charlesmn | 0:020912dfa221 | 194 | Dev->i2c_slave_address = NEW_SENSOR_LEFT_ADDRESS; |
charlesmn | 0:020912dfa221 | 195 | printf("configuring left channel \n"); |
charlesmn | 0:020912dfa221 | 196 | break; |
charlesmn | 0:020912dfa221 | 197 | case 2: |
charlesmn | 0:020912dfa221 | 198 | if (board->sensor_right== NULL ) continue; |
charlesmn | 0:020912dfa221 | 199 | Dev=&devRight; |
charlesmn | 0:020912dfa221 | 200 | Sensor=board->sensor_right; |
charlesmn | 0:020912dfa221 | 201 | Dev->i2c_slave_address = NEW_SENSOR_RIGHT_ADDRESS; |
charlesmn | 0:020912dfa221 | 202 | printf("configuring right channel \n"); |
charlesmn | 0:020912dfa221 | 203 | break; |
charlesmn | 0:020912dfa221 | 204 | default: |
charlesmn | 0:020912dfa221 | 205 | printf(" error in switch, invalid ToF sensor \n"); |
charlesmn | 0:020912dfa221 | 206 | } |
charlesmn | 0:020912dfa221 | 207 | |
charlesmn | 0:020912dfa221 | 208 | // configure the sensors |
charlesmn | 0:020912dfa221 | 209 | Dev->comms_speed_khz = 400; |
charlesmn | 0:020912dfa221 | 210 | |
charlesmn | 0:020912dfa221 | 211 | Dev->comms_type = 1; |
charlesmn | 0:020912dfa221 | 212 | |
charlesmn | 0:020912dfa221 | 213 | /* Device Initialization and setting */ |
charlesmn | 0:020912dfa221 | 214 | status = Sensor->vl53L1_DataInit(); |
charlesmn | 0:020912dfa221 | 215 | status = Sensor->vl53L1_StaticInit(); |
charlesmn | 0:020912dfa221 | 216 | status = Sensor->vl53L1_SetPresetMode(VL53L1_PRESETMODE_AUTONOMOUS); |
charlesmn | 0:020912dfa221 | 217 | status = Sensor->vl53L1_SetDistanceMode(VL53L1_DISTANCEMODE_LONG); |
charlesmn | 0:020912dfa221 | 218 | status = Sensor->vl53L1_SetMeasurementTimingBudgetMicroSeconds( 200 * 1000); |
charlesmn | 0:020912dfa221 | 219 | |
charlesmn | 0:020912dfa221 | 220 | |
charlesmn | 0:020912dfa221 | 221 | // set the ranging and signal rate filter |
charlesmn | 0:020912dfa221 | 222 | VL53L1_DetectionConfig_t thresholdconfig; |
charlesmn | 0:020912dfa221 | 223 | thresholdconfig.DetectionMode = VL53L1_DETECTION_DISTANCE_ONLY; /// type VL53L1_DetectionMode in vl53l1_def.h |
charlesmn | 0:020912dfa221 | 224 | thresholdconfig.Distance.CrossMode = VL53L1_THRESHOLD_IN_WINDOW; // type VL53L1_ThresholdMode. ignore if distance outside high and low |
charlesmn | 0:020912dfa221 | 225 | thresholdconfig.Distance.High = 300; // high distance in mm |
charlesmn | 0:020912dfa221 | 226 | thresholdconfig.Distance.Low = 200; // low distance in mm |
charlesmn | 0:020912dfa221 | 227 | thresholdconfig.Rate.CrossMode=0; // type VL53L1_ThresholdMode VL53L1_THRESHOLD_CROSSED_LOW VL53L1_THRESHOLD_CROSSED_HIGH VL53L1_THRESHOLD_OUT_OF_WINDOW VL53L1_THRESHOLD_IN_WINDOW |
charlesmn | 0:020912dfa221 | 228 | thresholdconfig.Rate.High = 0; |
charlesmn | 0:020912dfa221 | 229 | thresholdconfig.Rate.Low = 0; |
charlesmn | 0:020912dfa221 | 230 | thresholdconfig.IntrNoTarget = 0 ;// if 1 produce an interrupt even if there is no target found e.g out of range |
charlesmn | 0:020912dfa221 | 231 | status = Sensor->vl53L1_SetThresholdConfig(&thresholdconfig); |
charlesmn | 0:020912dfa221 | 232 | } |
charlesmn | 0:020912dfa221 | 233 | |
charlesmn | 0:020912dfa221 | 234 | // create interrupt handlers for the three sensors and start measurements |
charlesmn | 0:020912dfa221 | 235 | if (board->sensor_centre!= NULL ) |
charlesmn | 0:020912dfa221 | 236 | { |
charlesmn | 0:020912dfa221 | 237 | printf("starting interrupt centre\n"); |
charlesmn | 0:020912dfa221 | 238 | devCentre.i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS; |
charlesmn | 0:020912dfa221 | 239 | int1 = new WaitForMeasurement(CentreIntPin,&devCentre); // create interrupt handler |
charlesmn | 0:020912dfa221 | 240 | status = board->sensor_centre->vl53L1_StartMeasurement(); |
charlesmn | 0:020912dfa221 | 241 | } |
charlesmn | 0:020912dfa221 | 242 | |
charlesmn | 0:020912dfa221 | 243 | if (board->sensor_left!= NULL ) |
charlesmn | 0:020912dfa221 | 244 | { |
charlesmn | 0:020912dfa221 | 245 | printf("starting interrupt left\n"); |
charlesmn | 0:020912dfa221 | 246 | devLeft.i2c_slave_address = NEW_SENSOR_LEFT_ADDRESS; |
charlesmn | 0:020912dfa221 | 247 | int2 = new WaitForMeasurement(LeftIntPin,&devLeft); // create interrupt handler |
charlesmn | 0:020912dfa221 | 248 | status = board->sensor_left->vl53L1_StartMeasurement(); |
charlesmn | 0:020912dfa221 | 249 | printf("started interrupt left\n"); |
charlesmn | 0:020912dfa221 | 250 | } |
charlesmn | 0:020912dfa221 | 251 | |
charlesmn | 0:020912dfa221 | 252 | if (board->sensor_right!= NULL ) |
charlesmn | 0:020912dfa221 | 253 | { |
charlesmn | 0:020912dfa221 | 254 | printf("starting interrupt right\n"); |
charlesmn | 0:020912dfa221 | 255 | devRight.i2c_slave_address = NEW_SENSOR_RIGHT_ADDRESS; |
charlesmn | 0:020912dfa221 | 256 | int3 = new WaitForMeasurement(RightIntPin,&devRight); // create interrupt handler |
charlesmn | 0:020912dfa221 | 257 | status = board->sensor_right->vl53L1_StartMeasurement(); |
charlesmn | 0:020912dfa221 | 258 | } |
charlesmn | 0:020912dfa221 | 259 | |
charlesmn | 0:020912dfa221 | 260 | |
charlesmn | 0:020912dfa221 | 261 | printf("loop forever\n"); |
charlesmn | 0:020912dfa221 | 262 | |
charlesmn | 0:020912dfa221 | 263 | // loop waiting for interrupts to happen. This is signaled by int_centre_result,int_left_result or int_right_result |
charlesmn | 0:020912dfa221 | 264 | // being non zero. When the interrupts clear this is signaled by int_centre_dropped,int_left_dropped and int_right_dropped. |
charlesmn | 0:020912dfa221 | 265 | // These are set back to zero when processing is completed |
charlesmn | 0:020912dfa221 | 266 | while (1) |
charlesmn | 0:020912dfa221 | 267 | { |
charlesmn | 0:020912dfa221 | 268 | VL53L1_MultiRangingData_t MultiRangingData; |
charlesmn | 0:020912dfa221 | 269 | VL53L1_MultiRangingData_t *pMultiRangingData = &MultiRangingData; |
charlesmn | 0:020912dfa221 | 270 | |
charlesmn | 0:020912dfa221 | 271 | if ( int_left_dropped || int_centre_dropped || int_right_dropped ) |
charlesmn | 0:020912dfa221 | 272 | wait_ms(30); |
charlesmn | 0:020912dfa221 | 273 | |
charlesmn | 0:020912dfa221 | 274 | // when the interrupt pin goes loww start new measurement |
charlesmn | 0:020912dfa221 | 275 | if ( int_centre_dropped != 0) |
charlesmn | 0:020912dfa221 | 276 | { |
charlesmn | 0:020912dfa221 | 277 | int_centre_dropped = 0; |
charlesmn | 0:020912dfa221 | 278 | status = board->sensor_centre->vl53L1_ClearInterruptAndStartMeasurement(); |
charlesmn | 0:020912dfa221 | 279 | } |
charlesmn | 0:020912dfa221 | 280 | |
charlesmn | 0:020912dfa221 | 281 | if ( int_left_dropped != 0) |
charlesmn | 0:020912dfa221 | 282 | { |
charlesmn | 0:020912dfa221 | 283 | int_left_dropped = 0; |
charlesmn | 0:020912dfa221 | 284 | status = board->sensor_left->vl53L1_ClearInterruptAndStartMeasurement(); |
charlesmn | 0:020912dfa221 | 285 | } |
charlesmn | 0:020912dfa221 | 286 | |
charlesmn | 0:020912dfa221 | 287 | if ( int_right_dropped != 0) |
charlesmn | 0:020912dfa221 | 288 | { |
charlesmn | 0:020912dfa221 | 289 | int_right_dropped = 0; |
charlesmn | 0:020912dfa221 | 290 | status = board->sensor_right->vl53L1_ClearInterruptAndStartMeasurement(); |
charlesmn | 0:020912dfa221 | 291 | } |
charlesmn | 0:020912dfa221 | 292 | |
charlesmn | 0:020912dfa221 | 293 | if (int_right_result != 0) // interrupt seen on right sensor |
charlesmn | 0:020912dfa221 | 294 | { |
charlesmn | 0:020912dfa221 | 295 | status = board->sensor_right->vl53L1_GetMultiRangingData( pMultiRangingData); |
charlesmn | 0:020912dfa221 | 296 | if ( status == 0) |
charlesmn | 0:020912dfa221 | 297 | { |
charlesmn | 0:020912dfa221 | 298 | print_results( devRight.i2c_slave_address, pMultiRangingData ); |
charlesmn | 0:020912dfa221 | 299 | } |
charlesmn | 0:020912dfa221 | 300 | |
charlesmn | 0:020912dfa221 | 301 | // clear interrupt flag |
charlesmn | 0:020912dfa221 | 302 | int_right_result = 0; |
charlesmn | 0:020912dfa221 | 303 | } |
charlesmn | 0:020912dfa221 | 304 | |
charlesmn | 0:020912dfa221 | 305 | if (int_left_result != 0) // interrupt seen on left sensor |
charlesmn | 0:020912dfa221 | 306 | { |
charlesmn | 0:020912dfa221 | 307 | status = board->sensor_left->vl53L1_GetMultiRangingData( pMultiRangingData); |
charlesmn | 0:020912dfa221 | 308 | if ( status == 0) |
charlesmn | 0:020912dfa221 | 309 | { |
charlesmn | 0:020912dfa221 | 310 | print_results( devLeft.i2c_slave_address, pMultiRangingData ); |
charlesmn | 0:020912dfa221 | 311 | } |
charlesmn | 0:020912dfa221 | 312 | |
charlesmn | 0:020912dfa221 | 313 | // clear interrupt flag |
charlesmn | 0:020912dfa221 | 314 | int_left_result = 0; |
charlesmn | 0:020912dfa221 | 315 | } |
charlesmn | 0:020912dfa221 | 316 | |
charlesmn | 0:020912dfa221 | 317 | if (int_centre_result != 0) |
charlesmn | 0:020912dfa221 | 318 | { |
charlesmn | 0:020912dfa221 | 319 | status = board->sensor_centre->vl53L1_GetMultiRangingData( pMultiRangingData); |
charlesmn | 0:020912dfa221 | 320 | if ( status == 0) |
charlesmn | 0:020912dfa221 | 321 | { |
charlesmn | 0:020912dfa221 | 322 | print_results( devCentre.i2c_slave_address, pMultiRangingData ); |
charlesmn | 0:020912dfa221 | 323 | } |
charlesmn | 0:020912dfa221 | 324 | |
charlesmn | 0:020912dfa221 | 325 | // clear interrupt flag |
charlesmn | 0:020912dfa221 | 326 | int_centre_result = 0; |
charlesmn | 0:020912dfa221 | 327 | |
charlesmn | 0:020912dfa221 | 328 | } |
charlesmn | 0:020912dfa221 | 329 | wait_ms( 1 * 10); |
charlesmn | 0:020912dfa221 | 330 | |
charlesmn | 0:020912dfa221 | 331 | } |
charlesmn | 0:020912dfa221 | 332 | } |
charlesmn | 0:020912dfa221 | 333 | |
charlesmn | 0:020912dfa221 | 334 | |
charlesmn | 0:020912dfa221 | 335 | // print what ever results are required |
johnAlexander | 2:f0ec92af4b5f | 336 | void print_results( int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData ) |
charlesmn | 0:020912dfa221 | 337 | { |
johnAlexander | 2:f0ec92af4b5f | 338 | int no_of_object_found=pMultiRangingData->NumberOfObjectsFound; |
charlesmn | 0:020912dfa221 | 339 | |
johnAlexander | 2:f0ec92af4b5f | 340 | int RoiNumber=pMultiRangingData->RoiNumber; |
charlesmn | 0:020912dfa221 | 341 | |
johnAlexander | 2:f0ec92af4b5f | 342 | if (( no_of_object_found < 10 ) && ( no_of_object_found != 0)) |
johnAlexander | 2:f0ec92af4b5f | 343 | { |
johnAlexander | 2:f0ec92af4b5f | 344 | for(int j=0;j<no_of_object_found;j++){ |
johnAlexander | 2:f0ec92af4b5f | 345 | if ((pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) || |
johnAlexander | 2:f0ec92af4b5f | 346 | (pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL)) |
charlesmn | 0:020912dfa221 | 347 | { |
johnAlexander | 2:f0ec92af4b5f | 348 | 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 | 349 | devNumber, RoiNumber, |
johnAlexander | 2:f0ec92af4b5f | 350 | pMultiRangingData->RangeData[j].RangeStatus, |
johnAlexander | 2:f0ec92af4b5f | 351 | pMultiRangingData->RangeData[j].RangeMilliMeter, |
johnAlexander | 2:f0ec92af4b5f | 352 | pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65535.0, |
johnAlexander | 2:f0ec92af4b5f | 353 | pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65535.0); |
johnAlexander | 2:f0ec92af4b5f | 354 | } |
johnAlexander | 2:f0ec92af4b5f | 355 | } |
johnAlexander | 2:f0ec92af4b5f | 356 | } // if (( no_of_object_found < 10 ) && ( no_of_object_found != 0)) |
charlesmn | 0:020912dfa221 | 357 | } |
charlesmn | 0:020912dfa221 | 358 | |
charlesmn | 0:020912dfa221 | 359 | |
charlesmn | 0:020912dfa221 | 360 | #if (MBED_VERSION > 60300) |
charlesmn | 0:020912dfa221 | 361 | extern "C" void wait_ms(int ms) |
charlesmn | 0:020912dfa221 | 362 | { |
charlesmn | 0:020912dfa221 | 363 | thread_sleep_for(ms); |
charlesmn | 0:020912dfa221 | 364 | } |
charlesmn | 0:020912dfa221 | 365 | #endif |
charlesmn | 0:020912dfa221 | 366 | |
charlesmn | 0:020912dfa221 | 367 |