From 71db7d9e8c689e88d6a745823aa1e941dc0ccc50 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Thu, 7 Nov 2024 22:04:16 +0100 Subject: [PATCH] Programs --- wash/src/wash/program/EmptyProgram.java | 25 ++++++++++++++++++ wash/src/wash/program/WashingProgram1.java | 26 +++++++++++-------- wash/src/wash/program/WashingProgram2.java | 8 +++--- wash/src/wash/program/WashingProgram3.java | 18 ++++++++----- ...gProgramN.java => WashingProgramStop.java} | 10 +++---- 5 files changed, 60 insertions(+), 27 deletions(-) create mode 100644 wash/src/wash/program/EmptyProgram.java rename wash/src/wash/program/{WashingProgramN.java => WashingProgramStop.java} (89%) diff --git a/wash/src/wash/program/EmptyProgram.java b/wash/src/wash/program/EmptyProgram.java new file mode 100644 index 0000000..80dd816 --- /dev/null +++ b/wash/src/wash/program/EmptyProgram.java @@ -0,0 +1,25 @@ +package wash.program; + +import actor.ActorThread; +import wash.control.WashingMessage; + +/** + * Program 3 for washing machine. This also serves as an example of how washing + * programs can be structured. + * + * This short program stops all regulation of temperature and water levels, + * stops the barrel from spinning, and drains the machine of water. + * + * It can be used after an emergency stop (program 0) or a power failure. + */ +public final class EmptyProgram extends ActorThread { + + public EmptyProgram() { + } + + @Override + public void run() { + System.out.println("WARNINIG: Attempting to run empty program"); + System.exit(1); + } +} diff --git a/wash/src/wash/program/WashingProgram1.java b/wash/src/wash/program/WashingProgram1.java index 90e91fc..5783210 100644 --- a/wash/src/wash/program/WashingProgram1.java +++ b/wash/src/wash/program/WashingProgram1.java @@ -16,7 +16,7 @@ import static wash.control.WashingMessage.Order.*; * * It can be used after an emergency stop (program 0) or a power failure. */ -public class WashingProgram1 extends ActorThread { +public final class WashingProgram1 extends ActorThread { private WashingIO io; private ActorThread temp; @@ -36,40 +36,44 @@ public class WashingProgram1 extends ActorThread { @Override public void run() { try { - System.out.println("washing program 1 started."); // Lock the hatch io.lock(true); + water.send(new WashingMessage(this, WATER_FILL)); + receive(); + // Instruct SpinController to rotate barrel slowly, back and forth // Expect an acknowledgment in response. - System.out.println("setting SPIN_SLOW..."); spin.send(new WashingMessage(this, SPIN_SLOW)); - WashingMessage ack1 = receive(); - System.out.println("washing program 1 got " + ack1); + receive(); // Spin for five simulated minutes (one minute == 60000 milliseconds) Thread.sleep(5 * 60000 / Settings.SPEEDUP); // Instruct SpinController to stop spin barrel spin. // Expect an acknowledgment in response. - System.out.println("setting SPIN_OFF..."); spin.send(new WashingMessage(this, SPIN_OFF)); - WashingMessage ack2 = receive(); - System.out.println("washing program 1 got " + ack2); + receive(); - // usleep(1000); + temp.send(new WashingMessage(this, TEMP_IDLE)); + receive(); + + water.send(new WashingMessage(this, WATER_DRAIN)); + receive(); // Now that the barrel has stopped, it is safe to open the hatch. io.lock(false); - System.out.println("washing program 1 unlocked hatch"); + + System.out.println("WashingProgram1 Finished..."); + this.interrupt(); } catch (InterruptedException e) { + System.out.println("WashingProgram1 Interrupted..."); // If we end up here, it means the program was interrupt()'ed: // set all controllers to idle temp.send(new WashingMessage(this, TEMP_IDLE)); water.send(new WashingMessage(this, WATER_IDLE)); spin.send(new WashingMessage(this, SPIN_OFF)); - System.out.println("washing program terminated"); } } } diff --git a/wash/src/wash/program/WashingProgram2.java b/wash/src/wash/program/WashingProgram2.java index 6271d22..e84e596 100644 --- a/wash/src/wash/program/WashingProgram2.java +++ b/wash/src/wash/program/WashingProgram2.java @@ -16,7 +16,7 @@ import static wash.control.WashingMessage.Order.*; * * It can be used after an emergency stop (program 0) or a power failure. */ -public class WashingProgram2 extends ActorThread { +public final class WashingProgram2 extends ActorThread { private WashingIO io; private ActorThread temp; @@ -36,7 +36,7 @@ public class WashingProgram2 extends ActorThread { @Override public void run() { try { - System.out.println("Washing program 2 starting."); + System.out.println("WashingProgram2 Started..."); // Lock the hatch io.lock(true); @@ -45,15 +45,15 @@ public class WashingProgram2 extends ActorThread { // Now that the barrel has stopped, it is safe to open the hatch. io.lock(false); - System.out.println("Washing program 2 finished."); + System.out.println("WashingProgram2 Finished..."); } catch (InterruptedException e) { + System.out.println("WashingProgram2 Interrupted..."); // If we end up here, it means the program was interrupt()'ed: // set all controllers to idle temp.send(new WashingMessage(this, TEMP_IDLE)); water.send(new WashingMessage(this, WATER_IDLE)); spin.send(new WashingMessage(this, SPIN_OFF)); - System.out.println("washing program terminated"); } } } diff --git a/wash/src/wash/program/WashingProgram3.java b/wash/src/wash/program/WashingProgram3.java index 7f1aee7..c7b7f26 100644 --- a/wash/src/wash/program/WashingProgram3.java +++ b/wash/src/wash/program/WashingProgram3.java @@ -15,7 +15,7 @@ import static wash.control.WashingMessage.Order.*; * * It can be used after an emergency stop (program 0) or a power failure. */ -public class WashingProgram3 extends ActorThread { +public final class WashingProgram3 extends ActorThread { private WashingIO io; private ActorThread temp; @@ -36,14 +36,19 @@ public class WashingProgram3 extends ActorThread { @Override public void run() { try { - System.out.println("washing program 3 started"); + System.out.println("WashingProgram3 Started..."); io.lock(true); // Switch off heating - temp.send(new WashingMessage(this, TEMP_IDLE)); + // temp.send(new WashingMessage(this, TEMP_IDLE)); + // System.out.println(receive()); + // temp.send(new WashingMessage(this, TEMP_SET_40)); + // System.out.println(receive()); + + temp.send(new WashingMessage(this, WATER_FILL)); System.out.println(receive()); - temp.send(new WashingMessage(this, TEMP_SET_40)); + temp.send(new WashingMessage(this, WATER_DRAIN)); System.out.println(receive()); // Thread.sleep(5 * 60000 / Settings.SPEEDUP); @@ -51,16 +56,15 @@ public class WashingProgram3 extends ActorThread { // Unlock hatch io.lock(false); - System.out.println("Washing program 3 finished"); + System.out.println("WashingProgram3 Finished..."); } catch (InterruptedException e) { - + System.out.println("WashingProgram3 Interrupted..."); // If we end up here, it means the program was interrupt()'ed: // set all controllers to idle temp.send(new WashingMessage(this, TEMP_IDLE)); water.send(new WashingMessage(this, WATER_IDLE)); spin.send(new WashingMessage(this, SPIN_OFF)); - System.out.println("washing program 3 terminated"); } } } diff --git a/wash/src/wash/program/WashingProgramN.java b/wash/src/wash/program/WashingProgramStop.java similarity index 89% rename from wash/src/wash/program/WashingProgramN.java rename to wash/src/wash/program/WashingProgramStop.java index 44f4a45..410d596 100644 --- a/wash/src/wash/program/WashingProgramN.java +++ b/wash/src/wash/program/WashingProgramStop.java @@ -15,14 +15,14 @@ import static wash.control.WashingMessage.Order.*; * * It can be used after an emergency stop (program 0) or a power failure. */ -public class WashingProgramN extends ActorThread { +public final class WashingProgramStop extends ActorThread { private WashingIO io; private ActorThread temp; private ActorThread water; private ActorThread spin; - public WashingProgramN(WashingIO io, + public WashingProgramStop(WashingIO io, ActorThread temp, ActorThread water, ActorThread spin) @@ -36,7 +36,7 @@ public class WashingProgramN extends ActorThread { @Override public void run() { try { - System.out.println("User pressed stop button"); + System.out.println("WashingProgramStop Starting..."); // Switch off heating temp.send(new WashingMessage(this, TEMP_IDLE)); @@ -65,9 +65,9 @@ public class WashingProgramN extends ActorThread { // Unlock hatch io.lock(false); - System.out.println("washing stop program finished"); + System.out.println("WashingProgramStop Finished..."); } catch (InterruptedException e) { - + System.out.println("WashingProgramStop Interrupted..."); // If we end up here, it means the program was interrupt()'ed: // set all controllers to idle