-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathload_results.py
68 lines (56 loc) · 1.85 KB
/
load_results.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
"""
This files loads the results of the simulation, plots them, and animates them.
"""
import numpy as np
import pickle
from bioptim import OptimalControlProgram
# filename = "raw/miller_MillerDynamics.EXPLICIT_irand10_extraobjFalse_125_25.pckl" # or .bo, any file in raw.
filename = "raw/miller_MillerDynamics.ROOT_IMPLICIT_irand0_extraobjTrue_125_25.bo"
def show_animation(model_path: str, q: np.ndarray, fixed_floating_base: bool = False):
"""
Show the animation of the model
Parameters
----------
model_path : str
Model to animate
q : np.ndarray
Array of the states to animate
fixed_floating_base : bool
If the floating base is fixed or not when the motion is played
Returns
-------
None
"""
import bioviz
manually_animate = False
biorbd_viz = bioviz.Viz(model_path, show_floor=False, show_gravity_vector=False)
if fixed_floating_base:
q[:6, :] = 0
if manually_animate:
i = 0
while biorbd_viz.vtk_window.is_active:
biorbd_viz.set_q(q[:, i])
i = i + 1
else:
biorbd_viz.load_movement(q)
biorbd_viz.exec()
if filename.endswith(".bo"):
try:
ocp, sol = OptimalControlProgram.load(filename)
sol.print_cost()
sol.animate(n_frames=100)
except:
model_name = "Model_JeCh_15DoFs.bioMod"
file = open(f"{filename}", "rb")
data = pickle.load(file)
q = np.hstack((data[0][0]["q"], data[0][1]["q"]))
show_animation(model_name, q)
elif filename.endswith(".pckl"):
model_name = "Model_JeCh_15DoFs.bioMod"
file = open(f"{filename}", "rb")
data = pickle.load(file)
q = np.hstack((data["states"][0]["q"], data["states"][1]["q"]))
# Animate the model
show_animation(model_name, q)
else:
raise ValueError("filename must end with .bo or .pkl")