-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathMPU6050.ino
154 lines (135 loc) · 4.21 KB
/
MPU6050.ino
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
#include <Wire.h>
#define MPU6050_ADDRESS 0x68
#define SMPLRT_DIV 0
#define DLPF_CFG 4
#define ACC_ORIENTATION(X, Y, Z) {accADC[ROLL] = -X; accADC[PITCH] = -Y; accADC[YAW] = Z;}
#define GYRO_ORIENTATION(X, Y, Z) {gyroADC[ROLL] = Y; gyroADC[PITCH] = -X; gyroADC[YAW] = -Z;}
void i2cRead(uint8_t Address, uint8_t Register, uint8_t Nbytes, uint8_t* Data)
{
Wire.beginTransmission(Address);
Wire.write(Register);
Wire.endTransmission();
Wire.requestFrom(Address, Nbytes);
uint8_t index=0;
while (Wire.available())
Data[index++]=Wire.read();
}
void i2cWriteByte(uint8_t Address, uint8_t Register, uint8_t Data)
{
Wire.beginTransmission(Address);
Wire.write(Register);
Wire.write(Data);
Wire.endTransmission();
}
#define ADDRESS 0x03
int calibratingG = CALSTEPS;
int calibratingA = 0;
int16_t gyroZero[3] = {0,0,0};
int16_t accZero[3] = {0,0,0};
// ****************
// GYRO common part
// ****************
void GYRO_Common()
{
static int32_t g[3];
uint8_t axis, tilt=0;
if (calibratingG>0)
{
for (axis = 0; axis < 3; axis++)
{
// Reset g[axis] at start of calibration
if (calibratingG == CALSTEPS) g[axis]=0;
// Sum up 512 readings
g[axis] += gyroADC[axis];
// Clear global variables for next reading
gyroADC[axis]=0;
gyroZero[axis]=0;
if (calibratingG == 1)
{
if (g[axis]>=0) g[axis] += CALSTEPS/2;
else g[axis] -= CALSTEPS/2;
gyroZero[axis] = g[axis]/CALSTEPS;
}
}
calibratingG--;
}
for (axis = 0; axis < 3; axis++)
gyroADC[axis] = gyroADC[axis] - gyroZero[axis];
}
// ****************
// ACC common part
// ****************
void ACC_Common()
{
static int32_t a[3];
if (calibratingA>0)
{
for (uint8_t axis = 0; axis < 3; axis++)
{
// Reset a[axis] at start of calibration
if (calibratingA == CALSTEPS) a[axis]=0;
// Sum up 512 readings
a[axis] += accADC[axis];
// Clear global variables for next reading
accADC[axis]=0;
accZero[axis]=0;
}
// Calculate average, shift Z down by acc_1G and store values in EEPROM at end of calibration
if (calibratingA == 1)
{
if (a[0] >= 0) a[0] += CALSTEPS/2; else a[0] -= CALSTEPS/2;
if (a[1] >= 0) a[1] += CALSTEPS/2; else a[1] -= CALSTEPS/2;
if (a[2] >= 0) a[2] += CALSTEPS/2; else a[2] -= CALSTEPS/2;
accZero[0] = a[0]/CALSTEPS;
accZero[1] = a[1]/CALSTEPS;
accZero[2] = a[2]/CALSTEPS - ACCRESO;
Serial.print(" "); Serial.print(accZero[0]); Serial.println();
Serial.print(" "); Serial.print(accZero[1]); Serial.println();
Serial.print(" "); Serial.print(accZero[2]); Serial.println();
}
calibratingA--;
}
accADC[0] -= accZero[0];
accADC[1] -= accZero[1];
accADC[2] -= accZero[2];
}
void Gyro_getADC ()
{
uint8_t rawADC[6];
i2cRead(MPU6050_ADDRESS, 0x43,6,rawADC);
GYRO_ORIENTATION( ((rawADC[0]<<8) | rawADC[1]) ,
((rawADC[2]<<8) | rawADC[3]) ,
((rawADC[4]<<8) | rawADC[5]) );
GYRO_Common();
}
void ACC_getADC ()
{
uint8_t rawADC[6];
i2cRead(MPU6050_ADDRESS, 0x3B,6,rawADC);
ACC_ORIENTATION( ((rawADC[0]<<8) | rawADC[1]) ,
((rawADC[2]<<8) | rawADC[3]) ,
((rawADC[4]<<8) | rawADC[5]) );
ACC_Common();
}
void MPU6050_readId()
{
uint8_t id;
i2cRead(MPU6050_ADDRESS, 0x75, 1, &id);
if (id == 0x68) Serial.println("6050 ID OK");
else Serial.println("6050 ID Failed");
}
void MPU6050_init()
{
Wire.begin();
Wire.setClock(400000);
//Gyro_init
i2cWriteByte(MPU6050_ADDRESS, 0x6B, 0x80); //PWR_MGMT_1 -- DEVICE_RESET 1
delay(50);
i2cWriteByte(MPU6050_ADDRESS, 0x6B, 0x03); //PWR_MGMT_1 -- SLEEP 0; CYCLE 0; TEMP_DIS 0; CLKSEL 3 (PLL with Z Gyro reference)
i2cWriteByte(MPU6050_ADDRESS, 0x1A, DLPF_CFG); //CONFIG -- EXT_SYNC_SET 0 (disable input pin for data sync) ; default DLPF_CFG = 0 => ACC bandwidth = 260Hz GYRO bandwidth = 256Hz)
i2cWriteByte(MPU6050_ADDRESS, 0x1B, 0x18); //GYRO_CONFIG -- FS_SEL = 3: Full scale set to 2000 deg/sec
delay(50);
//ACC_init
i2cWriteByte(MPU6050_ADDRESS, 0x1C, 0x10); //ACCEL_CONFIG
delay(50);
}