diff --git a/wash/src/wash/program/WashingProgram1.java b/wash/src/wash/program/WashingProgram1.java new file mode 100644 index 0000000..90e91fc --- /dev/null +++ b/wash/src/wash/program/WashingProgram1.java @@ -0,0 +1,75 @@ +package wash.program; + +import actor.ActorThread; +import wash.control.Settings; +import wash.control.WashingMessage; +import wash.io.WashingIO; + +import static wash.control.WashingMessage.Order.*; + +/** + * 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 class WashingProgram1 extends ActorThread { + + private WashingIO io; + private ActorThread temp; + private ActorThread water; + private ActorThread spin; + + public WashingProgram1(WashingIO io, + ActorThread temp, + ActorThread water, + ActorThread spin) { + this.io = io; + this.temp = temp; + this.water = water; + this.spin = spin; + } + + @Override + public void run() { + try { + System.out.println("washing program 1 started."); + // Lock the hatch + io.lock(true); + + // 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); + + // 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); + + // usleep(1000); + + // Now that the barrel has stopped, it is safe to open the hatch. + io.lock(false); + System.out.println("washing program 1 unlocked hatch"); + } catch (InterruptedException e) { + + // 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 new file mode 100644 index 0000000..6271d22 --- /dev/null +++ b/wash/src/wash/program/WashingProgram2.java @@ -0,0 +1,59 @@ +package wash.program; + +import actor.ActorThread; +import wash.control.Settings; +import wash.control.WashingMessage; +import wash.io.WashingIO; + +import static wash.control.WashingMessage.Order.*; + +/** + * 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 class WashingProgram2 extends ActorThread { + + private WashingIO io; + private ActorThread temp; + private ActorThread water; + private ActorThread spin; + + public WashingProgram2(WashingIO io, + ActorThread temp, + ActorThread water, + ActorThread spin) { + this.io = io; + this.temp = temp; + this.water = water; + this.spin = spin; + } + + @Override + public void run() { + try { + System.out.println("Washing program 2 starting."); + // Lock the hatch + io.lock(true); + + // Spin for five simulated minutes (one minute == 60000 milliseconds) + Thread.sleep(5 * 60000 / Settings.SPEEDUP); + + // Now that the barrel has stopped, it is safe to open the hatch. + io.lock(false); + System.out.println("Washing program 2 finished."); + } catch (InterruptedException e) { + + // 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 new file mode 100644 index 0000000..7f1aee7 --- /dev/null +++ b/wash/src/wash/program/WashingProgram3.java @@ -0,0 +1,66 @@ +package wash.program; + +import actor.ActorThread; +import wash.control.WashingMessage; +import wash.io.WashingIO; + +import static wash.control.WashingMessage.Order.*; + +/** + * 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 class WashingProgram3 extends ActorThread { + + private WashingIO io; + private ActorThread temp; + private ActorThread water; + private ActorThread spin; + + public WashingProgram3(WashingIO io, + ActorThread temp, + ActorThread water, + ActorThread spin) + { + this.io = io; + this.temp = temp; + this.water = water; + this.spin = spin; + } + + @Override + public void run() { + try { + System.out.println("washing program 3 started"); + + io.lock(true); + + // Switch off heating + temp.send(new WashingMessage(this, TEMP_IDLE)); + System.out.println(receive()); + temp.send(new WashingMessage(this, TEMP_SET_40)); + System.out.println(receive()); + + // Thread.sleep(5 * 60000 / Settings.SPEEDUP); + + // Unlock hatch + io.lock(false); + + System.out.println("Washing program 3 finished"); + } catch (InterruptedException e) { + + // 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/WashingProgramN.java new file mode 100644 index 0000000..44f4a45 --- /dev/null +++ b/wash/src/wash/program/WashingProgramN.java @@ -0,0 +1,80 @@ +package wash.program; + +import actor.ActorThread; +import wash.control.WashingMessage; +import wash.io.WashingIO; + +import static wash.control.WashingMessage.Order.*; + +/** + * 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 class WashingProgramN extends ActorThread { + + private WashingIO io; + private ActorThread temp; + private ActorThread water; + private ActorThread spin; + + public WashingProgramN(WashingIO io, + ActorThread temp, + ActorThread water, + ActorThread spin) + { + this.io = io; + this.temp = temp; + this.water = water; + this.spin = spin; + } + + @Override + public void run() { + try { + System.out.println("User pressed stop button"); + + // Switch off heating + temp.send(new WashingMessage(this, TEMP_IDLE)); + + // Wait for temperature controller to acknowledge + WashingMessage ack1 = receive(); + System.out.println("got " + ack1); + + // Drain barrel, which may take some time. To ensure the barrel + // is drained before we continue, an acknowledgment is required. + water.send(new WashingMessage(this, WATER_DRAIN)); + WashingMessage ack2 = receive(); // wait for acknowledgment + System.out.println("got " + ack2); + + // Now that the barrel is drained, we can turn off water regulation. + water.send(new WashingMessage(this, WATER_IDLE)); + WashingMessage ack3 = receive(); // wait for acknowledgment + System.out.println("got " + ack3); + + // Switch off spin. We expect an acknowledgment, to ensure + // the hatch isn't opened while the barrel is spinning. + spin.send(new WashingMessage(this, SPIN_OFF)); + WashingMessage ack4 = receive(); // wait for acknowledgment + System.out.println("got " + ack4); + + // Unlock hatch + io.lock(false); + + System.out.println("washing stop program finished"); + } catch (InterruptedException e) { + + // 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 stop program terminated"); + } + } +}