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