|
a |
|
b/tests/test.obstacles.py |
|
|
1 |
# This script goes through OpenSim funcionalties |
|
|
2 |
# required for OpenSim-RL |
|
|
3 |
import opensim |
|
|
4 |
|
|
|
5 |
# Settings |
|
|
6 |
stepsize = 0.01 |
|
|
7 |
|
|
|
8 |
# Load existing model |
|
|
9 |
model_path = "../osim/models/gait9dof18musc.osim" |
|
|
10 |
model = opensim.Model(model_path) |
|
|
11 |
model.setUseVisualizer(True) |
|
|
12 |
|
|
|
13 |
# Create the ball |
|
|
14 |
r = 0.000001 |
|
|
15 |
ballBody = opensim.Body('ball', 0.0001 , opensim.Vec3(0), opensim.Inertia(1,1,.0001,0,0,0) ); |
|
|
16 |
ballGeometry = opensim.Ellipsoid(r, r, r) |
|
|
17 |
ballGeometry.setColor(opensim.Gray) |
|
|
18 |
ballBody.attachGeometry(ballGeometry) |
|
|
19 |
|
|
|
20 |
# Attach ball to the model |
|
|
21 |
ballJoint = opensim.FreeJoint("weldball", |
|
|
22 |
model.getGround(), # PhysicalFrame |
|
|
23 |
opensim.Vec3(0, 0, 0), |
|
|
24 |
opensim.Vec3(0, 0, 0), |
|
|
25 |
ballBody, # PhysicalFrame |
|
|
26 |
opensim.Vec3(0, 0, 0), |
|
|
27 |
opensim.Vec3(0, 0, 0)) |
|
|
28 |
model.addComponent(ballJoint) |
|
|
29 |
model.addComponent(ballBody) |
|
|
30 |
|
|
|
31 |
# Add contact |
|
|
32 |
ballContact = opensim.ContactSphere(r, opensim.Vec3(0,0,0), ballBody); |
|
|
33 |
model.addContactGeometry(ballContact) |
|
|
34 |
|
|
|
35 |
# Reinitialize the system with the new controller |
|
|
36 |
state = model.initSystem() |
|
|
37 |
|
|
|
38 |
for i in range(6): |
|
|
39 |
ballJoint.getCoordinate(i).setLocked(state, True) |
|
|
40 |
|
|
|
41 |
state.setTime(0) |
|
|
42 |
manager = opensim.Manager(model) |
|
|
43 |
manager.setIntegratorAccuracy(5e-4) |
|
|
44 |
manager.initialize(state) |
|
|
45 |
|
|
|
46 |
# Simulate |
|
|
47 |
for i in range(100): |
|
|
48 |
manager.integrate(i + stepsize) |
|
|
49 |
|
|
|
50 |
# Restart the model every 10 frames, with the new position of the ball |
|
|
51 |
if (i + 1) % 10 == 0: |
|
|
52 |
newloc = opensim.Vec3(float(i) / 5, 0, 0) |
|
|
53 |
opensim.PhysicalOffsetFrame.safeDownCast(ballJoint.getChildFrame()).set_translation(newloc) |
|
|
54 |
|
|
|
55 |
r = i * 0.005 |
|
|
56 |
#ballGeometry.set_radii(opensim.Vec3(r,r,r)) |
|
|
57 |
#ballBody.scale(opensim.Vec3(r, r, r)) |
|
|
58 |
ballContact.setRadius(r) |
|
|
59 |
|
|
|
60 |
state = model.initializeState() |
|
|
61 |
|
|
|
62 |
ballJoint.getCoordinate(3).setValue(state, i / 100.0) |
|
|
63 |
for i in range(6): |
|
|
64 |
ballJoint.getCoordinate(i).setLocked(state, True) |