-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict_motion.m
141 lines (140 loc) · 5.76 KB
/
predict_motion.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
function xp = predict_motion(net, type, t, x, predInterval, seqSteps, tForceStop)
numTime = length(t);
initIdx = find(t > tForceStop, 1, 'first'); % start where force stop acting
switch type
case "dnn6"
xp = zeros(numTime, 6);
xp(1:initIdx, :) = x(1:initIdx, :);
x0 = x(initIdx, :);
t0 = t(initIdx);
for i = initIdx+1 : numTime
if (t(i)-t0) > predInterval
idx = find(t >= t0+floor(predInterval/2), 1, 'first');
x0 = x(idx, :);
t0 = t(idx);
end
xp(i,:) = predict(net, [x0, t(i)-t0]);
end
case "lstm6"
xp = zeros(numTime, 6);
xp(1:initIdx, :) = x(1:initIdx, :);
startIdx = initIdx-seqSteps+1;
x0 = {[t(startIdx:initIdx), x(startIdx:initIdx,:)]'};
t0 = t(initIdx);
for i = initIdx+1 : numTime
if (t(i)-t0) > predInterval
idx = find(t >= t0+floor(predInterval/2), 1, 'first');
startIdx = idx-seqSteps+1;
x0 = {[t(startIdx:idx), x(startIdx:idx,:)]'};
t0 = t(idx);
end
dsState = arrayDatastore(x0, 'OutputType', 'same', 'ReadSize',1);
dsTime = arrayDatastore(t(i)-t0, 'ReadSize', 1);
dsTest = combine(dsState, dsTime);
xp(i,:) = predict(net, dsTest);
end
case {"pinn6", "pirn6"}
xp = zeros(numTime, 6);
xp(1:initIdx, :) = x(1:initIdx, :);
x0 = x(initIdx, :);
t0 = t(initIdx);
for i = initIdx+1 : numTime
if (t(i)-t0 > predInterval)
idx = find(t >= t0+floor(predInterval/2), 1, 'first');
x0 = x(idx, :);
t0 = t(idx);
end
xp(i,:) = extractdata(predict(net, dlarray([x0, t(i)-t0]', 'CB')));
end
case "dnn4"
xp = zeros(numTime, 4);
xp(1:initIdx, :) = x(1:initIdx, 1:4);
x0 = x(initIdx, 1:4);
t0 = t(initIdx);
for i = initIdx+1 : numTime
if (t(i)-t0) > predInterval
idx = find(t >= t0+floor(predInterval/2), 1, 'first');
x0 = x(idx, 1:4);
t0 = t(idx);
end
xp(i,:) = predict(net, [x0, t(i)-t0]);
end
case "lstm4"
xp = zeros(numTime, 4);
xp(1:initIdx, :) = x(1:initIdx, 1:4);
startIdx = initIdx-seqSteps+1;
x0 = {[t(startIdx:initIdx), x(startIdx:initIdx,1:4)]'};
t0 = t(initIdx);
for i = initIdx+1 : numTime
if (t(i)-t0) > predInterval
idx = find(t >= t0+floor(predInterval/2), 1, 'first');
startIdx = idx-seqSteps+1;
x0 = {[t(startIdx:idx), x(startIdx:idx,1:4)]'};
t0 = t(idx);
end
dsState = arrayDatastore(x0, 'OutputType', 'same', 'ReadSize',1);
dsTime = arrayDatastore(t(i)-t0, 'ReadSize', 1);
dsTest = combine(dsState, dsTime);
xp(i,:) = predict(net, dsTest);
end
case {"pinn4", "pirn4"}
xp = zeros(numTime, 4);
xp(1:initIdx, :) = x(1:initIdx, 1:4);
x0 = x(initIdx, 1:4);
t0 = t(initIdx);
for i = initIdx+1 : numTime
if (t(i)-t0 > predInterval)
idx = find(t >= t0+floor(predInterval/2), 1, 'first');
x0 = x(idx, 1:4);
t0 = t(idx);
end
xp(i,:) = extractdata(predict(net, dlarray([x0, t(i)-t0]', 'CB')));
end
case "dnn2"
xp = zeros(numTime, 2);
xp(1:initIdx, :) = x(1:initIdx, 1:2);
x0 = x(initIdx, 1:2);
t0 = t(initIdx);
for i = initIdx+1 : numTime
if (t(i)-t0) > predInterval
idx = find(t >= t0+floor(predInterval/2), 1, 'first');
x0 = x(idx, 1:2);
t0 = t(idx);
end
xp(i,:) = predict(net, [x0, t(i)-t0]);
end
case "lstm2"
xp = zeros(numTime, 2);
xp(1:initIdx, :) = x(1:initIdx, 1:2);
startIdx = initIdx-seqSteps+1;
x0 = {[t(startIdx:initIdx), x(startIdx:initIdx,1:2)]'};
t0 = t(initIdx);
for i = initIdx+1 : numTime
if (t(i)-t0) > predInterval
idx = find(t >= t0+floor(predInterval/2), 1, 'first');
startIdx = idx-seqSteps+1;
x0 = {[t(startIdx:idx), x(startIdx:idx,1:2)]'};
t0 = t(idx);
end
dsState = arrayDatastore(x0, 'OutputType', 'same', 'ReadSize',1);
dsTime = arrayDatastore(t(i)-t0, 'ReadSize', 1);
dsTest = combine(dsState, dsTime);
xp(i,:) = predict(net, dsTest);
end
case {"pinn2", "pirn2"}
xp = zeros(numTime, 2);
xp(1:initIdx, :) = x(1:initIdx, 1:2);
x0 = x(initIdx, 1:2);
t0 = t(initIdx);
for i = initIdx+1 : numTime
if (t(i)-t0 > predInterval)
idx = find(t >= t0+floor(predInterval/2), 1, 'first');
x0 = x(idx, 1:2);
t0 = t(idx);
end
xp(i,:) = extractdata(predict(net, dlarray([x0, t(i)-t0]', 'CB')));
end
otherwise
disp("unspecified type of model");
end
end