Sample MBed program for use with ST XNucleo VL53L1CB board. Uses interrupts to cope with up to 3 sensors in multizone mode. Includes MBed V6.4
Dependencies: X_NUCLEO_53L1A2
main.cpp@1:c67af60ec906, 2020-11-09 (annotated)
- Committer:
- charlesmn
- Date:
- Mon Nov 09 17:23:54 2020 +0000
- Revision:
- 1:c67af60ec906
- Parent:
- 0:e91189a84ad9
- Child:
- 2:25bcfa4b1aca
No functional change. Changed Filenames and functions names to be VL53L1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
charlesmn | 0:e91189a84ad9 | 1 | /* |
charlesmn | 0:e91189a84ad9 | 2 | * This VL53L1X Expansion board test application performs range measurements |
charlesmn | 0:e91189a84ad9 | 3 | * using the onboard embedded centre sensor and two satelites, in multizone, interrupt mode. |
charlesmn | 0:e91189a84ad9 | 4 | * Measured ranges are ouput on the Serial Port, running at 115200 baud. |
charlesmn | 0:e91189a84ad9 | 5 | * |
charlesmn | 0:e91189a84ad9 | 6 | * |
charlesmn | 0:e91189a84ad9 | 7 | * This is designed to work with MBed V2 , MBed V5 and MBed V6. |
charlesmn | 0:e91189a84ad9 | 8 | * |
charlesmn | 0:e91189a84ad9 | 9 | * |
charlesmn | 0:e91189a84ad9 | 10 | * The Reset button can be used to restart the program. |
charlesmn | 0:e91189a84ad9 | 11 | */ |
charlesmn | 0:e91189a84ad9 | 12 | |
charlesmn | 0:e91189a84ad9 | 13 | #include <stdio.h> |
charlesmn | 0:e91189a84ad9 | 14 | |
charlesmn | 0:e91189a84ad9 | 15 | #include "mbed.h" |
charlesmn | 1:c67af60ec906 | 16 | #include "XNucleo53L1A2.h" |
charlesmn | 0:e91189a84ad9 | 17 | #include "ToF_I2C.h" |
charlesmn | 0:e91189a84ad9 | 18 | #include <time.h> |
charlesmn | 0:e91189a84ad9 | 19 | |
charlesmn | 0:e91189a84ad9 | 20 | |
charlesmn | 0:e91189a84ad9 | 21 | // define the i2c comms pins |
charlesmn | 0:e91189a84ad9 | 22 | #define I2C_SDA D14 |
charlesmn | 0:e91189a84ad9 | 23 | #define I2C_SCL D15 |
charlesmn | 0:e91189a84ad9 | 24 | |
charlesmn | 0:e91189a84ad9 | 25 | #define NUM_SENSORS 3 |
charlesmn | 0:e91189a84ad9 | 26 | |
charlesmn | 1:c67af60ec906 | 27 | // define interrupt pins |
charlesmn | 0:e91189a84ad9 | 28 | PinName CentreIntPin = A2; |
charlesmn | 0:e91189a84ad9 | 29 | // the satellite pins depend on solder blobs on the back of the shield. |
charlesmn | 0:e91189a84ad9 | 30 | // they may not exist or may be one of two sets. |
charlesmn | 0:e91189a84ad9 | 31 | // the centre pin always exists |
charlesmn | 0:e91189a84ad9 | 32 | PinName LeftIntPin = D9; |
charlesmn | 0:e91189a84ad9 | 33 | PinName RightIntPin = D4; |
charlesmn | 0:e91189a84ad9 | 34 | // alternate set |
charlesmn | 0:e91189a84ad9 | 35 | //PinName LeftIntPin = D8; |
charlesmn | 0:e91189a84ad9 | 36 | //PinName RightIntPin = D2; |
charlesmn | 0:e91189a84ad9 | 37 | |
charlesmn | 0:e91189a84ad9 | 38 | |
charlesmn | 1:c67af60ec906 | 39 | static XNucleo53L1A2 *board=NULL; |
charlesmn | 0:e91189a84ad9 | 40 | |
charlesmn | 0:e91189a84ad9 | 41 | #if (MBED_VERSION > 60300) |
charlesmn | 0:e91189a84ad9 | 42 | UnbufferedSerial pc(SERIAL_TX, SERIAL_RX); |
charlesmn | 0:e91189a84ad9 | 43 | extern "C" void wait_ms(int ms); |
charlesmn | 0:e91189a84ad9 | 44 | #else |
charlesmn | 0:e91189a84ad9 | 45 | Serial pc(SERIAL_TX, SERIAL_RX); |
charlesmn | 0:e91189a84ad9 | 46 | #endif |
charlesmn | 0:e91189a84ad9 | 47 | |
charlesmn | 0:e91189a84ad9 | 48 | // flags to indicate an interrupt has happened |
charlesmn | 0:e91189a84ad9 | 49 | static int int_centre_result = 0; |
charlesmn | 0:e91189a84ad9 | 50 | static int int_left_result = 0; |
charlesmn | 0:e91189a84ad9 | 51 | static int int_right_result = 0; |
charlesmn | 0:e91189a84ad9 | 52 | |
charlesmn | 0:e91189a84ad9 | 53 | // flags to indicate an interrupt has cleared |
charlesmn | 0:e91189a84ad9 | 54 | static int int_centre_dropped = 0; |
charlesmn | 0:e91189a84ad9 | 55 | static int int_left_dropped = 0; |
charlesmn | 0:e91189a84ad9 | 56 | static int int_right_dropped = 0; |
charlesmn | 0:e91189a84ad9 | 57 | |
charlesmn | 0:e91189a84ad9 | 58 | // timing for debug purposes |
charlesmn | 0:e91189a84ad9 | 59 | uint32_t centre_polling_time_ms; |
charlesmn | 0:e91189a84ad9 | 60 | uint32_t centre_start_time_ms; |
charlesmn | 0:e91189a84ad9 | 61 | uint32_t centre_end_time_ms; |
charlesmn | 0:e91189a84ad9 | 62 | |
charlesmn | 1:c67af60ec906 | 63 | void process_interrupt( VL53L1 * sensor,VL53L1_DEV dev ); |
charlesmn | 0:e91189a84ad9 | 64 | void print_results( int devSpiNumber, VL53L1_MultiRangingData_t *pMultiRangingData ); |
charlesmn | 0:e91189a84ad9 | 65 | |
charlesmn | 0:e91189a84ad9 | 66 | |
charlesmn | 0:e91189a84ad9 | 67 | |
charlesmn | 0:e91189a84ad9 | 68 | // this class services the interrupts from the ToF sensors. |
charlesmn | 0:e91189a84ad9 | 69 | // There is a limited amount you can do in an interrupt routine; printfs,mutexes break them among other things. |
charlesmn | 0:e91189a84ad9 | 70 | // We keep things simple by only raising a flag so all the real work is done outside the interrupt. |
charlesmn | 0:e91189a84ad9 | 71 | // 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:e91189a84ad9 | 72 | class WaitForMeasurement { |
charlesmn | 0:e91189a84ad9 | 73 | public: |
charlesmn | 0:e91189a84ad9 | 74 | |
charlesmn | 0:e91189a84ad9 | 75 | |
charlesmn | 0:e91189a84ad9 | 76 | WaitForMeasurement(): _interrupt(A1) |
charlesmn | 0:e91189a84ad9 | 77 | { |
charlesmn | 0:e91189a84ad9 | 78 | } |
charlesmn | 0:e91189a84ad9 | 79 | |
charlesmn | 0:e91189a84ad9 | 80 | |
charlesmn | 0:e91189a84ad9 | 81 | // constructor - Sensor is not used and can be removed |
charlesmn | 0:e91189a84ad9 | 82 | WaitForMeasurement(PinName pin,VL53L1_DEV Dev) : _interrupt(pin) // create the InterruptIn on the pin specified to Counter |
charlesmn | 0:e91189a84ad9 | 83 | { |
charlesmn | 0:e91189a84ad9 | 84 | Devlocal = Dev; |
charlesmn | 0:e91189a84ad9 | 85 | pinlocal = pin; |
charlesmn | 0:e91189a84ad9 | 86 | |
charlesmn | 0:e91189a84ad9 | 87 | #include "mbed.h" |
charlesmn | 0:e91189a84ad9 | 88 | |
charlesmn | 0:e91189a84ad9 | 89 | printf("WaitForMeasurement %d \n",Dev->i2c_slave_address); |
charlesmn | 0:e91189a84ad9 | 90 | _interrupt.rise(callback(this, &WaitForMeasurement::got_interrupt)); // if interrupt happens read data |
charlesmn | 0:e91189a84ad9 | 91 | _interrupt.fall(callback(this, &WaitForMeasurement::interruptdropped)); // if interupt clears, start next reading |
charlesmn | 0:e91189a84ad9 | 92 | |
charlesmn | 0:e91189a84ad9 | 93 | } |
charlesmn | 0:e91189a84ad9 | 94 | |
charlesmn | 0:e91189a84ad9 | 95 | // function is called every time an interupt is cleared. causes |
charlesmn | 0:e91189a84ad9 | 96 | void interruptdropped() |
charlesmn | 0:e91189a84ad9 | 97 | { |
charlesmn | 0:e91189a84ad9 | 98 | |
charlesmn | 0:e91189a84ad9 | 99 | if (Devlocal->i2c_slave_address == NEW_SENSOR_CENTRE_ADDRESS) |
charlesmn | 0:e91189a84ad9 | 100 | int_centre_dropped = 1; //flag to main that interrupt cleared. A flag is raised which allows the main routine to service interupt. |
charlesmn | 0:e91189a84ad9 | 101 | if (Devlocal->i2c_slave_address == NEW_SENSOR_LEFT_ADDRESS) |
charlesmn | 0:e91189a84ad9 | 102 | int_left_dropped = 1; //flag to main that interrupt cleared |
charlesmn | 0:e91189a84ad9 | 103 | if (Devlocal->i2c_slave_address == NEW_SENSOR_RIGHT_ADDRESS) |
charlesmn | 0:e91189a84ad9 | 104 | int_right_dropped = 1; //flag to main that interrupt cleared |
charlesmn | 0:e91189a84ad9 | 105 | |
charlesmn | 0:e91189a84ad9 | 106 | } |
charlesmn | 0:e91189a84ad9 | 107 | |
charlesmn | 0:e91189a84ad9 | 108 | // function is called every time an interupt is seen. A flag is raised which allows the main routine to service the interupt. |
charlesmn | 0:e91189a84ad9 | 109 | void got_interrupt() |
charlesmn | 0:e91189a84ad9 | 110 | { |
charlesmn | 0:e91189a84ad9 | 111 | DigitalIn intp(pinlocal); |
charlesmn | 0:e91189a84ad9 | 112 | _count++; |
charlesmn | 0:e91189a84ad9 | 113 | |
charlesmn | 0:e91189a84ad9 | 114 | if (Devlocal->i2c_slave_address == NEW_SENSOR_CENTRE_ADDRESS) |
charlesmn | 0:e91189a84ad9 | 115 | int_centre_result = 1; //flag to main that interrupt happened |
charlesmn | 0:e91189a84ad9 | 116 | if (Devlocal->i2c_slave_address == NEW_SENSOR_LEFT_ADDRESS) |
charlesmn | 0:e91189a84ad9 | 117 | int_left_result = 1; //flag to main that interrupt happened7 |
charlesmn | 0:e91189a84ad9 | 118 | if (Devlocal->i2c_slave_address == NEW_SENSOR_RIGHT_ADDRESS) |
charlesmn | 0:e91189a84ad9 | 119 | int_right_result = 1; //flag to main that interrupt happened |
charlesmn | 0:e91189a84ad9 | 120 | |
charlesmn | 0:e91189a84ad9 | 121 | } |
charlesmn | 0:e91189a84ad9 | 122 | |
charlesmn | 0:e91189a84ad9 | 123 | |
charlesmn | 0:e91189a84ad9 | 124 | //destructor |
charlesmn | 0:e91189a84ad9 | 125 | ~WaitForMeasurement() |
charlesmn | 0:e91189a84ad9 | 126 | { |
charlesmn | 0:e91189a84ad9 | 127 | printf("WaitForMeasurement destruction \n"); |
charlesmn | 0:e91189a84ad9 | 128 | } |
charlesmn | 0:e91189a84ad9 | 129 | |
charlesmn | 0:e91189a84ad9 | 130 | private: |
charlesmn | 0:e91189a84ad9 | 131 | InterruptIn _interrupt; |
charlesmn | 0:e91189a84ad9 | 132 | PinName pinlocal; |
charlesmn | 0:e91189a84ad9 | 133 | volatile int _count; |
charlesmn | 0:e91189a84ad9 | 134 | VL53L1_DEV Devlocal; |
charlesmn | 0:e91189a84ad9 | 135 | int status; |
charlesmn | 0:e91189a84ad9 | 136 | |
charlesmn | 0:e91189a84ad9 | 137 | }; |
charlesmn | 0:e91189a84ad9 | 138 | |
charlesmn | 0:e91189a84ad9 | 139 | |
charlesmn | 0:e91189a84ad9 | 140 | |
charlesmn | 0:e91189a84ad9 | 141 | VL53L1_Dev_t devCentre; |
charlesmn | 0:e91189a84ad9 | 142 | VL53L1_Dev_t devLeft; |
charlesmn | 0:e91189a84ad9 | 143 | VL53L1_Dev_t devRight; |
charlesmn | 0:e91189a84ad9 | 144 | VL53L1_DEV Dev = &devCentre; |
charlesmn | 0:e91189a84ad9 | 145 | |
charlesmn | 0:e91189a84ad9 | 146 | |
charlesmn | 0:e91189a84ad9 | 147 | /*=================================== Main ================================== |
charlesmn | 0:e91189a84ad9 | 148 | =============================================================================*/ |
charlesmn | 0:e91189a84ad9 | 149 | int main() |
charlesmn | 0:e91189a84ad9 | 150 | { |
charlesmn | 0:e91189a84ad9 | 151 | int status; |
charlesmn | 1:c67af60ec906 | 152 | VL53L1 * Sensor; |
charlesmn | 0:e91189a84ad9 | 153 | uint16_t wordData; |
charlesmn | 0:e91189a84ad9 | 154 | uint8_t ToFSensor = 1; // 0=Left, 1=Center(default), 2=Right |
charlesmn | 0:e91189a84ad9 | 155 | |
charlesmn | 0:e91189a84ad9 | 156 | |
charlesmn | 0:e91189a84ad9 | 157 | WaitForMeasurement* int2; |
charlesmn | 0:e91189a84ad9 | 158 | WaitForMeasurement* int1; |
charlesmn | 0:e91189a84ad9 | 159 | WaitForMeasurement* int3; |
charlesmn | 0:e91189a84ad9 | 160 | |
charlesmn | 0:e91189a84ad9 | 161 | pc.baud(115200); // baud rate is important as printf statements take a lot of time |
charlesmn | 0:e91189a84ad9 | 162 | |
charlesmn | 0:e91189a84ad9 | 163 | printf("main_interrupt_multizone \r\n"); |
charlesmn | 0:e91189a84ad9 | 164 | |
charlesmn | 0:e91189a84ad9 | 165 | // create i2c interface |
charlesmn | 0:e91189a84ad9 | 166 | ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL); |
charlesmn | 0:e91189a84ad9 | 167 | |
charlesmn | 0:e91189a84ad9 | 168 | dev_I2C->frequency(400000); //also needs doing in spi_interface.c |
charlesmn | 0:e91189a84ad9 | 169 | |
charlesmn | 1:c67af60ec906 | 170 | /* creates the 53L1A2 expansion board singleton obj */ |
charlesmn | 1:c67af60ec906 | 171 | board = XNucleo53L1A2::instance(dev_I2C, CentreIntPin, RightIntPin, D2); |
charlesmn | 0:e91189a84ad9 | 172 | |
charlesmn | 0:e91189a84ad9 | 173 | printf("board created!\r\n"); |
charlesmn | 0:e91189a84ad9 | 174 | |
charlesmn | 0:e91189a84ad9 | 175 | /* init the 53L1A1 expansion board with default values */ |
charlesmn | 0:e91189a84ad9 | 176 | status = board->init_board(); |
charlesmn | 0:e91189a84ad9 | 177 | if (status) { |
charlesmn | 0:e91189a84ad9 | 178 | printf("Failed to init board!\r\n"); |
charlesmn | 0:e91189a84ad9 | 179 | return 0; |
charlesmn | 0:e91189a84ad9 | 180 | } |
charlesmn | 0:e91189a84ad9 | 181 | |
charlesmn | 0:e91189a84ad9 | 182 | |
charlesmn | 0:e91189a84ad9 | 183 | printf("board initiated! - %d\r\n", status); |
charlesmn | 0:e91189a84ad9 | 184 | // create the sensor controller classes for each sensor and initialise the sensors |
charlesmn | 0:e91189a84ad9 | 185 | for (ToFSensor=0;ToFSensor< NUM_SENSORS;ToFSensor++){ |
charlesmn | 0:e91189a84ad9 | 186 | switch(ToFSensor){ |
charlesmn | 0:e91189a84ad9 | 187 | case 0: |
charlesmn | 0:e91189a84ad9 | 188 | if (board->sensor_centre== NULL ) continue; // don't create if sensor not detected |
charlesmn | 0:e91189a84ad9 | 189 | Dev=&devCentre; |
charlesmn | 0:e91189a84ad9 | 190 | Sensor=board->sensor_centre; |
charlesmn | 0:e91189a84ad9 | 191 | Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS; |
charlesmn | 0:e91189a84ad9 | 192 | printf("configuring centre channel \n"); |
charlesmn | 0:e91189a84ad9 | 193 | break; |
charlesmn | 0:e91189a84ad9 | 194 | case 1: |
charlesmn | 0:e91189a84ad9 | 195 | if (board->sensor_left== NULL ) continue; // don't create if sensor not detected |
charlesmn | 0:e91189a84ad9 | 196 | Dev=&devLeft; |
charlesmn | 0:e91189a84ad9 | 197 | Sensor=board->sensor_left; |
charlesmn | 0:e91189a84ad9 | 198 | Dev->i2c_slave_address = NEW_SENSOR_LEFT_ADDRESS; |
charlesmn | 0:e91189a84ad9 | 199 | printf("configuring left channel \n"); |
charlesmn | 0:e91189a84ad9 | 200 | break; |
charlesmn | 0:e91189a84ad9 | 201 | case 2: |
charlesmn | 0:e91189a84ad9 | 202 | if (board->sensor_right== NULL ) continue; // don't create if sensor not detected |
charlesmn | 0:e91189a84ad9 | 203 | Dev=&devRight; |
charlesmn | 0:e91189a84ad9 | 204 | Sensor=board->sensor_right; |
charlesmn | 0:e91189a84ad9 | 205 | Dev->i2c_slave_address = NEW_SENSOR_RIGHT_ADDRESS; |
charlesmn | 0:e91189a84ad9 | 206 | printf("configuring right channel \n"); |
charlesmn | 0:e91189a84ad9 | 207 | break; |
charlesmn | 0:e91189a84ad9 | 208 | default: |
charlesmn | 0:e91189a84ad9 | 209 | printf(" error in switch, invalid ToF sensor \n"); |
charlesmn | 0:e91189a84ad9 | 210 | } //end of switch |
charlesmn | 0:e91189a84ad9 | 211 | |
charlesmn | 0:e91189a84ad9 | 212 | // configure the sensors |
charlesmn | 0:e91189a84ad9 | 213 | Dev->comms_speed_khz = 400; |
charlesmn | 0:e91189a84ad9 | 214 | |
charlesmn | 0:e91189a84ad9 | 215 | Dev->comms_type = 1; |
charlesmn | 0:e91189a84ad9 | 216 | |
charlesmn | 0:e91189a84ad9 | 217 | /* Device Initialization and setting */ |
charlesmn | 0:e91189a84ad9 | 218 | status = Sensor->vl53L1_DataInit(); |
charlesmn | 0:e91189a84ad9 | 219 | status = Sensor->vl53L1_StaticInit(); |
charlesmn | 0:e91189a84ad9 | 220 | status = Sensor->vl53L1_SetPresetMode( VL53L1_PRESETMODE_MULTIZONES_SCANNING); |
charlesmn | 0:e91189a84ad9 | 221 | |
charlesmn | 0:e91189a84ad9 | 222 | |
charlesmn | 0:e91189a84ad9 | 223 | |
charlesmn | 0:e91189a84ad9 | 224 | //configure the regions of interest for each sensor |
charlesmn | 0:e91189a84ad9 | 225 | VL53L1_RoiConfig_t roiConfig; |
charlesmn | 0:e91189a84ad9 | 226 | roiConfig.NumberOfRoi =3; |
charlesmn | 0:e91189a84ad9 | 227 | roiConfig.UserRois[0].TopLeftX = 0; |
charlesmn | 0:e91189a84ad9 | 228 | roiConfig.UserRois[0].TopLeftY = 9; |
charlesmn | 0:e91189a84ad9 | 229 | roiConfig.UserRois[0].BotRightX = 4; |
charlesmn | 0:e91189a84ad9 | 230 | roiConfig.UserRois[0].BotRightY = 5; |
charlesmn | 0:e91189a84ad9 | 231 | roiConfig.UserRois[1].TopLeftX = 5; |
charlesmn | 0:e91189a84ad9 | 232 | roiConfig.UserRois[1].TopLeftY = 9; |
charlesmn | 0:e91189a84ad9 | 233 | roiConfig.UserRois[1].BotRightX = 9; |
charlesmn | 0:e91189a84ad9 | 234 | roiConfig.UserRois[1].BotRightY = 4; |
charlesmn | 0:e91189a84ad9 | 235 | roiConfig.UserRois[2].TopLeftX = 11; |
charlesmn | 0:e91189a84ad9 | 236 | roiConfig.UserRois[2].TopLeftY = 9; |
charlesmn | 0:e91189a84ad9 | 237 | roiConfig.UserRois[2].BotRightX = 15; |
charlesmn | 0:e91189a84ad9 | 238 | roiConfig.UserRois[2].BotRightY = 5; |
charlesmn | 0:e91189a84ad9 | 239 | status = Sensor->vl53L1_SetROI( &roiConfig); |
charlesmn | 0:e91189a84ad9 | 240 | |
charlesmn | 0:e91189a84ad9 | 241 | |
charlesmn | 0:e91189a84ad9 | 242 | status = Sensor->vl53L1_SetDistanceMode( VL53L1_DISTANCEMODE_LONG); |
charlesmn | 0:e91189a84ad9 | 243 | // status = Sensor->VL53L1_SetMeasurementTimingBudgetMicroSeconds( 100 * 500); // error -21 is because of this. Don't know why |
charlesmn | 0:e91189a84ad9 | 244 | } |
charlesmn | 0:e91189a84ad9 | 245 | |
charlesmn | 0:e91189a84ad9 | 246 | // create interrupt handlers for the sensors that exist |
charlesmn | 0:e91189a84ad9 | 247 | if (board->sensor_centre!= NULL ) |
charlesmn | 0:e91189a84ad9 | 248 | { |
charlesmn | 0:e91189a84ad9 | 249 | printf("starting interrupt centre\n"); |
charlesmn | 0:e91189a84ad9 | 250 | devCentre.i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS; |
charlesmn | 0:e91189a84ad9 | 251 | int1 = new WaitForMeasurement(CentreIntPin,&devCentre); // create a interrupt class for this interrupt pin |
charlesmn | 0:e91189a84ad9 | 252 | status = board->sensor_centre->vl53L1_StartMeasurement(); //start the sensor measuring |
charlesmn | 0:e91189a84ad9 | 253 | } |
charlesmn | 0:e91189a84ad9 | 254 | |
charlesmn | 0:e91189a84ad9 | 255 | if (board->sensor_left!= NULL ) |
charlesmn | 0:e91189a84ad9 | 256 | { |
charlesmn | 0:e91189a84ad9 | 257 | printf("starting interrupt left\n"); |
charlesmn | 0:e91189a84ad9 | 258 | devLeft.i2c_slave_address = NEW_SENSOR_LEFT_ADDRESS; |
charlesmn | 0:e91189a84ad9 | 259 | int2 = new WaitForMeasurement(LeftIntPin,&devLeft); // create a interrupt class for this interrupt pin |
charlesmn | 0:e91189a84ad9 | 260 | status = board->sensor_left->vl53L1_StartMeasurement(); //start the sensor measuring |
charlesmn | 0:e91189a84ad9 | 261 | } |
charlesmn | 0:e91189a84ad9 | 262 | |
charlesmn | 0:e91189a84ad9 | 263 | if (board->sensor_right!= NULL ) |
charlesmn | 0:e91189a84ad9 | 264 | { |
charlesmn | 0:e91189a84ad9 | 265 | printf("starting interrupt right\n"); |
charlesmn | 0:e91189a84ad9 | 266 | devRight.i2c_slave_address = NEW_SENSOR_RIGHT_ADDRESS; |
charlesmn | 0:e91189a84ad9 | 267 | int3 = new WaitForMeasurement(RightIntPin,&devRight); // create a interrupt class for this interrupt pin |
charlesmn | 0:e91189a84ad9 | 268 | status = board->sensor_right->vl53L1_StartMeasurement();//start the sensor measuring |
charlesmn | 0:e91189a84ad9 | 269 | } |
charlesmn | 0:e91189a84ad9 | 270 | |
charlesmn | 0:e91189a84ad9 | 271 | |
charlesmn | 0:e91189a84ad9 | 272 | |
charlesmn | 0:e91189a84ad9 | 273 | |
charlesmn | 0:e91189a84ad9 | 274 | // loop waiting for interrupts to happen. This is signaled by int_centre_result,int_left_result or int_right_result |
charlesmn | 0:e91189a84ad9 | 275 | // being non zero. When the interrupts clear this is signaled by int_centre_dropped,int_left_dropped and int_right_dropped. |
charlesmn | 0:e91189a84ad9 | 276 | // These are set back to zero when processing is completed |
charlesmn | 0:e91189a84ad9 | 277 | while (1) |
charlesmn | 0:e91189a84ad9 | 278 | { |
charlesmn | 0:e91189a84ad9 | 279 | VL53L1_MultiRangingData_t MultiRangingData; |
charlesmn | 0:e91189a84ad9 | 280 | VL53L1_MultiRangingData_t *pMultiRangingData = &MultiRangingData; |
charlesmn | 0:e91189a84ad9 | 281 | |
charlesmn | 0:e91189a84ad9 | 282 | if ( int_left_dropped || int_centre_dropped || int_right_result) |
charlesmn | 0:e91189a84ad9 | 283 | wait_ms(20); |
charlesmn | 0:e91189a84ad9 | 284 | |
charlesmn | 0:e91189a84ad9 | 285 | |
charlesmn | 0:e91189a84ad9 | 286 | // when the interrupt pin goes low start new measurement |
charlesmn | 0:e91189a84ad9 | 287 | if ( int_centre_dropped != 0) |
charlesmn | 0:e91189a84ad9 | 288 | { |
charlesmn | 0:e91189a84ad9 | 289 | int_centre_dropped = 0; |
charlesmn | 0:e91189a84ad9 | 290 | status = board->sensor_centre->vl53L1_ClearInterruptAndStartMeasurement();// get next measurement |
charlesmn | 0:e91189a84ad9 | 291 | } |
charlesmn | 0:e91189a84ad9 | 292 | |
charlesmn | 0:e91189a84ad9 | 293 | if ( int_left_dropped != 0) |
charlesmn | 0:e91189a84ad9 | 294 | { |
charlesmn | 0:e91189a84ad9 | 295 | int_left_dropped = 0; |
charlesmn | 0:e91189a84ad9 | 296 | status = board->sensor_left->vl53L1_ClearInterruptAndStartMeasurement(); |
charlesmn | 0:e91189a84ad9 | 297 | } |
charlesmn | 0:e91189a84ad9 | 298 | |
charlesmn | 0:e91189a84ad9 | 299 | if ( int_right_dropped != 0) |
charlesmn | 0:e91189a84ad9 | 300 | { |
charlesmn | 0:e91189a84ad9 | 301 | int_right_dropped = 0; |
charlesmn | 0:e91189a84ad9 | 302 | status = board->sensor_right->vl53L1_ClearInterruptAndStartMeasurement(); |
charlesmn | 0:e91189a84ad9 | 303 | } |
charlesmn | 0:e91189a84ad9 | 304 | |
charlesmn | 0:e91189a84ad9 | 305 | if (int_right_result != 0) // interrupt seen on right sensor |
charlesmn | 0:e91189a84ad9 | 306 | { |
charlesmn | 0:e91189a84ad9 | 307 | status = board->sensor_right->vl53L1_GetMultiRangingData( pMultiRangingData); |
charlesmn | 0:e91189a84ad9 | 308 | if ( status == 0) |
charlesmn | 0:e91189a84ad9 | 309 | { |
charlesmn | 0:e91189a84ad9 | 310 | print_results( devRight.i2c_slave_address, pMultiRangingData ); |
charlesmn | 0:e91189a84ad9 | 311 | } |
charlesmn | 0:e91189a84ad9 | 312 | |
charlesmn | 0:e91189a84ad9 | 313 | // clear interrupt flag |
charlesmn | 0:e91189a84ad9 | 314 | int_right_result = 0; |
charlesmn | 0:e91189a84ad9 | 315 | } |
charlesmn | 0:e91189a84ad9 | 316 | |
charlesmn | 0:e91189a84ad9 | 317 | |
charlesmn | 0:e91189a84ad9 | 318 | if (int_left_result != 0) // interrupt seen on left sensor |
charlesmn | 0:e91189a84ad9 | 319 | { |
charlesmn | 0:e91189a84ad9 | 320 | status = board->sensor_left->vl53L1_GetMultiRangingData(pMultiRangingData); |
charlesmn | 0:e91189a84ad9 | 321 | if ( status == 0) |
charlesmn | 0:e91189a84ad9 | 322 | { |
charlesmn | 0:e91189a84ad9 | 323 | print_results( devLeft.i2c_slave_address, pMultiRangingData ); |
charlesmn | 0:e91189a84ad9 | 324 | } |
charlesmn | 0:e91189a84ad9 | 325 | |
charlesmn | 0:e91189a84ad9 | 326 | // clear interrupt flag |
charlesmn | 0:e91189a84ad9 | 327 | int_left_result = 0; |
charlesmn | 0:e91189a84ad9 | 328 | } |
charlesmn | 0:e91189a84ad9 | 329 | |
charlesmn | 0:e91189a84ad9 | 330 | if (int_centre_result != 0) |
charlesmn | 0:e91189a84ad9 | 331 | { |
charlesmn | 0:e91189a84ad9 | 332 | status = board->sensor_centre->vl53L1_GetMultiRangingData( pMultiRangingData); |
charlesmn | 0:e91189a84ad9 | 333 | if(status==0) { |
charlesmn | 0:e91189a84ad9 | 334 | print_results(devCentre.i2c_slave_address, pMultiRangingData ); |
charlesmn | 0:e91189a84ad9 | 335 | } //if(status==0) |
charlesmn | 0:e91189a84ad9 | 336 | |
charlesmn | 0:e91189a84ad9 | 337 | // clear interrupt flag |
charlesmn | 0:e91189a84ad9 | 338 | int_centre_result = 0; |
charlesmn | 0:e91189a84ad9 | 339 | |
charlesmn | 0:e91189a84ad9 | 340 | } |
charlesmn | 0:e91189a84ad9 | 341 | |
charlesmn | 0:e91189a84ad9 | 342 | wait_ms( 1 * 5); |
charlesmn | 0:e91189a84ad9 | 343 | |
charlesmn | 0:e91189a84ad9 | 344 | } |
charlesmn | 0:e91189a84ad9 | 345 | } |
charlesmn | 0:e91189a84ad9 | 346 | |
charlesmn | 0:e91189a84ad9 | 347 | |
charlesmn | 0:e91189a84ad9 | 348 | |
charlesmn | 0:e91189a84ad9 | 349 | // print out what data is required |
charlesmn | 0:e91189a84ad9 | 350 | void print_results( int devSpiNumber, VL53L1_MultiRangingData_t *pMultiRangingData ) |
charlesmn | 0:e91189a84ad9 | 351 | { |
charlesmn | 0:e91189a84ad9 | 352 | int no_of_object_found=pMultiRangingData->NumberOfObjectsFound; |
charlesmn | 0:e91189a84ad9 | 353 | |
charlesmn | 0:e91189a84ad9 | 354 | int RoiNumber=pMultiRangingData->RoiNumber; |
charlesmn | 0:e91189a84ad9 | 355 | int RoiStatus=pMultiRangingData->RoiStatus; |
charlesmn | 0:e91189a84ad9 | 356 | |
charlesmn | 0:e91189a84ad9 | 357 | if (( no_of_object_found < 10 ) && ( no_of_object_found != 0)) |
charlesmn | 0:e91189a84ad9 | 358 | { |
charlesmn | 0:e91189a84ad9 | 359 | for(int j=0;j<no_of_object_found;j++){ |
charlesmn | 0:e91189a84ad9 | 360 | if ((pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) || |
charlesmn | 0:e91189a84ad9 | 361 | (pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL)) |
charlesmn | 0:e91189a84ad9 | 362 | { |
charlesmn | 0:e91189a84ad9 | 363 | printf("\t spiAddr=%d \t RoiNumber=%d \t D=%5dmm \n", |
charlesmn | 0:e91189a84ad9 | 364 | devSpiNumber, |
charlesmn | 0:e91189a84ad9 | 365 | RoiNumber, |
charlesmn | 0:e91189a84ad9 | 366 | pMultiRangingData->RangeData[j].RangeMilliMeter); |
charlesmn | 0:e91189a84ad9 | 367 | } |
charlesmn | 0:e91189a84ad9 | 368 | else |
charlesmn | 0:e91189a84ad9 | 369 | { |
charlesmn | 0:e91189a84ad9 | 370 | // printf("RangeStatus %d %d\n",j, pMultiRangingData->RangeData[j].RangeStatus); |
charlesmn | 0:e91189a84ad9 | 371 | } |
charlesmn | 0:e91189a84ad9 | 372 | } |
charlesmn | 0:e91189a84ad9 | 373 | } // if (( no_of_object_found < 10 ) && ( no_of_object_found != 0)) |
charlesmn | 0:e91189a84ad9 | 374 | else |
charlesmn | 0:e91189a84ad9 | 375 | { |
charlesmn | 0:e91189a84ad9 | 376 | // printf("no_of_object_found %d \n",no_of_object_found); |
charlesmn | 0:e91189a84ad9 | 377 | } |
charlesmn | 0:e91189a84ad9 | 378 | |
charlesmn | 0:e91189a84ad9 | 379 | } |
charlesmn | 0:e91189a84ad9 | 380 | |
charlesmn | 0:e91189a84ad9 | 381 | |
charlesmn | 0:e91189a84ad9 | 382 | |
charlesmn | 0:e91189a84ad9 | 383 | #if (MBED_VERSION > 60300) |
charlesmn | 0:e91189a84ad9 | 384 | void wait_ms(int ms) |
charlesmn | 0:e91189a84ad9 | 385 | { |
charlesmn | 0:e91189a84ad9 | 386 | thread_sleep_for(ms); |
charlesmn | 0:e91189a84ad9 | 387 | } |
charlesmn | 0:e91189a84ad9 | 388 | #endif |
charlesmn | 0:e91189a84ad9 | 389 |