Controllers (Broken)

This commit is contained in:
Imbus 2024-11-07 22:26:12 +01:00
parent 22410c419f
commit 819da6a4c3
3 changed files with 57 additions and 32 deletions

View file

@ -25,7 +25,16 @@ public final class ControllerSpin extends ActorThread<WashingMessage> {
/** Recursive listener method */ /** Recursive listener method */
public void listener() { 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()) { switch (m.order()) {
case Order.SPIN_OFF -> spin.setMode(WashingIO.Spin.IDLE); case Order.SPIN_OFF -> spin.setMode(WashingIO.Spin.IDLE);
@ -52,7 +61,7 @@ public final class ControllerSpin extends ActorThread<WashingMessage> {
changed = true; changed = true;
} }
private void loop() { private void loop() throws InterruptedException {
io.setSpinMode(spinMode); io.setSpinMode(spinMode);
if (changed) { if (changed) {
@ -60,13 +69,17 @@ public final class ControllerSpin extends ActorThread<WashingMessage> {
changed = false; changed = false;
} }
usleep(60000 / Settings.SPEEDUP); Thread.sleep(60000 / Settings.SPEEDUP);
loop(); loop();
} }
@Override @Override
public void run() { public void run() {
loop(); try {
loop();
} catch (InterruptedException e) {
return;
}
} }
} }
} }

View file

@ -23,24 +23,27 @@ public final class ControllerTemp extends ActorThread<WashingMessage> {
heater.start(); heater.start();
while (true) { 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()) { switch (m.order()) {
case Order.TEMP_SET_40 -> heater.setTarget(40); case Order.TEMP_SET_40 -> heater.setTarget(40);
case Order.TEMP_SET_60 -> heater.setTarget(60); 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 -> { default -> {
continue; continue;
} }
} }
} }
} }
/** /**
* Heater class that extends Thread and controls the temperature * Heater class that extends Thread and controls the temperature
* of the washing machine. * of the washing machine.
* *
* Note that the heater has access to local variables in the * Note that the heater has access to local variables in the
* TemperatureController class. * TemperatureController class.
*/ */
@ -61,30 +64,36 @@ public final class ControllerTemp extends ActorThread<WashingMessage> {
@Override @Override
public void run() { public void run() {
double current; double current;
while (!isInterrupted()) { try {
current = io.getTemperature(); while (!isInterrupted()) {
current = io.getTemperature();
if (io.getWaterLevel() == 0) { if (io.getWaterLevel() == 0) {
io.heat(false); io.heat(false);
usleep(60000 / Settings.SPEEDUP); sleep(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);
continue; 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();
} }
} }
} }

View file

@ -3,8 +3,6 @@ package wash.control;
import actor.ActorThread; import actor.ActorThread;
import wash.control.WashingMessage.Order; import wash.control.WashingMessage.Order;
import wash.io.WashingIO; import wash.io.WashingIO;
import java.util.LinkedList;
import java.util.Queue;
public class ControllerWater extends ActorThread<WashingMessage> { public class ControllerWater extends ActorThread<WashingMessage> {
@ -26,9 +24,13 @@ public class ControllerWater extends ActorThread<WashingMessage> {
public void run() { public void run() {
waterpid.start(); waterpid.start();
while (true) { 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()) { switch (m.order()) {
case Order.WATER_DRAIN -> waterpid.setTarget(0); case Order.WATER_DRAIN -> waterpid.setTarget(0);
case Order.WATER_FILL -> waterpid.setTarget(20); case Order.WATER_FILL -> waterpid.setTarget(20);
@ -40,6 +42,7 @@ public class ControllerWater extends ActorThread<WashingMessage> {
} }
} }
/** A pid controller that is not actually a pid controller */
public final class WaterPid extends Thread { public final class WaterPid extends Thread {
private double target; private double target;
private final double tolerance; // Acceptable range around target private final double tolerance; // Acceptable range around target