-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTrajoptPlant.py
405 lines (341 loc) · 12.8 KB
/
TrajoptPlant.py
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import numpy as np
import random
import copy
from GRiD import RBDReference
from GRiD import URDFParser
class TrajoptPlant:
def __init__(self, integrator_type: int = 0, options = {}, need_path: bool = False):
self.validate_integrator_type(integrator_type)
self.integrator_type = integrator_type
self.set_default_options(options, need_path)
self.options = options
def validate_integrator_type(self, integrator_type: int):
if not (integrator_type in [0, 1, 2, 3, 4, -1]):
print("Invalid integrator options are [0 : euler, 1 : semi-implicit euler, 2 : midpoint, 3 : rk3, 4 : rk4, -1 : hard-coded as dynamics")
exit()
def set_default_options(self, options: dict, need_path: bool = False):
options.setdefault('path_to_urdf', None)
options.setdefault('gravity', -9.81)
if need_path and (not options.get('path_to_urdf')):
print("You must include the 'path_to_urdf' in the options.")
exit()
##############################
# Child class must implement #
##############################
def forward_dynamics(self):
raise NotImplementedError
def forward_dynamics_gradient(self):
raise NotImplementedError
def get_num_pos(self):
raise NotImplementedError
def get_num_vel(self):
raise NotImplementedError
def get_num_cntrl(self):
raise NotImplementedError
##############################
# Child class must implement #
##############################
###################################
# Placeholder Finite Diff Hessian #
###################################
def forward_dynamics_hessian(self, x: np.ndarray, u: np.ndarray):
nq = self.get_num_pos()
nv = self.get_num_vel()
nu = self.get_num_cntrl()
eps = 1e-4
hessian = np.zeros((nq,nq+nv+nu,nq+nv+nu))
for i in range(nq+nv):
x_p = copy.deepcopy(x)
x_m = copy.deepcopy(x)
x_p[i] += eps
x_m[i] -= eps
grad_p = self.forward_dynamics_gradient(x_p,u)
grad_m = self.forward_dynamics_gradient(x_m,u)
delta = grad_p - grad_m
delta /= (2*eps)
hessian[:,:,i] = np.squeeze(delta)
for i in range(nu):
u_p = copy.deepcopy(u)
u_m = copy.deepcopy(u)
u_p[i] += eps
u_m[i] -= eps
grad_p = self.forward_dynamics_gradient(x,u_p)
grad_m = self.forward_dynamics_gradient(x,u_m)
delta = grad_p - grad_m
delta /= (2*eps)
hessian[:,:,nq+nv+i] = np.squeeze(delta)
return hessian
# [ v ;
# qdd ]
def qdd_to_xdot(self, xk: np.ndarray, qdd: np.ndarray):
nq = self.get_num_pos()
nv = self.get_num_vel()
nu = self.get_num_cntrl()
return np.vstack((xk[nq:], qdd)).flatten()
# [ 0 ; eye ; 0
# dqdd/dq ; dqdd/dv ; dqdd/du ]
def dqdd_to_dxdot(self, dqdd: np.ndarray):
nq = self.get_num_pos()
nv = self.get_num_vel()
m = self.get_num_cntrl()
top = np.hstack((np.zeros((nq,nq)), np.eye(nv), np.zeros((nq,m))))
return np.vstack((top, dqdd))
# Tensor of dxdot_dq->dv->du
# [ 0 ; 0 ; 0
# dqdd/dq2 ; dqdd/dv_dq ; dqdd/du_dq ]
# [ 0 ; 0 ; 0
# dqdd/dq_dv ; dqdd/dv2 ; dqdd/du_dv ]
# [ 0 ; 0 ; 0
# dqdd/dq_du ; dqdd/dv_du ; dqdd/du2 ]
# however note that du_dv, dq_du, dv_du, du2 = 0 so we equivalently have
# [ 0 ; 0 ; 0
# dqdd/dq2 ; dqdd/dv_dq ; dqdd/du_dq ]
# [ 0 ; 0 ; 0
# dqdd/dq_dv ; dqdd/dv2 ; 0 ]
# [ 0 ; 0 ; 0
# 0 ; 0 ; 0 ]
def d2qdd_to_d2xdot_simple(self, d2qdd):
nq = self.get_num_pos()
nx = nq + self.get_num_vel()
n = nx + self.get_num_cntrl()
return np.vstack((np.zeros((nq,n,n)),d2qdd))
# dqdd_dq2, dqdd_dq_dv, dqdd_dv_dq, dqdd_dv2, dqdd_du_dq
# # first section 1 (dq)
# dq_d2_bottom = np.hstack((dqdd_dq2, dqdd_dv_dq, dqdd_du_dq))
# d2_top = np.zeros(dq_d2_bottom.shape)
# dq_d2 = np.vstack((d2_top,dq_d2_bottom))
# # then section 2 (dv)
# dv_d2_bottom = np.hstack((dqdd_dq_dv, dqdd_dv2, np.zeros(dqdd_du_dq.shape)))
# dq_d2 = np.vstack((d2_top,dv_d2_bottom))
# # then section 3 (du)
# du_d2 = np.vstack((d2_top,d2_top))
# # then tensor connect the three sections
# dx2 = np.dstack((dq_d2,dv_d2,du_d2))
return dx2
def integrator(self, xk: np.ndarray, uk: np.ndarray, dt: float, return_gradient: bool = False, return_hessian: bool = False):
n = len(xk)
if self.integrator_type == -1: # hard coded into model
if not return_gradient:
return self.integrator(xk,uk)
else:
return self.integrator_gradient(xk,uk)
if self.integrator_type == 0: # euler
# xkp1 = xk + dt * [vk,qddk]
# dxkp1 = [Ix | 0u ] + dt*[ 0q, Iv, 0u; dqdd]
qdd = self.forward_dynamics(xk,uk)
xdot = self.qdd_to_xdot(xk, qdd)
xkp1 = xk + dt*xdot
if not return_gradient and not return_hessian:
return xkp1
elif not return_hessian:
dqdd = self.forward_dynamics_gradient(xk,uk)
dxdot = self.dqdd_to_dxdot(dqdd)
A = np.eye(n) + dt*dxdot[:,0:n]
B = dt*dxdot[:,n:]
return A, B
else:
d2qdd = self.forward_dynamics_hessian(xk,uk)
dx2 = dt*self.d2qdd_to_d2xdot_simple(d2qdd)
nx = self.get_num_pos() + self.get_num_vel()
#return dxx, dux as others are zero
fxx = dx2[:,:nx,:nx]
fux = dx2[:,nx:,:nx]
return (dx2[:,:nx,:nx], dx2[:,nx:,:nx])
elif self.integrator_type == 1: # semi-implicit euler
# vkp1 = vk + dt*qddk
# qkp1 = qk + dt*vkp1
# xkp1 = [qkp1; vkp1]
# dxkp1 = [Ix | 0u ] + dt*[[0q, Iv, 0u] + dt*dqdd; dqdd]
nq = self.get_num_pos()
nv = self.get_num_vel()
nu = self.get_num_cntrl()
qdd = self.forward_dynamics(xk,uk)
vkp1 = xk[nq:] + dt*qdd
qkp1 = xk[0:nq] + dt*vkp1
if not return_gradient and not return_hessian:
return np.hstack((qkp1,vkp1)).transpose()
elif not return_hessian:
dqdd = self.forward_dynamics_gradient(xk,uk)
zIz = np.hstack((np.zeros((nq,nq)),np.eye(nq),np.zeros((nq,nu))))
Iz = np.hstack((np.eye(nq+nv),np.zeros((nq+nv,nu))))
AB = Iz + dt*np.vstack((zIz + dt*dqdd, dqdd))
return AB[:,0:nq+nv], AB[:,nq+nv:]
else:
print("[!] Error Hessians Not Implemented Yet For This Integrator!")
elif self.integrator_type == 2: # midpoint
xdot1 = self.qdd_to_xdot(xk, self.forward_dynamics(xk,uk))
midpoint = xk + 0.5*dt*xdot1
xdot2 = self.qdd_to_xdot(xk, self.forward_dynamics(midpoint,uk))
xkp1 = xk + dt*xdot2
if not return_gradient and not return_hessian:
return xkp1
elif not return_hessian:
dxdot1 = self.dqdd_to_dxdot(self.forward_dynamics_gradient(xk,uk))
A1 = np.eye(n) + 0.5*dt*dxdot1[:,0:n]
B1 = 0.5*dt*dxdot1[:,n:]
dxdot2 = self.dqdd_to_dxdot(self.forward_dynamics_gradient(midpoint,uk))
A2 = np.eye(n) + 0.5*dt*dxdot2[:,0:n]
B2 = 0.5*dt*dxdot2[:,n:]
A = np.matmul(A2,A1)
B = np.matmul(A2,B1) + B2
return A, B
else:
print("[!] Error Hessians Not Implemented Yet For This Integrator!")
elif self.integrator_type == 3: # rk3
xdot1 = self.qdd_to_xdot(xk, self.forward_dynamics(xk,uk))
point1 = xk + 0.5*dt*xdot1
xdot2 = self.qdd_to_xdot(xk, self.forward_dynamics(point1,uk))
point2 = xk + 0.75*dt*xdot2
xdot3 = self.qdd_to_xdot(xk, self.forward_dynamics(point2,uk))
xkp1 = xk + (dt/9)*(2*xdot1 + 3*xdot2 + 4*xdot3)
if not return_gradient and not return_hessian:
return xkp1
elif not return_hessian:
dxdot1 = self.dqdd_to_dxdot(self.forward_dynamics_gradient(xk,uk))
A1 = np.eye(n) + 2/9*dt*dxdot1[:,0:n]
B1 = 2/9*dt*dxdot1[:,n:]
dxdot2 = self.dqdd_to_dxdot(self.forward_dynamics_gradient(point1,uk))
A2 = np.eye(n) + 1/3*dt*dxdot2[:,0:n]
B2 = 1/3*dt*dxdot1[:,n:]
dxdot3 = self.dqdd_to_dxdot(self.forward_dynamics_gradient(point2,uk))
A3 = np.eye(n) + 4/9*dt*dxdot3[:,0:n]
B3 = 4/9*dt*dxdot1[:,n:]
A = np.matmul(A3,np.matmul(A2,A1))
B = np.matmul(A3,np.matmul(A2,B1)) + np.matmul(A3,B2) + B3
return A,B
else:
print("[!] Error Hessians Not Implemented Yet For This Integrator!")
elif self.integrator_type == 4: # rk4
xdot1 = self.qdd_to_xdot(xk, self.forward_dynamics(xk,uk))
point1 = xk + 0.5*dt*xdot1
xdot2 = self.qdd_to_xdot(xk, self.forward_dynamics(point1,uk))
point2 = xk + 0.5*dt*xdot2
xdot3 = self.qdd_to_xdot(xk, self.forward_dynamics(point2,uk))
point3 = xk + dt*xdot3
xdot4 = self.qdd_to_xdot(xk, self.forward_dynamics(point3,uk))
xkp1 = xk + (dt/6)*(xdot1 + 2*xdot2 + 2*xdot3 + xdot4)
if not return_gradient and not return_hessian:
return xkp1
elif not return_hessian:
dxdot1 = self.dqdd_to_dxdot(self.forward_dynamics_gradient(xk,uk))
A1 = np.eye(n) + 1/6*dt*dxdot1[:,0:n]
B1 = 1/6*dt*dxdot1[:,n:]
dxdot2 = self.dqdd_to_dxdot(self.forward_dynamics_gradient(point1,uk))
A2 = np.eye(n) + 1/3*dt*dxdot2[:,0:n]
B2 = 1/3*dt*dxdot1[:,n:]
dxdot3 = self.dqdd_to_dxdot(self.forward_dynamics_gradient(point2,uk))
A3 = np.eye(n) + 1/3*dt*dxdot3[:,0:n]
B3 = 1/3*dt*dxdot1[:,n:]
dxdot4 = self.dqdd_to_dxdot(self.forward_dynamics_gradient(point3,uk))
A4 = np.eye(n) + 1/6*dt*dxdot4[:,0:n]
B4 = 1/6*dt*dxdot1[:,n:]
A = np.matmul(A4,np.matmul(A3,np.matmul(A2,A1)))
B = np.matmul(A4,np.matmul(A3,np.matmul(A2,B1))) + np.matmul(A4,np.matmul(A3,B2)) + np.matmul(A4,B3) + B4
return A,B
else:
print("[!] Error Hessians Not Implemented Yet For This Integrator!")
class DoubleIntegratorPlant(TrajoptPlant):
def __init__(self, integrator_type: int = 0, options = {}):
super().__init__(integrator_type, options)
def forward_dynamics(self, x, u):
return u
def forward_dynamics_gradient(self, x, u):
return np.array([0, 0, 1])
def get_num_pos(self):
return 1
def get_num_vel(self):
return 1
def get_num_cntrl(self):
return 1
class PendulumPlant(TrajoptPlant):
def __init__(self, integrator_type = 0, options = {}):
super().__init__(integrator_type, options)
def forward_dynamics(self, x: np.ndarray, u: np.ndarray):
# m * l^2 * theta_dd + b * theta_d + m * g * l * sin(theta) = u
# assume 0 damping and m = l = 1
# theta_dd = u - g * sin(theta)
return u - 9.81 * np.sin(x[0])
def forward_dynamics_gradient(self, x: np.ndarray, u: np.ndarray):
return np.array([- 9.81 * np.cos(x[0]), 0, 1])
def get_num_pos(self):
return 1
def get_num_vel(self):
return 1
def get_num_cntrl(self):
return 1
# http://www.matthewpeterkelly.com/tutorials/cartPole/index.html
class CartPolePlant(TrajoptPlant):
def __init__(self, integrator_type = 0, options = {}):
super().__init__(integrator_type, options)
def forward_dynamics(self, x: np.ndarray, u: np.ndarray):
gravity = self.options['gravity']
# assuming m_cart = m_pole = l_pole = 1
q = x[0] # position of cart on track
theta = x[1] # angle of pole
q_d = x[2]
theta_d = x[3]
st = np.sin(theta)
ct = np.cos(theta)
LHS = np.array([[ct,1],[2, ct]])
rhs = np.array([[gravity*st],[u[0] + theta_d*theta_d*st]])
LHS_inv = np.linalg.inv(LHS)
xdd = np.matmul(LHS_inv,rhs)
return xdd.flatten()
def forward_dynamics_gradient(self, x: np.ndarray, u: np.ndarray):
gravity = self.options['gravity']
# assuming m_cart = m_pole = l_pole = 1
q = x[0] # position of cart on track
theta = x[1] # angle of pole
q_d = x[2]
theta_d = x[3]
st = np.sin(theta)
ct = np.cos(theta)
LHS = np.array([[ct,1],[2, ct]])
rhs = np.array([[gravity*st],[u[0] + theta_d*theta_d*st]])
LHS_inv = np.linalg.inv(LHS)
LHS_dtheta = np.array([[-st,0],[0, -st]])
rhs_dtheta = np.array([[gravity*ct],[theta_d*theta_d*ct]])
rhs_dtheta_d = np.array([[0],[2*theta_d*st]])
rhs_du = np.array([[0],[1]])
LHS_inv_dtheta = -np.matmul(LHS_inv,np.matmul(LHS_dtheta,LHS_inv))
result = np.zeros((2,5))
result[:,1:2] = np.matmul(LHS_inv_dtheta,rhs) + np.matmul(LHS_inv,rhs_dtheta)
result[:,3:4] = np.matmul(LHS_inv,rhs_dtheta_d)
result[:,4:5] = np.matmul(LHS_inv,rhs_du)
return result
def get_num_pos(self):
return 2
def get_num_vel(self):
return 2
def get_num_cntrl(self):
return 1
class URDFPlant(TrajoptPlant):
def __init__(self, integrator_type = 0, options = {}):
super().__init__(integrator_type, options, True)
parser = URDFParser.URDFParser()
self.robot = parser.parse(options['path_to_urdf'])
self.rbdReference = RBDReference.RBDReference(self.robot)
def forward_dynamics(self, x: np.ndarray, u: np.ndarray):
nq = self.get_num_pos()
q = x[0:nq]
qd = x[nq:]
(c, _, _, _) = self.rbdReference.rnea(q, qd, None, self.options['gravity'])
Minv = self.rbdReference.minv(q)
qdd = np.matmul(Minv,(u-c))
return qdd
def forward_dynamics_gradient(self, x: np.ndarray, u: np.ndarray):
nq = self.get_num_pos()
q = x[0:nq]
qd = x[nq:]
(c, _, _, _) = self.rbdReference.rnea(q, qd, None, self.options['gravity'])
Minv = self.rbdReference.minv(q)
qdd = np.matmul(Minv,(u-c))
dc_du = self.rbdReference.rnea_grad(q, qd, qdd, self.options['gravity'])
df_du = np.matmul(-Minv,dc_du)
return np.hstack((df_du,Minv))
def get_num_pos(self):
return self.robot.get_num_pos()
def get_num_vel(self):
return self.robot.get_num_vel()
def get_num_cntrl(self):
return self.robot.get_num_cntrl()