-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeadReckon_MadgwickGait.m
248 lines (203 loc) · 7.56 KB
/
deadReckon_MadgwickGait.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
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
% General code analysis
% Adapted from Madgwick gait analysis script
% More general: Contains HPF and LPF to filter both sides
clear;
close all;
clc;
addpath('Libraries/ximu_matlab_library'); % include x-IMU MATLAB library
addpath('Libraries/quaternion_library');
addpath('Libraries/MahonyAHRS');
addpath('Libraries');
% -------------------------------------------------------------------------
% Select dataset (comment in/out)
% filePath = 'Datasets/straightLine';
% startTime = 6;
% stopTime = 26;
dataPath = '../data/sept29/processed_walk around lab.csv';
% dataPath = 'data/20170924/walk_forwardback.csv';
% dataPath = '../../../Projects/Dead Reckoning IMU/Libraries/Gait-Tracking-With-x-IMU-master/Gait Tracking With x-IMU/Datasets/straightLine_CalInertialAndMag.csv';
dataTemp = csvread(dataPath,1,0); % Skip the header
time = dataTemp(:,1);
packets = dataTemp(:,2);
acc = dataTemp(:,3:5); % Accelerometer data, g values
gyr = dataTemp(:,6:8); % Gyro data, degrees per second
accX = acc(:,1);
accY = acc(:,2);
accZ = acc(:,3);
gyrX = gyr(:,1);
gyrY = gyr(:,2);
gyrZ = gyr(:,3);
mcuFreq = 16; % MCU Recording frequency, in Hz
samplePeriod = 1 / (mcuFreq); % Period is 1/frequency
% cutoffFreq = (filtCutOff)/(1/samplePeriod);
% -------------------------------------------------------------------------
% Manually frame data
% startTime = 0;
% stopTime = 10;
% indexSel = find(sign(time-startTime)+1, 1) : find(sign(time-stopTime)+1, 1);
% time = time(indexSel);
% gyrX = gyrX(indexSel, :);
% gyrY = gyrY(indexSel, :);
% gyrZ = gyrZ(indexSel, :);
% accX = accX(indexSel, :);
% accY = accY(indexSel, :);
% accZ = accZ(indexSel, :);
% -------------------------------------------------------------------------
% Detect stationary periods
% Compute accelerometer magnitude
acc_mag = sqrt(accX.*accX + accY.*accY + accZ.*accZ);
% Default filter values are 0.001 and 5, with threshold 0.05
% HP filter accelerometer data
filtCutOff = 0.25;
filtHPF = (2*filtCutOff)/(1/samplePeriod);
% filtHPF = 7.8e-6;
[b, a] = butter(1, filtHPF, 'high');
acc_magFilt = filtfilt(b, a, acc_mag);
% Compute absolute value
acc_magFilt = abs(acc_magFilt);
% LP filter accelerometer data
filtCutOff = 7.9;
filtLPF = (2*filtCutOff)/(1/samplePeriod);
% filtLPF = 0.99;
[b, a] = butter(1, filtLPF, 'low');
acc_magFilt = filtfilt(b, a, acc_magFilt);
% Threshold detection
stationary = acc_magFilt < 0.025;
% -------------------------------------------------------------------------
% Plot data raw sensor data and stationary periods
figure('Position', [9 39 900 600], 'NumberTitle', 'off', 'Name', 'Sensor Data');
ax(1) = subplot(2,1,1);
hold on;
plot(time, gyrX, 'r');
plot(time, gyrY, 'g');
plot(time, gyrZ, 'b');
title('Gyroscope');
xlabel('Time (s)');
ylabel('Angular velocity (^\circ/s)');
legend('X', 'Y', 'Z');
hold off;
ax(2) = subplot(2,1,2);
hold on;
plot(time, accX, 'r');
plot(time, accY, 'g');
plot(time, accZ, 'b');
plot(time, acc_magFilt, ':k');
plot(time, stationary, 'k', 'LineWidth', 2);
title('Accelerometer');
xlabel('Time (s)');
ylabel('Acceleration (g)');
legend('X', 'Y', 'Z', 'Filtered', 'Stationary');
hold off;
linkaxes(ax,'x');
% -------------------------------------------------------------------------
% Compute orientation
quat = zeros(length(time), 4);
AHRSalgorithm = AHRS('SamplePeriod', samplePeriod, 'Kp', 1, 'KpInit', 1);
% Initial convergence
initPeriod = 2;
indexSel = 1 : find(sign(time-(time(1)+initPeriod))+1, 1);
for i = 1:2000
AHRSalgorithm.UpdateIMU([0 0 0], [mean(accX(indexSel)) mean(accY(indexSel)) mean(accZ(indexSel))]);
end
% For all data
for t = 1:length(time)
if(stationary(t))
AHRSalgorithm.Kp = 0.5;
else
AHRSalgorithm.Kp = 0;
end
AHRSalgorithm.UpdateIMU(deg2rad([gyrX(t) gyrY(t) gyrZ(t)]), [accX(t) accY(t) accZ(t)]);
quat(t,:) = AHRSalgorithm.Quaternion;
end
% -------------------------------------------------------------------------
% Compute translational accelerations
% Rotate body accelerations to Earth frame
acc = quaternRotate([accX accY accZ], quaternConj(quat));
% % Remove gravity from measurements
% acc = acc - [zeros(length(time), 2) ones(length(time), 1)]; % unnecessary due to velocity integral drift compensation
% Convert acceleration measurements to m/s/s
acc = acc * 9.81;
% Plot translational accelerations
figure('Position', [9 39 900 300], 'NumberTitle', 'off', 'Name', 'Accelerations');
hold on;
plot(time, acc(:,1), 'r');
plot(time, acc(:,2), 'g');
plot(time, acc(:,3), 'b');
title('Acceleration');
xlabel('Time (s)');
ylabel('Acceleration (m/s/s)');
legend('X', 'Y', 'Z');
hold off;
% -------------------------------------------------------------------------
% Compute translational velocities
acc(:,3) = acc(:,3) - 9.81;
% Integrate acceleration to yield velocity
vel = zeros(size(acc));
for t = 2:length(vel)
vel(t,:) = vel(t-1,:) + acc(t,:) * samplePeriod;
if(stationary(t) == 1)
vel(t,:) = [0 0 0]; % force zero velocity when foot stationary
end
end
% Compute integral drift during non-stationary periods
velDrift = zeros(size(vel));
stationaryStart = find([0; diff(stationary)] == -1);
stationaryEnd = find([0; diff(stationary)] == 1);
for i = 1:numel(stationaryEnd)
driftRate = vel(stationaryEnd(i)-1, :) / (stationaryEnd(i) - stationaryStart(i));
enum = 1:(stationaryEnd(i) - stationaryStart(i));
drift = [enum'*driftRate(1) enum'*driftRate(2) enum'*driftRate(3)];
velDrift(stationaryStart(i):stationaryEnd(i)-1, :) = drift;
end
% Remove integral drift
vel = vel - velDrift;
% Plot translational velocity
figure('Position', [9 39 900 300], 'NumberTitle', 'off', 'Name', 'Velocity');
hold on;
plot(time, vel(:,1), 'r');
plot(time, vel(:,2), 'g');
plot(time, vel(:,3), 'b');
title('Velocity');
xlabel('Time (s)');
ylabel('Velocity (m/s)');
legend('X', 'Y', 'Z');
hold off;
% -------------------------------------------------------------------------
% Compute translational position
% Integrate velocity to yield position
pos = zeros(size(vel));
for t = 2:length(pos)
pos(t,:) = pos(t-1,:) + vel(t,:) * samplePeriod; % integrate velocity to yield position
end
% Plot translational position
figure('Position', [9 39 900 600], 'NumberTitle', 'off', 'Name', 'Position');
hold on;
plot(time, pos(:,1), 'r');
plot(time, pos(:,2), 'g');
plot(time, pos(:,3), 'b');
title('Position');
xlabel('Time (s)');
ylabel('Position (m)');
legend('X', 'Y', 'Z');
hold off;
% -------------------------------------------------------------------------
% Plot 3D foot trajectory
% % Remove stationary periods from data to plot
% posPlot = pos(find(~stationary), :);
% quatPlot = quat(find(~stationary), :);
posPlot = pos;
quatPlot = quat;
% Extend final sample to delay end of animation
extraTime = 20;
onesVector = ones(extraTime*(1/samplePeriod), 1);
posPlot = [posPlot; [posPlot(end, 1)*onesVector, posPlot(end, 2)*onesVector, posPlot(end, 3)*onesVector]];
quatPlot = [quatPlot; [quatPlot(end, 1)*onesVector, quatPlot(end, 2)*onesVector, quatPlot(end, 3)*onesVector, quatPlot(end, 4)*onesVector]];
% Create 6 DOF animation
SamplePlotFreq = 4;
Spin = 120;
SixDOFanimation(posPlot, quatern2rotMat(quatPlot), ...
'SamplePlotFreq', SamplePlotFreq, 'Trail', 'All', ...
'Position', [9 39 1280 768], 'View', [(100:(Spin/(length(posPlot)-1)):(100+Spin))', 10*ones(length(posPlot), 1)], ...
'AxisLength', 0.1, 'ShowArrowHead', false, ...
'Xlabel', 'X (m)', 'Ylabel', 'Y (m)', 'Zlabel', 'Z (m)', 'ShowLegend', false, ...
'CreateAVI', false, 'AVIfileNameEnum', false, 'AVIfps', ((1/samplePeriod) / SamplePlotFreq));