-
Notifications
You must be signed in to change notification settings - Fork 4
/
demo_car_CDDP.m
280 lines (222 loc) · 7.47 KB
/
demo_car_CDDP.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
function demo_car
% A demo of iLQG/DDP with car-parking dynamics
clc;
close all
fprintf(['\nA This is simple DDP"\n'])
full_DDP = false;
% set up the optimization problem
DYNCST = @(x,u,i) car_dyn_cst(x,u,full_DDP);
CONST = @(x) car_constraint(x);
T = 500; % horizon
x0 = [1;1;pi*3/2;0]; % initial state
u0 = .1*randn(2,T); % initial controls
Op.plot = 0; % plot the derivatives as well
Op.maxIter = 500;
% prepare the visualization window and graphics callback
figure(9);
set(gcf,'name','car parking','Menu','none','NumberT','off')
set(gca,'xlim',[-4 4],'ylim',[-4 4],'DataAspectRatio',[1 1 1])
grid on
box on
% plot target configuration with light colors
handles = car_plot([0 0 0 0]', [0 0]');
fcolor = get(handles,'facecolor');
ecolor = get(handles,'edgecolor');
fcolor = cellfun(@(x) (x+3)/4,fcolor,'UniformOutput',false);
ecolor = cellfun(@(x) (x+3)/4,ecolor,'UniformOutput',false);
set(handles, {'facecolor','edgecolor'}, [fcolor ecolor])
% prepare and install trajectory visualization callback
line_handle = line([0 0],[0 0],'color','b','linewidth',2);
plotFn = @(x) set(line_handle,'Xdata',x(1,:),'Ydata',x(2,:));
Op.plotFn = plotFn;
[x,u]= iLQG_C(DYNCST, CONST, x0, u0, Op);
% animate the resulting trajectory
figure(9)
handles = [];
for i=1:T
set(0,'currentfigure',9);
delete(handles)
handles = car_plot(x(:,i), u(:,i));
drawnow
end
function y = car_dynamics(x,u)
% === states and controls:
% x = [x y t v]' = [x; y; car_angle; front_wheel_velocity]
% u = [w a]' = [front_wheel_angle; acceleration]
% constants
d = 2.0; % d = distance between back and front axles
h = 0.03; % h = timestep (seconds)
% controls
w = u(1,:,:); % w = front wheel angle
a = u(2,:,:); % a = front wheel acceleration
o = x(3,:,:); % o = car angle
% z = unit_vector(o)
z = [cos(o); sin(o)];
v = x(4,:,:); % v = front wheel velocity
f = h*v; % f = front wheel rolling distance
% b = back wheel rolling distance
b = d + f.*cos(w) - sqrt(d^2 - (f.*sin(w)).^2);
% do = change in car angle
do = asin(sin(w).*f/d);
dy = [tt(b, z); do; h*a]; % change in state
y = x + dy; % new state
function d = car_constraint(x)
d = 1-(x(1)+2)^2-(x(2)+3)^2;
function c = car_cost(x, u)
% cost function for car-parking problem
% sum of 3 terms:
% lu: quadratic cost on controls
% lf: final cost on distance from target parking configuration
% lx: running cost on distance from origin to encourage tight turns
final = isnan(u(1,:));
u(:,final) = 0;
cu = 1e-2*[1 .01]; % control cost coefficients
cf = [ .1 .1 1 .3]; % final cost coefficients
pf = [.01 .01 .01 1]'; % smoothness scales for final cost
cx = 1e-3*[1 1]; % running cost coefficients
px = [.1 .1]'; % smoothness scales for running cost
% control cost
lu = cu*u.^2;
% final cost
if any(final)
llf = cf*sabs(x(:,final),pf);
lf = double(final);
lf(final)= llf;
else
lf = 0;
end
% running cost
lx = cx*sabs(x(1:2,:),px);
% total cost
c = lu + lx + lf;
function y = sabs(x,p)
% smooth absolute-value function (a.k.a pseudo-Huber)
y = pp( sqrt(pp(x.^2,p.^2)), -p);
function [f,c,fx,fu,fxx,fxu,fuu,cx,cu,cxx,cxu,cuu] = car_dyn_cst(x,u,full_DDP)
% combine car dynamics and cost
% use helper function finite_difference() to compute derivatives
if nargout == 2
f = car_dynamics(x,u);
c = car_cost(x,u);
else
% state and control indices
ix = 1:4;
iu = 5:6;
% dynamics first derivatives
xu_dyn = @(xu) car_dynamics(xu(ix,:),xu(iu,:));
J = finite_difference(xu_dyn, [x; u]);
fx = J(:,ix,:);
fu = J(:,iu,:);
% dynamics second derivatives
if full_DDP
xu_Jcst = @(xu) finite_difference(xu_dyn, xu);
JJ = finite_difference(xu_Jcst, [x; u]);
JJ = reshape(JJ, [4 6 size(J)]);
JJ = 0.5*(JJ + permute(JJ,[1 3 2 4])); %symmetrize
fxx = JJ(:,ix,ix,:);
fxu = JJ(:,ix,iu,:);
fuu = JJ(:,iu,iu,:);
else
[fxx,fxu,fuu] = deal([]);
end
% cost first derivatives
xu_cost = @(xu) car_cost(xu(ix,:),xu(iu,:));
J = squeeze(finite_difference(xu_cost, [x; u]));
cx = J(ix,:);
cu = J(iu,:);
% cost second derivatives
xu_Jcst = @(xu) squeeze(finite_difference(xu_cost, xu));
JJ = finite_difference(xu_Jcst, [x; u]);
JJ = 0.5*(JJ + permute(JJ,[2 1 3])); %symmetrize
cxx = JJ(ix,ix,:);
cxu = JJ(ix,iu,:);
cuu = JJ(iu,iu,:);
[f,c] = deal([]);
end
function J = finite_difference(fun, x, h)
% simple finite-difference derivatives
% assumes the function fun() is vectorized
if nargin < 3
h = 2^-17;
end
[n, K] = size(x);
H = [zeros(n,1) h*eye(n)];
H = permute(H, [1 3 2]);
X = pp(x, H);
X = reshape(X, n, K*(n+1));
Y = fun(X);
m = numel(Y)/(K*(n+1));
Y = reshape(Y, m, K, n+1);
J = pp(Y(:,:,2:end), -Y(:,:,1)) / h;
J = permute(J, [1 3 2]);
% ======== graphics functions ========
function h = car_plot(x,u)
body = [0.9 2.1 0.3]; % body = [width length curvature]
bodycolor = 0.5*[1 1 1];
headlights = [0.25 0.1 .1 body(1)/2]; % headlights [width length curvature x]
lightcolor = [1 1 0];
wheel = [0.15 0.4 .06 1.1*body(1) -1.1 .9]; % wheels = [width length curvature x yb yf]
wheelcolor = 'k';
h = [];
% make wheels
for front = 1:2
for right = [-1 1]
h(end+1) = rrect(wheel,wheelcolor)'; %#ok<AGROW>
if front == 2
twist(h(end),0,0,u(1))
end
twist(h(end),right*wheel(4),wheel(4+front))
end
end
% make body
h(end+1) = rrect(body,bodycolor);
% make window (hard coded)
h(end+1) = patch([-.8 .8 .7 -.7],.6+.3*[1 1 -1 -1],'w');
% headlights
h(end+1) = rrect(headlights(1:3),lightcolor);
twist(h(end),headlights(4),body(2)-headlights(2))
h(end+1) = rrect(headlights(1:3),lightcolor);
twist(h(end),-headlights(4),body(2)-headlights(2))
% put rear wheels at (0,0)
twist(h,0,-wheel(5))
% align to x-axis
twist(h,0,0,-pi/2)
% make origin (hard coded)
ol = 0.1;
ow = 0.01;
h(end+1) = patch(ol*[-1 1 1 -1],ow*[1 1 -1 -1],'k');
h(end+1) = patch(ow*[1 1 -1 -1],ol*[-1 1 1 -1],'k');
viscircles([-2 -3],1);
twist(h,x(1),x(2),x(3))
function twist(obj,x,y,theta)
% a planar twist: rotate object by theta, then translate by (x,y)
i = 1i;
if nargin == 3
theta = 0;
end
for h = obj
Z = get(h,'xdata') + i*get(h,'ydata');
Z = Z * exp(i*theta);
Z = Z + (x + i*y);
set(h,'xdata',real(Z),'ydata',imag(Z));
end
function h = rrect(wlc, color)
% draw a rounded rectangle (using complex numbers and a kronecker sum :-)
N = 25; % number of points per corner
width = wlc(1);
length = wlc(2);
curve = wlc(3);
a = linspace(0,2*pi,4*N);
circle = curve*exp(1i*a);
width = width-curve;
length = length-curve;
rect1 = diag(width*[1 -1 -1 1] + 1i*length *[1 1 -1 -1]);
rectN = sum(kron(rect1, ones(1,N)), 1) ;
rr = circle + rectN;
rr = [rr rr(1)]; % close the curve
h = patch(real(rr),imag(rr),color);
% utility functions: singleton-expanded addition and multiplication
function c = pp(a,b)
c = bsxfun(@plus,a,b);
function c = tt(a,b)
c = bsxfun(@times,a,b);