Skip to content

Commit 7776805

Browse files
authored
Fix #51, #52, Add LowPass Filter functions (#53)
- fix #52, add **uint8_t getAddrress()** - fix #51, add **bool setDLPFMode(uint8_t mode)** and **uint8_t getDLPFMode()** - add const float GRAVITY=9.80655; - minor edits
1 parent c23e6c5 commit 7776805

File tree

7 files changed

+99
-24
lines changed

7 files changed

+99
-24
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ 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.3] - 2024-05-08
10+
- fix #52, add **uint8_t getAddrress()**
11+
- fix #51, add **bool setDLPFMode(uint8_t mode)** and **uint8_t getDLPFMode()**
12+
- add const float GRAVITY=9.80655;
13+
- minor edits
14+
15+
916
## [0.5.2] - 2024-01-16
1017
- fix #48, use float variables in example GY521_test_1.ino
1118
- add **void calibrate(uint16_t times)** to API
@@ -17,7 +24,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1724
- add some tables
1825
- minor edits in examples
1926

20-
2127
## [0.5.1] - 2023-12-11
2228
- redo initialization order.
2329

GY521.cpp

Lines changed: 32 additions & 2 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.5.2
4+
// VERSION: 0.5.3
55
// PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor
66
// URL: https://github.com/RobTillaart/GY521
77

@@ -53,6 +53,12 @@ bool GY521::isConnected()
5353
}
5454

5555

56+
uint8_t GY521::getAddress()
57+
{
58+
return _address;
59+
}
60+
61+
5662
void GY521::reset()
5763
{
5864
setThrottleTime(GY521_THROTTLE_TIME);
@@ -71,7 +77,7 @@ void GY521::calibrate(uint16_t times)
7177
// disable throttling / caching of read values.
7278
bool oldThrottle = _throttle;
7379
_throttle = false;
74-
80+
7581
// set errors to zero
7682
axe = aye = aze = 0;
7783
gxe = gye = gze = 0;
@@ -476,6 +482,30 @@ uint8_t GY521::getGyroSensitivity()
476482
}
477483

478484

