Skip to content

Commit 579dea8

Browse files
authored
Added orientation arguments to calibrate function (#59)
* Fix calibration error scaling * Added orientation arguments to calibrate function --------- Co-authored-by: Maik Menz <[email protected]>
1 parent 66f4850 commit 579dea8

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

GY521.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ GY521::GY521(uint8_t address, TwoWire *wire)
2626
_address = address;
2727
_wire = wire;
2828

29+
// initialize errors
30+
// don't do it in reset, as users might want to keep them
31+
axe = aye = aze = 0;
32+
2933
reset();
3034
}
3135

@@ -68,12 +72,8 @@ void GY521::reset()
6872
}
6973

7074

71-
void GY521::calibrate(uint16_t times)
75+
void GY521::calibrate(uint16_t times, float angleX, float angleY, bool inverted)
7276
{
73-
// disable throttling / caching of read values.
74-
bool oldThrottle = _throttle;
75-
_throttle = false;
76-
7777
// set all errors to zero, to get the raw reads.
7878
axe = aye = aze = 0;
7979
gxe = gye = gze = 0;
@@ -97,20 +97,27 @@ void GY521::calibrate(uint16_t times)
9797
_gze -= getGyroZ();
9898
}
9999

100-
// scale accelerometer calibration errors so read() should get all zero's on average.
101-
float factor = _raw2g / times;
102-
axe = _axe * factor;
103-
aye = _aye * factor;
104-
aze = _aze * factor;
105-
106100
// scale gyro calibration errors so read() should get all zero's on average.
107-
factor = _raw2dps / times;
101+
float factor = _raw2dps / times;
108102
gxe = _gxe * factor;
109103
gye = _gye * factor;
110104
gze = _gze * factor;
111105

112-
// restore throttle state.
113-
_throttle = oldThrottle;
106+
// scale accelerometer calibration errors so read() should get all zero's on average.
107+
factor = _raw2g / times;
108+
axe = _axe * factor;
109+
aye = _aye * factor;
110+
aze = _aze * factor;
111+
112+
// remove expected gravity from error
113+
angleX *= GY521_DEGREES2RAD;
114+
angleY *= GY521_DEGREES2RAD;
115+
float _gravx = -sin(angleY) * cos(angleX);
116+
float _gravy = sin(angleX);
117+
float _gravz = cos(angleY) * cos(angleX);
118+
axe -= _gravx;
119+
aye -= _gravy;
120+
aze += inverted ? -_gravz : _gravz;
114121
}
115122

116123

GY521.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const float GRAVITY = 9.80655;
3333

3434
// CONVERSION CONSTANTS
3535
#define GY521_RAD2DEGREES (180.0 / PI)
36+
#define GY521_DEGREES2RAD (PI / 180.0)
3637
#define GY521_RAW2DPS (1.0 / 131.0)
3738
#define GY521_RAW2G (1.0 / 16384.0)
3839

@@ -51,7 +52,7 @@ class GY521
5152
// EXPERIMENTAL
5253
// calibrate needs to be called to compensate for errors.
5354
// must be called after setAccelSensitivity(as); and setGyroSensitivity(gs);
54-
void calibrate(uint16_t times);
55+
void calibrate(uint16_t times, float angleX = 0, float angleY = 0, bool inverted = false);
5556

5657
bool wakeup();
5758
// throttle to force delay between reads.

0 commit comments

Comments
 (0)