Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Jan 17, 2024
1 parent 2d45a16 commit 3120105
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 25 deletions.
12 changes: 9 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.5.2] - 2024-01-16
- fix #48, update example
- add some tables to readme.md
- minor edits
- fix #48, use float variables in example GY521_test_1.ino
- add **void calibrate(uint16_t times)** to API
- add **GY521_performance_calibrate.ino** example
- add **GY521_raw_cooked.ino** example
- make explicit that pitch roll yaw is work in progress.
- update readme.md
- calibrate section
- add some tables
- minor edits in examples


## [0.5.1] - 2023-12-11
Expand Down
41 changes: 41 additions & 0 deletions GY521.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,41 @@ void GY521::reset()
}


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;

for (uint16_t i = 0; i < times; i++)
{
read();
axe -= getAccelX();
aye -= getAccelY();
aze -= getAccelZ();
gxe -= getGyroX();
gye -= getGyroY();
gze -= getGyroZ();
}

// adjust calibration errors so table should get all zero's.
float factor = 1.0 / times;
axe *= factor;
aye *= factor;
aze *= factor;
gxe *= factor;
gye *= factor;
gze *= factor;

// restore throttle state
_throttle = oldThrottle;
}


bool GY521::wakeup()
{
_wire->beginTransmission(_address);
Expand Down Expand Up @@ -138,6 +173,7 @@ int16_t GY521::read()
float _ay2 = _ay * _ay;
float _az2 = _az * _az;

// calculate angle
_aax = atan( _ay / sqrt(_ax2 + _az2)) * GY521_RAD2DEGREES;
_aay = atan(-1.0 * _ax / sqrt(_ay2 + _az2)) * GY521_RAD2DEGREES;
_aaz = atan( _az / sqrt(_ax2 + _ay2)) * GY521_RAD2DEGREES;
Expand All @@ -159,10 +195,13 @@ int16_t GY521::read()
_gy += gye;
_gz += gze;

// integrate gyroscope data
_gax += _gx * duration;
_gay += _gy * duration;
_gaz += _gz * duration;

// experimental below this line.
// Pitch Roll Yaw are work in progress
// normalize
// _gax etc might loose precision after many iterations #36
if (_normalize)
Expand Down Expand Up @@ -250,6 +289,7 @@ int16_t GY521::readAccel()
float _ay2 = _ay * _ay;
float _az2 = _az * _az;

// calculate angles.
_aax = atan( _ay / sqrt(_ax2 + _az2)) * GY521_RAD2DEGREES;
_aay = atan(-1.0 * _ax / sqrt(_ay2 + _az2)) * GY521_RAD2DEGREES;
_aaz = atan( _az / sqrt(_ax2 + _ay2)) * GY521_RAD2DEGREES;
Expand Down Expand Up @@ -318,6 +358,7 @@ int16_t GY521::readGyro()
_gaz += _gz * duration;


// experimental below this line.
// normalize
// _gax etc might loose precision after many iterations #36
if (_normalize)
Expand Down
7 changes: 7 additions & 0 deletions GY521.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class GY521
bool isConnected();
void reset();

// EXPERIMENTAL
// calibrate needs to be called to compensate for errors.
// must be called after setAccelSensitivity(as); and setGyroSensitivity(gs);
void calibrate(uint16_t times);

bool wakeup();
// throttle to force delay between reads.
Expand Down Expand Up @@ -90,6 +94,9 @@ class GY521
float getGyroX() { return _gx; };
float getGyroY() { return _gy; };
float getGyroZ() { return _gz; };

// EXPERIMENTAL
// pitch, roll and yaw is work in progress.
float getPitch() { return _pitch; };
float getRoll() { return _roll; };
float getYaw() { return _yaw; };
Expand Down
51 changes: 44 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ Arduino library for I2C GY521 accelerometer-gyroscope sensor a.k.a. MCU-6050.

## Description

Experimental library for GY521 a.k.a. MCU-6050.
**Experimental** library for GY521 a.k.a. MCU-6050.

Library is work in progress, in fact it is extracted and extended from an old project.
It needs to be tested a lot more.

See changelog.md for latest updates.


#### 0.5.0 Breaking change

Version 0.5.0 introduced a breaking change.
Expand All @@ -37,10 +38,22 @@ before calling **begin()**.
- **GY521_angle** read angleX, angleY, angleZ.
- **GY521_performance** measure performance.
- **GY521_pitch_roll_yaw** to get pitch roll yaw.
- **GY521_readCalibration_1** read calibration values / errors for a flat sensor.
- **GY521_raw_cooked** make a table of raw measurements and derived data
for analysis e.g. in a spreadsheet.
- **GY521_readCalibration_1** read calibration values / errors for a "flat" sensor.
- **GY521_readCalibration_2** generate calibration code snippet.
- **GY521_test_1** test working of the sensor.
- **GY521_test_2** test set/get functions.
- **GY521_test_2** test set/get functions (sort of unit test).
- **GY521_two_sensors** demo for two sensors.


#### Related

- https://invensense.tdk.com/wp-content/uploads/2015/02/MPU-6000-Datasheet1.pdf
- https://cdn.sparkfun.com/datasheets/Sensors/Accelerometers/RM-MPU-6000A.pdf register map.
- https://github.com/RobTillaart/Angle
- https://github.com/RobTillaart/AngleConverter



## Breakout board
Expand All @@ -67,6 +80,11 @@ AD0 connected to VCC => 0x69

## Calibration (short version for now)

Since version 0.5.2 the library has a build in **void calibrate(times)** function which
can be called instead of manual copying.
This function overwrites the values of axe aye aze gxe gye gze.
**calibrate()** must be called after **setAccelSensitivity(as)** and **setGyroSensitivity(gs)**.

1. load and run calibration example
it shows a header containing 6 numbers and 10 lines of 8 numbers
1. wait until the middle 6 of the longer lines stabilize (should all be 0)
Expand All @@ -90,6 +108,15 @@ Note call **Wire.begin()** before **begin()**.
- **bool wakeUp()** idem.


### Calibrate

- **void calibrate(uint16_t times)** This function overwrites the values of axe aye aze gxe gye gze.
To get quality error numbers, the GY521 sensor should not move during calibration.
The parameter times determines the number of measurements made.
Typical values are 100 or more.
Please note this is a time consuming function.


### Throttle

- **void setThrottle(bool throttle = true)** throttle to force "delay" between reads.
Expand Down Expand Up @@ -146,6 +173,12 @@ One must explicitly call **read()** to get new values.
- **float getGyroX()** idem.
- **float getGyroY()** idem.
- **float getGyroZ()** idem.


#### Experimental Pitch Roll and Yaw

Pitch Roll and Yaw is work in progress and should not be used for projects yet.

- **float getPitch()** idem. May return any number.
If **setNormalize(true)** return value will be 0-359.999
- **float getRoll()** idem. May return any number.
Expand All @@ -162,7 +195,7 @@ Read the register PDF for the specific value and meaning of registers.
- **uint8_t getRegister(uint8_t reg)**


## documents
## Documents

- check details registers - MPU-6000-Register-Map1.pdf

Expand Down Expand Up @@ -204,22 +237,27 @@ unit dps = degrees per second.

## Operation

See examples, use with care
See examples, use with care.


## Future

#### Must

- time
- improve documentation
- investigate Pitch Roll and Yaw math in detail.
- investigate math needed.
- implementation.
- when?
- test test and test ...(ESP too)

#### Should

- test **calibrate()** function for different sensitivities.

#### Could

- calibrate sketch could print code snippet to include...
- add examples
- improve unit tests?
- reorder code in read(), would that save some micros.?
Expand All @@ -228,7 +266,6 @@ See examples, use with care
- make enum for sensitivity Accel?
- make enum for sensitivity Gyro?


#### Wont

- look for maths optimizations (atan, hypot, performance)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//
// FILE: GY521_performance_calibrate.ino
// AUTHOR: Rob Tillaart
// PURPOSE: read angleX, angleY, angleZ
// URL: https://github.com/RobTillaart/GY521


#include "GY521.h"

GY521 sensor(0x68);


void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println(__FILE__);
Serial.print("GY521_LIB_VERSION: ");
Serial.println(GY521_LIB_VERSION);

Wire.begin();

delay(100);
// while (sensor.wakeup() == false)
{
Serial.print(millis());
Serial.println("\tCould not connect to GY521: please check the GY521 address (0x68/0x69)");
delay(1000);
}
sensor.setAccelSensitivity(0); // 2g
sensor.setGyroSensitivity(0); // 250 degrees/s
sensor.setThrottle(false);

test(100);
}


