-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathThe_Code_2.ino
358 lines (290 loc) · 10.7 KB
/
The_Code_2.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/**
@file read9axis.ino
@brief This is an Example for the FaBo 9Axis I2C Brick.
http://fabo.io/202.html
Released under APACHE LICENSE, VERSION 2.0
http://www.apache.org/licenses/
@author FaBo<info@fabo.io>
*/
#include <Wire.h>
#include <FaBo9Axis_MPU9250.h>
#include <Kalman.h>
FaBo9Axis fabo_9axis;
float accX, accY, accZ;
float gyroX, gyroY, gyroZ;
float magX, magY, magZ;
float tempRaw;
float yaw2;
double roll, pitch, yaw; // Roll and pitch are calculated using the accelerometer while yaw is calculated using the magnetometer
double gyroXangle, gyroYangle, gyroZangle; // Angle calculate using the gyro only
double compAngleX, compAngleY, compAngleZ; // Calculated angle using a complementary filter
double kalAngleX, kalAngleY, kalAngleZ; // Calculated angle using a Kalman filter
Kalman kalmanX, kalmanY, kalmanZ; // Create the Kalman instances
#define MAG0MAX 603
#define MAG0MIN -578
#define MAG1MAX 542
#define MAG1MIN -701
#define MAG2MAX 547
#define MAG2MIN -556
float magOffset[3] = { (MAG0MAX + MAG0MIN) / 2, (MAG1MAX + MAG1MIN) / 2, (MAG2MAX + MAG2MIN) / 2 };
float magGain[3];
uint32_t timer;
float avg();
void setup() {
// Serial.begin(9600);
delay(100); // Wait for sensors to get ready
Serial.begin(115200);
Wire.begin();
Serial.println("RESET");
Serial.println();
Serial.println("configuring device.");
if (fabo_9axis.begin()) {
Serial.println("configured FaBo 9Axis I2C Brick");
} else {
Serial.println("device error");
while(1);
}
calibrateMag();
delay(100); // Wait for sensors to stabilize
/* Set Kalman and gyro starting angle */
updateMPU6050();
updateHMC5883L();
updatePitchRoll();
updateYaw();
kalmanX.setAngle(roll); // First set roll starting angle
gyroXangle = roll;
compAngleX = roll;
kalmanY.setAngle(pitch); // Then pitch
gyroYangle = pitch;
compAngleY = pitch;
kalmanZ.setAngle(yaw); // And finally yaw
gyroZangle = yaw;
compAngleZ = yaw;
timer = micros(); // Initialize the timer
}
int YAW=0;
int count=0;
void loop() {
/* Update all the IMU values */
updateMPU6050();
updateHMC5883L();
double dt = (double)(micros() - timer) / 1000000; // Calculate delta time
timer = micros();
/* Roll and pitch estimation */
updatePitchRoll();
double gyroXrate = gyroX / 131.0; // Convert to deg/s
double gyroYrate = gyroY / 131.0; // Convert to deg/s
#ifdef RESTRICT_PITCH
// This fixes the transition problem when the accelerometer angle jumps between -180 and 180 degrees
if ((roll < -90 && kalAngleX > 90) || (roll > 90 && kalAngleX < -90)) {
kalmanX.setAngle(roll);
compAngleX = roll;
kalAngleX = roll;
gyroXangle = roll;
} else
kalAngleX = kalmanX.getAngle(roll, gyroXrate, dt); // Calculate the angle using a Kalman filter
if (abs(kalAngleX) > 90)
gyroYrate = -gyroYrate; // Invert rate, so it fits the restricted accelerometer reading
kalAngleY = kalmanY.getAngle(pitch, gyroYrate, dt);
#else
// This fixes the transition problem when the accelerometer angle jumps between -180 and 180 degrees
if ((pitch < -90 && kalAngleY > 90) || (pitch > 90 && kalAngleY < -90)) {
kalmanY.setAngle(pitch);
compAngleY = pitch;
kalAngleY = pitch;
gyroYangle = pitch;
} else
kalAngleY = kalmanY.getAngle(pitch, gyroYrate, dt); // Calculate the angle using a Kalman filter
if (abs(kalAngleY) > 90)
gyroXrate = -gyroXrate; // Invert rate, so it fits the restricted accelerometer reading
kalAngleX = kalmanX.getAngle(roll, gyroXrate, dt); // Calculate the angle using a Kalman filter
#endif
/* Yaw estimation */
updateYaw();
double gyroZrate = gyroZ / 131.0; // Convert to deg/s
// This fixes the transition problem when the yaw angle jumps between -180 and 180 degrees
if ((yaw < -90 && kalAngleZ > 90) || (yaw > 90 && kalAngleZ < -90)) {
kalmanZ.setAngle(yaw);
compAngleZ = yaw;
kalAngleZ = yaw;
gyroZangle = yaw;
} else
kalAngleZ = kalmanZ.getAngle(yaw, gyroZrate, dt); // Calculate the angle using a Kalman filter
/* Estimate angles using gyro only */
gyroXangle += gyroXrate * dt; // Calculate gyro angle without any filter
gyroYangle += gyroYrate * dt;
gyroZangle += gyroZrate * dt;
//gyroXangle += kalmanX.getRate() * dt; // Calculate gyro angle using the unbiased rate from the Kalman filter
//gyroYangle += kalmanY.getRate() * dt;
//gyroZangle += kalmanZ.getRate() * dt;
/* Estimate angles using complimentary filter */
compAngleX = 0.93 * (compAngleX + gyroXrate * dt) + 0.07 * roll; // Calculate the angle using a Complimentary filter
compAngleY = 0.93 * (compAngleY + gyroYrate * dt) + 0.07 * pitch;
compAngleZ = 0.93 * (compAngleZ + gyroZrate * dt) + 0.07 * yaw;
// Reset the gyro angles when they has drifted too much
if (gyroXangle < -180 || gyroXangle > 180)
gyroXangle = kalAngleX;
if (gyroYangle < -180 || gyroYangle > 180)
gyroYangle = kalAngleY;
if (gyroZangle < -180 || gyroZangle > 180)
gyroZangle = kalAngleZ;
if (count<20&&count>=10){
delay(100);
yaw2 = avg();
//YAW= YAW + yaw;
//Serial.println(yaw2);
Serial.println(yaw);
}
if (count>20){
float yaw1;
yaw1=avg();
Serial.println(yaw1-yaw2);
/* Print Data */
#if 1
//Serial.print("roll");
//Serial.print(roll); Serial.print("\t");
/*Serial.print(gyroXangle); Serial.print("\t");
Serial.print(compAngleX); Serial.print("\t");
Serial.print(kalAngleX); Serial.print("\t");*/
//Serial.print("\t");
//Serial.print("pitch");
// Serial.print(pitch); Serial.print("\t");
/*Serial.print(gyroYangle); Serial.print("\t");
Serial.print(compAngleY); Serial.print("\t");
Serial.print(kalAngleY); Serial.print("\t");*/
//Serial.print("\t");
//Serial.print("yaw");-
//yaw=yaw+180;
delay(100);
/*Serial.print(gyroZangle); Serial.print("\t");
Serial.print(compAngleZ); Serial.print("\t");
Serial.print(kalAngleZ); Serial.print("\t");*/
#endif
/*#if 0 // Set to 1 to print the IMU data
Serial.print(accX / 16384.0); Serial.print("\t"); // Converted into g's
Serial.print(accY / 16384.0); Serial.print("\t");
Serial.print(accZ / 16384.0); Serial.print("\t");
Serial.print(gyroXrate); Serial.print("\t"); // Converted into degress per second
Serial.print(gyroYrate); Serial.print("\t");
Serial.print(gyroZrate); Serial.print("\t");
Serial.print(magX); Serial.print("\t"); // After gain and offset compensation
Serial.print(magY); Serial.print("\t");
Serial.print(magZ); Serial.print("\t");
#endif
#if 0 // Set to 1 to print the temperature
Serial.print("\t");
double temperature = (double)tempRaw / 340.0 + 36.53;
Serial.print(temperature); Serial.print("\t");
#endif*/
Serial.println();
} count++;
delay(100);
}
void updateMPU6050() {
/* while (i2cRead(MPU6050, 0x3B, i2cData, 14)); // Get accelerometer and gyroscope values
accX = ((i2cData[0] << 8) | i2cData[1]);
accY = -((i2cData[2] << 8) | i2cData[3]);
accZ = ((i2cData[4] << 8) | i2cData[5]);
tempRaw = (i2cData[6] << 8) | i2cData[7];
gyroX = -(i2cData[8] << 8) | i2cData[9];
gyroY = (i2cData[10] << 8) | i2cData[11];
gyroZ = -(i2cData[12] << 8) | i2cData[13];*/
fabo_9axis.readAccelXYZ(&accX,&accY,&accZ);
fabo_9axis.readGyroXYZ(&gyroX,&gyroY,&gyroZ);
//fabo_9axis.readMagnetXYZ(&magX,&magY, &magZ);
fabo_9axis.readTemperature(&tempRaw);
}
void updateHMC5883L() {
//fabo_9axis.readAccelXYZ(&accX,&accY,&az);
//fabo_9axis.readGyroXYZ(&gyroX,&gyroY,&gyroZ);
fabo_9axis.readMagnetXYZ(&magX,&magY, &magZ);
//fabo_9axis.readTemperature(&tempRaw);
}
void updatePitchRoll() {
// Source: http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf eq. 25 and eq. 26
// atan2 outputs the value of -π to π (radians) - see http://en.wikipedia.org/wiki/Atan2
// It is then converted from radians to degrees
#ifdef RESTRICT_PITCH // Eq. 25 and 26
roll = atan2(accY, accZ) * RAD_TO_DEG;
pitch = atan(-accX / sqrt(accY * accY + accZ * accZ)) * RAD_TO_DEG;
#else // Eq. 28 and 29
roll = atan(accY / sqrt(accX * accX + accZ * accZ)) * RAD_TO_DEG;
pitch = atan2(-accX, accZ) * RAD_TO_DEG;
#endif
}
void updateYaw() { // See: http://www.freescale.com/files/sensors/doc/app_note/AN4248.pdf
magX *= -1; // Invert axis - this it done here, as it should be done after the calibration
magZ *= -1;
magX *= magGain[0];
magY *= magGain[1];
magZ *= magGain[2];
magX -= magOffset[0];
magY -= magOffset[1];
magZ -= magOffset[2];
double rollAngle = kalAngleX * DEG_TO_RAD;
double pitchAngle = kalAngleY * DEG_TO_RAD;
double Bfy = magZ * sin(rollAngle) - magY * cos(rollAngle);
double Bfx = magX * cos(pitchAngle) + magY * sin(pitchAngle) * sin(rollAngle) + magZ * sin(pitchAngle) * cos(rollAngle);
yaw = atan2(-Bfy, Bfx) * RAD_TO_DEG;
yaw *= -1;
}
void calibrateMag() { // Inspired by: https://code.google.com/p/open-headtracker/
//i2cWrite(HMC5883L, 0x00, 0x11, true);
delay(100); // Wait for sensor to get ready
updateHMC5883L(); // Read positive bias values
float magPosOff[3] = { magX, magY, magZ };
//i2cWrite(HMC5883L, 0x00, 0x12, true);
delay(100); // Wait for sensor to get ready
updateHMC5883L(); // Read negative bias values
float magNegOff[3] = { magX, magY, magZ };
//i2cWrite(HMC5883L, 0x00, 0x10, true); // Back to normal
magGain[0] = -2500 / float(magNegOff[0] - magPosOff[0]);
magGain[1] = -2500 / float(magNegOff[1] - magPosOff[1]);
magGain[2] = -2500 / float(magNegOff[2] - magPosOff[2]);
#if 0
Serial.print("Mag cal: ");
Serial.print(magNegOff[0] - magPosOff[0]);
Serial.print(",");
Serial.print(magNegOff[1] - magPosOff[1]);
Serial.print(",");
Serial.println(magNegOff[2] - magPosOff[2]);
Serial.print("Gain: ");
Serial.print(magGain[0]);
Serial.print(",");
Serial.print(magGain[1]);
Serial.print(",");
Serial.println(magGain[2]);
#endif
}
float BubbleSort (float arr[], int n)
{
int i, j;
for (i = 0; i < n; ++i)
{
for (j = 0; j < n-i-1; ++j)
{
// Comparing consecutive data and switching values if value at j > j+1.
if (arr[j] > arr[j+1])
{
arr[j] = arr[j]+arr[j+1];
arr[j+1] = arr[j]-arr[j + 1];
arr[j] = arr[j]-arr[j + 1];
}
}
// Value at n-i-1 will be maximum of all the values below this index.
} float p=0;
for(int k=2; k<9; k++)
p=p+arr[k];
return p/7.00;
}
float avg(){
float a[10];
for(int i=0;i<10;i++)
{
delay(10);
updateYaw();
a[i]=yaw;
}
yaw=BubbleSort( a , 10 );
return yaw;
}