diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ceeb30..79431be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [0.6.1] - 2024-07-25 +- redo of **calibrate()** function by Maik Menz. - improve initialization of gax, gay and gaz -- add **_readRaw()** to improve calibrate() +- add **readRaw()** to improve calibrate() ## [0.6.0] - 2024-06-22 diff --git a/GY521.cpp b/GY521.cpp index 6e2b147..bb80b7f 100644 --- a/GY521.cpp +++ b/GY521.cpp @@ -72,8 +72,12 @@ void GY521::reset() } -void GY521::calibrate(uint16_t times, float angleX, float angleY, bool inverted) +bool GY521::calibrate(uint16_t times, float angleX, float angleY, bool inverted) { + // disable throttling / caching of read values. + bool oldThrottle = _throttle; + _throttle = false; + // set all errors to zero, to get the raw reads. axe = aye = aze = 0; gxe = gye = gze = 0; @@ -85,26 +89,32 @@ void GY521::calibrate(uint16_t times, float angleX, float angleY, bool inverted) // adjust times if zero. if (times == 0) times = 1; - // summarize (6x) the measurements. + // sum (6x) the measurements. + uint16_t samples = 0; for (uint16_t i = 0; i < times; i++) { - _readRaw(); - _axe -= getAccelX(); - _aye -= getAccelY(); - _aze -= getAccelZ(); - _gxe -= getGyroX(); - _gye -= getGyroY(); - _gze -= getGyroZ(); + if (_readRaw() == GY521_OK) + { + _axe -= getAccelX(); + _aye -= getAccelY(); + _aze -= getAccelZ(); + _gxe -= getGyroX(); + _gye -= getGyroY(); + _gze -= getGyroZ(); + samples++; + } } + if (samples == 0) return false; + // scale gyro calibration errors so read() should get all zero's on average. - float factor = _raw2dps / times; + float factor = _raw2dps / samples; gxe = _gxe * factor; gye = _gye * factor; gze = _gze * factor; // scale accelerometer calibration errors so read() should get all zero's on average. - factor = _raw2g / times; + factor = _raw2g / samples; axe = _axe * factor; aye = _aye * factor; aze = _aze * factor; @@ -118,6 +128,11 @@ void GY521::calibrate(uint16_t times, float angleX, float angleY, bool inverted) axe -= _gravx; aye -= _gravy; aze += inverted ? -_gravz : _gravz; + + // restore throttle state. + _throttle = oldThrottle; + + return true; } diff --git a/GY521.h b/GY521.h index 768078f..5f6757d 100644 --- a/GY521.h +++ b/GY521.h @@ -52,7 +52,7 @@ class GY521 // EXPERIMENTAL // calibrate needs to be called to compensate for errors. // must be called after setAccelSensitivity(as); and setGyroSensitivity(gs); - void calibrate(uint16_t times, float angleX = 0, float angleY = 0, bool inverted = false); + bool calibrate(uint16_t times, float angleX = 0, float angleY = 0, bool inverted = false); bool wakeup(); // throttle to force delay between reads. diff --git a/README.md b/README.md index 25feedf..29e84ce 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,11 @@ It needs to be tested a lot more. See changelog.md for latest updates. +#### 0.6.1 + +Improved **calibrate()** to support any angle. + + #### 0.6.0 Fixed a bug in calibration function, making previous versions obsolete. @@ -92,6 +97,10 @@ This function overwrites the values of axe aye aze gxe gye gze. Note the **calibrate()** function takes time, depending on the number of times. +Since version 0.6.1 the calibrate function is extended with optional parameters so the +sensor can be calibrated in any angle. +**bool calibrate(times, angleX = 0, angleY = 0, inverted = false)** + #### Manual calibration @@ -120,14 +129,24 @@ Note call **Wire.begin()** before **begin()**. ### Calibrate -- **void calibrate(uint16_t times)** This function overwrites the values of axe aye aze gxe gye gze. -To get "quality error" offsets, the GY521 sensor should not move during the calibration. -The parameter times determines the number of measurements made. -Typical values are 100 or more. -Please note this is a time consuming function. +- **bool calibrate(uint16_t times, float angleX = 0, float angleY = 0, bool inverted = false)** +This function overwrites the values of axe aye aze and gxe gye gze. +To improve the quality of the error offsets, the GY521 sensor should not move during the calibration. +The parameter times determines the number of measurements the calibration function should make. +Note that the actual number of samples can be less if a read of the sensor fails. +If there is no good read at all the function returns **false**. +Typical values for times are 100 or more. +If times is set to 0, it will be forced to 1. +Please note this call will be very time consuming. Ideal the function **calibrate()** should continue until it is stable (how to define) for n reads. -Drawback is that this would make the duration unpredictable. +Drawback is that this would make the duration unpredictable. + +New since 0.6.1 (experimental) +The optional parameters **float angleX = 0, float angleY = 0** should be between -90 .. +90. +These can be used if the sensor is not lying flat during calibration. +The optional parameter **bool inverted = false** should be set to true if the sensor is +upside down. ### Throttle @@ -293,7 +312,9 @@ However if one specific is needed, please open an issue. #### Should -- test **calibrate()** function for different sensitivities. +- test **calibrate()** function + - for different sensitivities. + - for different angles. #### Could diff --git a/library.json b/library.json index edca9f2..376c882 100644 --- a/library.json +++ b/library.json @@ -8,6 +8,9 @@ "name": "Rob Tillaart", "email": "Rob.Tillaart@gmail.com", "maintainer": true + }, + { + "name": "Maik Menz", } ], "repository":