-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimple_pid.py
59 lines (41 loc) · 1.26 KB
/
simple_pid.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
import cv2
import numpy as np
import time
class pid_control:
def __init__(self, _kp=0.1, _ki=0, _kd=0.0001):
self.Kp = _kp
self.Ki = _ki
self.Kd = _kd
self.current_value = 0
self.cycle = 1
self.error = 0
self.dt_error = 0
def calc_p(self):
return round(float(self.Kp * self.error), 4)
def calc_i(self):
return round(float(self.Ki * self.dt_error), 4)
def calc_d(self, _error):
return round(float(self.Kd * _error), 4)
def pid_calc(self, _target):
_c = 0
self.error = _target - self.current_value
_c += self.calc_p()
self.dt_error += self.error
_c += self.calc_i()
temp = self.dt_error / self.cycle
_c += self.calc_d(temp)
self.current_value += _c
self.cycle += 1
def get_current_value(self):
return self.current_value
def __str__(self):
return "Current value = {0}, Error = {1}, Cycle = {2}".format(
self.current_value, self.error, self.cycle
)
if __name__ == "__main__":
pid_controller = pid_control()
target_value = 5123
while True:
pid_controller.pid_calc(target_value)
print(pid_controller)
time.sleep(0.01)