5.6. Time domain controller design#

5.6.1. Time domain PID controller design#

We can write a time function that describes a generic PID controller:

\[\begin{align*} u(t) &= k_p \left ( b_{sp} r(t) - y(t) \right ) + k_i \int_0^t \left (r(\tau)-y(\tau) \right) d \tau + k_d \frac{d(r(t)-y(t))}{dt} \end{align*}\]

The Laplace transform of this equation (if we assume that \(y(0)=\dot y(0)=r(0)= \dot r(0)=0\)) is equal to

\[\begin{align*} U(s) &= k_p \left ( b_{sp} R(s) - Y(s) \right ) + k_i \frac{R(s)-Y(s)}{s} + k_d s \left( R(s)-Y(s) \right) \end{align*}\]

with \(k_p\) the propotional error gain, \(k_i\) the integrational error gain and \(k_d\) the differential error gain. This equation include an additional \(b_{sp}\) setpoint gain/weight that will be essential for this approach.

The figure shown below visualized the control loop include a first order system defined as

\[\begin{align*} H(s)&= \frac{Y(s)}{U(s)}=\frac{K}{\tau s +1} \label{eq:H(s)} \tag{2.16} \end{align*}\]

assuming that \(V_{sd}=0\) making \(v_m(t) =u(t)\) together with the PID controller with setpoint weighting \(b_{sp}\).

TD_control

Fig.15 - PID control loop with set point weighting.

The closed loop transfer function from R(s) to Y(s) can be found by substituting U(s) by

\[\begin{align*} U(s) &= \frac{\tau s +1}{K} Y(s) \end{align*}\]

in equation \(\ref{eq:H(s)}\) leading to

\[\begin{align*} \frac{\tau s +1}{K} Y(s) &=k_p \left ( b_{sp} R(s) - Y(s) \right ) + k_i \frac{R(s)-Y(s)}{s} + k_d s \left( R(s)-Y(s) \right) ). \end{align*}\]

After isolating the terms in R(s) and Y(s) we get

\[\begin{align*} \left(\frac{\tau s +1}{K} + k_p + \frac{k_i}{s} + k_d s \right) Y(s) &= \left(k_p b_{sp} + \frac{k_i}{s} + k_ds \right) R(s) \end{align*}\]

leading to the closed loop transfer function

(5.14)#\[\begin{align} \frac{Y(s)}{R(s)} &= \frac{k_p b_{sp} + \frac{k_i}{s} + k_ds }{\frac{\tau s +1}{K} + k_p + \frac{k_i}{s} + k_d s} \\ &= \frac{K(k_p b_{sp} + k_i + k_d s^2) }{(\tau s +1)s + k_p K s+ K k_i + K k_d s^2} \\ &= \frac{K(k_p b_{sp} + k_i + k_d s^2) }{(\tau + K k_d) s^2+ (1 + k_p K) s+ K k_i } \label{eq:CL} \tag{2.14} \end{align}\]

If you want equation \(\ref{eq:CL}\) to fit the standard second order transfer function model

\[\begin{align*} H(s) &= \frac{\omega_n^2}{s^2+2 \zeta \omega_n+ \omega_n^2} \end{align*}\]

with \(\omega_n\) the natural frequency and \(\zeta\) the damping coefficient that can be directly relate to time conditions (\(t_s,t_p,t_r\), …) then we need to take \(k_d=0\) and \(b_sp=0\).

\[\begin{align*} \frac{Kk_i }{\tau s^2+ (1 + k_p K) s+ K k_i } &= \frac{\omega_n^2}{s^2+2 \zeta \omega_n s+ \omega_n^2} \end{align*}\]

or

\[\begin{align*} \frac{Kk_i/\tau }{s^2+ (1 + k_p K)/\tau s+ K k_i/\tau } &= \frac{\omega_n^2}{s^2+2 \zeta \omega_n s+ \omega_n^2}. \end{align*}\]

This lead to the following two conditions for \(k_i\) and \(k_p\) given desired values for \(\zeta\) and \(\omega_n\) (specifications) and a system defined by K and \(\tau\) (given in advance):

\[\begin{align*} 2 \zeta \omega_n &= \frac{1 + k_p K}{\tau} \label{eq:TD2} \tag{2.15}\\ \omega_n^2 &= \frac{K k_i}{\tau} \label{eq:TD3} \tag{2.16} \end{align*}\]

5.6.2. Time domain controller design in software#

To design a time domain controller in software we need to follow 5 steps:

  1. Specify your transfer function

  2. Determine \(\zeta\) and \(\omega_n\)

  3. Determine K and \(\tau\) from the transfer function

  4. Calculate the gains: \(k_i\) and \(k_p\) using equations \(\ref{eq:TD2},\ref{eq:TD3}\)

  5. Simulate the closed loop response

Example: We have a first order transfer function \(H(s) =\frac{3}{0.2s+1}\) and we want to design a controller that has an overshoot of 3% and a rise time \(t_r\) of 0.8 seconds.

