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 2a51fbc commit 7aff945
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package process;
package continuous;

/**
* Created by marlon on 6/16/14.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package process;
package continuous;

/**
* Created by marlon on 6/19/14.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package process;
package continuous;

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.DoublePropertyBase;
Expand Down Expand Up @@ -61,9 +61,9 @@ public PITuning(PIController controller, FirstOrderSystem process) {

// ********** Methods **********//
private void recalculate() {
undampedNaturalFrequency.set(4/(dampingRatio.get() * settlingTime.get()));
controller.setProportionalGain(((2*dampingRatio.get()*undampedNaturalFrequency.get()) - (1/process.getTau())) / (process.getGain()/process.getTau()));
controller.setIntegralTime((2*dampingRatio.get()*undampedNaturalFrequency.get() - (1/process.getTau())) / (Math.pow(undampedNaturalFrequency.get(), 2)));
undampedNaturalFrequency.set(4.0/(dampingRatio.get() * settlingTime.get()));
controller.setProportionalGain(((2.0*dampingRatio.get()*undampedNaturalFrequency.get()) - (1.0/process.getTau())) / (process.getGain()/process.getTau()));
controller.setIntegralTime((2.0*dampingRatio.get()*undampedNaturalFrequency.get() - (1.0/process.getTau())) / (Math.pow(undampedNaturalFrequency.get(), 2.0)));
controller.setIntegralGain(controller.getProportionalGain() / controller.getIntegralTime());
}

Expand Down
78 changes: 43 additions & 35 deletions src/main/java/gui/GuiController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package gui;

