-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtestingMPU.ino
212 lines (192 loc) · 5.38 KB
/
testingMPU.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
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
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"
#include "PID_v1.h"
// Uncomment to provide human readable output
//#define HUMAN_READABLE_OUTPUT
// Uncomment to provide parsable output in CSV format
#define PARSABLE_OUTPUT
// Uncomment to provide debug ouput
//#define DEBUG_OUTPUT
// Use arrays to simplify the code
const int arraySize = 6;
const int avgArraySize = 15;
// PID uses doubles, MPU commands are int16_ts
int16_t receivedData[arraySize] = { 0 };
double dreceivedData[arraySize] = { 0 };
int16_t offsets[arraySize] = { 0 };
double doffsets[arraySize] = { 0 };
// Store values in a circular buffer to calculate averages
int avgIndexCount[arraySize] = { 0 };
double offsetArray[arraySize][avgArraySize] = { 0 };
double readValueArray[arraySize][avgArraySize] = { 0 };
// Set point for XY axis is 0 if perfectly level
double XYAccSetpoint = 0;
// Set point for Z axis is 16384(gravity)
double ZAccSetpoint = 16384;
// Set point for gyros are 0
double gyroSetpoint = 0;
// Use array of setpoints to ease the setup process
double dsetpoint[arraySize] = { XYAccSetpoint, XYAccSetpoint, ZAccSetpoint,
gyroSetpoint, gyroSetpoint, gyroSetpoint };
// Set tunings
double kp = 0.03125;
double ki = 0.25;
double kd = 0;
// Array of pointers to PIDs used throughout
PID tuningController[arraySize];
// MPU to calibrate, using int0 for interrupts
MPU6050 mpu;
// Forward declarations
void switchAndUpdateOffsets(const size_t index);
double avgOffsetFromIndex(const size_t index);
double avgValueFromIndex(const size_t index);
void printCalibrationDataToSerial();
void setup()
{
// Enable serial
Serial.begin(115200);
// Setup the PID controllers
for (size_t i = 0; i < arraySize; ++i)
{
tuningController[i].setup(&(dreceivedData[i]), &(doffsets[i]),
&(dsetpoint[i]), kp, ki, kd, DIRECT);
tuningController[i].SetOutputLimits(-15000, 15000);
tuningController[i].SetMode(AUTOMATIC);
}
// Start the I2C bus
Wire.begin();
// Start the MPU
#ifdef DEBUG_OUTPUT
Serial.println(F("Initializing MPU"));
#endif // DEBUG_OUTPUT
mpu.initialize();
// Wait for a bit while initialising
delay(10);
// Check if connection is up
if (!mpu.testConnection())
{
#ifdef DEBUG_OUTPUT
Serial.println(F("MPU connection failed, reset it!"));
#endif // DEBUG_OUTPUT
}
}
void loop()
{
static unsigned long lastSerialUpdate = 0;
static const unsigned long deltaTSerialUpdate = 200;
// Get data from MPU
// [0]: xAccelerometer
// [1]: yAccelerometer
// [2]: zAccelerometer
// [3]: xGyro
// [4]: yGyro
// [5]: zGyro
mpu.getMotion6(&receivedData[0], &receivedData[1], &receivedData[2],
&receivedData[3], &receivedData[4], &receivedData[5]);
// Convert to doubles for PIDs
for (size_t i = 0; i < arraySize; ++i)
{
dreceivedData[i] = static_cast<double>(receivedData[i]);
}
// Check and update PIDs
for (size_t i = 0; i < arraySize; ++i)
{
if (tuningController[i].Compute())
{
switchAndUpdateOffsets(i);
// Store the most recently read value and new offset to averaging arrays
offsetArray[i][avgIndexCount[i]] = doffsets[i];
readValueArray[i][avgIndexCount[i]] = dreceivedData[i];
avgIndexCount[i]++;
if (avgIndexCount[i] >= avgArraySize)
{
avgIndexCount[i] = 0;
}
}
}
// Check if time to send update of average values over serial
if ((millis() - lastSerialUpdate) > deltaTSerialUpdate)
{
lastSerialUpdate = millis();
printCalibrationDataToSerial();
}
}
void switchAndUpdateOffsets(const size_t index)
{
offsets[index] = static_cast<int16_t>(doffsets[index]);
switch (index)
{
case 0:
mpu.setXAccelOffset(offsets[index]);
break;
case 1:
mpu.setYAccelOffset(offsets[index]);
break;
case 2:
mpu.setZAccelOffset(offsets[index]);
break;
case 3:
mpu.setXGyroOffset(offsets[index]);
break;
case 4:
mpu.setYGyroOffset(offsets[index]);
break;
case 5:
mpu.setZGyroOffset(offsets[index]);
break;
default:
break;
}
}
double avgOffsetFromIndex(const size_t index)
{
double sum = 0;
for (size_t i = 0; i < avgArraySize; ++i)
{
sum += offsetArray[index][i];
}
return (sum / static_cast<double>(avgArraySize));
}
double avgValueFromIndex(const size_t index)
{
double sum = 0;
for (size_t i = 0; i < avgArraySize; ++i)
{
sum += readValueArray[index][i];
}
return (sum / static_cast<double>(avgArraySize));
}
void printCalibrationDataToSerial()
{
#ifdef HUMAN_READABLE_OUTPUT
Serial.println(
F("avgXacc \tavgYacc \tavgZacc \tavgXgyro \tavgYgyro \tavgZgyro"));
for (size_t i = 0; i < arraySize; ++i)
{
Serial.print(avgValueFromIndex(i), 1);
Serial.print("\t\t");
}
Serial.println("");
Serial.println(F("avgXaccOff \tavgYaccOff \tavgZaccOff \tavgXgyroOff \t"
"avgXgyroOff \tavgXgyroOff"));
for (size_t i = 0; i < arraySize; ++i)
{
Serial.print(avgOffsetFromIndex(i), 1);
Serial.print("\t\t");
}
Serial.println("");
#endif // HUMAN_READABLE_OUTPUT
#ifdef PARSABLE_OUTPUT
for (size_t i = 0; i < arraySize-1; ++i)
{
Serial.print(avgValueFromIndex(i), 1);
Serial.print(",");
Serial.print(avgOffsetFromIndex(i),1);
Serial.print(",");
}
Serial.print(avgValueFromIndex(arraySize - 1), 1);
Serial.print(",");
Serial.println(avgOffsetFromIndex(arraySize - 1), 1);
#endif // PARSABLE_OUTPUT
}