5.6.2.1. 1. We specify a transfer function:#

from control.matlab import tf,dcgain,pole,step,damp,stepinfo 
import numpy as np 
import matplotlib.pyplot as plt # Load the pyplot object fromthe matplotlib library
#from Mycode import response

sys = tf([3],[0.2,1])
print(sys)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 1
----> 1 from control.matlab import tf,dcgain,pole,step,damp,stepinfo 
      2 import numpy as np 
      3 import matplotlib.pyplot as plt # Load the pyplot object fromthe matplotlib library

ModuleNotFoundError: No module named 'control'

5.6.2.2. 2. We determine \(\zeta\) and \(\omega_n\) from time conditions#

Use the formulas derived in section 2.4 to determine the natural frequency (\(\omega_n\)) and damping ratio (\(\zeta\)) based on the given time domain criteria being \(M_p,t_r,t_s\) or \(T_p\).

To determine the damping ratio (\(\zeta\)) the previously derived equation:

\[\begin{align*} M_p&= e^{-\zeta\pi/\sqrt{1-\zeta^2}} \end{align*}\]

can be rewritten as

\[\begin{align*} \zeta = \sqrt{\frac{-log(Mp)^2}{-log(M_p)^2-\pi^2}} \end{align*}\]

To determine the natural frequency (\(\omega_n\)) the previously derived equation:

\[\begin{align*} t_r&=\frac{\pi-\frac{\tan^{-1}(\sqrt{1-\zeta^2}}{\zeta})}{\omega_d} \end{align*}\]

can be rewritten as

\[\begin{align*} \omega_d&= \frac{\pi-\frac{\tan^{-1}(\sqrt{1-\zeta^2}}{\zeta})}{t_r} \end{align*}\]

with \(\omega_d=\omega_n \sqrt{1- \zeta^2}\) this leads to an equation for \(\omega_n\)

\[\begin{align*} \omega_n & = \frac{\pi-\frac{\tan^{-1}(\sqrt{1-\zeta^2}}{\zeta})}{t_r \sqrt{1-\zeta^2}}. \end{align*}\]
#Overshoot 
Mp=0.05

#Settling Time = ts
#ts=0.1

#Rise time = tr  
tr=0.8

#Determine damping (z) and natural frequency (omega_n)
z = float(np.sqrt(-np.log(Mp)**2/(-np.log(Mp)**2-np.pi**2)))

#omega_n= 4/(ts*z)
omega_n = float((np.pi - np.arctan(np.sqrt(1-z**2)/z))/np.sqrt(1-z**2)/tr) 

print("Damping ratio=",z)
print("Natural frequency=",omega_n)
Damping ratio= 0.6901067305598216
Natural frequency= 4.028617400280874

5.6.2.3. 3. Determine K and \(\tau\) from the transfer function#

The dcgain Python function is used to dertermine the gain at 0 Hz input (s=0) leading tot the calculation of K in the first order standard form \(C(s)=\frac{K}{\tau s+1}\).

The pole Python function is used to calculate the poles of a transfer function.

# Convert to standard form: k/(tau*s+1)
K= float(dcgain(sys))
tau = float(-1/pole(sys))

5.6.2.4. 4. Calculate the gains: \(k_i\) and \(k_p\) using equations \(\ref{eq:TD2},\ref{eq:TD3}\)#

#PI controller: 
Ki=(omega_n**2*tau)/K 
Kp=(2*z*omega_n*tau-1)/K
bsp=0
Kd=0

print("Ki is equal to %2.2f"%Ki)
print("Kp is equal to %2.2f"%Kp)
Ki is equal to 1.08
Kp is equal to 0.04

5. Simulate the closed loop response

CL =tf(K*Ki,[tau,K*Kp+1,K*Ki])
T = np.linspace(0,3,10000)
yout,T = step(CL,T)
wn,d,p= damp(CL)

plt.plot(T, yout, 'b', label='Step time response')
plt.grid()
plt.show() # show the plot

S=stepinfo(CL)

print("Percentage overshoot:%.2f" % S.get("Overshoot"),"%")
print("Rise time:%.2f" % S.get("RiseTime"), "sec.")
print("Settling time:%.2f" % S.get("SettlingTime"),"sec" )
_____Eigenvalue______ Damping___ Frequency_
     -2.78    +2.916j     0.6901      4.029
     -2.78    -2.916j     0.6901      4.029
../../_images/f927ce54f0a2fb17bbd78711956fa9729008abebab20145bd8032ce0d9d1600e.png
Percentage overshoot:5.00 %
Rise time:0.53 sec.
Settling time:1.51 sec

The measured rise time (0.53 sec.) differs from the requested rise time (0.8 sec) because the requested rise time is measured between 0% and 100% while the stepinfo function returns the time between 10% and 90% (0.53 sec.).