diff --git a/brewpi.py b/brewpi.py index 4e3dc68..b8df80b 100644 --- a/brewpi.py +++ b/brewpi.py @@ -56,9 +56,11 @@ class HeaterTask(ATask): self.interval = interval self.msg_handler = msg_handler msg_handler.set_recv_handler(self.recv) + self.power_soll = 0 + self.pulse_counter = 0 def actor(self, y): - self.heater.setPower(max(0, 0 + self.heater.get_power_max() * y)) + self.power_soll = max(0, 0 + self.heater.get_power_max() * y) def on_changed_active(self, is_activated): asyncio.create_task(self.send({'Activate': int(is_activated)})) @@ -70,7 +72,7 @@ class HeaterTask(ATask): print(data) for pair in data.items(): if 'Power' in pair[0]: - self.heater.setPower(pair[1]) + self.power_soll = pair[1] if 'Activate' in pair[0]: self.heater.activate(bool(pair[1])) @@ -79,13 +81,36 @@ class HeaterTask(ATask): async def on_process(self): print("{}: Started with interval {} s".format(self.msg_handler.get_key(), self.interval)) - await self.send({'Capabilities': {'Power': {'Min': self.heater.get_power_min(), 'Max': self.heater.get_power_max()}}}) + await self.send({'Capabilities': {'Power': {'Min': 0, 'Max': self.heater.get_power_max()}}}) self.heater.set_on_changed('is_active', ChangedInteger(self.on_changed_active).set) self.heater.set_on_changed('power_eff', ChangedInteger(self.on_changed_power).set) self.heater.activate(False) + pulse_period_s = 1 + pulse_period_count = pulse_period_s/self.interval + while True: + if self.power_soll < self.heater.get_power_min(): + self.heater.setPower(self.heater.get_power_min()) + self.heater.activate(False) + else: + self.heater.setPower(self.power_soll) + self.heater.activate(True) + + on_count = pulse_period_count + if self.heater.get_power_min() > 0: + on_count = pulse_period_count*min(1, self.power_soll/self.heater.get_power_min()) + + self.pulse_counter += 1 + if self.pulse_counter >= pulse_period_count: + self.pulse_counter = 0 + + if self.pulse_counter < on_count: + self.heater.activate(True) + else: + self.heater.activate(False) + self.heater.process() await asyncio.sleep(self.interval) diff --git a/components/actor/heater.py b/components/actor/heater.py index 01662b4..b4ed525 100644 --- a/components/actor/heater.py +++ b/components/actor/heater.py @@ -9,7 +9,7 @@ class Heater(AHeater): self.power_eff = 0 def get_power_min(self): - return 0 + return 500 def get_power_max(self): return 3500