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@2:25bcfa4b1aca, 2021-05-12 (annotated)
- Committer:
- johnAlexander
- Date:
- Wed May 12 14:30:46 2021 +0000
- Revision:
- 2:25bcfa4b1aca
- Parent:
- 1:c67af60ec906
- Child:
- 3:09f23aad108a
Add satellite sensor support.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
charlesmn | 0:e91189a84ad9 | 1 | /* |
johnAlexander | 2:25bcfa4b1aca | 2 | * This VL53L1CB Expansion board test application performs range measurements |
johnAlexander | 2:25bcfa4b1aca | 3 | * using the onboard embedded sensor and two satellites, in interrupt mode. |
johnAlexander | 2:25bcfa4b1aca | 4 | * Measured ranges are ouput on the Serial Port, running at 115200 baud. |
johnAlexander | 2:25bcfa4b1aca | 5 | * |
johnAlexander | 2:25bcfa4b1aca | 6 | * This is designed to work with MBed v2.x, & MBedOS v5.x / v6.x. |
johnAlexander | 2:25bcfa4b1aca | 7 | * |
johnAlexander | 2:25bcfa4b1aca | 8 | * The Reset button can be used to restart the program. |
johnAlexander | 2:25bcfa4b1aca | 9 | * |
johnAlexander | 2:25bcfa4b1aca | 10 | * *** NOTE : |
johnAlexander | 2:25bcfa4b1aca | 11 | * Default Mbed build system settings disable printf() floating-point support. |
johnAlexander | 2:25bcfa4b1aca | 12 | * Offline builds can enable this, again. |
johnAlexander | 2:25bcfa4b1aca | 13 | * https://github.com/ARMmbed/mbed-os/blob/master/platform/source/minimal-printf/README.md |
johnAlexander | 2:25bcfa4b1aca | 14 | * .\mbed-os\platform\mbed_lib.json |
charlesmn | 0:e91189a84ad9 | 15 | * |
johnAlexander | 2:25bcfa4b1aca | 16 | * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of |
johnAlexander | 2:25bcfa4b1aca | 17 | * the X-NUCELO-53L1A2 expansion board are not made/OFF. |
johnAlexander | 2:25bcfa4b1aca | 18 | * These links must be made to allow interrupts from the Satellite boards |
johnAlexander | 2:25bcfa4b1aca | 19 | * to be received. |
johnAlexander | 2:25bcfa4b1aca | 20 | * U11 and U18 must be made/ON to allow interrupts to be received from the |
johnAlexander | 2:25bcfa4b1aca | 21 | * INT_L & INT_R positions; or |
johnAlexander | 2:25bcfa4b1aca | 22 | * U10 and U15 must be made/ON to allow interrupts to be received from the |
johnAlexander | 2:25bcfa4b1aca | 23 | * Alternate INT_L & INT_R positions. |
johnAlexander | 2:25bcfa4b1aca | 24 | * The X_NUCLEO_53L1A2 library defaults to use the INT_L/INT_R positions. |
johnAlexander | 2:25bcfa4b1aca | 25 | * INT_L is available on expansion board Arduino Connector CN5, pin 1 as D8. |
johnAlexander | 2:25bcfa4b1aca | 26 | * Alternate INT_L is on CN5 Connector pin 2 as D9. |
johnAlexander | 2:25bcfa4b1aca | 27 | * INT_R is available on expansion board Arduino Connector CN9, pin 3 as D2. |
johnAlexander | 2:25bcfa4b1aca | 28 | * Alternate INT_R is on CN9 Connector pin 5 as D4. |
johnAlexander | 2:25bcfa4b1aca | 29 | * The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A2/ |
charlesmn | 0:e91189a84ad9 | 30 | * |
charlesmn | 0:e91189a84ad9 | 31 | */ |
charlesmn | 0:e91189a84ad9 | 32 | |
charlesmn | 0:e91189a84ad9 | 33 | #include <stdio.h> |
johnAlexander | 2:25bcfa4b1aca | 34 | #include <time.h> |
charlesmn | 0:e91189a84ad9 | 35 | |
charlesmn | 0:e91189a84ad9 | 36 | #include "mbed.h" |
johnAlexander | 2:25bcfa4b1aca | 37 | |
charlesmn | 1:c67af60ec906 | 38 | #include "XNucleo53L1A2.h" |
charlesmn | 0:e91189a84ad9 | 39 | #include "ToF_I2C.h" |
charlesmn | 0:e91189a84ad9 | 40 | |
charlesmn | 0:e91189a84ad9 | 41 | |
charlesmn | 0:e91189a84ad9 | 42 | // define the i2c comms pins |
charlesmn | 0:e91189a84ad9 | 43 | #define I2C_SDA D14 |
charlesmn | 0:e91189a84ad9 | 44 | #define I2C_SCL D15 |
charlesmn | 0:e91189a84ad9 | 45 | |
charlesmn | 0:e91189a84ad9 | 46 | #define NUM_SENSORS 3 |
charlesmn | 0:e91189a84ad9 | 47 | |
charlesmn | 1:c67af60ec906 | 48 | // define interrupt pins |
charlesmn | 0:e91189a84ad9 | 49 | PinName CentreIntPin = A2; |
charlesmn | 0:e91189a84ad9 | 50 | // the satellite pins depend on solder blobs on the back of the shield. |
charlesmn | 0:e91189a84ad9 | 51 | // they may not exist or may be one of two sets. |
charlesmn | 0:e91189a84ad9 | 52 | // the centre pin always exists |
johnAlexander | 2:25bcfa4b1aca | 53 | //PinName LeftIntPin = D8; |
johnAlexander | 2:25bcfa4b1aca | 54 | PinName RightIntPin = D2; |
charlesmn | 0:e91189a84ad9 | 55 | // alternate set |
johnAlexander | 2:25bcfa4b1aca | 56 | PinName LeftIntPin = D9; |
johnAlexander | 2:25bcfa4b1aca | 57 | //PinName RightIntPin = D4; |
charlesmn | 0:e91189a84ad9 | 58 | |
charlesmn | 0:e91189a84ad9 | 59 | |
charlesmn | 1:c67af60ec906 | 60 | static XNucleo53L1A2 *board=NULL; |
charlesmn | 0:e91189a84ad9 | 61 | |
charlesmn | 0:e91189a84ad9 | 62 | #if (MBED_VERSION > 60300) |
johnAlexander | 2:25bcfa4b1aca | 63 | UnbufferedSerial pc(USBTX, USBRX); |
charlesmn | 0:e91189a84ad9 | 64 | extern "C" void wait_ms(int ms); |
charlesmn | 0:e91189a84ad9 | 65 | #else |
charlesmn | 0:e91189a84ad9 | 66 | Serial pc(SERIAL_TX, SERIAL_RX); |
charlesmn | 0:e91189a84ad9 | 67 | #endif |
charlesmn | 0:e91189a84ad9 | 68 | |
johnAlexander | 2:25bcfa4b1aca | 69 | #if TARGET_STM // we are cross compiling for an STM32-Nucleo |
johnAlexander | 2:25bcfa4b1aca | 70 | InterruptIn stop_button(BUTTON1); |
johnAlexander | 2:25bcfa4b1aca | 71 | #endif |
johnAlexander | 2:25bcfa4b1aca | 72 | #if TARGET_Freescale // we are cross-compiling for NXP FRDM boards. |
johnAlexander | 2:25bcfa4b1aca | 73 | InterruptIn stop_button(SW2); |
johnAlexander | 2:25bcfa4b1aca | 74 | #endif |
charlesmn | 0:e91189a84ad9 | 75 | |
charlesmn | 1:c67af60ec906 | 76 | void process_interrupt( VL53L1 * sensor,VL53L1_DEV dev ); |
charlesmn | 0:e91189a84ad9 | 77 | void print_results( int devSpiNumber, VL53L1_MultiRangingData_t *pMultiRangingData ); |
charlesmn | 0:e91189a84ad9 | 78 | |
charlesmn | 0:e91189a84ad9 | 79 | |
johnAlexander | 2:25bcfa4b1aca | 80 | VL53L1_Dev_t devCentre; |
johnAlexander | 2:25bcfa4b1aca | 81 | VL53L1_Dev_t devLeft; |
johnAlexander | 2:25bcfa4b1aca | 82 | VL53L1_Dev_t devRight; |
johnAlexander | 2:25bcfa4b1aca | 83 | VL53L1_DEV Dev = &devCentre; |
charlesmn | 0:e91189a84ad9 | 84 | |
johnAlexander | 2:25bcfa4b1aca | 85 | /* Installed sensors count */ |
johnAlexander | 2:25bcfa4b1aca | 86 | int sensorCnt = 0; |
johnAlexander | 2:25bcfa4b1aca | 87 | |
johnAlexander | 2:25bcfa4b1aca | 88 | /* installed sensors prefixes */ |
johnAlexander | 2:25bcfa4b1aca | 89 | char installedSensors[3]; |
johnAlexander | 2:25bcfa4b1aca | 90 | |
johnAlexander | 2:25bcfa4b1aca | 91 | /* interrupt requests */ |
johnAlexander | 2:25bcfa4b1aca | 92 | volatile bool centerSensor = false; |
johnAlexander | 2:25bcfa4b1aca | 93 | volatile bool leftSensor = false; |
johnAlexander | 2:25bcfa4b1aca | 94 | volatile bool rightSensor = false; |
johnAlexander | 2:25bcfa4b1aca | 95 | volatile bool int_measuring_stop = false; |
johnAlexander | 2:25bcfa4b1aca | 96 | |
johnAlexander | 2:25bcfa4b1aca | 97 | /* Current sensor number*/ |
johnAlexander | 2:25bcfa4b1aca | 98 | volatile int currentSensor = 0; |
charlesmn | 0:e91189a84ad9 | 99 | |
johnAlexander | 2:25bcfa4b1aca | 100 | /* current displayed sensor change IRQ */ |
johnAlexander | 2:25bcfa4b1aca | 101 | volatile bool switchChanged = false; |
charlesmn | 0:e91189a84ad9 | 102 | |
johnAlexander | 2:25bcfa4b1aca | 103 | /* ISR callback function of the centre sensor */ |
johnAlexander | 2:25bcfa4b1aca | 104 | void sensor_centre_irq(void) |
charlesmn | 0:e91189a84ad9 | 105 | { |
johnAlexander | 2:25bcfa4b1aca | 106 | centerSensor = true; |
johnAlexander | 2:25bcfa4b1aca | 107 | board->sensor_centre->disable_interrupt_measure_detection_irq(); |
johnAlexander | 2:25bcfa4b1aca | 108 | } |
johnAlexander | 2:25bcfa4b1aca | 109 | |
johnAlexander | 2:25bcfa4b1aca | 110 | /* ISR callback function of the left sensor */ |
johnAlexander | 2:25bcfa4b1aca | 111 | void sensor_left_irq(void) |
johnAlexander | 2:25bcfa4b1aca | 112 | { |
johnAlexander | 2:25bcfa4b1aca | 113 | leftSensor = true; |
johnAlexander | 2:25bcfa4b1aca | 114 | board->sensor_left->disable_interrupt_measure_detection_irq(); |
charlesmn | 0:e91189a84ad9 | 115 | } |
charlesmn | 0:e91189a84ad9 | 116 | |
johnAlexander | 2:25bcfa4b1aca | 117 | /* ISR callback function of the right sensor */ |
johnAlexander | 2:25bcfa4b1aca | 118 | void sensor_right_irq(void) |
johnAlexander | 2:25bcfa4b1aca | 119 | { |
johnAlexander | 2:25bcfa4b1aca | 120 | rightSensor = true; |
johnAlexander | 2:25bcfa4b1aca | 121 | board->sensor_right->disable_interrupt_measure_detection_irq(); |
johnAlexander | 2:25bcfa4b1aca | 122 | } |
charlesmn | 0:e91189a84ad9 | 123 | |
johnAlexander | 2:25bcfa4b1aca | 124 | /* ISR callback function of the user blue button to switch measuring sensor. */ |
johnAlexander | 2:25bcfa4b1aca | 125 | void switch_measuring_sensor_irq(void) |
johnAlexander | 2:25bcfa4b1aca | 126 | { |
johnAlexander | 2:25bcfa4b1aca | 127 | stop_button.disable_irq(); |
johnAlexander | 2:25bcfa4b1aca | 128 | switchChanged = true; |
johnAlexander | 2:25bcfa4b1aca | 129 | } |
johnAlexander | 2:25bcfa4b1aca | 130 | |
johnAlexander | 2:25bcfa4b1aca | 131 | /* |
johnAlexander | 2:25bcfa4b1aca | 132 | * This function calls the interrupt handler for each sensor |
johnAlexander | 2:25bcfa4b1aca | 133 | * and outputs the range |
johnAlexander | 2:25bcfa4b1aca | 134 | */ |
johnAlexander | 2:25bcfa4b1aca | 135 | inline void measure_sensors() |
johnAlexander | 2:25bcfa4b1aca | 136 | { |
johnAlexander | 2:25bcfa4b1aca | 137 | int status = 0; |
johnAlexander | 2:25bcfa4b1aca | 138 | bool current = false; |
johnAlexander | 2:25bcfa4b1aca | 139 | uint16_t distance = 0; |
johnAlexander | 2:25bcfa4b1aca | 140 | |
johnAlexander | 2:25bcfa4b1aca | 141 | /* Handle the interrupt and output the range from the centre sensor */ |
johnAlexander | 2:25bcfa4b1aca | 142 | if (centerSensor) { |
johnAlexander | 2:25bcfa4b1aca | 143 | centerSensor = false; |
johnAlexander | 2:25bcfa4b1aca | 144 | // board->sensor_centre->handle_irq(&distance); |
johnAlexander | 2:25bcfa4b1aca | 145 | status = board->sensor_centre->VL53L1_GetDistance(&distance); |
johnAlexander | 2:25bcfa4b1aca | 146 | status = board->sensor_centre->VL53L1_ClearInterrupt(); |
johnAlexander | 2:25bcfa4b1aca | 147 | board->sensor_centre->enable_interrupt_measure_detection_irq(); |
charlesmn | 0:e91189a84ad9 | 148 | |
johnAlexander | 2:25bcfa4b1aca | 149 | current = (currentSensor == 0); |
johnAlexander | 2:25bcfa4b1aca | 150 | if (current) { |
johnAlexander | 2:25bcfa4b1aca | 151 | printf("Centre: %d\r\n", distance); |
johnAlexander | 2:25bcfa4b1aca | 152 | } |
charlesmn | 0:e91189a84ad9 | 153 | } |
charlesmn | 0:e91189a84ad9 | 154 | |
johnAlexander | 2:25bcfa4b1aca | 155 | /* Handle the interrupt and output the range from the left sensor */ |
johnAlexander | 2:25bcfa4b1aca | 156 | if (leftSensor) { |
johnAlexander | 2:25bcfa4b1aca | 157 | leftSensor = false; |
johnAlexander | 2:25bcfa4b1aca | 158 | // board->sensor_left->handle_irq(&distance); |
johnAlexander | 2:25bcfa4b1aca | 159 | status = board->sensor_left->VL53L1_GetDistance(&distance); |
johnAlexander | 2:25bcfa4b1aca | 160 | status = board->sensor_left->VL53L1_ClearInterrupt(); |
johnAlexander | 2:25bcfa4b1aca | 161 | board->sensor_left->enable_interrupt_measure_detection_irq(); |
johnAlexander | 2:25bcfa4b1aca | 162 | |
johnAlexander | 2:25bcfa4b1aca | 163 | current = (installedSensors[currentSensor] == 'L'); |
johnAlexander | 2:25bcfa4b1aca | 164 | if (current) { |
johnAlexander | 2:25bcfa4b1aca | 165 | printf("Left: %d\r\n", distance); |
johnAlexander | 2:25bcfa4b1aca | 166 | } |
johnAlexander | 2:25bcfa4b1aca | 167 | } |
charlesmn | 0:e91189a84ad9 | 168 | |
johnAlexander | 2:25bcfa4b1aca | 169 | /* Handle the interrupt and output the range from the right sensor */ |
johnAlexander | 2:25bcfa4b1aca | 170 | if (rightSensor) { |
johnAlexander | 2:25bcfa4b1aca | 171 | rightSensor = false; |
johnAlexander | 2:25bcfa4b1aca | 172 | // board->sensor_right->handle_irq(&distance); |
johnAlexander | 2:25bcfa4b1aca | 173 | status = board->sensor_right->VL53L1_GetDistance(&distance); |
johnAlexander | 2:25bcfa4b1aca | 174 | status = board->sensor_right->VL53L1_ClearInterrupt(); |
johnAlexander | 2:25bcfa4b1aca | 175 | board->sensor_right->enable_interrupt_measure_detection_irq(); |
johnAlexander | 2:25bcfa4b1aca | 176 | |
johnAlexander | 2:25bcfa4b1aca | 177 | current = (installedSensors[currentSensor] == 'R'); |
johnAlexander | 2:25bcfa4b1aca | 178 | if (current) { |
johnAlexander | 2:25bcfa4b1aca | 179 | printf("Right: %d\r\n", distance); |
johnAlexander | 2:25bcfa4b1aca | 180 | } |
johnAlexander | 2:25bcfa4b1aca | 181 | } |
johnAlexander | 2:25bcfa4b1aca | 182 | } |
charlesmn | 0:e91189a84ad9 | 183 | |
johnAlexander | 2:25bcfa4b1aca | 184 | /* |
johnAlexander | 2:25bcfa4b1aca | 185 | * Add to an array a character that represents the sensor and start ranging |
johnAlexander | 2:25bcfa4b1aca | 186 | */ |
johnAlexander | 2:25bcfa4b1aca | 187 | int init_sensors_array() |
johnAlexander | 2:25bcfa4b1aca | 188 | { |
johnAlexander | 2:25bcfa4b1aca | 189 | int status = 0; |
johnAlexander | 2:25bcfa4b1aca | 190 | VL53L1 *Sensor; |
charlesmn | 0:e91189a84ad9 | 191 | uint8_t ToFSensor = 1; // 0=Left, 1=Center(default), 2=Right |
charlesmn | 0:e91189a84ad9 | 192 | |
johnAlexander | 2:25bcfa4b1aca | 193 | sensorCnt = 0; |
charlesmn | 0:e91189a84ad9 | 194 | |
johnAlexander | 2:25bcfa4b1aca | 195 | if (board->sensor_centre != NULL) { |
johnAlexander | 2:25bcfa4b1aca | 196 | Dev = &devCentre; |
johnAlexander | 2:25bcfa4b1aca | 197 | Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS; |
johnAlexander | 2:25bcfa4b1aca | 198 | Sensor = board->sensor_centre; |
johnAlexander | 2:25bcfa4b1aca | 199 | printf("configuring centre channel \n"); |
charlesmn | 0:e91189a84ad9 | 200 | } |
johnAlexander | 2:25bcfa4b1aca | 201 | |
johnAlexander | 2:25bcfa4b1aca | 202 | if (board->sensor_left != NULL) { |
johnAlexander | 2:25bcfa4b1aca | 203 | Dev = &devLeft; |
johnAlexander | 2:25bcfa4b1aca | 204 | Dev->i2c_slave_address = NEW_SENSOR_LEFT_ADDRESS; |
johnAlexander | 2:25bcfa4b1aca | 205 | Sensor = board->sensor_left; |
johnAlexander | 2:25bcfa4b1aca | 206 | printf("configuring left channel \n"); |
johnAlexander | 2:25bcfa4b1aca | 207 | } |
johnAlexander | 2:25bcfa4b1aca | 208 | |
johnAlexander | 2:25bcfa4b1aca | 209 | if (board->sensor_right != NULL) { |
johnAlexander | 2:25bcfa4b1aca | 210 | Dev = &devRight; |
johnAlexander | 2:25bcfa4b1aca | 211 | Dev->i2c_slave_address = NEW_SENSOR_RIGHT_ADDRESS; |
johnAlexander | 2:25bcfa4b1aca | 212 | Sensor = board->sensor_right; |
johnAlexander | 2:25bcfa4b1aca | 213 | printf("configuring right channel \n"); |
johnAlexander | 2:25bcfa4b1aca | 214 | } |
johnAlexander | 2:25bcfa4b1aca | 215 | /* |
johnAlexander | 2:25bcfa4b1aca | 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 | //configure the regions of interest for each sensor |
charlesmn | 0:e91189a84ad9 | 222 | VL53L1_RoiConfig_t roiConfig; |
johnAlexander | 2:25bcfa4b1aca | 223 | roiConfig.NumberOfRoi = 3; |
charlesmn | 0:e91189a84ad9 | 224 | roiConfig.UserRois[0].TopLeftX = 0; |
charlesmn | 0:e91189a84ad9 | 225 | roiConfig.UserRois[0].TopLeftY = 9; |
charlesmn | 0:e91189a84ad9 | 226 | roiConfig.UserRois[0].BotRightX = 4; |
charlesmn | 0:e91189a84ad9 | 227 | roiConfig.UserRois[0].BotRightY = 5; |
charlesmn | 0:e91189a84ad9 | 228 | roiConfig.UserRois[1].TopLeftX = 5; |
charlesmn | 0:e91189a84ad9 | 229 | roiConfig.UserRois[1].TopLeftY = 9; |
charlesmn | 0:e91189a84ad9 | 230 | roiConfig.UserRois[1].BotRightX = 9; |
charlesmn | 0:e91189a84ad9 | 231 | roiConfig.UserRois[1].BotRightY = 4; |
charlesmn | 0:e91189a84ad9 | 232 | roiConfig.UserRois[2].TopLeftX = 11; |
charlesmn | 0:e91189a84ad9 | 233 | roiConfig.UserRois[2].TopLeftY = 9; |
charlesmn | 0:e91189a84ad9 | 234 | roiConfig.UserRois[2].BotRightX = 15; |
charlesmn | 0:e91189a84ad9 | 235 | roiConfig.UserRois[2].BotRightY = 5; |
charlesmn | 0:e91189a84ad9 | 236 | status = Sensor->vl53L1_SetROI( &roiConfig); |
charlesmn | 0:e91189a84ad9 | 237 | |
charlesmn | 0:e91189a84ad9 | 238 | |
charlesmn | 0:e91189a84ad9 | 239 | status = Sensor->vl53L1_SetDistanceMode( VL53L1_DISTANCEMODE_LONG); |
charlesmn | 0:e91189a84ad9 | 240 | // status = Sensor->VL53L1_SetMeasurementTimingBudgetMicroSeconds( 100 * 500); // error -21 is because of this. Don't know why |
johnAlexander | 2:25bcfa4b1aca | 241 | */ |
johnAlexander | 2:25bcfa4b1aca | 242 | |
johnAlexander | 2:25bcfa4b1aca | 243 | |
johnAlexander | 2:25bcfa4b1aca | 244 | /* start the measure on the center sensor */ |
johnAlexander | 2:25bcfa4b1aca | 245 | if (NULL != board->sensor_centre) { |
johnAlexander | 2:25bcfa4b1aca | 246 | installedSensors[sensorCnt] = 'C'; |
johnAlexander | 2:25bcfa4b1aca | 247 | status = board->sensor_centre->stop_measurement(); |
johnAlexander | 2:25bcfa4b1aca | 248 | if (status != 0) { |
johnAlexander | 2:25bcfa4b1aca | 249 | return status; |
johnAlexander | 2:25bcfa4b1aca | 250 | } |
johnAlexander | 2:25bcfa4b1aca | 251 | status = board->sensor_centre->start_measurement(&sensor_centre_irq); |
johnAlexander | 2:25bcfa4b1aca | 252 | if (status != 0) { |
johnAlexander | 2:25bcfa4b1aca | 253 | return status; |
johnAlexander | 2:25bcfa4b1aca | 254 | } |
johnAlexander | 2:25bcfa4b1aca | 255 | ++sensorCnt; |
johnAlexander | 2:25bcfa4b1aca | 256 | } |
johnAlexander | 2:25bcfa4b1aca | 257 | /* start the measure on the left sensor */ |
johnAlexander | 2:25bcfa4b1aca | 258 | if (NULL != board->sensor_left) { |
johnAlexander | 2:25bcfa4b1aca | 259 | installedSensors[sensorCnt] = 'L'; |
johnAlexander | 2:25bcfa4b1aca | 260 | status = board->sensor_left->stop_measurement(); |
johnAlexander | 2:25bcfa4b1aca | 261 | if (status != 0) { |
johnAlexander | 2:25bcfa4b1aca | 262 | return status; |
johnAlexander | 2:25bcfa4b1aca | 263 | } |
johnAlexander | 2:25bcfa4b1aca | 264 | status = board->sensor_left->start_measurement(&sensor_left_irq); |
johnAlexander | 2:25bcfa4b1aca | 265 | if (status != 0) { |
johnAlexander | 2:25bcfa4b1aca | 266 | return status; |
johnAlexander | 2:25bcfa4b1aca | 267 | } |
johnAlexander | 2:25bcfa4b1aca | 268 | ++sensorCnt; |
johnAlexander | 2:25bcfa4b1aca | 269 | } |
johnAlexander | 2:25bcfa4b1aca | 270 | /* start the measure on the right sensor */ |
johnAlexander | 2:25bcfa4b1aca | 271 | if (NULL != board->sensor_right) { |
johnAlexander | 2:25bcfa4b1aca | 272 | installedSensors[sensorCnt] = 'R'; |
johnAlexander | 2:25bcfa4b1aca | 273 | status = board->sensor_right->stop_measurement(); |
johnAlexander | 2:25bcfa4b1aca | 274 | if (status != 0) { |
johnAlexander | 2:25bcfa4b1aca | 275 | return status; |
johnAlexander | 2:25bcfa4b1aca | 276 | } |
johnAlexander | 2:25bcfa4b1aca | 277 | status = board->sensor_right->start_measurement(&sensor_right_irq); |
johnAlexander | 2:25bcfa4b1aca | 278 | if (status != 0) { |
johnAlexander | 2:25bcfa4b1aca | 279 | return status; |
johnAlexander | 2:25bcfa4b1aca | 280 | } |
johnAlexander | 2:25bcfa4b1aca | 281 | ++sensorCnt; |
johnAlexander | 2:25bcfa4b1aca | 282 | } |
johnAlexander | 2:25bcfa4b1aca | 283 | currentSensor = 0; |
johnAlexander | 2:25bcfa4b1aca | 284 | return status; |
johnAlexander | 2:25bcfa4b1aca | 285 | } |
johnAlexander | 2:25bcfa4b1aca | 286 | |
johnAlexander | 2:25bcfa4b1aca | 287 | |
johnAlexander | 2:25bcfa4b1aca | 288 | |
johnAlexander | 2:25bcfa4b1aca | 289 | /*=================================== Main ================================== |
johnAlexander | 2:25bcfa4b1aca | 290 | =============================================================================*/ |
johnAlexander | 2:25bcfa4b1aca | 291 | int main() |
johnAlexander | 2:25bcfa4b1aca | 292 | { |
johnAlexander | 2:25bcfa4b1aca | 293 | int status; |
johnAlexander | 2:25bcfa4b1aca | 294 | |
johnAlexander | 2:25bcfa4b1aca | 295 | stop_button.rise(&switch_measuring_sensor_irq); |
johnAlexander | 2:25bcfa4b1aca | 296 | stop_button.enable_irq(); |
johnAlexander | 2:25bcfa4b1aca | 297 | |
johnAlexander | 2:25bcfa4b1aca | 298 | // pc.baud(115200); // baud rate is important as printf statements take a lot of time |
johnAlexander | 2:25bcfa4b1aca | 299 | |
johnAlexander | 2:25bcfa4b1aca | 300 | printf("mbed version : %d \r\n", MBED_VERSION); |
johnAlexander | 2:25bcfa4b1aca | 301 | |
johnAlexander | 2:25bcfa4b1aca | 302 | // create i2c interface |
johnAlexander | 2:25bcfa4b1aca | 303 | ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL); |
johnAlexander | 2:25bcfa4b1aca | 304 | /* creates the 53L1A2 expansion board singleton obj */ |
johnAlexander | 2:25bcfa4b1aca | 305 | board = XNucleo53L1A2::instance(dev_I2C, CentreIntPin, LeftIntPin, RightIntPin); |
johnAlexander | 2:25bcfa4b1aca | 306 | |
johnAlexander | 2:25bcfa4b1aca | 307 | printf("board created!\r\n"); |
johnAlexander | 2:25bcfa4b1aca | 308 | |
johnAlexander | 2:25bcfa4b1aca | 309 | /* init the 53L1A1 expansion board with default values */ |
johnAlexander | 2:25bcfa4b1aca | 310 | status = board->init_board(); |
johnAlexander | 2:25bcfa4b1aca | 311 | if (status) { |
johnAlexander | 2:25bcfa4b1aca | 312 | printf("Failed to init board!\r\n"); |
johnAlexander | 2:25bcfa4b1aca | 313 | return status; |
charlesmn | 0:e91189a84ad9 | 314 | } |
charlesmn | 0:e91189a84ad9 | 315 | |
johnAlexander | 2:25bcfa4b1aca | 316 | printf("board initiated! - %d\r\n", status); |
charlesmn | 0:e91189a84ad9 | 317 | |
johnAlexander | 2:25bcfa4b1aca | 318 | /* init an array with chars to id the sensors */ |
johnAlexander | 2:25bcfa4b1aca | 319 | status = init_sensors_array(); |
johnAlexander | 2:25bcfa4b1aca | 320 | if (status != 0) { |
johnAlexander | 2:25bcfa4b1aca | 321 | printf("Failed to init sensors!\r\n"); |
johnAlexander | 2:25bcfa4b1aca | 322 | return status; |
charlesmn | 0:e91189a84ad9 | 323 | } |
charlesmn | 0:e91189a84ad9 | 324 | |
johnAlexander | 2:25bcfa4b1aca | 325 | printf("loop forever\n"); |
charlesmn | 0:e91189a84ad9 | 326 | |
johnAlexander | 2:25bcfa4b1aca | 327 | /* |
charlesmn | 0:e91189a84ad9 | 328 | // loop waiting for interrupts to happen. This is signaled by int_centre_result,int_left_result or int_right_result |
charlesmn | 0:e91189a84ad9 | 329 | // being non zero. When the interrupts clear this is signaled by int_centre_dropped,int_left_dropped and int_right_dropped. |
charlesmn | 0:e91189a84ad9 | 330 | // These are set back to zero when processing is completed |
charlesmn | 0:e91189a84ad9 | 331 | while (1) |
charlesmn | 0:e91189a84ad9 | 332 | { |
charlesmn | 0:e91189a84ad9 | 333 | VL53L1_MultiRangingData_t MultiRangingData; |
charlesmn | 0:e91189a84ad9 | 334 | VL53L1_MultiRangingData_t *pMultiRangingData = &MultiRangingData; |
charlesmn | 0:e91189a84ad9 | 335 | |
charlesmn | 0:e91189a84ad9 | 336 | if ( int_left_dropped || int_centre_dropped || int_right_result) |
charlesmn | 0:e91189a84ad9 | 337 | wait_ms(20); |
charlesmn | 0:e91189a84ad9 | 338 | |
charlesmn | 0:e91189a84ad9 | 339 | |
charlesmn | 0:e91189a84ad9 | 340 | // when the interrupt pin goes low start new measurement |
charlesmn | 0:e91189a84ad9 | 341 | if ( int_centre_dropped != 0) |
charlesmn | 0:e91189a84ad9 | 342 | { |
charlesmn | 0:e91189a84ad9 | 343 | int_centre_dropped = 0; |
charlesmn | 0:e91189a84ad9 | 344 | status = board->sensor_centre->vl53L1_ClearInterruptAndStartMeasurement();// get next measurement |
charlesmn | 0:e91189a84ad9 | 345 | } |
charlesmn | 0:e91189a84ad9 | 346 | |
charlesmn | 0:e91189a84ad9 | 347 | if ( int_left_dropped != 0) |
charlesmn | 0:e91189a84ad9 | 348 | { |
charlesmn | 0:e91189a84ad9 | 349 | int_left_dropped = 0; |
charlesmn | 0:e91189a84ad9 | 350 | status = board->sensor_left->vl53L1_ClearInterruptAndStartMeasurement(); |
charlesmn | 0:e91189a84ad9 | 351 | } |
charlesmn | 0:e91189a84ad9 | 352 | |
charlesmn | 0:e91189a84ad9 | 353 | if ( int_right_dropped != 0) |
charlesmn | 0:e91189a84ad9 | 354 | { |
charlesmn | 0:e91189a84ad9 | 355 | int_right_dropped = 0; |
charlesmn | 0:e91189a84ad9 | 356 | status = board->sensor_right->vl53L1_ClearInterruptAndStartMeasurement(); |
charlesmn | 0:e91189a84ad9 | 357 | } |
charlesmn | 0:e91189a84ad9 | 358 | |
charlesmn | 0:e91189a84ad9 | 359 | if (int_right_result != 0) // interrupt seen on right sensor |
charlesmn | 0:e91189a84ad9 | 360 | { |
charlesmn | 0:e91189a84ad9 | 361 | status = board->sensor_right->vl53L1_GetMultiRangingData( pMultiRangingData); |
charlesmn | 0:e91189a84ad9 | 362 | if ( status == 0) |
charlesmn | 0:e91189a84ad9 | 363 | { |
charlesmn | 0:e91189a84ad9 | 364 | print_results( devRight.i2c_slave_address, pMultiRangingData ); |
charlesmn | 0:e91189a84ad9 | 365 | } |
charlesmn | 0:e91189a84ad9 | 366 | |
charlesmn | 0:e91189a84ad9 | 367 | // clear interrupt flag |
charlesmn | 0:e91189a84ad9 | 368 | int_right_result = 0; |
charlesmn | 0:e91189a84ad9 | 369 | } |
charlesmn | 0:e91189a84ad9 | 370 | |
charlesmn | 0:e91189a84ad9 | 371 | |
charlesmn | 0:e91189a84ad9 | 372 | if (int_left_result != 0) // interrupt seen on left sensor |
charlesmn | 0:e91189a84ad9 | 373 | { |
charlesmn | 0:e91189a84ad9 | 374 | status = board->sensor_left->vl53L1_GetMultiRangingData(pMultiRangingData); |
charlesmn | 0:e91189a84ad9 | 375 | if ( status == 0) |
charlesmn | 0:e91189a84ad9 | 376 | { |
charlesmn | 0:e91189a84ad9 | 377 | print_results( devLeft.i2c_slave_address, pMultiRangingData ); |
charlesmn | 0:e91189a84ad9 | 378 | } |
charlesmn | 0:e91189a84ad9 | 379 | |
charlesmn | 0:e91189a84ad9 | 380 | // clear interrupt flag |
charlesmn | 0:e91189a84ad9 | 381 | int_left_result = 0; |
charlesmn | 0:e91189a84ad9 | 382 | } |
charlesmn | 0:e91189a84ad9 | 383 | |
charlesmn | 0:e91189a84ad9 | 384 | if (int_centre_result != 0) |
charlesmn | 0:e91189a84ad9 | 385 | { |
charlesmn | 0:e91189a84ad9 | 386 | status = board->sensor_centre->vl53L1_GetMultiRangingData( pMultiRangingData); |
charlesmn | 0:e91189a84ad9 | 387 | if(status==0) { |
charlesmn | 0:e91189a84ad9 | 388 | print_results(devCentre.i2c_slave_address, pMultiRangingData ); |
charlesmn | 0:e91189a84ad9 | 389 | } //if(status==0) |
charlesmn | 0:e91189a84ad9 | 390 | |
charlesmn | 0:e91189a84ad9 | 391 | // clear interrupt flag |
charlesmn | 0:e91189a84ad9 | 392 | int_centre_result = 0; |
charlesmn | 0:e91189a84ad9 | 393 | |
charlesmn | 0:e91189a84ad9 | 394 | } |
charlesmn | 0:e91189a84ad9 | 395 | |
charlesmn | 0:e91189a84ad9 | 396 | wait_ms( 1 * 5); |
charlesmn | 0:e91189a84ad9 | 397 | |
charlesmn | 0:e91189a84ad9 | 398 | } |
johnAlexander | 2:25bcfa4b1aca | 399 | */ |
johnAlexander | 2:25bcfa4b1aca | 400 | /* Main ranging interrupt loop */ |
johnAlexander | 2:25bcfa4b1aca | 401 | while (true) { |
johnAlexander | 2:25bcfa4b1aca | 402 | measure_sensors(); |
johnAlexander | 2:25bcfa4b1aca | 403 | if (switchChanged) { |
johnAlexander | 2:25bcfa4b1aca | 404 | ++currentSensor; |
johnAlexander | 2:25bcfa4b1aca | 405 | if (currentSensor == sensorCnt) |
johnAlexander | 2:25bcfa4b1aca | 406 | currentSensor = 0; |
johnAlexander | 2:25bcfa4b1aca | 407 | printf("Sensor changed to %c\r\n", installedSensors[currentSensor]); |
johnAlexander | 2:25bcfa4b1aca | 408 | switchChanged = false; |
johnAlexander | 2:25bcfa4b1aca | 409 | stop_button.enable_irq(); |
johnAlexander | 2:25bcfa4b1aca | 410 | } |
johnAlexander | 2:25bcfa4b1aca | 411 | } |
charlesmn | 0:e91189a84ad9 | 412 | } |
charlesmn | 0:e91189a84ad9 | 413 | |
charlesmn | 0:e91189a84ad9 | 414 | |
charlesmn | 0:e91189a84ad9 | 415 | |
charlesmn | 0:e91189a84ad9 | 416 | // print out what data is required |
charlesmn | 0:e91189a84ad9 | 417 | void print_results( int devSpiNumber, VL53L1_MultiRangingData_t *pMultiRangingData ) |
charlesmn | 0:e91189a84ad9 | 418 | { |
charlesmn | 0:e91189a84ad9 | 419 | int no_of_object_found=pMultiRangingData->NumberOfObjectsFound; |
charlesmn | 0:e91189a84ad9 | 420 | |
charlesmn | 0:e91189a84ad9 | 421 | int RoiNumber=pMultiRangingData->RoiNumber; |
charlesmn | 0:e91189a84ad9 | 422 | int RoiStatus=pMultiRangingData->RoiStatus; |
charlesmn | 0:e91189a84ad9 | 423 | |
charlesmn | 0:e91189a84ad9 | 424 | if (( no_of_object_found < 10 ) && ( no_of_object_found != 0)) |
charlesmn | 0:e91189a84ad9 | 425 | { |
charlesmn | 0:e91189a84ad9 | 426 | for(int j=0;j<no_of_object_found;j++){ |
charlesmn | 0:e91189a84ad9 | 427 | if ((pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) || |
charlesmn | 0:e91189a84ad9 | 428 | (pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL)) |
charlesmn | 0:e91189a84ad9 | 429 | { |
charlesmn | 0:e91189a84ad9 | 430 | printf("\t spiAddr=%d \t RoiNumber=%d \t D=%5dmm \n", |
charlesmn | 0:e91189a84ad9 | 431 | devSpiNumber, |
charlesmn | 0:e91189a84ad9 | 432 | RoiNumber, |
charlesmn | 0:e91189a84ad9 | 433 | pMultiRangingData->RangeData[j].RangeMilliMeter); |
charlesmn | 0:e91189a84ad9 | 434 | } |
charlesmn | 0:e91189a84ad9 | 435 | else |
charlesmn | 0:e91189a84ad9 | 436 | { |
charlesmn | 0:e91189a84ad9 | 437 | // printf("RangeStatus %d %d\n",j, pMultiRangingData->RangeData[j].RangeStatus); |
charlesmn | 0:e91189a84ad9 | 438 | } |
charlesmn | 0:e91189a84ad9 | 439 | } |
charlesmn | 0:e91189a84ad9 | 440 | } // if (( no_of_object_found < 10 ) && ( no_of_object_found != 0)) |
charlesmn | 0:e91189a84ad9 | 441 | else |
charlesmn | 0:e91189a84ad9 | 442 | { |
charlesmn | 0:e91189a84ad9 | 443 | // printf("no_of_object_found %d \n",no_of_object_found); |
charlesmn | 0:e91189a84ad9 | 444 | } |
charlesmn | 0:e91189a84ad9 | 445 | |
charlesmn | 0:e91189a84ad9 | 446 | } |
charlesmn | 0:e91189a84ad9 | 447 | |
charlesmn | 0:e91189a84ad9 | 448 | |
charlesmn | 0:e91189a84ad9 | 449 | |
charlesmn | 0:e91189a84ad9 | 450 | #if (MBED_VERSION > 60300) |
charlesmn | 0:e91189a84ad9 | 451 | void wait_ms(int ms) |
charlesmn | 0:e91189a84ad9 | 452 | { |
charlesmn | 0:e91189a84ad9 | 453 | thread_sleep_for(ms); |
charlesmn | 0:e91189a84ad9 | 454 | } |
charlesmn | 0:e91189a84ad9 | 455 | #endif |
charlesmn | 0:e91189a84ad9 | 456 |