Skip to content

Commit

Permalink
fix #22 improve maths (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart authored Jul 5, 2021
1 parent 3f148d7 commit c0c6090
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
29 changes: 21 additions & 8 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.2
// VERSION: 0.3.3
// PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor
// URL: https://github.com/RobTillaart/GY521
//
Expand All @@ -20,6 +20,7 @@
// 0.3.0 2021-04-07 fix #18 acceleration error correction (kudo's to Merkxic)
// 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


#include "GY521.h"
Expand All @@ -30,6 +31,8 @@
#define GY521_WAKEUP 0x00

#define RAD2DEGREES (180.0 / PI)
#define RAW2DPS_FACTOR (1.0 / 131.0)
#define RAW2G_FACTOR (1.0 / 16384.0)


/////////////////////////////////////////////////////
Expand Down Expand Up @@ -131,6 +134,8 @@ int16_t GY521::read()
float duration = (now - _lastTime) * 0.001; // time in seconds.
_lastTime = now;

// next lines might be merged per axis.

// Convert raw acceleration to g's
_ax *= _raw2g;
_ay *= _raw2g;
Expand All @@ -142,9 +147,17 @@ int16_t GY521::read()
_az += aze;

// prepare for Pitch Roll Yaw
_aax = atan(_ay / hypot(_ax, _az)) * RAD2DEGREES;
_aay = atan(-1.0 * _ax / hypot(_ay, _az)) * RAD2DEGREES;
_aaz = atan(_az / hypot(_ax, _ay)) * RAD2DEGREES;
float _ax2 = _ax * _ax;
float _ay2 = _ay * _ay;
float _az2 = _az * _az;

_aax = atan( _ay / sqrt(_ax2 + _az2)) * RAD2DEGREES;
_aay = atan(-1.0 * _ax / sqrt(_ay2 + _az2)) * RAD2DEGREES;
_aaz = atan( _az / sqrt(_ax2 + _ay2)) * RAD2DEGREES;
// optimize #22
// _aax = atan(_ay / hypot(_ax, _az)) * RAD2DEGREES;
// _aay = atan(-1.0 * _ax / hypot(_ay, _az)) * RAD2DEGREES;
// _aaz = atan(_az / hypot(_ax, _ay)) * RAD2DEGREES;

// Convert to Celsius
_temperature = _temperature * 0.00294117647 + 36.53; // == /340.0 + 36.53;
Expand Down Expand Up @@ -190,8 +203,8 @@ bool GY521::setAccelSensitivity(uint8_t as)
return false;
}
}
// calculate conversion factor.
_raw2g = (1 << _afs) / 16384.0;
// calculate conversion factor. // 4 possible values => lookup table?
_raw2g = (1 << _afs) * RAW2G_FACTOR;
return true;
}

Expand Down Expand Up @@ -227,8 +240,8 @@ bool GY521::setGyroSensitivity(uint8_t gs)
return false;
}
}
// calculate conversion factor.
_raw2dps = (1 << _gfs) / 131.0;
// calculate conversion factor.. // 4 possible values => lookup table?
_raw2dps = (1 << _gfs) * RAW2DPS_FACTOR;
return true;
}

Expand Down
4 changes: 2 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.2
// VERSION: 0.3.3
// 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.2"))
#define GY521_LIB_VERSION (F("0.3.3"))


#ifndef GY521_THROTTLE_TIME
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.2
// VERSION: 0.3.3
// PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor
// URL: https://github.com/RobTillaart/GY521
//
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ AD0 connected to VCC => 0x69

#### Set before read

// as = 0,1,2,3 ==> 2g 4g 8g 16g
- **bool setAccelSensitivity(uint8_t as)** as = 0, 1, 2, 3 ==> 2g 4g 8g 16g
- **uint8_t getAccelSensitivity()** returns 0, 1, 2, 3
- **bool setGyroSensitivity(uint8_t gs)** gs = 0,1,2,3 ==> 250, 500, 1000, 2000 degrees/second
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.2",
"version": "0.3.3",
"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.2
version=0.3.3
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Arduino library for GY521 angle measurement
Expand Down

0 comments on commit c0c6090

Please sign in to comment.