From 819da6a4c3ffcb7cecfe71368283bf6a37a9d99b Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Thu, 7 Nov 2024 22:26:12 +0100 Subject: [PATCH] Controllers (Broken) --- wash/src/wash/control/ControllerSpin.java | 21 ++++++-- wash/src/wash/control/ControllerTemp.java | 57 +++++++++++++--------- wash/src/wash/control/ControllerWater.java | 11 +++-- 3 files changed, 57 insertions(+), 32 deletions(-) diff --git a/wash/src/wash/control/ControllerSpin.java b/wash/src/wash/control/ControllerSpin.java index 1db00cc..7ec645b 100644 --- a/wash/src/wash/control/ControllerSpin.java +++ b/wash/src/wash/control/ControllerSpin.java @@ -25,7 +25,16 @@ public final class ControllerSpin extends ActorThread { /** Recursive listener method */ public void listener() { - m = poll(60000 / Settings.SPEEDUP).orElse(new WashingMessage(this, Order.NOOP)); + if(isInterrupted()) { + spin.interrupt(); + return; + } + + try { + m = take(); + } catch (Exception e) { + System.exit(1); + } switch (m.order()) { case Order.SPIN_OFF -> spin.setMode(WashingIO.Spin.IDLE); @@ -52,7 +61,7 @@ public final class ControllerSpin extends ActorThread { changed = true; } - private void loop() { + private void loop() throws InterruptedException { io.setSpinMode(spinMode); if (changed) { @@ -60,13 +69,17 @@ public final class ControllerSpin extends ActorThread { changed = false; } - usleep(60000 / Settings.SPEEDUP); + Thread.sleep(60000 / Settings.SPEEDUP); loop(); } @Override public void run() { - loop(); + try { + loop(); + } catch (InterruptedException e) { + return; + } } } } diff --git a/wash/src/wash/control/ControllerTemp.java b/wash/src/wash/control/ControllerTemp.java index 4fa3f29..3caef71 100644 --- a/wash/src/wash/control/ControllerTemp.java +++ b/wash/src/wash/control/ControllerTemp.java @@ -23,24 +23,27 @@ public final class ControllerTemp extends ActorThread { heater.start(); while (true) { - m = poll(60000 / Settings.SPEEDUP).orElse(new WashingMessage(this, Order.NOOP)); + try { + m = take(); + } catch (Exception e) { + System.exit(1); + } switch (m.order()) { case Order.TEMP_SET_40 -> heater.setTarget(40); case Order.TEMP_SET_60 -> heater.setTarget(60); - case Order.TEMP_IDLE -> heater.setTarget(0); + case Order.TEMP_IDLE -> heater.setTarget(0); // TODO: Error default -> { continue; } } - } } /** * Heater class that extends Thread and controls the temperature * of the washing machine. - * + * * Note that the heater has access to local variables in the * TemperatureController class. */ @@ -61,30 +64,36 @@ public final class ControllerTemp extends ActorThread { @Override public void run() { double current; - while (!isInterrupted()) { - current = io.getTemperature(); + try { + while (!isInterrupted()) { + current = io.getTemperature(); - if (io.getWaterLevel() == 0) { - io.heat(false); - usleep(60000 / Settings.SPEEDUP); - continue; - } - - if (current < target) { - io.heat(true); - } - - else if (current >= target) { - io.heat(false); - if(!hovering) { - sendAck(); - hovering = true; - usleep(60000 / Settings.SPEEDUP); + if (io.getWaterLevel() == 0) { + io.heat(false); + sleep(60000 / Settings.SPEEDUP); continue; } - } - usleep(60000 / Settings.SPEEDUP); + if (current < target) { + io.heat(true); + } + + else if (current >= target) { + io.heat(false); + + if (!hovering) { + sendAck(); + hovering = true; + sleep(60000 / Settings.SPEEDUP); + continue; + } + } + + sleep(60000 / Settings.SPEEDUP); + } + } catch (InterruptedException e) { + io.heat(false); + Thread.currentThread().interrupt(); } } } diff --git a/wash/src/wash/control/ControllerWater.java b/wash/src/wash/control/ControllerWater.java index a01c97b..0e7f647 100644 --- a/wash/src/wash/control/ControllerWater.java +++ b/wash/src/wash/control/ControllerWater.java @@ -3,8 +3,6 @@ package wash.control; import actor.ActorThread; import wash.control.WashingMessage.Order; import wash.io.WashingIO; -import java.util.LinkedList; -import java.util.Queue; public class ControllerWater extends ActorThread { @@ -26,9 +24,13 @@ public class ControllerWater extends ActorThread { public void run() { waterpid.start(); while (true) { - m = poll(60000 / Settings.SPEEDUP).orElse(new WashingMessage(this, Order.NOOP)); + // m = poll(60000 / Settings.SPEEDUP).orElse(new WashingMessage(this, Order.NOOP)); + try { + m = take(); + } catch (Exception e) { + System.exit(1); + } - // io.getWaterLevel(); switch (m.order()) { case Order.WATER_DRAIN -> waterpid.setTarget(0); case Order.WATER_FILL -> waterpid.setTarget(20); @@ -40,6 +42,7 @@ public class ControllerWater extends ActorThread { } } + /** A pid controller that is not actually a pid controller */ public final class WaterPid extends Thread { private double target; private final double tolerance; // Acceptable range around target