-
Notifications
You must be signed in to change notification settings - Fork 1
/
hikersim.py
65 lines (47 loc) · 1.57 KB
/
hikersim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import numpy as np
x_stop = -2.5
x_brake = -38.5
def braking_spec(time_gap, speed, x_stop=x_stop, x_brake=x_brake):
if x_brake >= x_stop:
return 0, np.inf, np.inf
v0 = speed
x0 = -time_gap*v0
brake_dist = x_stop - x_brake
a = -v0**2/(2*brake_dist)
brake_dur = -v0/a
t_brake = (x_brake - x0)/v0
t_stop = t_brake + brake_dur
return a, t_brake, t_stop
def simulate_trajectory(t, time_gap, speed, braking, x_stop=x_stop, x_brake=x_brake):
v0 = speed
x0 = -time_gap*v0
if not braking:
x = t*v0 + x0
v = np.repeat(speed, len(t))
return x, v, (np.inf, np.inf)
a, t_brake, t_stop = braking_spec(time_gap, speed, x_stop=x_stop, x_brake=x_brake)
v = np.zeros(len(t))
v = np.piecewise(t,
[(t <= t_brake), (t > t_brake) & (t <= t_stop)],
[lambda t: v0, lambda t: (t - t_brake)*a + v0, 0.0],
)
x = np.piecewise(t,
[(t <= t_brake), (t > t_brake) & (t <= t_stop)],
[lambda t: (t*v0 + x0), lambda t: (t_brake*v0 + x0) + (t - t_brake)*v0 + a/2*(t - t_brake)**2, x_stop]
)
return x, v, (t_brake, t_stop)
def test():
import matplotlib.pyplot as plt
v0 = 25/2.237
time_gap = 2.0
trng = np.linspace(-3, 20, 100)
x, v, (t_brake, t_stop) = simulate_trajectory(trng, time_gap, v0, True)
#plt.plot(trng, v)
#plt.plot(trng, x)
tau = -x/v
plt.plot(trng, tau)
plt.axvline(0, color='black')
plt.axhline(time_gap, color='black')
plt.show()
if __name__ == '__main__':
test()