Skip to content

Commit

Permalink
update readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Aug 14, 2024
1 parent 579dea8 commit bbf5276
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 20 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 26 additions & 11 deletions GY521.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}


Expand Down
2 changes: 1 addition & 1 deletion GY521.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
35 changes: 28 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions library.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"name": "Rob Tillaart",
"email": "[email protected]",
"maintainer": true
},
{
"name": "Maik Menz",
}
],
"repository":
Expand Down

0 comments on commit bbf5276

Please sign in to comment.