aligator  0.6.1
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
Loading...
Searching...
No Matches
plotting.py
Go to the documentation of this file.
1import matplotlib.pyplot as plt
2import numpy as np
3
4from aligator import HistoryCallback, Results
5
6
7def plot_convergence(cb: HistoryCallback, ax: plt.Axes, res: Results = None):
8 from proxsuite_nlp.utils import plot_pd_errs
9
10 prim_infeas = cb.storage.prim_infeas.tolist()
11 dual_infeas = cb.storage.dual_infeas.tolist()
12 if res is not None:
13 prim_infeas.append(res.primal_infeas)
14 dual_infeas.append(res.dual_infeas)
15 plot_pd_errs(ax, prim_infeas, dual_infeas)
16 ax.grid(axis="y", which="major")
17 return
18
19
20def plot_se2_pose(q: np.ndarray, ax: plt.Axes, alpha=0.5, fc="tab:blue"):
21 from matplotlib import transforms
22
23 w = 1.0
24 h = 0.4
25 center = (q[0] - 0.5 * w, q[1] - 0.5 * h)
26 rect = plt.Rectangle(center, w, h, fc=fc, alpha=alpha)
27 theta = np.arctan2(q[3], q[2])
28 transform_ = transforms.Affine2D().rotate_around(*q[:2], -theta) + ax.transData
29 rect.set_transform(transform_)
30 ax.add_patch(rect)
31 return rect
32
33
35 if isinstance(axes, np.ndarray):
36 axes = axes.flatten()
37 elif not isinstance(axes, list):
38 axes = [axes]
39 return axes
40
41
43 times,
44 us,
45 ncols=2,
46 axes=None,
47 effort_limit=None,
48 joint_names=None,
49 rmodel=None,
50 figsize=(6.4, 6.4),
51) -> plt.Figure:
52 t0 = times[0]
53 tf = times[-1]
54 us = np.asarray(us)
55 nu = us.shape[1]
56 nrows, r = divmod(nu, ncols)
57 nrows += int(r > 0)
58 if axes is None:
59 fig, axes = plt.subplots(nrows, ncols, sharex="col", figsize=figsize)
60 else:
61 fig = axes.flat[0].get_figure()
62 axes = _axes_flatten_if_ndarray(axes)
63
64 if rmodel is not None:
65 effort_limit = rmodel.effortLimit
66 joint_names = rmodel.names
67
68 for i in range(nu):
69 ax: plt.Axes = axes[i]
70 ax.step(times[:-1], us[:, i])
71 if effort_limit is not None:
72 ylim = ax.get_ylim()
73 ax.hlines(-effort_limit[i], t0, tf, colors="k", linestyles="--")
74 ax.hlines(+effort_limit[i], t0, tf, colors="r", linestyles="dashdot")
75 ax.set_ylim(*ylim)
76 if joint_names is not None:
77 joint_name = joint_names[i].lower()
78 ax.set_ylabel(joint_name)
79 fig.supxlabel("Time $t$")
80 fig.suptitle("Control trajectories")
81 fig.tight_layout()
82 return fig
83
84
86 times, vs, rmodel, axes=None, ncols=2, figsize=(6.4, 6.4)
87) -> plt.Figure:
88 vs = np.asarray(vs)
89 nv = vs.shape[1]
90 idx_to_joint_id_map = {}
91 jid = 0
92 for i in range(nv):
93 if i in rmodel.idx_vs.tolist():
94 jid += 1
95 idx_to_joint_id_map[i] = jid
96 nrows, r = divmod(nv, ncols)
97 nrows += int(r > 0)
98
99 vel_limit = rmodel.velocityLimit
100 t0 = times[0]
101 tf = times[-1]
102
103 if axes is None:
104 fig, axes = plt.subplots(nrows, ncols, figsize=figsize)
105 fig: plt.Figure
106 else:
107 fig = axes.flat[0].get_figure()
108 axes = _axes_flatten_if_ndarray(axes)
109
110 for i in range(nv):
111 ax: plt.Axes = axes[i]
112 ax.plot(times, vs[:, i])
113 jid = idx_to_joint_id_map[i]
114 joint_name = rmodel.names[jid].lower()
115 ylim = ax.get_ylim()
116 ax.hlines(-vel_limit[i], t0, tf, colors="k", linestyles="--")
117 ax.hlines(+vel_limit[i], t0, tf, colors="r", linestyles="dashdot")
118 ax.set_ylim(*ylim)
119 ax.set_ylabel(joint_name)
120
121 fig.supxlabel("Time $t$")
122 fig.suptitle("Velocity trajectories")
123 fig.tight_layout()
124 return fig
plt.Figure plot_controls_traj(times, us, ncols=2, axes=None, effort_limit=None, joint_names=None, rmodel=None, figsize=(6.4, 6.4))
Definition plotting.py:51
plt.Figure plot_velocity_traj(times, vs, rmodel, axes=None, ncols=2, figsize=(6.4, 6.4))
Definition plotting.py:87
plot_se2_pose(np.ndarray q, plt.Axes ax, alpha=0.5, fc="tab:blue")
Definition plotting.py:20
plot_convergence(HistoryCallback cb, plt.Axes ax, Results res=None)
Definition plotting.py:7