-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest_simulator_instance.py
More file actions
586 lines (520 loc) · 20.1 KB
/
Copy pathtest_simulator_instance.py
File metadata and controls
586 lines (520 loc) · 20.1 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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
import pinocchio as pin
import simple
import numpy as np
from hppfcl import (
Halfspace,
Sphere,
HeightFieldAABB,
CollisionRequest,
CollisionResult,
collide,
)
try:
import example_robot_data as erd
has_erd = True
except:
print("Cannot find example robot data. Skipping simulator tests.")
has_erd = False
def setBallsAndHalfSpace(length=[0.2], mass=[1.0]):
assert len(length) == len(mass) or len(length) == 1 or len(mass) == 1
N = max(len(length), len(mass))
if len(length) == 1:
length = length * N
if len(mass) == 1:
mass = mass * N
model = pin.Model()
geom_model = pin.GeometryModel()
# create plane for floor
n = np.array([0.0, 0.0, 1])
plane_shape = Halfspace(n, 0)
T = pin.SE3(np.eye(3), np.zeros(3))
plane = pin.GeometryObject("plane", 0, 0, T, plane_shape)
plane.meshColor = np.array([0.5, 0.5, 0.5, 1.0])
plane_id = geom_model.addGeometryObject(plane)
ball_ids = []
for n_ball in range(N):
a = length[n_ball]
m = mass[n_ball]
freeflyer = pin.JointModelFreeFlyer()
joint = model.addJoint(0, freeflyer, pin.SE3.Identity(), "ball_" + str(n_ball))
model.appendBodyToJoint(
joint, pin.Inertia.FromSphere(m, a / 2), pin.SE3.Identity()
)
ball_shape = Sphere(a / 2)
geom_ball = pin.GeometryObject(
"ball_" + str(n_ball), joint, joint, pin.SE3.Identity(), ball_shape
)
geom_ball.meshColor = np.array([1.0, 0.2, 0.2, 1.0])
ball_id = geom_model.addGeometryObject(geom_ball)
for id in ball_ids:
col_pair = pin.CollisionPair(id, ball_id)
geom_model.addCollisionPair(col_pair)
ball_ids += [ball_id]
col_pair = pin.CollisionPair(plane_id, ball_id)
geom_model.addCollisionPair(col_pair)
model.qref = pin.neutral(model)
model.qinit = model.qref.copy()
for n_ball in range(N):
model.qinit[7 * n_ball] += a
model.qinit[7 * n_ball + 2] += 0.1
data = model.createData()
geom_data = geom_model.createData()
for req in geom_data.collisionRequests:
req.security_margin = 1e-3
return model, data, geom_model, geom_data
def test_init():
model = pin.Model()
geom_model = pin.GeometryModel()
data = model.createData()
geom_data = geom_model.createData()
sim = simple.Simulator(model, data, geom_model, geom_data)
assert True
def test_init2():
model = pin.Model()
geom_model = pin.GeometryModel()
sim = simple.Simulator(model, geom_model)
assert True
def test_simulator_handles():
model = pin.Model()
geom_model = pin.GeometryModel()
data = model.createData()
geom_data = geom_model.createData()
sim = simple.Simulator(model, data, geom_model, geom_data)
model2 = pin.buildSampleModelHumanoid()
model2.lowerPositionLimit = -np.ones((model2.nq, 1))
model2.upperPositionLimit = np.ones((model2.nq, 1))
geom_model2 = pin.buildSampleGeometryModelHumanoid(model2)
data2 = model2.createData()
geom_data2 = geom_model2.createData()
sim = simple.Simulator(model2, data2, geom_model2, geom_data2)
q = pin.randomConfiguration(model2)
pin.updateGeometryPlacements(sim.model, sim.data, sim.geom_model, sim.geom_data, q)
assert sim.model == model2
assert not (sim.model == model)
assert sim.geom_model == geom_model2
assert not (sim.geom_model == geom_model)
assert sim.geom_data == geom_data2
assert not (sim.geom_data == geom_data)
model3 = model2.copy()
freeflyer = pin.JointModelFreeFlyer()
joint = model2.addJoint(0, freeflyer, pin.SE3.Identity(), "ball_0")
assert sim.model.njoints == model3.njoints + 1
gemo_model3 = geom_model2.copy()
ball_shape = Sphere(0.1 / 2)
geom_ball = pin.GeometryObject(
"ball_0", joint, joint, pin.SE3.Identity(), ball_shape
)
ball_id = geom_model2.addGeometryObject(geom_ball)
assert sim.geom_model.ngeoms == gemo_model3.ngeoms + 1
def test_init_empty():
model = pin.Model()
geom_model = pin.GeometryModel()
data = model.createData()
geom_data = geom_model.createData()
sim = simple.Simulator(model, data, geom_model, geom_data)
q = pin.neutral(model)
v = np.zeros(model.nv)
tau = np.zeros(model.nv)
dt = 1e-3
sim.step(q, v, tau, dt)
assert True
def test_step():
model = pin.buildSampleModelManipulator()
geom_model = pin.buildSampleGeometryModelManipulator(model)
data = model.createData()
geom_data = geom_model.createData()
sim = simple.Simulator(model, data, geom_model, geom_data)
q = pin.randomConfiguration(model)
v = np.random.random(model.nv)
tau = np.random.random(model.nv)
dt = 1e-3
sim.step(q, v, tau, dt)
fext = [pin.Force(np.random.random(6)) for i in range(model.njoints)]
sim.step(q, v, tau, fext, dt)
assert True
def test_step_balls():
mass = [12.0]
length = [1.5]
model, data, geom_model, geom_data = setBallsAndHalfSpace(length, mass)
q = pin.neutral(model)
q[2] = length[0] / 2.0 + 1.0
v = np.zeros(model.nv)
tau = np.zeros(model.nv)
dt = 1e-3
sim = simple.Simulator(model, data, geom_model, geom_data)
sim.step(q, v, tau, dt)
assert sim.constraints_problem.getNumberOfContacts() == 0
q[2] = 0.0
v = np.zeros(model.nv)
sim.step(q, v, tau, dt)
assert sim.constraints_problem.getNumberOfContacts() == 1
assert np.linalg.norm(sim.vnew) < 1e-6
mass = [12.0, 0.1]
length = [1.5, 0.5]
model, data, geom_model, geom_data = setBallsAndHalfSpace(length, mass)
sim = simple.Simulator(model, data, geom_model, geom_data)
q = pin.neutral(model)
q[0] = 0.0
q[7] = length[1] / 2.0 + 1.0
q[2] = 0.0
q[9] = 0.0
v = np.zeros(model.nv)
tau = np.zeros(model.nv)
sim.step(q, v, tau, dt)
assert np.linalg.norm(sim.vnew) < 1e-6
num_contacts = sim.constraints_problem.getNumberOfContacts()
assert num_contacts == 2
lam = sim.constraints_problem.frictional_point_constraints_forces()[
: 3 * num_contacts
]
assert len(lam) == 3 * num_contacts
sim.step(q, v, tau, dt)
assert True
def test_step_balls_with_constraints():
nballs = 2
mass = [23.0] * nballs
length = [2.0] * nballs
model, data, geom_model, geom_data = setBallsAndHalfSpace(length, mass)
model.lowerPositionLimit = np.ones(model.nq) * -100.0
model.upperPositionLimit = np.ones(model.nq) * 100.0
q0 = pin.neutral(model)
for i in range(nballs):
q0[7 * i] = i * length[i] * 1.5
q0[7 * i + 2] = 10.0
q = q0.copy()
v = np.zeros(model.nv)
tau = np.zeros(model.nv)
dt = 1e-3
sim = simple.Simulator(model, data, geom_model, geom_data)
sim.step(q, v, tau, dt)
vnew = v + dt * pin.aba(model, data, q, v, tau)
assert sim.constraints_problem.constraints_problem_size() == nballs * 3 * 2
assert np.linalg.norm(sim.vnew - vnew) < 1e-6
model, data, geom_model, geom_data = setBallsAndHalfSpace(length, mass)
model.lowerPositionLimit = np.ones(model.nq) * -100.0
model.upperPositionLimit = np.ones(model.nq) * 100.0
for i in range(nballs // 2):
model.lowerPositionLimit[7 * i + 2] = 10.0
q = q0.copy()
v = np.zeros(model.nv)
tau = np.zeros(model.nv)
dt = 1e-3
sim = simple.Simulator(model, data, geom_model, geom_data)
sim.step(q, v, tau, dt)
vnew = v + dt * pin.aba(model, data, q, v, tau)
assert sim.constraints_problem.constraints_problem_size() == nballs * 3 * 2
assert (
np.linalg.norm(sim.vnew[(nballs // 2) * 6 :] - vnew[(nballs // 2) * 6 :]) < 1e-6
)
assert np.linalg.norm(sim.vnew[: (nballs // 2) * 6]) < 1e-6
def addSystemCollisionPairs(model, geom_model, qref):
"""
Add the right collision pairs of a model, given qref.
qref is here as a `T-pose`. The function uses this pose to determine which objects are in collision
in this ref pose. If objects are in collision, they are not added as collision pairs, as they are considered
to always be in collision.
"""
data = model.createData()
geom_data = geom_model.createData()
pin.updateGeometryPlacements(model, data, geom_model, geom_data, qref)
geom_model.removeAllCollisionPairs()
num_col_pairs = 0
for i in range(len(geom_model.geometryObjects)):
for j in range(i + 1, len(geom_model.geometryObjects)):
# Don't add collision pair if same object
if i != j:
gobj_i: pin.GeometryObject = geom_model.geometryObjects[i]
gobj_j: pin.GeometryObject = geom_model.geometryObjects[j]
if gobj_i.name == "floor" or gobj_j.name == "floor":
num_col_pairs += 1
col_pair = pin.CollisionPair(i, j)
geom_model.addCollisionPair(col_pair)
else:
if gobj_i.parentJoint != gobj_j.parentJoint:
# Compute collision between the geometries. Only add the collision pair if there is no collision.
M1 = geom_data.oMg[i]
M2 = geom_data.oMg[j]
colreq = CollisionRequest()
colreq.security_margin = 1e-2 # 1cm of clearance
colres = CollisionResult()
collide(
gobj_i.geometry, M1, gobj_j.geometry, M2, colreq, colres
)
if not colres.isCollision():
num_col_pairs += 1
col_pair = pin.CollisionPair(i, j)
geom_model.addCollisionPair(col_pair)
def addFloor(geom_model: pin.GeometryModel):
floor_collision_shape = Halfspace(0, 0, 1, 0)
M = pin.SE3.Identity()
floor_collision_object = pin.GeometryObject("floor", 0, 0, M, floor_collision_shape)
geom_model.addGeometryObject(floor_collision_object)
def test_manipulator_limits():
model = pin.buildSampleModelManipulator()
geom_model = pin.buildSampleGeometryModelManipulator(model)
q0 = pin.neutral(model)
model.lowerPositionLimit = q0.copy()
model.upperPositionLimit = q0.copy()
sim = simple.Simulator(model, geom_model)
v = np.zeros(model.nv)
tau = np.zeros(model.nv)
dt = 1e-3
sim.step(q0, v, tau, dt)
assert sim.constraints_problem.getNumberOfContacts() == 0
assert sim.constraints_problem.constraints_problem_size() == 2 * model.nq
assert np.linalg.norm(sim.vnew) < 1e-6
def test_humanoid_limits():
model = pin.buildSampleModelHumanoid()
geom_model = pin.buildSampleGeometryModelHumanoid(model)
q0 = pin.neutral(model)
model.lowerPositionLimit = q0.copy()
model.upperPositionLimit = q0.copy()
sim = simple.Simulator(model, geom_model)
v = np.zeros(model.nv)
tau = np.zeros(model.nv)
dt = 1e-3
sim.step(q0, v, tau, dt)
assert sim.constraints_problem.getNumberOfContacts() == 0
assert sim.constraints_problem.constraints_problem_size() == 2 * (model.nq - 4)
assert sim.admm_constraint_solver.getAbsoluteConvergenceResidual() < 1e-6
def test_freeflyer_limits():
model = pin.Model()
geom_model = pin.GeometryModel()
a = 0.1
m = 3.8
freeflyer = pin.JointModelFreeFlyer()
joint = model.addJoint(0, freeflyer, pin.SE3.Identity(), "ball")
model.appendBodyToJoint(joint, pin.Inertia.FromSphere(m, a / 2), pin.SE3.Identity())
ball_shape = Sphere(a / 2)
geom_ball = pin.GeometryObject("ball", joint, joint, pin.SE3.Identity(), ball_shape)
geom_ball.meshColor = np.array([1.0, 0.2, 0.2, 1.0])
ball_id = geom_model.addGeometryObject(geom_ball)
q0 = pin.neutral(model)
model.lowerPositionLimit = q0.copy()
model.upperPositionLimit = q0.copy()
sim = simple.Simulator(model, geom_model)
v = np.zeros(model.nv)
tau = np.zeros(model.nv)
dt = 1e-3
sim.step(q0, v, tau, dt)
assert sim.constraints_problem.getNumberOfContacts() == 0
assert sim.constraints_problem.constraints_problem_size() == 2 * (model.nq - 4)
assert np.linalg.norm(sim.vnew) < 1e-6
def test_composite_limits():
model = pin.Model()
geom_model = pin.GeometryModel()
a = 0.1
m = 3.8
composite = pin.JointModelComposite(2)
composite.addJoint(pin.JointModelRX())
composite.addJoint(pin.JointModelRY())
joint = model.addJoint(0, composite, pin.SE3.Identity(), "ball")
model.appendBodyToJoint(joint, pin.Inertia.FromSphere(m, a / 2), pin.SE3.Identity())
ball_shape = Sphere(a / 2)
geom_ball = pin.GeometryObject("ball", joint, joint, pin.SE3.Identity(), ball_shape)
geom_ball.meshColor = np.array([1.0, 0.2, 0.2, 1.0])
ball_id = geom_model.addGeometryObject(geom_ball)
q0 = pin.neutral(model)
model.lowerPositionLimit = q0.copy()
model.upperPositionLimit = q0.copy()
sim = simple.Simulator(model, geom_model)
v = np.zeros(model.nv)
tau = np.zeros(model.nv)
dt = 1e-3
sim.step(q0, v, tau, dt)
assert sim.constraints_problem.getNumberOfContacts() == 0
assert sim.constraints_problem.constraints_problem_size() == 2 * model.nq
assert np.linalg.norm(sim.vnew) < 1e-6
model = pin.Model()
geom_model = pin.GeometryModel()
a = 0.1
m = 3.8
composite = pin.JointModelComposite(2)
composite.addJoint(pin.JointModelPX())
composite.addJoint(pin.JointModelPY())
joint = model.addJoint(0, composite, pin.SE3.Identity(), "ball")
model.appendBodyToJoint(joint, pin.Inertia.FromSphere(m, a / 2), pin.SE3.Identity())
ball_shape = Sphere(a / 2)
geom_ball = pin.GeometryObject("ball", joint, joint, pin.SE3.Identity(), ball_shape)
geom_ball.meshColor = np.array([1.0, 0.2, 0.2, 1.0])
ball_id = geom_model.addGeometryObject(geom_ball)
q0 = pin.neutral(model)
model.lowerPositionLimit = q0.copy()
model.upperPositionLimit = q0.copy()
sim = simple.Simulator(model, geom_model)
v = np.zeros(model.nv)
tau = np.zeros(model.nv)
dt = 1e-3
sim.step(q0, v, tau, dt)
assert sim.constraints_problem.getNumberOfContacts() == 0
assert sim.constraints_problem.constraints_problem_size() == 2 * model.nq
assert np.linalg.norm(sim.vnew) < 1e-6
def test_mujoco_humanoid_limits():
import os
path = os.path.abspath(__file__)
path = path.split("/")
path = "/".join(path[:-2])
path = os.path.join(path, "test_data/mujoco_humanoid.xml")
model = pin.buildModelFromMJCF(path)
geom_model = pin.buildGeomFromMJCF(model, path, pin.COLLISION)
addFloor(geom_model)
q0 = pin.neutral(model)
addSystemCollisionPairs(model, geom_model, q0)
sim = simple.Simulator(model, geom_model)
# First we test without contact but with limits
q = q0.copy()
q[2] += 1.0
v = np.zeros(model.nv)
tau = np.zeros(model.nv)
dt = 1e-3
sim.step(q, v, tau, dt)
data = model.createData()
vnew = v + dt * pin.aba(model, data, q, v, tau)
assert np.linalg.norm(sim.vnew - vnew) < 1e-6
assert sim.constraints_problem.getNumberOfContacts() == 0
assert sim.constraints_problem.constraints_problem_size() == (model.nq - 4) * 2
# Now we test without contact but with tight limits
model.lowerPositionLimit = q0.copy()
model.lowerPositionLimit[2] += 1.0
model.upperPositionLimit = model.lowerPositionLimit.copy()
sim = simple.Simulator(model, geom_model)
q = model.lowerPositionLimit.copy()
v = np.zeros(model.nv)
tau = np.zeros(model.nv)
dt = 1e-3
sim.step(q, v, tau, dt)
assert sim.constraints_problem.getNumberOfContacts() == 0
assert sim.constraints_problem.constraints_problem_size() == (model.nq - 4) * 2
assert sim.admm_constraint_solver.getAbsoluteConvergenceResidual() < 1e-6
# Now we test with contact but without limits
model.lowerPositionLimit = -np.ones(model.nq) * np.finfo("d").max
model.upperPositionLimit = np.ones(model.nq) * np.finfo("d").max
sim = simple.Simulator(model, geom_model)
q = q0.copy()
v = np.zeros(model.nv)
tau = np.zeros(model.nv)
dt = 1e-3
sim.step(q, v, tau, dt)
assert (
sim.constraints_problem.constraints_problem_size()
== sim.constraints_problem.getNumberOfContacts() * 3
)
assert sim.admm_constraint_solver.getAbsoluteConvergenceResidual() < 1e-6
# Now we test with contact and with large limits
model.lowerPositionLimit = -np.ones(model.nq) * 10000.0
model.upperPositionLimit = np.ones(model.nq) * 10000.0
sim = simple.Simulator(model, geom_model)
q = q0.copy()
v = np.zeros(model.nv)
tau = np.zeros(model.nv)
dt = 1e-3
sim.step(q, v, tau, dt)
assert (
sim.constraints_problem.constraints_problem_size()
== (model.nq - 4) * 2 + 3 * sim.constraints_problem.getNumberOfContacts()
)
# TODO Now we test with contact and limits
model = pin.buildModelFromMJCF(path)
sim = simple.Simulator(model, geom_model)
q = q0.copy()
v = np.zeros(model.nv)
tau = np.zeros(model.nv)
dt = 1e-3
sim.step(q, v, tau, dt)
if has_erd:
def setSolo():
# robot model
robot = erd.load("solo12")
model = robot.model.copy()
model.qref = np.array(
[
0.09906518,
0.20099078,
0.32502457,
0.19414175,
-0.00524735,
-0.97855773,
0.06860185,
0.00968163,
0.60963582,
-1.61206407,
-0.02543309,
0.66709088,
-1.50870083,
0.32405118,
-1.15305599,
1.56867351,
-0.39097222,
-1.29675892,
1.39741073,
]
)
model.qref = pin.normalize(model, model.qref)
# Create height field ground
def ground(xy):
return (
np.sin(xy[0] * 3) / 5
+ np.cos(xy[1] ** 2 * 3) / 20
+ np.sin(xy[1] * xy[0] * 5) / 10
)
xg = np.arange(-2, 2, 0.02)
nx = xg.shape[0]
xy_g = np.meshgrid(xg, xg)
xy_g = np.stack(xy_g)
elev_g = np.zeros((nx, nx))
elev_g[:, :] = ground(xy_g)
sx = xg[-1] - xg[0]
sy = xg[-1] - xg[0]
elev_g[:, :] = elev_g[::-1, :]
# Create geometry model
geom_model = robot.collision_model
hfield = HeightFieldAABB(sx, sy, elev_g, np.min(elev_g))
Mhfield = pin.SE3.Identity()
ground = pin.GeometryObject("ground", 0, Mhfield, hfield)
ground_id = geom_model.addGeometryObject(ground)
# Add collision pairs between the ground and the robot
geom_model.removeAllCollisionPairs()
a = 0.01910275
frames_names = ["HR_FOOT", "HL_FOOT", "FR_FOOT", "FL_FOOT"]
for name in frames_names:
frame_id = model.getFrameId(name)
frame = model.frames[frame_id]
joint_id = frame.parentJoint
frame_placement = frame.placement
shape_name = name + "_shape"
shape = Sphere(a)
geometry = pin.GeometryObject(shape_name, joint_id, frame_placement, shape)
geometry.meshColor = np.array([1.0, 0.2, 0.2, 1.0])
geom_id = geom_model.addGeometryObject(geometry)
foot_plane = pin.CollisionPair(
ground_id, geom_id
) # order should be inverted ?
geom_model.addCollisionPair(foot_plane)
# Create data
data = model.createData()
geom_data = geom_model.createData()
for req in geom_data.collisionRequests:
req.security_margin = 1e-3
return model, data, geom_model, geom_data
def test_step_solo():
model, data, geom_model, geom_data = setSolo()
q = model.qref.copy()
v = np.zeros(model.nv)
tau = np.zeros(model.nv)
dt = 1e-3
sim = simple.Simulator(model, data, geom_model, geom_data)
sim.step(q, v, tau, dt)
assert sim.constraints_problem.getNumberOfContacts() == 4
# test_init()
# test_init2()
# test_simulator_handles()
# test_init_empty()
# test_step()
# test_step_balls()
# test_step_balls_with_constraints()
# test_humanoid_limits()
# test_freeflyer_limits()
# test_composite_limits()
# test_manipulator_limits()
# test_mujoco_humanoid_limits()
# print("All tests passed")