support for PyPy
[liba.git] / python / test / 3rd / trajtrap.py
blob9dfd8cf65a6cf1ea8ce8916337491325f5751bcf
1 #!/usr/bin/env python
2 import os, sys
4 base = os.path.dirname(__file__)
5 path = os.path.dirname(base)
6 path = os.path.dirname(path)
7 sys.path.insert(0, path)
8 try:
9 import numpy as np
10 import matplotlib.pyplot as plt
11 except Exception as e:
12 print(e)
13 exit()
15 import liba # type: ignore
17 traj = liba.trajtrap()
18 traj.gen(2, 2, -2, 0, 4)
19 data = np.arange(0, traj.t, traj.t / 1000)
21 plt.figure("trapezoidal velocity trajectory")
23 plt.subplot(311)
24 plt.title("trapezoidal velocity trajectory")
25 plt.ylabel("Position")
26 plt.plot(data, np.array(traj.pos(data), copy=False), "r-", label="q")
27 plt.legend()
29 plt.subplot(312)
30 plt.ylabel("Velocity")
31 plt.plot(data, np.array(traj.vel(data), copy=False), "b-", label="v")
32 plt.legend()
34 plt.subplot(313)
35 plt.ylabel("Acceleration")
36 plt.plot(data, np.array(traj.acc(data), copy=False), "g-", label="a")
37 plt.legend()
39 plt.xlabel("t")
40 plt.savefig(os.path.join(base, "trajectory_trapezoidal.png"))
41 plt.show()