-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
138 lines (98 loc) · 3.75 KB
/
controller.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
from MotorController import MCInterface
import threading
from time import sleep
# Store necessary values to perform sliding window/Kalman filtering and other filtering
class InputFilter:
def __init__(self):
# Initialize
self.prevTime = None
self.Kp = 1.5
self.Ki = 0.3
self.Kd = 0.7
self.output_threshold = 5
def filter(self, cur_velocity):
return self.check_thresholds(cur_velocity)
def check_thresholds(self, cur_velocity):
cur_velocity = cur_velocity * 150
if -1 * self.output_threshold < cur_velocity < self.output_threshold:
return 0
return cur_velocity
def error2vel(self, error):
return error
# Not in use currently
def pid(self, cur_velocity, new_velocity, cur_time, error):
# Record the time
if self.prevTime is None:
self.prevTime = cur_time
prevError = error
intError = error
return
deltaT = (cur_time - self.prevTime) * 100
self.prevTime = cur_time
# error = currVelocity - prevVelocity
self.logger.info(error, cur_velocity, deltaT)
# if(abs(error/prevVelocity) > 0.5):
# continue
diffError = (error - self.prevError) / deltaT
intError = self.prevError + (error * deltaT)
self.prevError = error
self.logger.info(diffError, intError)
cur_velocity = self.Kp * error + self.Kd * diffError + self.Ki * intError
cur_velocity = int(cur_velocity)
return cur_velocity
class ControllerLoop(threading.Thread):
def __init__(self, threadID, med_dist_queue, lat_dist_queue, logger):
self.logger = logger
threading.Thread.__init__(self)
self.threadID = threadID
self.mc = MCInterface()
self.input_filter = InputFilter()
self.med_dist_queue = med_dist_queue
self.lat_dist_queue = lat_dist_queue
self.kill_received = False
def run(self):
while not self.kill_received:
if self.lat_dist_queue.empty():
self.medial_drive()
else:
self.lateral_drive()
sleep(0.5)
self.stop()
def stop(self):
self.mc.forwardM0(0)
self.mc.forwardM1(0)
def set_velocity(self, cur_velocity):
if cur_velocity > 0:
self.mc.reverseM0(cur_velocity)
self.mc.reverseM1(cur_velocity)
else:
norm_vel = abs(int(2*cur_velocity))
self.mc.forwardM0(norm_vel)
self.mc.forwardM1(norm_vel)
self.logger.info("Tuned & normalized velocity " + str(cur_velocity))
def set_lateral_velocity(self, cur_velocity):
print("Lateral Drive!!! --->" + str(cur_velocity))
norm_vel = int(0.7 * cur_velocity)
if cur_velocity > 0:
self.mc.forwardM0(norm_vel)
self.mc.reverseM1(norm_vel)
else:
norm_vel = abs(int(cur_velocity))
self.mc.reverseM0(norm_vel)
self.mc.forwardM1(norm_vel)
def medial_drive(self):
if self.med_dist_queue.empty():
self.stop()
return
self.logger.debug("Queue: " + str(self.med_dist_queue.queue))
error = self.med_dist_queue.get()
cur_velocity = self.input_filter.error2vel(error)
cur_velocity = self.input_filter.filter(cur_velocity)
self.set_velocity(cur_velocity)
def lateral_drive(self):
self.logger.debug("Queue: " + str(self.lat_dist_queue.queue))
error = self.lat_dist_queue.get()
#print("Retreived --> " + str(error))
cur_velocity = self.input_filter.error2vel(error)
cur_velocity = self.input_filter.filter(cur_velocity)
self.set_lateral_velocity(cur_velocity)