Pid
Pid
GPIO.23 providees PWM pulses to drive DC motor (DC motor drive between motor and RPi)
"""
import threading
import time
GPIO.setmode(GPIO.BCM)
GPIO.output(23, 0)
feedback=0.0
previous_time =0.0
previous_error=0.0
Integral=0.0
D_cycal=10
RunRPM=0
Loop_value=0
a=0
avr=0
i=0
def PID_function():
global previous_time
global previous_error
global Integral
global D_cycal
global Kp
global Ki
global Kd
error = int(Set_RPM) -feedback # Differnce between expected RPM and run RPM
if (previous_time== 0):
previous_time =time.time()
current_time = time.time()
Integral=10
if Integral<-10:
Integral=-10
Iout=((Ki/10) * Integral)
previous_time = current_time
previous_error = error
Dout=((Kd/1000 )* Derivative)
if ((output>D_cycal)&(D_cycal<90)):
D_cycal+=1
if ((output<D_cycal)&(D_cycal>10)):
D_cycal-=1
return ()
def RPM_function():
global feedback
tc=time.time()
while (GPIO.input(22)==False):
v=0
ts=time.time()
time_count=ts-tc
if (time_count>7):
feedback=0
return ()
while (GPIO.input(22)==True):
i=0
ts=time.time()
time_count=ts-tc
if (time_count>7):
feedback=0
return ()
while (GPIO.input(22)==False):
s=0
while (GPIO.input(22)==True):
h=0
feedback = w
return ()
def main_function():
global D_cycal
if Loop_value==1:
t1 = threading.Thread(target=RPM_function)
t1.start()
t1.join()
t2 = threading.Thread(target=PID_function)
t2.start()
t2.join()
GatePulse.ChangeDutyCycle(D_cycal)
else:
GatePulse.ChangeDutyCycle(0)
def change_Kp_value(slider_value):
global Kp
Proportional.value = slider_value
Kp=int(slider_value)
def change_Ki_value(slider_value):
global Ki
Intregral.value = slider_value
Ki= int(slider_value)
def change_Kd_value(slider_value):
global Kd
Deravative.value = slider_value
Kd=int(slider_value)
def change_SetRPM_value(slider_value):
global Set_RPM
SetRPM.value = slider_value
Set_RPM=int(slider_value)
def update_rpm():
global avr
global i
global a
global feedback
if(Loop_value==1):
if i<6:
a+=feedback
i+=1
else:
Run_RPM.value = int(a/6)
a=0
i=0
else:
Run_RPM.value = 0
def start_funcdtion():
global Loop_value
GatePulse.start(25)
Loop_value=1
Startbutton.toggle()
Startbutton.toggle()
Startbutton.repeat(1, main_function)
def Stop_function():
global Loop_value
Loop_value=0
Startbutton.cancel(main_function)
Run_RPM.value=00
D_cycal=0
Stopbutton.toggle()
Stopbutton.toggle()
GatePulse.ChangeDutyCycle(0)
def close_window():
Stop_function()
app.hide()
message = Text(app, text="PID based DC motor controller", color="saddle brown", size= 20)
Startbutton.text_color="dark green"
Startbutton.text_size=15
SetRPM.text_color="blue"
Proportional.text_color="DarkOliveGreen4"
Intregral.text_color="DarkOliveGreen4"
Deravative.text_color="DarkOliveGreen4"
Run_RPM.text_color="tomato"
Run_RPM = Text(app, text= RunRPM)
Run_RPM.repeat(1, update_rpm)
Stopbutton.text_color="red"
Stopbutton.text_size=15
#close_button.text_coller="yellow"
close_button.text_size=20
app.display()