Sample program for 3 sensors using polling in autonomous mode.

Dependencies:   X_NUCLEO_53L1A2

Committer:
johnAlexander
Date:
Wed May 12 08:37:43 2021 +0000
Revision:
5:b233bdf91671
Parent:
4:0ac9998b69ac
Child:
6:19d56b30bfa7
reformat code for publication.

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
charlesmn 0:020912dfa221 48
charlesmn 1:ff48a20de191 49 static XNucleo53L1A2 *board=NULL;
charlesmn 0:020912dfa221 50
johnAlexander 5:b233bdf91671 51 #if (MBED_VERSION > 60300)
johnAlexander 5:b233bdf91671 52 UnbufferedSerial pc(USBTX, USBRX);
johnAlexander 5:b233bdf91671 53 extern "C" void wait_ms(int ms);
charlesmn 0:020912dfa221 54 #else
johnAlexander 5:b233bdf91671 55 Serial pc(SERIAL_TX, SERIAL_RX);
charlesmn 0:020912dfa221 56 #endif
charlesmn 0:020912dfa221 57
johnAlexander 3:c1e893e6752f 58 void print_results(int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData );
charlesmn 0:020912dfa221 59
johnAlexander 3:c1e893e6752f 60
johnAlexander 3:c1e893e6752f 61 VL53L1_Dev_t devCentre;
johnAlexander 3:c1e893e6752f 62 VL53L1_DEV Dev = &devCentre;
johnAlexander 5:b233bdf91671 63
johnAlexander 4:0ac9998b69ac 64 VL53L1 *Sensor;
johnAlexander 4:0ac9998b69ac 65
charlesmn 0:020912dfa221 66
johnAlexander 5:b233bdf91671 67
johnAlexander 3:c1e893e6752f 68 /* flags that handle interrupt request for sensor and user blue button*/
johnAlexander 3:c1e893e6752f 69 volatile bool int_sensor = false;
johnAlexander 3:c1e893e6752f 70 volatile bool int_stop = false;
johnAlexander 3:c1e893e6752f 71
johnAlexander 3:c1e893e6752f 72 /* ISR callback function of the centre sensor */
johnAlexander 3:c1e893e6752f 73 void sensor_irq(void)
johnAlexander 3:c1e893e6752f 74 {
johnAlexander 3:c1e893e6752f 75 int_sensor = true;
johnAlexander 3:c1e893e6752f 76 board->sensor_centre->disable_interrupt_measure_detection_irq();
johnAlexander 3:c1e893e6752f 77 }
johnAlexander 3:c1e893e6752f 78
johnAlexander 3:c1e893e6752f 79 /* Start the sensor ranging */
johnAlexander 3:c1e893e6752f 80 int init_sensor()
johnAlexander 3:c1e893e6752f 81 {
johnAlexander 3:c1e893e6752f 82 int status = 0;
johnAlexander 5:b233bdf91671 83
johnAlexander 4:0ac9998b69ac 84 Dev=&devCentre;
johnAlexander 4:0ac9998b69ac 85 Sensor=board->sensor_centre;
johnAlexander 5:b233bdf91671 86
johnAlexander 4:0ac9998b69ac 87 // configure the sensors
johnAlexander 5:b233bdf91671 88 Dev->comms_speed_khz = 400;
johnAlexander 4:0ac9998b69ac 89 Dev->comms_type = 1;
johnAlexander 4:0ac9998b69ac 90 Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS;
johnAlexander 5:b233bdf91671 91
johnAlexander 5:b233bdf91671 92 printf("configuring centre channel \n");
johnAlexander 5:b233bdf91671 93
johnAlexander 5:b233bdf91671 94 /* Device Initialization and setting */
johnAlexander 4:0ac9998b69ac 95 status = Sensor->vl53L1_DataInit();
johnAlexander 4:0ac9998b69ac 96 status = Sensor->vl53L1_StaticInit();
johnAlexander 4:0ac9998b69ac 97 status = Sensor->vl53L1_SetPresetMode(VL53L1_PRESETMODE_AUTONOMOUS);
johnAlexander 4:0ac9998b69ac 98 status = Sensor->vl53L1_SetDistanceMode(VL53L1_DISTANCEMODE_LONG);
johnAlexander 5:b233bdf91671 99 status = Sensor->vl53L1_SetMeasurementTimingBudgetMicroSeconds( 200 * 1000);
johnAlexander 4:0ac9998b69ac 100
johnAlexander 5:b233bdf91671 101
johnAlexander 4:0ac9998b69ac 102 // set the ranging and signal rate filter
johnAlexander 4:0ac9998b69ac 103 VL53L1_DetectionConfig_t thresholdconfig;
johnAlexander 4:0ac9998b69ac 104
johnAlexander 4:0ac9998b69ac 105 thresholdconfig.DetectionMode = VL53L1_DETECTION_DISTANCE_ONLY; /// type VL53L1_DetectionMode in vl53l1_def.h
johnAlexander 5:b233bdf91671 106 thresholdconfig.Distance.CrossMode = VL53L1_THRESHOLD_IN_WINDOW; // type VL53L1_ThresholdMode. ignore if distance outside high and low
johnAlexander 4:0ac9998b69ac 107 thresholdconfig.Distance.High = 300; // high distance in mm
johnAlexander 4:0ac9998b69ac 108 thresholdconfig.Distance.Low = 200; // low distance in mm
johnAlexander 5:b233bdf91671 109 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 110 thresholdconfig.Rate.High = 0;
johnAlexander 4:0ac9998b69ac 111 thresholdconfig.Rate.Low = 0;
johnAlexander 5:b233bdf91671 112 thresholdconfig.IntrNoTarget = 0 ;// if 1 produce an interrupt even if there is no target found e.g out of range
johnAlexander 5:b233bdf91671 113
johnAlexander 4:0ac9998b69ac 114 status = Sensor->vl53L1_SetThresholdConfig(&thresholdconfig);
johnAlexander 5:b233bdf91671 115
johnAlexander 4:0ac9998b69ac 116 // create interrupt handler and start measurements
johnAlexander 5:b233bdf91671 117 if (board->sensor_centre!= NULL) {
johnAlexander 3:c1e893e6752f 118 status = board->sensor_centre->stop_measurement();
johnAlexander 3:c1e893e6752f 119 if (status != 0) {
johnAlexander 4:0ac9998b69ac 120 return status;
johnAlexander 3:c1e893e6752f 121 }
johnAlexander 3:c1e893e6752f 122
johnAlexander 3:c1e893e6752f 123 status = board->sensor_centre->start_measurement(&sensor_irq);
johnAlexander 3:c1e893e6752f 124 if (status != 0) {
johnAlexander 3:c1e893e6752f 125 return status;
johnAlexander 3:c1e893e6752f 126 }
johnAlexander 3:c1e893e6752f 127 }
johnAlexander 3:c1e893e6752f 128 return status;
johnAlexander 3:c1e893e6752f 129 }
johnAlexander 3:c1e893e6752f 130
johnAlexander 3:c1e893e6752f 131 /* ISR callback function of the user blue button to switch measuring sensor. */
johnAlexander 3:c1e893e6752f 132 void measuring_stop_irq(void)
johnAlexander 3:c1e893e6752f 133 {
johnAlexander 3:c1e893e6752f 134 int_stop = true;
johnAlexander 3:c1e893e6752f 135 }
johnAlexander 3:c1e893e6752f 136
charlesmn 0:020912dfa221 137 /*=================================== Main ==================================
charlesmn 0:020912dfa221 138 =============================================================================*/
charlesmn 0:020912dfa221 139 int main()
johnAlexander 5:b233bdf91671 140 {
charlesmn 0:020912dfa221 141 int status;
johnAlexander 4:0ac9998b69ac 142 uint16_t distance = 0;
johnAlexander 5:b233bdf91671 143
charlesmn 0:020912dfa221 144 pc.baud(115200); // baud rate is important as printf statements take a lot of time
johnAlexander 5:b233bdf91671 145
johnAlexander 5:b233bdf91671 146 printf("mbed version : %d \r\n",MBED_VERSION);
charlesmn 0:020912dfa221 147
charlesmn 0:020912dfa221 148 // create i2c interface
charlesmn 0:020912dfa221 149 ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL);
johnAlexander 3:c1e893e6752f 150 /* creates the 53L1A2 expansion board singleton obj */
johnAlexander 3:c1e893e6752f 151 board = XNucleo53L1A2::instance(dev_I2C, CentreIntPin, LeftIntPin, RightIntPin);
johnAlexander 5:b233bdf91671 152
charlesmn 0:020912dfa221 153 printf("board created!\r\n");
charlesmn 0:020912dfa221 154
charlesmn 0:020912dfa221 155 /* init the 53L1A1 expansion board with default values */
charlesmn 0:020912dfa221 156 status = board->init_board();
charlesmn 0:020912dfa221 157 if (status) {
charlesmn 0:020912dfa221 158 printf("Failed to init board!\r\n");
johnAlexander 5:b233bdf91671 159 return status;
charlesmn 0:020912dfa221 160 }
johnAlexander 5:b233bdf91671 161
charlesmn 0:020912dfa221 162 printf("board initiated! - %d\r\n", status);
johnAlexander 5:b233bdf91671 163
johnAlexander 3:c1e893e6752f 164 /* init an array with chars to id the sensors */
johnAlexander 3:c1e893e6752f 165 status = init_sensor();
johnAlexander 3:c1e893e6752f 166 if (status != 0) {
johnAlexander 3:c1e893e6752f 167 printf("Failed to init sensors!\r\n");
johnAlexander 3:c1e893e6752f 168 return status;
johnAlexander 3:c1e893e6752f 169 }
johnAlexander 3:c1e893e6752f 170
johnAlexander 5:b233bdf91671 171 printf("loop forever\n");
johnAlexander 3:c1e893e6752f 172
johnAlexander 4:0ac9998b69ac 173 VL53L1_MultiRangingData_t MultiRangingData;
johnAlexander 5:b233bdf91671 174 VL53L1_MultiRangingData_t *pMultiRangingData = NULL;
johnAlexander 5:b233bdf91671 175
johnAlexander 3:c1e893e6752f 176 while (true) {
johnAlexander 5:b233bdf91671 177 pMultiRangingData = &MultiRangingData;
johnAlexander 5:b233bdf91671 178
johnAlexander 3:c1e893e6752f 179 if (int_sensor) {
johnAlexander 3:c1e893e6752f 180 int_sensor = false;
johnAlexander 4:0ac9998b69ac 181 status = board->sensor_centre->vl53L1_GetMultiRangingData( pMultiRangingData);
johnAlexander 5:b233bdf91671 182 if (status == 0) {
johnAlexander 4:0ac9998b69ac 183 print_results( devCentre.i2c_slave_address, pMultiRangingData );
johnAlexander 4:0ac9998b69ac 184 }
johnAlexander 4:0ac9998b69ac 185 status = board->sensor_centre->VL53L1_ClearInterrupt();
johnAlexander 4:0ac9998b69ac 186 board->sensor_centre->enable_interrupt_measure_detection_irq();
charlesmn 0:020912dfa221 187 }
charlesmn 0:020912dfa221 188
johnAlexander 3:c1e893e6752f 189 if (int_stop) {
johnAlexander 3:c1e893e6752f 190 printf("\r\nEnding loop mode \r\n");
johnAlexander 3:c1e893e6752f 191 break;
charlesmn 0:020912dfa221 192 }
charlesmn 0:020912dfa221 193 }
johnAlexander 5:b233bdf91671 194
johnAlexander 5:b233bdf91671 195 printf("Terminating.\n");
johnAlexander 3:c1e893e6752f 196 }
johnAlexander 5:b233bdf91671 197
johnAlexander 5:b233bdf91671 198
johnAlexander 5:b233bdf91671 199 // print what ever results are required
johnAlexander 2:f0ec92af4b5f 200 void print_results( int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData )
charlesmn 0:020912dfa221 201 {
johnAlexander 2:f0ec92af4b5f 202 int no_of_object_found=pMultiRangingData->NumberOfObjectsFound;
johnAlexander 5:b233bdf91671 203
johnAlexander 2:f0ec92af4b5f 204 int RoiNumber=pMultiRangingData->RoiNumber;
charlesmn 0:020912dfa221 205
johnAlexander 5:b233bdf91671 206 if (( no_of_object_found < 10 ) && ( no_of_object_found != 0)) {
johnAlexander 5:b233bdf91671 207 for(int j=0; j<no_of_object_found; j++) {
johnAlexander 5:b233bdf91671 208 if ((pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) ||
johnAlexander 5:b233bdf91671 209 (pMultiRangingData->RangeData[j].RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL)) {
johnAlexander 2:f0ec92af4b5f 210 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 211 devNumber, RoiNumber,
johnAlexander 5:b233bdf91671 212 pMultiRangingData->RangeData[j].RangeStatus,
johnAlexander 5:b233bdf91671 213 pMultiRangingData->RangeData[j].RangeMilliMeter,
johnAlexander 5:b233bdf91671 214 pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65535.0,
johnAlexander 5:b233bdf91671 215 pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65535.0);
johnAlexander 2:f0ec92af4b5f 216 }
johnAlexander 2:f0ec92af4b5f 217 }
johnAlexander 5:b233bdf91671 218 } // if (( no_of_object_found < 10 ) && ( no_of_object_found != 0))
johnAlexander 5:b233bdf91671 219 }
charlesmn 0:020912dfa221 220
johnAlexander 5:b233bdf91671 221
charlesmn 0:020912dfa221 222 #if (MBED_VERSION > 60300)
charlesmn 0:020912dfa221 223 extern "C" void wait_ms(int ms)
johnAlexander 5:b233bdf91671 224 {
charlesmn 0:020912dfa221 225 thread_sleep_for(ms);
johnAlexander 5:b233bdf91671 226 }
johnAlexander 5:b233bdf91671 227 #endif
johnAlexander 5:b233bdf91671 228
johnAlexander 5:b233bdf91671 229