Skip to content

Commit 1c2e645

Browse files
authored
update example / documentation (#49)
- fix #48, use float variables in example GY521_test_1.ino - add **void calibrate(uint16_t times)** to API - add **GY521_performance_calibrate.ino** example - add **GY521_raw_cooked.ino** example - make explicit that pitch roll yaw is work in progress. - update readme.md - calibrate section - add some tables - minor edits in examples
1 parent 4fc890a commit 1c2e645

File tree

17 files changed

+392
-55
lines changed

17 files changed

+392
-55
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.5.2] - 2024-01-16
10+
- fix #48, use float variables in example GY521_test_1.ino
11+
- add **void calibrate(uint16_t times)** to API
12+
- add **GY521_performance_calibrate.ino** example
13+
- add **GY521_raw_cooked.ino** example
14+
- make explicit that pitch roll yaw is work in progress.
15+
- update readme.md
16+
- calibrate section
17+
- add some tables
18+
- minor edits in examples
19+
20+
921
## [0.5.1] - 2023-12-11
1022
- redo initialization order.
1123

12-
1324
## [0.5.0] - 2023-12-05
1425
- refactor API, begin()
1526
- update readme.md

GY521.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: GY521.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.5.1
4+
// VERSION: 0.5.2
55
// PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor
66
// URL: https://github.com/RobTillaart/GY521
77

@@ -66,6 +66,41 @@ void GY521::reset()
6666
}
6767

6868

