|
| 1 | +#include <Arduino.h> |
| 2 | + |
| 3 | +#include <ComponentObject.h> |
| 4 | +#include <RangeSensor.h> |
| 5 | +#include <SparkFun_VL53L1X.h> |
| 6 | +#include <vl53l1x_class.h> |
| 7 | +#include <vl53l1_error_codes.h> |
| 8 | + |
| 9 | +/* |
| 10 | + Calling distance offset calibration for the laser based VL53L1X |
| 11 | + By: Armin Joachimsmeyer |
| 12 | + for SparkFun Electronics |
| 13 | + Date: February 20th, 2020 |
| 14 | + License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license). |
| 15 | +
|
| 16 | + SparkFun labored with love to create this code. Feel like supporting open source hardware? |
| 17 | + Buy a board from SparkFun! https://www.sparkfun.com/products/14667 |
| 18 | +
|
| 19 | + This example calls the offset calibration for a VL53L1X sensor and stores the value to EEPROM. |
| 20 | +
|
| 21 | + Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor. |
| 22 | + */ |
| 23 | + |
| 24 | +#include <Wire.h> |
| 25 | +#include "SparkFun_VL53L1X.h" |
| 26 | + |
| 27 | +#define VERSION_EXAMPLE "1.0" |
| 28 | + |
| 29 | +#define EEPROM_ADDRESS_FOR_OFFSET (E2END - 2) // last 2 bytes of EEPROM |
| 30 | + |
| 31 | +//Optional interrupt and shutdown pins. |
| 32 | +#define SHUTDOWN_PIN 2 |
| 33 | +#define INTERRUPT_PIN 3 |
| 34 | +#define TONE_PIN 11 |
| 35 | + |
| 36 | +SFEVL53L1X distanceSensor; |
| 37 | +//Uncomment the following line to use the optional shutdown and interrupt pins. |
| 38 | +//SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN); |
| 39 | + |
| 40 | +void setup(void) { |
| 41 | + Wire.begin(); |
| 42 | + |
| 43 | + // initialize the digital pin as an output. |
| 44 | + pinMode(LED_BUILTIN, OUTPUT); |
| 45 | + Serial.begin(9600); |
| 46 | +#if defined(__AVR_ATmega32U4__) |
| 47 | + while (!Serial); //delay for Leonardo, but this loops forever for Maple Serial |
| 48 | +#endif |
| 49 | +#if defined(SERIAL_USB) |
| 50 | + delay(2000); // To be able to connect Serial monitor after reset and before first printout |
| 51 | +#endif |
| 52 | + // Just to know which program is running on my Arduino |
| 53 | + Serial.println(F("START " __FILE__ "\r\nVersion " VERSION_EXAMPLE " from " __DATE__)); |
| 54 | + |
| 55 | + tone(TONE_PIN, 1000, 100); |
| 56 | + delay(200); |
| 57 | + |
| 58 | + Serial.println(); |
| 59 | + Serial.println("*****************************************************************************************************"); |
| 60 | + Serial.println(" Offset calibration"); |
| 61 | + Serial.println("Place a light grey (17 % gray) target at a distance of 140mm in front of the VL53L1X sensor."); |
| 62 | + Serial.println("The calibration will start 5 seconds after a distance below 10 cm was detected for 1 second."); |
| 63 | + Serial.println("Use the resulting offset distance as parameter for the setOffset() function called after begin()."); |
| 64 | + Serial.println("*****************************************************************************************************"); |
| 65 | + Serial.println(); |
| 66 | + |
| 67 | +#ifdef AVR |
| 68 | + Serial.print("Old offset value read from EEPROM="); |
| 69 | + Serial.print((int16_t) eeprom_read_word((uint16_t*) EEPROM_ADDRESS_FOR_OFFSET)); |
| 70 | + Serial.println(" mm"); |
| 71 | +#endif |
| 72 | + |
| 73 | + if (distanceSensor.begin() == 0) { //Begin returns 0 on a good init |
| 74 | + Serial.println("Sensor online!"); |
| 75 | + } |
| 76 | + |
| 77 | + // Short mode max distance is limited to 1.3 m but has a better ambient immunity. |
| 78 | + // Above 1.3 meter error 4 is thrown (wrap around). |
| 79 | + distanceSensor.setDistanceModeShort(); |
| 80 | + //distanceSensor.setDistanceModeLong(); // default |
| 81 | + distanceSensor.setTimingBudgetInMs(50); |
| 82 | + |
| 83 | + int tLowDistanceCount = 0; |
| 84 | + while (tLowDistanceCount < 20) { |
| 85 | + while (!distanceSensor.checkForDataReady()) { |
| 86 | + delay(1); |
| 87 | + } |
| 88 | + if (distanceSensor.getDistance() < 100) { |
| 89 | + tLowDistanceCount++; |
| 90 | + } else { |
| 91 | + tLowDistanceCount = 0; |
| 92 | + } |
| 93 | + distanceSensor.clearInterrupt(); |
| 94 | + } |
| 95 | + |
| 96 | + Serial.println("Distance below 10cm detected for more than a second, start offset calibration in 5 seconds"); |
| 97 | + tone(TONE_PIN, 1000, 100); |
| 98 | + delay(1000); |
| 99 | + tone(TONE_PIN, 1000, 100); |
| 100 | + delay(1000); |
| 101 | + tone(TONE_PIN, 1000, 100); |
| 102 | + delay(1000); |
| 103 | + tone(TONE_PIN, 1000, 100); |
| 104 | + delay(1000); |
| 105 | + tone(TONE_PIN, 1000, 900); |
| 106 | + delay(1000); |
| 107 | + |
| 108 | + /* |
| 109 | + * Place a target, 17 % gray, at a distance of 140 mm from the sensor and call the VL53L1X_CalibrateOffset (dev, 140, &offset) function. |
| 110 | + * The calibration may take a few seconds. The offset correction is applied to the sensor at the end of calibration. |
| 111 | + * |
| 112 | + * The calibration function takes 50 measurements and then takes the difference between the target distance |
| 113 | + * and the average distance and then calls setOffset() with this value. Thats all. No magic. |
| 114 | + */ |
| 115 | + distanceSensor.calibrateOffset(140); |
| 116 | + Serial.print("Result of offset calibration. RealDistance - MeasuredDistance="); |
| 117 | + Serial.print(distanceSensor.getOffset()); |
| 118 | + Serial.print(" mm"); |
| 119 | + Serial.println(); |
| 120 | + |
| 121 | +#ifdef AVR |
| 122 | + eeprom_write_word((uint16_t*) EEPROM_ADDRESS_FOR_OFFSET, distanceSensor.getOffset()); |
| 123 | + Serial.println("Offset value written to end of EEPROM."); |
| 124 | +#endif |
| 125 | + |
| 126 | + // measure periodically. Intermeasurement period must be >/= timing budget. |
| 127 | + distanceSensor.setIntermeasurementPeriod(100); |
| 128 | + distanceSensor.startRanging(); // Start once |
| 129 | + |
| 130 | + tone(TONE_PIN, 1000, 900); |
| 131 | +} |
| 132 | + |
| 133 | +void loop(void) { |
| 134 | + |
| 135 | +} |
| 136 | + |
0 commit comments