support for PyPy
[liba.git] / python / test / 3rd / trajpoly3.py
blob4f6f72aa04594b97b7eb144ee1d3c275e8359ece
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 t = (0, 1)
18 p = (0, 1)
19 v = (0, 1)
20 traj = liba.trajpoly3(t[1] - t[0], p[0], p[1], v[0], v[1])
21 data = np.arange(t[0], t[1], (t[1] - t[0]) / 1000)
23 text_p = "p="
24 if traj.p[0]:
25 text_p += "%g" % (traj.p[0])
26 if traj.p[1]:
27 text_p += "%+g%s" % (traj.p[1], "t")
28 if traj.p[2]:
29 text_p += "%+g%s" % (traj.p[2], "t^2")
30 if traj.p[3]:
31 text_p += "%+g%s" % (traj.p[3], "t^3")
32 text_p = text_p.replace("=+", "=")
34 text_v = "v="
35 if traj.v[0]:
36 text_v += "%g" % (traj.v[0])
37 if traj.v[1]:
38 text_v += "%+g%s" % (traj.v[1], "t")
39 if traj.v[2]:
40 text_v += "%+g%s" % (traj.v[2], "t^2")
41 text_v = text_v.replace("=+", "=")
43 text_a = "a="
44 if traj.a[0]:
45 text_a += "%g" % (traj.a[0])
46 if traj.a[1]:
47 text_a += "%+g%s" % (traj.a[1], "t")
48 text_a = text_a.replace("=+", "=")
50 plt.figure("3 polynomial trajectory")
52 plt.subplot(311)
53 plt.title("cubic polynomial trajectory")
54 plt.ylabel("Position")
55 plt.plot(data, np.array(traj.pos(data), copy=False), "r-", label=text_p)
56 plt.legend()
58 plt.subplot(312)
59 plt.ylabel("Velocity")
60 plt.plot(data, np.array(traj.vel(data), copy=False), "b-", label=text_v)
61 plt.legend()
63 plt.subplot(313)
64 plt.ylabel("Acceleration")
65 plt.plot(data, np.array(traj.acc(data), copy=False), "g-", label=text_a)
66 plt.legend()
68 plt.xlabel("t")
69 plt.savefig(os.path.join(base, "trajectory_polynomial_3.png"))
70 plt.show()