-
Notifications
You must be signed in to change notification settings - Fork 1
/
computeKinematicDifferences.m
357 lines (314 loc) · 13.9 KB
/
computeKinematicDifferences.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
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
function [] = computeKinematicDifferences(solution, trackorprescribe)
import org.opensim.modeling.*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% solution = MocoTrajectory(solution);
Time = solution.getTimeMat();
numColPoints = solution.getNumTimes();
workingdir = pwd;
% try storage instead
solutionstatestable = solution.exportToStatesTable();
solutionNumRows = solutionstatestable.getNumRows();
solutionLabels = solutionstatestable.getColumnLabels();
solutionNumLabels = solutionLabels.size();
solutionremove = [];
% find all the labels that are not jointset
for l = 0:solutionNumLabels-1
templabel = solutionLabels.get(l);
if ~contains(string(templabel), 'jointset')
solutionremove = [solutionremove, {templabel}];
end
end
% remove all the ones that aren't jointset
for r = 1:length(solutionremove)
solutionstatestable.removeColumn(solutionremove{r});
end
% overwrite label vector and count
solutionLabels = solutionstatestable.getColumnLabels();
solutionNumLabels = solutionLabels.size();
% load the tracked or original states for comparison
% get a variable to say which muscle driven one it is
if strcmp(trackorprescribe, 'track')
% trackstatestable = TimeSeriesTable('muscle_statetrack_grfprescribe_tracked_states.sto');
trackstatestable = TimeSeriesTable('torque_statetrack_grfprescribe_tracked_states.sto');
elseif strcmp(trackorprescribe, 'prescribe')
trackstatestable = TimeSeriesTable('torque_statetrack_grfprescribe_tracked_states.sto');
end
trackNumRows = trackstatestable.getNumRows();
trackLabels = trackstatestable.getColumnLabels();
trackNumLabels = trackLabels.size();
trackremove = [];
% find all the labels that are not jointset
for l = 0:trackNumLabels-1
templabel = trackLabels.get(l);
if ~contains(string(templabel), 'jointset') || contains(string(templabel), 'forceset')
trackremove = [trackremove, {templabel}];
end
end
% remove all the ones that aren't jointset
for r = 1:length(trackremove)
trackstatestable.removeColumn(trackremove{r});
end
% overwrite label vector and count
trackLabels = trackstatestable.getColumnLabels();
trackNumLabels = trackLabels.size();
% have a matching set of jointset values in the tables
% compare the two trajectories for each of the values that we care
% about
solutiontime = solutionstatestable.getIndependentColumn();
solutiontime2 = [];
for t = 0:solutiontime.size()-1
try
solutiontime2 = [solutiontime2, solutiontime.get(t).doubleValue()];
catch
solutiontime2 = [solutiontime2, solutiontime.get(t)];
end
end
tracktime = trackstatestable.getIndependentColumn();
tracktime2 = [];
for t = 0:tracktime.size()-1
try
tracktime2 = [tracktime2, tracktime.get(t).doubleValue()];
catch
tracktime2 = [tracktime2, tracktime.get(t)];
end
end
% now want to do the same thing but plot differences between signals
% have to figure out the trailing ends on the tracked states
% use the time vectors to see where they overlap
timesplit1 = length(find(tracktime2 < solutiontime2(1)));
timesplit2 = find(tracktime2 > solutiontime2(end));
timesplit2 = timesplit2(1);
if timesplit1 == 0
timesplit1 = 1;
temptracktime = tracktime2(timesplit1:timesplit2);
else
temptracktime = tracktime2(timesplit1:timesplit2);
end
% temptracktime = tracktime2(timesplit1:timesplit2);
% tempfig1 = figure('Position',[1,1,1920,1080]);
% % go through all the joints
% for l=0:trackNumLabels-1
% templabel = trackLabels.get(l);
% temptrack = trackstatestable.getDependentColumn(templabel);
% temptrack2 = temptrack.getAsMat();
% tempsolution = solutionstatestable.getDependentColumn(templabel);
% tempsolution2 = tempsolution.getAsMat();
%
% % okay now what...
% subplot(6,8,l+1);
% plot(tracktime2, temptrack2);
% hold on;
% plot(solutiontime2, tempsolution2);
% title(string(templabel));
% xlabel('time [s]');
% ylabel('[rad or rad/s]');
% grid on;
%
%
% end
%
% % subplot(6,8,l+2);
% legend('solution','original','location','eastoutside');
% workingdirlength = length(workingdir);
% trial = workingdir(workingdirlength-6:workingdirlength);
% condition = workingdir(workingdirlength-18:workingdirlength-8);
% subject = workingdir(workingdirlength-26:workingdirlength-20);
%
% print(tempfig1, ...
% strcat(strcat('C:\Users\JP\code\repos\Stanford\delplab\projects\muscleModel\analysis\', ...
% strcat(subject,strcat('\',strcat(string(trial),strcat('_',strcat(condition,'_kinematics_differences.png'))))))),...
% '-dpng', '-r500')
% disp('print 1')
%
%
%
%
%
% tempfig2 = figure('Position',[1,1,1920,1080]);
% % go through all the joints
% for l=0:trackNumLabels-1
% templabel = trackLabels.get(l);
% temptrack = trackstatestable.getDependentColumn(templabel);
% temptrack2 = temptrack.getAsMat();
% tempsolution = solutionstatestable.getDependentColumn(templabel);
% tempsolution2 = tempsolution.getAsMat();
%
% % have to figure out where they overlap
% tempsolution3 = interp1(solutiontime2, tempsolution2, temptracktime);
%
% % okay now what...
% subplot(6,8,l+1);
% plot(temptracktime, temptrack2(timesplit1:timesplit2));
% hold on;
% plot(temptracktime, tempsolution3);
% title(string(templabel));
% xlabel('time [s]');
% ylabel('[rad or rad/s]');
% grid on;
% end
%
% % subplot(6,8,l+2);
% legend('solution','original','location','eastoutside');
% workingdirlength = length(workingdir);
% trial = workingdir(workingdirlength-6:workingdirlength);
% condition = workingdir(workingdirlength-18:workingdirlength-8);
% subject = workingdir(workingdirlength-26:workingdirlength-20);
%
% print(tempfig2, ...
% strcat(strcat('C:\Users\JP\code\repos\Stanford\delplab\projects\muscleModel\analysis\', ...
% strcat(subject,strcat('\',strcat(string(trial),strcat('_',strcat(condition,'_kinematics_differences_adj.png'))))))),...
% '-dpng', '-r500')
% disp('print 1')
% same figure but in degrees
tempfig3 = figure('Position',[1,1,2800,1080]);
% go through all the joints
for l=0:trackNumLabels-1
templabel = trackLabels.get(l);
templabel = string(templabel); % like double - idk why
if contains(templabel, 'tx') || contains(templabel, 'ty') || contains(templabel, 'tz')
temptrack = trackstatestable.getDependentColumn(templabel);
temptrack2 = temptrack.getAsMat();
tempsolution = solutionstatestable.getDependentColumn(templabel);
tempsolution2 = tempsolution.getAsMat();
% have to figure out where they overlap
tempsolution3 = interp1(solutiontime2, tempsolution2, temptracktime);
else
temptrack = trackstatestable.getDependentColumn(templabel);
temptrack2 = temptrack.getAsMat();
temptrack2 = temptrack2.*180./pi();
tempsolution = solutionstatestable.getDependentColumn(templabel);
tempsolution2 = tempsolution.getAsMat();
% have to figure out where they overlap
tempsolution3 = interp1(solutiontime2, tempsolution2, temptracktime);
tempsolution3 = tempsolution3.*180./pi();
end
% okay plot everything
subplot(6,8,l+1);
plot(temptracktime, temptrack2(timesplit1:timesplit2));
hold on;
plot(temptracktime, tempsolution3);
title(string(templabel));
xlabel('time [s]');
ylabel('[deg or deg/s]');
grid on;
end
% subplot(6,8,l+2);
legend('solution','original','location','eastoutside');
workingdirlength = length(workingdir);
trial = workingdir(workingdirlength-6:workingdirlength);
if strcmp(workingdir(workingdirlength-15), '\') % welkexo
condition = workingdir(workingdirlength-14:workingdirlength-8);
subject = workingdir(workingdirlength-22:workingdirlength-16);
end
if strcmp(workingdir(workingdirlength-19),'\') % welknatural
condition = workingdir(workingdirlength-18:workingdirlength-8);
subject = workingdir(workingdirlength-26:workingdirlength-20);
end
temptarget = strcat(pwd,'\..\..\..\..\analysis\',subject,'\',string(trial),'_',condition,'_kinematics_differences_adj_deg_',trackorprescribe);
delete(strcat(temptarget, '.png'));
print(tempfig3, temptarget, '-dpng', '-r500')
disp('print 1')
% now a figure that actually takes the differences between them, or the
% deviation in the tracking problem from the IK angles
% same figure but in degrees
tempfig4 = figure('Position',[1,1,2800,1080]);
% go through all the joints
% want to get two data structures - one with names, and one with error
errnames = [];
errvalus = [];
for l=0:trackNumLabels-1
templabel = trackLabels.get(l);
templabel = string(templabel);
if contains(templabel, 'tx') || contains(templabel, 'ty') || contains(templabel, 'tz')
temptrack = trackstatestable.getDependentColumn(templabel);
temptrack2 = temptrack.getAsMat();
tempsolution = solutionstatestable.getDependentColumn(templabel);
tempsolution2 = tempsolution.getAsMat();
% have to figure out where they overlap
tempsolution3 = interp1(solutiontime2, tempsolution2, temptracktime);
% okay now what...
subplot(6,8,l+1);
% plot(temptracktime, ((tempsolution3.*180./pi()) - (temptrack2(timesplit1:timesplit2).*180./pi())')');
plot(temptracktime, (tempsolution3 - (temptrack2(timesplit1:timesplit2))')');
hold on;
% plot(temptracktime, tempsolution3.*180./pi());
% get the RMS error
err = ((tempsolution3 - (temptrack2(timesplit1:timesplit2))')');
err = err(~isnan(err));
sqerr = err.^2;
msqerr = mean(sqerr);
rmse = sqrt(msqerr);
% add to vectors
if ~contains(templabel, 'speed')
errnames = [errnames, templabel];
errvalus = [errvalus, rmse];
end
title(string(templabel));
xlabel(strcat('time [s]\nRMSE: ',string(rmse)));
% xlabel(strcat('time [s]\nRMSE: ',string(rmse)));
ylabel('difference [m]');
grid on;
% legend(strcat('RMSE: ', string(rmse)));
else
temptrack = trackstatestable.getDependentColumn(templabel);
temptrack2 = temptrack.getAsMat();
tempsolution = solutionstatestable.getDependentColumn(templabel);
tempsolution2 = tempsolution.getAsMat();
% have to figure out where they overlap
tempsolution3 = interp1(solutiontime2, tempsolution2, temptracktime);
% convert to degrees
tempsolution3 = tempsolution3.*180./pi();
temptrack2 = temptrack2.*180./pi();
% okay now what...
subplot(6,8,l+1);
% plot(temptracktime, ((tempsolution3.*180./pi()) - (temptrack2(timesplit1:timesplit2).*180./pi())')');
plot(temptracktime, (tempsolution3 - (temptrack2(timesplit1:timesplit2))')');
hold on;
% plot(temptracktime, tempsolution3.*180./pi());
% get the RMS error
err = ((tempsolution3 - (temptrack2(timesplit1:timesplit2))')');
err = err(~isnan(err));
sqerr = err.^2;
msqerr = mean(sqerr);
rmse = sqrt(msqerr);
% add to vectors
if ~contains(templabel, 'speed')
errnames = [errnames, templabel];
errvalus = [errvalus, rmse];
end
title(string(templabel));
xlabel(strcat('time [s]\nRMSE: ',string(rmse)));
ylabel('difference [deg]');
grid on;
% legend(strcat('RMSE: ', string(rmse)));
end
% % okay now what...
% subplot(6,8,l+1);
% % plot(temptracktime, ((tempsolution3.*180./pi()) - (temptrack2(timesplit1:timesplit2).*180./pi())')');
% plot(temptracktime, (tempsolution3 - (temptrack2(timesplit1:timesplit2))')');
% hold on;
% % plot(temptracktime, tempsolution3.*180./pi());
% title(string(templabel));
% xlabel('time [s]');
% ylabel('[deg or deg/s]');
% grid on;
end
% subplot(6,8,l+2);
legend('solution-original','location','eastoutside');
workingdirlength = length(workingdir);
if strcmp(workingdir(workingdirlength-15), '\') % welkexo
condition = workingdir(workingdirlength-14:workingdirlength-8);
subject = workingdir(workingdirlength-22:workingdirlength-16);
end
if strcmp(workingdir(workingdirlength-19),'\') % welknatural
condition = workingdir(workingdirlength-18:workingdirlength-8);
subject = workingdir(workingdirlength-26:workingdirlength-20);
end
temptarget2 = strcat(pwd,'\..\..\..\..\analysis\',subject,'\',string(trial),'_',condition,'_kinematics_differences_adj_deg_diff_',trackorprescribe);
delete(strcat(temptarget2, '.png'));
print(tempfig4, temptarget2, '-dpng', '-r500')
disp('print 1')
% close all
end