forked from tony950309/state-estimator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenjamin2011.m
More file actions
302 lines (267 loc) · 9.2 KB
/
Benjamin2011.m
File metadata and controls
302 lines (267 loc) · 9.2 KB
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
%% state estimator (One DOF Inverted Pendulum model)
% For MSc project on state estimation for humanoid robot,the basic approach
% that simplifies the humanoid robot to an inverted pendulum model, based
% on the paper ---¡¶State Estimation for Force-Controlled Humanoid Balance
% using Simple Models in the Presence of Modeling Error¡·
% --- Benjamin J. Stephens 2011.
% By Jiacheng Gu on 2018/3/13
% ** means parameters need to be tuned
%% Initilization
clear;
clc;
close all;
NumOfStep = 1:800;
T = 25e-4; % time step f = 400Hz
t = T * NumOfStep;
g = 9.81; % gravity
z_0 = 0.6; % height of COM (centre of mass)
omega = sqrt(g / z_0); % w
% **
u_x = 0.0; % ** u_x is an unknown external force (under Eq11)
x_offset = 0.02; % ** x_offset is an unknown offset (under Eq9)
alpha = 0.01; % ** amplitude of the known ground truth state
% trajectories generated by sinusoids
beta = 4 * pi; % ** frequency
% **
% state trajectory
x = alpha * sin( beta * t); % COM position (Eq22)
% x = normrnd(x,1e-8);
dx = alpha * beta * cos(beta * t); % d(x) velocity (Eq23)
% dx = normrnd(dx,1e-4);
% position of COP (Eq24)
x_c = x + 1 / omega^2 * (alpha * beta^2 * sin(beta * t) + u_x);
% x_c = normrnd(x_c,1e-8);
% COM measurement , y is the vector of measurement
y1 = x-x_offset; % measured position of COM (Eq25)
% y1 = normrnd(y1,1e-8);
y2 = x_c; % measured position of COP (Eq26)
% y2 = normrnd(y2,1e-4);
%% Naive Estimator (under EQ26)
A0 = [1 T 0;
omega^2 * T 1 -omega^2 * T; % Eq3
0 0 1];
B0 = [0 0 T]'; % Eq4
C0_1 = [1 0 0]; % Eq6
C0_2 = [0 0 1]; % Eq7
x0 = [0 0 0]'; % original x
u = diff(x_c)/T; % uk is input on timestep k
u = [u, u(end)]; % here u is a vector wrt time t
Q = eye(3); % ** noise level in system dynamics
Q(1, 1) = 1e-8;
Q(2, 2) = 1e-4;
Q(3, 3) = 1e-4;
isUseCOP = 0; % 0 means not using COP, position only
R = eye(isUseCOP+1); % ** noise level in measurement(table 2)
R(isUseCOP+1,isUseCOP+1) = 1e-5;
R(1,1) = 1e-5;
if isUseCOP
y_S1 = [y1; y2]; %#ok<UNRCH>
C0 = [C0_1;C0_2];
else
y_S1 = y1;
C0 = C0_1;
end
[x_hat_array, ~, ~] = myEstimator(A0,B0,C0,u,y_S1,x0,Q,R);
x_Naive_estimated_S1 = x_hat_array(1,:);
dx_Naive_estimated_S1 = x_hat_array(2,:);
x_c_Naive_estimated_S1 = x_hat_array(3,:);
isUseCOP = 1; % 1 means using COP,both COP and position
R = eye(isUseCOP+1); % ** noise level in measurement(table 2)
R(isUseCOP+1,isUseCOP+1) = 1e-5;
R(1,1) = 1e-5;
if isUseCOP
y = [y1; y2];
C0 = [C0_1;C0_2];
else
y = y1; %#ok<UNRCH>
C0 = C0_1;
end
[x_hat_array, ~, ~] = myEstimator(A0,B0,C0,u,y,x0,Q,R);
x_Naive_estimated_S12 = x_hat_array(1,:);
dx_Naive_estimated_S12 = x_hat_array(2,:);
x_c_Naive_estimated_S12 = x_hat_array(3,:);
%% plot Naive estimator
% t = 1:length(t);
figure('units','normalized','outerposition',[0 0 1 1],'name','Naive State estimation')
subplot(4,1,1)
plot(t, x,'b')
hold on
grid on
plot(t, x_Naive_estimated_S1,'r--','linewidth',2)
plot(t, x_Naive_estimated_S12,'k:','linewidth',2)
legend('Ground Truth Position', 'S1', 'S12')
title('Position')
subplot(4,1,2)
plot(t, dx,'b')
hold on
grid on
plot(t, dx_Naive_estimated_S1,'r--','linewidth',2)
plot(t, dx_Naive_estimated_S12,'k:','linewidth',2)
legend('Ground Truth velocity', 'S1','S12')
title('Velocity')
subplot(4,1,3)
plot(t, x_c,'b')
hold on
grid on
plot(t, x_c_Naive_estimated_S1,'r--','linewidth',2)
plot(t, x_c_Naive_estimated_S12,'k:','linewidth',2)
legend('Ground Truth COP', 'S1','S12')
title('COP')
%% COM offset estimator
A1 = [1 T 0 0;
omega^2 * T 1 -omega^2 * T 0; % Eq10
0 0 1 0;
0 0 0 1];
B1 = [0 0 T 0]'; % Eq4
C1_1 = [1 0 0 1]; % Eq18
C1_2 = [0 0 1 1]; % Eq7
x0 = [0 0 0 0]'; % original x
u = diff(x_c)/T; % uk is input on timestep k
u = [u, u(end)]; % here u is a vector wrt time t
Q = 1e-8; % ** noise level in system dynamics
isUseCOP = 0; % 0 means not using COP, position only
R = eye(isUseCOP+1); % ** noise level in measurement(table 2)
R(isUseCOP+1,isUseCOP+1) = 1e-5;
R(1,1) = 1e-5;
if isUseCOP
y_S1 = [y1; y2]; %#ok<UNRCH>
C1 = [C1_1;C1_2];
else
y_S1 = y1;
C1 = C1_1;
end
[x_hat_array, ~, ~] = myEstimator(A1,B1,C1,u,y_S1,x0,Q,R);
x_COM_estimated_S1 = x_hat_array(1,:);
dx_COM_estimated_S1 = x_hat_array(2,:);
x_c_COM_estimated_S1 = x_hat_array(3,:);
delta_x_COM_estimated_S1 = x_hat_array(4,:);
isUseCOP = 1; % 1 means using COP,both COP and position
R = eye(isUseCOP+1); % ** noise level in measurement(table 2)
R(isUseCOP+1,isUseCOP+1) = 1e-5;
R(1,1) = 1e-5;
if isUseCOP
y = [y1; y2];
C1 = [C1_1;C1_2];
else
y = y1; %#ok<UNRCH>
C1 = C1_1;
end
[x_hat_array, ~, ~] = myEstimator(A1,B1,C1,u,y,x0,Q,R);
x_COM_estimated_S12 = x_hat_array(1,:);
dx_COM_estimated_S12 = x_hat_array(2,:);
x_c_COM_estimated_S12 = x_hat_array(3,:);
delta_x_COM_estimated_S12 = x_hat_array(4,:);
%% plot COM offset estimator
% t = 1:length(t);
figure('units','normalized','outerposition',[0 0 1 1],'name','Naive State estimation')
subplot(4,1,1)
plot(t, x,'b')
hold on
grid on
plot(t, x_COM_estimated_S1,'r--','linewidth',2)
plot(t, x_COM_estimated_S12,'k:','linewidth',2)
legend('Ground Truth Position', 'S1', 'S12')
title('Position')
subplot(4,1,2)
plot(t, dx,'b')
hold on
grid on
plot(t, dx_COM_estimated_S1,'r--','linewidth',2)
plot(t, dx_COM_estimated_S12,'k:','linewidth',2)
legend('Ground Truth velocity', 'S1','S12')
title('Velocity')
subplot(4,1,3)
plot(t, x_c,'b')
hold on
grid on
plot(t, x_c_COM_estimated_S1,'r--','linewidth',2)
plot(t, x_c_COM_estimated_S12,'k:','linewidth',2)
legend('Ground Truth COP', 'S1','S12')
title('COP')
subplot(4,1,4)
plot(t, x_offset * ones(length(t)),'b')
hold on
grid on
plot(t, delta_x_COM_estimated_S1,'r--','linewidth',2)
plot(t, delta_x_COM_estimated_S12,'k:','linewidth',2)
legend('Ground Truth COM offset', 'S1','S12')
title('COM offset')
%% External force estimator
A2 = [1 T 0 0;
omega^2 * T 1 -omega^2 * T T; % Eq10
0 0 1 0;
0 0 0 1];
B2 = [0 0 T 0]'; % Eq4
C2_1 = [1 0 0 0]; % Eq19
C2_2 = [0 0 1 0]; % Eq20
x0 = [0 0 0 0]'; % original x
u = diff(x_c)/T; % uk is input on timestep k
u = [u, u(end)]; % here u is a vector wrt time t
Q = 1e-8; % ** noise level in system dynamics
isUseCOP = 0; % 0 means not using COP, position only
R = eye(isUseCOP+1); % ** noise level in measurement(table 2)
R(isUseCOP+1,isUseCOP+1) = 1e-5;
R(1,1) = 1e-5;
if isUseCOP
y_S1 = [y1; y2]; %#ok<UNRCH>
C2 = [C2_1;C2_2];
else
y_S1 = y1;
C2 = C2_1;
end
[x_hat_array, ~, ~] = myEstimator(A2,B2,C2,u,y_S1,x0,Q,R);
x_force_estimated_S1 = x_hat_array(1,:);
dx_force_estimated_S1 = x_hat_array(2,:);
x_c_force_estimated_S1 = x_hat_array(3,:);
delta_x_force_estimated_S1 = x_hat_array(4,:);
isUseCOP = 1; % 1 means using COP,both COP and position
R = eye(isUseCOP+1); % ** noise level in measurement(table 2)
R(isUseCOP+1,isUseCOP+1) = 1e-5;
R(1,1) = 1e-5;
if isUseCOP
y_S1 = [y1; y2];
C2 = [C2_1;C2_2];
else
y_S1 = y1; %#ok<UNRCH>
C2 = C2_1;
end
[x_hat_array, ~, ~] = myEstimator(A2,B2,C2,u,y_S1,x0,Q,R);
x_force_estimated_S12 = x_hat_array(1,:);
dx_force_estimated_S12 = x_hat_array(2,:);
x_c_force_estimated_S12 = x_hat_array(3,:);
delta_x_force_estimated_S12 = x_hat_array(4,:);
%% plot External force estimator
% t = 1:length(t);
figure('units','normalized','outerposition',[0 0 1 1],'name','Naive State estimation')
subplot(4,1,1)
plot(t, x,'b')
hold on
grid on
plot(t, x_force_estimated_S1,'r--','linewidth',2)
plot(t, x_force_estimated_S12,'k:','linewidth',2)
legend('Ground Truth Position', 'S1', 'S12')
title('Position')
subplot(4,1,2)
plot(t, dx,'b')
hold on
grid on
plot(t, dx_force_estimated_S1,'r--','linewidth',2)
plot(t, dx_force_estimated_S12,'k:','linewidth',2)
legend('Ground Truth velocity', 'S1','S12')
title('Velocity')
subplot(4,1,3)
plot(t, x_c,'b')
hold on
grid on
plot(t, x_c_force_estimated_S1,'r--','linewidth',2)
plot(t, x_c_force_estimated_S12,'k:','linewidth',2)
legend('Ground Truth COP', 'S1','S12')
title('COP')
subplot(4,1,4)
plot(t, u_x * ones(length(t)),'b')
hold on
grid on
plot(t, delta_x_force_estimated_S1,'r--','linewidth',2)
plot(t, delta_x_force_estimated_S12,'k:','linewidth',2)
legend('Ground Truth force', 'S1','S12')
title('force')