485+
// CONFIGURATION
486+
// Digital Low Pass Filter datasheet P13-reg26
487+
bool GY521::setDLPFMode(uint8_t mode)
488+
{
489+
if (mode > 6)
490+
{
491+
_error = GY521_ERROR_PARAMETER;
492+
return false;
493+
}
494+
uint8_t value = getRegister(GY521_CONFIG);
495+
value &= 0xF8;
496+
value |= mode;
497+
return (setRegister(GY521_CONFIG, value) == GY521_OK);
498+
}
499+
500+
501+
uint8_t GY521::getDLPFMode()
502+
{
503+
uint8_t val = getRegister(GY521_CONFIG);
504+
return val & 0x07;
505+
}
506+
507+
508+
// GENERIC
479509
uint8_t GY521::setRegister(uint8_t reg, uint8_t value)
480510
{
481511
_wire->beginTransmission(_address);

GY521.h

Lines changed: 10 additions & 2 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.2
5+
// VERSION: 0.5.3
66
// PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor
77
// URL: https://github.com/RobTillaart/GY521
88

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

1313

14-
#define GY521_LIB_VERSION (F("0.5.2"))
14+
#define GY521_LIB_VERSION (F("0.5.3"))
15+
16+
const float GRAVITY = 9.80655;
1517

1618

1719
// THROTTLE TIMING
@@ -26,6 +28,7 @@
2628
#define GY521_ERROR_READ -1
2729
#define GY521_ERROR_WRITE -2
2830
#define GY521_ERROR_NOT_CONNECTED -3
31+
#define GY521_ERROR_PARAMETER -4
2932

3033

3134
// CONVERSION CONSTANTS
@@ -42,6 +45,7 @@ class GY521
4245

4346
bool begin();
4447
bool isConnected();
48+
uint8_t getAddress();
4549
void reset();
4650

4751
// EXPERIMENTAL
@@ -105,6 +109,10 @@ class GY521
105109
// last time sensor is actually read.
106110
uint32_t lastTime() { return _lastTime; };
107111

112+
// CONFIGURATION
113+
// Digital Low Pass Filter - datasheet P13-reg26
114+
bool setDLPFMode(uint8_t mode); // returns false if mode > 6
115+
uint8_t getDLPFMode();
108116

109117
// generic worker to get access to all functionality
110118
uint8_t setRegister(uint8_t reg, uint8_t value);

README.md

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ for analysis e.g. in a spreadsheet.
5656
- https://github.com/RobTillaart/AngleConverter
5757

5858

59-
6059
## Breakout board
6160

6261
From left to right
@@ -142,8 +141,8 @@ Drawback is that this would make the duration unpredictable.
142141
- **uint8_t getAccelSensitivity()** returns 0, 1, 2, 3
143142
- **bool setGyroSensitivity(uint8_t gs)** gs = 0,1,2,3 ==> 250, 500, 1000, 2000 degrees/second
144143
- **uint8_t getGyroSensitivity()** returns 0, 1, 2, 3
145-
= **void setNormalize(bool normalize = true)** normalizes pitch roll yaw or not. Default true.
146-
= **bool getNormalize()** returns flag.
144+
- **void setNormalize(bool normalize = true)** normalizes pitch roll yaw or not. Default true.
145+
- **bool getNormalize()** returns flag.
147146

148147

149148
#### Actual read
@@ -196,7 +195,25 @@ If **setNormalize(true)** return value will be 0-359.999
196195
If **setNormalize(true)** return value will be 0-359.999
197196

198197

199-
### Register access
198+
#### Digital Low Pass Filter
199+
200+
See datasheet P13-reg26
201+
202+
- **bool setDLPFMode(uint8_t mode)** mode = 0..6, returns false if mode > 6.
203+
- **uint8_t getDLPFMode()** returns the current (set) mode.
204+
205+
| Mode | Acc bandwidth | delay | Gyro bandwidth | delay | Fs |
206+
|:----:|:-------------:|:------:|:--------------:|:------:|:-----:|
207+
| 0 | 260 Hz | 0.0 | 256 Hz | 1.0 | 8 kHz |
208+
| 1 | 184 Hz | 2.0 | 188 Hz | 1.9 | 1 kHz |
209+
| 2 | 94 Hz | 3.0 | 98 Hz | 2.8 | 1 kHz |
210+
| 3 | 44 Hz | 4.9 | 42 Hz | 4.8 | 1 kHz |
211+
| 4 | 21 Hz | 8.5 | 20 Hz | 8.3 | 1 kHz |
212+
| 5 | 10 Hz | 13.8 | 10 Hz | 13.4 | 1 kHz |
213+
| 6 | 5 Hz | 19.0 | 5 Hz | 18.6 | 1 kHz |
214+
215+
216+
#### Generic Register Access
200217

201218
Read the register PDF for the specific value and meaning of registers.
202219

@@ -222,26 +239,31 @@ Read the register PDF for the specific value and meaning of registers.
222239

223240
#### Sensitivity Acceleration
224241

225-
unit g = gravity == 9.81 m/s^2
242+
The strength of Earth's gravity varies with latitude (equator = 0°, poles = 90°).
243+
The standard value for gravity (gn) is 9.80665 m/s^2 (often 9.81 m/s^2)
244+
At the equator the gravity (ge) is 9.78033 m/s^2.
245+
246+
The library provides the constant GRAVITY = 9.80655
226247

227-
| Acceleration | value | notes |
228-
|:--------------|:-------:|:-------:|
229-
| 2 g | 0 | default
230-
| 4 g | 1 |
231-
| 8 g | 2 |
232-
| 16 g | 3 |
248+
249+
| value | Acceleration | m/s2 | notes |
250+
|:-------:|:--------------|:----------:|:-------:|
251+
| 0 | 2 g | 19.6131 | default
252+
| 1 | 4 g | 39.2262 |
253+
| 2 | 8 g | 78.4524 |
254+
| 3 | 16 g | 156.9048 |
233255

234256

235257
#### Sensitivity Gyroscope
236258

237259
unit dps = degrees per second.
238260

239-
| Gyroscope | value | notes |
240-
|:--------------|:-------:|:-------:|
241-
| 250 dps | 0 | default
242-
| 500 dps | 1 |
243-
| 1000 dps | 2 |
244-
| 2000 dps | 3 |
261+
| value | Gyroscope | radians/sec | notes |
262+
|:-------:|:------------|:-------------:|:-------:|
263+
| 0 | 250 dps | 4.36332313 | default
264+
| 1 | 500 dps | 8.72664626 |
265+
| 2 | 1000 dps | 17.45329252 |
266+
| 3 | 2000 dps | 34.90658504 |
245267

246268

247269
## Operation
@@ -251,6 +273,9 @@ See examples, use with care.
251273

252274
## Future
253275

276+
There is no intention to implement getters and setters for all registers.
277+
However if one specific is needed, please open an issue.
278+
254279
#### Must
255280

256281
- time

keywords.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ GY521 KEYWORD1
88
# Methods and Functions (KEYWORD2)
99
begin KEYWORD2
1010
isConnected KEYWORD2
11+
getAddress KEYWORD2
1112
reset KEYWORD2
1213
wakeup KEYWORD2
1314

@@ -43,8 +44,13 @@ getRoll KEYWORD2
4344
getYaw KEYWORD2
4445

4546
lastTime KEYWORD2
47+
48+
setDLPFMode KEYWORD2
49+
getDLPFMode KEYWORD2
50+
4651
setRegister KEYWORD2
4752
getRegister KEYWORD2
53+
4854
getError KEYWORD2
4955

5056

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.5.2",
18+
"version": "0.5.3",
1919
"license": "MIT",
2020
"frameworks": "*",
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.5.2
2+
version=0.5.3
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)