|
1 | 1 | import numpy as np |
2 | 2 | import matplotlib.pyplot as plt |
3 | 3 | from numpy.polynomial import Chebyshev as T |
| 4 | +from utils import create_toy_data |
4 | 5 |
|
5 | 6 | if __name__ == "__main__": |
6 | 7 | velocity = 1 # m/s |
7 | | - DT = 1 # sec |
| 8 | + DT = 5 # sec |
8 | 9 | start_pos = 0 # meters |
9 | 10 | time = 0.0 |
10 | 11 |
|
11 | | - plane_height = 5 |
| 12 | + plane_height = 10 |
12 | 13 |
|
13 | 14 | pos = start_pos |
14 | 15 |
|
|
18 | 19 | y3 = T.basis(4)(x) + T.basis(5)(x) |
19 | 20 |
|
20 | 21 | ly = np.concatenate((y2, y1, y3)) |
21 | | - ly = ly + abs(np.min(ly)) |
| 22 | + ly_min = abs(np.min(ly)) |
| 23 | + ly = ly + (ly_min*2) |
| 24 | + |
22 | 25 | lx = np.arange(len(ly)) |
23 | 26 |
|
| 27 | + # std |
| 28 | + std = 1 |
| 29 | + y_sine_noise = create_toy_data(ly, std) |
| 30 | + |
24 | 31 | SIM_TIME = len(ly) |
25 | 32 |
|
26 | 33 | show_animation = True |
27 | 34 |
|
28 | 35 | fig, axs = plt.subplots(1, 1, figsize=(14, 5)) |
29 | | - axs.set_axisbelow(True) |
| 36 | + ax = axs |
| 37 | + ax.set_axisbelow(True) |
30 | 38 |
|
31 | | - while SIM_TIME >= time: |
32 | | - time += DT |
33 | | - print(time, ly[int(time)], end='\r') |
34 | | - pos += velocity * DT |
| 39 | + # while SIM_TIME > time: |
| 40 | + for pos in range(0, len(lx), DT): |
35 | 41 |
|
36 | 42 | if show_animation: |
37 | 43 | plt.cla() |
38 | 44 | # for stopping simulation with the esc key. |
39 | 45 | plt.gcf().canvas.mpl_connect('key_release_event', |
40 | 46 | lambda event: [exit(0) if event.key == 'escape' else None]) |
41 | 47 |
|
42 | | - axs.grid(ls='--') |
43 | | - axs.plot(lx, ly, c='g') |
44 | | - axs.fill_between(lx, ly, color="green", alpha=0.6) |
| 48 | + ax.grid(ls='--') |
| 49 | + ax.plot(lx, ly, c='g') |
| 50 | + ax.fill_between(lx, ly, color="green", alpha=0.6) |
| 51 | + |
| 52 | + # std range |
| 53 | + ax.fill_between(lx, ly - std, ly + std, |
| 54 | + color="gray", label="std.", alpha=0.5) |
| 55 | + |
| 56 | + # noise within std |
| 57 | + |
| 58 | + ax.scatter(lx, y_sine_noise, c='b', marker='*', lw=0.5) |
45 | 59 |
|
46 | 60 | # Plot plane route |
47 | | - axs.axhline(y=plane_height, xmin=0, xmax=1, |
48 | | - c='r', ls='--', lw=1) |
| 61 | + ax.axhline(y=plane_height, xmin=0, xmax=1, |
| 62 | + c='r', ls='--', lw=1) |
49 | 63 | # plane current pos |
50 | | - axs.scatter(pos, plane_height, c='r', lw=3) |
| 64 | + ax.scatter(pos, plane_height, c='r', lw=3, marker='>') |
51 | 65 |
|
52 | 66 | # distance to current height |
53 | | - axs.scatter(pos, ly[int(time)], c='k', lw=3) |
| 67 | + |
| 68 | + ax.scatter(pos, ly[pos], c='k', lw=3) |
54 | 69 |
|
55 | 70 | # settings |
56 | | - axs.grid(True) |
| 71 | + ax.grid(True) |
57 | 72 | # plt.xlim(-1, SIM_TIME) |
58 | | - axs.set_xlabel('X Position (m)') |
59 | | - axs.set_ylabel('Height (m)') |
60 | | - axs.set_title('Simulation time: {:.2f} seconds'.format(time)) |
| 73 | + ax.set_xlabel('X Position (m)') |
| 74 | + ax.set_ylabel('Height (m)') |
| 75 | + ax.set_title('Simulation time: {:.1f} seconds'.format(pos+DT)) |
61 | 76 |
|
62 | 77 | plt.pause(0.001) |
| 78 | + |
| 79 | + if show_animation: |
| 80 | + plt.show() |
0 commit comments