Skip to content

Commit

Permalink
First Stable Test
Browse files Browse the repository at this point in the history
  • Loading branch information
marl0rd committed Jun 24, 2014
1 parent d3aea8e commit c00646c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 67 deletions.
117 changes: 54 additions & 63 deletions src/main/java/gui/GuiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,34 @@ public class GuiController extends AnchorPane {
private FirstOrderSystem process;
private PIController controller;
private PITuning piTuning;
private Loop loop;

@FXML private ChoiceBox<Preferences.ControlType> controlTypeChoiceBox;
@FXML private ChoiceBox<Preferences.Source> sourceChoiceBox;
@FXML private ChoiceBox<Preferences.ControllerType> controllerChoiceBox;
@FXML private ChoiceBox<String> 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<String, Double> trendings;
@FXML private TextField dampingRatioTextField;
@FXML private TextField settlingTimeTextField;
private XYChart.Series<String, Double> outputTrendingSeries;
private Simulator simulator;

@FXML private ChoiceBox<Preferences.ControlType> controlTypeChoiceBox;
@FXML private ChoiceBox<Preferences.Source> sourceChoiceBox;
@FXML private ChoiceBox<Preferences.Controller> controllerChoiceBox;
@FXML private ChoiceBox<String> 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<String, Double> trendings;
@FXML private TextField dampingRatioTextField;
@FXML private TextField settlingTimeTextField;
private XYChart.Series<String, Double> outputTrendingSeries;

// ********** Constructor **********//
public GuiController() {
// Load the FXML
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Gui.fxml"));
fxmlLoader.setRoot(this);
Expand All @@ -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();
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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()));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/simulation/PISimulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/util/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

}

0 comments on commit c00646c

Please sign in to comment.