Skip to content

Commit

Permalink
Fix #51, #52, Add LowPass Filter functions
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed May 8, 2024
1 parent c23e6c5 commit 3217457
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 24 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.5.3] - 2024-05-08
- 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


## [0.5.2] - 2024-01-16
- fix #48, use float variables in example GY521_test_1.ino
- add **void calibrate(uint16_t times)** to API
Expand All @@ -17,7 +24,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- add some tables
- minor edits in examples


## [0.5.1] - 2023-12-11
- redo initialization order.

Expand Down
33 changes: 31 additions & 2 deletions GY521.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: GY521.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.5.2
// VERSION: 0.5.3
// PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor
// URL: https://github.com/RobTillaart/GY521

Expand Down Expand Up @@ -53,6 +53,12 @@ bool GY521::isConnected()
}


uint8_t GY521::getAddress()
{
return _address;
}


void GY521::reset()
{
setThrottleTime(GY521_THROTTLE_TIME);
Expand All @@ -71,7 +77,7 @@ void GY521::calibrate(uint16_t times)
// disable throttling / caching of read values.
bool oldThrottle = _throttle;
_throttle = false;

// set errors to zero
axe = aye = aze = 0;
gxe = gye = gze = 0;
Expand Down Expand Up @@ -476,6 +482,29 @@ uint8_t GY521::getGyroSensitivity()
}


// CONFIGURATION
// Digital Low Pass Filter datasheet P13-reg26
bool GY521::setDLPFMode(uint8_t mode)
{
if (mode > 6)
{
return false;
}
uint8_t val = getRegister(GY521_CONFIG);
val &= 0xF8;
val |= mode;
return true;
}


uint8_t GY521::getDLPFMode()
{
uint8_t val = getRegister(GY521_CONFIG);
return val & 0x07;
}


// GENERIC
uint8_t GY521::setRegister(uint8_t reg, uint8_t value)
{
_wire->beginTransmission(_address);
Expand Down
11 changes: 9 additions & 2 deletions GY521.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: GY521.h
// AUTHOR: Rob Tillaart
// VERSION: 0.5.2
// VERSION: 0.5.3
// PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor
// URL: https://github.com/RobTillaart/GY521

Expand All @@ -11,7 +11,9 @@
#include "Wire.h"


#define GY521_LIB_VERSION (F("0.5.2"))
#define GY521_LIB_VERSION (F("0.5.3"))

const float GRAVITY = 9.80655;


// THROTTLE TIMING
Expand Down Expand Up @@ -42,6 +44,7 @@ class GY521

bool begin();
bool isConnected();
uint8_t getAddress();
void reset();

// EXPERIMENTAL
Expand Down Expand Up @@ -105,6 +108,10 @@ class GY521
// last time sensor is actually read.
uint32_t lastTime() { return _lastTime; };

// CONFIGURATION
// Digital Low Pass Filter - datasheet P13-reg26
bool setDLPFMode(uint8_t mode); // returns false if mode > 6
uint8_t getDLPFMode();

// generic worker to get access to all functionality
uint8_t setRegister(uint8_t reg, uint8_t value);
Expand Down
59 changes: 42 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ for analysis e.g. in a spreadsheet.
- https://github.com/RobTillaart/AngleConverter



## Breakout board

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


#### Actual read
Expand Down Expand Up @@ -196,7 +195,25 @@ If **setNormalize(true)** return value will be 0-359.999
If **setNormalize(true)** return value will be 0-359.999


### Register access
#### Digital Low Pass Filter

See datasheet P13-reg26

- **bool setDLPFMode(uint8_t mode)** mode = 0..6, returns false if mode > 6.
- **uint8_t getDLPFMode()** returns the current (set) mode.

| Mode | Acc bandwidth | delay | Gyro bandwidth | delay | Fs |
|:----:|:-------------:|:------:|:--------------:|:------:|:-----:|
| 0 | 260 Hz | 0.0 | 256 Hz | 1.0 | 8 kHz |
| 1 | 184 Hz | 2.0 | 188 Hz | 1.9 | 1 kHz |
| 2 | 94 Hz | 3.0 | 98 Hz | 2.8 | 1 kHz |
| 3 | 44 Hz | 4.9 | 42 Hz | 4.8 | 1 kHz |
| 4 | 21 Hz | 8.5 | 20 Hz | 8.3 | 1 kHz |
| 5 | 10 Hz | 13.8 | 10 Hz | 13.4 | 1 kHz |
| 6 | 5 Hz | 19.0 | 5 Hz | 18.6 | 1 kHz |


#### Generic Register Access

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

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

#### Sensitivity Acceleration

unit g = gravity == 9.81 m/s^2
The strength of Earth's gravity varies with latitude (equator = 0°, poles = 90°).
The standard value for gravity (gn) is 9.80665 m/s^2 (often 9.81 m/s^2)
At the equator the gravity (ge) is 9.78033 m/s^2.

The library provides the constant GRAVITY = 9.80655

| Acceleration | value | notes |
|:--------------|:-------:|:-------:|
| 2 g | 0 | default
| 4 g | 1 |
| 8 g | 2 |
| 16 g | 3 |

| value | Acceleration | m/s2 | notes |
|:-------:|:--------------|:----------:|:-------:|
| 0 | 2 g | 19.6131 | default
| 1 | 4 g | 39.2262 |
| 2 | 8 g | 78.4524 |
| 3 | 16 g | 156.9048 |


#### Sensitivity Gyroscope

unit dps = degrees per second.

| Gyroscope | value | notes |
|:--------------|:-------:|:-------:|
| 250 dps | 0 | default
| 500 dps | 1 |
| 1000 dps | 2 |
| 2000 dps | 3 |
| value | Gyroscope | radians/sec | notes |
|:-------:|:------------|:-------------:|:-------:|
| 0 | 250 dps | 4.36332313 | default
| 1 | 500 dps | 8.72664626 |
| 2 | 1000 dps | 17.45329252 |
| 3 | 2000 dps | 34.90658504 |


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

## Future

There is no intention to implement getters and setters for all registers.
However if one specific is needed, please open an issue.

#### Must

- time
Expand Down
6 changes: 6 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ GY521 KEYWORD1
# Methods and Functions (KEYWORD2)
begin KEYWORD2
isConnected KEYWORD2
getAddress KEYWORD2
reset KEYWORD2
wakeup KEYWORD2

Expand Down Expand Up @@ -43,8 +44,13 @@ getRoll KEYWORD2
getYaw KEYWORD2

lastTime KEYWORD2

setDLPFMode KEYWORD2
getDLPFMode KEYWORD2

setRegister KEYWORD2
getRegister KEYWORD2

getError KEYWORD2


Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/GY521.git"
},
"version": "0.5.2",
"version": "0.5.3",
"license": "MIT",
"frameworks": "*",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=GY521
version=0.5.2
version=0.5.3
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Arduino library for GY521 angle measurement
Expand Down

0 comments on commit 3217457

Please sign in to comment.