69+
void GY521::calibrate(uint16_t times)
70+
{
71+
// disable throttling / caching of read values.
72+
bool oldThrottle = _throttle;
73+
_throttle = false;
74+
75+
// set errors to zero
76+
axe = aye = aze = 0;
77+
gxe = gye = gze = 0;
78+
79+
for (uint16_t i = 0; i < times; i++)
80+
{
81+
read();
82+
axe -= getAccelX();
83+
aye -= getAccelY();
84+
aze -= getAccelZ();
85+
gxe -= getGyroX();
86+
gye -= getGyroY();
87+
gze -= getGyroZ();
88+
}
89+
90+
// adjust calibration errors so table should get all zero's.
91+
float factor = 1.0 / times;
92+
axe *= factor;
93+
aye *= factor;
94+
aze *= factor;
95+
gxe *= factor;
96+
gye *= factor;
97+
gze *= factor;
98+
99+
// restore throttle state
100+
_throttle = oldThrottle;
101+
}
102+
103+
69104
bool GY521::wakeup()
70105
{
71106
_wire->beginTransmission(_address);
@@ -138,6 +173,7 @@ int16_t GY521::read()
138173
float _ay2 = _ay * _ay;
139174
float _az2 = _az * _az;
140175

176+
// calculate angle
141177
_aax = atan( _ay / sqrt(_ax2 + _az2)) * GY521_RAD2DEGREES;
142178
_aay = atan(-1.0 * _ax / sqrt(_ay2 + _az2)) * GY521_RAD2DEGREES;
143179
_aaz = atan( _az / sqrt(_ax2 + _ay2)) * GY521_RAD2DEGREES;
@@ -159,10 +195,13 @@ int16_t GY521::read()
159195
_gy += gye;
160196
_gz += gze;
161197

198+
// integrate gyroscope data
162199
_gax += _gx * duration;
163200
_gay += _gy * duration;
164201
_gaz += _gz * duration;
165202

203+
// experimental below this line.
204+
// Pitch Roll Yaw are work in progress
166205
// normalize
167206
// _gax etc might loose precision after many iterations #36
168207
if (_normalize)
@@ -250,6 +289,7 @@ int16_t GY521::readAccel()
250289
float _ay2 = _ay * _ay;
251290
float _az2 = _az * _az;
252291

292+
// calculate angles.
253293
_aax = atan( _ay / sqrt(_ax2 + _az2)) * GY521_RAD2DEGREES;
254294
_aay = atan(-1.0 * _ax / sqrt(_ay2 + _az2)) * GY521_RAD2DEGREES;
255295
_aaz = atan( _az / sqrt(_ax2 + _ay2)) * GY521_RAD2DEGREES;
@@ -318,6 +358,7 @@ int16_t GY521::readGyro()
318358
_gaz += _gz * duration;
319359

320360

361+
// experimental below this line.
321362
// normalize
322363
// _gax etc might loose precision after many iterations #36
323364
if (_normalize)

GY521.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: GY521.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.5.1
5+
// VERSION: 0.5.2
66
// PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor
77
// URL: https://github.com/RobTillaart/GY521
88

@@ -11,7 +11,7 @@
1111
#include "Wire.h"
1212

1313

14-
#define GY521_LIB_VERSION (F("0.5.1"))
14+
#define GY521_LIB_VERSION (F("0.5.2"))
1515

1616

1717
// THROTTLE TIMING
@@ -37,18 +37,23 @@
3737
class GY521
3838
{
3939
public:
40-
GY521(uint8_t address = 0x69, TwoWire *wire = &Wire); // 0x68 or 0x69
40+
// address == 0x68 or 0x69
41+
GY521(uint8_t address = 0x69, TwoWire *wire = &Wire);
4142

4243
bool begin();
4344
bool isConnected();
4445
void reset();
4546

47+
// EXPERIMENTAL
48+
// calibrate needs to be called to compensate for errors.
49+
// must be called after setAccelSensitivity(as); and setGyroSensitivity(gs);
50+
void calibrate(uint16_t times);
4651

4752
bool wakeup();
4853
// throttle to force delay between reads.
4954
void setThrottle(bool throttle = true) { _throttle = throttle; };
5055
bool getThrottle() { return _throttle; };
51-
// 0..65535 (max milliseconds == roughly 1 minute.
56+
// 0..65535 max milliseconds == roughly 1 minute.
5257
void setThrottleTime(uint16_t ti ) { _throttleTime = ti; };
5358
uint16_t getThrottleTime() { return _throttleTime; };
5459

@@ -89,6 +94,9 @@ class GY521
8994
float getGyroX() { return _gx; };
9095
float getGyroY() { return _gy; };
9196
float getGyroZ() { return _gz; };
97+
98+
// EXPERIMENTAL
99+
// pitch, roll and yaw is work in progress.
92100
float getPitch() { return _pitch; };
93101
float getRoll() { return _roll; };
94102
float getYaw() { return _yaw; };

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2017-2023 Rob Tillaart
3+
Copyright (c) 2017-2024 Rob Tillaart
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 101 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ Arduino library for I2C GY521 accelerometer-gyroscope sensor a.k.a. MCU-6050.
1616

1717
## Description
1818

19-
Experimental library for GY521 a.k.a. MCU-6050.
19+
**Experimental** library for GY521 a.k.a. MCU-6050.
2020

2121
Library is work in progress, in fact it is extracted and extended from an old project.
2222
It needs to be tested a lot more.
2323

2424
See changelog.md for latest updates.
2525

26+
2627
#### 0.5.0 Breaking change
2728

2829
Version 0.5.0 introduced a breaking change.
@@ -35,12 +36,25 @@ before calling **begin()**.
3536
#### Examples
3637

3738
- **GY521_angle** read angleX, angleY, angleZ.
39+
- **GY521_performance_calibrate.ino** determine calibration performance.
3840
- **GY521_performance** measure performance.
3941
- **GY521_pitch_roll_yaw** to get pitch roll yaw.
40-
- **GY521_readCalibration_1** read calibration values / errors for a flat sensor.
42+
- **GY521_raw_cooked** make a table of raw measurements and derived data
43+
for analysis e.g. in a spreadsheet.
44+
- **GY521_readCalibration_1** read calibration values / errors for a "flat" sensor.
4145
- **GY521_readCalibration_2** generate calibration code snippet.
4246
- **GY521_test_1** test working of the sensor.
43-
- **GY521_test_2** test set/get functions.
47+
- **GY521_test_2** test set/get functions (sort of unit test).
48+
- **GY521_two_sensors** demo for two sensors.
49+
50+
51+
#### Related
52+
53+
- https://invensense.tdk.com/wp-content/uploads/2015/02/MPU-6000-Datasheet1.pdf
54+
- https://cdn.sparkfun.com/datasheets/Sensors/Accelerometers/RM-MPU-6000A.pdf register map.
55+
- https://github.com/RobTillaart/Angle
56+
- https://github.com/RobTillaart/AngleConverter
57+
4458

4559

4660
## Breakout board
@@ -67,6 +81,16 @@ AD0 connected to VCC => 0x69
6781

6882
## Calibration (short version for now)
6983

84+
Since version 0.5.2 the library has a build in **void calibrate(times)** function which
85+
can be called instead of manual calibration.
86+
This function overwrites the values of axe aye aze gxe gye gze.
87+
**calibrate()** must be called after **setAccelSensitivity(as)** and **setGyroSensitivity(gs)**.
88+
89+
Note the **calibrate()** function takes time, depending on the number of times.
90+
91+
92+
#### Manual calibration
93+
7094
1. load and run calibration example
7195
it shows a header containing 6 numbers and 10 lines of 8 numbers
7296
1. wait until the middle 6 of the longer lines stabilize (should all be 0)
@@ -90,6 +114,18 @@ Note call **Wire.begin()** before **begin()**.
90114
- **bool wakeUp()** idem.
91115

92116

117+
### Calibrate
118+
119+
- **void calibrate(uint16_t times)** This function overwrites the values of axe aye aze gxe gye gze.
120+
To get "quality error" offsets, the GY521 sensor should not move during the calibration.
121+
The parameter times determines the number of measurements made.
122+
Typical values are 100 or more.
123+
Please note this is a time consuming function.
124+
125+
Ideal the function **calibrate()** should continue until it is stable (how to define) for n reads.
126+
Drawback is that this would make the duration unpredictable.
127+
128+
93129
### Throttle
94130

95131
- **void setThrottle(bool throttle = true)** throttle to force "delay" between reads.
@@ -112,7 +148,8 @@ Note call **Wire.begin()** before **begin()**.
112148

113149
#### Actual read
114150

115-
- **int16_t read()** returns status = GY521_OK on success.
151+
- **int16_t read()** reads all core measurements.
152+
returns status = GY521_OK on success.
116153
- **int16_t readAccel()** read accelerometer only to speed up interaction. This call does update the throttle timer.
117154
returns status = GY521_OK on success.
118155
- **int16_t readGyro()** read gyroscope only to speed up interaction. This call does update the throttle timer.
@@ -132,7 +169,8 @@ In version 0.4.0 the normalization of pitch, roll and yaw is fixed and made cond
132169

133170
#### Calls after read
134171

135-
Note that multiple calls will return the same value. One must explicitly call **read()** to get new values.
172+
Note that multiple calls will return the same value.
173+
One must explicitly call **read()** to get new values.
136174

137175
- **float getAccelX()** idem.
138176
- **float getAccelY()** idem.
@@ -144,6 +182,12 @@ Note that multiple calls will return the same value. One must explicitly call **
144182
- **float getGyroX()** idem.
145183
- **float getGyroY()** idem.
146184
- **float getGyroZ()** idem.
185+
186+
187+
#### Experimental Pitch Roll and Yaw
188+
189+
Pitch Roll and Yaw is work in progress and should not be used for projects yet.
190+
147191
- **float getPitch()** idem. May return any number.
148192
If **setNormalize(true)** return value will be 0-359.999
149193
- **float getRoll()** idem. May return any number.
@@ -160,34 +204,76 @@ Read the register PDF for the specific value and meaning of registers.
160204
- **uint8_t getRegister(uint8_t reg)**
161205

162206

163-
## documents
207+
## Documents
208+
209+
- check details registers - MPU-6000-Register-Map1.pdf
210+
211+
212+
#### Error codes
213+
214+
| Error code | value | notes |
215+
|:----------------------------|:-------:|:-------:|
216+
| GY521_OK | 0 | not an error
217+
| GY521_THROTTLED | 1 | not an error
218+
| GY521_ERROR_READ | -1 |
219+
| GY521_ERROR_WRITE | -2 |
220+
| GY521_ERROR_NOT_CONNECTED | -3 |
221+
164222

165-
- check details - MPU-6000-Register-Map1.pdf
223+
#### Sensitivity Acceleration
224+
225+
unit g = gravity == 9.81 m/s^2
226+
227+
| Acceleration | value | notes |
228+
|:--------------|:-------:|:-------:|
229+
| 2 g | 0 | default
230+
| 4 g | 1 |
231+
| 8 g | 2 |
232+
| 16 g | 3 |
233+
234+
235+
#### Sensitivity Gyroscope
236+
237+
unit dps = degrees per second.
238+
239+
| Gyroscope | value | notes |
240+
|:--------------|:-------:|:-------:|
241+
| 250 dps | 0 | default
242+
| 500 dps | 1 |
243+
| 1000 dps | 2 |
244+
| 2000 dps | 3 |
166245

167246

168247
## Operation
169248

170-
See examples, use with care
249+
See examples, use with care.
171250

172251

173252
## Future
174253

175254
#### Must
176255

256+
- time
177257
- improve documentation
178-
- add tables where appropriate
179-
- sensitivity, error codes etc
258+
- investigate Pitch Roll and Yaw math in detail.
259+
- investigate math needed.
260+
- implementation.
261+
- when?
180262
- test test and test ...(ESP too)
181263

182264
#### Should
183265

184-
- add performance sketch
266+
- test **calibrate()** function for different sensitivities.
185267

186268
#### Could
187269

188-
- calibrate sketch could print code snippet to include...
189270
- add examples
190271
- improve unit tests?
272+
- reorder code in read(), would that save some micros.?
273+
- first all ax, then ay etc
274+
- footprint / performance gain?
275+
- make enum for sensitivity Accel?
276+
- make enum for sensitivity Gyro?
191277

192278
#### Wont
193279

@@ -196,6 +282,9 @@ See examples, use with care
196282
- other ideas affect accuracy, so unless new ideas arise.
197283
- calibrate function in the lib
198284
- not as lib will grow too large.
285+
- defaults value for functions?
286+
- user must set function parameters explicit
287+
199288

200289
## Support
201290

examples/GY521_angle/GY521_angle.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void setup()
2626
while (sensor.wakeup() == false)
2727
{
2828
Serial.print(millis());
29-
Serial.println("\tCould not connect to GY521");
29+
Serial.println("\tCould not connect to GY521: please check the GY521 address (0x68/0x69)");
3030
delay(1000);
3131
}
3232
sensor.setAccelSensitivity(2); // 8g

0 commit comments

Comments
 (0)