diff --git a/PID.py b/PID.py index 6eef0f7..5ec4bdf 100644 --- a/PID.py +++ b/PID.py @@ -79,8 +79,10 @@ def update(self, feedback_value): delta_time = self.current_time - self.last_time delta_error = error - self.last_error + self.PTerm = self.Kp * error + if (delta_time >= self.sample_time): - self.PTerm = self.Kp * error + # self.PTerm = self.Kp * error self.ITerm += error * delta_time if (self.ITerm < -self.windup_guard): @@ -96,7 +98,7 @@ def update(self, feedback_value): self.last_time = self.current_time self.last_error = error - self.output = self.PTerm + (self.Ki * self.ITerm) + (self.Kd * self.DTerm) + self.output = self.PTerm + (self.Ki * self.ITerm) + (self.Kd * self.DTerm) def setKp(self, proportional_gain): """Determines how aggressively the PID reacts to the current error with setting Proportional Gain""" diff --git a/__pycache__/PID.cpython-36.pyc b/__pycache__/PID.cpython-36.pyc new file mode 100644 index 0000000..a97f919 Binary files /dev/null and b/__pycache__/PID.cpython-36.pyc differ diff --git a/test_pid.py b/test_pid.py index 761f261..565c870 100644 --- a/test_pid.py +++ b/test_pid.py @@ -50,7 +50,7 @@ def test_pid(P = 0.2, I = 0.0, D= 0.0, L=100): pid = PID.PID(P, I, D) pid.SetPoint=0.0 - pid.setSampleTime(0.01) + pid.setSampleTime(0.1) END = L feedback = 0 @@ -66,7 +66,7 @@ def test_pid(P = 0.2, I = 0.0, D= 0.0, L=100): feedback += (output - (1/i)) if i>9: pid.SetPoint = 1 - time.sleep(0.02) + time.sleep(0.01) feedback_list.append(feedback) setpoint_list.append(pid.SetPoint) @@ -90,5 +90,6 @@ def test_pid(P = 0.2, I = 0.0, D= 0.0, L=100): plt.show() if __name__ == "__main__": - test_pid(1.2, 1, 0.001, L=50) -# test_pid(0.8, L=50) \ No newline at end of file + test_pid(0.5, 0.1, 0.001, L=200) + test_pid(1, 0.1, 0.001, L=200) + test_pid(1, 0.5, 0.001, L=200) \ No newline at end of file