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@4:738d308ed16e, 2021-06-13 (annotated)
- Committer:
- charlesmn
- Date:
- Sun Jun 13 14:14:07 2021 +0000
- Revision:
- 4:738d308ed16e
- Parent:
- 3:09f23aad108a
Compile with VL53L1CB class
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 | 4:738d308ed16e | 76 | void process_interrupt( VL53L1CB * 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 | 3:09f23aad108a | 139 | |
johnAlexander | 2:25bcfa4b1aca | 140 | uint16_t distance = 0; |
johnAlexander | 3:09f23aad108a | 141 | |
johnAlexander | 3:09f23aad108a | 142 | VL53L1_MultiRangingData_t MultiRangingData; |
johnAlexander | 3:09f23aad108a | 143 | VL53L1_MultiRangingData_t *pMultiRangingData = &MultiRangingData; |
johnAlexander | 2:25bcfa4b1aca | 144 | |
johnAlexander | 2:25bcfa4b1aca | 145 | /* Handle the interrupt and output the range from the centre sensor */ |
johnAlexander | 2:25bcfa4b1aca | 146 | if (centerSensor) { |
johnAlexander | 2:25bcfa4b1aca | 147 | centerSensor = false; |
johnAlexander | 3:09f23aad108a | 148 | |
charlesmn | 4:738d308ed16e | 149 | status = board->sensor_centre->VL53L1CB_GetDistance(&distance); |
charlesmn | 0:e91189a84ad9 | 150 | |
johnAlexander | 2:25bcfa4b1aca | 151 | current = (currentSensor == 0); |
johnAlexander | 2:25bcfa4b1aca | 152 | if (current) { |
johnAlexander | 2:25bcfa4b1aca | 153 | printf("Centre: %d\r\n", distance); |
johnAlexander | 2:25bcfa4b1aca | 154 | } |
johnAlexander | 3:09f23aad108a | 155 | |
charlesmn | 4:738d308ed16e | 156 | status = board->sensor_centre->VL53L1CB_ClearInterrupt(); |
johnAlexander | 3:09f23aad108a | 157 | board->sensor_centre->enable_interrupt_measure_detection_irq(); |
johnAlexander | 3:09f23aad108a | 158 | |
charlesmn | 0:e91189a84ad9 | 159 | } |
charlesmn | 0:e91189a84ad9 | 160 | |
johnAlexander | 2:25bcfa4b1aca | 161 | /* Handle the interrupt and output the range from the left sensor */ |
johnAlexander | 2:25bcfa4b1aca | 162 | if (leftSensor) { |
johnAlexander | 2:25bcfa4b1aca | 163 | leftSensor = false; |
johnAlexander | 3:09f23aad108a | 164 | |
charlesmn | 4:738d308ed16e | 165 | status = board->sensor_left->VL53L1CB_GetDistance(&distance); |
johnAlexander | 2:25bcfa4b1aca | 166 | |
johnAlexander | 2:25bcfa4b1aca | 167 | current = (installedSensors[currentSensor] == 'L'); |
johnAlexander | 2:25bcfa4b1aca | 168 | if (current) { |
johnAlexander | 2:25bcfa4b1aca | 169 | printf("Left: %d\r\n", distance); |
johnAlexander | 2:25bcfa4b1aca | 170 | } |
charlesmn | 4:738d308ed16e | 171 | status = board->sensor_left->VL53L1CB_ClearInterrupt(); |
johnAlexander | 3:09f23aad108a | 172 | board->sensor_left->enable_interrupt_measure_detection_irq(); |
johnAlexander | 3:09f23aad108a | 173 | |
johnAlexander | 2:25bcfa4b1aca | 174 | } |
charlesmn | 0:e91189a84ad9 | 175 | |
johnAlexander | 2:25bcfa4b1aca | 176 | /* Handle the interrupt and output the range from the right sensor */ |
johnAlexander | 2:25bcfa4b1aca | 177 | if (rightSensor) { |
johnAlexander | 2:25bcfa4b1aca | 178 | rightSensor = false; |
johnAlexander | 3:09f23aad108a | 179 | |
charlesmn | 4:738d308ed16e | 180 | status = board->sensor_right->VL53L1CB_GetDistance(&distance); |
johnAlexander | 2:25bcfa4b1aca | 181 | |
johnAlexander | 2:25bcfa4b1aca | 182 | current = (installedSensors[currentSensor] == 'R'); |
johnAlexander | 2:25bcfa4b1aca | 183 | if (current) { |
johnAlexander | 2:25bcfa4b1aca | 184 | printf("Right: %d\r\n", distance); |
johnAlexander | 2:25bcfa4b1aca | 185 | } |
charlesmn | 4:738d308ed16e | 186 | status = board->sensor_right->VL53L1CB_ClearInterrupt(); |
johnAlexander | 3:09f23aad108a | 187 | board->sensor_right->enable_interrupt_measure_detection_irq(); |
johnAlexander | 3:09f23aad108a | 188 | |
johnAlexander | 2:25bcfa4b1aca | 189 | } |
johnAlexander | 2:25bcfa4b1aca | 190 | } |
charlesmn | 0:e91189a84ad9 | 191 | |
charlesmn | 4:738d308ed16e | 192 | int configure_sensor(VL53L1CB *Sensor) |
johnAlexander | 3:09f23aad108a | 193 | { |
johnAlexander | 3:09f23aad108a | 194 | int status = 0; |
johnAlexander | 3:09f23aad108a | 195 | |
johnAlexander | 3:09f23aad108a | 196 | // Device Initialization and setting |
charlesmn | 4:738d308ed16e | 197 | status = Sensor->VL53L1CB_DataInit(); |
charlesmn | 4:738d308ed16e | 198 | status = Sensor->VL53L1CB_StaticInit(); |
charlesmn | 4:738d308ed16e | 199 | status = Sensor->VL53L1CB_SetPresetMode( VL53L1_PRESETMODE_MULTIZONES_SCANNING); |
johnAlexander | 3:09f23aad108a | 200 | |
johnAlexander | 3:09f23aad108a | 201 | //configure the regions of interest for each sensor |
johnAlexander | 3:09f23aad108a | 202 | VL53L1_RoiConfig_t roiConfig; |
johnAlexander | 3:09f23aad108a | 203 | |
johnAlexander | 3:09f23aad108a | 204 | roiConfig.NumberOfRoi = 3; |
johnAlexander | 3:09f23aad108a | 205 | roiConfig.UserRois[0].TopLeftX = 0; |
johnAlexander | 3:09f23aad108a | 206 | roiConfig.UserRois[0].TopLeftY = 9; |
johnAlexander | 3:09f23aad108a | 207 | roiConfig.UserRois[0].BotRightX = 4; |
johnAlexander | 3:09f23aad108a | 208 | roiConfig.UserRois[0].BotRightY = 5; |
johnAlexander | 3:09f23aad108a | 209 | roiConfig.UserRois[1].TopLeftX = 5; |
johnAlexander | 3:09f23aad108a | 210 | roiConfig.UserRois[1].TopLeftY = 9; |
johnAlexander | 3:09f23aad108a | 211 | roiConfig.UserRois[1].BotRightX = 9; |
johnAlexander | 3:09f23aad108a | 212 | roiConfig.UserRois[1].BotRightY = 4; |
johnAlexander | 3:09f23aad108a | 213 | roiConfig.UserRois[2].TopLeftX = 11; |
johnAlexander | 3:09f23aad108a | 214 | roiConfig.UserRois[2].TopLeftY = 9; |
johnAlexander | 3:09f23aad108a | 215 | roiConfig.UserRois[2].BotRightX = 15; |
johnAlexander | 3:09f23aad108a | 216 | roiConfig.UserRois[2].BotRightY = 5; |
charlesmn | 4:738d308ed16e | 217 | status = Sensor->VL53L1CB_SetROI( &roiConfig); |
johnAlexander | 3:09f23aad108a | 218 | |
johnAlexander | 3:09f23aad108a | 219 | |
charlesmn | 4:738d308ed16e | 220 | status = Sensor->VL53L1CB_SetDistanceMode( VL53L1_DISTANCEMODE_LONG); |
johnAlexander | 3:09f23aad108a | 221 | // status = Sensor->VL53L1_SetMeasurementTimingBudgetMicroSeconds( 100 * 500); // error -21 is because of this. Don't know why |
johnAlexander | 3:09f23aad108a | 222 | |
johnAlexander | 3:09f23aad108a | 223 | return status; |
johnAlexander | 3:09f23aad108a | 224 | } |
johnAlexander | 3:09f23aad108a | 225 | |
johnAlexander | 2:25bcfa4b1aca | 226 | /* |
johnAlexander | 2:25bcfa4b1aca | 227 | * Add to an array a character that represents the sensor and start ranging |
johnAlexander | 2:25bcfa4b1aca | 228 | */ |
johnAlexander | 2:25bcfa4b1aca | 229 | int init_sensors_array() |
johnAlexander | 2:25bcfa4b1aca | 230 | { |
johnAlexander | 2:25bcfa4b1aca | 231 | int status = 0; |
charlesmn | 4:738d308ed16e | 232 | VL53L1CB *Sensor; |
charlesmn | 0:e91189a84ad9 | 233 | uint8_t ToFSensor = 1; // 0=Left, 1=Center(default), 2=Right |
charlesmn | 0:e91189a84ad9 | 234 | |
johnAlexander | 2:25bcfa4b1aca | 235 | sensorCnt = 0; |
charlesmn | 0:e91189a84ad9 | 236 | |
johnAlexander | 2:25bcfa4b1aca | 237 | if (board->sensor_centre != NULL) { |
johnAlexander | 2:25bcfa4b1aca | 238 | Dev = &devCentre; |
johnAlexander | 2:25bcfa4b1aca | 239 | Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS; |
johnAlexander | 3:09f23aad108a | 240 | status = configure_sensor(board->sensor_centre); |
johnAlexander | 2:25bcfa4b1aca | 241 | printf("configuring centre channel \n"); |
charlesmn | 0:e91189a84ad9 | 242 | } |
johnAlexander | 2:25bcfa4b1aca | 243 | |
johnAlexander | 2:25bcfa4b1aca | 244 | if (board->sensor_left != NULL) { |
johnAlexander | 2:25bcfa4b1aca | 245 | Dev = &devLeft; |
johnAlexander | 2:25bcfa4b1aca | 246 | Dev->i2c_slave_address = NEW_SENSOR_LEFT_ADDRESS; |
johnAlexander | 3:09f23aad108a | 247 | status = configure_sensor(board->sensor_left); |
johnAlexander | 2:25bcfa4b1aca | 248 | printf("configuring left channel \n"); |
johnAlexander | 2:25bcfa4b1aca | 249 | } |
johnAlexander | 2:25bcfa4b1aca | 250 | |
johnAlexander | 2:25bcfa4b1aca | 251 | if (board->sensor_right != NULL) { |
johnAlexander | 2:25bcfa4b1aca | 252 | Dev = &devRight; |
johnAlexander | 2:25bcfa4b1aca | 253 | Dev->i2c_slave_address = NEW_SENSOR_RIGHT_ADDRESS; |
johnAlexander | 3:09f23aad108a | 254 | status = configure_sensor(board->sensor_right); |
johnAlexander | 2:25bcfa4b1aca | 255 | printf("configuring right channel \n"); |
johnAlexander | 2:25bcfa4b1aca | 256 | } |
johnAlexander | 2:25bcfa4b1aca | 257 | |
johnAlexander | 2:25bcfa4b1aca | 258 | /* start the measure on the center sensor */ |
johnAlexander | 2:25bcfa4b1aca | 259 | if (NULL != board->sensor_centre) { |
johnAlexander | 2:25bcfa4b1aca | 260 | installedSensors[sensorCnt] = 'C'; |
johnAlexander | 2:25bcfa4b1aca | 261 | status = board->sensor_centre->stop_measurement(); |
johnAlexander | 2:25bcfa4b1aca | 262 | if (status != 0) { |
johnAlexander | 2:25bcfa4b1aca | 263 | return status; |
johnAlexander | 2:25bcfa4b1aca | 264 | } |
johnAlexander | 2:25bcfa4b1aca | 265 | status = board->sensor_centre->start_measurement(&sensor_centre_irq); |
johnAlexander | 2:25bcfa4b1aca | 266 | if (status != 0) { |
johnAlexander | 2:25bcfa4b1aca | 267 | return status; |
johnAlexander | 2:25bcfa4b1aca | 268 | } |
johnAlexander | 2:25bcfa4b1aca | 269 | ++sensorCnt; |
johnAlexander | 2:25bcfa4b1aca | 270 | } |
johnAlexander | 2:25bcfa4b1aca | 271 | /* start the measure on the left sensor */ |
johnAlexander | 2:25bcfa4b1aca | 272 | if (NULL != board->sensor_left) { |
johnAlexander | 2:25bcfa4b1aca | 273 | installedSensors[sensorCnt] = 'L'; |
johnAlexander | 2:25bcfa4b1aca | 274 | status = board->sensor_left->stop_measurement(); |
johnAlexander | 2:25bcfa4b1aca | 275 | if (status != 0) { |
johnAlexander | 2:25bcfa4b1aca | 276 | return status; |
johnAlexander | 2:25bcfa4b1aca | 277 | } |
johnAlexander | 2:25bcfa4b1aca | 278 | status = board->sensor_left->start_measurement(&sensor_left_irq); |
johnAlexander | 2:25bcfa4b1aca | 279 | if (status != 0) { |
johnAlexander | 2:25bcfa4b1aca | 280 | return status; |
johnAlexander | 2:25bcfa4b1aca | 281 | } |
johnAlexander | 2:25bcfa4b1aca | 282 | ++sensorCnt; |
johnAlexander | 2:25bcfa4b1aca | 283 | } |
johnAlexander | 2:25bcfa4b1aca | 284 | /* start the measure on the right sensor */ |
johnAlexander | 2:25bcfa4b1aca | 285 | if (NULL != board->sensor_right) { |
johnAlexander | 2:25bcfa4b1aca | 286 | installedSensors[sensorCnt] = 'R'; |
johnAlexander | 2:25bcfa4b1aca | 287 | status = board->sensor_right->stop_measurement(); |
johnAlexander | 2:25bcfa4b1aca | 288 | if (status != 0) { |
johnAlexander | 2:25bcfa4b1aca | 289 | return status; |
johnAlexander | 2:25bcfa4b1aca | 290 | } |
johnAlexander | 2:25bcfa4b1aca | 291 | status = board->sensor_right->start_measurement(&sensor_right_irq); |
johnAlexander | 2:25bcfa4b1aca | 292 | if (status != 0) { |
johnAlexander | 2:25bcfa4b1aca | 293 | return status; |
johnAlexander | 2:25bcfa4b1aca | 294 | } |
johnAlexander | 2:25bcfa4b1aca | 295 | ++sensorCnt; |
johnAlexander | 2:25bcfa4b1aca | 296 | } |
johnAlexander | 2:25bcfa4b1aca | 297 | currentSensor = 0; |
johnAlexander | 2:25bcfa4b1aca | 298 | return status; |
johnAlexander | 2:25bcfa4b1aca | 299 | } |
johnAlexander | 2:25bcfa4b1aca | 300 | |
johnAlexander | 2:25bcfa4b1aca | 301 | |
johnAlexander | 2:25bcfa4b1aca | 302 | |
johnAlexander | 2:25bcfa4b1aca | 303 | /*=================================== Main ================================== |
johnAlexander | 2:25bcfa4b1aca | 304 | =============================================================================*/ |
johnAlexander | 2:25bcfa4b1aca | 305 | int main() |
johnAlexander | 2:25bcfa4b1aca | 306 | { |
johnAlexander | 2:25bcfa4b1aca | 307 | int status; |
johnAlexander | 2:25bcfa4b1aca | 308 | |
johnAlexander | 2:25bcfa4b1aca | 309 | stop_button.rise(&switch_measuring_sensor_irq); |
johnAlexander | 2:25bcfa4b1aca | 310 | stop_button.enable_irq(); |
johnAlexander | 2:25bcfa4b1aca | 311 | |
johnAlexander | 3:09f23aad108a | 312 | pc.baud(115200); // baud rate is important as printf statements take a lot of time |
johnAlexander | 2:25bcfa4b1aca | 313 | |
johnAlexander | 2:25bcfa4b1aca | 314 | printf("mbed version : %d \r\n", MBED_VERSION); |
johnAlexander | 2:25bcfa4b1aca | 315 | |
johnAlexander | 2:25bcfa4b1aca | 316 | // create i2c interface |
johnAlexander | 2:25bcfa4b1aca | 317 | ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL); |
johnAlexander | 2:25bcfa4b1aca | 318 | /* creates the 53L1A2 expansion board singleton obj */ |
johnAlexander | 2:25bcfa4b1aca | 319 | board = XNucleo53L1A2::instance(dev_I2C, CentreIntPin, LeftIntPin, RightIntPin); |
johnAlexander | 2:25bcfa4b1aca | 320 | |
johnAlexander | 2:25bcfa4b1aca | 321 | printf("board created!\r\n"); |
johnAlexander | 2:25bcfa4b1aca | 322 | |
johnAlexander | 2:25bcfa4b1aca | 323 | /* init the 53L1A1 expansion board with default values */ |
johnAlexander | 2:25bcfa4b1aca | 324 | status = board->init_board(); |
johnAlexander | 2:25bcfa4b1aca | 325 | if (status) { |
johnAlexander | 2:25bcfa4b1aca | 326 | printf("Failed to init board!\r\n"); |
johnAlexander | 2:25bcfa4b1aca | 327 | return status; |
charlesmn | 0:e91189a84ad9 | 328 | } |
charlesmn | 0:e91189a84ad9 | 329 | |
johnAlexander | 2:25bcfa4b1aca | 330 | printf("board initiated! - %d\r\n", status); |
charlesmn | 0:e91189a84ad9 | 331 | |
johnAlexander | 2:25bcfa4b1aca | 332 | /* init an array with chars to id the sensors */ |
johnAlexander | 2:25bcfa4b1aca | 333 | status = init_sensors_array(); |
johnAlexander | 2:25bcfa4b1aca | 334 | if (status != 0) { |
johnAlexander | 2:25bcfa4b1aca | 335 | printf("Failed to init sensors!\r\n"); |
johnAlexander | 2:25bcfa4b1aca | 336 | return status; |
charlesmn | 0:e91189a84ad9 | 337 | } |
charlesmn | 0:e91189a84ad9 | 338 | |
johnAlexander | 2:25bcfa4b1aca | 339 | printf("loop forever\n"); |
charlesmn | 0:e91189a84ad9 | 340 | |
johnAlexander | 2:25bcfa4b1aca | 341 | /* Main ranging interrupt loop */ |
johnAlexander | 2:25bcfa4b1aca | 342 | while (true) { |
johnAlexander | 3:09f23aad108a | 343 | |
johnAlexander | 2:25bcfa4b1aca | 344 | measure_sensors(); |
johnAlexander | 3:09f23aad108a | 345 | |
johnAlexander | 2:25bcfa4b1aca | 346 | if (switchChanged) { |
johnAlexander | 2:25bcfa4b1aca | 347 | ++currentSensor; |
johnAlexander | 2:25bcfa4b1aca | 348 | if (currentSensor == sensorCnt) |
johnAlexander | 2:25bcfa4b1aca | 349 | currentSensor = 0; |
johnAlexander | 2:25bcfa4b1aca | 350 | printf("Sensor changed to %c\r\n", installedSensors[currentSensor]); |
johnAlexander | 2:25bcfa4b1aca | 351 | switchChanged = false; |
johnAlexander | 2:25bcfa4b1aca | 352 | stop_button.enable_irq(); |
johnAlexander | 2:25bcfa4b1aca | 353 | } |
johnAlexander | 2:25bcfa4b1aca | 354 | } |
charlesmn | 0:e91189a84ad9 | 355 | } |
charlesmn | 0:e91189a84ad9 | 356 | |
charlesmn | 0:e91189a84ad9 | 357 | |
charlesmn | 0:e91189a84ad9 | 358 | |
charlesmn | 0:e91189a84ad9 | 359 | // print out what data is required |
johnAlexander | 3:09f23aad108a | 360 | void print_results( int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData ) |
charlesmn | 0:e91189a84ad9 | 361 | { |
johnAlexander | 3:09f23aad108a | 362 | int no_of_object_found=pMultiRangingData->NumberOfObjectsFound; |
johnAlexander | 3:09f23aad108a | 363 | int signal_rate = 0; |
johnAlexander | 3:09f23aad108a | 364 | int ambient_rate = 0; |
johnAlexander | 3:09f23aad108a | 365 | |
johnAlexander | 3:09f23aad108a | 366 | int RoiNumber=pMultiRangingData->RoiNumber; |
charlesmn | 0:e91189a84ad9 | 367 | |
johnAlexander | 3:09f23aad108a | 368 | if (( no_of_object_found < 10 ) && ( no_of_object_found != 0)) { |
johnAlexander | 3:09f23aad108a | 369 | for(int j=0; j<no_of_object_found; j++) { |
johnAlexander | 3:09f23aad108a | 370 | if ((pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) || |
johnAlexander | 3:09f23aad108a | 371 | (pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL)) { |
johnAlexander | 3:09f23aad108a | 372 | signal_rate = pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65535; |
johnAlexander | 3:09f23aad108a | 373 | ambient_rate = pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65535; |
johnAlexander | 3:09f23aad108a | 374 | printf("\t i2cAddr=%d \t RoiNumber=%d \t status=%d, \t D=%5dmm, \t Signal=%d Mcps, \t Ambient=%d Mcps \n", |
johnAlexander | 3:09f23aad108a | 375 | devNumber, RoiNumber, |
johnAlexander | 3:09f23aad108a | 376 | pMultiRangingData->RangeData[j].RangeStatus, |
johnAlexander | 3:09f23aad108a | 377 | pMultiRangingData->RangeData[j].RangeMilliMeter, |
johnAlexander | 3:09f23aad108a | 378 | signal_rate, |
johnAlexander | 3:09f23aad108a | 379 | ambient_rate); |
johnAlexander | 3:09f23aad108a | 380 | /* |
johnAlexander | 3:09f23aad108a | 381 | // online compiler disables printf() / floating-point support, for code-size reasons. |
johnAlexander | 3:09f23aad108a | 382 | // offline compiler can switch it on. |
johnAlexander | 3:09f23aad108a | 383 | 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 | 3:09f23aad108a | 384 | devNumber, RoiNumber, |
johnAlexander | 3:09f23aad108a | 385 | pMultiRangingData->RangeData[j].RangeStatus, |
johnAlexander | 3:09f23aad108a | 386 | pMultiRangingData->RangeData[j].RangeMilliMeter, |
johnAlexander | 3:09f23aad108a | 387 | pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65535.0, |
johnAlexander | 3:09f23aad108a | 388 | pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65535.0); |
johnAlexander | 3:09f23aad108a | 389 | */ |
charlesmn | 0:e91189a84ad9 | 390 | } |
johnAlexander | 3:09f23aad108a | 391 | } |
johnAlexander | 3:09f23aad108a | 392 | } // if (( no_of_object_found < 10 ) && ( no_of_object_found != 0)) |
charlesmn | 0:e91189a84ad9 | 393 | } |
charlesmn | 0:e91189a84ad9 | 394 | |
charlesmn | 0:e91189a84ad9 | 395 | |
charlesmn | 0:e91189a84ad9 | 396 | |
charlesmn | 0:e91189a84ad9 | 397 | #if (MBED_VERSION > 60300) |
charlesmn | 0:e91189a84ad9 | 398 | void wait_ms(int ms) |
charlesmn | 0:e91189a84ad9 | 399 | { |
charlesmn | 0:e91189a84ad9 | 400 | thread_sleep_for(ms); |
charlesmn | 0:e91189a84ad9 | 401 | } |
johnAlexander | 3:09f23aad108a | 402 | #endif |
charlesmn | 0:e91189a84ad9 | 403 |