-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPD.py
49 lines (33 loc) · 1.32 KB
/
PD.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
from util import *
# proportional function, curve1(x) = 1 (if x >= 0.0126), curve1(-0.0063) = -0.1250235
def curve1(x):
if x > .5:
x = 1 - Range(x, 1)
s = x * x * x * 5e5
return Range(s, 1)
def linear1(x):
return Range(x / 60, 1)
# PD for steering
def steer_from_angle(angle, derivative, pi=PI):
return curve1(Range180(angle - derivative / 19, PI) / PI)
def time_until_full_stop(speed):
return abs(speed) / BRAKES_RATE + 1 / 60
def time_until_desired_velocity(relative_vel):
if relative_vel > 0:
rate = 2100
else:
rate = 9000
return abs(relative_vel) / rate
# PD for throttling, uses a 1-dimensional location for forwards or backwards
def throttle_to_point(location, velocity, brakes=True):
return sign(location - time_until_full_stop(velocity) * brakes * velocity) * linear1(abs(location) + abs(velocity))
def throttle_velocity(velocity, desired_velocity, last_throttle):
relative_vel = desired_velocity - velocity - last_throttle * 9
return Range(time_until_desired_velocity(relative_vel) * 60 * sign(relative_vel), 1)
def boost_velocity(velocity, desired_velocity, last_boost):
relative_vel = desired_velocity - velocity - last_boost * 20
if velocity < 1400:
threshold = 200
else:
threshold = 20
return relative_vel > threshold