import conicaltank.ConicalTankProcess;
import javafx.application.Platform;
import process.ConicalTankProcess;
import continuous.FirstOrderSystem;
import continuous.PIController;
import continuous.PITuning;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
Expand All @@ -13,7 +17,7 @@
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import process.*;
import simulation.*;
import util.Preferences;
import java.io.IOException;
import java.sql.Date;
Expand All @@ -31,9 +35,9 @@ public class GuiController extends AnchorPane {

// ********** Fields **********//
private ConicalTankProcess conicalTank;
private FirstOrderSystem process;
private PIController controller;
private PITuning piTuning;
private FirstOrderSystem process;
private PIController controller;
private PITuning piTuning;
private Loop loop;

@FXML private TextField heightSetPointTextField;
Expand Down Expand Up @@ -105,7 +109,7 @@ private void registerListeners() {
preferencesMenuItem.setOnAction(value -> showPreferences());

// Loop refresh
loop.timeStampProperty().addListener(value -> updateGui());
loop.timeStampProperty().addListener(updateGui);
}

private void showPreferences() {
Expand All @@ -122,11 +126,9 @@ private void showPreferences() {
loop.setSamplingTime(Preferences.samplingTime);
loop.setSimulationMode(Preferences.simulationMode);
loop.setLoopType(Preferences.loopType);
recalculate();
}

private void startSimulation(){
recalculate();
loop.start();
}

Expand All @@ -136,39 +138,45 @@ private void stopSimulation(){
}

private void updateGui(){
Platform.runLater(() -> {
if(loop.getLoopStarted()){
// Update Loop visualization
outputTrendingSeries.getData().add(new XYChart.Data<>(TIME_FORMAT.format(Date.from(Instant.now())), loop.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);
}
if(loop.getLoopStarted()){
// Update Loop visualization
outputTrendingSeries.getData().add(new XYChart.Data<>(TIME_FORMAT.format(Date.from(Instant.now())), loop.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(loop.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()));
// Update conicalTank visualization
heightOperationPointLabel.setText(String.valueOf(conicalTank.getHeightOperationPoint()));
inflowOperationPointLabel.setText(String.valueOf(conicalTank.getInflowOperationPoint()));

// Update Controller visualization
kpLabel.setText(Double.toString(loop.getController().getProportionalGain()));
kiLabel.setText(Double.toString(loop.getController().getIntegralGain()));
integralTimeLabel.setText(Double.toString(loop.getController().getIntegralTime()));
});
// 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()));

// Update Controller visualization
kpLabel.setText(Double.toString(loop.getController().getProportionalGain()));
kiLabel.setText(Double.toString(loop.getController().getIntegralGain()));
integralTimeLabel.setText(Double.toString(loop.getController().getIntegralTime()));
}

private InvalidationListener updateGui = new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
updateGui();
loop.timeStampProperty().removeListener(this);
loop.timeStampProperty().addListener(this);
}
};

private void recalculate() {
linealizeInNewSetPoint();
tuneController();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package conicaltank;
package process;

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.DoublePropertyBase;
import process.FirstOrderSystem;
import continuous.FirstOrderSystem;

/**
* Created by marlon on 6/18/14.
Expand Down Expand Up @@ -53,6 +53,7 @@ protected void invalidated() {

// ********** Methods **********//
private void recalculate(){

double alpha = ((9/2) *
(OBSTRUCTION * Math.pow(HEIGHT,2) * Math.sqrt(2*GRAVITY) * Math.pow(heightOperationPoint.get(), -5/2)) /
(2 * Math.PI * Math.pow(RADIUS,2))) -
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package process;
package simulation;

import continuous.FirstOrderSystem;
import util.Preferences;

/**
* Created by marlon on 6/17/14.
*
* | gain |
* process.input ------>| --------- |-------> process.output
* simulation.input ------>| --------- |-------> simulation.output
* | tau*s + 1 |
*
*/
public class FirstOrderSimulator extends Thread{
// ********** Fields **********//
private final double[] vz;
private FirstOrderSystem process;
private FirstOrderSystem process;
private boolean started;
private double samplingTime;
private Preferences.SimulationMode simulationMode;
Expand All @@ -33,17 +34,17 @@ public FirstOrderSimulator(FirstOrderSystem process) {
public void run() {
started = true;
while(started){
vz[0] = process.getInput() - ((samplingTime- 2 * process.getTau()) / (samplingTime + 2 * process.getTau())) * vz[1];
vz[0] = process.getInput() - ((samplingTime- 2.0 * process.getTau()) / (samplingTime + 2.0 * process.getTau())) * vz[1];
System.out.println("vcz[0]" + vz[0]);
process.setOutput(((process.getGain() * samplingTime) / (samplingTime + 2 * process.getTau())) * (vz[0] + vz[1]));
process.setOutput(((process.getGain() * samplingTime) / (samplingTime + 2.0 * process.getTau())) * (vz[0] + vz[1]));
delay();
vz[1] = vz[0];
System.out.println("vcz[1]" + vz[1]);
}
}

public void delay(){
// The samplingTime of process is based in second, the sleep method is based in milliseconds
// The samplingTime of simulation is based in second, the sleep method is based in milliseconds
try {
Thread.sleep((long) ((samplingTime * simulationMode.factor)* 1000));
} catch (InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package process;
package simulation;

import continuous.FirstOrderSystem;
import continuous.PIController;
import javafx.application.Platform;
import javafx.beans.property.*;
import util.Preferences;

Expand All @@ -12,8 +15,8 @@ public class Loop extends Thread {
private double input;
private double output;
private double error;
private FirstOrderSystem process;
private PIController controller;
private FirstOrderSystem process;
private PIController controller;
private FirstOrderSimulator processThread;
private PISimulator controllerThread;
private LongProperty timeStamp;
Expand Down Expand Up @@ -57,12 +60,12 @@ public void run() {
while(loopStarted.get()){
System.out.print("I=" + this.input + "\t");

error = getProcessThread().getProcess().getOutput() - input;
error = input - getProcessThread().getProcess().getOutput();
if(loopType == Preferences.LoopType.CLOSE_LOOP){
processThread.getProcess().setInput(this.input);
} else {
controllerThread.getController().setInput(this.error);
processThread.getProcess().setInput(controllerThread.getController().getOutput());
} else {
processThread.getProcess().setInput(this.input);
}

System.out.print("C=" + processThread.getProcess().getOutput() + "\t");
Expand All @@ -73,19 +76,21 @@ public void run() {
}

public void delay(){
// The samplingTime of process is based in second, the sleep method is based in milliseconds
// The samplingTime of simulation is based in second, the sleep method is based in milliseconds
try {
Thread.sleep((long) ((samplingTime * simulationMode.factor)* 1000));
} catch (InterruptedException e) {
loopStarted.set(false);
processThread.setStarted(false);
controllerThread.setStarted(false);
}
System.out.println(System.currentTimeMillis());
timeStamp.set(System.currentTimeMillis());
Platform.runLater(() -> {
setTimeStamp(System.currentTimeMillis());
});
}

// ********** Setters and Getters **********//
// ********** Setters and Getters **********//

public double getInput() {
return input;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package process;
package simulation;

import continuous.PIController;
import util.Preferences;

/**
Expand All @@ -8,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 All @@ -29,15 +30,15 @@ public void run() {
started = true;
while(started){
vz[0] = controller.getInput() - vz[1];
controller.setOutput(((1/2) * (2*controller.getProportionalGain() + samplingTime*controller.getIntegralGain()) * vz[0]) +
((1/2) * (samplingTime*controller.getIntegralGain() - 2*controller.getProportionalGain()) * vz[1]));
controller.setOutput(((1.0 / 2.0) * (2.0 * controller.getProportionalGain() + samplingTime * controller.getIntegralGain()) * vz[0]) +
((1.0 / 2.0) * (samplingTime * controller.getIntegralGain() - 2.0 * controller.getProportionalGain()) * vz[1]));
delay();
vz[1] = vz[0];
}
}

public void delay(){
// The samplingTime of process is based in second, the sleep method is based in milliseconds
// The samplingTime of simulation is based in second, the sleep method is based in milliseconds
try {
Thread.sleep((long) ((samplingTime * simulationMode.factor)* 1000));
} catch (InterruptedException e) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/sources/Constant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package sources;

/**
* One constant value function
*/
public class Constant extends Thread {
}
7 changes: 7 additions & 0 deletions src/main/java/sources/PulseGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package sources;

/**
* Function of Pulse
*/
public class PulseGenerator extends Thread {
}
7 changes: 7 additions & 0 deletions src/main/java/sources/Ramp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package sources;

/**
* Ramp Function
*/
public class Ramp extends Thread {
}
7 changes: 7 additions & 0 deletions src/main/java/sources/Random.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package sources;

/**
* Function random
*/
public class Random extends Thread {
}
7 changes: 7 additions & 0 deletions src/main/java/sources/SineWave.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package sources;

/**
* Created by marlon on 6/24/14.
*/
public class SineWave extends Thread {
}
7 changes: 7 additions & 0 deletions src/main/java/sources/Step.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package sources;

/**
* Step Function
*/
public class Step extends Thread {
}

0 comments on commit 7aff945

Please sign in to comment.