├── Control library.py └── README.md /Control library.py: -------------------------------------------------------------------------------- 1 | import control as ct 2 | import matplotlib.pyplot as plt 3 | 4 | s = ct.TransferFunction.s 5 | 6 | # System parameters 7 | m = 0.5 8 | l = 1 9 | k = 0.5 10 | J = m*l*l 11 | g = 9.81 12 | 13 | # Non-linear system: 14 | # tau = J*ddtheta + k*dtheta + m*g*l*sin(theta) 15 | 16 | # Linearised system in theta = pi/2: 17 | # tau = J*ddtheta + k*dtheta - m*g*l*theta 18 | 19 | # Transfer function 20 | P = 1/(J*s**2 + k*s - m*g*l) 21 | 22 | # Plot root-locus of P 23 | plt.figure() 24 | ct.root_locus(P) 25 | 26 | # Controller transfer function - lead-lag 27 | Tc = 0.2 28 | z = -3 29 | K = 2 30 | C = K*(s-z)/(Tc*s+1) 31 | 32 | # Close-loop transfer function 33 | G = ct.feedback(C*P, 1) 34 | 35 | # Plot root-locus of C*P 36 | plt.figure() 37 | ct.root_locus(C*P) 38 | 39 | # Plot Nyquist diagram 40 | plt.figure() 41 | ct.nyquist_plot(C*P) 42 | 43 | # Ideal step response 44 | t, yout = ct.step_response(G) 45 | plt.figure() 46 | plt.plot(t, yout) 47 | plt.grid() 48 | plt.xlabel("Time [s]") 49 | 50 | # Show plots 51 | plt.show() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-Control-Library 2 | An example of how to use the Python Control Library. 3 | 4 | Code created to support a Linkedin post. Follow me on Linkedin! https://www.linkedin.com/in/simone-bertoni-control-eng/ 5 | --------------------------------------------------------------------------------