Skip to content

Commit

Permalink
fix #24 improve precision (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart authored Jul 12, 2021
1 parent c0c6090 commit 11292be
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
16 changes: 10 additions & 6 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.3.3
// VERSION: 0.3.4
// PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor
// URL: https://github.com/RobTillaart/GY521
//
Expand All @@ -21,6 +21,7 @@
// 0.3.1 2021-06-13 added more unit test + some initialization
// 0.3.2 2021-07-05 fix #20 support multiWire
// 0.3.3 2021-07-05 fix #22 improve maths
// 0.3.4 2021-07-12 fix #24 improve precision


#include "GY521.h"
Expand Down Expand Up @@ -93,14 +94,16 @@ bool GY521::wakeup()

int16_t GY521::read()
{
uint32_t now = millis();
if (_throttle)
{
if ((millis() - _lastTime) < _throttleTime)
if ((now - _lastTime) < _throttleTime)
{
// not an error.
return GY521_THROTTLED;
}
}
_lastTime = now;

// Connected ?
_wire->beginTransmission(_address);
Expand Down Expand Up @@ -129,10 +132,11 @@ int16_t GY521::read()
_gy = _WireRead2(); // GYRO_YOUT_H GYRO_YOUT_L
_gz = _WireRead2(); // GYRO_ZOUT_H GYRO_ZOUT_L

// time interval
uint32_t now = millis();
float duration = (now - _lastTime) * 0.001; // time in seconds.
_lastTime = now;
// duration interval
now = micros();
float duration = (now - _lastMicros) * 1e-6; // duration in seconds.
_lastMicros = now;


// next lines might be merged per axis.

Expand Down
5 changes: 3 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.3.3
// VERSION: 0.3.4
// PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor
// URL: https://github.com/RobTillaart/GY521
//
Expand All @@ -15,7 +15,7 @@
#include "Wire.h"


#define GY521_LIB_VERSION (F("0.3.3"))
#define GY521_LIB_VERSION (F("0.3.4"))


#ifndef GY521_THROTTLE_TIME
Expand Down Expand Up @@ -104,6 +104,7 @@ class GY521
bool _throttle = true; // to prevent reading too fast
uint16_t _throttleTime = GY521_THROTTLE_TIME;
uint32_t _lastTime = 0; // to measure duration for math & throttle
uint32_t _lastMicros = 0; // to measure duration for math & throttle
int16_t _error = GY521_OK; // initially everything is OK

uint8_t _afs = 0; // sensitivity factor
Expand Down
2 changes: 1 addition & 1 deletion GY521_registers.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: GY521_registers.h
// AUTHOR: Rob Tillaart
// VERSION: 0.3.3
// VERSION: see GY521.cpp
// PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor
// URL: https://github.com/RobTillaart/GY521
//
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.3.3",
"version": "0.3.4",
"license": "MIT",
"frameworks": "arduino",
"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.3.3
version=0.3.4
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Arduino library for GY521 angle measurement
Expand Down

0 comments on commit 11292be

Please sign in to comment.