Time response analysis of a 1st order system with PID feedback control loop and step input using python

166 Views Asked by At

I am new to control theory. Since I don't have a Matlab license, I am working with python to test my understanding of various control theory concepts. I am using python's control system library. Recently I have started working on time response analysis of 1st order closed loops systems. I have written the below program to plot the step responses.

import matplotlib.pyplot as plt

from matplotlib.widgets import Slider

import control as ctl 
from control.matlab import step

import numpy as np 

T = np.linspace(0,10,1000)
s = ctl.tf("s")

fig,ax = plt.subplots()
plt.subplots_adjust(left = 0.1,bottom=0.4)

# plt.axis ([0,10,0,2])

pid,=plt.plot(T,T,color="green",lw=2,label="PID")
p,=plt.plot(T,T,color="blue",lw=2,label="P")
pd,=plt.plot(T,T,color="red",lw=2,label="PD")
pi,=plt.plot(T,T,color="black",lw=2,label="PI")
plt.legend()
plt.xlabel('Time')
plt.ylabel('Response')
plt.title('1st order system with various feedback controls')


axSliderKp = plt.axes([0.1,0.3,0.8,0.05])
axSliderTi =plt.axes([0.1,0.2,0.8,0.05])
axSliderTd =plt.axes([0.1,0.1,0.8,0.05])

Kp_Slider = Slider(axSliderKp,"Kp",valmin =0.0001,valmax=100,valinit=100,valfmt = "%1.5f",valstep=0.01,color="cyan",closedmin=False)
Ti_Slider = Slider(axSliderTi,"Ti",valmin =0.0001,valmax=10,valinit=1,valfmt = "%1.1f",valstep=0.01,color="magenta",closedmin=False)
Td_Slider = Slider(axSliderTd,"Td",valmin =0.0001,valmax=1,valinit =1,valfmt = "%1.5f",valstep=0.01,color ="yellow",closedmin=False)

def val_update(val):
    Kp = Kp_Slider.val
    Ti = Ti_Slider.val 
    Td = Td_Slider.val

    PID_part = Kp*(1+1/(Ti*s) +(Td*s))
    P_Part = Kp
    PI_Part = Kp*(1+1/(Ti*s))
    PD_Part = Kp*(1+(Td*s))

    plant = 1/(s+3)

    PID_Plant =  PID_part*plant

    P_Plant = ctl.feedback(P_Part*plant)
    PI_Plant = ctl.feedback(PI_Part*plant)
    PD_Plant = ctl.feedback(PD_Part*plant)

    PID_res,t = step(PID_Plant)
    P_res , t = step(P_Plant)
    PI_res, t = step(PI_Plant)
    PD_res,t =step(PD_Plant)

    pid.set_ydata(PID_res)

    pid.set_xdata(t)

    p.set_ydata(P_res)

    p.set_xdata(t)

    pi.set_ydata(PI_res)

    pi.set_xdata(t)

    pd.set_ydata(PD_res)

    pd.set_xdata(t)

    plt.draw()

Kp_Slider.on_changed(val_update)
Ti_Slider.on_changed(val_update)
Td_Slider.on_changed(val_update)


plt.show()

This program takes inputs for Kp, Ti, and Td using sliders on matplotlib plot. I am able to get the plots for P, PI, and PD controls on this plot. But for some weird reason, the PID control giving the absurd plot for almost all the Kp, Ti, and Td that I have tried. Below is the plot for some values of PID.

The plot of P, PI,PD, and PID controls of a closed-loop 1st order system

Zoomed-in plot

Why is the PID plot the way it is? Is the PID making the system unstable?

And also at Td=0, the P plot and PD plot are matching with each other. Then shouldn't the PID and PI plots also match at Td=0? Refer to the below plot.

Plot at Td=0

I have been trying to understand this and make sense of it for the last couple of days. I have no luck so far. Can any of you kindly help me understand this?

1

There are 1 best solutions below

0
On BEST ANSWER

I am extremely dumb. I made a mistake in the program. I failed to compute the feedback for the PID system.

This line:

PID_Plant =  PID_part*plant

should be changed to:

PID_Plant =  ctl.feedback(PID_part*plant)

Now I am getting the correct (I hope) plots.

P, PI, PD and PID plots of 1st order closed-loop system with unit step input