Skip to content

Commit b0a5174

Browse files
authored
fix #42, pitch and roll (#43)
- fix #42 roll + pitch "jumps" after full rotation. - fixed normalization code. - made normalization conditional - add **void setNormalize(bool normalize = true)** - add **bool getNormalize()** - update readme.md - minor edits
1 parent 790735f commit b0a5174

File tree

7 files changed

+102
-53
lines changed

7 files changed

+102
-53
lines changed

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,24 @@ 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.4.0] - 2023-06-11
10+
- fix #42 roll + pitch "jumps" after full rotation.
11+
- fixed normalization code.
12+
- made normalization conditional
13+
- add **void setNormalize(bool normalize = true)**
14+
- add **bool getNormalize()**
15+
- update readme.md
16+
- minor edits
17+
18+
----
19+
920
## [0.3.9] - 2023-01-27
1021
- update GitHub actions
1122
- update license 2023
1223
- edit changelog.md
1324
- update readme.md
1425
- minor edits
1526

16-
1727
## [0.3.8] - 2022-10-07
1828
- added CHANGELOG.md
1929
- fix #36 - limit values of pitch() roll() yaw() to 0..360 range.

GY521.cpp

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

@@ -128,7 +128,7 @@ int16_t GY521::read()
128128
_lastMicros = now;
129129

130130

131-
// next lines might be merged per axis.
131+
// next lines might be merged per axis. (performance)
132132

133133
// Convert raw acceleration to g's
134134
_ax *= _raw2g;
@@ -170,23 +170,37 @@ int16_t GY521::read()
170170
_gay += _gy * duration;
171171
_gaz += _gz * duration;
172172

173-
// normalize internal vars
174-
if (_gax < 0) _gax += 360.0;
175-
else if (_gax >= 360.0) _gax -= 360.0;
176-
if (_gay < 0) _gay += 360.0;
177-
else if (_gay >= 360.0) _gay -= 360.0;
178-
if (_gaz < 0) _gaz += 360.0;
179-
else if (_gaz >= 360.0) _gaz -= 360.0;
173+
// normalize
174+
// _gax etc might loose precision after many iterations #36
175+
if (_normalize)
176+
{
177+
// correction at 375 due to the factor 0.96 in roll
178+
if (_gax >= 375) _gax -= 375;
179+
else if (_gax < 0) _gax += 375;
180+
// correction at 375 due to the factor 0.96 in pitch
181+
if (_gay >= 375) _gay -= 375;
182+
else if (_gay < 0) _gay += 375;
183+
// correction at 360
184+
if (_gaz >= 360) _gaz -= 360;
185+
else if (_gaz < 0) _gaz += 360;
186+
}
187+
180188

189+
// Calculate Pitch Roll Yaw
181190
_yaw = _gaz;
182-
_pitch = 0.96 * _gay + 0.04 * _aay;
183191
_roll = 0.96 * _gax + 0.04 * _aax;
192+
_pitch = 0.96 * _gay + 0.04 * _aay;
184193

185-
// normalize external vars.
186-
if (_pitch < 0) _pitch += 360;
187-
else if (_pitch >= 360) _pitch -= 360;
188-
if (_roll < 0) _roll += 360;
189-
else if (_roll >= 360) _roll -= 360;
194+
195+
if (_normalize)
196+
{
197+
if (_pitch >= 360) _pitch -= 360;
198+
else if (_pitch < 0) _pitch += 360;
199+
if (_roll >= 360) _roll -= 360;
200+
else if (_roll < 0) _roll += 360;
201+
if (_yaw >= 360) _yaw -= 360;
202+
else if (_yaw < 0) _yaw += 360;
203+
}
190204

191205
return GY521_OK;
192206
}
@@ -310,14 +324,21 @@ int16_t GY521::readGyro()
310324
_gay += _gy * duration;
311325
_gaz += _gz * duration;
312326

