Last active
August 29, 2015 14:20
-
-
Save lazyval/de508dc5ca90c966152e to your computer and use it in GitHub Desktop.
PID controller made in python, see also http://www.csimn.com/CSI_pages/PIDforDummies.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
kp = 1.2 | |
ki = 0.9 | |
kd = 0.3 | |
set_point = 50 | |
prev_error = 0 | |
cumulative_moving_average = 0 | |
iteration = 0 | |
def make_iteration(measured): | |
global iteration, set_point, prev_error, cumulative_moving_average | |
iteration += 1 | |
err = measured - set_point | |
rate_of_change = prev_error - err | |
cumulative_moving_average = (err + (cumulative_moving_average * iteration)) / (iteration + 1) | |
integral = cumulative_moving_average * ki | |
derivative = rate_of_change * kd | |
proportional = err * kp | |
prev_error = err | |
return proportional + derivative + integral | |
max_iterations = 20 | |
output = 0 | |
for x in range(0, max_iterations): | |
output -= make_iteration(output) | |
print "Output is ", output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the the run with up to date version of code:
And here is what will happen if we'll replace cumulative sum with just sum of all previously seen errors: