as per name

Dependencies:   VL53L1CB

Committer:
charlesmn
Date:
Mon Jun 14 09:36:20 2021 +0000
Revision:
0:86d5438023b9
Child:
1:9f70f022f5be
first release

Who changed what in which revision?

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