pidController.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/python
  2. #
  3. # This file is part of IvPID.
  4. # Copyright (C) 2015 Ivmech Mechatronics Ltd. <bilgi@ivmech.com>
  5. #
  6. # IvPID is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # IvPID is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. # title :PID.py
  19. # description :python pid controller
  20. # author :Caner Durmusoglu
  21. # date :20151218
  22. # version :0.1
  23. # notes :
  24. # python_version :2.7
  25. # ==============================================================================
  26. """Ivmech PID Controller is simple implementation of a Proportional-Integral-Derivative (PID) Controller in the Python Programming Language.
  27. More information about PID Controller: http://en.wikipedia.org/wiki/PID_controller
  28. """
  29. import time
  30. class PID:
  31. """PID Controller
  32. """
  33. def __init__(self, P=0.2, I=0.0, D=0.0, current_time=None):
  34. self.Kp = P
  35. self.Ki = I
  36. self.Kd = D
  37. self.sample_time = 0.00
  38. self.current_time = current_time if current_time is not None else time.time()
  39. self.last_time = self.current_time
  40. self.clear()
  41. def clear(self):
  42. """Clears PID computations and coefficients"""
  43. self.SetPoint = 0.0
  44. self.PTerm = 0.0
  45. self.ITerm = 0.0
  46. self.DTerm = 0.0
  47. self.last_error = 0.0
  48. # Windup Guard
  49. self.int_error = 0.0
  50. self.windup_guard = 20.0
  51. self.output = 0.0
  52. def update(self, feedback_value, current_time=None):
  53. """Calculates PID value for given reference feedback
  54. .. math::
  55. u(t) = K_p e(t) + K_i \int_{0}^{t} e(t)dt + K_d {de}/{dt}
  56. .. figure:: images/pid_1.png
  57. :align: center
  58. Test PID with Kp=1.2, Ki=1, Kd=0.001 (test_pid.py)
  59. """
  60. error = self.SetPoint - feedback_value
  61. self.current_time = current_time if current_time is not None else time.time()
  62. delta_time = self.current_time - self.last_time
  63. delta_error = error - self.last_error
  64. if (delta_time >= self.sample_time):
  65. self.PTerm = self.Kp * error
  66. self.ITerm += error * delta_time
  67. if (self.ITerm < -self.windup_guard):
  68. self.ITerm = -self.windup_guard
  69. elif (self.ITerm > self.windup_guard):
  70. self.ITerm = self.windup_guard
  71. self.DTerm = 0.0
  72. if delta_time > 0:
  73. self.DTerm = delta_error / delta_time
  74. # Remember last time and last error for next calculation
  75. self.last_time = self.current_time
  76. self.last_error = error
  77. self.output = self.PTerm + (self.Ki * self.ITerm) + (self.Kd * self.DTerm)
  78. return self.output
  79. def setKp(self, proportional_gain):
  80. """Determines how aggressively the PID reacts to the current error with setting Proportional Gain"""
  81. self.Kp = proportional_gain
  82. def setKi(self, integral_gain):
  83. """Determines how aggressively the PID reacts to the current error with setting Integral Gain"""
  84. self.Ki = integral_gain
  85. def setKd(self, derivative_gain):
  86. """Determines how aggressively the PID reacts to the current error with setting Derivative Gain"""
  87. self.Kd = derivative_gain
  88. def setInput(self, setpoint):
  89. self.SetPoint = setpoint
  90. def setWindup(self, windup):
  91. """Integral windup, also known as integrator windup or reset windup,
  92. refers to the situation in a PID feedback controller where
  93. a large change in setpoint occurs (say a positive change)
  94. and the integral terms accumulates a significant error
  95. during the rise (windup), thus overshooting and continuing
  96. to increase as this accumulated error is unwound
  97. (offset by errors in the other direction).
  98. The specific problem is the excess overshooting.
  99. """
  100. self.windup_guard = windup
  101. def setSampleTime(self, sample_time):
  102. """PID that should be updated at a regular interval.
  103. Based on a pre-determined sampe time, the PID decides if it should compute or return immediately.
  104. """
  105. self.sample_time = sample_time