[077a87]: / tests / test.obstacles.py

Download this file

65 lines (51 with data), 2.0 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
# This script goes through OpenSim funcionalties
# required for OpenSim-RL
import opensim
# Settings
stepsize = 0.01
# Load existing model
model_path = "../osim/models/gait9dof18musc.osim"
model = opensim.Model(model_path)
model.setUseVisualizer(True)
# Create the ball
r = 0.000001
ballBody = opensim.Body('ball', 0.0001 , opensim.Vec3(0), opensim.Inertia(1,1,.0001,0,0,0) );
ballGeometry = opensim.Ellipsoid(r, r, r)
ballGeometry.setColor(opensim.Gray)
ballBody.attachGeometry(ballGeometry)
# Attach ball to the model
ballJoint = opensim.FreeJoint("weldball",
model.getGround(), # PhysicalFrame
opensim.Vec3(0, 0, 0),
opensim.Vec3(0, 0, 0),
ballBody, # PhysicalFrame
opensim.Vec3(0, 0, 0),
opensim.Vec3(0, 0, 0))
model.addComponent(ballJoint)
model.addComponent(ballBody)
# Add contact
ballContact = opensim.ContactSphere(r, opensim.Vec3(0,0,0), ballBody);
model.addContactGeometry(ballContact)
# Reinitialize the system with the new controller
state = model.initSystem()
for i in range(6):
ballJoint.getCoordinate(i).setLocked(state, True)
state.setTime(0)
manager = opensim.Manager(model)
manager.setIntegratorAccuracy(5e-4)
manager.initialize(state)
# Simulate
for i in range(100):
manager.integrate(i + stepsize)
# Restart the model every 10 frames, with the new position of the ball
if (i + 1) % 10 == 0:
newloc = opensim.Vec3(float(i) / 5, 0, 0)
opensim.PhysicalOffsetFrame.safeDownCast(ballJoint.getChildFrame()).set_translation(newloc)
r = i * 0.005
#ballGeometry.set_radii(opensim.Vec3(r,r,r))
#ballBody.scale(opensim.Vec3(r, r, r))
ballContact.setRadius(r)
state = model.initializeState()
ballJoint.getCoordinate(3).setValue(state, i / 100.0)
for i in range(6):
ballJoint.getCoordinate(i).setLocked(state, True)