-
Notifications
You must be signed in to change notification settings - Fork 1
/
MPU9250_interfacing.ino
144 lines (97 loc) · 3.35 KB
/
MPU9250_interfacing.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
#include <Wire.h>
#include <Servo.h>
#define SerialPort Serial
#include <SparkFunMPU9250-DMP.h>
MPU9250_DMP imu;
double roll , pitch;
float phi = 0;
float theta = 0;
long int pre_ts=0;
float phi_n_1, theta_n_1;
float phi_dot, theta_dot, phi_quat;
long gyro_x_cal, gyro_y_cal, gyro_z_cal;
float Q[4] = {1,0,0,0} ;
float Q_dot [4] ;
float Q_pre [4] = {1,0,0,0} ;
void setup()
{
{
{
SerialPort.begin(9600);
if (imu.begin() != INV_SUCCESS)
{
while (1)
{
SerialPort.println("Unable to communicate with MPU-9250");
SerialPort.println("Check connections, and try again.");
SerialPort.println();
delay(3000);
}
}
imu.setSensors(INV_XYZ_GYRO | INV_XYZ_ACCEL | INV_XYZ_COMPASS);
imu.setGyroFSR(250); // Set gyro to 2000 dps
// Accel options are +/- 2, 4, 8, or 16 g
imu.setAccelFSR(2); // Set accel to +/-2g
imu.setLPF(10); // Set LPF corner frequency to 5Hz
imu.setSampleRate(10); // Set sample rate to 10Hz
imu.setCompassSampleRate(50); // Set mag rate to 10Hz
}
//Wire.begin();
//for (int cal_int = 0; cal_int < 1000 ; cal_int ++){
//read_IMUDATA();
pre_ts=millis();
}
}
void loop()
{
if ( imu.dataReady() )
{
imu.update(UPDATE_ACCEL | UPDATE_GYRO | UPDATE_COMPASS);
printIMUData(millis()-pre_ts);
pre_ts=millis();
}
}
void printIMUData(long int dt)
{
float accelX = imu.calcAccel(imu.ax);
float accelY = imu.calcAccel(imu.ay);
float accelZ = imu.calcAccel(imu.az);
float gyroX = imu.calcGyro(imu.gx)/57.3;
float gyroY = imu.calcGyro(imu.gy)/57.3;
float gyroZ = imu.calcGyro(imu.gz)/57.3;
// NORMALIZE ACCELE VALUES
float naccel = sqrt(accelX*accelX +accelY*accelY+accelZ*accelZ);
accelX =accelX/naccel;
accelY =accelY/naccel;
accelZ =accelZ/naccel;
//Euler angle from accel
pitch = atan2 (accelY ,( sqrt ((accelX * accelX) + (accelZ * accelZ))));
roll = atan2(-accelX ,( sqrt((accelY * accelY) + (accelZ * accelZ))));
roll = (0.98*(roll+(gyroX)*dt/1000.0f) +0.02*(accelX))*57.3;
pitch = (0.98*(pitch + gyroY*dt/1000.0f)+0.02*(accelY))*57.3;
// quaternions
Q_dot[0] = -0.5* ( (gyroX*Q_pre[1]) +(gyroY*Q_pre[2]) + (Q_pre[3]*gyroZ));
Q_dot[1] = 0.5* ( (gyroX*Q_pre[0]) +(gyroZ*Q_pre[2]) - (Q_pre[3]*gyroY));
Q_dot[2] = 0.5* ( (gyroY*Q_pre[0]) -(gyroZ*Q_pre[1]) + (Q_pre[3]*gyroX));
Q_dot[3] = 0.5* ( (gyroZ*Q_pre[0]) +(gyroY*Q_pre[1]) - (Q_pre[2]*gyroX));
Q[0] =Q_pre[0]+ (Q_dot[0]*dt/1000.0);
Q_pre[0] = Q[0];
Q[1] =Q_pre[1]+ (Q_dot[1]*dt/1000.0);
Q_pre[1] = Q[1];
Q[2] =Q_pre[2]+ (Q_dot[2]*dt/1000.0);
Q_pre[2] = Q[2];
Q[3] =Q_pre[3]+ (Q_dot[3]*dt/1000.0);
Q_pre[3] = Q[3];
double n = (sqrt((Q[0]*Q[0]) + (Q[1]*Q[1]) + (Q[2]*Q[2]) + (Q[3]*Q[3])));
float Q0 =Q[0] / n;
float Q1 =Q[1] / n;
float Q2 = Q[2]/n;
float Q3 = Q[3]/n;
phi_quat = atan2 (2*((Q0*Q1)+(Q2*Q3)), ((0.5f-Q1*Q1-Q2*Q2)));
float theta_quat = asin( 2*((Q0*Q2)- (Q1*Q3)));
phi_quat = (0.98*(phi_quat+gyroX*dt/1000.0f) +0.02*(accelX))*57.3;
theta_quat = (0.98*(theta_quat+gyroY*dt/1000.0f) +0.02*(accelY))*57.3;
int filtered_roll = 0.99*(roll+roll*dt/1000.0f)+0.01*(phi_quat);
int filtered_pitch = 0.99*(pitch+pitch*dt/1000.0f)+0.01*(theta_quat);
Serial.println(String(filtered_roll)+','+String(filtered_pitch));
}