-
Notifications
You must be signed in to change notification settings - Fork 5
/
MPU6050.h
256 lines (219 loc) · 7.42 KB
/
MPU6050.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
MPU6050.h - Header file for the MPU6050 Triple Axis Gyroscope & Accelerometer Arduino Library.
Version: 1.0.3
(c) 2014-2015 Korneliusz Jarzebski
www.jarzebski.pl
This program is free software: you can redistribute it and/or modify
it under the terms of the version 3 GNU General Public License as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MPU6050_h
#define MPU6050_h
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define MPU6050_ADDRESS (0x68) // 0x69 when AD0 pin to Vcc
#define MPU6050_REG_ACCEL_XOFFS_H (0x06)
#define MPU6050_REG_ACCEL_XOFFS_L (0x07)
#define MPU6050_REG_ACCEL_YOFFS_H (0x08)
#define MPU6050_REG_ACCEL_YOFFS_L (0x09)
#define MPU6050_REG_ACCEL_ZOFFS_H (0x0A)
#define MPU6050_REG_ACCEL_ZOFFS_L (0x0B)
#define MPU6050_REG_GYRO_XOFFS_H (0x13)
#define MPU6050_REG_GYRO_XOFFS_L (0x14)
#define MPU6050_REG_GYRO_YOFFS_H (0x15)
#define MPU6050_REG_GYRO_YOFFS_L (0x16)
#define MPU6050_REG_GYRO_ZOFFS_H (0x17)
#define MPU6050_REG_GYRO_ZOFFS_L (0x18)
#define MPU6050_REG_CONFIG (0x1A)
#define MPU6050_REG_GYRO_CONFIG (0x1B) // Gyroscope Configuration
#define MPU6050_REG_ACCEL_CONFIG (0x1C) // Accelerometer Configuration
#define MPU6050_REG_FF_THRESHOLD (0x1D)
#define MPU6050_REG_FF_DURATION (0x1E)
#define MPU6050_REG_MOT_THRESHOLD (0x1F)
#define MPU6050_REG_MOT_DURATION (0x20)
#define MPU6050_REG_ZMOT_THRESHOLD (0x21)
#define MPU6050_REG_ZMOT_DURATION (0x22)
#define MPU6050_REG_INT_PIN_CFG (0x37) // INT Pin. Bypass Enable Configuration
#define MPU6050_REG_INT_ENABLE (0x38) // INT Enable
#define MPU6050_REG_INT_STATUS (0x3A)
#define MPU6050_REG_ACCEL_XOUT_H (0x3B)
#define MPU6050_REG_ACCEL_XOUT_L (0x3C)
#define MPU6050_REG_ACCEL_YOUT_H (0x3D)
#define MPU6050_REG_ACCEL_YOUT_L (0x3E)
#define MPU6050_REG_ACCEL_ZOUT_H (0x3F)
#define MPU6050_REG_ACCEL_ZOUT_L (0x40)
#define MPU6050_REG_TEMP_OUT_H (0x41)
#define MPU6050_REG_TEMP_OUT_L (0x42)
#define MPU6050_REG_GYRO_XOUT_H (0x43)
#define MPU6050_REG_GYRO_XOUT_L (0x44)
#define MPU6050_REG_GYRO_YOUT_H (0x45)
#define MPU6050_REG_GYRO_YOUT_L (0x46)
#define MPU6050_REG_GYRO_ZOUT_H (0x47)
#define MPU6050_REG_GYRO_ZOUT_L (0x48)
#define MPU6050_REG_MOT_DETECT_STATUS (0x61)
#define MPU6050_REG_MOT_DETECT_CTRL (0x69)
#define MPU6050_REG_USER_CTRL (0x6A) // User Control
#define MPU6050_REG_PWR_MGMT_1 (0x6B) // Power Management 1
#define MPU6050_REG_WHO_AM_I (0x75) // Who Am I
#ifndef VECTOR_STRUCT_H
#define VECTOR_STRUCT_H
struct Vector
{
float XAxis;
float YAxis;
float ZAxis;
};
#endif
struct Activites
{
bool isOverflow;
bool isFreeFall;
bool isInactivity;
bool isActivity;
bool isPosActivityOnX;
bool isPosActivityOnY;
bool isPosActivityOnZ;
bool isNegActivityOnX;
bool isNegActivityOnY;
bool isNegActivityOnZ;
bool isDataReady;
};
typedef enum
{
MPU6050_CLOCK_KEEP_RESET = 0b111,
MPU6050_CLOCK_EXTERNAL_19MHZ = 0b101,
MPU6050_CLOCK_EXTERNAL_32KHZ = 0b100,
MPU6050_CLOCK_PLL_ZGYRO = 0b011,
MPU6050_CLOCK_PLL_YGYRO = 0b010,
MPU6050_CLOCK_PLL_XGYRO = 0b001,
MPU6050_CLOCK_INTERNAL_8MHZ = 0b000
} mpu6050_clockSource_t;
typedef enum
{
MPU6050_SCALE_2000DPS = 0b11,
MPU6050_SCALE_1000DPS = 0b10,
MPU6050_SCALE_500DPS = 0b01,
MPU6050_SCALE_250DPS = 0b00
} mpu6050_dps_t;
typedef enum
{
MPU6050_RANGE_16G = 0b11,
MPU6050_RANGE_8G = 0b10,
MPU6050_RANGE_4G = 0b01,
MPU6050_RANGE_2G = 0b00,
} mpu6050_range_t;
typedef enum
{
MPU6050_DELAY_3MS = 0b11,
MPU6050_DELAY_2MS = 0b10,
MPU6050_DELAY_1MS = 0b01,
MPU6050_NO_DELAY = 0b00,
} mpu6050_onDelay_t;
typedef enum
{
MPU6050_DHPF_HOLD = 0b111,
MPU6050_DHPF_0_63HZ = 0b100,
MPU6050_DHPF_1_25HZ = 0b011,
MPU6050_DHPF_2_5HZ = 0b010,
MPU6050_DHPF_5HZ = 0b001,
MPU6050_DHPF_RESET = 0b000,
} mpu6050_dhpf_t;
typedef enum
{
MPU6050_DLPF_6 = 0b110,
MPU6050_DLPF_5 = 0b101,
MPU6050_DLPF_4 = 0b100,
MPU6050_DLPF_3 = 0b011,
MPU6050_DLPF_2 = 0b010,
MPU6050_DLPF_1 = 0b001,
MPU6050_DLPF_0 = 0b000,
} mpu6050_dlpf_t;
class MPU6050
{
public:
bool begin(mpu6050_dps_t scale = MPU6050_SCALE_2000DPS, mpu6050_range_t range = MPU6050_RANGE_2G, int mpua = MPU6050_ADDRESS, int SDA = -1, int SCL = -1);
void setClockSource(mpu6050_clockSource_t source);
void setScale(mpu6050_dps_t scale);
void setRange(mpu6050_range_t range);
mpu6050_clockSource_t getClockSource(void);
mpu6050_dps_t getScale(void);
mpu6050_range_t getRange(void);
void setDHPFMode(mpu6050_dhpf_t dhpf);
void setDLPFMode(mpu6050_dlpf_t dlpf);
mpu6050_onDelay_t getAccelPowerOnDelay();
void setAccelPowerOnDelay(mpu6050_onDelay_t delay);
uint8_t getIntStatus(void);
bool getIntZeroMotionEnabled(void);
void setIntZeroMotionEnabled(bool state);
bool getIntMotionEnabled(void);
void setIntMotionEnabled(bool state);
bool getIntFreeFallEnabled(void);
void setIntFreeFallEnabled(bool state);
uint8_t getMotionDetectionThreshold(void);
void setMotionDetectionThreshold(uint8_t threshold);
uint8_t getMotionDetectionDuration(void);
void setMotionDetectionDuration(uint8_t duration);
uint8_t getZeroMotionDetectionThreshold(void);
void setZeroMotionDetectionThreshold(uint8_t threshold);
uint8_t getZeroMotionDetectionDuration(void);
void setZeroMotionDetectionDuration(uint8_t duration);
uint8_t getFreeFallDetectionThreshold(void);
void setFreeFallDetectionThreshold(uint8_t threshold);
uint8_t getFreeFallDetectionDuration(void);
void setFreeFallDetectionDuration(uint8_t duration);
bool getSleepEnabled(void);
void setSleepEnabled(bool state);
bool getI2CMasterModeEnabled(void);
void setI2CMasterModeEnabled(bool state);
bool getI2CBypassEnabled(void);
void setI2CBypassEnabled(bool state);
float readTemperature(void);
Activites readActivites(void);
int16_t getGyroOffsetX(void);
void setGyroOffsetX(int16_t offset);
int16_t getGyroOffsetY(void);
void setGyroOffsetY(int16_t offset);
int16_t getGyroOffsetZ(void);
void setGyroOffsetZ(int16_t offset);
int16_t getAccelOffsetX(void);
void setAccelOffsetX(int16_t offset);
int16_t getAccelOffsetY(void);
void setAccelOffsetY(int16_t offset);
int16_t getAccelOffsetZ(void);
void setAccelOffsetZ(int16_t offset);
void calibrateGyro(uint8_t samples = 50);
void setThreshold(uint8_t multiple = 1);
uint8_t getThreshold(void);
Vector readRawGyro(void);
Vector readNormalizeGyro(void);
Vector readRawAccel(void);
Vector readNormalizeAccel(void);
Vector readScaledAccel(void);
private:
Vector ra, rg; // Raw vectors
Vector na, ng; // Normalized vectors
Vector tg, dg; // Threshold and Delta for Gyro
Vector th; // Threshold
Activites a; // Activities
float dpsPerDigit, rangePerDigit;
float actualThreshold;
bool useCalibrate;
int mpuAddress;
uint8_t fastRegister8(uint8_t reg);
uint8_t readRegister8(uint8_t reg);
void writeRegister8(uint8_t reg, uint8_t value);
int16_t readRegister16(uint8_t reg);
void writeRegister16(uint8_t reg, int16_t value);
bool readRegisterBit(uint8_t reg, uint8_t pos);
void writeRegisterBit(uint8_t reg, uint8_t pos, bool state);
};
#endif