|
a |
|
b/tests/test.manager.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 |
# Build the controller (we will iteratively |
|
|
14 |
# update it at every step of the simulation) |
|
|
15 |
brain = opensim.PrescribedController() |
|
|
16 |
controllers = [] |
|
|
17 |
|
|
|
18 |
# Add actuators to the controller |
|
|
19 |
state = model.initSystem() # we need to initialize the system (?) |
|
|
20 |
muscleSet = model.getMuscles() |
|
|
21 |
forceSet = model.getForceSet() |
|
|
22 |
|
|
|
23 |
for j in range(muscleSet.getSize()): |
|
|
24 |
func = opensim.Constant(1.0) |
|
|
25 |
controllers.append(func) |
|
|
26 |
brain.addActuator(muscleSet.get(j)) |
|
|
27 |
brain.prescribeControlForActuator(j, func) |
|
|
28 |
|
|
|
29 |
model.addController(brain) |
|
|
30 |
|
|
|
31 |
blockos = opensim.Body('blockos', 0.0001 , opensim.Vec3(0), opensim.Inertia(1,1,.0001,0,0,0) ); |
|
|
32 |
pj = opensim.PinJoint("pinblock", |
|
|
33 |
model.getGround(), # PhysicalFrame |
|
|
34 |
opensim.Vec3(-0.5, 0, 0), |
|
|
35 |
opensim.Vec3(0, 0, 0), |
|
|
36 |
blockos, # PhysicalFrame |
|
|
37 |
opensim.Vec3(0, 0, 0), |
|
|
38 |
opensim.Vec3(0, 0, 0)) |
|
|
39 |
|
|
|
40 |
bodyGeometry = opensim.Ellipsoid(0.1, 0.1, 0.1) |
|
|
41 |
bodyGeometry.setColor(opensim.Gray) |
|
|
42 |
blockos.attachGeometry(bodyGeometry) |
|
|
43 |
|
|
|
44 |
model.addComponent(pj) |
|
|
45 |
model.addComponent(blockos) |
|
|
46 |
#block = opensim.ContactMesh('block.obj',opensim.Vec3(0,0,0), opensim.Vec3(0,0,0), blockos); |
|
|
47 |
block = opensim.ContactSphere(0.4, opensim.Vec3(0,0,0), blockos); |
|
|
48 |
model.addContactGeometry(block) |
|
|
49 |
|
|
|
50 |
# Reinitialize the system with the new controller |
|
|
51 |
state0 = model.initSystem() |
|
|
52 |
state = opensim.State(state0) |
|
|
53 |
|
|
|
54 |
# Change max force |
|
|
55 |
muscleSet.get(0).setMaxIsometricForce(100000.0) |
|
|
56 |
|
|
|
57 |
# Get ligaments |
|
|
58 |
ligamentSet = [] |
|
|
59 |
for j in range(20, 26): |
|
|
60 |
ligamentSet.append(opensim.CoordinateLimitForce.safeDownCast(forceSet.get(j))) |
|
|
61 |
|
|
|
62 |
state.setTime(0) |
|
|
63 |
manager = opensim.Manager(model) |
|
|
64 |
manager.setIntegratorAccuracy(5e-4) |
|
|
65 |
manager.initialize(state) |
|
|
66 |
|
|
|
67 |
for i in range(20): |
|
|
68 |
# Set some excitation values |
|
|
69 |
for j in range(muscleSet.getSize()): |
|
|
70 |
controllers[j].setValue( ((i + j) % 10) * 0.1) |
|
|
71 |
|
|
|
72 |
# Integrate |
|
|
73 |
t = (i+1) * stepsize |
|
|
74 |
manager.integrate(t) |
|
|
75 |
|
|
|
76 |
# Report activations and excitations |
|
|
77 |
model.realizeDynamics(state) |
|
|
78 |
print("%f %f" % (t, muscleSet.get(0).getActivation(state))) |
|
|
79 |
print("%f %f" % (t, muscleSet.get(0).getExcitation(state))) |
|
|
80 |
|
|
|
81 |
# Ligaments |
|
|
82 |
for lig in ligamentSet: |
|
|
83 |
print(lig.calcLimitForce(state)) |
|
|
84 |
|
|
|
85 |
# Restart the model every 100 frames |
|
|
86 |
if (i + 1) % 100 == 0: |
|
|
87 |
state = opensim.State(state0) |