Sample program for 3 sensors using polling in autonomous mode.

Dependencies:   X_NUCLEO_53L1A2

Committer:
johnAlexander
Date:
Fri May 21 08:38:45 2021 +0000
Revision:
7:242f30acc456
Parent:
6:19d56b30bfa7
Child:
8:ac303cd2d35f
Match autonomous setup to C, ULD driver example's settings.; Update board class.; Target MbedOS v6.10.0.

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 7:242f30acc456 53 //PinName LeftIntPin = D8;
johnAlexander 6:19d56b30bfa7 54 PinName RightIntPin = D2;
johnAlexander 6:19d56b30bfa7 55 // alternate set
johnAlexander 7:242f30acc456 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 7:242f30acc456 107
johnAlexander 4:0ac9998b69ac 108 status = Sensor->vl53L1_SetPresetMode(VL53L1_PRESETMODE_AUTONOMOUS);
johnAlexander 4:0ac9998b69ac 109 status = Sensor->vl53L1_SetDistanceMode(VL53L1_DISTANCEMODE_LONG);
johnAlexander 4:0ac9998b69ac 110
johnAlexander 7:242f30acc456 111 status = Sensor->vl53L1_SetInterMeasurementPeriodMilliSeconds(500);
johnAlexander 4:0ac9998b69ac 112
johnAlexander 7:242f30acc456 113 status = Sensor->vl53L1_SetMeasurementTimingBudgetMicroSeconds(45000);
johnAlexander 5:b233bdf91671 114
johnAlexander 7:242f30acc456 115 status = Sensor->vl53L1_SetSequenceStepEnable(VL53L1_SEQUENCESTEP_MM1, 0);
johnAlexander 7:242f30acc456 116 status = Sensor->vl53L1_SetSequenceStepEnable(VL53L1_SEQUENCESTEP_MM2, 0);
johnAlexander 7:242f30acc456 117
johnAlexander 5:b233bdf91671 118
johnAlexander 4:0ac9998b69ac 119 // create interrupt handler and start measurements
johnAlexander 5:b233bdf91671 120 if (board->sensor_centre!= NULL) {
johnAlexander 3:c1e893e6752f 121 status = board->sensor_centre->stop_measurement();
johnAlexander 3:c1e893e6752f 122 if (status != 0) {
johnAlexander 4:0ac9998b69ac 123 return status;
johnAlexander 3:c1e893e6752f 124 }
johnAlexander 3:c1e893e6752f 125
johnAlexander 3:c1e893e6752f 126 status = board->sensor_centre->start_measurement(&sensor_irq);
johnAlexander 3:c1e893e6752f 127 if (status != 0) {
johnAlexander 3:c1e893e6752f 128 return status;
johnAlexander 3:c1e893e6752f 129 }
johnAlexander 3:c1e893e6752f 130 }
johnAlexander 3:c1e893e6752f 131 return status;
johnAlexander 3:c1e893e6752f 132 }
johnAlexander 3:c1e893e6752f 133
johnAlexander 3:c1e893e6752f 134 /* ISR callback function of the user blue button to switch measuring sensor. */
johnAlexander 3:c1e893e6752f 135 void measuring_stop_irq(void)
johnAlexander 3:c1e893e6752f 136 {
johnAlexander 3:c1e893e6752f 137 int_stop = true;
johnAlexander 3:c1e893e6752f 138 }
johnAlexander 3:c1e893e6752f 139
charlesmn 0:020912dfa221 140 /*=================================== Main ==================================
charlesmn 0:020912dfa221 141 =============================================================================*/
charlesmn 0:020912dfa221 142 int main()
johnAlexander 5:b233bdf91671 143 {
charlesmn 0:020912dfa221 144 int status;
johnAlexander 4:0ac9998b69ac 145 uint16_t distance = 0;
johnAlexander 5:b233bdf91671 146
charlesmn 0:020912dfa221 147 pc.baud(115200); // baud rate is important as printf statements take a lot of time
johnAlexander 5:b233bdf91671 148
johnAlexander 5:b233bdf91671 149 printf("mbed version : %d \r\n",MBED_VERSION);
charlesmn 0:020912dfa221 150
charlesmn 0:020912dfa221 151 // create i2c interface
charlesmn 0:020912dfa221 152 ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL);
johnAlexander 3:c1e893e6752f 153 /* creates the 53L1A2 expansion board singleton obj */
johnAlexander 6:19d56b30bfa7 154 board = XNucleo53L1A2::instance(dev_I2C, CentreIntPin, LeftIntPin, RightIntPin);
johnAlexander 5:b233bdf91671 155
charlesmn 0:020912dfa221 156 printf("board created!\r\n");
charlesmn 0:020912dfa221 157
charlesmn 0:020912dfa221 158 /* init the 53L1A1 expansion board with default values */
charlesmn 0:020912dfa221 159 status = board->init_board();
charlesmn 0:020912dfa221 160 if (status) {
charlesmn 0:020912dfa221 161 printf("Failed to init board!\r\n");
johnAlexander 5:b233bdf91671 162 return status;
charlesmn 0:020912dfa221 163 }
johnAlexander 5:b233bdf91671 164
charlesmn 0:020912dfa221 165 printf("board initiated! - %d\r\n", status);
johnAlexander 5:b233bdf91671 166
johnAlexander 3:c1e893e6752f 167 /* init an array with chars to id the sensors */
johnAlexander 3:c1e893e6752f 168 status = init_sensor();
johnAlexander 3:c1e893e6752f 169 if (status != 0) {
johnAlexander 3:c1e893e6752f 170 printf("Failed to init sensors!\r\n");
johnAlexander 3:c1e893e6752f 171 return status;
johnAlexander 3:c1e893e6752f 172 }
johnAlexander 3:c1e893e6752f 173
johnAlexander 5:b233bdf91671 174 printf("loop forever\n");
johnAlexander 3:c1e893e6752f 175
johnAlexander 4:0ac9998b69ac 176 VL53L1_MultiRangingData_t MultiRangingData;
johnAlexander 5:b233bdf91671 177 VL53L1_MultiRangingData_t *pMultiRangingData = NULL;
johnAlexander 5:b233bdf91671 178
johnAlexander 3:c1e893e6752f 179 while (true) {
johnAlexander 5:b233bdf91671 180 pMultiRangingData = &MultiRangingData;
johnAlexander 5:b233bdf91671 181
johnAlexander 3:c1e893e6752f 182 if (int_sensor) {
johnAlexander 3:c1e893e6752f 183 int_sensor = false;
johnAlexander 7:242f30acc456 184 status = board->sensor_centre->vl53L1_GetMultiRangingData( pMultiRangingData);
johnAlexander 7:242f30acc456 185
johnAlexander 4:0ac9998b69ac 186 print_results( devCentre.i2c_slave_address, pMultiRangingData );
charlesmn 0:020912dfa221 187
johnAlexander 7:242f30acc456 188 status = board->sensor_centre->VL53L1_ClearInterrupt();
johnAlexander 7:242f30acc456 189 board->sensor_centre->enable_interrupt_measure_detection_irq();
charlesmn 0:020912dfa221 190 }
charlesmn 0:020912dfa221 191 }
johnAlexander 5:b233bdf91671 192
johnAlexander 5:b233bdf91671 193 printf("Terminating.\n");
johnAlexander 3:c1e893e6752f 194 }
johnAlexander 5:b233bdf91671 195
johnAlexander 5:b233bdf91671 196
johnAlexander 5:b233bdf91671 197 // print what ever results are required
johnAlexander 2:f0ec92af4b5f 198 void print_results( int devNumber, VL53L1_MultiRangingData_t *pMultiRangingData )
charlesmn 0:020912dfa221 199 {
johnAlexander 7:242f30acc456 200 int no_of_object_found = pMultiRangingData->NumberOfObjectsFound;
johnAlexander 6:19d56b30bfa7 201 int signal_rate = 0;
johnAlexander 6:19d56b30bfa7 202 int ambient_rate = 0;
johnAlexander 5:b233bdf91671 203
johnAlexander 7:242f30acc456 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 6:19d56b30bfa7 210 signal_rate = pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65535;
johnAlexander 6:19d56b30bfa7 211 ambient_rate = pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65535;
johnAlexander 6:19d56b30bfa7 212 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 213 devNumber, RoiNumber,
johnAlexander 6:19d56b30bfa7 214 pMultiRangingData->RangeData[j].RangeStatus,
johnAlexander 6:19d56b30bfa7 215 pMultiRangingData->RangeData[j].RangeMilliMeter,
johnAlexander 6:19d56b30bfa7 216 signal_rate,
johnAlexander 6:19d56b30bfa7 217 ambient_rate);
johnAlexander 6:19d56b30bfa7 218 /*
johnAlexander 6:19d56b30bfa7 219 // online compiler disables printf() / floating-point support, for code-size reasons.
johnAlexander 6:19d56b30bfa7 220 // offline compiler can switch it on.
johnAlexander 2:f0ec92af4b5f 221 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 222 devNumber, RoiNumber,
johnAlexander 5:b233bdf91671 223 pMultiRangingData->RangeData[j].RangeStatus,
johnAlexander 5:b233bdf91671 224 pMultiRangingData->RangeData[j].RangeMilliMeter,
johnAlexander 5:b233bdf91671 225 pMultiRangingData->RangeData[j].SignalRateRtnMegaCps / 65535.0,
johnAlexander 5:b233bdf91671 226 pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps / 65535.0);
johnAlexander 6:19d56b30bfa7 227 */
johnAlexander 2:f0ec92af4b5f 228 }
johnAlexander 2:f0ec92af4b5f 229 }
johnAlexander 5:b233bdf91671 230 } // if (( no_of_object_found < 10 ) && ( no_of_object_found != 0))
johnAlexander 5:b233bdf91671 231 }
charlesmn 0:020912dfa221 232
johnAlexander 5:b233bdf91671 233
charlesmn 0:020912dfa221 234 #if (MBED_VERSION > 60300)
charlesmn 0:020912dfa221 235 extern "C" void wait_ms(int ms)
johnAlexander 5:b233bdf91671 236 {
charlesmn 0:020912dfa221 237 thread_sleep_for(ms);
johnAlexander 5:b233bdf91671 238 }
johnAlexander 5:b233bdf91671 239 #endif
johnAlexander 5:b233bdf91671 240
johnAlexander 5:b233bdf91671 241