# Analytical Introduction to PID controllers¶

In [457]:
from IPython.core.display import HTML
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide(); } else {$('div.input').show();
}
code_show = !code_show
}
plt.grid('on')


#### Integral term¶

Finally, we must increase the system type (the number of free integrators in the open-loop transfer function) by 1 in order to eliminate steady-state error. We do this by adding an integral term to the controller. The relative magnitude of this term will be quite small because the integral term will remove phase and thus destabilize our system. This is a small term that, over time, will creep up on the target step input.

The first Bode is that of the PID controller, the second is that of the open-loop controller+physics.

In the video below, note that the integral term very slowly creeps the arm to the target hover angle.

In [756]:
Kp = 55000
Kd = 5000
Ki = 20000

sys8 = signal.TransferFunction([Kd, Kp, Ki],
[ 1, 0])
w, mag, phase = signal.bode(sys8)

plt.figure()
plt.title('Bode plot for PID controller')
plt.grid('on')
plt.semilogx(w, mag)    # Bode magnitude plot
plt.ylabel('dB')
plt.figure()
plt.semilogx(w, phase)  # Bode phase plot
plt.grid('on')
plt.ylabel('Degrees')
plt.show()


Here is the Bode-plot for the open-loop, PID compensated system:

In [757]:
Kp = 55000
Kd = 5000
Ki = 20000

sys6 = signal.TransferFunction(  [Kd*alpha, Kp*alpha, Ki*alpha],
[beta, gamma, epsilon, lambdas+(Kd*alpha), mu + (Kp*alpha), Ki*alpha])
w, mag, phase = signal.bode(sys6)

plt.figure()
plt.title('Bode plot for open-loop, PID-compensated system')
plt.grid('on')
plt.semilogx(w, mag)    # Bode magnitude plot
plt.ylabel('dB')
plt.figure()
plt.semilogx(w, phase)  # Bode phase plot
plt.grid('on')