-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPU
112 lines (97 loc) · 3.14 KB
/
MPU
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
// This code gives you the 3 accelerations and 3 gyroscope values from the MPU sensor. The temp data is not retrieved.
#include <Wire.h>
const int MPU = 0x68;
int16_t rawAcX, rawAcY, rawAcZ, rawGyX, rawGyY, rawGyZ;
float biasX = 0.0, biasY = 0.0, biasZ = 0.0;
float calibratedAcX, calibratedAcY, calibratedAcZ, calibratedGyX, calibratedGyY, calibratedGyZ;
float accelerationX, accelerationY, accelerationZ;
float gyroX, gyroY, gyroZ;
void setup() {
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(9600);
calibrateSensors();
}
void loop() {
readSensors();
calibrateValues();
scaleAndConvert();
displayData();
delay(500);
}
void calibrateSensors() {
int numCalibrationSamples = 1000; // Adjust this based on desired calibration accuracy
for (int i = 0; i < numCalibrationSamples; i++) {
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU, 14, true);
rawAcX = Wire.read() << 8 | Wire.read();
rawAcY = Wire.read() << 8 | Wire.read();
rawAcZ = Wire.read() << 8 | Wire.read();
Wire.read(); // Skip temperature
Wire.read(); // Skip temperature
rawGyX = Wire.read() << 8 | Wire.read();
rawGyY = Wire.read() << 8 | Wire.read();
rawGyZ = Wire.read() << 8 | Wire.read();
biasX += rawAcX;
biasY += rawAcY;
biasZ += rawAcZ;
delay(10); // Adjust delay if necessary for stability
}
biasX /= numCalibrationSamples;
biasY /= numCalibrationSamples;
biasZ /= numCalibrationSamples;
}
void readSensors() {
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU, 14, true);
rawAcX = Wire.read() << 8 | Wire.read();
rawAcY = Wire.read() << 8 | Wire.read();
rawAcZ = Wire.read() << 8 | Wire.read();
Wire.read(); // Skip temperature
Wire.read(); // Skip temperature
rawGyX = Wire.read() << 8 | Wire.read();
rawGyY = Wire.read() << 8 | Wire.read();
rawGyZ = Wire.read() << 8 | Wire.read();
}
void calibrateValues() {
calibratedAcX = rawAcX - biasX;
calibratedAcY = rawAcY - biasY;
calibratedAcZ = rawAcZ - biasZ;
calibratedGyX = rawGyX;
calibratedGyY = rawGyY;
calibratedGyZ = rawGyZ;
}
void scaleAndConvert() {
float accelSensitivity = 2.0 / 32767.0; // Assuming ±2g full-scale range
float gyroSensitivity = 250.0 / 32767.0; // Assuming ±250°/s full-scale range
accelerationX = calibratedAcX * accelSensitivity * 9.81;
accelerationY = calibratedAcY * accelSensitivity * 9.81;
accelerationZ = calibratedAcZ * accelSensitivity * 9.81;
gyroX = calibratedGyX * gyroSensitivity;
gyroY = calibratedGyY * gyroSensitivity;
gyroZ = calibratedGyZ * gyroSensitivity;
}
void displayData() {
// Serial.print("Acceleration (m/s²): ");
Serial.print("Xa = ");
Serial.print(accelerationX);
Serial.print(" | Ya = ");
Serial.print(accelerationY);
Serial.print(" | Za = ");
Serial.println(accelerationZ);
//Serial.print("Gyroscope (°/s): ");
Serial.print("Xg = ");
Serial.print(gyroX);
Serial.print(" | Yg = ");
Serial.print(gyroY);
Serial.print(" | Zg = ");
Serial.println(gyroZ);
Serial.println();
}