diff --git a/src/main/java/gui/GuiController.java b/src/main/java/gui/GuiController.java index 342f367..ffb0a40 100644 --- a/src/main/java/gui/GuiController.java +++ b/src/main/java/gui/GuiController.java @@ -38,33 +38,34 @@ public class GuiController extends AnchorPane { private FirstOrderSystem process; private PIController controller; private PITuning piTuning; - private Loop loop; - - @FXML private ChoiceBox controlTypeChoiceBox; - @FXML private ChoiceBox sourceChoiceBox; - @FXML private ChoiceBox controllerChoiceBox; - @FXML private ChoiceBox processChoiceBox; - @FXML private TextField sourceValueTextField; - @FXML private MenuItem startSimulationMenuItem; - @FXML private MenuItem stopSimulationMenuItem; - @FXML private MenuItem preferencesMenuItem; - @FXML private Label inputValueLabel; - @FXML private Label samplingTimeLabel; - @FXML private Label kpLabel; - @FXML private Label kiLabel; - @FXML private Label integralTimeLabel; - @FXML private Label tauLabel; - @FXML private Label gainLabel; - @FXML private Label outputValueLabel; - @FXML private CategoryAxis categoryAxis; - @FXML private NumberAxis numberAxis; - @FXML private LineChart trendings; - @FXML private TextField dampingRatioTextField; - @FXML private TextField settlingTimeTextField; - private XYChart.Series outputTrendingSeries; + private Simulator simulator; + + @FXML private ChoiceBox controlTypeChoiceBox; + @FXML private ChoiceBox sourceChoiceBox; + @FXML private ChoiceBox controllerChoiceBox; + @FXML private ChoiceBox processChoiceBox; + @FXML private TextField sourceValueTextField; + @FXML private MenuItem startSimulationMenuItem; + @FXML private MenuItem stopSimulationMenuItem; + @FXML private MenuItem preferencesMenuItem; + @FXML private Label inputValueLabel; + @FXML private Label samplingTimeLabel; + @FXML private Label kpLabel; + @FXML private Label kiLabel; + @FXML private Label integralTimeLabel; + @FXML private Label tauLabel; + @FXML private Label gainLabel; + @FXML private Label outputValueLabel; + @FXML private CategoryAxis categoryAxis; + @FXML private NumberAxis numberAxis; + @FXML private LineChart trendings; + @FXML private TextField dampingRatioTextField; + @FXML private TextField settlingTimeTextField; + private XYChart.Series outputTrendingSeries; // ********** Constructor **********// public GuiController() { + // Load the FXML try { FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Gui.fxml")); fxmlLoader.setRoot(this); @@ -74,10 +75,10 @@ public GuiController() { throw new RuntimeException(exception); } - process = new FirstOrderSystem(); - controller = new PIController(); - piTuning = new PITuning(controller, process); - loop = new Loop(process, controller); + simulator = new Simulator(process, controller); + process = new FirstOrderSystem(); + controller = new PIController(); + piTuning = new PITuning(controller, process); initializeGraphics(); registerListeners(); @@ -97,8 +98,13 @@ public String toString(Number object) { // ChoiceBoxes controlTypeChoiceBox.getItems().addAll(Preferences.ControlType.values()); + controlTypeChoiceBox.getSelectionModel().select(Preferences.controlType); sourceChoiceBox.getItems().addAll(Preferences.Source.values()); - + sourceChoiceBox.getSelectionModel().select(Preferences.source); + controllerChoiceBox.getItems().addAll(Preferences.Controller.values()); + controllerChoiceBox.getSelectionModel().select(Preferences.controller); + processChoiceBox.getItems().addAll("ConicalTank"); + processChoiceBox.getSelectionModel().select(0); } private void registerListeners() { @@ -108,7 +114,7 @@ private void registerListeners() { preferencesMenuItem.setOnAction(value -> showPreferences()); // The loop inform about new values, so they are displayed in the gui: - loop.timeStampProperty().addListener(updateGui); + simulator.timeStampProperty().addListener(updateGui); } private void showPreferences() { @@ -122,72 +128,57 @@ private void showPreferences() { Preferences.simulationMode = preferencesController.getSelectedSimulationMode(); Preferences.loopType = preferencesController.getSelectedLoopType(); - loop.setSamplingTime(Preferences.samplingTime); - loop.setSimulationMode(Preferences.simulationMode); - loop.setLoopType(Preferences.loopType); + simulator.setSamplingTime(Preferences.samplingTime); + simulator.setSimulationMode(Preferences.simulationMode); + simulator.setLoopType(Preferences.loopType); } private void startSimulation(){ - loop.start(); + simulator.start(); } private void stopSimulation(){ - loop.interrupt(); - loop = new Loop(process, controller); + simulator.interrupt(); + simulator = new Simulator(process, controller); } private void updateGui(){ - if(loop.getLoopStarted()){ + if(simulator.getLoopStarted()){ // Update Loop visualization - outputTrendingSeries.getData().add(new XYChart.Data<>(TIME_FORMAT.format(Date.from(Instant.now())), loop.getOutput())); + outputTrendingSeries.getData().add(new XYChart.Data<>(TIME_FORMAT.format(Date.from(Instant.now())), simulator.getOutput())); while (outputTrendingSeries.getData().size() > TRENDING_DATA_LIMIT) { outputTrendingSeries.getData().remove(0); } - consoleListView.getItems().add("I= " + loop.getInput() + ", \t O=" + loop.getOutput()); - while(consoleListView.getItems().size() > CONSOLE_DATA_LIMIT) { - consoleListView.getItems().remove(0); - } } - samplingTimeLabel.setText(Double.toString(loop.getSamplingTime())); - - // Update conicalTank visualization - heightOperationPointLabel.setText(String.valueOf(conicalTank.getHeightOperationPoint())); - inflowOperationPointLabel.setText(String.valueOf(conicalTank.getInflowOperationPoint())); + samplingTimeLabel.setText(Double.toString(simulator.getSamplingTime())); // Update Process visualization - tauLabel.setText(Double.toString(loop.getProcess().getTau())); - gainLabel.setText(Double.toString(loop.getProcess().getGain())); - inputLabel.setText(Double.toString(loop.getProcess().getInput())); - outputLabel.setText(Double.toString(loop.getProcess().getOutput())); + tauLabel.setText(Double.toString(simulator.getProcess().getTau())); + gainLabel.setText(Double.toString(simulator.getProcess().getGain())); + inputValueLabel.setText(Double.toString(simulator.getProcess().getInput())); + outputValueLabel.setText(Double.toString(simulator.getProcess().getOutput())); // Update Controller visualization - kpLabel.setText(Double.toString(loop.getController().getProportionalGain())); - kiLabel.setText(Double.toString(loop.getController().getIntegralGain())); - integralTimeLabel.setText(Double.toString(loop.getController().getIntegralTime())); + kpLabel.setText(Double.toString(simulator.getController().getProportionalGain())); + kiLabel.setText(Double.toString(simulator.getController().getIntegralGain())); + integralTimeLabel.setText(Double.toString(simulator.getController().getIntegralTime())); } private InvalidationListener updateGui = new InvalidationListener() { @Override public void invalidated(Observable observable) { updateGui(); - loop.timeStampProperty().removeListener(this); - loop.timeStampProperty().addListener(this); + simulator.timeStampProperty().removeListener(this); + simulator.timeStampProperty().addListener(this); } }; private void recalculate() { - linealizeInNewSetPoint(); tuneController(); - loop.setInput(Double.parseDouble(heightSetPointTextField.getText())); updateGui(); } - private void linealizeInNewSetPoint(){ - conicalTank.setInflowOperationPoint(Double.parseDouble(flowSetPointTextField.getText())); - conicalTank.setHeightOperationPoint(Double.parseDouble(heightSetPointTextField.getText())); - } - private void tuneController() { piTuning.setDampingRatio(Double.parseDouble(dampingRatioTextField.getText())); piTuning.setSettlingTime(Double.parseDouble(settlingTimeTextField.getText())); diff --git a/src/main/java/simulation/PISimulator.java b/src/main/java/simulation/PISimulator.java index 89b0d46..1e1e34e 100644 --- a/src/main/java/simulation/PISimulator.java +++ b/src/main/java/simulation/PISimulator.java @@ -9,7 +9,7 @@ public class PISimulator extends Thread { // ********** Fields **********// private final double[] vz; - private PIController controller; + private PIController controller; private boolean started; private double samplingTime; private Preferences.SimulationMode simulationMode; diff --git a/src/main/java/simulation/Loop.java b/src/main/java/simulation/Simulator.java similarity index 98% rename from src/main/java/simulation/Loop.java rename to src/main/java/simulation/Simulator.java index 694d24f..c557ba7 100644 --- a/src/main/java/simulation/Loop.java +++ b/src/main/java/simulation/Simulator.java @@ -10,7 +10,7 @@ /** * Created by marlon on 6/20/14. */ -public class Loop extends Thread { +public class Simulator extends Thread { // ********** Fields **********// private double input; private double output; @@ -28,7 +28,7 @@ public class Loop extends Thread { private Preferences.LoopType loopType; // ********** Constructor **********// - public Loop(FirstOrderSystem process, PIController controller) { + public Simulator(FirstOrderSystem process, PIController controller) { setName("LoopSimulator"); setDaemon(true); this.process = process; diff --git a/src/main/java/util/Preferences.java b/src/main/java/util/Preferences.java index d161acd..10c12d9 100644 --- a/src/main/java/util/Preferences.java +++ b/src/main/java/util/Preferences.java @@ -19,12 +19,13 @@ public static enum SimulationMode { } public static enum LoopType {CLOSE_LOOP, OPEN_LOOP} public static enum ControlType {CLASSIC, ADAPTATIVE} - public static enum ControllerType {P, PI, PID} + public static enum Controller{P, PI, PID} public static enum Source {CONSTANT, PUSE_GENERATOR, RAMP, RANDOM, SINE_WAVE, STEP} private static final SimulationMode DEFAULT_SIMULATION_MODE = SimulationMode.SLOW; private static final LoopType DEFAULT_LOOP_TYPE = LoopType.CLOSE_LOOP; private static final ControlType DEFAULT_CONTROL_TYPE = ControlType.ADAPTATIVE; + private static final Controller DEFAULT_CONTROLLER = Controller.PI; private static final Source DEFAULT_SOURCE = Source.CONSTANT; private static final double DEFAULT_SAMPLING_TIME = 0.1; @@ -33,6 +34,7 @@ public static enum Source {CONSTANT, PUSE_GENERATOR, RAMP, RANDOM, SINE_WAVE, ST public static double samplingTime = DEFAULT_SAMPLING_TIME; public static LoopType loopType = DEFAULT_LOOP_TYPE; public static ControlType controlType = DEFAULT_CONTROL_TYPE; + public static Controller controller = DEFAULT_CONTROLLER; public static Source source = DEFAULT_SOURCE; }