void loop()
{
Serial.println("\n\tTIME\tACCELEROMETER\t\tGYROSCOPE");
for (uint16_t times = 20; times <= 500; times += 20)
{
test(times);
}
}



void test(uint16_t times)
{
// flush all output
delay(100);
uint32_t start = micros();
sensor.calibrate(100);
uint32_t duration = micros() - start;

// print results
Serial.print(times);
Serial.print('\t');
Serial.print(duration);
Serial.print('\t');
Serial.print(sensor.axe, 3);
Serial.print('\t');
Serial.print(sensor.aye, 3);
Serial.print('\t');
Serial.print(sensor.aze, 3);
Serial.print('\t');
Serial.print(sensor.gxe, 3);
Serial.print('\t');
Serial.print(sensor.gye, 3);
Serial.print('\t');
Serial.print(sensor.gze, 3);
Serial.print('\n');
}


// -- END OF FILE --
20 changes: 13 additions & 7 deletions examples/GY521_pitch_roll_yaw/GY521_pitch_roll_yaw.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ void setup()
Serial.println(__FILE__);
Serial.print("GY521_LIB_VERSION: ");
Serial.println(GY521_LIB_VERSION);
Serial.println("warning: Pitch roll yaw is work in progress.");

Wire.begin();

Expand All @@ -29,19 +30,24 @@ void setup()
Serial.println("\tCould not connect to GY521: please check the GY521 address (0x68/0x69)");
delay(1000);
}

sensor.setAccelSensitivity(2); // 8g
sensor.setGyroSensitivity(1); // 500 degrees/s

sensor.setThrottle();
Serial.println("start...");

// uint32_t startCalibrate = micros();
sensor.calibrate(100);
// Serial.print("Duration: ");
// Serial.println(micros() - startCalibrate);

// set calibration values from calibration sketch.
sensor.axe = 0.574;
sensor.aye = -0.002;
sensor.aze = -1.043;
sensor.gxe = 10.702;
sensor.gye = -6.436;
sensor.gze = -0.676;
// sensor.axe = 0.574;
// sensor.aye = -0.002;
// sensor.aze = -1.043;
// sensor.gxe = 10.702;
// sensor.gye = -6.436;
// sensor.gze = -0.676;
}


Expand Down
Loading

0 comments on commit 3120105

Please sign in to comment.