Skip to content

Commit f12c704

Browse files
authored
Merge pull request #3 from RobTillaart/develop
Develop
2 parents f14d438 + a9ede00 commit f12c704

File tree

8 files changed

+196
-81
lines changed

8 files changed

+196
-81
lines changed

GY521.cpp

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
//
22
// FILE: GY521.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.1
4+
// VERSION: 0.1.2
55
// PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor
66
// URL: https://github.com/RobTillaart/GY521
77
//
88
// HISTORY:
99
// 0.1.0 2017-11-20 initial version
1010
// 0.1.1 2020-07-09 refactor + initial release
11+
// 0.1.2 2020-08-06 fix setAccelSensitivity + add getters
1112

12-
#include "Wire.h"
1313
#include "GY521.h"
1414

1515
// keep names in sync with BIG MPU6050 lib
@@ -32,8 +32,8 @@
3232
//
3333
GY521::GY521(uint8_t address)
3434
{
35-
_address = 0x69;
36-
if (address == 0x68) _address = 0x68;
35+
_address = address;
36+
setThrottleTime(GY521_THROTTLE_TIME);
3737
}
3838

3939
bool GY521::wakeup()
@@ -44,12 +44,14 @@ bool GY521::wakeup()
4444
return Wire.endTransmission() == 0;
4545
}
4646

