-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathScript.m
175 lines (139 loc) · 4.71 KB
/
Script.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
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
%% Housekeeping
addpath('ximu_matlab_library'); % include x-IMU MATLAB library
addpath('quaternion_library'); % include quatenrion library
close all; % close all figures
clear; % clear all variables
clc; % clear the command terminal
%% Import data
xIMUdata = xIMUdataClass('LoggedData/LoggedData');
samplePeriod = 1/256;
gyr = [xIMUdata.CalInertialAndMagneticData.Gyroscope.X...
xIMUdata.CalInertialAndMagneticData.Gyroscope.Y...
xIMUdata.CalInertialAndMagneticData.Gyroscope.Z]; % gyroscope
acc = [xIMUdata.CalInertialAndMagneticData.Accelerometer.X...
xIMUdata.CalInertialAndMagneticData.Accelerometer.Y...
xIMUdata.CalInertialAndMagneticData.Accelerometer.Z]; % accelerometer
% Plot
figure('NumberTitle', 'off', 'Name', 'Gyroscope');
hold on;
plot(gyr(:,1), 'r');
plot(gyr(:,2), 'g');
plot(gyr(:,3), 'b');
xlabel('sample');
ylabel('dps');
title('Gyroscope');
legend('X', 'Y', 'Z');
figure('NumberTitle', 'off', 'Name', 'Accelerometer');
hold on;
plot(acc(:,1), 'r');
plot(acc(:,2), 'g');
plot(acc(:,3), 'b');
xlabel('sample');
ylabel('g');
title('Accelerometer');
legend('X', 'Y', 'Z');
%% Process data through AHRS algorithm (calcualte orientation)
% See: http://www.x-io.co.uk/open-source-imu-and-ahrs-algorithms/
R = zeros(3,3,length(gyr)); % rotation matrix describing sensor relative to Earth
ahrs = MahonyAHRS('SamplePeriod', samplePeriod, 'Kp', 1);
for i = 1:length(gyr)
ahrs.UpdateIMU(gyr(i,:) * (pi/180), acc(i,:)); % gyroscope units must be radians
R(:,:,i) = quatern2rotMat(ahrs.Quaternion)'; % transpose because ahrs provides Earth relative to sensor
end
%% Calculate 'tilt-compensated' accelerometer
tcAcc = zeros(size(acc)); % accelerometer in Earth frame
for i = 1:length(acc)
tcAcc(i,:) = R(:,:,i) * acc(i,:)';
end
% Plot
figure('NumberTitle', 'off', 'Name', '''Tilt-Compensated'' accelerometer');
hold on;
plot(tcAcc(:,1), 'r');
plot(tcAcc(:,2), 'g');
plot(tcAcc(:,3), 'b');
xlabel('sample');
ylabel('g');
title('''Tilt-compensated'' accelerometer');
legend('X', 'Y', 'Z');
%% Calculate linear acceleration in Earth frame (subtracting gravity)
linAcc = tcAcc - [zeros(length(tcAcc), 1), zeros(length(tcAcc), 1), ones(length(tcAcc), 1)];
linAcc = linAcc * 9.81; % convert from 'g' to m/s/s
% Plot
figure('NumberTitle', 'off', 'Name', 'Linear Acceleration');
hold on;
plot(linAcc(:,1), 'r');
plot(linAcc(:,2), 'g');
plot(linAcc(:,3), 'b');
xlabel('sample');
ylabel('g');
title('Linear acceleration');
legend('X', 'Y', 'Z');
%% Calculate linear velocity (integrate acceleartion)
linVel = zeros(size(linAcc));
for i = 2:length(linAcc)
linVel(i,:) = linVel(i-1,:) + linAcc(i,:) * samplePeriod;
end
% Plot
figure('NumberTitle', 'off', 'Name', 'Linear Velocity');
hold on;
plot(linVel(:,1), 'r');
plot(linVel(:,2), 'g');
plot(linVel(:,3), 'b');
xlabel('sample');
ylabel('g');
title('Linear velocity');
legend('X', 'Y', 'Z');
%% High-pass filter linear velocity to remove drift
order = 1;
filtCutOff = 0.1;
[b, a] = butter(order, (2*filtCutOff)/(1/samplePeriod), 'high');
linVelHP = filtfilt(b, a, linVel);
% Plot
figure('NumberTitle', 'off', 'Name', 'High-pass filtered Linear Velocity');
hold on;
plot(linVelHP(:,1), 'r');
plot(linVelHP(:,2), 'g');
plot(linVelHP(:,3), 'b');
xlabel('sample');
ylabel('g');
title('High-pass filtered linear velocity');
legend('X', 'Y', 'Z');
%% Calculate linear position (integrate velocity)
linPos = zeros(size(linVelHP));
for i = 2:length(linVelHP)
linPos(i,:) = linPos(i-1,:) + linVelHP(i,:) * samplePeriod;
end
% Plot
figure('NumberTitle', 'off', 'Name', 'Linear Position');
hold on;
plot(linPos(:,1), 'r');
plot(linPos(:,2), 'g');
plot(linPos(:,3), 'b');
xlabel('sample');
ylabel('g');
title('Linear position');
legend('X', 'Y', 'Z');
%% High-pass filter linear position to remove drift
order = 1;
filtCutOff = 0.1;
[b, a] = butter(order, (2*filtCutOff)/(1/samplePeriod), 'high');
linPosHP = filtfilt(b, a, linPos);
% Plot
figure('NumberTitle', 'off', 'Name', 'High-pass filtered Linear Position');
hold on;
plot(linPosHP(:,1), 'r');
plot(linPosHP(:,2), 'g');
plot(linPosHP(:,3), 'b');
xlabel('sample');
ylabel('g');
title('High-pass filtered linear position');
legend('X', 'Y', 'Z');
%% Play animation
SamplePlotFreq = 8;
SixDOFanimation(linPosHP, R, ...
'SamplePlotFreq', SamplePlotFreq, 'Trail', 'Off', ...
'Position', [9 39 1280 720], ...
'AxisLength', 0.1, 'ShowArrowHead', false, ...
'Xlabel', 'X (m)', 'Ylabel', 'Y (m)', 'Zlabel', 'Z (m)', 'ShowLegend', false, 'Title', 'Unfiltered',...
'CreateAVI', false, 'AVIfileNameEnum', false, 'AVIfps', ((1/samplePeriod) / SamplePlotFreq));
%% End of script