VL53L1CB expansion shield autonomous interrupt example.

Dependencies:   X_NUCLEO_53L1A2

Committer:
johnAlexander
Date:
Tue May 11 15:33:09 2021 +0000
Revision:
4:0ac9998b69ac
Parent:
3:c1e893e6752f
Enable interrupt support, for central, expansion board sensor.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
charlesmn 0:020912dfa221 1 /*
johnAlexander 2:f0ec92af4b5f 2 * This VL53L1X Expansion board test application performs range measurements
johnAlexander 2:f0ec92af4b5f 3 * using the onboard embedded centre sensor and two satelites, in autonomous, interrupt mode.
johnAlexander 2:f0ec92af4b5f 4 * Measured ranges are ouput on the Serial Port, running at 115200 baud.
charlesmn 0:020912dfa221 5 *
charlesmn 0:020912dfa221 6 * This is designed to work with MBed V2 , MBed V5 and MBed V6.
charlesmn 0:020912dfa221 7 *
johnAlexander 2:f0ec92af4b5f 8 * The Reset button can be used to restart the program.
charlesmn 0:020912dfa221 9 *
johnAlexander 2:f0ec92af4b5f 10 * *** Note :
johnAlexander 2:f0ec92af4b5f 11 * Default Mbed build system settings disable print floating-point support.
johnAlexander 2:f0ec92af4b5f 12 * Offline builds can enable this, again.
johnAlexander 2:f0ec92af4b5f 13 * https://github.com/ARMmbed/mbed-os/blob/master/platform/source/minimal-printf/README.md
johnAlexander 2:f0ec92af4b5f 14 * .\mbed-os\platform\mbed_lib.json
johnAlexander 2:f0ec92af4b5f 15 *
charlesmn 0:020912dfa221 16 */
charlesmn 0:020912dfa221 17
charlesmn 0:020912dfa221 18 #include <stdio.h>
johnAlexander 2:f0ec92af4b5f 19 #include <time.h>
charlesmn 0:020912dfa221 20
charlesmn 0:020912dfa221 21 #include "mbed.h"
johnAlexander 2:f0ec92af4b5f 22
charlesmn 1:ff48a20de191 23 #include "XNucleo53L1A2.h"
charlesmn 0:020912dfa221 24 #include "ToF_I2C.h"
charlesmn 0:020912dfa221 25
charlesmn 0:020912dfa221 26 // i2c comms port pins
charlesmn 0:020912dfa221 27 #define I2C_SDA D14
charlesmn 0:020912dfa221 28 #define I2C_SCL D15
charlesmn 0:020912dfa221 29
charlesmn 0:020912dfa221 30
charlesmn 0:020912dfa221 31 #define NUM_SENSORS 3
charlesmn 0:020912dfa221 32
charlesmn 1:ff48a20de191 33 // define the interrupt pins
charlesmn 0:020912dfa221 34 PinName CentreIntPin = A2;
charlesmn 0:020912dfa221 35 // the satellite pins depend on solder blobs on the back of the shield.
charlesmn 0:020912dfa221 36 // they may not exist or may be one of two sets.
charlesmn 0:020912dfa221 37 // the centre pin always exists
charlesmn 0:020912dfa221 38 PinName LeftIntPin = D9;
charlesmn 0:020912dfa221 39 PinName RightIntPin = D4;
charlesmn 0:020912dfa221 40 // alternate set
charlesmn 0:020912dfa221 41 //PinName LeftIntPin = D8;
charlesmn 0:020912dfa221 42 //PinName RightIntPin = D2;
charlesmn 0:020912dfa221 43
charlesmn 0:020912dfa221 44
charlesmn 0:020912dfa221 45
charlesmn 1:ff48a20de191 46 static XNucleo53L1A2 *board=NULL;
charlesmn 0:020912dfa221 47
charlesmn 0:020912dfa221 48 #if (MBED_VERSION > 60300)
johnAlexander 2:f0ec92af4b5f 49 UnbufferedSerial pc(USBTX, USBRX);
johnAlexander 2:f0ec92af4b5f 50 extern "C" void wait_ms(int ms);
charlesmn 0:020912dfa221 51 #else
johnAlexander 2:f0ec92af4b5f 52 Serial pc(SERIAL_TX, SERIAL_RX);
charlesmn 0:020912dfa221 53 #endif
charlesmn 0:020912dfa221 54
johnAlexander 3:c1e893e6752f 55 void print_results(int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData );
charlesmn 0:020912dfa221 56
johnAlexander 3:c1e893e6752f 57
johnAlexander 3:c1e893e6752f 58 VL53L1_Dev_t devCentre;
johnAlexander 3:c1e893e6752f 59 VL53L1_Dev_t devLeft;
johnAlexander 3:c1e893e6752f 60 VL53L1_Dev_t devRight;
johnAlexander 3:c1e893e6752f 61 VL53L1_DEV Dev = &devCentre;
johnAlexander 4:0ac9998b69ac 62 VL53L1 *Sensor;
johnAlexander 4:0ac9998b69ac 63
charlesmn 0:020912dfa221 64
charlesmn 0:020912dfa221 65
johnAlexander 3:c1e893e6752f 66 /* flags that handle interrupt request for sensor and user blue button*/
johnAlexander 3:c1e893e6752f 67 volatile bool int_sensor = false;
johnAlexander 3:c1e893e6752f 68 volatile bool int_stop = false;
johnAlexander 3:c1e893e6752f 69
johnAlexander 3:c1e893e6752f 70 /* ISR callback function of the centre sensor */
johnAlexander 3:c1e893e6752f 71 void sensor_irq(void)
johnAlexander 3:c1e893e6752f 72 {
johnAlexander 3:c1e893e6752f 73 int_sensor = true;
johnAlexander 3:c1e893e6752f 74 board->sensor_centre->disable_interrupt_measure_detection_irq();
johnAlexander 3:c1e893e6752f 75 }
johnAlexander 3:c1e893e6752f 76
johnAlexander 3:c1e893e6752f 77 /* Start the sensor ranging */
johnAlexander 3:c1e893e6752f 78 int init_sensor()
johnAlexander 3:c1e893e6752f 79 {
johnAlexander 3:c1e893e6752f 80 int status = 0;
johnAlexander 4:0ac9998b69ac 81
johnAlexander 4:0ac9998b69ac 82 Dev=&devCentre;
johnAlexander 4:0ac9998b69ac 83 Sensor=board->sensor_centre;
johnAlexander 4:0ac9998b69ac 84
johnAlexander 4:0ac9998b69ac 85 // configure the sensors
johnAlexander 4:0ac9998b69ac 86 Dev->comms_speed_khz = 400;
johnAlexander 4:0ac9998b69ac 87 Dev->comms_type = 1;
johnAlexander 4:0ac9998b69ac 88 Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS;
johnAlexander 4:0ac9998b69ac 89
johnAlexander 4:0ac9998b69ac 90 printf("configuring centre channel \n");
johnAlexander 4:0ac9998b69ac 91
johnAlexander 4:0ac9998b69ac 92 /* Device Initialization and setting */
johnAlexander 4:0ac9998b69ac 93 status = Sensor->vl53L1_DataInit();
johnAlexander 4:0ac9998b69ac 94 status = Sensor->vl53L1_StaticInit();
johnAlexander 4:0ac9998b69ac 95 status = Sensor->vl53L1_SetPresetMode(VL53L1_PRESETMODE_AUTONOMOUS);
johnAlexander 4:0ac9998b69ac 96 status = Sensor->vl53L1_SetDistanceMode(VL53L1_DISTANCEMODE_LONG);
johnAlexander 4:0ac9998b69ac 97 status = Sensor->vl53L1_SetMeasurementTimingBudgetMicroSeconds( 200 * 1000);
johnAlexander 4:0ac9998b69ac 98
johnAlexander 4:0ac9998b69ac 99
johnAlexander 4:0ac9998b69ac 100 // set the ranging and signal rate filter
johnAlexander 4:0ac9998b69ac 101 VL53L1_DetectionConfig_t thresholdconfig;
johnAlexander 4:0ac9998b69ac 102
johnAlexander 4:0ac9998b69ac 103 thresholdconfig.DetectionMode = VL53L1_DETECTION_DISTANCE_ONLY; /// type VL53L1_DetectionMode in vl53l1_def.h
johnAlexander 4:0ac9998b69ac 104 thresholdconfig.Distance.CrossMode = VL53L1_THRESHOLD_IN_WINDOW; // type VL53L1_ThresholdMode. ignore if distance outside high and low
johnAlexander 4:0ac9998b69ac 105 thresholdconfig.Distance.High = 300; // high distance in mm
johnAlexander 4:0ac9998b69ac 106 thresholdconfig.Distance.Low = 200; // low distance in mm
johnAlexander 4:0ac9998b69ac 107 thresholdconfig.Rate.CrossMode=0; // type VL53L1_ThresholdMode VL53L1_THRESHOLD_CROSSED_LOW VL53L1_THRESHOLD_CROSSED_HIGH VL53L1_THRESHOLD_OUT_OF_WINDOW VL53L1_THRESHOLD_IN_WINDOW
johnAlexander 4:0ac9998b69ac 108 thresholdconfig.Rate.High = 0;
johnAlexander 4:0ac9998b69ac 109 thresholdconfig.Rate.Low = 0;
johnAlexander 4:0ac9998b69ac 110 thresholdconfig.IntrNoTarget = 0 ;// if 1 produce an interrupt even if there is no target found e.g out of range
johnAlexander 4:0ac9998b69ac 111
johnAlexander 4:0ac9998b69ac 112 status = Sensor->vl53L1_SetThresholdConfig(&thresholdconfig);
johnAlexander 4:0ac9998b69ac 113
johnAlexander 4:0ac9998b69ac 114 // create interrupt handler and start measurements
johnAlexander 4:0ac9998b69ac 115 if (board->sensor_centre!= NULL) {
johnAlexander 3:c1e893e6752f 116 status = board->sensor_centre->stop_measurement();
johnAlexander 3:c1e893e6752f 117 if (status != 0) {
johnAlexander 4:0ac9998b69ac 118 return status;
johnAlexander 3:c1e893e6752f 119 }
johnAlexander 3:c1e893e6752f 120
johnAlexander 3:c1e893e6752f 121 status = board->sensor_centre->start_measurement(&sensor_irq);
johnAlexander 3:c1e893e6752f 122 if (status != 0) {
johnAlexander 3:c1e893e6752f 123 return status;
johnAlexander 3:c1e893e6752f 124 }
johnAlexander 3:c1e893e6752f 125 }
johnAlexander 3:c1e893e6752f 126 return status;
johnAlexander 3:c1e893e6752f 127 }
johnAlexander 3:c1e893e6752f 128
johnAlexander 3:c1e893e6752f 129 /* ISR callback function of the user blue button to switch measuring sensor. */
johnAlexander 3:c1e893e6752f 130 void measuring_stop_irq(void)
johnAlexander 3:c1e893e6752f 131 {
johnAlexander 3:c1e893e6752f 132 int_stop = true;
johnAlexander 3:c1e893e6752f 133 }
johnAlexander 3:c1e893e6752f 134
charlesmn 0:020912dfa221 135 /*=================================== Main ==================================
charlesmn 0:020912dfa221 136 =============================================================================*/
charlesmn 0:020912dfa221 137 int main()
charlesmn 0:020912dfa221 138 {
johnAlexander 4:0ac9998b69ac 139 #if (MBED_VERSION < 60000)
johnAlexander 4:0ac9998b69ac 140 #if USER_BUTTON==PC_13 // we are cross compiling for Nucleo-f401
johnAlexander 4:0ac9998b69ac 141 InterruptIn stop_button(USER_BUTTON);
johnAlexander 4:0ac9998b69ac 142 stop_button.rise(&measuring_stop_irq);
johnAlexander 4:0ac9998b69ac 143 #endif
johnAlexander 4:0ac9998b69ac 144 #endif
charlesmn 0:020912dfa221 145 int status;
johnAlexander 4:0ac9998b69ac 146 uint16_t distance = 0;
johnAlexander 4:0ac9998b69ac 147
charlesmn 0:020912dfa221 148 pc.baud(115200); // baud rate is important as printf statements take a lot of time
charlesmn 0:020912dfa221 149
johnAlexander 3:c1e893e6752f 150 printf("Autonomous Interrupt, mbed = %d \r\n",MBED_VERSION);
charlesmn 0:020912dfa221 151
charlesmn 0:020912dfa221 152 // create i2c interface
charlesmn 0:020912dfa221 153 ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL);
johnAlexander 3:c1e893e6752f 154 /* creates the 53L1A2 expansion board singleton obj */
johnAlexander 3:c1e893e6752f 155 board = XNucleo53L1A2::instance(dev_I2C, CentreIntPin, LeftIntPin, RightIntPin);
charlesmn 0:020912dfa221 156
charlesmn 0:020912dfa221 157 dev_I2C->frequency(400000); //also needs doing in spi_interface.c
johnAlexander 3:c1e893e6752f 158
charlesmn 0:020912dfa221 159 printf("board created!\r\n");
charlesmn 0:020912dfa221 160
charlesmn 0:020912dfa221 161 /* init the 53L1A1 expansion board with default values */
charlesmn 0:020912dfa221 162 status = board->init_board();
charlesmn 0:020912dfa221 163 if (status) {
charlesmn 0:020912dfa221 164 printf("Failed to init board!\r\n");
charlesmn 0:020912dfa221 165 return 0;
charlesmn 0:020912dfa221 166 }
johnAlexander 3:c1e893e6752f 167
charlesmn 0:020912dfa221 168 printf("board initiated! - %d\r\n", status);
johnAlexander 3:c1e893e6752f 169
johnAlexander 3:c1e893e6752f 170 /* init an array with chars to id the sensors */
johnAlexander 3:c1e893e6752f 171 status = init_sensor();
johnAlexander 3:c1e893e6752f 172 if (status != 0) {
johnAlexander 3:c1e893e6752f 173 printf("Failed to init sensors!\r\n");
johnAlexander 3:c1e893e6752f 174 return status;
johnAlexander 3:c1e893e6752f 175 }
johnAlexander 3:c1e893e6752f 176
johnAlexander 3:c1e893e6752f 177 printf("loop forever\n");
johnAlexander 3:c1e893e6752f 178
johnAlexander 3:c1e893e6752f 179 // loop waiting for interrupts to happen. This is signaled by int_centre_result,int_left_result or int_right_result
johnAlexander 3:c1e893e6752f 180 // being non zero. When the interrupts clear this is signaled by int_centre_dropped,int_left_dropped and int_right_dropped.
johnAlexander 3:c1e893e6752f 181 // These are set back to zero when processing is completed
johnAlexander 3:c1e893e6752f 182 /* while (1)
johnAlexander 3:c1e893e6752f 183 {
johnAlexander 3:c1e893e6752f 184 VL53L1_MultiRangingData_t MultiRangingData;
johnAlexander 3:c1e893e6752f 185 VL53L1_MultiRangingData_t *pMultiRangingData = &MultiRangingData;
johnAlexander 3:c1e893e6752f 186
johnAlexander 3:c1e893e6752f 187 if ( int_left_dropped || int_centre_dropped || int_right_dropped )
johnAlexander 3:c1e893e6752f 188 wait_ms(30);
johnAlexander 3:c1e893e6752f 189
johnAlexander 3:c1e893e6752f 190 // when the interrupt pin goes loww start new measurement
johnAlexander 3:c1e893e6752f 191 if (int_centre_dropped != 0)
charlesmn 0:020912dfa221 192 {
johnAlexander 3:c1e893e6752f 193 int_centre_dropped = 0;
johnAlexander 3:c1e893e6752f 194 status = board->sensor_centre->vl53L1_ClearInterruptAndStartMeasurement();
charlesmn 0:020912dfa221 195 }
johnAlexander 3:c1e893e6752f 196
johnAlexander 3:c1e893e6752f 197 if (int_centre_result != 0)
charlesmn 0:020912dfa221 198 {
johnAlexander 3:c1e893e6752f 199 status = board->sensor_centre->vl53L1_GetMultiRangingData( pMultiRangingData);
johnAlexander 3:c1e893e6752f 200 if (status == 0)
johnAlexander 3:c1e893e6752f 201 {
johnAlexander 3:c1e893e6752f 202 print_results( devCentre.i2c_slave_address, pMultiRangingData );
johnAlexander 3:c1e893e6752f 203 }
johnAlexander 3:c1e893e6752f 204
johnAlexander 3:c1e893e6752f 205 // clear interrupt flag
johnAlexander 3:c1e893e6752f 206 int_centre_result = 0;
johnAlexander 3:c1e893e6752f 207
johnAlexander 3:c1e893e6752f 208 }
johnAlexander 3:c1e893e6752f 209 wait_ms( 1 * 10);
johnAlexander 3:c1e893e6752f 210 }
johnAlexander 3:c1e893e6752f 211 */
johnAlexander 3:c1e893e6752f 212
johnAlexander 4:0ac9998b69ac 213 VL53L1_MultiRangingData_t MultiRangingData;
johnAlexander 4:0ac9998b69ac 214 VL53L1_MultiRangingData_t *pMultiRangingData = NULL;
johnAlexander 4:0ac9998b69ac 215
johnAlexander 3:c1e893e6752f 216 while (true) {
johnAlexander 4:0ac9998b69ac 217 pMultiRangingData = &MultiRangingData;
johnAlexander 4:0ac9998b69ac 218
johnAlexander 3:c1e893e6752f 219 if (int_sensor) {
johnAlexander 3:c1e893e6752f 220 int_sensor = false;
johnAlexander 4:0ac9998b69ac 221 /*
johnAlexander 4:0ac9998b69ac 222 // status = board->sensor_centre->handle_irq(&distance);
johnAlexander 4:0ac9998b69ac 223 // status = get_measurement(distance);
johnAlexander 4:0ac9998b69ac 224 // status = board->sensor_centre->VL53L1_GetDistance(&distance);
johnAlexander 4:0ac9998b69ac 225 status = board->sensor_centre->VL53L1_ClearInterrupt();
johnAlexander 4:0ac9998b69ac 226 board->sensor_centre->enable_interrupt_measure_detection_irq();
johnAlexander 3:c1e893e6752f 227 printf("distance: %d\r\n", distance);
johnAlexander 4:0ac9998b69ac 228 */
johnAlexander 4:0ac9998b69ac 229 status = board->sensor_centre->vl53L1_GetMultiRangingData( pMultiRangingData);
johnAlexander 4:0ac9998b69ac 230 if (status == 0)
johnAlexander 4:0ac9998b69ac 231 {
johnAlexander 4:0ac9998b69ac 232 print_results( devCentre.i2c_slave_address, pMultiRangingData );
johnAlexander 4:0ac9998b69ac 233 }
johnAlexander 4:0ac9998b69ac 234 status = board->sensor_centre->VL53L1_ClearInterrupt();
johnAlexander 4:0ac9998b69ac 235 // status = board->sensor_centre->vl53L1_ClearInterruptAndStartMeasurement(); // doesnt run!
johnAlexander 4:0ac9998b69ac 236 board->sensor_centre->enable_interrupt_measure_detection_irq();
charlesmn 0:020912dfa221 237 }
charlesmn 0:020912dfa221 238
johnAlexander 3:c1e893e6752f 239 if (int_stop) {
johnAlexander 3:c1e893e6752f 240 printf("\r\nEnding loop mode \r\n");
johnAlexander 3:c1e893e6752f 241 break;
charlesmn 0:020912dfa221 242 }
charlesmn 0:020912dfa221 243 }
charlesmn 0:020912dfa221 244
johnAlexander 3:c1e893e6752f 245 }
johnAlexander 3:c1e893e6752f 246
charlesmn 0:020912dfa221 247
charlesmn 0:020912dfa221 248 // print what ever results are required
johnAlexander 2:f0ec92af4b5f 249 void print_results( int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData )
charlesmn 0:020912dfa221 250 {
johnAlexander 2:f0ec92af4b5f 251 int no_of_object_found=pMultiRangingData->NumberOfObjectsFound;
charlesmn 0:020912dfa221 252
johnAlexander 2:f0ec92af4b5f 253 int RoiNumber=pMultiRangingData->RoiNumber;
charlesmn 0:020912dfa221 254
johnAlexander 2:f0ec92af4b5f 255 if (( no_of_object_found < 10 ) && ( no_of_object_found != 0))
johnAlexander 2:f0ec92af4b5f 256 {
johnAlexander 2:f0ec92af4b5f 257 for(int j=0;j<no_of_object_found;j++){
johnAlexander 2:f0ec92af4b5f 258 if ((pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) ||
johnAlexander 2:f0ec92af4b5f 259 (pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL))
charlesmn 0:020912dfa221 260 {
johnAlexander 2:f0ec92af4b5f 261 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 2:f0ec92af4b5f 262 devNumber, RoiNumber,
johnAlexander 2:f0ec92af4b5f 263 pMultiRangingData->RangeData[j].RangeStatus,
johnAlexander 2:f0ec92af4b5f 264 pMultiRangingData->RangeData[j].RangeMilliMeter,
johnAlexander 2:f0ec92af4b5f 265 pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65535.0,
johnAlexander 2:f0ec92af4b5f 266 pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65535.0);
johnAlexander 2:f0ec92af4b5f 267 }
johnAlexander 2:f0ec92af4b5f 268 }
johnAlexander 2:f0ec92af4b5f 269 } // if (( no_of_object_found < 10 ) && ( no_of_object_found != 0))
charlesmn 0:020912dfa221 270 }
charlesmn 0:020912dfa221 271
charlesmn 0:020912dfa221 272
charlesmn 0:020912dfa221 273 #if (MBED_VERSION > 60300)
charlesmn 0:020912dfa221 274 extern "C" void wait_ms(int ms)
charlesmn 0:020912dfa221 275 {
charlesmn 0:020912dfa221 276 thread_sleep_for(ms);
charlesmn 0:020912dfa221 277 }
johnAlexander 4:0ac9998b69ac 278 #endif
charlesmn 0:020912dfa221 279
charlesmn 0:020912dfa221 280