From 6939650708f8b06fdc49ab4ccf587ce8b82b4d49 Mon Sep 17 00:00:00 2001 From: jens Date: Wed, 2 Dec 2020 11:24:31 +0100 Subject: [PATCH] - PID: use pot for testing --- brewpi.py | 2 +- components/pid/pid.py | 3 +-- components/pid/temp_controller.py | 28 +++++++++++++++++++++------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/brewpi.py b/brewpi.py index b021503..f6a4f4a 100644 --- a/brewpi.py +++ b/brewpi.py @@ -227,7 +227,7 @@ if __name__ == '__main__': "Hold": { "Pid": { "kp": 0.8, - "ki": 0.002, + "ki": 0.008, "kd": 0.0, "rho": 1.0 } diff --git a/components/pid/pid.py b/components/pid/pid.py index d1b4ee6..ee38b19 100644 --- a/components/pid/pid.py +++ b/components/pid/pid.py @@ -36,7 +36,7 @@ class Pid: yp = kp * err # Reset yi on error sign changes - if yp > self.y_max or yp < self.y_min: + if yp > self.y_max or yp <= self.y_min or np.sign(err) != np.sign(self.err): self.yi = 0 self.err = err @@ -44,7 +44,6 @@ class Pid: y = yp + self.yi + yd self.y = max(self.y_min, min(self.y_max, y)) - print("yi = {}".format(self.yi)) def get_y(self): return self.y diff --git a/components/pid/temp_controller.py b/components/pid/temp_controller.py index e678ecb..de95db7 100644 --- a/components/pid/temp_controller.py +++ b/components/pid/temp_controller.py @@ -1,4 +1,5 @@ from components.pid.pid import Pid +from components.plant.pot import Pot from matplotlib.pyplot import plot, figure, subplot, title, xlabel, ylabel, grid, show import numpy as np from utils.value import AttributeChange @@ -53,9 +54,9 @@ if __name__ == '__main__': "dt": 1.0, "Hold": { "Pid": { - "kp": 0.5, - "ki": 0.0, - "kd": 0.5, + "kp": 0.8, + "ki": 0.008, + "kd": 0.0, "rho": 1.0 } }, @@ -69,9 +70,20 @@ if __name__ == '__main__': } } + pot_params = { + "dt" : 1.0, + "theta" : 20, + "C" : 4190, + "M" : 20, + "L" : 0.05, + "Td" : 12, + "kn" : 0.2 + } + temp_ist = 0 temp_soll = 20 ctrl = TempController(params) + plant = Pot(pot_params) _temp_ist = np.empty(0) _temp_soll = np.empty(0) _y = np.empty(0) @@ -80,7 +92,7 @@ if __name__ == '__main__': a = 0.5 fb = 0 rho = 0.02 - temps = [{'Temp': 20, 'Duration': 100}, {'Temp': 40, 'Duration': 100}, {'Temp': 35, 'Duration': 100}] + temps = [{'Temp': 20, 'Duration': 1000}, {'Temp': 40, 'Duration': 1000}, {'Temp': 50, 'Duration': 1000}, {'Temp': 60, 'Duration': 1000}, {'Temp': 70, 'Duration': 1000}, {'Temp': 80, 'Duration': 1000}] t = 0 for temp in temps: @@ -95,9 +107,11 @@ if __name__ == '__main__': break hold_counter -= 1 ctrl.process() - y = max(0, ctrl.get_power_hold()) - fb = (1-a)*fb + a*round(y, 2) - temp_ist += fb + plant.process() + y = max(0, 3500*ctrl.get_power_hold()) + plant.setPower(y) + temp_ist = round(plant.getTemperature(), 1) + fb = plant.getPower() ctrl.set_theta_ist(temp_ist) if abs(temp_ist - temp_soll) < 0.1: hold = True