-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.py
56 lines (44 loc) · 1.3 KB
/
driver.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
# import numpy
class Driver(object):
"""Driver Model that models a PID controller"""
def __init__(self, k_p, k_i, k_d, delta_t):
self.k_p = k_p
self.k_i = k_i
self.k_d = k_d
self.delta_t = delta_t
self.v_i = 0
self.v_d = 0
self.alpha = 0
self.beta = 0
def compute_step(self, v_cyc, velocity):
"""Computes a single simulation step and return `alpha` (throttle) and `beta` (brake)"""
v_err = v_cyc - velocity
# Compute PID controller values
v_p = self.k_p * v_err
self.v_i = self.v_i + self.k_i * v_err * self.delta_t
self.v_d = ((self.k_d * v_err) - self.v_d) / self.delta_t
alpha = v_p + self.v_i + self.v_d
beta = alpha
# Saturate I term
if self.v_i > 0.75:
self.v_i = 0.75
elif self.v_i < -0.75:
self.v_i = -0.75
if alpha >= 1:
self.alpha = 1
else:
self.alpha = alpha
if beta <= -1:
self.beta = -1
else:
self.beta = beta
if v_err > 0:
self.beta = 0
else:
self.alpha = 0
return {
"alpha": self.alpha,
"beta": self.beta,
"v_p": v_p,
"v_i": self.v_i
}