313-
// normalize internal vars
314-
if (_gax < 0) _gax += 360.0;
315-
else if (_gax >= 360.0) _gax -= 360.0;
316-
if (_gay < 0) _gay += 360.0;
317-
else if (_gay >= 360.0) _gay -= 360.0;
318-
if (_gaz < 0) _gaz += 360.0;
319-
else if (_gaz >= 360.0) _gaz -= 360.0;
320327

328+
// normalize
329+
// _gax etc might loose precision after many iterations #36
330+
if (_normalize)
331+
{
332+
// correction at 375 due to the factor 0.96 in roll
333+
if (_gax >= 375) _gax -= 375;
334+
else if (_gax < 0) _gax += 375;
335+
// correction at 375 due to the factor 0.96 in pitch
336+
if (_gay >= 375) _gay -= 375;
337+
else if (_gay < 0) _gay += 375;
338+
// correction at 360
339+
if (_gaz >= 360) _gaz -= 360;
340+
else if (_gaz < 0) _gaz += 360;
341+
}
321342
return GY521_OK;
322343
}
323344

GY521.h

Lines changed: 17 additions & 10 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.3.9
5+
// VERSION: 0.4.0
66
// PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor
77
// URL: https://github.com/RobTillaart/GY521
88
//
@@ -12,7 +12,7 @@
1212
#include "Wire.h"
1313

1414

15-
#define GY521_LIB_VERSION (F("0.3.9"))
15+
#define GY521_LIB_VERSION (F("0.4.0"))
1616

1717

1818
// THROTTLE TIMING
@@ -58,6 +58,18 @@ class GY521
5858
uint16_t getThrottleTime() { return _throttleTime; };
5959

6060

61+
// SET BEFORE READ
62+
// as = 0,1,2,3 ==> 2g 4g 8g 16g
63+
bool setAccelSensitivity(uint8_t as);
64+
uint8_t getAccelSensitivity(); // returns 0,1,2,3
65+
// gs = 0,1,2,3 ==> 250, 500, 1000, 2000 degrees/second
66+
bool setGyroSensitivity(uint8_t gs);
67+
uint8_t getGyroSensitivity(); // returns 0,1,2,3
68+
// normalizes Pitch Roll and Yaw.
69+
void setNormalize(bool normalize = true) { _normalize = normalize; };
70+
bool getNormalize() { return _normalize; };
71+
72+
6173
// READ THE SENSOR
6274
// returns GY521_OK or one of the error codes above.
6375
int16_t read();
@@ -70,13 +82,6 @@ class GY521
7082
// read temperature only, does not affect throttle.
7183
int16_t readTemperature();
7284

73-
// SET BEFORE READ
74-
// as = 0,1,2,3 ==> 2g 4g 8g 16g
75-
bool setAccelSensitivity(uint8_t as);
76-
uint8_t getAccelSensitivity(); // returns 0,1,2,3
77-
// gs = 0,1,2,3 ==> 250, 500, 1000, 2000 degrees/second
78-
bool setGyroSensitivity(uint8_t gs);
79-
uint8_t getGyroSensitivity(); // returns 0,1,2,3
8085

8186
// CALL AFTER READ
8287
float getAccelX() { return _ax; };
@@ -128,11 +133,13 @@ class GY521
128133
uint8_t _gfs = 0;
129134
float _raw2dps = GY521_RAW2DPS;
130135
float _gx, _gy, _gz; // gyro raw
131-
float _gax, _gay, _gaz; // gyro processed.
136+
float _gax, _gay, _gaz; // gyro processed
132137
float _pitch, _roll, _yaw; // used by user
133138

134139
float _temperature = 0;
135140

141+
bool _normalize = true; // default true.
142+
136143
// to read register of 2 bytes.
137144
int16_t _WireRead2();
138145

README.md

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Experimental library for GY521 a.k.a. MCU-6050.
1818
Library is work in progress, in fact it is extracted and extended from an old project.
1919
It needs to be tested a lot more.
2020

21+
See changelog.md for latest updates.
22+
2123

2224
#### Examples
2325

