From ddf8003f5c5d9de7168a44894f2d68acbd39cbd4 Mon Sep 17 00:00:00 2001 From: jens Date: Thu, 17 Dec 2020 16:28:50 +0100 Subject: [PATCH] - refactored - integrated stirrer --- brewpi.py | 11 ++++-- components/actor/__init__.py | 1 + components/actor/heaterFactory.py | 4 +-- components/actor/heater_hendi.py | 4 +-- .../{StirrerFactory.py => stirrerFactory.py} | 6 ++-- components/actor/stirrerpololu1376.py | 2 +- components/actor/stirrersim.py | 7 ++-- components/astirrer.py | 8 ++--- tasks/__init__.py | 1 + tasks/pot.py | 2 +- tasks/stirrer.py | 36 +++++++++++++++++++ tasks/tempctrl.py | 4 +-- tasks/tempsensor.py | 4 +-- utils/value.py | 2 ++ 14 files changed, 66 insertions(+), 26 deletions(-) rename components/actor/{StirrerFactory.py => stirrerFactory.py} (66%) create mode 100644 tasks/stirrer.py diff --git a/brewpi.py b/brewpi.py index ad22a5b..ef80c2e 100644 --- a/brewpi.py +++ b/brewpi.py @@ -6,8 +6,8 @@ from ws.server.ws_server_multi_user import WsServerMultiUser from components.sensor import TempSensorFactory from components.pid import TempController from components.plant import Pot -from components.actor import HeaterFactory -from tasks import TaskManager, TempSensorTask, HeaterTask, PotTask, TcTask +from components.actor import HeaterFactory, StirrerFactory +from tasks import TaskManager, TempSensorTask, HeaterTask, PotTask, TcTask, StirrerTask DT_CTRL = 0.1 @@ -32,7 +32,7 @@ if __name__ == '__main__': taskmgr.add(PotTask(pot, DT_CTRL, dispatcher.msgio_get("Pot"))) # Heater - heater = HeaterFactory.create(config['Controller']['heater_name']) + heater = HeaterFactory.create(config['Controller']['heater_name'], config['Heater']) heater.set_on_changed("power_eff", ChangedFloat(pot.set_power, prec=0).set) heater_task = HeaterTask(heater, DT_CTRL, dispatcher.msgio_get("Heater")) taskmgr.add(heater_task) @@ -45,6 +45,11 @@ if __name__ == '__main__': taskmgr.add(tc_task) sensor.set_on_changed("temp", ChangedFloat(tc.set_theta_ist, prec=2).set) + # Stirrer + stirrer = StirrerFactory.create(config['Controller']['stirrer_name'], config['Stirrer']) + stirrer_task = StirrerTask(stirrer, DT_CTRL, dispatcher.msgio_get("Stirrer")) + taskmgr.add(stirrer_task) + # Message dispatcher h_dispatcher = taskmgr.start() h_server = server.listen("0.0.0.0", 8765) diff --git a/components/actor/__init__.py b/components/actor/__init__.py index f891c75..a548b13 100644 --- a/components/actor/__init__.py +++ b/components/actor/__init__.py @@ -1 +1,2 @@ from components.actor.heaterFactory import HeaterFactory +from components.actor.stirrerFactory import StirrerFactory diff --git a/components/actor/heaterFactory.py b/components/actor/heaterFactory.py index b069f91..c95bb57 100644 --- a/components/actor/heaterFactory.py +++ b/components/actor/heaterFactory.py @@ -2,10 +2,10 @@ class HeaterFactory: @staticmethod - def create(name): + def create(name, params): if "sim" in name: from components.actor.heater_sim import HeaterSim return HeaterSim() elif "Hendi" in name: from components.actor.heater_hendi import HeaterHendi - return HeaterHendi('/dev/ttyUSB0', 115200) + return HeaterHendi(params) diff --git a/components/actor/heater_hendi.py b/components/actor/heater_hendi.py index 24db0eb..43dbd8b 100644 --- a/components/actor/heater_hendi.py +++ b/components/actor/heater_hendi.py @@ -3,12 +3,12 @@ from hendiCtrl import HendiCtrl class HeaterHendi(AHeater): - def __init__(self, port, baudrate): + def __init__(self, params): AHeater.__init__(self) self.power_set = self.get_power_min() self.is_active = False self.power_eff = 0 - self.hendi = HendiCtrl(port, baudrate) + self.hendi = HendiCtrl(params['port'], params['baudrate']) def get_power_min(self): caps = self.hendi.getCapabilties() diff --git a/components/actor/StirrerFactory.py b/components/actor/stirrerFactory.py similarity index 66% rename from components/actor/StirrerFactory.py rename to components/actor/stirrerFactory.py index 55a1df5..3f30f7b 100644 --- a/components/actor/StirrerFactory.py +++ b/components/actor/stirrerFactory.py @@ -2,10 +2,10 @@ class StirrerFactory: @staticmethod - def create(name, dt): + def create(name, params): if "sim" in name: from components.actor.stirrersim import StirrerSim - return StirrerSim(dt) + return StirrerSim(params) elif "1376" in name: from components.actor.stirrerpololu1376 import StirrerPololu1376 - return StirrerPololu1376(dt, '/dev/ttyUSB0', 115200) + return StirrerPololu1376(params) diff --git a/components/actor/stirrerpololu1376.py b/components/actor/stirrerpololu1376.py index 5ff4d26..b489eb4 100644 --- a/components/actor/stirrerpololu1376.py +++ b/components/actor/stirrerpololu1376.py @@ -8,7 +8,7 @@ class StirrerPololu1376(AStirrer): return "Pololu1376" def __init__(self, dt, port, baudrate): - super(StirrerPololu1376, self).__init__(dt) + AStirrer.__init__(self, dt) self.isMasterOn = 0 self.ser_speed = baudrate diff --git a/components/actor/stirrersim.py b/components/actor/stirrersim.py index 835671d..06d7522 100644 --- a/components/actor/stirrersim.py +++ b/components/actor/stirrersim.py @@ -3,11 +3,8 @@ import time class StirrerSim(AStirrer): - def name(self): - return "FakeStirrer" - - def __init__(self, dt): - super(StirrerSim, self).__init__(dt) + def __init__(self, params): + AStirrer.__init__(self, params['dt']) def __del__(self): self.activate(False) diff --git a/components/astirrer.py b/components/astirrer.py index 203fca3..1c6bb8b 100644 --- a/components/astirrer.py +++ b/components/astirrer.py @@ -1,12 +1,10 @@ import abc +from utils.value import AttributeChange -class AStirrer: - @abc.abstractmethod - def name(self): - return "" - +class AStirrer(AttributeChange): def __init__(self, dt): + AttributeChange.__init__(self) self.dt = dt self.speed = 0 self.cycleTime = 1 diff --git a/tasks/__init__.py b/tasks/__init__.py index e48f498..053a10c 100644 --- a/tasks/__init__.py +++ b/tasks/__init__.py @@ -1,5 +1,6 @@ from tasks.task import * from tasks.tempsensor import * from tasks.heater import * +from tasks.stirrer import * from tasks.pot import * from tasks.tempctrl import * diff --git a/tasks/pot.py b/tasks/pot.py index 2b8bdc3..354ece5 100644 --- a/tasks/pot.py +++ b/tasks/pot.py @@ -1,8 +1,8 @@ import asyncio from tasks import ATask -from components.plant import APlant from ws.message import MsgIo from utils.value import ChangedInteger, ChangedFloat +from components.plant import APlant class PotTask(ATask): diff --git a/tasks/stirrer.py b/tasks/stirrer.py new file mode 100644 index 0000000..33eecc0 --- /dev/null +++ b/tasks/stirrer.py @@ -0,0 +1,36 @@ +import asyncio +from tasks import ATask +from ws.message import MsgIo +from components import AStirrer + + +class StirrerTask(ATask): + def __init__(self, stirrer_device: AStirrer, interval, msg_handler: MsgIo): + ATask.__init__(self, interval) + self.msg_handler = msg_handler + msg_handler.set_recv_handler(self.recv) + self.device = stirrer_device + + def on_speed_changed(self, value): + asyncio.create_task(self.send({'Speed': value})) + + def on_dutycycle_changed(self, value): + asyncio.create_task(self.send({'DutyCycle': value})) + + def on_cycletime_changed(self, value): + asyncio.create_task(self.send({'CycleTime': value})) + + async def recv(self, data): + print(data) + + async def send(self, data): + await self.msg_handler.send(data) + + async def on_process(self): + print("{}: Started with interval {} s".format(self.msg_handler.get_key(), self.interval)) + self.device.set_on_changed("speed", self.on_speed_changed) + self.device.set_on_changed("dutyCycle", self.on_dutycycle_changed) + self.device.set_on_changed("cycleTime", self.on_cycletime_changed) + while True: + self.device.process() + await asyncio.sleep(self.interval) diff --git a/tasks/tempctrl.py b/tasks/tempctrl.py index 25859fb..f49b30b 100644 --- a/tasks/tempctrl.py +++ b/tasks/tempctrl.py @@ -1,8 +1,8 @@ import asyncio from tasks import ATask -from components.pid import TempController -from utils.value import ChangedFloat from ws.message import MsgIo +from utils.value import ChangedFloat +from components.pid import TempController class TcTask(ATask): diff --git a/tasks/tempsensor.py b/tasks/tempsensor.py index 3823b45..ce34276 100644 --- a/tasks/tempsensor.py +++ b/tasks/tempsensor.py @@ -1,8 +1,8 @@ import asyncio from tasks import ATask -from components import ATemperatureSensor -from utils.value import ChangedFloat from ws.message import MsgIo +from utils.value import ChangedFloat +from components import ATemperatureSensor class TempSensorTask(ATask): diff --git a/utils/value.py b/utils/value.py index b47283b..39507f1 100644 --- a/utils/value.py +++ b/utils/value.py @@ -1,3 +1,5 @@ + + class Value: def __init__(self, initial=None): self.value = initial