-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathramps.py
143 lines (94 loc) · 3.05 KB
/
ramps.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import math
# maximum acceleration, assuming we aim to go from 0 to 100 rpm in 1 s
# a = 100 rpm / 1s = 100.0 / 60.0 rotations / 1s / 1s = 100.0 / 60.0 * (2 * pi) [rad / s^2]
a_m = 100.0 / 60.0 * (2 * math.pi) # [rad/s^2]
# maximum velocity, assuming 100 rpm
v_M = 100.0 / 60.0 * (2 * math.pi) # [rad/s]
def calculate_ramp_times(start_position, start_velocity, target_position):
"""
Implementation of doi:10.1088/1757-899X/294/1/012055 Chapter #2
"""
# m is the ratio of acceleration to deceleration
m = 1.0
s = target_position - start_position
a = a_m
v_s = start_velocity
T = -v_s * (m + 1) / a + math.sqrt((v_s * (m + 1) / a)**2 + 2 * s * (m + 1) / a)
t_a = T / (m + 1)
t_d = T - t_a
v = v_s + a * t_a
if v <= v_M:
# triangular speed profile
# we are done, only need the time values
t_v = 0.0
pass
else:
# trapezoidal speed profile
v = v_M
t_a = (v - v_s) / a
T_ad = t_a * (m + 1)
t_d = T_ad - t_a
s_ad = v_s * T_ad + a * T_ad**2 / (2 * (m + 1))
t_v = (s - s_ad) / v
T = T_ad + t_v
pass
return (t_a, t_v, t_d)
def test_scenario(target_position, steps_per_second):
t_a, t_v, t_d = calculate_ramp_times(0.0, 0.0, target_position)
print(f"Times: a: {t_a}, v: {t_v}, d: {t_d} [s]")
result_t = list() # time
result_a = list() # acceleration
result_v = list() # speed
result_x = list() # position
t = 0.0
a = 0.0
v = 0.0
x = 0.0
dt = 1.0 / steps_per_second
for i in range(int(t_a * steps_per_second)):
t = dt * i
a = a_m
v += a * dt
x += v * dt
result_t.append(t)
result_a.append(a)
result_v.append(v)
result_x.append(x)
for i in range(int(t_v * steps_per_second)):
t = t_a + dt * i
a = 0.0
v += a * dt
x += v * dt
result_t.append(t)
result_a.append(a)
result_v.append(v)
result_x.append(x)
for i in range(int(t_d * steps_per_second)):
t = t_a + t_v + dt * i
a = -a_m
v += a * dt
x += v * dt
result_t.append(t)
result_a.append(a)
result_v.append(v)
result_x.append(x)
return result_t, result_a, result_v, result_x
def main():
import matplotlib.pyplot as plt
print(f"Maximum acceleration: {a_m} [rad/s^2]")
print(f"Maximum speed: {v_M} [rad/s]")
result_t, result_a, result_v, result_x = test_scenario(100.0, 10000)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(result_t, result_a, color='tab:blue', label="Acceleration [rad/s^2]")
ax.plot(result_t, result_v, color='tab:orange', label="Speed [rad/s]")
ax.plot(result_t, result_x, color='tab:red', label="Position [rad]")
# set the limits
#ax.set_xlim([0, 1])
#ax.set_ylim([0, 1])
#ax.set_title('line plot with data points')
plt.legend(loc='lower right')
# display the plot
plt.show()
if __name__ == "__main__":
main()