@@ -93,6 +95,8 @@ AD0 connected to VCC => 0x69
9395
- **uint8_t getAccelSensitivity()** returns 0, 1, 2, 3
9496
- **bool setGyroSensitivity(uint8_t gs)** gs = 0,1,2,3 ==> 250, 500, 1000, 2000 degrees/second
9597
- **uint8_t getGyroSensitivity()** returns 0, 1, 2, 3
98+
= **void setNormalize(bool normalize = true)** normalizes pitch roll yaw or not. Default true.
99+
= **bool getNormalize()** returns flag.
96100

97101

98102
#### Actual read
@@ -108,28 +112,33 @@ returns status = GY521_OK on success.
108112
- **uint32_t lastTime()** last time sensor is actually read. In milliseconds. Not updated by readTemperature().
109113

110114
Since version 0.3.8 the **read()** and **readGyro()** function is updated to keep the range of **getPitch()**,
111-
**getRoll()** and **getYaw()** in the range 0..360 degrees. (Issue #36).
112-
Problem is that with continuous rotation in a same direction internal variables will overflow and new
115+
**getRoll()** and **getYaw()** in the range 0..359.999 degrees. (Issue #36).
116+
Problem is that with continuous rotation in a same direction internal variables will overflow and new
113117
movements (angles) will get lost as insignificant digits.
114118

119+
In version 0.4.0 the normalization of pitch, roll and yaw is fixed and made conditional. (Issue #42).
120+
115121

116-
#### Call after read
122+
#### Calls after read
117123

118124
Note that multiple calls will return the same value. One must explicitly call **read()** to get new values.
119125

120-
- **float getAccelX()** idem
121-
- **float getAccelY()** idem
122-
- **float getAccelZ()** idem
123-
- **float getAngleX()** idem
124-
- **float getAngleY()** idem
125-
- **float getAngleZ()** idem
126-
- **float getTemperature()** idem
127-
- **float getGyroX()** idem
128-
- **float getGyroY()** idem
129-
- **float getGyroZ()** idem
130-
- **float getPitch()** idem. Returns 0.00 - 359.99.
131-
- **float getRoll()** idem. Returns 0.00 - 359.99.
132-
- **float getYaw()** idem. Returns 0.00 - 359.99.
126+
- **float getAccelX()** idem.
127+
- **float getAccelY()** idem.
128+
- **float getAccelZ()** idem.
129+
- **float getAngleX()** idem.
130+
- **float getAngleY()** idem.
131+
- **float getAngleZ()** idem.
132+
- **float getTemperature()** idem.
133+
- **float getGyroX()** idem.
134+
- **float getGyroY()** idem.
135+
- **float getGyroZ()** idem.
136+
- **float getPitch()** idem. May return any number.
137+
If **setNormalize(true)** return value will be 0-359.999
138+
- **float getRoll()** idem. May return any number.
139+
If **setNormalize(true)** return value will be 0-359.999
140+
- **float getYaw()** idem. May return any number.
141+
If **setNormalize(true)** return value will be 0-359.999
133142

134143

135144
### Register access
@@ -155,10 +164,10 @@ See examples, use with care
155164
#### Must
156165

157166
- improve documentation
167+
- test test and test ...(ESP too)
158168

159-
#### Should
160169

161-
- test test and test ...(ESP too)
170+
#### Should
162171

163172
#### Could
164173

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ setThrottle KEYWORD2
2020
getThrottle KEYWORD2
2121
setThrottleTime KEYWORD2
2222
getThrottleTime KEYWORD2
23+
setNormalize KEYWORD2
24+
getNormalize KEYWORD2
2325

2426
setAccelSensitivity KEYWORD2
2527
getAccelSensitivity KEYWORD2

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/GY521.git"
1717
},
18-
"version": "0.3.9",
18+
"version": "0.4.0",
1919
"license": "MIT",
2020
"frameworks": "arduino",
2121
"platforms": "*",

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=GY521
2-
version=0.3.9
2+
version=0.4.0
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=Arduino library for GY521 angle measurement

0 commit comments

Comments
 (0)