-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_kmp_lfd.m
271 lines (212 loc) · 8.41 KB
/
demo_kmp_lfd.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
% Demonstration script for Orientation-KMP
%
% Author
% Sipu Ruan, 2023
%
% Reference (modified parts of the code)
% - PbDLib: https://gitlab.idiap.ch/rli/pbdlib-matlab/
% - Orientation-KMP: https://github.com/yanlongtu/robInfLib-matlab
close all; clear; clc;
add_paths();
addpath ../src/external/pbdlib-matlab/demos/m_fcts/
addpath ../src/external/robInfLib-matlab/fcts/
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Tunable parameters
% ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
% Number of time steps
n_step = 50;
% Number of sampled trajectories from distribution
n_sample = 5;
% Number of states in the GMM
n_state = 8;
% KMP parameters
kmp_param.lamda = 0.1; % control mean prediction
kmp_param.lamdac = 60; % control variance prediction
kmp_param.kh = 10;
% Name of the dataset
dataset_name = "lasa_handwriting/pose_data";
% dataset_name = 'panda_arm';
% Type of demonstration
demo_type = "Snake";
% demo_type = "simulation/circle";
% demo_type = "real/pouring/default";
% Scaling of via pose mean and covariance
VIA_POSE_SCALE.mean = [1e-3 * ones(3,1); 1e-4 * ones(3,1)];
VIA_POSE_SCALE.covariance = 1e-5;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data_folder = strcat("../data/", dataset_name, "/", demo_type, "/");
% Group is fixed as PCG, since translation and rotation are learned
% separately
group_name = 'PCG';
%% Load data
argin.n_step = n_step;
argin.data_folder = data_folder;
argin.group_name = group_name;
argin.align_method = "interp";
% Load and parse demo data
filenames = dir(strcat(argin.data_folder, "*.json"));
g_demo = parse_demo_trajectory(filenames, argin);
% Generate random via/goal poses
t_via = [0, 1];
trials = generate_random_trials(g_demo{1}, t_via, VIA_POSE_SCALE);
n_demo = length(g_demo);
disp("Generated random configurations!")
% Load random via/goal poses
g_via_1 = trials.g_via{1};
cov_via_1 = trials.cov_via{1};
g_via_2 = trials.g_via{2};
cov_via_2 = trials.cov_via{2};
%% Learning KMP model from demos
param.n_step = n_step;
param.kmp_param = kmp_param;
model.nbStates = n_state;
tic;
% Construct class and learn GMM/GMR model
kmp_obj = kmp(g_demo, model, param);
% Condition on goal pose
kmp_obj.compute_kmp_via_point(g_via_1, cov_via_1, t_via(1));
traj_kmp_goal = kmp_obj.get_kmp_trajectory();
g_sample_kmp_goal = kmp_obj.get_samples(traj_kmp_goal, n_sample);
traj_gmr_goal = kmp_obj.get_gmr_trajectory();
% Condition on via pose
kmp_obj.compute_kmp_via_point(g_via_2, cov_via_2, t_via(2));
traj_kmp_via = kmp_obj.get_kmp_trajectory();
g_sample_kmp_via = kmp_obj.get_samples(traj_kmp_via, n_sample);
traj_gmr_via = kmp_obj.get_gmr_trajectory();
toc;
% Extract distributions for GMR and KMP models
[kmp_mean_goal, kmp_cov_goal, kmp_var_goal] = kmp_obj.get_prob_model(traj_kmp_goal);
[kmp_mean_via, kmp_cov_via, kmp_var_via] = kmp_obj.get_prob_model(traj_kmp_via);
[gmr_mean_goal, gmr_cov_goal, gmr_var_goal] = kmp_obj.get_prob_model(traj_gmr_goal);
[gmr_mean_via, gmr_cov_via, gmr_var_via] = kmp_obj.get_prob_model(traj_gmr_via);
% Convert samples to pose
sample_kmp_struct = generate_pose_struct(g_sample_kmp_via,...
group_name);
%% Plots
model = kmp_obj.get_gmm_model();
%%%%%%%%%%
figure; hold on; axis equal;
% Demos
for i = 1:n_demo
plot3(g_demo{i}.pose(1,:), g_demo{i}.pose(2,:), g_demo{i}.pose(3,:))
end
% Via poses
plot3(g_via_1(1,4), g_via_1(2,4), g_via_1(3,4), '*', 'LineWidth', 1.5)
plot3(g_via_2(1,4), g_via_2(2,4), g_via_2(3,4), 'o', 'LineWidth', 1.5)
% GMM
plotGMM3D(model.Mu(5:7,:), model.Sigma(5:7,5:7,:), [.8 0 0], .5);
% GMR
plot3(gmr_mean_via(4,:), gmr_mean_via(5,:), gmr_mean_via(6,:), 'k--', 'LineWidth', 1.5);
% KMP
plot3(kmp_mean_via(4,:), kmp_mean_via(5,:), kmp_mean_via(6,:), 'b-', 'LineWidth', 1.5)
%%%%%%%%%%
figure; hold on; axis equal;
% Via poses
plot3(g_via_1(1,4), g_via_1(2,4), g_via_1(3,4), '*', 'LineWidth', 1.5)
plot3(g_via_2(1,4), g_via_2(2,4), g_via_2(3,4), 'o', 'LineWidth', 1.5)
% GMM
plotGMM3D(model.Mu(5:7,:), model.Sigma(5:7,5:7,:), [.8 0 0], .5);
% GMR
plot3(gmr_mean_via(4,:), gmr_mean_via(5,:), gmr_mean_via(6,:), 'k--', 'LineWidth', 1.5);
% KMP
plot3(kmp_mean_via(4,:), kmp_mean_via(5,:), kmp_mean_via(6,:), 'b-', 'LineWidth', 1.5)
plot3(kmp_mean_via(4,:) + kmp_var_via(4,:), kmp_mean_via(5, :) + kmp_var_via(5,:),...
kmp_mean_via(6,:) + kmp_var_via(6,:), 'm--', 'LineWidth', 1.5)
plot3(kmp_mean_via(4,:) - kmp_var_via(4,:), kmp_mean_via(5,:) - kmp_var_via(5,:),...
kmp_mean_via(6,:) - kmp_var_via(6,:), 'm--', 'LineWidth', 1.5)
%%%%%%%%%%
% Trajectory profile
figure;
t_steps = 0:1/(n_step-1):1;
exp_via_1 = get_exp_coord(g_via_1, group_name);
exp_via_2 = get_exp_coord(g_via_2, group_name);
% For translation part
subplot(2,1,1); hold on;
% Demos
for i = 1:n_demo
plot(t_steps, g_demo{i}.exponential(4,:),...
t_steps, g_demo{i}.exponential(5,:),...
t_steps, g_demo{i}.exponential(6,:), 'k')
end
plot(t_via(1), exp_via_1(4), '*', t_via(1), exp_via_1(5), '*',...
t_via(1), exp_via_1(6), '*')
plot(t_via(2), exp_via_2(4), 'o', t_via(2), exp_via_2(5), 'o',...
t_via(2), exp_via_2(6), 'o')
plot(t_steps, kmp_mean_via(4,:), t_steps, kmp_mean_via(5,:),...
t_steps, kmp_mean_via(6,:), 'LineWidth', 1.5)
plot(t_steps, kmp_mean_via(4,:) + kmp_var_via(4,:), 'm--',...
t_steps, kmp_mean_via(5,:) + kmp_var_via(5,:), 'm--',...
t_steps, kmp_mean_via(6,:) + kmp_var_via(6,:), 'm--',...
'LineWidth', 1.5)
plot(t_steps, kmp_mean_via(4,:) - kmp_var_via(4,:), 'm--',...
t_steps, kmp_mean_via(5,:) - kmp_var_via(5,:), 'm--',...
t_steps, kmp_mean_via(6,:) - kmp_var_via(6,:), 'm--',...
'LineWidth', 1.5)
title('Translation part')
xlabel('Time')
% For rotation part, in exponential coordinates
subplot(2,1,2); hold on;
% Demos
for i = 1:n_demo
plot(t_steps, g_demo{i}.exponential(1,:),...
t_steps, g_demo{i}.exponential(2,:),...
t_steps, g_demo{i}.exponential(3,:), 'k')
end
plot(t_via(1), exp_via_1(1), '*', t_via(1), exp_via_1(2), '*',...
t_via(1), exp_via_1(3), '*')
plot(t_via(2), exp_via_2(1), 'o', t_via(2), exp_via_2(2), 'o',...
t_via(2), exp_via_2(3), 'o')
plot(t_steps, kmp_mean_via(1,:), t_steps, kmp_mean_via(2,:),...
t_steps, kmp_mean_via(3,:), 'LineWidth', 1.5)
plot(t_steps, kmp_mean_via(1,:) + kmp_var_via(1,:), 'm--',...
t_steps, kmp_mean_via(2,:) + kmp_var_via(2,:), 'm--',...
t_steps, kmp_mean_via(3,:) + kmp_var_via(3,:), 'm--',...
'LineWidth', 1.5)
plot(t_steps, kmp_mean_via(1,:) - kmp_var_via(1,:), 'm--',...
t_steps, kmp_mean_via(2,:) - kmp_var_via(2,:), 'm--',...
t_steps, kmp_mean_via(3,:) - kmp_var_via(3,:), 'm--',...
'LineWidth', 1.5)
title('Rotation part, in so(3)')
xlabel('Time')
%%%%%%%%%%
% Trajectory profile
figure;
t_steps = 0:1/(n_step-1):1;
exp_via_1 = get_exp_coord(g_via_1, group_name);
exp_via_2 = get_exp_coord(g_via_2, group_name);
% For translation part
subplot(2,1,1); hold on;
plot(t_via(1), exp_via_1(4), '*', t_via(1), exp_via_1(5), '*',...
t_via(1), exp_via_1(6), '*')
plot(t_via(2), exp_via_2(4), 'o', t_via(2), exp_via_2(5), 'o',...
t_via(2), exp_via_2(6), 'o')
plot(t_steps, kmp_mean_via(4,:), t_steps, kmp_mean_via(5,:),...
t_steps, kmp_mean_via(6,:), 'LineWidth', 1.5)
plot(t_steps, kmp_mean_via(4,:) + kmp_var_via(4,:), 'm--',...
t_steps, kmp_mean_via(5,:) + kmp_var_via(5,:), 'm--',...
t_steps, kmp_mean_via(6,:) + kmp_var_via(6,:), 'm--',...
'LineWidth', 1.5)
plot(t_steps, kmp_mean_via(4,:) - kmp_var_via(4,:), 'm--',...
t_steps, kmp_mean_via(5,:) - kmp_var_via(5,:), 'm--',...
t_steps, kmp_mean_via(6,:) - kmp_var_via(6,:), 'm--',...
'LineWidth', 1.5)
title('Translation part')
xlabel('Time')
% For rotation part, in exponential coordinates
subplot(2,1,2); hold on;
plot(t_via(1), exp_via_1(1), '*', t_via(1), exp_via_1(2), '*',...
t_via(1), exp_via_1(3), '*')
plot(t_via(2), exp_via_2(1), 'o', t_via(2), exp_via_2(2), 'o',...
t_via(2), exp_via_2(3), 'o')
plot(t_steps, kmp_mean_via(1,:), t_steps, kmp_mean_via(2,:),...
t_steps, kmp_mean_via(3,:), 'LineWidth', 1.5)
plot(t_steps, kmp_mean_via(1,:) + kmp_var_via(1,:), 'm--',...
t_steps, kmp_mean_via(2,:) + kmp_var_via(2,:), 'm--',...
t_steps, kmp_mean_via(3,:) + kmp_var_via(3,:), 'm--',...
'LineWidth', 1.5)
plot(t_steps, kmp_mean_via(1,:) - kmp_var_via(1,:), 'm--',...
t_steps, kmp_mean_via(2,:) - kmp_var_via(2,:), 'm--',...
t_steps, kmp_mean_via(3,:) - kmp_var_via(3,:), 'm--',...
'LineWidth', 1.5)
title('Rotation part, in so(3)')
xlabel('Time')