Compare commits

..

3 commits

Author SHA1 Message Date
Imbus
3d5b098595 Correctly implemented programs 2024-11-15 10:56:12 +01:00
Imbus
81ee61c779 Reworked spin controller 2024-11-15 10:56:04 +01:00
Imbus
20fa65c926 Slightly re-tuned temp and water controllers 2024-11-15 10:55:55 +01:00
5 changed files with 70 additions and 69 deletions

View file

@ -1,13 +1,17 @@
package wash.control;
import actor.ActorThread;
import wash.io.WashingIO;
import wash.control.WashingMessage.Order;
import wash.io.WashingIO;
import wash.io.WashingIO.Spin;
public final class ControllerSpin extends ActorThread<WashingMessage> {
private WashingIO io;
private Spin spin = new Spin();
private WashingMessage m;
private WashingMessage temp;
private boolean ackSent = true;
private Order spinState = Order.SPIN_OFF;
long start = System.currentTimeMillis();
public ControllerSpin(WashingIO io) {
this.io = io;
@ -15,70 +19,50 @@ public final class ControllerSpin extends ActorThread<WashingMessage> {
protected void sendAck() {
m.sendAck(this);
ackSent = true;
}
@Override
public void run() {
spin.start();
listener(); // Drop into a recursive listener method
}
/** Recursive listener method */
public void listener() {
if(isInterrupted()) {
spin.interrupt();
return;
}
long elapsed; // "Virtual" seconds elapsed, used for modulo ops in spin direction
while (true) {
try {
m = take();
} catch (Exception e) {
System.exit(1);
temp = receiveWithTimeout(10000 / Settings.SPEEDUP);
elapsed = ((System.currentTimeMillis() - start) * (Settings.SPEEDUP) / 1000 * 60);
// If there is a new message, swap
if (temp != null) {
m = temp;
ackSent = false; // We have a new order
}
switch (m.order()) {
case Order.SPIN_OFF -> spin.setMode(WashingIO.Spin.IDLE);
case Order.SPIN_FAST -> spin.setMode(WashingIO.Spin.FAST);
case Order.SPIN_SLOW -> spin.setMode(WashingIO.Spin.LEFT);
// If this message is not the same as last time
if (m != null && m.order() != spinState) {
spinState = m.order();
}
switch (spinState) {
case Order.SPIN_OFF -> {
io.setSpinMode(Spin.IDLE);
}
case Order.SPIN_FAST -> {
io.setSpinMode(Spin.FAST);
}
case Order.SPIN_SLOW -> {
if (elapsed % 2 == 0)
io.setSpinMode(Spin.RIGHT);
else
io.setSpinMode(Spin.LEFT);
}
default -> {
listener();
continue;
}
}
listener();
}
private final class Spin extends Thread {
private WashingIO.Spin spinMode;
boolean changed = false;
public Spin() {
spinMode = WashingIO.Spin.IDLE;
}
public synchronized void setMode(WashingIO.Spin m) {
spinMode = m;
changed = true;
}
private void loop() throws InterruptedException {
io.setSpinMode(spinMode);
if (changed) {
if (m != null && !ackSent)
sendAck();
changed = false;
}
Thread.sleep(60000 / Settings.SPEEDUP);
loop();
}
@Override
public void run() {
try {
loop();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
System.out.println(e);
}
}
}

View file

@ -24,7 +24,7 @@ public final class ControllerTemp extends ActorThread<WashingMessage> {
public void run() {
while (true) {
try {
temp = receiveWithTimeout(10000 / Settings.SPEEDUP);
temp = receiveWithTimeout(30000 / Settings.SPEEDUP);
// If there is a new message, swap
if (temp != null) {
@ -39,7 +39,7 @@ public final class ControllerTemp extends ActorThread<WashingMessage> {
switch (heaterState) {
case Order.TEMP_SET_40 -> {
if (io.getTemperature() <= 39) {
if (io.getTemperature() <= 38.5) {
io.heat(true);
} else {
io.heat(false);
@ -48,7 +48,7 @@ public final class ControllerTemp extends ActorThread<WashingMessage> {
}
}
case Order.TEMP_SET_60 -> {
if (io.getTemperature() <= 59) {
if (io.getTemperature() <= 58.5) {
io.heat(true);
} else {
io.heat(false);

View file

@ -46,13 +46,14 @@ public class ControllerWater extends ActorThread<WashingMessage> {
case Order.WATER_DRAIN -> {
io.drain(true);
io.fill(false);
// WARNING: Float comparison
if (io.getWaterLevel() == 0 && !ackSent)
sendAck();
}
case Order.WATER_FILL -> {
io.drain(false);
if (io.getWaterLevel() < 19)
if (io.getWaterLevel() < 10)
io.fill(true);
else {
io.fill(false);
@ -64,6 +65,9 @@ public class ControllerWater extends ActorThread<WashingMessage> {
case Order.WATER_IDLE -> {
io.drain(false);
io.fill(false);
if (!ackSent) {
sendAck();
}
}
default -> {
continue;

View file

@ -47,16 +47,23 @@ public final class WashingProgram1 extends ActorThread<WashingMessage> {
temp.send(new WashingMessage(this, TEMP_SET_40));
receive();
spin.send(new WashingMessage(this, SPIN_SLOW));
receive();
// Keep the temperature for 30 min
Thread.sleep(30 * 60000 / Settings.SPEEDUP);
// Drain
temp.send(new WashingMessage(this, TEMP_IDLE));
receive();
spin.send(new WashingMessage(this, SPIN_OFF));
receive();
water.send(new WashingMessage(this, WATER_DRAIN));
receive();
// Rinse 5*2 minutes in cold water
spin.send(new WashingMessage(this, SPIN_SLOW));
receive();
for(int a = 0; a < 5; a++) {
water.send(new WashingMessage(this, WATER_FILL));
receive();
@ -70,7 +77,7 @@ public final class WashingProgram1 extends ActorThread<WashingMessage> {
// Centrifuge for five minutes
spin.send(new WashingMessage(this, SPIN_FAST));
receive();
Thread.sleep(5 * 60000 / Settings.SPEEDUP);
Thread.sleep(10 * 60000 / Settings.SPEEDUP);
spin.send(new WashingMessage(this, SPIN_OFF));
receive();
@ -81,6 +88,9 @@ public final class WashingProgram1 extends ActorThread<WashingMessage> {
water.send(new WashingMessage(this, WATER_DRAIN));
receive();
water.send(new WashingMessage(this, WATER_IDLE));
receive();
// Now that the barrel has stopped, it is safe to open the hatch.
io.lock(false);

View file

@ -48,6 +48,9 @@ public final class WashingProgram2 extends ActorThread<WashingMessage> {
temp.send(new WashingMessage(this, TEMP_SET_40));
receive();
spin.send(new WashingMessage(this, SPIN_SLOW));
receive();
// Keep the temperature for 20 min
Thread.sleep(20 * 60000 / Settings.SPEEDUP);
@ -72,7 +75,7 @@ public final class WashingProgram2 extends ActorThread<WashingMessage> {
receive();
// Rinse 5*2 minutes in cold water
for(int a = 0; a < 5; a++) {
for(int a = 0; a <= 5; a++) {
water.send(new WashingMessage(this, WATER_FILL));
receive();
@ -90,10 +93,10 @@ public final class WashingProgram2 extends ActorThread<WashingMessage> {
spin.send(new WashingMessage(this, SPIN_OFF));
receive();
temp.send(new WashingMessage(this, TEMP_IDLE));
water.send(new WashingMessage(this, WATER_DRAIN));
receive();
water.send(new WashingMessage(this, WATER_DRAIN));
water.send(new WashingMessage(this, WATER_IDLE));
receive();
// Now that the barrel has stopped, it is safe to open the hatch.