-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathExample7_Calibration.ino
147 lines (125 loc) · 5.01 KB
/
Example7_Calibration.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
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 <Wire.h>
#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("VL53L1X 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)
{
distanceSensor.startRanging();
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)
{
}