Skip to content

Instantly share code, notes, and snippets.

@zpconn
Created November 14, 2013 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zpconn/7473361 to your computer and use it in GitHub Desktop.
Save zpconn/7473361 to your computer and use it in GitHub Desktop.
A simple discrete-time PID controller.
# A simple discrete-time PID controller. If plot is true, this will use matplotlib to show a graph.
# Otherwise, it will simply print out the successive values of the controller.
plot = True
if __name__ == '__main__':
if plot:
import matplotlib.pyplot as plt
v = [0] * 100
v[0] = 200
setpoint = 150
e0 = 0
e1 = 0
e2 = 0
kp = 0.3
td = 0.25
ti = 3
h = 1
for i in range(99):
e3 = e2
e2 = e1
e1 = (setpoint - v[i]) / (1 + (i+1) * 0.1)
v[i+1] = v[i] + kp * (e1 - e2) + kp * h * e1 / ti + kp * td * (e1 - 2 * e2 + e3) / h
if (plot == False):
print v[i+1]
if (plot == True):
plt.plot(v)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment