|
2 | 2 | // |
3 | 3 | // FILE: GY521.h |
4 | 4 | // AUTHOR: Rob Tillaart |
5 | | -// VERSION: 0.1.1 |
| 5 | +// VERSION: 0.1.2 |
6 | 6 | // PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor |
7 | 7 | // URL: https://github.com/RobTillaart/GY521 |
8 | 8 | // |
|
13 | 13 | #include "Arduino.h" |
14 | 14 | #include "Wire.h" |
15 | 15 |
|
16 | | -#define GY521_LIB_VERSION (F("0.1.1")) |
| 16 | +#define GY521_LIB_VERSION (F("0.1.2")) |
17 | 17 |
|
18 | 18 | #ifndef GY521_THROTTLE_TIME |
19 | | -#define GY521_THROTTLE_TIME 10 // milliseconds |
| 19 | +#define GY521_THROTTLE_TIME 10 // milliseconds |
20 | 20 | #endif |
21 | 21 |
|
22 | 22 | // ERRORCODES getRaw() |
|
29 | 29 | class GY521 |
30 | 30 | { |
31 | 31 | 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 |
70 | 75 |
|
71 | 76 | 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; |
88 | 94 | }; |
89 | 95 |
|
90 | 96 |
|
|
0 commit comments