-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpidtest2.py
68 lines (55 loc) · 1.2 KB
/
pidtest2.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
import encoder
import wpilib
import motors
total_l, total_r = 0, 0
lol2 = []
def inp(l, r):
def fun():
global total_l, total_r
total_l += l.get_steps()
total_r += r.get_steps()
if total_r == 0 or total_l == 0:
i = 1
else:
i = total_r / total_l
if i > 10:
return 10
lol2.append(i)
return i
return fun
speed = 100
lol = []
def out(i):
lol.append(i)
motors.leftSet(int(speed * (1-i)) if i > 0 else speed)
motors.rightSet(int(speed * (1+i)) if i < 0 else speed)
def create():
l, r = encoder.getLeftRight()
kp = 5
ki = 1
kd = 1
kf = 1
source = inp(l, r)
output = out
ctrlr = wpilib.PIDController(kp, ki, kd, kf, source, output)
ctrlr.setInputRange(0, 10)
ctrlr.setOutputRange(-1.0, 1.0)
ctrlr.setAbsoluteTolerance(0.1)
ctrlr.setContinuous()
return ctrlr
def stop(c):
c.disable()
motors.bothSet(0)
print(total_l, total_r)
def start(c):
global total_l, total_r
total_l = 0
total_r = 0
c.origSource()
c.setSetpoint(1)
c.enable()
def f(func):
while not func():
pass
if __name__ == '__main__':
create()