Detected people approaching if the distance is < 6 feet then LEDs blinking RED otherwise LEDs light green.

Dependencies:   mbed X_NUCLEO_53L1A1_mbed WS2812

Committer:
elab
Date:
Tue Sep 29 09:30:43 2020 +0000
Revision:
0:ea8694e55809
first version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elab 0:ea8694e55809 1 /*
elab 0:ea8694e55809 2 * This VL53L1X satellite board sample application performs range measurements
elab 0:ea8694e55809 3 * with interrupts enabled to generate a hardware interrupt each time a new
elab 0:ea8694e55809 4 * measurement is ready to be read.
elab 0:ea8694e55809 5 *
elab 0:ea8694e55809 6 * Measured ranges are output on the Serial Port, running at 115200 baud.
elab 0:ea8694e55809 7 *
elab 0:ea8694e55809 8 * On STM32-Nucleo boards :
elab 0:ea8694e55809 9 * The User Blue button stops the current measurement and entire program,
elab 0:ea8694e55809 10 * releasing all resources.
elab 0:ea8694e55809 11 *
elab 0:ea8694e55809 12 * The Black Reset button is used to restart the program.
elab 0:ea8694e55809 13 *
elab 0:ea8694e55809 14 * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of
elab 0:ea8694e55809 15 * the X-NUCELO-53L1A1 expansion board are not made/OFF.
elab 0:ea8694e55809 16 * These links must be made to allow interrupts from the Satellite boards
elab 0:ea8694e55809 17 * to be received.
elab 0:ea8694e55809 18 * U11 and U18 must be made/ON to allow interrupts to be received from the
elab 0:ea8694e55809 19 * INT_L & INT_R positions; or
elab 0:ea8694e55809 20 * U10 and U15 must be made/ON to allow interrupts to be received from the
elab 0:ea8694e55809 21 * Alternate INT_L & INT_R positions.
elab 0:ea8694e55809 22 * The X_NUCLEO_53L1A1 firmware library defaults to use the INT_L/INT_R
elab 0:ea8694e55809 23 * positions.
elab 0:ea8694e55809 24 * INT_L is available on expansion board Arduino Connector CN5, pin 1 as D9.
elab 0:ea8694e55809 25 * Alternate INT_L is on CN5 Connector pin 2 as D8.
elab 0:ea8694e55809 26 * INT_R is available on expansion board Arduino Connector CN9, pin 3 as D4.
elab 0:ea8694e55809 27 * Alternate INT_R is on CN9 Connector pin 5 as D2.
elab 0:ea8694e55809 28 * The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A1/
elab 0:ea8694e55809 29 */
elab 0:ea8694e55809 30
elab 0:ea8694e55809 31 #include <stdio.h>
elab 0:ea8694e55809 32
elab 0:ea8694e55809 33 #include "mbed.h"
elab 0:ea8694e55809 34 #include "VL53L1X_I2C.h"
elab 0:ea8694e55809 35 #include "VL53L1X_Class.h"
elab 0:ea8694e55809 36 #include "LED_WS2812.h"
elab 0:ea8694e55809 37
elab 0:ea8694e55809 38 #define VL53L1_I2C_SDA D14
elab 0:ea8694e55809 39 #define VL53L1_I2C_SCL D15
elab 0:ea8694e55809 40
elab 0:ea8694e55809 41 #if TARGET_STM // we are cross compiling for an STM32-Nucleo
elab 0:ea8694e55809 42 InterruptIn stop_button(USER_BUTTON);
elab 0:ea8694e55809 43 #endif
elab 0:ea8694e55809 44 #if TARGET_Freescale // we are cross-compiling for NXP FRDM boards.
elab 0:ea8694e55809 45 InterruptIn stop_button(SW2);
elab 0:ea8694e55809 46 #endif
elab 0:ea8694e55809 47
elab 0:ea8694e55809 48 static VL53L1X *sensor = NULL;
elab 0:ea8694e55809 49 Serial pc(SERIAL_TX, SERIAL_RX);
elab 0:ea8694e55809 50
elab 0:ea8694e55809 51 /* flags that handle interrupt request for sensor and user blue button*/
elab 0:ea8694e55809 52 volatile bool int_sensor = false;
elab 0:ea8694e55809 53 volatile bool int_stop = false;
elab 0:ea8694e55809 54
elab 0:ea8694e55809 55
elab 0:ea8694e55809 56 /* ISR callback function of the sensor */
elab 0:ea8694e55809 57 void sensor_irq(void)
elab 0:ea8694e55809 58 {
elab 0:ea8694e55809 59 int_sensor = true;
elab 0:ea8694e55809 60 sensor->disable_interrupt_measure_detection_irq();
elab 0:ea8694e55809 61 }
elab 0:ea8694e55809 62
elab 0:ea8694e55809 63 /* ISR callback function of the user blue button to switch measuring sensor. */
elab 0:ea8694e55809 64 void measuring_stop_irq(void)
elab 0:ea8694e55809 65 {
elab 0:ea8694e55809 66 int_stop = true;
elab 0:ea8694e55809 67 }
elab 0:ea8694e55809 68
elab 0:ea8694e55809 69 /* Start the sensor ranging */
elab 0:ea8694e55809 70 int start_ranging()
elab 0:ea8694e55809 71 {
elab 0:ea8694e55809 72 int status = 0;
elab 0:ea8694e55809 73 /* start the measure on the sensor */
elab 0:ea8694e55809 74 if (NULL != sensor) {
elab 0:ea8694e55809 75 status = sensor->stop_measurement();
elab 0:ea8694e55809 76 if (status != 0) {
elab 0:ea8694e55809 77 return status;
elab 0:ea8694e55809 78 }
elab 0:ea8694e55809 79
elab 0:ea8694e55809 80 status = sensor->start_measurement(&sensor_irq);
elab 0:ea8694e55809 81 if (status != 0) {
elab 0:ea8694e55809 82 return status;
elab 0:ea8694e55809 83 }
elab 0:ea8694e55809 84 }
elab 0:ea8694e55809 85 return status;
elab 0:ea8694e55809 86 }
elab 0:ea8694e55809 87
elab 0:ea8694e55809 88 int range_measure(VL53L1X_DevI2C *device_i2c)
elab 0:ea8694e55809 89 {
elab 0:ea8694e55809 90 int status = 0;
elab 0:ea8694e55809 91 uint16_t distance = 0;
elab 0:ea8694e55809 92 /* Create a xshutdown pin */
elab 0:ea8694e55809 93 DigitalOut xshutdown(D7);
elab 0:ea8694e55809 94
elab 0:ea8694e55809 95
elab 0:ea8694e55809 96 /* create instance of sensor class */
elab 0:ea8694e55809 97 sensor = new VL53L1X(device_i2c, &xshutdown, A2);
elab 0:ea8694e55809 98
elab 0:ea8694e55809 99 sensor->vl53l1_off();
elab 0:ea8694e55809 100 /* initialise sensor */
elab 0:ea8694e55809 101 sensor->init_sensor(0x52);
elab 0:ea8694e55809 102
elab 0:ea8694e55809 103 if (status) {
elab 0:ea8694e55809 104 delete sensor;
elab 0:ea8694e55809 105 sensor= NULL;
elab 0:ea8694e55809 106 printf("Sensor centre not present\n\r");
elab 0:ea8694e55809 107 }
elab 0:ea8694e55809 108
elab 0:ea8694e55809 109 /* init an array with chars to id the sensors */
elab 0:ea8694e55809 110 status = start_ranging();
elab 0:ea8694e55809 111 if (status != 0) {
elab 0:ea8694e55809 112 printf("Failed to start ranging!\r\n");
elab 0:ea8694e55809 113 return status;
elab 0:ea8694e55809 114 }
elab 0:ea8694e55809 115
elab 0:ea8694e55809 116 if (NULL != sensor) {
elab 0:ea8694e55809 117 printf("Entering loop mode\r\n");
elab 0:ea8694e55809 118 /* Main ranging interrupt loop */
elab 0:ea8694e55809 119 while (true) {
elab 0:ea8694e55809 120 if (int_sensor) {
elab 0:ea8694e55809 121 int_sensor = false;
elab 0:ea8694e55809 122 status = sensor->handle_irq(&distance);
elab 0:ea8694e55809 123 printf("distance: %d\r\n", distance);
elab 0:ea8694e55809 124 }
elab 0:ea8694e55809 125
elab 0:ea8694e55809 126 if (int_stop) {
elab 0:ea8694e55809 127 printf("\r\nEnding loop mode \r\n");
elab 0:ea8694e55809 128 break;
elab 0:ea8694e55809 129 }
elab 0:ea8694e55809 130 }
elab 0:ea8694e55809 131 }
elab 0:ea8694e55809 132
elab 0:ea8694e55809 133 return status;
elab 0:ea8694e55809 134
elab 0:ea8694e55809 135 }
elab 0:ea8694e55809 136
elab 0:ea8694e55809 137
elab 0:ea8694e55809 138 int range_threshold(VL53L1X_DevI2C *device_i2c)
elab 0:ea8694e55809 139 {
elab 0:ea8694e55809 140 int status = 0;
elab 0:ea8694e55809 141 uint16_t distance = 0;
elab 0:ea8694e55809 142 /* Create a xshutdown pin */
elab 0:ea8694e55809 143 DigitalOut xshutdown(D7);
elab 0:ea8694e55809 144
elab 0:ea8694e55809 145 LED_WS2812 LED(A3,4);
elab 0:ea8694e55809 146 LED.SetIntensity(25);
elab 0:ea8694e55809 147 LED.SetColor(1,BLACK);
elab 0:ea8694e55809 148
elab 0:ea8694e55809 149 /* create instance of sensor class */
elab 0:ea8694e55809 150 sensor = new VL53L1X(device_i2c, &xshutdown, A2);
elab 0:ea8694e55809 151
elab 0:ea8694e55809 152 sensor->vl53l1_off();
elab 0:ea8694e55809 153 /* initialise sensor */
elab 0:ea8694e55809 154 sensor->init_sensor(0x52);
elab 0:ea8694e55809 155
elab 0:ea8694e55809 156 if (status) {
elab 0:ea8694e55809 157 delete sensor;
elab 0:ea8694e55809 158 sensor= NULL;
elab 0:ea8694e55809 159 printf("Sensor centre not present\n\r");
elab 0:ea8694e55809 160 }
elab 0:ea8694e55809 161
elab 0:ea8694e55809 162 status = sensor->vl53l1x_set_inter_measurement_in_ms(1000);
elab 0:ea8694e55809 163 status = sensor->vl53l1x_set_distance_threshold(1000,
elab 0:ea8694e55809 164 1000, 0, 0);
elab 0:ea8694e55809 165 status = start_ranging();
elab 0:ea8694e55809 166 if (status != 0) {
elab 0:ea8694e55809 167 printf("Failed to start ranging!\r\n");
elab 0:ea8694e55809 168 return status;
elab 0:ea8694e55809 169 }
elab 0:ea8694e55809 170
elab 0:ea8694e55809 171 if (NULL != sensor) {
elab 0:ea8694e55809 172 printf("Entering loop mode\r\n");
elab 0:ea8694e55809 173 /* Main ranging interrupt loop */
elab 0:ea8694e55809 174 while (true) {
elab 0:ea8694e55809 175 LED.SetColor(GREEN);
elab 0:ea8694e55809 176 printf("BLACK\n");
elab 0:ea8694e55809 177 if (int_sensor) {
elab 0:ea8694e55809 178 LED.SetColor(RED);
elab 0:ea8694e55809 179 wait(1);
elab 0:ea8694e55809 180 printf("RED\n");
elab 0:ea8694e55809 181 int_sensor = false;
elab 0:ea8694e55809 182 status = sensor->handle_irq(&distance);
elab 0:ea8694e55809 183 printf("distance: %d\r\n", distance);
elab 0:ea8694e55809 184
elab 0:ea8694e55809 185 }
elab 0:ea8694e55809 186 if (int_stop) {
elab 0:ea8694e55809 187 printf("\r\nEnding loop mode \r\n");
elab 0:ea8694e55809 188 break;
elab 0:ea8694e55809 189 }
elab 0:ea8694e55809 190 }
elab 0:ea8694e55809 191 }
elab 0:ea8694e55809 192
elab 0:ea8694e55809 193 return status;
elab 0:ea8694e55809 194
elab 0:ea8694e55809 195 }
elab 0:ea8694e55809 196
elab 0:ea8694e55809 197 //
elab 0:ea8694e55809 198 //
elab 0:ea8694e55809 199 //int range_threshold(VL53L1X_DevI2C *device_i2c)
elab 0:ea8694e55809 200 //{
elab 0:ea8694e55809 201 // printf("toto");
elab 0:ea8694e55809 202 // return 0;
elab 0:ea8694e55809 203 //}
elab 0:ea8694e55809 204
elab 0:ea8694e55809 205
elab 0:ea8694e55809 206 /*=================================== Main ==================================
elab 0:ea8694e55809 207 =============================================================================*/
elab 0:ea8694e55809 208 int main()
elab 0:ea8694e55809 209 {
elab 0:ea8694e55809 210 stop_button.rise(&measuring_stop_irq);
elab 0:ea8694e55809 211
elab 0:ea8694e55809 212 VL53L1X_DevI2C *device_i2c = new VL53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL);
elab 0:ea8694e55809 213 // range_measure(device_i2c); // start continuous measures
elab 0:ea8694e55809 214 range_threshold(device_i2c);
elab 0:ea8694e55809 215
elab 0:ea8694e55809 216 return 0;
elab 0:ea8694e55809 217 }