-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpid_class.py
More file actions
70 lines (61 loc) · 2.48 KB
/
pid_class.py
File metadata and controls
70 lines (61 loc) · 2.48 KB
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
import time
class pid(object):
def __init__(self, ilk_p=0, ilk_i=0, ilk_d=0, ilk_imax=0):
# default config dosyasi
self.p_kazanci = ilk_p
self.i_kazanci = ilk_i
self.d_kazanci = ilk_d
self.imax = abs(ilk_imax) # mutlak deger aliyor /integral degerinin maksimum degerini gosteriyor eger hata cok artarsa saturasyon icin
self.integrator = 0 # integral toplaminin degeri
self.son_hata = None
self.son_yenileme = time.time()
# __str__ - pozisyon vektorunu yazdir
def __str__(self):
return "P:%s,I:%s,D:%s,IMAX:%s,Integrator:%s" % (self.p_kazanci, self.i_kazanci, self.d_kazanci, self.imax, self.integrator)
# get_dt - zaman farkini son yenilemeden aliyor
def get_dt(self, max_dt):
simdi = time.time()
dt = simdi - self.son_yenileme
self.son_yenileme = simdi
if dt > max_dt:
return 0.0
else:
return dt
#get_p - p terimini getirir
def get_p(self, hata):
return self.p_kazanci * hata
# get_i - i terimini getirir
def get_i(self, hata, dt):
self.integrator = self.integrator + hata * self.i_kazanci * dt
self.integrator = min(self.integrator, self.imax) #iki terimin kucuk olani
self.integrator = max(self.integrator, -self.imax) #iki terimin buyuk olani
return self.integrator
# get_d - d terimini getirir
def get_d(self, hata, dt):
if self.son_hata is None:
self.son_hata = hata
ret = (hata - self.son_hata) * self.d_kazanci * dt
self.son_hata = hata
return ret
#pi blogunu cagirir
def get_pi(self, hata, dt):
return self.get_p(hata) + self.get_i(hata , dt)
#pd blogunu cagirir
def get_pd(self , hata , dt):
return self.get_p(hata) + self.get_d(hata , dt)
#pid blogunu cagirir
def get_pid(self,hata,dt):
return self.get_p(hata) + self.get_i(hata , dt) + self.get_d(hata , dt)
#integrali yeniden ayarlamak icin
def get_integrator(self):
return self.integrator
#reset_i integrali resetlemek icin
def reset_i(self):
self.integrator = 0
def main(self):
print "ayarladi"
#result_p = test_pid.get_p(girdi)
#result_i = test_pid.get_i(girdi,0.01)
#result_d = test_pid.get_d(girdi,0.01)
#result = result_p + result_i + result_d
#print "Err %s, Result: %f (P:%f, I:%f, D:%f, Int:%f)" % (i, result, result_p, result_i, result_d, self.get_integrator())'''