-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_call.m
142 lines (120 loc) · 3.87 KB
/
main_call.m
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
% Quaternion based Extended Kalman Filter using MPU 9250
% Goals : State estimation, narrow down the uncertanity by fusing the
% sensor data
% Create a function that takes in the IMU raw data and sampling frequency
% along with the other neccessary inputs. Will explain in the future.
% Whihc spits out the roll pitch yaw.
% This then is sent to simulink function block, whihc inturn goes into the
% simsacpe model.
%%
% Includes
addpath('utils');
addpath('datafiles');
%% Read the logged data
%Data read from the Hardware
%Uncomment when using the hardware
MPU9250 = table2array(MPU9250data);
sensor_data = MPU9250;
%Data read from the Mobile sensor
%Uncomment when using the Mobile sensor
%F1 = load('90.mat');
%sensor_data = F1;
data_num=round(size(sensor_data,1)); % get the length
L = data_num - 2; % matrix length(row) --> drone data_log: 7600
X_ACCL = zeros;
Y_ACCL = zeros;
Z_ACCL = zeros;
X_GYRO = zeros;
Y_GYRO = zeros;
Z_GYRO = zeros;
X_MAG = zeros;
Y_MAG = zeros;
Z_MAG = zeros;
%%
%This block is only if the data is logged in from the mobile device. Uncomment when using this.
% j=0;
% for i = 1:L
% j = j+1;
% T = timetable2table(F1.AngularVelocity);
% X_GYRO(j,1) = T.X(i);
% Y_GYRO(j,1) = T.Y(i);
% Z_GYRO(j,1) = T.Z(i);
%
% T = timetable2table(F1.Acceleration);
% X_ACCL(j,1) = T.X(i);
% Y_ACCL(j,1) = T.Y(i);
% Z_ACCL(j,1) = T.Z(i);
%
% T = timetable2table(F1.MagneticField);
% X_MAG(j,1) = T.X(i);
% Y_MAG(j,1) = T.Y(i);
% Z_MAG(j,1) = T.Z(i);
% end
%
%%
%This block is only if the data is logged in from the Hardware device. Uncomment when using this.
j = 0;
for i=3:data_num % for time being
j=j+1;
% just calling the logged data and placing it in a matrix
X_ACCL(j,1) = mean(sensor_data(i,1)); % Depends on how datas are logged
Y_ACCL(j,1) = mean(sensor_data(i,2));
Z_ACCL(j,1) = -mean(sensor_data(i,3));
X_GYRO(j,1) = mean(sensor_data(i,4)); %
Y_GYRO(j,1) = mean(sensor_data(i,5));
Z_GYRO(j,1) = mean(sensor_data(i,6));
X_MAG(j,1) = mean(sensor_data(i,7));
Y_MAG(j,1) = mean(sensor_data(i,8));
Z_MAG(j,1) = mean(sensor_data(i,9));
end
%init()
Time = zeros(L,1);
Time(1,1) = 0.007; % Sampling Period (Real)
gyro_bias = [ -3.1; -3.1; 0.]; % Gyro bias
% Organize
%Time = load('Time.mat');
acc = [X_ACCL, Y_ACCL, Z_ACCL]; % Accel data
gyro = [X_GYRO - gyro_bias(1), Y_GYRO - gyro_bias(2), Z_GYRO - gyro_bias(3)]; % Gyro data
mag = [X_MAG, Y_MAG, Z_MAG]; % Mag data for Yaw
% Call the function
[acc_roll, acc_pitch, mag_yaw, estimate_roll, estimate_pitch, estimate_yaw,Time] = FusionKalman(acc,gyro_bias,gyro,mag,Time,L);
%%
%Plots
% %ROLL
% figure;
% plot(Time,acc_roll,Time,estimate_roll);
% legend('roll-acc','roll-ekf','FontSize',10);
% xlabel('t / s','FontSize',20)
% ylabel('roll','FontSize',20)
% title('roll','FontSize',20);
% %PITCH
% figure;
% plot(Time,acc_pitch,Time,estimate_pitch);
% legend('pitch-acc','pitch-ekf','FontSize',10);
% xlabel('t / s','FontSize',20)
% ylabel('pitch','FontSize',20)
% title('pitch','FontSize',20);
plot(Time,acc(1),Time,Z_GYRO);
legend('acc','gyro','FontSize',10);
xlabel('t / s','FontSize',20)
ylabel('roll','FontSize',20)
title('roll','FontSize',20);
shg
%YAW
% figure(3);
% plot(Time,mag_yaw,Time,360 - estimate_yaw);
% legend('yaw-mag','yaw-ekf','FontSize',10);
% xlabel('t / s','FontSize',20)
% ylabel('yaw','FontSize',20)
% title('yaw','FontSize',20);
% end
%%
%For Simulink only
acc_cat = cat(2,Time,acc); % concat with time for simulink
gyro_cat = cat(2,Time,gyro); % concat with time for simulink
mag_cat = cat(2,Time,mag);% concat with time for simulink
roll_cat = cat(2,Time,estimate_roll);
pitch_cat = cat(2,Time,estimate_pitch);
yaw_cat = cat(2,Time,estimate_yaw);
%end
save main_call.mat