A sample program to control one VL53L1 ToF sensor in multizone mode using polling to find out if a measurement is available. Mbed V6.3 but will run any MBed version by dropping replacing this one. Maint6 release.
Dependencies: X_NUCLEO_53L1A2
main.cpp@11:eafe2b5c130e, 2021-05-21 (annotated)
- Committer:
- johnAlexander
- Date:
- Fri May 21 09:08:32 2021 +0000
- Revision:
- 11:eafe2b5c130e
- Parent:
- 10:17d61466185f
- Child:
- 12:3dd44d06629c
Align multizone settings with configuration from C, ULD example.; Reformat code for publication.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
charlesmn | 0:50b05f035d13 | 1 | /* |
johnAlexander | 10:17d61466185f | 2 | * This VL53L1CB Expansion board test application performs range measurements |
johnAlexander | 11:eafe2b5c130e | 3 | * using the onboard embedded sensor, in polling mode. |
johnAlexander | 11:eafe2b5c130e | 4 | * Measured ranges are output on the Serial Port, running at 115200 baud. |
charlesmn | 0:50b05f035d13 | 5 | * |
johnAlexander | 11:eafe2b5c130e | 6 | * This is designed to work with MBed v2.x, & MBedOS v5.x / v6.x. |
charlesmn | 0:50b05f035d13 | 7 | * |
johnAlexander | 10:17d61466185f | 8 | * The Reset button can be used to restart the program. |
johnAlexander | 9:0a3e1affe004 | 9 | * |
johnAlexander | 11:eafe2b5c130e | 10 | * *** NOTE : |
johnAlexander | 11:eafe2b5c130e | 11 | * Default Mbed build system settings disable printf() floating-point support. |
johnAlexander | 11:eafe2b5c130e | 12 | * Offline builds can enable this, again. |
johnAlexander | 11:eafe2b5c130e | 13 | * https://github.com/ARMmbed/mbed-os/blob/master/platform/source/minimal-printf/README.md |
johnAlexander | 11:eafe2b5c130e | 14 | * .\mbed-os\platform\mbed_lib.json |
johnAlexander | 11:eafe2b5c130e | 15 | * |
johnAlexander | 11:eafe2b5c130e | 16 | * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of |
johnAlexander | 11:eafe2b5c130e | 17 | * the X-NUCELO-53L1A1 expansion board are not made/OFF. |
johnAlexander | 11:eafe2b5c130e | 18 | * These links must be made to allow interrupts from the Satellite boards |
johnAlexander | 11:eafe2b5c130e | 19 | * to be received. |
johnAlexander | 11:eafe2b5c130e | 20 | * U11 and U18 must be made/ON to allow interrupts to be received from the |
johnAlexander | 11:eafe2b5c130e | 21 | * INT_L & INT_R positions; or |
johnAlexander | 11:eafe2b5c130e | 22 | * U10 and U15 must be made/ON to allow interrupts to be received from the |
johnAlexander | 11:eafe2b5c130e | 23 | * Alternate INT_L & INT_R positions. |
johnAlexander | 11:eafe2b5c130e | 24 | * The X_NUCLEO_53L1A2 library defaults to use the INT_L/INT_R positions. |
johnAlexander | 11:eafe2b5c130e | 25 | * INT_L is available on expansion board Arduino Connector CN5, pin 1 as D8. |
johnAlexander | 11:eafe2b5c130e | 26 | * Alternate INT_L is on CN5 Connector pin 2 as D9. |
johnAlexander | 11:eafe2b5c130e | 27 | * INT_R is available on expansion board Arduino Connector CN9, pin 3 as D2. |
johnAlexander | 11:eafe2b5c130e | 28 | * Alternate INT_R is on CN9 Connector pin 5 as D4. |
johnAlexander | 11:eafe2b5c130e | 29 | * The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A2/ |
johnAlexander | 9:0a3e1affe004 | 30 | * |
charlesmn | 0:50b05f035d13 | 31 | */ |
johnAlexander | 10:17d61466185f | 32 | |
charlesmn | 0:50b05f035d13 | 33 | #include <stdio.h> |
johnAlexander | 6:bc6ac1e294f4 | 34 | #include <time.h> |
charlesmn | 0:50b05f035d13 | 35 | |
charlesmn | 0:50b05f035d13 | 36 | #include "mbed.h" |
johnAlexander | 11:eafe2b5c130e | 37 | |
johnAlexander | 4:177d711cc20f | 38 | #include "XNucleo53L1A2.h" |
charlesmn | 0:50b05f035d13 | 39 | #include "ToF_I2C.h" |
charlesmn | 0:50b05f035d13 | 40 | |
johnAlexander | 11:eafe2b5c130e | 41 | // i2c comms port pins |
johnAlexander | 10:17d61466185f | 42 | #define I2C_SDA D14 |
johnAlexander | 10:17d61466185f | 43 | #define I2C_SCL D15 |
charlesmn | 0:50b05f035d13 | 44 | |
charlesmn | 0:50b05f035d13 | 45 | |
johnAlexander | 11:eafe2b5c130e | 46 | #define NUM_SENSORS 3 |
johnAlexander | 11:eafe2b5c130e | 47 | |
johnAlexander | 11:eafe2b5c130e | 48 | // define interrupt pins |
johnAlexander | 11:eafe2b5c130e | 49 | PinName CentreIntPin = A2; |
johnAlexander | 11:eafe2b5c130e | 50 | // the satellite pins depend on solder blobs on the back of the shield. |
johnAlexander | 11:eafe2b5c130e | 51 | // they may not exist or may be one of two sets. |
johnAlexander | 11:eafe2b5c130e | 52 | // the centre pin always exists |
johnAlexander | 11:eafe2b5c130e | 53 | //PinName LeftIntPin = D8; |
johnAlexander | 11:eafe2b5c130e | 54 | PinName RightIntPin = D2; |
johnAlexander | 11:eafe2b5c130e | 55 | // alternate set |
johnAlexander | 11:eafe2b5c130e | 56 | PinName LeftIntPin = D9; |
johnAlexander | 11:eafe2b5c130e | 57 | //PinName RightIntPin = D4; |
charlesmn | 0:50b05f035d13 | 58 | |
johnAlexander | 4:177d711cc20f | 59 | static XNucleo53L1A2 *board=NULL; |
johnAlexander | 11:eafe2b5c130e | 60 | |
johnAlexander | 10:17d61466185f | 61 | #if (MBED_VERSION > 60300) |
johnAlexander | 10:17d61466185f | 62 | UnbufferedSerial pc(USBTX, USBRX); |
johnAlexander | 10:17d61466185f | 63 | extern "C" void wait_ms(int ms); |
charlesmn | 2:ef5e40bad526 | 64 | #else |
johnAlexander | 10:17d61466185f | 65 | Serial pc(SERIAL_TX, SERIAL_RX); |
charlesmn | 2:ef5e40bad526 | 66 | #endif |
charlesmn | 0:50b05f035d13 | 67 | |
johnAlexander | 11:eafe2b5c130e | 68 | void print_results(int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData ); |
johnAlexander | 11:eafe2b5c130e | 69 | |
johnAlexander | 11:eafe2b5c130e | 70 | |
johnAlexander | 11:eafe2b5c130e | 71 | VL53L1_Dev_t devCentre; |
johnAlexander | 11:eafe2b5c130e | 72 | VL53L1_DEV Dev = &devCentre; |
johnAlexander | 11:eafe2b5c130e | 73 | |
johnAlexander | 11:eafe2b5c130e | 74 | VL53L1 *Sensor; |
johnAlexander | 11:eafe2b5c130e | 75 | |
johnAlexander | 11:eafe2b5c130e | 76 | |
johnAlexander | 11:eafe2b5c130e | 77 | |
johnAlexander | 11:eafe2b5c130e | 78 | /* flags that handle interrupt request for sensor and user blue button*/ |
johnAlexander | 11:eafe2b5c130e | 79 | volatile bool int_sensor = false; |
johnAlexander | 11:eafe2b5c130e | 80 | volatile bool int_stop = false; |
johnAlexander | 11:eafe2b5c130e | 81 | |
johnAlexander | 11:eafe2b5c130e | 82 | /* ISR callback function of the centre sensor */ |
johnAlexander | 11:eafe2b5c130e | 83 | void sensor_irq(void) |
johnAlexander | 11:eafe2b5c130e | 84 | { |
johnAlexander | 11:eafe2b5c130e | 85 | int_sensor = true; |
johnAlexander | 11:eafe2b5c130e | 86 | board->sensor_centre->disable_interrupt_measure_detection_irq(); |
johnAlexander | 11:eafe2b5c130e | 87 | } |
johnAlexander | 11:eafe2b5c130e | 88 | |
johnAlexander | 11:eafe2b5c130e | 89 | /* Start the sensor ranging */ |
johnAlexander | 11:eafe2b5c130e | 90 | int init_sensor() |
johnAlexander | 11:eafe2b5c130e | 91 | { |
johnAlexander | 11:eafe2b5c130e | 92 | int status = 0; |
johnAlexander | 11:eafe2b5c130e | 93 | |
johnAlexander | 11:eafe2b5c130e | 94 | Dev=&devCentre; |
johnAlexander | 11:eafe2b5c130e | 95 | Sensor=board->sensor_centre; |
johnAlexander | 11:eafe2b5c130e | 96 | |
johnAlexander | 11:eafe2b5c130e | 97 | // configure the sensors |
johnAlexander | 11:eafe2b5c130e | 98 | Dev->comms_speed_khz = 400; |
johnAlexander | 11:eafe2b5c130e | 99 | Dev->comms_type = 1; |
johnAlexander | 11:eafe2b5c130e | 100 | Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS; |
johnAlexander | 11:eafe2b5c130e | 101 | |
johnAlexander | 11:eafe2b5c130e | 102 | printf("configuring centre channel \n"); |
johnAlexander | 11:eafe2b5c130e | 103 | |
johnAlexander | 11:eafe2b5c130e | 104 | /* Device Initialization and setting */ |
johnAlexander | 11:eafe2b5c130e | 105 | status = Sensor->vl53L1_DataInit(); |
johnAlexander | 11:eafe2b5c130e | 106 | status = Sensor->vl53L1_StaticInit(); |
johnAlexander | 11:eafe2b5c130e | 107 | |
johnAlexander | 11:eafe2b5c130e | 108 | VL53L1_RoiConfig_t RoiConfig; |
johnAlexander | 11:eafe2b5c130e | 109 | |
johnAlexander | 11:eafe2b5c130e | 110 | RoiConfig.NumberOfRoi = 3; |
charlesmn | 0:50b05f035d13 | 111 | |
johnAlexander | 11:eafe2b5c130e | 112 | RoiConfig.UserRois[0].TopLeftX = 3; |
johnAlexander | 11:eafe2b5c130e | 113 | RoiConfig.UserRois[0].TopLeftY = 10; |
johnAlexander | 11:eafe2b5c130e | 114 | RoiConfig.UserRois[0].BotRightX = 10; |
johnAlexander | 11:eafe2b5c130e | 115 | RoiConfig.UserRois[0].BotRightY = 3; |
johnAlexander | 11:eafe2b5c130e | 116 | |
johnAlexander | 11:eafe2b5c130e | 117 | RoiConfig.UserRois[1].TopLeftX = 5; |
johnAlexander | 11:eafe2b5c130e | 118 | RoiConfig.UserRois[1].TopLeftY = 12; |
johnAlexander | 11:eafe2b5c130e | 119 | RoiConfig.UserRois[1].BotRightX = 12; |
johnAlexander | 11:eafe2b5c130e | 120 | RoiConfig.UserRois[1].BotRightY = 5; |
johnAlexander | 11:eafe2b5c130e | 121 | |
johnAlexander | 11:eafe2b5c130e | 122 | RoiConfig.UserRois[2].TopLeftX = 6; |
johnAlexander | 11:eafe2b5c130e | 123 | RoiConfig.UserRois[2].TopLeftY = 13; |
johnAlexander | 11:eafe2b5c130e | 124 | RoiConfig.UserRois[2].BotRightX = 13; |
johnAlexander | 11:eafe2b5c130e | 125 | RoiConfig.UserRois[2].BotRightY = 6; |
johnAlexander | 11:eafe2b5c130e | 126 | |
johnAlexander | 11:eafe2b5c130e | 127 | status = Sensor->vl53L1_SetPresetMode(VL53L1_PRESETMODE_MULTIZONES_SCANNING); |
johnAlexander | 11:eafe2b5c130e | 128 | status = Sensor->vl53L1_SetDistanceMode(VL53L1_DISTANCEMODE_LONG); |
johnAlexander | 11:eafe2b5c130e | 129 | |
johnAlexander | 11:eafe2b5c130e | 130 | status = Sensor->vl53L1_SetROI(&RoiConfig); |
johnAlexander | 11:eafe2b5c130e | 131 | |
johnAlexander | 11:eafe2b5c130e | 132 | status = Sensor->vl53L1_SetMeasurementTimingBudgetMicroSeconds(17000); |
charlesmn | 0:50b05f035d13 | 133 | |
johnAlexander | 10:17d61466185f | 134 | |
johnAlexander | 11:eafe2b5c130e | 135 | // create interrupt handler and start measurements |
johnAlexander | 11:eafe2b5c130e | 136 | if (board->sensor_centre!= NULL) { |
johnAlexander | 11:eafe2b5c130e | 137 | status = board->sensor_centre->stop_measurement(); |
johnAlexander | 11:eafe2b5c130e | 138 | if (status != 0) { |
johnAlexander | 11:eafe2b5c130e | 139 | return status; |
johnAlexander | 11:eafe2b5c130e | 140 | } |
johnAlexander | 11:eafe2b5c130e | 141 | |
johnAlexander | 11:eafe2b5c130e | 142 | status = Sensor->vl53L1_StartMeasurement(); |
johnAlexander | 11:eafe2b5c130e | 143 | if (status != 0) { |
johnAlexander | 11:eafe2b5c130e | 144 | return status; |
johnAlexander | 11:eafe2b5c130e | 145 | } |
johnAlexander | 11:eafe2b5c130e | 146 | } |
johnAlexander | 11:eafe2b5c130e | 147 | return status; |
johnAlexander | 11:eafe2b5c130e | 148 | } |
johnAlexander | 11:eafe2b5c130e | 149 | |
johnAlexander | 11:eafe2b5c130e | 150 | /* ISR callback function of the user blue button to switch measuring sensor. */ |
johnAlexander | 11:eafe2b5c130e | 151 | void measuring_stop_irq(void) |
johnAlexander | 11:eafe2b5c130e | 152 | { |
johnAlexander | 11:eafe2b5c130e | 153 | int_stop = true; |
johnAlexander | 11:eafe2b5c130e | 154 | } |
johnAlexander | 11:eafe2b5c130e | 155 | |
charlesmn | 0:50b05f035d13 | 156 | /*=================================== Main ================================== |
charlesmn | 0:50b05f035d13 | 157 | =============================================================================*/ |
charlesmn | 0:50b05f035d13 | 158 | int main() |
johnAlexander | 10:17d61466185f | 159 | { |
charlesmn | 0:50b05f035d13 | 160 | int status; |
johnAlexander | 11:eafe2b5c130e | 161 | uint16_t distance = 0; |
charlesmn | 0:50b05f035d13 | 162 | |
charlesmn | 0:50b05f035d13 | 163 | pc.baud(115200); // baud rate is important as printf statements take a lot of time |
johnAlexander | 11:eafe2b5c130e | 164 | |
johnAlexander | 11:eafe2b5c130e | 165 | printf("mbed version : %d \r\n",MBED_VERSION); |
charlesmn | 0:50b05f035d13 | 166 | |
charlesmn | 0:50b05f035d13 | 167 | // create i2c interface |
charlesmn | 0:50b05f035d13 | 168 | ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL); |
johnAlexander | 11:eafe2b5c130e | 169 | /* creates the 53L1A2 expansion board singleton obj */ |
johnAlexander | 11:eafe2b5c130e | 170 | board = XNucleo53L1A2::instance(dev_I2C, CentreIntPin, LeftIntPin, RightIntPin); |
johnAlexander | 10:17d61466185f | 171 | |
charlesmn | 0:50b05f035d13 | 172 | printf("board created!\r\n"); |
charlesmn | 0:50b05f035d13 | 173 | |
charlesmn | 0:50b05f035d13 | 174 | /* init the 53L1A1 expansion board with default values */ |
charlesmn | 0:50b05f035d13 | 175 | status = board->init_board(); |
charlesmn | 0:50b05f035d13 | 176 | if (status) { |
charlesmn | 0:50b05f035d13 | 177 | printf("Failed to init board!\r\n"); |
johnAlexander | 11:eafe2b5c130e | 178 | return status; |
charlesmn | 0:50b05f035d13 | 179 | } |
johnAlexander | 10:17d61466185f | 180 | |
charlesmn | 0:50b05f035d13 | 181 | printf("board initiated! - %d\r\n", status); |
charlesmn | 0:50b05f035d13 | 182 | |
johnAlexander | 11:eafe2b5c130e | 183 | /* init an array with chars to id the sensors */ |
johnAlexander | 11:eafe2b5c130e | 184 | status = init_sensor(); |
johnAlexander | 11:eafe2b5c130e | 185 | if (status != 0) { |
johnAlexander | 11:eafe2b5c130e | 186 | printf("Failed to init sensors!\r\n"); |
johnAlexander | 11:eafe2b5c130e | 187 | return status; |
johnAlexander | 11:eafe2b5c130e | 188 | } |
charlesmn | 0:50b05f035d13 | 189 | |
johnAlexander | 11:eafe2b5c130e | 190 | printf("loop forever\n"); |
johnAlexander | 10:17d61466185f | 191 | |
johnAlexander | 11:eafe2b5c130e | 192 | VL53L1_MultiRangingData_t MultiRangingData; |
johnAlexander | 11:eafe2b5c130e | 193 | VL53L1_MultiRangingData_t *pMultiRangingData = NULL; |
johnAlexander | 10:17d61466185f | 194 | |
johnAlexander | 11:eafe2b5c130e | 195 | while (true) { |
johnAlexander | 11:eafe2b5c130e | 196 | pMultiRangingData = &MultiRangingData; |
johnAlexander | 10:17d61466185f | 197 | |
johnAlexander | 11:eafe2b5c130e | 198 | status = board->sensor_centre->vl53L1_WaitMeasurementDataReady(); |
johnAlexander | 11:eafe2b5c130e | 199 | status = board->sensor_centre->vl53L1_GetMultiRangingData( pMultiRangingData); |
charlesmn | 0:50b05f035d13 | 200 | |
johnAlexander | 11:eafe2b5c130e | 201 | print_results( devCentre.i2c_slave_address, pMultiRangingData ); |
johnAlexander | 10:17d61466185f | 202 | |
johnAlexander | 11:eafe2b5c130e | 203 | status = board->sensor_centre->VL53L1_ClearInterrupt(); |
johnAlexander | 11:eafe2b5c130e | 204 | } |
johnAlexander | 10:17d61466185f | 205 | |
johnAlexander | 11:eafe2b5c130e | 206 | printf("Terminating.\n"); |
charlesmn | 0:50b05f035d13 | 207 | } |
johnAlexander | 10:17d61466185f | 208 | |
johnAlexander | 10:17d61466185f | 209 | |
johnAlexander | 11:eafe2b5c130e | 210 | // print what ever results are required |
johnAlexander | 11:eafe2b5c130e | 211 | void print_results( int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData ) |
charlesmn | 0:50b05f035d13 | 212 | { |
johnAlexander | 11:eafe2b5c130e | 213 | int no_of_object_found = pMultiRangingData->NumberOfObjectsFound; |
johnAlexander | 11:eafe2b5c130e | 214 | int signal_rate = 0; |
johnAlexander | 11:eafe2b5c130e | 215 | int ambient_rate = 0; |
johnAlexander | 10:17d61466185f | 216 | |
johnAlexander | 11:eafe2b5c130e | 217 | int RoiNumber = pMultiRangingData->RoiNumber; |
johnAlexander | 10:17d61466185f | 218 | |
johnAlexander | 11:eafe2b5c130e | 219 | if (no_of_object_found <= 1) |
johnAlexander | 11:eafe2b5c130e | 220 | no_of_object_found = 1; |
johnAlexander | 11:eafe2b5c130e | 221 | for(int j=0; j<no_of_object_found; j++) { |
johnAlexander | 11:eafe2b5c130e | 222 | signal_rate = pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65535; |
johnAlexander | 11:eafe2b5c130e | 223 | ambient_rate = pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65535; |
johnAlexander | 11:eafe2b5c130e | 224 | printf("\t i2cAddr=%d \t RoiNumber=%d \t status=%d, \t D=%5dmm, \t Signal=%d Mcps, \t Ambient=%d Mcps \n", |
johnAlexander | 11:eafe2b5c130e | 225 | devNumber, RoiNumber, |
johnAlexander | 11:eafe2b5c130e | 226 | pMultiRangingData->RangeData[j].RangeStatus, |
johnAlexander | 11:eafe2b5c130e | 227 | pMultiRangingData->RangeData[j].RangeMilliMeter, |
johnAlexander | 11:eafe2b5c130e | 228 | signal_rate, |
johnAlexander | 11:eafe2b5c130e | 229 | ambient_rate); |
johnAlexander | 11:eafe2b5c130e | 230 | /* |
johnAlexander | 11:eafe2b5c130e | 231 | // online compiler disables printf() / floating-point support, for code-size reasons. |
johnAlexander | 11:eafe2b5c130e | 232 | // offline compiler can switch it on. |
johnAlexander | 11:eafe2b5c130e | 233 | 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 | 11:eafe2b5c130e | 234 | devNumber, RoiNumber, |
johnAlexander | 11:eafe2b5c130e | 235 | pMultiRangingData->RangeData[j].RangeStatus, |
johnAlexander | 11:eafe2b5c130e | 236 | pMultiRangingData->RangeData[j].RangeMilliMeter, |
johnAlexander | 11:eafe2b5c130e | 237 | pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65535.0, |
johnAlexander | 11:eafe2b5c130e | 238 | pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65535.0); |
johnAlexander | 11:eafe2b5c130e | 239 | */ |
johnAlexander | 10:17d61466185f | 240 | } |
johnAlexander | 10:17d61466185f | 241 | } |
charlesmn | 2:ef5e40bad526 | 242 | |
johnAlexander | 10:17d61466185f | 243 | |
charlesmn | 3:d1a3d15a06ff | 244 | #if (MBED_VERSION > 60300) |
charlesmn | 2:ef5e40bad526 | 245 | extern "C" void wait_ms(int ms) |
johnAlexander | 10:17d61466185f | 246 | { |
charlesmn | 2:ef5e40bad526 | 247 | thread_sleep_for(ms); |
johnAlexander | 10:17d61466185f | 248 | } |
johnAlexander | 10:17d61466185f | 249 | #endif |
johnAlexander | 10:17d61466185f | 250 | |
johnAlexander | 10:17d61466185f | 251 |