forked from PrinceBot-Ratthanin/NKP_V2-Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPU6050_tockn.cpp
139 lines (116 loc) · 3.59 KB
/
MPU6050_tockn.cpp
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
#include "MPU6050_tockn.h"
#include "Arduino.h"
MPU6050::MPU6050(TwoWire &w){
wire = &w;
accCoef = 0.02f;
gyroCoef = 0.98f;
}
MPU6050::MPU6050(TwoWire &w, float aC, float gC){
wire = &w;
accCoef = aC;
gyroCoef = gC;
}
void MPU6050::begin(){
writeMPU6050(MPU6050_SMPLRT_DIV, 0x00);
writeMPU6050(MPU6050_CONFIG, 0x00);
writeMPU6050(MPU6050_GYRO_CONFIG, 0x08);
writeMPU6050(MPU6050_ACCEL_CONFIG, 0x00);
writeMPU6050(MPU6050_PWR_MGMT_1, 0x01);
this->update();
angleGyroX = 0;
angleGyroY = 0;
angleX = this->getAccAngleX();
angleY = this->getAccAngleY();
preInterval = millis();
}
void MPU6050::writeMPU6050(byte reg, byte data){
wire->beginTransmission(MPU6050_ADDR);
wire->write(reg);
wire->write(data);
wire->endTransmission();
}
byte MPU6050::readMPU6050(byte reg) {
wire->beginTransmission(MPU6050_ADDR);
wire->write(reg);
wire->endTransmission(true);
wire->requestFrom(MPU6050_ADDR, 1);
byte data = wire->read();
return data;
}
void MPU6050::setGyroOffsets(float x, float y, float z){
gyroXoffset = x;
gyroYoffset = y;
gyroZoffset = z;
}
void MPU6050::calcGyroOffsets(bool console, uint16_t delayBefore, uint16_t delayAfter){
float x = 0, y = 0, z = 0;
int16_t rx, ry, rz;
delay(delayBefore);
if(console){
Serial.println();
Serial.println("========================================");
Serial.println("Calculating gyro offsets");
Serial.print("DO NOT MOVE MPU6050");
}
for(int i = 0; i < 3000; i++){
if(console && i % 1000 == 0){
Serial.print(".");
}
wire->beginTransmission(MPU6050_ADDR);
wire->write(0x43);
wire->endTransmission(false);
wire->requestFrom((int)MPU6050_ADDR, 6);
rx = wire->read() << 8 | wire->read();
ry = wire->read() << 8 | wire->read();
rz = wire->read() << 8 | wire->read();
x += ((float)rx) / 65.5;
y += ((float)ry) / 65.5;
z += ((float)rz) / 65.5;
}
gyroXoffset = x / 3000;
gyroYoffset = y / 3000;
gyroZoffset = z / 3000;
if(console){
Serial.println();
Serial.println("Done!");
Serial.print("X : ");Serial.println(gyroXoffset);
Serial.print("Y : ");Serial.println(gyroYoffset);
Serial.print("Z : ");Serial.println(gyroZoffset);
Serial.println("Program will start after 3 seconds");
Serial.print("========================================");
delay(delayAfter);
}
}
void MPU6050::update(){
wire->beginTransmission(MPU6050_ADDR);
wire->write(0x3B);
wire->endTransmission(false);
wire->requestFrom((int)MPU6050_ADDR, 14);
rawAccX = wire->read() << 8 | wire->read();
rawAccY = wire->read() << 8 | wire->read();
rawAccZ = wire->read() << 8 | wire->read();
rawTemp = wire->read() << 8 | wire->read();
rawGyroX = wire->read() << 8 | wire->read();
rawGyroY = wire->read() << 8 | wire->read();
rawGyroZ = wire->read() << 8 | wire->read();
temp = (rawTemp + 12412.0) / 340.0;
accX = ((float)rawAccX) / 16384.0;
accY = ((float)rawAccY) / 16384.0;
accZ = ((float)rawAccZ) / 16384.0;
angleAccX = atan2(accY, accZ + abs(accX)) * 360 / 2.0 / PI;
angleAccY = atan2(accX, accZ + abs(accY)) * 360 / -2.0 / PI;
gyroX = ((float)rawGyroX) / 65.5;
gyroY = ((float)rawGyroY) / 65.5;
gyroZ = ((float)rawGyroZ) / 65.5;
gyroX -= gyroXoffset;
gyroY -= gyroYoffset;
gyroZ -= gyroZoffset;
interval = (millis() - preInterval) * 0.001;
angleGyroX += gyroX * interval;
angleGyroY += gyroY * interval;
angleGyroZ += gyroZ * interval;
angleX = (gyroCoef * (angleX + gyroX * interval)) + (accCoef * angleAccX);
angleY = (gyroCoef * (angleY + gyroY * interval)) + (accCoef * angleAccY);
angleZ = angleGyroZ;
preInterval = millis();
}