/
track_markers_2D_pendulum.py
230 lines (198 loc) · 7.03 KB
/
track_markers_2D_pendulum.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
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
"""
This example uses the data from the balanced pendulum example to generate the data to track.
When it optimizes the program, contrary to the vanilla pendulum, it tracks the values instead of 'knowing' that
it is supposed to balance the pendulum. It is designed to show how to track marker and kinematic data.
Note that the final node is not tracked.
"""
from typing import Callable
import importlib.util
from pathlib import Path
import platform
import biorbd_casadi as biorbd
import numpy as np
from casadi import MX, horzcat, DM
from bioptim import (
BiorbdModel,
OptimalControlProgram,
DynamicsList,
DynamicsFcn,
BoundsList,
ObjectiveList,
ObjectiveFcn,
Axis,
PlotType,
OdeSolver,
OdeSolverBase,
Node,
Solver,
PhaseDynamics,
SolutionMerge,
)
# Load track_segment_on_rt
EXAMPLES_FOLDER = Path(__file__).parent / ".."
spec = importlib.util.spec_from_file_location("data_to_track", str(EXAMPLES_FOLDER) + "/getting_started/pendulum.py")
data_to_track = importlib.util.module_from_spec(spec)
spec.loader.exec_module(data_to_track)
def get_markers_pos(x: DM | np.ndarray, idx_marker: int, fun: Callable, n_q: int) -> DM | np.ndarray:
"""
Get the position of a specific marker from the states
Parameters
----------
x: DM | np.ndarray
The states to get the marker positions from
idx_marker: int
The index of the marker to get the position
fun: Callable
The casadi function of the marker position
Returns
-------
The 3xT ([X, Y, Z] x [Time]) matrix of data
"""
marker_pos = []
for i in range(x.shape[1]):
marker_pos.append(fun(x[:n_q, i])[:, idx_marker])
marker_pos = horzcat(*marker_pos)
return marker_pos
def prepare_ocp(
bio_model: BiorbdModel,
final_time: float,
n_shooting: int,
markers_ref: np.ndarray,
tau_ref: np.ndarray,
ode_solver: OdeSolverBase = OdeSolver.RK4(),
phase_dynamics: PhaseDynamics = PhaseDynamics.SHARED_DURING_THE_PHASE,
expand_dynamics: bool = True,
) -> OptimalControlProgram:
"""
Prepare the ocp
Parameters
----------
bio_model: BiorbdModel
The loaded biorbd model
final_time: float
The time at final node
n_shooting: int
The number of shooting points
markers_ref: np.ndarray
The markers to track
tau_ref: np.ndarray
The generalized forces to track
ode_solver: OdeSolverBase
The ode solver to use
phase_dynamics: PhaseDynamics
If the dynamics equation within a phase is unique or changes at each node.
PhaseDynamics.SHARED_DURING_THE_PHASE is much faster, but lacks the capability to have changing dynamics within
a phase. A good example of when PhaseDynamics.ONE_PER_NODE should be used is when different external forces
are applied at each node
expand_dynamics: bool
If the dynamics function should be expanded. Please note, this will solve the problem faster, but will slow down
the declaration of the OCP, so it is a trade-off. Also depending on the solver, it may or may not work
(for instance IRK is not compatible with expanded dynamics)
Returns
-------
The OptimalControlProgram ready to be solved
"""
# Add objective functions
objective_functions = ObjectiveList()
objective_functions.add(
ObjectiveFcn.Mayer.TRACK_MARKERS,
axes=[Axis.Y, Axis.Z],
node=Node.ALL,
weight=0.5,
target=markers_ref[1:, :, :],
)
objective_functions.add(ObjectiveFcn.Lagrange.TRACK_CONTROL, key="tau", target=tau_ref)
# Dynamics
dynamics = DynamicsList()
dynamics.add(DynamicsFcn.TORQUE_DRIVEN, expand_dynamics=expand_dynamics, phase_dynamics=phase_dynamics)
# Path constraint
x_bounds = BoundsList()
x_bounds["q"] = bio_model.bounds_from_ranges("q")
x_bounds["q"][:, 0] = 0
x_bounds["qdot"] = bio_model.bounds_from_ranges("qdot")
x_bounds["qdot"][:, 0] = 0
# Define control path constraint
n_tau = bio_model.nb_tau
tau_min, tau_max = -100, 100
u_bounds = BoundsList()
u_bounds["tau"] = [tau_min] * n_tau, [tau_max] * n_tau
# ------------- #
return OptimalControlProgram(
bio_model,
dynamics,
n_shooting,
final_time,
x_bounds=x_bounds,
u_bounds=u_bounds,
objective_functions=objective_functions,
ode_solver=ode_solver,
)
def main():
"""
Firstly, it solves the getting_started/pendulum.py example. Afterward, it gets the marker positions and joint
torque from the solution and uses them to track. It then creates and solves this ocp and show the results
"""
biorbd_path = str(EXAMPLES_FOLDER) + "/getting_started/models/pendulum.bioMod"
bio_model = BiorbdModel(biorbd_path)
final_time = 1
n_shooting = 20
ocp_to_track = data_to_track.prepare_ocp(
biorbd_model_path=biorbd_path, final_time=final_time, n_shooting=n_shooting
)
sol = ocp_to_track.solve()
states = sol.decision_states(to_merge=SolutionMerge.NODES)
controls = sol.decision_controls(to_merge=SolutionMerge.NODES)
q, qdot, tau = states["q"], states["qdot"], controls["tau"]
n_q = bio_model.nb_q
n_marker = bio_model.nb_markers
x = np.concatenate((q, qdot))
symbolic_states = MX.sym("q", n_q, 1)
markers_fun = biorbd.to_casadi_func("ForwardKin", bio_model.markers, symbolic_states)
markers_ref = np.zeros((3, n_marker, n_shooting + 1))
for i in range(n_shooting + 1):
markers_ref[:, :, i] = markers_fun(x[:n_q, i])
tau_ref = tau[:, :-1]
ocp = prepare_ocp(
bio_model,
final_time=final_time,
n_shooting=n_shooting,
markers_ref=markers_ref,
tau_ref=tau_ref,
)
# --- plot markers position --- #
title_markers = ["x", "y", "z"]
marker_color = ["tab:red", "tab:orange"]
ocp.add_plot(
"Markers plot coordinates",
update_function=lambda t0, phases_dt, node_idx, x, u, p, a, d: get_markers_pos(x, 0, markers_fun, n_q),
linestyle=".-",
plot_type=PlotType.STEP,
color=marker_color[0],
)
ocp.add_plot(
"Markers plot coordinates",
update_function=lambda t0, phases_dt, node_idx, x, u, p, a, d: get_markers_pos(x, 1, markers_fun, n_q),
linestyle=".-",
plot_type=PlotType.STEP,
color=marker_color[1],
)
ocp.add_plot(
"Markers plot coordinates",
update_function=lambda t0, phases_dt, node_idx, x, u, p, a, d: markers_ref[:, 0, :],
plot_type=PlotType.PLOT,
color=marker_color[0],
legend=title_markers,
)
ocp.add_plot(
"Markers plot coordinates",
update_function=lambda t0, phases_dt, node_idx, x, u, p, a, d: markers_ref[:, 1, :],
plot_type=PlotType.PLOT,
color=marker_color[1],
legend=title_markers,
)
# --- Solve the program --- #
sol = ocp.solve(Solver.IPOPT(show_online_optim=platform.system() == "Linux"))
# --- Show results --- #
sol.animate(n_frames=100)
if __name__ == "__main__":
main()