|
| 1 | +from .biorbd import currentLinearAlgebraBackend |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +try: |
| 6 | + from matplotlib import pyplot as plt |
| 7 | + |
| 8 | + matplotlib_found = True |
| 9 | +except ModuleNotFoundError: |
| 10 | + matplotlib_found = False |
| 11 | +if currentLinearAlgebraBackend() == 1: |
| 12 | + from casadi import Function, MX |
| 13 | + |
| 14 | + |
| 15 | +def surface_max_torque_actuator(model, dof, resolution=40): |
| 16 | + if not matplotlib_found: |
| 17 | + raise ModuleNotFoundError("matplotlib must be installed to use biorbd.surface_max_torque_actuator") |
| 18 | + |
| 19 | + d2r = np.pi / 180 |
| 20 | + min_bound_q = -200 * d2r |
| 21 | + max_bound_q = 200 * d2r |
| 22 | + min_bound_qdot = -500 * d2r |
| 23 | + max_bound_qdot = 500 * d2r |
| 24 | + nbq = model.nbQ() |
| 25 | + |
| 26 | + if currentLinearAlgebraBackend() == 1: |
| 27 | + torque_act = MX.sym("act", nbq, 1) |
| 28 | + q_sym = MX.sym("q", nbq, 1) |
| 29 | + qdot_sym = MX.sym("q_dot", nbq, 1) |
| 30 | + torque_func = Function( |
| 31 | + "torque_func", |
| 32 | + [torque_act, q_sym, qdot_sym], |
| 33 | + [model.torque(torque_act, q_sym, qdot_sym).to_mx()], |
| 34 | + ["activation", "Q", "Qdot"], |
| 35 | + ["Tau"], |
| 36 | + ) |
| 37 | + else: |
| 38 | + torque_func = model.torque |
| 39 | + |
| 40 | + max_act = np.ones(nbq) |
| 41 | + q = np.arange(min_bound_q, max_bound_q, (max_bound_q - min_bound_q) / resolution) |
| 42 | + qdot = np.arange(min_bound_qdot, max_bound_qdot, (max_bound_qdot - min_bound_qdot) / resolution) |
| 43 | + |
| 44 | + tau_pos = np.zeros((resolution, resolution)) |
| 45 | + tau_neg = np.zeros((resolution, resolution)) |
| 46 | + for i in range(resolution): |
| 47 | + for j in range(resolution): |
| 48 | + pos = torque_func(max_act, np.ones(nbq) * q[i], np.ones(nbq) * qdot[j]) |
| 49 | + neg = torque_func(-max_act, np.ones(nbq) * q[i], np.ones(nbq) * qdot[j]) |
| 50 | + if currentLinearAlgebraBackend() == 0: |
| 51 | + pos = pos.to_array() |
| 52 | + neg = neg.to_array() |
| 53 | + tau_pos[i, j] = pos[dof] |
| 54 | + tau_neg[i, j] = neg[dof] |
| 55 | + |
| 56 | + q = q / d2r |
| 57 | + qdot = qdot / d2r |
| 58 | + q, qdot = np.meshgrid(qdot, q) |
| 59 | + |
| 60 | + def plot_surface(tau): |
| 61 | + fig = plt.figure() |
| 62 | + ax = fig.gca(projection="3d") |
| 63 | + ax.plot_surface(q, qdot, tau) |
| 64 | + ax.set_xlabel("Qdot", fontsize=15) |
| 65 | + ax.set_ylabel("Q", fontsize=15) |
| 66 | + ax.set_zlabel("Tau", fontsize=15) |
| 67 | + |
| 68 | + plot_surface(tau_pos) |
| 69 | + plot_surface(tau_neg) |
| 70 | + |
| 71 | + plt.show() |
0 commit comments