-
Notifications
You must be signed in to change notification settings - Fork 3
/
ocp_propulsion.py
446 lines (367 loc) · 14.3 KB
/
ocp_propulsion.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
"""
# todo: not implemented at all, just a copy of the example
It needs to be a example that simulates the pushing phase of a propulsion cycle of a wheelchair.
i.e. with the close-loop on the handrim, and the rolling joint constraint.
We need to get inspiration from the work of Anais.
"""
import numpy as np
from biorbd_casadi import marker_index
from casadi import MX, SX, vertcat, Function, jacobian, sqrt, atan2
from bioptim import (
OptimalControlProgram,
DynamicsList,
ConfigureProblem,
DynamicsFunctions,
ParameterList,
ObjectiveFcn,
ObjectiveList,
ConstraintList,
BoundsList,
InitialGuessList,
OdeSolver,
OdeSolverBase,
NonLinearProgram,
Solver,
DynamicsEvaluation,
BiMappingList,
PhaseDynamics,
HolonomicConstraintsList,
)
from wheelchair_utils.custom_biorbd_model_holonomic import BiorbdModelCustomHolonomic
def custom_dynamic(
time: MX | SX,
states: MX | SX,
controls: MX | SX,
parameters: MX | SX,
stochastic_variables: MX | SX,
nlp: NonLinearProgram,
) -> DynamicsEvaluation:
"""
The custom dynamics function that provides the derivative of the states: dxdt = f(x, u, p)
Parameters
----------
states: MX | SX
The state of the system
controls: MX | SX
The controls of the system
parameters: MX | SX
The parameters acting on the system
nlp: NonLinearProgram
A reference to the phase
Returns
-------
The derivative of the states in the tuple[MX | SX] format
"""
u = DynamicsFunctions.get(nlp.states["u"], states)
udot = DynamicsFunctions.get(nlp.states["udot"], states)
tau = DynamicsFunctions.get(nlp.controls["tau"], controls)
uddot = nlp.model.partitioned_forward_dynamics(u, udot, tau)
return DynamicsEvaluation(dxdt=vertcat(udot, uddot), defects=None)
def custom_configure(ocp: OptimalControlProgram, nlp: NonLinearProgram):
"""
Tell the program which variables are states and controls.
The user is expected to use the ConfigureProblem.configure_xxx functions.
Parameters
----------
ocp: OptimalControlProgram
A reference to the ocp
nlp: NonLinearProgram
A reference to the phase
"""
name_u = [nlp.model.name_dof[i] for i in range(nlp.model.nb_independent_joints)]
axes_idx = ConfigureProblem._apply_phase_mapping(ocp, nlp, "u")
ConfigureProblem.configure_new_variable("u", name_u, ocp, nlp, True, False, False, axes_idx=axes_idx)
name = "udot"
name_qdot = ConfigureProblem._get_kinematics_based_names(nlp, "qdot")
name_udot = [name_qdot[i] for i in range(nlp.model.nb_independent_joints)]
axes_idx = ConfigureProblem._apply_phase_mapping(ocp, nlp, name)
ConfigureProblem.configure_new_variable(name, name_udot, ocp, nlp, True, False, False, axes_idx=axes_idx)
ConfigureProblem.configure_tau(ocp, nlp, as_states=False, as_controls=True)
ConfigureProblem.configure_dynamics_function(ocp, nlp, custom_dynamic)
def generate_close_loop_constraint(
biorbd_model,
marker_1: str,
marker_2: str,
index: slice = slice(0, 3),
local_frame_index: int = None,
parameters: MX = MX(),
) -> tuple[Function, Function, Function]:
"""Generate a close loop constraint between two markers"""
# symbolic variables to create the functions
q_sym = MX.sym("q", biorbd_model.nb_q, 1)
q_dot_sym = MX.sym("q_dot", biorbd_model.nb_qdot, 1)
q_ddot_sym = MX.sym("q_ddot", biorbd_model.nb_qdot, 1)
# symbolic markers in global frame
marker_1_sym = biorbd_model.marker(q_sym, index=marker_index(biorbd_model.model, marker_1))
marker_2_sym = biorbd_model.marker(q_sym, index=marker_index(biorbd_model.model, marker_2))
# if local frame is provided, the markers are expressed in the same local frame
if local_frame_index is not None:
jcs_t = biorbd_model.homogeneous_matrices_in_global(q_sym, local_frame_index, inverse=True)
marker_1_sym = (jcs_t.to_mx() @ vertcat(marker_1_sym, 1))[:3]
marker_2_sym = (jcs_t.to_mx() @ vertcat(marker_2_sym, 1))[:3]
# the constraint is the distance between the two markers, set to zero
constraint = (marker_1_sym - marker_2_sym)[index]
# the jacobian of the constraint
constraint_jacobian = jacobian(constraint, q_sym)
constraint_func = Function(
"holonomic_constraint",
[q_sym, parameters],
[constraint],
["q"],
["holonomic_constraint"],
).expand()
constraint_jacobian_func = Function(
"holonomic_constraint_jacobian",
[q_sym, parameters],
[constraint_jacobian],
["q"],
["holonomic_constraint_jacobian"],
).expand()
# the double derivative of the constraint
constraint_double_derivative = (
constraint_jacobian_func(q_sym) @ q_ddot_sym + constraint_jacobian_func(q_dot_sym) @ q_dot_sym
)
constraint_double_derivative_func = Function(
"holonomic_constraint_double_derivative",
[q_sym, q_dot_sym, q_ddot_sym, parameters],
[constraint_double_derivative],
["q", "q_dot", "q_ddot"],
["holonomic_constraint_double_derivative"],
).expand()
return constraint_func, constraint_jacobian_func, constraint_double_derivative_func
def generate_close_loop_constraint_polar_coordinates(
biorbd_model: BiorbdModelCustomHolonomic,
marker_1: str,
wheel_frame_index: int = None,
handrim_radius: float = 0.35,
contact_angle: float = None,
) -> tuple[Function, Function, Function]:
"""
Generate a close loop constraint between two markers, in polar coordinates for the wheel.
In order to get lagrange multipliers as radial and tangential forces applied on the wheel.
assumed in x-y plane
Parameters
----------
biorbd_model: BiorbdModelCustomHolonomic
The biorbd model
marker_1: str
The name of the marker to constraint
wheel_frame_index: int
The index of the wheel frame
handrim_radius: float
The radius of the handrim
contact_angle: float
polar angle of the contact point on the wheel, in radian need to be symbolic to be able to find the best location.
**extra_params, optional
Extra parameters to pass to the constraint function
"""
# symbolic variables to create the functions
q_sym = MX.sym("q", biorbd_model.nb_q, 1)
q_dot_sym = MX.sym("q_dot", biorbd_model.nb_qdot, 1)
q_ddot_sym = MX.sym("q_ddot", biorbd_model.nb_qdot, 1)
# symbolic markers in global frame
marker_1_sym = biorbd_model.marker(q_sym, index=marker_index(biorbd_model.model, marker_1))
# if local frame is provided, the markers are expressed in the same local frame
if wheel_frame_index is not None:
jcs_t = biorbd_model.homogeneous_matrices_in_global(q_sym, wheel_frame_index, inverse=True)
marker_1_sym = (jcs_t.to_mx() @ vertcat(marker_1_sym, 1))[:3]
# express in polar coordinates
radial_constraint = sqrt((marker_1_sym[0]) ** 2 + (marker_1_sym[1]) ** 2) - handrim_radius
tangential_constraint = atan2(marker_1_sym[1], marker_1_sym[0]) - contact_angle
# the constraint
constraint = vertcat(radial_constraint, tangential_constraint)
# the jacobian of the constraint
constraint_jacobian = jacobian(constraint, q_sym)
constraint_func = Function(
"holonomic_constraint",
[q_sym, parameters],
[constraint],
["q"],
["holonomic_constraint"],
).expand()
constraint_jacobian_func = Function(
"holonomic_constraint_jacobian",
[q_sym, parameters],
[constraint_jacobian],
["q"],
["holonomic_constraint_jacobian"],
).expand()
# the double derivative of the constraint
constraint_double_derivative = (
constraint_jacobian_func(q_sym) @ q_ddot_sym
+ jacobian(constraint_jacobian_func(q_sym) @ q_dot_sym, q_sym) @ q_dot_sym
)
constraint_double_derivative_func = Function(
"holonomic_constraint_double_derivative",
[q_sym, q_dot_sym, q_ddot_sym, parameters],
[constraint_double_derivative],
["q", "q_dot", "q_ddot"],
["holonomic_constraint_double_derivative"],
).expand()
return constraint_func, constraint_jacobian_func, constraint_double_derivative_func
def generate_rolling_joint_constraint(
biorbd_model: BiorbdModelCustomHolonomic,
translation_joint_index: int,
rotation_joint_index: int,
radius: float = 1,
) -> tuple[Function, Function, Function]:
"""Generate a rolling joint constraint between two joints"""
# symbolic variables to create the functions
q_sym = MX.sym("q", biorbd_model.nb_q, 1)
q_dot_sym = MX.sym("q_dot", biorbd_model.nb_qdot, 1)
q_ddot_sym = MX.sym("q_ddot", biorbd_model.nb_qdot, 1)
constraint = q_sym[translation_joint_index] + radius * q_sym[rotation_joint_index]
constraint_jacobian = jacobian(constraint, q_sym)
constraint_func = Function(
"rolling_joint_constraint",
[q_sym],
[constraint],
["q"],
["rolling_joint_constraint"],
).expand()
constraint_jacobian_func = Function(
"rolling_joint_constraint_jacobian",
[q_sym],
[constraint_jacobian],
["q"],
["rolling_joint_constraint_jacobian"],
).expand()
constraint_double_derivative = (
constraint_jacobian_func(q_sym) @ q_ddot_sym
+ jacobian(constraint_jacobian_func(q_sym) @ q_dot_sym, q_sym) @ q_dot_sym
)
constraint_double_derivative_func = Function(
"rolling_joint_constraint_double_derivative",
[q_sym, q_dot_sym, q_ddot_sym],
[constraint_double_derivative],
["q", "q_dot", "q_ddot"],
["rolling_joint_constraint_double_derivative"],
).expand()
return constraint_func, constraint_jacobian_func, constraint_double_derivative_func
def prepare_ocp(
biorbd_model_path: str,
ode_solver: OdeSolverBase = OdeSolver.RK4(),
n_shooting=50,
) -> OptimalControlProgram:
"""
Prepare the program
Parameters
----------
biorbd_model_path: str
The path of the biorbd model
ode_solver: OdeSolverBase
The type of ode solver used
n_shooting: int
The number of shooting points
Returns
-------
The ocp ready to be solved
"""
# --- Options --- #
# BioModel path
bio_model = BiorbdModelCustomHolonomic(biorbd_model_path)
holonomic_constraints = HolonomicConstraintsList()
holonomic_constraints.add(
key="rolling_joint_constraint",
constraints_fcn=generate_rolling_joint_constraint,
biorbd_model=bio_model,
translation_joint_index=0,
rotation_joint_index=1,
radius=0.35,
)
bio_model.set_holonomic_configuration(
constraints_list=holonomic_constraints, independent_joint_index=[0, 2, 3], dependent_joint_index=[1]
)
# constraint, constraint_jacobian, constraint_double_derivative = generate_close_loop_constraint(
# biorbd_model=bio_model,
# marker_1="marker_4",
# marker_2="handrim_contact",
# local_frame_index=0,
# parameters=contact_angle,
# )
parameters = ParameterList()
# def set_contact_angle(biomodel: BiorbdModelCustomHolonomic, contact_angle: MX):
# """Set the contact angle of the wheel"""
# biomodel.add_extra_parameter("contact_angle", contact_angle)
#
# parameters.add(
# parameter_name="contact_angle", # the polar angle of the contact point in the wheel frame
# function=set_contact_angle,
# initial_guess=InitialGuess(0),
# bounds=Bounds(-2*np.pi, 2*np.pi),
# size=1,
# )
# bio_model.set_dependencies(independent_joint_index=[0, 2, 3], dependent_joint_index=[1])
final_time = 0.5
# Add objective functions
objective_functions = ObjectiveList()
objective_functions.add(ObjectiveFcn.Lagrange.MINIMIZE_CONTROL, key="tau", weight=100, multi_thread=False)
objective_functions.add(ObjectiveFcn.Mayer.MINIMIZE_TIME, weight=1, min_bound=0.5, max_bound=0.6)
# Dynamics
dynamics = DynamicsList()
dynamics.add(
custom_configure, dynamic_function=custom_dynamic, phase_dynamics=PhaseDynamics.SHARED_DURING_THE_PHASE
)
# Path Constraints
constraints = ConstraintList()
# Boundaries
mapping = BiMappingList()
mapping.add("q", to_second=[0, None, 1, 2], to_first=[0, 2, 3])
mapping.add("qdot", to_second=[0, None, 1, 2], to_first=[0, 2, 3])
x_bounds = BoundsList()
x_bounds["q"] = bio_model.bounds_from_ranges("q", mapping=mapping)
x_bounds["qdot"] = bio_model.bounds_from_ranges("qdot", mapping=mapping)
# Initial guess
x_init = InitialGuessList()
x_init.add(key="q", initial_guess=[0, 0, 0, 0, 0, 0])
# Define control path constraint
tau_min, tau_max, tau_init = -50, 50, 0
variable_bimapping = BiMappingList()
variable_bimapping.add("tau", to_second=[None, None, 0, 1], to_first=[2, 3])
u_bounds = BoundsList()
u_bounds.add("tau", min=[tau_min] * 2, max=[tau_max] * 2)
u_init = InitialGuessList()
u_init.add("tau", [tau_init] * 2)
# ------------- #
return (
OptimalControlProgram(
bio_model,
dynamics=dynamics,
n_shooting=n_shooting,
phase_time=final_time,
x_init=x_init,
u_init=u_init,
x_bounds=x_bounds,
u_bounds=u_bounds,
objective_functions=objective_functions,
constraints=constraints,
ode_solver=ode_solver,
use_sx=False,
variable_mappings=variable_bimapping,
parameters=parameters,
n_threads=8,
),
bio_model,
)
def main():
"""
Runs the optimization and animates it
"""
model_path = "wheelchair_model.bioMod"
n_shooting = 50
ocp, bio_model = prepare_ocp(biorbd_model_path=model_path, n_shooting=n_shooting)
# --- Solve the program --- #
sol = ocp.solve(Solver.IPOPT(show_online_optim=False, _max_iter=500))
# --- Show results --- #
# sol.animate()
q = np.zeros((4, n_shooting + 1))
for i, ui in enumerate(sol.states["u"].T):
vi = bio_model.compute_v_from_u_numeric(ui, v_init=np.zeros(2)).toarray()
qi = bio_model.q_from_u_and_v(ui[:, np.newaxis], vi).toarray().squeeze()
q[:, i] = qi
import bioviz
viz = bioviz.Viz(model_path)
viz.load_movement(q)
viz.exec()
if __name__ == "__main__":
main()