Sample program for 3 sensors using polling in autonomous mode.

Dependencies:   X_NUCLEO_53L1A2

Committer:
johnAlexander
Date:
Wed May 12 09:00:10 2021 +0000
Revision:
6:19d56b30bfa7
Parent:
5:b233bdf91671
Child:
7:242f30acc456
print signal & ambient rates as absolute values / integers, for online compiler usecase. leave comment in about floating-point use.

Who changed what in which revision?

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