Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed X_NUCLEO_53L1A1_mbed
main.cpp
00001 /* 00002 * This VL53L1X Expansion board sample application performs range measurements 00003 * with interrupts enabled to generate a hardware interrupt each time a new 00004 * measurement is ready to be read. 00005 * 00006 * Measured ranges are output on the Serial Port, running at 115200 baud. 00007 * 00008 * The application supports the centre, on-board, sensor and up to two satellite boards. 00009 * 00010 * The User Blue button switches between the currently selected sensor to display range 00011 * results from. 00012 * 00013 * The Black Reset button is used to restart the program. 00014 * 00015 * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of 00016 * the X-NUCELO-53L0A1 expansion board are not made/OFF. 00017 * These links must be made to allow interrupts from the Satellite boards 00018 * to be received. 00019 * U11 and U18 must be made/ON to allow interrupts to be received from the 00020 * INT_L & INT_R positions; or 00021 * U10 and U15 must be made/ON to allow interrupts to be received from the 00022 * Alternate INT_L & INT_R positions. 00023 * The X_NUCLEO_53L1A1 firmware library defaults to use the INT_L/INT_R 00024 * positions. 00025 * INT_L is available on expansion board Arduino Connector CN5, pin 1 as D9. 00026 * Alternate INT_L is on CN5 Connector pin 2 as D8. 00027 * INT_R is available on expansion board Arduino Connector CN9, pin 3 as D4. 00028 * Alternate INT_R is on CN9 Connector pin 5 as D2. 00029 * The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A1/ 00030 */ 00031 00032 #include <stdio.h> 00033 00034 #include "mbed.h" 00035 #include "XNucleo53L1A1.h" 00036 #include "vl53L1x_I2c.h" 00037 00038 #define VL53L1_I2C_SDA D14 00039 #define VL53L1_I2C_SCL D15 00040 00041 #if USER_BUTTON==PC_13 // we are cross compiling for Nucleo-64s 00042 //InterruptIn stop_button(USER_BUTTON); 00043 #endif 00044 00045 /* Installed sensors count */ 00046 int sensorCnt = 0; 00047 00048 /* installed sensors prefixes */ 00049 char installedSensors[3]; 00050 00051 static XNucleo53L1A1 *board=NULL; 00052 //Serial pc(SERIAL_TX, SERIAL_RX); 00053 00054 /* interrupt requests */ 00055 volatile bool centerSensor = false; 00056 volatile bool leftSensor = false; 00057 volatile bool rightSensor = false; 00058 volatile bool int_measuring_stop = false; 00059 /* Current sensor number*/ 00060 volatile int currentSensor = 0; 00061 00062 /* current displayed sensor change IRQ */ 00063 volatile bool switchChanged = false; 00064 00065 /* ISR callback function of the centre sensor */ 00066 void sensor_centre_irq(void) 00067 { 00068 centerSensor = true; 00069 board->sensor_centre->disable_interrupt_measure_detection_irq(); 00070 } 00071 00072 /* ISR callback function of the left sensor */ 00073 void sensor_left_irq(void) 00074 { 00075 leftSensor = true; 00076 board->sensor_left->disable_interrupt_measure_detection_irq(); 00077 } 00078 00079 /* ISR callback function of the right sensor */ 00080 void sensor_right_irq(void) 00081 { 00082 rightSensor = true; 00083 board->sensor_right->disable_interrupt_measure_detection_irq(); 00084 } 00085 00086 /* ISR callback function of the user blue button to switch measuring sensor. */ 00087 void switch_measuring_sensor_irq(void) 00088 { 00089 stop_button.disable_irq(); 00090 switchChanged = true; 00091 } 00092 00093 /* 00094 * This function calls the interrupt handler for each sensor 00095 * and outputs the range 00096 */ 00097 inline void measure_sensors() 00098 { 00099 bool current = false; 00100 uint16_t distance = 0; 00101 00102 /* Handle the interrupt and output the range from the centre sensor */ 00103 if (centerSensor) { 00104 centerSensor = false; 00105 board->sensor_centre->handle_irq(&distance); 00106 current = (currentSensor == 0); 00107 if (current) { 00108 printf("Centre: %d\r\n", distance); 00109 } 00110 } 00111 00112 /* Handle the interrupt and output the range from the left sensor */ 00113 if (leftSensor) { 00114 leftSensor = false; 00115 board->sensor_left->handle_irq(&distance); 00116 current = (installedSensors[currentSensor] == 'L'); 00117 if (current) { 00118 printf("Left: %d\r\n", distance); 00119 } 00120 } 00121 00122 /* Handle the interrupt and output the range from the right sensor */ 00123 if (rightSensor) { 00124 rightSensor = false; 00125 board->sensor_right->handle_irq(&distance); 00126 current = (installedSensors[currentSensor] == 'R'); 00127 if (current) { 00128 printf("Right: %d\r\n", distance); 00129 } 00130 } 00131 } 00132 00133 /* 00134 * Add to an array a character that represents the sensor and start ranging 00135 */ 00136 int init_sensors_array() 00137 { 00138 int status = 0; 00139 sensorCnt = 0; 00140 /* start the measure on the center sensor */ 00141 if (NULL != board->sensor_centre) { 00142 installedSensors[sensorCnt] = 'C'; 00143 status = board->sensor_centre->stop_measurement(); 00144 if (status != 0) { 00145 return status; 00146 } 00147 status = board->sensor_centre->start_measurement(&sensor_centre_irq); 00148 if (status != 0) { 00149 return status; 00150 } 00151 ++sensorCnt; 00152 } 00153 /* start the measure on the left sensor */ 00154 if (NULL != board->sensor_left) { 00155 installedSensors[sensorCnt] = 'L'; 00156 status = board->sensor_left->stop_measurement(); 00157 if (status != 0) { 00158 return status; 00159 } 00160 status = board->sensor_left->start_measurement(&sensor_left_irq); 00161 if (status != 0) { 00162 return status; 00163 } 00164 ++sensorCnt; 00165 } 00166 /* start the measure on the right sensor */ 00167 if (NULL != board->sensor_right) { 00168 installedSensors[sensorCnt] = 'R'; 00169 status = board->sensor_right->stop_measurement(); 00170 if (status != 0) { 00171 return status; 00172 } 00173 status = board->sensor_right->start_measurement(&sensor_right_irq); 00174 if (status != 0) { 00175 return status; 00176 } 00177 ++sensorCnt; 00178 } 00179 currentSensor = 0; 00180 return status; 00181 } 00182 00183 /* 00184 * Main ranging function 00185 */ 00186 int range_measure(vl53L1X_DevI2C *device_i2c) 00187 { 00188 int status = 0; 00189 00190 /* creates the 53L1A1 expansion board singleton obj */ 00191 board = XNucleo53L1A1::instance(device_i2c, A2, D9, D2); 00192 00193 /* init the 53L1A1 expansion board with default values */ 00194 status = board->init_board(); 00195 if (status != 0) { 00196 printf("Failed to init board!\r\n"); 00197 return status; 00198 } 00199 00200 /* init an array with chars to id the sensors */ 00201 status = init_sensors_array(); 00202 if (status != 0) { 00203 printf("Failed to init sensors!\r\n"); 00204 return status; 00205 } 00206 00207 printf("Entering loop mode\r\n"); 00208 00209 /* Main ranging interrupt loop */ 00210 while (true) { 00211 measure_sensors(); 00212 if (switchChanged) { 00213 ++currentSensor; 00214 if (currentSensor == sensorCnt) 00215 currentSensor = 0; 00216 printf("Sensor changed to %c\r\n", installedSensors[currentSensor]); 00217 switchChanged = false; 00218 stop_button.enable_irq(); 00219 } 00220 } 00221 delete board; 00222 return status; 00223 } 00224 00225 /*=================================== Main ================================== 00226 =============================================================================*/ 00227 int main() 00228 { 00229 #if USER_BUTTON==PC_13 // we are cross compiling for Nucleo-f401 00230 stop_button.rise(&switch_measuring_sensor_irq); 00231 stop_button.enable_irq(); 00232 #endif 00233 vl53L1X_DevI2C *dev_I2C = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL); 00234 range_measure(dev_I2C); // start continuous measures 00235 return 0; 00236 } 00237
Generated on Wed Jul 13 2022 04:10:26 by
1.7.2