Shortly, I have a project for autonomous control of DC motors using Pi Camera. I've designed a Fuzzy PID Controller to do so.

PID controller's feedback is the location and radius of the target object on the camera interface. And PID controller's Kp, Ki and Kd gains are adjusted via a Mamdani Fuzzy Inference System (FIS). Then I control the motors using PWM signals. During the process I use VNC Viewer and I get the video output of the camera. So, I didn't disable these yet but if it's possible I want to keep at least the VNC Viewer.

My issue is, Mamdani FIS is running extremely slowly to calculate the result value. Takes nearly 5 seconds. I need it to calculate the value instantly. Here's the Mamdani FIS code below:

Code: Select all

```
import numpy as np
#Trapezoidal Membership Function
def trpmf(alt,ust,a,b,c,d,xi):
global x
global mu_x
global mu_xi
x1 = np.arange(alt,a-.001,.001)
x2 = np.arange(a,b-.001,.001)
x3 = np.arange(b,c-.001,.001)
x4 = np.arange(c,d-.001,.001)
x5 = np.arange(d,ust,.001)
x = np.concatenate([x1,x2,x3,x4,x5])
alfa = 1
mux1= np.zeros(x1.shape)
mux2= alfa*(x2-a)/(b-a)
mux3= alfa*np.ones(x3.shape)
mux4= alfa*(x4-d)/(c-d)
mux5= np.zeros(x5.shape)
mu_x = np.concatenate([mux1, mux2, mux3, mux4, mux5])
if xi>=a and xi<b and a!=b:
mu_xi=alfa*(xi-a)/(b-a)
elif xi>=a and xi<b and a==b:
mu_xi=alfa
elif xi>=b and xi<=c:
mu_xi=alfa
elif xi>c and xi<=d and c!=d:
mu_xi=alfa*(xi-d)/(c-d)
elif xi>c and xi<=d and c==d:
mu_xi=alfa
else:
mu_xi=0
return 0
#Triangular Membership Function
def trgmf(alt,ust,a,b,c,xi):
global x
global mu_x
global mu_xi
x1 = np.arange(alt,a-.001,.001)
x2 = np.arange(a,b-.001,.001)
x3 = np.arange(b,c-.001,.001)
x4 = np.arange(c,ust,.001)
x = np.concatenate([x1,x2,x3,x4])
alfa = 1
mu_x1 = np.zeros(x1.shape)
mu_x2 = alfa*(x2-a)/(b-a)
mu_x3 = alfa*(x3-c)/(b-c)
mu_x4 = np.zeros(x4.shape)
mu_x = np.concatenate([mu_x1, mu_x2, mu_x3, mu_x4])
if xi>=a and xi<=b:
mu_xi = (xi-a)/(b-a)
elif xi<=c and xi>b:
mu_xi = (xi-c)/(b-c)
else:
mu_xi = 0
return 0
#The Mamdani FIS
def mamdani_x(ei,dei,ki):
global x
global mu_x
global mu_xi
# e(t) Error Membership Functions
trpmf(-100,300,-55,-55,0,55,ei)
mu_e_SC = mu_xi
trgmf(-100,300,0,55,110,ei)
mu_e_CA = mu_xi
trgmf(-100,300,55,110,165,ei)
mu_e_A = mu_xi
trgmf(-100,300,110,165,220,ei)
mu_e_O = mu_xi
trgmf(-100,300,165,220,275,ei)
mu_e_F = mu_xi
trpmf(-100,300,220,275,300,300,ei)
mu_e_CF = mu_xi
# de(t) Derivative Error Membership Functions
trpmf(-100,300,-50,-50,-3,0,dei)
mu_de_N = mu_xi
trpmf(-100,300,0,3,100,100,dei)
mu_de_P = mu_xi
trgmf(-100,300,-3,0,3,dei)
mu_de_SC = mu_xi
# K(z) Output Membership Function
trpmf(-100,300,3.15,3.15,3.25,3.3,ki)
mu_S = mu_x
trgmf(-100,300,3.25,3.3,3.35,ki)
mu_CD = mu_x
trgmf(-100,300,3.3,3.35,3.4,ki)
mu_D = mu_x
trgmf(-100,300,3.35,3.4,3.45,ki)
mu_OM = mu_x
trgmf(-100,300,3.4,3.45,3.5,ki)
mu_Y = mu_x
trpmf(-100,300,3.45,3.5,3.6,3.6,ki)
mu_CY = mu_x
#Rules Base via "AND" Operation
mu_kural1 = min(mu_e_SC,mu_de_N)
mu_kural2 = min(mu_e_CA,mu_de_N)
mu_kural3 = min(mu_e_A,mu_de_N)
mu_kural4 = min(mu_e_O,mu_de_N)
mu_kural5 = min(mu_e_F,mu_de_N)
mu_kural6 = min(mu_e_CF,mu_de_N)
mu_kural7 = min(mu_e_SC,mu_de_SC)
mu_kural8 = min(mu_e_CA,mu_de_SC)
mu_kural9 = min(mu_e_A,mu_de_SC)
mu_kural10 = min(mu_e_O,mu_de_SC)
mu_kural11 = min(mu_e_F,mu_de_SC)
mu_kural12 = min(mu_e_CF,mu_de_SC)
mu_kural13 = min(mu_e_SC,mu_de_P)
mu_kural14 = min(mu_e_CA,mu_de_P)
mu_kural15 = min(mu_e_A,mu_de_P)
mu_kural16 = min(mu_e_O,mu_de_P)
mu_kural17 = min(mu_e_F,mu_de_P)
mu_kural18 = min(mu_e_CF,mu_de_P)
#Result Fuzzy Sets of the Rules via "THEN" operation
mu_sonuc1 = np.dot(mu_kural1,mu_S)
mu_sonuc2 = np.dot(mu_kural2,mu_S)
mu_sonuc3 = np.dot(mu_kural3,mu_CD)
mu_sonuc4 = np.dot(mu_kural4,mu_D)
mu_sonuc5 = np.dot(mu_kural5,mu_OM)
mu_sonuc6 = np.dot(mu_kural6,mu_Y)
mu_sonuc7 = np.dot(mu_kural7,mu_S)
mu_sonuc8 = np.dot(mu_kural8,mu_CD)
mu_sonuc9 = np.dot(mu_kural9,mu_D)
mu_sonuc10 = np.dot(mu_kural10,mu_OM)
mu_sonuc11 = np.dot(mu_kural11,mu_Y)
mu_sonuc12 = np.dot(mu_kural12,mu_CY)
mu_sonuc13 = np.dot(mu_kural13,mu_CD)
mu_sonuc14 = np.dot(mu_kural14,mu_D)
mu_sonuc15 = np.dot(mu_kural15,mu_OM)
mu_sonuc16 = np.dot(mu_kural16,mu_Y)
mu_sonuc17 = np.dot(mu_kural17,mu_CY)
mu_sonuc18 = np.dot(mu_kural18,mu_CY)
#Output Fuzzy Set via "IMPLEMENTATION" Operation
mu_b1 = max(max(mu_sonuc1,mu_sonuc2,key=tuple),mu_sonuc3,key=tuple)
mu_b2 = max(max(mu_sonuc4,mu_sonuc5,key=tuple),mu_sonuc6,key=tuple)
mu_b3 = max(max(mu_sonuc7,mu_sonuc8,key=tuple),mu_sonuc9,key=tuple)
mu_b4 = max(max(mu_sonuc10,mu_sonuc11,key=tuple),mu_sonuc12,key=tuple)
mu_b5 = max(max(mu_sonuc13,mu_sonuc14,key=tuple),mu_sonuc15,key=tuple)
mu_b6 = max(max(mu_sonuc16,mu_sonuc17,key=tuple),mu_sonuc18,key=tuple)
mu_bx = max(max(mu_b1,mu_b2,key=tuple),mu_b3,key=tuple)
mu_by = max(max(mu_b4,mu_b5,key=tuple),mu_b6,key=tuple)
mu_birlestirme = max(mu_bx,mu_by,key=tuple)
#The Result of Numerical Output via "DEFUZZYICATION" Operation
a_cbk = sum(mu_birlestirme)
k = sum([np.dot(mu_birlestirme,x)])/a_cbk
return k
Kp = mamdani_x(275,-21,0)
Ki = Kp/2
Kd = Ki/2
print(Kp)
```

Any help will be appreciated.