diff --git a/lib/SparkFun_VL53L1X/LICENSE.md b/lib/SparkFun_VL53L1X/LICENSE.md new file mode 100644 index 000000000..5c9ff4a0a --- /dev/null +++ b/lib/SparkFun_VL53L1X/LICENSE.md @@ -0,0 +1,23 @@ +COPYRIGHT(c) 2018 STMicroelectronics + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + 3. Neither the name of STMicroelectronics nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/lib/SparkFun_VL53L1X/README.md b/lib/SparkFun_VL53L1X/README.md new file mode 100644 index 000000000..0c524f868 --- /dev/null +++ b/lib/SparkFun_VL53L1X/README.md @@ -0,0 +1,48 @@ +SparkFun Qwiic 4m Distance Sensor with VL53L1X +======================================== +[![Build Status](https://github.com/sparkfun/SparkFun_VL53L1X_Arduino_Library/workflows/LibraryBuild/badge.svg)](https://github.com/sparkfun/SparkFun_VL53L1X_Arduino_Library/actions) + + +![SparkFun Distance Sensor Breakout - 4 Meter, VL53L1X (Qwiic)](https://cdn.sparkfun.com//assets/parts/1/2/9/4/8/14722-SparkFun_Distance_Sensor_Breakout-_4_Meter__VL53L1X__Qwiic_-01.jpg) + +[*SparkFun Distance Sensor Breakout - 4 Meter, VL53L1X (Qwiic)(SEN-14722)*](https://www.sparkfun.com/products/14722) + +The VL53L1X is the latest Time Of Flight (ToF) sensor to be released. It uses a VCSEL (vertical cavity surface emitting laser) to emit a class 1 IR laser and time the reflection to the target. What does all this mean? You can measure the distance to an object up to 4 meters away with millimeter resolution! That’s pretty incredible. + +We’re far from done: The VL53L1X is a highly complex sensor with a multitude of options and configurations. We’ve written example sketches that allow you to read the distance, signal rate, and range status. Because ST has chosen not to release a complete datasheet we are forced to reverse engineer the interface from their example code and I2C data stream captures. If you’re into puzzles we could use your help to make the library better! + +We’ve found the precision of the sensor to be 1mm but the accuracy is around +/-5mm. + +SparkFun labored with love to create this code. Feel like supporting open source hardware? +Buy a [breakout board](https://www.sparkfun.com/products/14722) from SparkFun! + +Repository Contents +------------------- + +* **/Documents** - Datasheet and User Manual +* **/Hardware** - Eagle design files (.brd, .sch) +* **/Production** - .brd files + +Documentation +-------------- +* **[Hookup Guide](https://learn.sparkfun.com/tutorials/qwiic-distance-sensor-vl53l1x-hookup-guide)** + +Product Versions +-------------- +* **[SEN-14722](https://www.sparkfun.com/products/14722)** - SparkFun red version +* **[SPX-14667](https://www.sparkfun.com/products/14667)** - SparkX Version + +License Information +------------------- + +This product is _**open source**_! + +Please review the LICENSE.md file for license information. + +If you have any questions or concerns on licensing, please contact techsupport@sparkfun.com. + +Please use, reuse, and modify these files as you see fit. Please maintain attribution to SparkFun Electronics and release any derivative under the same license. + +Distributed as-is; no warranty is given. + +- Your friends at SparkFun. diff --git a/lib/SparkFun_VL53L1X/examples/Example1_ReadDistance/Example1_ReadDistance.ino b/lib/SparkFun_VL53L1X/examples/Example1_ReadDistance/Example1_ReadDistance.ino new file mode 100644 index 000000000..2af66adbc --- /dev/null +++ b/lib/SparkFun_VL53L1X/examples/Example1_ReadDistance/Example1_ReadDistance.ino @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include + +/* + Reading distance from the laser based VL53L1X + By: Nathan Seidle + SparkFun Electronics + Date: April 4th, 2018 + License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license). + + SparkFun labored with love to create this code. Feel like supporting open source hardware? + Buy a board from SparkFun! https://www.sparkfun.com/products/14667 + + This example prints the distance to an object. + + Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor. +*/ + +#include +#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X + +//Optional interrupt and shutdown pins. +#define SHUTDOWN_PIN 2 +#define INTERRUPT_PIN 3 + +SFEVL53L1X distanceSensor; +//Uncomment the following line to use the optional shutdown and interrupt pins. +//SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN); + +void setup(void) +{ + Wire.begin(); + + Serial.begin(115200); + Serial.println("VL53L1X Qwiic Test"); + + if (distanceSensor.begin() != 0) //Begin returns 0 on a good init + { + Serial.println("Sensor failed to begin. Please check wiring. Freezing..."); + while (1) + ; + } + Serial.println("Sensor online!"); +} + +void loop(void) +{ + distanceSensor.startRanging(); //Write configuration bytes to initiate measurement + while (!distanceSensor.checkForDataReady()) + { + delay(1); + } + int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor + distanceSensor.clearInterrupt(); + distanceSensor.stopRanging(); + + Serial.print("Distance(mm): "); + Serial.print(distance); + + float distanceInches = distance * 0.0393701; + float distanceFeet = distanceInches / 12.0; + + Serial.print("\tDistance(ft): "); + Serial.print(distanceFeet, 2); + + Serial.println(); +} diff --git a/lib/SparkFun_VL53L1X/examples/Example2_SetDistanceMode/Example2_SetDistanceMode.ino b/lib/SparkFun_VL53L1X/examples/Example2_SetDistanceMode/Example2_SetDistanceMode.ino new file mode 100644 index 000000000..212fada5e --- /dev/null +++ b/lib/SparkFun_VL53L1X/examples/Example2_SetDistanceMode/Example2_SetDistanceMode.ino @@ -0,0 +1,70 @@ +/* + Reading distance from the laser based VL53L1X + By: Nathan Seidle + Revised by: Andy England + SparkFun Electronics + Date: April 4th, 2018 + License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license). + + SparkFun labored with love to create this code. Feel like supporting open source hardware? + Buy a board from SparkFun! https://www.sparkfun.com/products/14667 + + This example prints the distance to an object. + + Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor. +*/ + +#include +#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X + +//Optional interrupt and shutdown pins. +#define SHUTDOWN_PIN 2 +#define INTERRUPT_PIN 3 + +SFEVL53L1X distanceSensor; +//Uncomment the following line to use the optional shutdown and interrupt pins. +//SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN); + +void setup(void) +{ + Wire.begin(); + + Serial.begin(115200); + Serial.println("VL53L1X Qwiic Test"); + + if (distanceSensor.begin() != 0) //Begin returns 0 on a good init + { + Serial.println("Sensor failed to begin. Please check wiring. Freezing..."); + while (1) + ; + } + Serial.println("Sensor online!"); + + distanceSensor.setDistanceModeShort(); + //distanceSensor.setDistanceModeLong(); +} + +void loop(void) +{ + distanceSensor.startRanging(); //Write configuration bytes to initiate measurement + + while (!distanceSensor.checkForDataReady()) + { + delay(1); + } + int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor + distanceSensor.clearInterrupt(); + + distanceSensor.stopRanging(); + + Serial.print("Distance(mm): "); + Serial.print(distance); + + float distanceInches = distance * 0.0393701; + float distanceFeet = distanceInches / 12.0; + + Serial.print("\tDistance(ft): "); + Serial.print(distanceFeet, 2); + + Serial.println(); +} diff --git a/lib/SparkFun_VL53L1X/examples/Example3_StatusAndRate/Example3_StatusAndRate.ino b/lib/SparkFun_VL53L1X/examples/Example3_StatusAndRate/Example3_StatusAndRate.ino new file mode 100644 index 000000000..edbe1bd5d --- /dev/null +++ b/lib/SparkFun_VL53L1X/examples/Example3_StatusAndRate/Example3_StatusAndRate.ino @@ -0,0 +1,117 @@ +/* + Reading distance from the laser based VL53L1X + By: Nathan Seidle + SparkFun Electronics + Date: April 4th, 2018 + License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license). + + SparkFun labored with love to create this code. Feel like supporting open source hardware? + Buy a board from SparkFun! https://www.sparkfun.com/products/14667 + + This example demonstrates how to read and average distance, the measurement status, and the signal rate. + + Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor. +*/ + +#include +#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X + +//Optional interrupt and shutdown pins. +#define SHUTDOWN_PIN 2 +#define INTERRUPT_PIN 3 + +SFEVL53L1X distanceSensor; +//Uncomment the following line to use the optional shutdown and interrupt pins. +//SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN); + +//Store distance readings to get rolling average +#define HISTORY_SIZE 10 +int history[HISTORY_SIZE]; +byte historySpot; + +void setup(void) +{ + Wire.begin(); + + Serial.begin(115200); + Serial.println("VL53L1X Qwiic Test"); + + if (distanceSensor.begin() != 0) //Begin returns 0 on a good init + { + Serial.println("Sensor failed to begin. Please check wiring. Freezing..."); + while (1) + ; + } + Serial.println("Sensor online!"); + + for (int x = 0; x < HISTORY_SIZE; x++) + history[x] = 0; +} + +void loop(void) +{ + long startTime = millis(); + distanceSensor.startRanging(); //Write configuration block of 135 bytes to setup a measurement + while (!distanceSensor.checkForDataReady()) + { + delay(1); + } + int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor + distanceSensor.clearInterrupt(); + distanceSensor.stopRanging(); + long endTime = millis(); + + Serial.print("Distance(mm): "); + Serial.print(distance); + + history[historySpot] = distance; + if (++historySpot == HISTORY_SIZE) + historySpot = 0; + + long avgDistance = 0; + for (int x = 0; x < HISTORY_SIZE; x++) + avgDistance += history[x]; + + avgDistance /= HISTORY_SIZE; + Serial.print("\tavgDistance: "); + Serial.print(avgDistance); + + float distanceInches = avgDistance * 0.0393701; + float distanceFeet = distanceInches / 12; + + Serial.print("\tavgDistance(ft): "); + Serial.print(distanceFeet, 2); + + int signalRate = distanceSensor.getSignalRate(); + Serial.print("\tSignal rate: "); + Serial.print(signalRate); + + byte rangeStatus = distanceSensor.getRangeStatus(); + Serial.print("\tRange Status: "); + + //Make it human readable + switch (rangeStatus) + { + case 0: + Serial.print("Good"); + break; + case 1: + Serial.print("Sigma fail"); + break; + case 2: + Serial.print("Signal fail"); + break; + case 7: + Serial.print("Wrapped target fail"); + break; + default: + Serial.print("Unknown: "); + Serial.print(rangeStatus); + break; + } + + Serial.print("\tHz: "); + Serial.print(1000.0 / (float)(endTime - startTime), 2); + + Serial.println(); +} diff --git a/lib/SparkFun_VL53L1X/examples/Example4_SetIntermeasurementPeriod/Example4_SetIntermeasurementPeriod.ino b/lib/SparkFun_VL53L1X/examples/Example4_SetIntermeasurementPeriod/Example4_SetIntermeasurementPeriod.ino new file mode 100644 index 000000000..7791f6f5a --- /dev/null +++ b/lib/SparkFun_VL53L1X/examples/Example4_SetIntermeasurementPeriod/Example4_SetIntermeasurementPeriod.ino @@ -0,0 +1,67 @@ +/* + Reading distance from the laser based VL53L1X + By: Nathan Seidle + SparkFun Electronics + Date: April 4th, 2018 + License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license). + + SparkFun labored with love to create this code. Feel like supporting open source hardware? + Buy a board from SparkFun! https://www.sparkfun.com/products/14667 + + This example prints the distance to an object. + + Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor. +*/ + +#include +#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X + +//Optional interrupt and shutdown pins. +#define SHUTDOWN_PIN 2 +#define INTERRUPT_PIN 3 + +SFEVL53L1X distanceSensor; +//Uncomment the following line to use the optional shutdown and interrupt pins. +//SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN); + +void setup(void) +{ + Wire.begin(); + + Serial.begin(115200); + Serial.println("VL53L1X Qwiic Test"); + + if (distanceSensor.begin() != 0) //Begin returns 0 on a good init + { + Serial.println("Sensor failed to begin. Please check wiring. Freezing..."); + while (1) + ; + } + Serial.println("Sensor online!"); + + // Intermeasurement period must be >= timing budget. Default = 100 ms. + distanceSensor.setIntermeasurementPeriod(200); + Serial.println(distanceSensor.getIntermeasurementPeriod()); + distanceSensor.startRanging(); // Start only once (and do never call stop) +} + +void loop(void) +{ + while (!distanceSensor.checkForDataReady()) + { + delay(1); + } + int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor + distanceSensor.clearInterrupt(); + + Serial.print("Distance(mm): "); + Serial.print(distance); + + float distanceInches = distance * 0.0393701; + float distanceFeet = distanceInches / 12.0; + + Serial.print("\tDistance(ft): "); + Serial.print(distanceFeet, 2); + + Serial.println(); +} diff --git a/lib/SparkFun_VL53L1X/examples/Example5_LCDDemo/Example5_LCDDemo.ino b/lib/SparkFun_VL53L1X/examples/Example5_LCDDemo/Example5_LCDDemo.ino new file mode 100644 index 000000000..664230304 --- /dev/null +++ b/lib/SparkFun_VL53L1X/examples/Example5_LCDDemo/Example5_LCDDemo.ino @@ -0,0 +1,207 @@ +/* + Reading distance from the laser based VL53L1X + By: Nathan Seidle + SparkFun Electronics + Date: April 4th, 2018 + License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license). + + SparkFun labored with love to create this code. Feel like supporting open source hardware? + Buy a board from SparkFun! https://www.sparkfun.com/products/14667 + + Reading distance and outputting the distance and speed to a SerLCD. + This is based on the Speed Trap project: https://www.youtube.com/watch?v=uC9CkhJiIaQ + + Are you getting weird readings? Make sure the vacuum tape has been removed. + +*/ +#include +#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X +#include + +//Optional interrupt and shutdown pins. +#define SHUTDOWN_PIN 2 +#define INTERRUPT_PIN 3 + +SFEVL53L1X distanceSensor; +//Uncomment the following line to use the optional shutdown and interrupt pins. +//SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN); + +#if defined(ESP8266) +SoftwareSerial lcd(10, 9); // RX, TX +#elif defined(ARDUINO_ARCH_APOLLO3) +SoftwareSerial lcd(14, 15); // use RX1, TX1 of Apollo boards +#else +SoftwareSerial lcd(10, A3); // RX, TX +#endif + +//Store distance readings to get rolling average +#define HISTORY_SIZE 8 +int history[HISTORY_SIZE]; +byte historySpot; + +long lastReading = 0; +long lastDistance = 0; +float newDistance; +int maxDistance = 0; + +const byte numberOfDeltas = 8; +float deltas[numberOfDeltas]; +byte deltaSpot = 0; //Keeps track of where we are within the deltas array + +//This controls how quickly the display updates +//Too quickly and it gets twitchy. Too slow and it doesn't seem like it's responding. +#define LOOPTIME 50 + +int maxMPH = 0; //Keeps track of what the latest fastest speed is +long maxMPH_timeout = 0; //Forget the max speed after some length of time + +#define maxMPH_remember 3000 //After this number of ms the system will forget the max speed + +boolean readingValid = false; +long validCount = 0; + +void setup(void) +{ + Wire.begin(); + Wire.setClock(400000); + + Serial.begin(115200); + Serial.println("VL53L1X Qwiic Test"); + + lcd.begin(9600); + + lcd.write(254); //Move cursor to beginning of first line + lcd.write(128); + lcd.print("Distance: 3426 "); + lcd.print("12 mph "); + + if (distanceSensor.begin() != 0) //Begin returns 0 on a good init + { + Serial.println("Sensor failed to begin. Please check wiring. Freezing..."); + while (1) + ; + } + Serial.println("Sensor online!"); + + for (int x = 0; x < HISTORY_SIZE; x++) + history[x] = 0; +} + +void loop(void) +{ + + //Write configuration block of 135 bytes to setup a measurement + distanceSensor.startRanging(); + while (!distanceSensor.checkForDataReady()) + { + delay(1); + } + int distanceMM = distanceSensor.getDistance(); + distanceSensor.clearInterrupt(); + distanceSensor.stopRanging(); + + lastReading = millis(); + + history[historySpot] = distanceMM; + if (historySpot++ == HISTORY_SIZE) + historySpot = 0; + + long avgDistance = 0; + for (int x = 0; x < HISTORY_SIZE; x++) + avgDistance += history[x]; + + avgDistance /= HISTORY_SIZE; + + //Every loop let's get a reading + newDistance = distanceMM / 10; //Go get distance in cm + + int deltaDistance = lastDistance - newDistance; + lastDistance = newDistance; + + //Scan delta array to see if this new delta is sane or not + boolean safeDelta = true; + for (int x = 0; x < numberOfDeltas; x++) + { + //We don't want to register jumps greater than 30cm in 50ms + //But if we're less than 1000cm then maybe + //30 works well + if (abs(deltaDistance - deltas[x]) > 40) + safeDelta = false; + } + + //Insert this new delta into the array + if (safeDelta) + { + deltas[deltaSpot++] = deltaDistance; + if (deltaSpot > numberOfDeltas) + deltaSpot = 0; //Wrap this variable + } + + //Get average of the current deltas array + float avgDeltas = 0.0; + for (byte x = 0; x < numberOfDeltas; x++) + avgDeltas += (float)deltas[x]; + avgDeltas /= numberOfDeltas; + + //22.36936 comes from a big coversion from cm per 50ms to mile per hour + float instantMPH = 22.36936 * (float)avgDeltas / (float)LOOPTIME; + + instantMPH = abs(instantMPH); //We want to measure as you walk away + + instantMPH = ceil(instantMPH); //Round up to the next number. This is helpful if we're not displaying decimals. + + if (instantMPH > maxMPH) + { + maxMPH = instantMPH; + maxMPH_timeout = millis(); + } + + if (millis() - maxMPH_timeout > maxMPH_remember) + { + maxMPH = 0; + } + + int signalRate = distanceSensor.getSignalRate(); + if (signalRate < 10) + { + readingValid = false; + validCount = 0; + } + else + { + validCount++; + if (avgDistance > maxDistance) + maxDistance = avgDistance; + } + + if (validCount > 10) + readingValid = true; + + if (readingValid == false) + { + instantMPH = 0; + avgDistance = 0; + } + + //Convert mm per millisecond to miles per hour + //float mph = distanceDelta * 2.236936 / (float)timeDelta; + //mph *= -1; //Flip sign as we will be traveling towards sensor, decreasing the distance + + lcd.write(254); //Move cursor + lcd.write(138); + lcd.print(" "); + lcd.write(254); //Move cursor + lcd.write(138); + + if (readingValid == true) + lcd.print(avgDistance); + else + lcd.print(maxDistance); + + lcd.write(254); //Move cursor + lcd.write(192); + lcd.print(instantMPH, 0); + lcd.print(" mph "); + + delay(25); +} diff --git a/lib/SparkFun_VL53L1X/examples/Example6_ArduinoPlotterOutput/Example6_ArduinoPlotterOutput.ino b/lib/SparkFun_VL53L1X/examples/Example6_ArduinoPlotterOutput/Example6_ArduinoPlotterOutput.ino new file mode 100644 index 000000000..d086e6acc --- /dev/null +++ b/lib/SparkFun_VL53L1X/examples/Example6_ArduinoPlotterOutput/Example6_ArduinoPlotterOutput.ino @@ -0,0 +1,112 @@ +/* + Reading distance from the laser based VL53L1X + By: Armin Joachimsmeyer + for SparkFun Electronics + Date: February 20th, 2020 + License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license). + + SparkFun labored with love to create this code. Feel like supporting open source hardware? + Buy a board from SparkFun! https://www.sparkfun.com/products/14667 + + This example prints the distance to an object to the Arduino Serial Plotter and generates a tone depending on distance. + + Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor. + */ + +#include +#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X + +//Optional interrupt and shutdown pins. +#define SHUTDOWN_PIN 2 +#define INTERRUPT_PIN 3 +#define TONE_PIN 11 + +SFEVL53L1X distanceSensor; +//Uncomment the following line to use the optional shutdown and interrupt pins. +//SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN); + +void setup(void) +{ + Wire.begin(); + + // initialize the digital pin as an output. + pinMode(LED_BUILTIN, OUTPUT); + Serial.begin(115200); + while (!Serial) + ; //delay for Leonardo + // Just to know which program is running on my Arduino + Serial.println(F("START " __FILE__)); + +#if !defined(ESP32) && !defined(ARDUINO_SAM_DUE) && !defined(__SAM3X8E__) + // test beep for the connected speaker + tone(TONE_PIN, 1000, 100); + delay(200); +#endif + + if (distanceSensor.begin() != 0) //Begin returns 0 on a good init + { + Serial.println("Sensor failed to begin. Please check wiring. Freezing..."); + while (1) + ; + } + Serial.println("Sensor online!"); + + // Short mode max distance is limited to 1.3 m but has a better ambient immunity. + // Above 1.3 meter error 4 is thrown (wrap around). + distanceSensor.setDistanceModeShort(); + //distanceSensor.setDistanceModeLong(); // default + + // Print Legend + Serial.println("Distance Signal-Rate/100 Ambient-Rate/100"); + + /* + * The minimum timing budget is 20 ms for the short distance mode and 33 ms for the medium and long distance modes. + * Predefined values = 15, 20, 33, 50, 100(default), 200, 500. + * This function must be called after SetDistanceMode. + */ + distanceSensor.setTimingBudgetInMs(50); + + // measure periodically. Intermeasurement period must be >/= timing budget. + distanceSensor.setIntermeasurementPeriod(100); + distanceSensor.startRanging(); // Start once +} + +void loop(void) +{ + while (!distanceSensor.checkForDataReady()) + { + delay(1); + } + byte rangeStatus = distanceSensor.getRangeStatus(); + unsigned int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor + distanceSensor.clearInterrupt(); + + /* + * With signed int we get overflow at short distances. + * With unsigned we get an overflow below around 2.5 cm. + */ + unsigned int tSignalRate = distanceSensor.getSignalRate(); + unsigned int tAmbientRate = distanceSensor.getAmbientRate(); + + if (rangeStatus == 0) + { +#if !defined(ESP32) && !defined(ARDUINO_SAM_DUE) && !defined(__SAM3X8E__) + tone(11, distance + 500); +#endif + } + else + { + // if tAmbientRate > tSignalRate we likely get a signal fail error condition + // in Distance mode short we get error 4 (out of bounds) or 7 (wrap around) if the distance is greater than 1.3 meter. + distance = rangeStatus; +#if !defined(ESP32) && !defined(ARDUINO_SAM_DUE) && !defined(__SAM3X8E__) + noTone(11); +#endif + } + + Serial.print(distance); + Serial.print(' '); + Serial.print(tSignalRate / 100); + Serial.print(' '); + Serial.println(tAmbientRate / 100); +} diff --git a/lib/SparkFun_VL53L1X/examples/Example7_Calibration/Example7_Calibration.ino b/lib/SparkFun_VL53L1X/examples/Example7_Calibration/Example7_Calibration.ino new file mode 100644 index 000000000..c06f22013 --- /dev/null +++ b/lib/SparkFun_VL53L1X/examples/Example7_Calibration/Example7_Calibration.ino @@ -0,0 +1,154 @@ +#include + +#include +#include +#include +#include +#include + +/* + Calling distance offset calibration for the laser based VL53L1X + By: Armin Joachimsmeyer + for SparkFun Electronics + Date: February 20th, 2020 + License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license). + + SparkFun labored with love to create this code. Feel like supporting open source hardware? + Buy a board from SparkFun! https://www.sparkfun.com/products/14667 + + This example calls the offset calibration for a VL53L1X sensor and stores the value to EEPROM. + + Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor. + */ + +#include +#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X + +#define VERSION_EXAMPLE "1.0" + +#define EEPROM_ADDRESS_FOR_OFFSET (E2END - 2) // last 2 bytes of EEPROM + +//Optional interrupt and shutdown pins. +#define SHUTDOWN_PIN 2 +#define INTERRUPT_PIN 3 +#define TONE_PIN 11 + +SFEVL53L1X distanceSensor; +//Uncomment the following line to use the optional shutdown and interrupt pins. +//SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN); + +void setup(void) +{ + Wire.begin(); + + // initialize the digital pin as an output. + pinMode(LED_BUILTIN, OUTPUT); + Serial.begin(115200); +#if defined(__AVR_ATmega32U4__) + while (!Serial) + ; //delay for Leonardo, but this loops forever for Maple Serial +#endif +#if defined(SERIAL_USB) + delay(2000); // To be able to connect Serial monitor after reset and before first printout +#endif + // Just to know which program is running on my Arduino + Serial.println(F("START " __FILE__ "\r\nVersion " VERSION_EXAMPLE " from " __DATE__)); + +#if !defined(ESP32) && !defined(ARDUINO_SAM_DUE) && !defined(__SAM3X8E__) + tone(TONE_PIN, 1000, 100); + delay(200); +#endif + + Serial.println(); + Serial.println("*****************************************************************************************************"); + Serial.println(" Offset calibration"); + Serial.println("Place a light grey (17 % gray) target at a distance of 140mm in front of the VL53L1X sensor."); + Serial.println("The calibration will start 5 seconds after a distance below 10 cm was detected for 1 second."); + Serial.println("Use the resulting offset distance as parameter for the setOffset() function called after begin()."); + Serial.println("*****************************************************************************************************"); + Serial.println(); + +#ifdef AVR + Serial.print("Old offset value read from EEPROM="); + Serial.print((int16_t)eeprom_read_word((uint16_t *)EEPROM_ADDRESS_FOR_OFFSET)); + Serial.println(" mm"); +#endif + + if (distanceSensor.begin() != 0) //Begin returns 0 on a good init + { + Serial.println("Sensor failed to begin. Please check wiring. Freezing..."); + while (1) + ; + } + Serial.println("Sensor online!"); + + // Short mode max distance is limited to 1.3 m but has a better ambient immunity. + // Above 1.3 meter error 4 is thrown (wrap around). + distanceSensor.setDistanceModeShort(); + //distanceSensor.setDistanceModeLong(); // default + distanceSensor.setTimingBudgetInMs(50); + + int tLowDistanceCount = 0; + while (tLowDistanceCount < 20) + { + while (!distanceSensor.checkForDataReady()) + { + delay(1); + } + if (distanceSensor.getDistance() < 100) + { + tLowDistanceCount++; + } + else + { + tLowDistanceCount = 0; + } + distanceSensor.clearInterrupt(); + } + + Serial.println("Distance below 10cm detected for more than a second, start offset calibration in 5 seconds"); +#if !defined(ESP32) && !defined(ARDUINO_SAM_DUE) && !defined(__SAM3X8E__) + tone(TONE_PIN, 1000, 100); + delay(1000); + tone(TONE_PIN, 1000, 100); + delay(1000); + tone(TONE_PIN, 1000, 100); + delay(1000); + tone(TONE_PIN, 1000, 100); + delay(1000); + tone(TONE_PIN, 1000, 900); + delay(1000); +#else + delay(5000); +#endif + + /* + * Place a target, 17 % gray, at a distance of 140 mm from the sensor and call the VL53L1X_CalibrateOffset (dev, 140, &offset) function. + * The calibration may take a few seconds. The offset correction is applied to the sensor at the end of calibration. + * + * The calibration function takes 50 measurements and then takes the difference between the target distance + * and the average distance and then calls setOffset() with this value. Thats all. No magic. + */ + distanceSensor.calibrateOffset(140); + Serial.print("Result of offset calibration. RealDistance - MeasuredDistance="); + Serial.print(distanceSensor.getOffset()); + Serial.print(" mm"); + Serial.println(); + +#ifdef AVR + eeprom_write_word((uint16_t *)EEPROM_ADDRESS_FOR_OFFSET, distanceSensor.getOffset()); + Serial.println("Offset value written to end of EEPROM."); +#endif + + // measure periodically. Intermeasurement period must be >/= timing budget. + distanceSensor.setIntermeasurementPeriod(100); + distanceSensor.startRanging(); // Start once + +#if !defined(ESP32) && !defined(ARDUINO_SAM_DUE) && !defined(__SAM3X8E__) + tone(TONE_PIN, 1000, 900); +#endif +} + +void loop(void) +{ +} diff --git a/lib/SparkFun_VL53L1X/keywords.txt b/lib/SparkFun_VL53L1X/keywords.txt new file mode 100644 index 000000000..3bc7a87c8 --- /dev/null +++ b/lib/SparkFun_VL53L1X/keywords.txt @@ -0,0 +1,69 @@ +####################################### +# Syntax Coloring Map For ST_FlightSense_Sensor_Shields +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +SFEVL53L1X KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +init KEYWORD2 +begin KEYWORD2 +checkID KEYWORD2 +sensorOn KEYWORD2 +sensorOff KEYWORD2 +getSoftwareVersion KEYWORD2 +setI2CAddress KEYWORD2 +getI2CAddress KEYWORD2 +clearInterrupt KEYWORD2 +setInterruptPolarityHigh KEYWORD2 +setInterruptPolarityLow KEYWORD2 +getInterruptPolarity KEYWORD2 +startRanging KEYWORD2 +stopRanging KEYWORD2 +checkForDataReady KEYWORD2 +setTimingBudgetInMs KEYWORD2 +getTimingBudgetInMs KEYWORD2 +setDistanceModeLong KEYWORD2 +setDistanceModeShort KEYWORD2 +getDistanceMode KEYWORD2 +setIntermeasurementPeriod KEYWORD2 +getIntermeasurementPeriod KEYWORD2 +checkBootState KEYWORD2 +getSensorID KEYWORD2 +getDistance KEYWORD2 +getSignalPerSpad KEYWORD2 +getAmbientPerSpad KEYWORD2 +getSignalRate KEYWORD2 +getSpadNb KEYWORD2 +getAmbientRate KEYWORD2 +getRangeStatus KEYWORD2 +setOffset KEYWORD2 +getOffset KEYWORD2 +setXTalk KEYWORD2 +getXTalk KEYWORD2 +setDistanceThreshold KEYWORD2 +getDistanceThresholdWindow KEYWORD2 +getDistanceThresholdLow KEYWORD2 +getDistanceThresholdHig KEYWORD2 +setROI KEYWORD2 +getROIX KEYWORD2 +getROIY KEYWORD2 +setSignalThreshold KEYWORD2 +getSignalThreshold KEYWORD2 +setSigmaThreshold KEYWORD2 +getSigmaThreshold KEYWORD2 +startTemperatureUpdate KEYWORD2 +calibrateOffset KEYWORD2 +calibrateXTalk KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### + + diff --git a/lib/SparkFun_VL53L1X/library.properties b/lib/SparkFun_VL53L1X/library.properties new file mode 100644 index 000000000..8d1786d6d --- /dev/null +++ b/lib/SparkFun_VL53L1X/library.properties @@ -0,0 +1,9 @@ +name=SparkFun VL53L1X 4m Laser Distance Sensor +version=1.2.9 +author=SparkFun Electronics +maintainer=SparkFun Electronics +sentence=Library for the SparkFun Qwiic 4m Distance Sensor - VL53L1X +paragraph=The VL53L1X is the latest Time Of Flight (ToF) sensor to be released. It uses a VCSEL (vertical cavity surface emitting laser) to emit a class 1 IR laser and time the reflection to the target. What does all this mean? You can measure the distance to an object up to 4 meters away with millimeter resolution! That’s pretty incredible. We’ve found the precision of the sensor to be 1mm but the accuracy is around +/-5mm. Available at: https://www.sparkfun.com/products/14667 +category=Sensors +url=https://github.com/sparkfun/SparkFun_VL53L1X_Arduino_Library +architectures=* diff --git a/lib/SparkFun_VL53L1X/ComponentObject.h b/lib/SparkFun_VL53L1X/src/ComponentObject.h similarity index 100% rename from lib/SparkFun_VL53L1X/ComponentObject.h rename to lib/SparkFun_VL53L1X/src/ComponentObject.h diff --git a/lib/SparkFun_VL53L1X/RangeSensor.h b/lib/SparkFun_VL53L1X/src/RangeSensor.h similarity index 100% rename from lib/SparkFun_VL53L1X/RangeSensor.h rename to lib/SparkFun_VL53L1X/src/RangeSensor.h diff --git a/lib/SparkFun_VL53L1X/SparkFun_VL53L1X.cpp b/lib/SparkFun_VL53L1X/src/SparkFun_VL53L1X.cpp similarity index 100% rename from lib/SparkFun_VL53L1X/SparkFun_VL53L1X.cpp rename to lib/SparkFun_VL53L1X/src/SparkFun_VL53L1X.cpp diff --git a/lib/SparkFun_VL53L1X/SparkFun_VL53L1X.h b/lib/SparkFun_VL53L1X/src/SparkFun_VL53L1X.h similarity index 100% rename from lib/SparkFun_VL53L1X/SparkFun_VL53L1X.h rename to lib/SparkFun_VL53L1X/src/SparkFun_VL53L1X.h diff --git a/lib/SparkFun_VL53L1X/vl53l1_error_codes.h b/lib/SparkFun_VL53L1X/src/vl53l1_error_codes.h similarity index 100% rename from lib/SparkFun_VL53L1X/vl53l1_error_codes.h rename to lib/SparkFun_VL53L1X/src/vl53l1_error_codes.h diff --git a/lib/SparkFun_VL53L1X/vl53l1x_class.cpp b/lib/SparkFun_VL53L1X/src/vl53l1x_class.cpp similarity index 100% rename from lib/SparkFun_VL53L1X/vl53l1x_class.cpp rename to lib/SparkFun_VL53L1X/src/vl53l1x_class.cpp diff --git a/lib/SparkFun_VL53L1X/vl53l1x_class.h b/lib/SparkFun_VL53L1X/src/vl53l1x_class.h similarity index 100% rename from lib/SparkFun_VL53L1X/vl53l1x_class.h rename to lib/SparkFun_VL53L1X/src/vl53l1x_class.h