```python
import control
import sympy
import numpy as np
import matplotlib.pyplot as plt
sympy.init_printing()
```

## Original System


```python
s = control.tf([1, 0], [0, 1])
```


```python
G = 1/((s+2)*s)
G
```




$$\frac{1}{s^2 + 2 s}$$



### Requirements

* 0 steady state error to unit ramp response
* damping ratio of sqrt(2)/2
* settling time of 2 seconds


```python
control.rlocus(G, xlim=[-3, 0], ylim=[-3, 3]);
plt.title('uncompensated root locus')
sd = -2 + 2j

plt.plot(np.real(sd), np.imag(sd), 's')
plt.plot(np.real(sd), -np.imag(sd), 's')
```


```python
Gc = G/(1+G)
t, y = control.step_response(Gc, T=np.linspace(0, 20));
plt.plot(t, y - 1, label='y')
#plt.hlines(1, 0, 20, label='u')
plt.title('uncompensated step response error')

Kv = np.real((control.minreal(s*G).horner(0))[0, 0])
Kv

plt.figure()
t, y = control.step_response(Gc/s, T=np.linspace(0, 20));
plt.plot(t, t-y, label='e')
plt.hlines(1/Kv, 0, 20)
plt.title('uncompensated unit ramp response error')
```

## PID Compensator


* Do PD first


```python
H_PD = 2*(s + 4)

```


```python
control.rlocus(G*H_PD, xlim=[-10, 1]);
plt.plot(-2, 2, 's')
plt.plot(-2, -2, 's')
plt.title('PD compensated root locus')
```


```python
Gc_PD = G*H_PD/(1+G*H_PD)
t, y = control.step_response(Gc_PD/s, T=np.linspace(0, 10, 1000));
plt.plot(t, t)
plt.plot(t, y)
plt.title('PD compensated ramp response')

plt.figure()
t, y = control.step_response(Gc_PD/s, T=np.linspace(0, 20, 1000));
plt.plot(t, t-y)
plt.hlines(0.5, 0, 20)
```

## PI Compensator


```python
z = 0.01
H_PI = (s+z)/s
```


```python
H = H_PD*H_PI
```


```python
H
```




$$\frac{2 s^2 + 8.02 s + 0.08}{s}$$



Find: 

K_P = 8.02, K_I = 0.08, K_D = 2


```python
1/np.abs((G*H).horner(-2 + 2j))
```




    array([[[1.00250312]]])




```python
control.rlocus(G*H,xlim=[-10, 0], ylim=[-3, 3]);
plt.plot(-2, 2, 's')
plt.plot(-2, -2, 's')
```


```python
control.minreal(G*H_PD*s).horner(0)
```

    1 states have been removed from the model





    array([[[4.+0.j]]])




```python
control.minreal(G*H*s).horner(0)
```

    1 states have been removed from the model





    array([[[inf+nanj]]])




```python
Gc_PID = G*H/(1+G*H)
t, y = control.step_response(Gc_PID/s, T=np.linspace(0, 1000, 1000));
plt.plot(t, t)
plt.plot(t, y)
plt.title('PID compensated ramp response')

plt.figure()
plt.plot(t, t - y)
plt.hlines(t[0], t[-1], 80)
plt.title('PID compensated ramp response error')
```

47ba46c59593837bba391e3604896f08726e51df
ipynb
Jupyter Notebook
08.PID.ipynb
jgoppert/aae364_notebook
bc77866860e6bdb7d59467ce9817a604c7f26e20
BSD-3-Clause
2020-01-18T03:46:08.000Z
2020-01-18T03:46:08.000Z
08.PID.ipynb
jgoppert/aae364_notebook
bc77866860e6bdb7d59467ce9817a604c7f26e20
BSD-3-Clause
08.PID.ipynb
jgoppert/aae364_notebook
bc77866860e6bdb7d59467ce9817a604c7f26e20
BSD-3-Clause
2019-03-30T19:32:13.000Z
2020-07-09T07:18:17.000Z
Qwen/Qwen-72B
1. YES
2. YES