47-
4847
int GY521::read()
4948
{
5049
if (_throttle)
5150
{
52-
if ((millis() - _lastTime) < GY521_THROTTLE_TIME) return GY521_THROTTLED;
51+
if ((millis() - _lastTime) < _throttleTime)
52+
{
53+
return GY521_THROTTLED;
54+
}
5355
}
5456

5557
// Connected ?
@@ -116,28 +118,48 @@ int GY521::read()
116118

117119
void GY521::setAccelSensitivity(uint8_t as)
118120
{
119-
if (as > 3) as = 3;
121+
_afs = as;
122+
if (_afs > 3) _afs = 3;
120123
uint8_t val = getRegister(GY521_ACCEL_CONFIG);
121-
val &= 0xE7;
122-
val |= (as << 3);
123-
setRegister(GY521_ACCEL_CONFIG, val);
124+
// no need to write same value
125+
if (((val >> 3) & 3) != _afs)
126+
{
127+
val &= 0xE7;
128+
val |= (_afs << 3);
129+
setRegister(GY521_ACCEL_CONFIG, val);
130+
}
124131
// calculate conversion factor.
125-
_raw2g = 16384.0;
126-
for (uint8_t i = 0; i < _afs; i++) _raw2g *= 0.5;
127-
_raw2g = 1.0 / _raw2g;
132+
_raw2g = (1 << _afs) / 16384.0;
133+
}
134+
135+
uint8_t GY521::getAccelSensitivity()
136+
{
137+
uint8_t val = getRegister(GY521_ACCEL_CONFIG);
138+
_afs = (val >> 3) & 3;
139+
return _afs;
128140
}
129141

130142
void GY521::setGyroSensitivity(uint8_t gs)
131143
{
132144
_gfs = gs;
133145
if (_gfs > 3) _gfs = 3;
134146
uint8_t val = getRegister(GY521_GYRO_CONFIG);
135-
val &= 0xE7;
136-
val |= (_gfs << 3);
137-
setRegister(GY521_GYRO_CONFIG, val);
138-
_raw2dps = 131.0;
139-
for (uint8_t i = 0; i < _gfs; i++) _raw2dps *= 0.5;
140-
_raw2dps = 1.0 / _raw2dps;
147+
// no need to write same value
148+
if (((val >> 3) & 3) != _gfs)
149+
{
150+
val &= 0xE7;
151+
val |= (_gfs << 3);
152+
setRegister(GY521_GYRO_CONFIG, val);
153+
}
154+
// calculate conversion factor.
155+
_raw2dps = (1 << _gfs) / 131.0;
156+
}
157+
158+
uint8_t GY521::getGyroSensitivity()
159+
{
160+
uint8_t val = getRegister(GY521_GYRO_CONFIG);
161+
_gfs = (val >> 3) & 3;
162+
return _gfs;
141163
}
142164

143165
uint8_t GY521::setRegister(uint8_t reg, uint8_t value)

GY521.h

Lines changed: 63 additions & 57 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.1.1
5+
// VERSION: 0.1.2
66
// PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor
77
// URL: https://github.com/RobTillaart/GY521
88
//
@@ -13,10 +13,10 @@
1313
#include "Arduino.h"
1414
#include "Wire.h"
1515

16-
#define GY521_LIB_VERSION (F("0.1.1"))
16+
#define GY521_LIB_VERSION (F("0.1.2"))
1717

1818
#ifndef GY521_THROTTLE_TIME
19-
#define GY521_THROTTLE_TIME 10 // milliseconds
19+
#define GY521_THROTTLE_TIME 10 // milliseconds
2020
#endif
2121

2222
// ERRORCODES getRaw()
@@ -29,62 +29,68 @@
2929
class GY521
3030
{
3131
public:
32-
GY521(uint8_t address = 0x69); // 0x68 or 0x69
33-
34-
bool wakeup();
35-
// throttle to force delay between reads.
36-
void setThrottle(bool throttle = true) { _throttle = throttle; };
37-
bool getThrottle() { return _throttle; };
38-
int read();
39-
40-
// as = 0,1,2,3 ==> 2g 4g 8g 16g
41-
void setAccelSensitivity(uint8_t as);
42-
float getAccelX() { return _ax; };
43-
float getAccelY() { return _ay; };
44-
float getAccelZ() { return _az; };
45-
float getAngleX() { return _aax; };
46-
float getAngleY() { return _aay; };
47-
float getAngleZ() { return _aaz; };
48-
49-
float getTemperature() { return _temperature; };
50-
51-
// gs = 0,1,2,3 ==> 250, 500, 1000, 2000 degrees/second
52-
void setGyroSensitivity(uint8_t gs);
53-
float getGyroX() { return _gx; };
54-
float getGyroY() { return _gy; };
55-
float getGyroZ() { return _gz; };
56-
57-
float getPitch() { return _pitch; };
58-
float getRoll() { return _roll; };
59-
float getYaw() { return _yaw; };
60-
61-
uint32_t lastTime() { return _lastTime; };
62-
63-
// generic worker to get access to all fucntionality
64-
uint8_t setRegister(uint8_t reg, uint8_t value);
65-
uint8_t getRegister(uint8_t reg);
66-
67-
// callibration errors
68-
float axe = 0, aye = 0, aze = 0; // accelerometer errors
69-
float gxe = 0, gye = 0, gze = 0; // gyro errors
32+
GY521(uint8_t address = 0x69); // 0x68 or 0x69
33+
34+
bool wakeup();
35+
// throttle to force delay between reads.
36+
void setThrottle(bool throttle = true) { _throttle = throttle; };
37+
bool getThrottle() { return _throttle; };
38+
// 0..65535 is roughly 1 minute.
39+
void setThrottleTime(uint16_t ti ) { _throttleTime = ti; };
40+
uint16_t getThrottleTime() { return _throttleTime; };
41+
int read();
42+
43+
// as = 0,1,2,3 ==> 2g 4g 8g 16g
44+
void setAccelSensitivity(uint8_t as);
45+
uint8_t getAccelSensitivity(); // returns 0,1,2,3
46+
float getAccelX() { return _ax; };
47+
float getAccelY() { return _ay; };
48+
float getAccelZ() { return _az; };
49+
float getAngleX() { return _aax; };
50+
float getAngleY() { return _aay; };
51+
float getAngleZ() { return _aaz; };
52+
53+
float getTemperature() { return _temperature; };
54+
55+
// gs = 0,1,2,3 ==> 250, 500, 1000, 2000 degrees/second
56+
void setGyroSensitivity(uint8_t gs);
57+
uint8_t getGyroSensitivity(); // returns 0,1,2,3
58+
float getGyroX() { return _gx; };
59+
float getGyroY() { return _gy; };
60+
float getGyroZ() { return _gz; };
61+
62+
float getPitch() { return _pitch; };
63+
float getRoll() { return _roll; };
64+
float getYaw() { return _yaw; };
65+
66+
uint32_t lastTime() { return _lastTime; };
67+
68+
// generic worker to get access to all functionality
69+
uint8_t setRegister(uint8_t reg, uint8_t value);
70+
uint8_t getRegister(uint8_t reg);
71+
72+
// callibration errors
73+
float axe = 0, aye = 0, aze = 0; // accelerometer errors
74+
float gxe = 0, gye = 0, gze = 0; // gyro errors
7075

7176
private:
72-
uint8_t _address; // I2C address
73-
bool _throttle = true; // to prevent reading too fast
74-
uint32_t _lastTime = 0; // to measure duration for math & throttle
75-
76-
uint8_t _afs = 0; // sensitivity factor
77-
float _raw2g = 1/16384.0; // raw data to gravity g's
78-
float _ax, _ay, _az; // accelerometer raw
79-
float _aax, _aay, _aaz; // accelerometer processed
80-
81-
uint8_t _gfs = 0;
82-
float _raw2dps = 1/131;
83-
float _gx, _gy, _gz; // gyro raw
84-
float _gax, _gay, _gaz; // gyro processed.
85-
float _pitch, _roll, _yaw; // used by user
86-
87-
float _temperature = 0;
77+
uint8_t _address; // I2C address
78+
bool _throttle = true; // to prevent reading too fast
79+
uint16_t _throttleTime = GY521_THROTTLE_TIME;
80+
uint32_t _lastTime = 0; // to measure duration for math & throttle
81+
82+
uint8_t _afs = 0; // sensitivity factor
83+
float _raw2g = 1.0/16384.0; // raw data to gravity g's
84+
float _ax, _ay, _az; // accelerometer raw
85+
float _aax, _aay, _aaz; // accelerometer processed
86+
87+
uint8_t _gfs = 0;
88+
float _raw2dps = 1.0/131.0;
89+
float _gx, _gy, _gz; // gyro raw
90+
float _gax, _gay, _gaz; // gyro processed.
91+
float _pitch, _roll, _yaw; // used by user
92+
93+
float _temperature = 0;
8894
};
8995

9096

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ Experimental library for GY521 a.k.a. MCU-6050
99
Library is work in progress, in fact extracted and extended from an old project.
1010
It needs to be tested a lot more.
1111

12-
It has two examples
12+
It has three examples
1313
- calibration example to determine the offsets needed
1414
- example to read values.
15+
- test sketch to test get / set values.
1516

1617
## Calibration (short version for now)
1718

examples/test1/test1.ino

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ void setup()
1919
Wire.begin();
2020

2121
delay(100);
22-
if (sensor.wakeup() == false)
22+
while (sensor.wakeup() == false)
2323
{
24-
Serial.println("Could not conect to GY521");
24+
Serial.print(millis());
25+
Serial.println("\tCould not connect to GY521");
26+
delay(1000);
2527
}
2628
sensor.setAccelSensitivity(0); // 2g
2729
sensor.setGyroSensitivity(0); // 250 degrees/s

examples/test_2/test_2.ino

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//
2+
// FILE: test_2.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.0
5+
// PURPOSE: test set/get
6+
// DATE: 2020-08-06
7+
8+
#include "GY521.h"
9+
10+
GY521 sensor(0x69);
11+
12+
void setup()
13+
{
14+
Serial.begin(115200);
15+
Serial.println(__FILE__);
16+
17+
Wire.begin();
18+
19+
delay(100);
20+
if (sensor.wakeup() == false)
21+
{
22+
Serial.println("Could not conect to GY521");
23+
}
24+
25+
Serial.println("ACCEL TEST");
26+
for (uint8_t n = 0; n < 4; n++)
27+
{
28+
sensor.setAccelSensitivity(n);
29+
uint8_t p = sensor.getAccelSensitivity();
30+
if (n != p)
31+
{
32+
Serial.print("\tAccel err:\t");
33+
Serial.print(n);
34+
Serial.print("\t");
35+
Serial.println(p);
36+
}
37+
}
38+
39+
Serial.println("GYRO TEST");
40+
for (uint8_t n = 0; n < 4; n++)
41+
{
42+
sensor.setGyroSensitivity(n);
43+
uint8_t p = sensor.getGyroSensitivity();
44+
if (n != p)
45+
{
46+
Serial.print("\tGyro err:\t");
47+
Serial.print(n);
48+
Serial.print("\t");
49+
Serial.println(p);
50+
}
51+
}
52+
53+
Serial.println("THROTTLE TEST");
54+
sensor.setThrottle();
55+
if (sensor.getThrottle() == false)
56+
{
57+
Serial.println("\tThrot:\ttrue");
58+
}
59+
sensor.setThrottle(false);
60+
if (sensor.getThrottle() == true)
61+
{
62+
Serial.println("\tThrot err:\tfalse");
63+
}
64+
65+
for (uint16_t n = 0; n < 1000; n++) // 0 - 1 second.
66+
{
67+
sensor.setThrottleTime(n);
68+
if (sensor.getThrottleTime() != n)
69+
{
70+
Serial.print("\tTT err:\t");
71+
Serial.println(n);
72+
}
73+
}
74+
75+
Serial.println("\nDone...");
76+
}
77+
78+
void loop()
79+
{
80+
}
81+
82+
// -- END OF FILE --

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ setThrottle KEYWORD2
1212
getThrottle KEYWORD2
1313

1414
setAccelSensitivity KEYWORD2
15+
getAccelSensitivity KEYWORD2
1516
getAccelX KEYWORD2
1617
getAccelY KEYWORD2
1718
getAccelZ KEYWORD2
@@ -21,6 +22,7 @@ getAngleZ KEYWORD2
2122
getTemperature KEYWORD2
2223

2324
setGyroSensitivity KEYWORD2
25+
getGyroSensitivity KEYWORD2
2426
getGyroX KEYWORD2
2527
getGyroY KEYWORD2
2628
getGyroZ KEYWORD2

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.1.1",
18+
"version":"0.1.2",
1919
"frameworks": "arduino",
2020
"platforms": "*"
2121
}

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.1.1
2+
version=0.1.2
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)