-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathGY521.h
163 lines (122 loc) · 4.94 KB
/
GY521.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#pragma once
//
// FILE: GY521.h
// AUTHOR: Rob Tillaart
// VERSION: 0.6.1
// PURPOSE: Arduino library for I2C GY521 accelerometer-gyroscope sensor
// URL: https://github.com/RobTillaart/GY521
#include "Arduino.h"
#include "Wire.h"
#define GY521_LIB_VERSION (F("0.6.1"))
const float GRAVITY = 9.80655;
// THROTTLE TIMING
#ifndef GY521_THROTTLE_TIME
#define GY521_THROTTLE_TIME 10 // milliseconds
#endif
// ERROR CODES
#define GY521_OK 0
#define GY521_THROTTLED 1
#define GY521_ERROR_READ -1
#define GY521_ERROR_WRITE -2
#define GY521_ERROR_NOT_CONNECTED -3
#define GY521_ERROR_PARAMETER -4
// CONVERSION CONSTANTS
#define GY521_RAD2DEGREES (180.0 / PI)
#define GY521_DEGREES2RAD (PI / 180.0)
#define GY521_RAW2DPS (1.0 / 131.0)
#define GY521_RAW2G (1.0 / 16384.0)
class GY521
{
public:
// address == 0x68 or 0x69
GY521(uint8_t address = 0x69, TwoWire *wire = &Wire);
bool begin();
bool isConnected();
uint8_t getAddress();
void reset();
// EXPERIMENTAL
// calibrate needs to be called to compensate for errors.
// must be called after setAccelSensitivity(as); and setGyroSensitivity(gs);
bool calibrate(uint16_t times, float angleX = 0, float angleY = 0, bool inverted = false);
bool wakeup();
// throttle to force delay between reads.
void setThrottle(bool throttle = true) { _throttle = throttle; };
bool getThrottle() { return _throttle; };
// 0..65535 max milliseconds == roughly 1 minute.
void setThrottleTime(uint16_t ti ) { _throttleTime = ti; };
uint16_t getThrottleTime() { return _throttleTime; };
// SET BEFORE READ
// as = 0,1,2,3 ==> 2g 4g 8g 16g
bool setAccelSensitivity(uint8_t as);
uint8_t getAccelSensitivity(); // returns 0,1,2,3
// gs = 0,1,2,3 ==> 250, 500, 1000, 2000 degrees/second
bool setGyroSensitivity(uint8_t gs);
uint8_t getGyroSensitivity(); // returns 0,1,2,3
// normalizes Pitch Roll and Yaw.
void setNormalize(bool normalize = true) { _normalize = normalize; };
bool getNormalize() { return _normalize; };
// READ THE SENSOR
// returns GY521_OK or one of the error codes above.
int16_t read();
// optimized partial reading
// read accelerometer only
int16_t readAccel();
// read gyroscope only can be done too
// however for pitch roll yaw you need all.
int16_t readGyro();
// read temperature only, does not affect throttle.
int16_t readTemperature();
// CALL AFTER READ
float getAccelX() { return _ax; };
float getAccelY() { return _ay; };
float getAccelZ() { return _az; };
float getAngleX() { return _aax; };
float getAngleY() { return _aay; };
float getAngleZ() { return _aaz; };
float getTemperature() { return _temperature; };
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; };
// last time sensor is actually read.
uint32_t lastTime() { return _lastTime; };
// CONFIGURATION
// Digital Low Pass Filter - datasheet P13-reg26
bool setDLPFMode(uint8_t mode); // returns false if mode > 6
uint8_t getDLPFMode();
// generic worker to get access to all functionality
uint8_t setRegister(uint8_t reg, uint8_t value);
uint8_t getRegister(uint8_t reg);
// get last error and reset error to OK.
int16_t getError() { return _error; _error = GY521_OK; };
// calibration errors
float axe = 0, aye = 0, aze = 0; // accelerometer errors
float gxe = 0, gye = 0, gze = 0; // gyro errors
private:
uint8_t _address; // I2C address
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
float _raw2g = GY521_RAW2G; // raw data to gravity g's
float _ax, _ay, _az; // accelerometer raw
float _aax, _aay, _aaz; // accelerometer processed
uint8_t _gfs = 0;
float _raw2dps = GY521_RAW2DPS;
float _gx, _gy, _gz; // gyro raw
float _gax, _gay, _gaz; // gyro processed
float _pitch, _roll, _yaw; // used by user
float _temperature = 0;
bool _normalize = true; // default true.
int16_t _readRaw();
// to read register of 2 bytes.
int16_t _WireRead2();
TwoWire* _wire;
};
// -- END OF FILE --