Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions src/Character.java → src/simulator/Character.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package simulator;

import java.awt.*;

public class Character extends Rectangle {
Expand All @@ -24,6 +26,9 @@ public class Character extends Rectangle {
private double lastTimestamp;
private double lastError;
private double lastSpeed;
private double error;
private double dt;
private double errorRate;

public static Character getInstance() {
if (instance == null) {
Expand All @@ -44,9 +49,9 @@ private Character(int width, int height, int sourceX, int sourceY) {
}

public void update() {
double error = Setpoint.getInstance().x - this.x;
double dt = System.currentTimeMillis() - lastTimestamp;
double errorRate = (error - lastError) / dt;
error = Setpoint.getInstance().x - this.x;
dt = System.currentTimeMillis() - lastTimestamp;
errorRate = (error - lastError) / dt;
if (Math.abs(error) < I_ZONE) errorSum += error;
int moveValue = (int) (error * kP + errorSum * kI + errorRate * kD);
moveValue = (int) normalizeSpeed(moveValue);
Expand Down Expand Up @@ -99,4 +104,13 @@ public void setPID(double kP, double kI, double kD) {
setI(kI);
setD(kD);
}

public double getError() {
return error;
}

public double getRate() {
return errorRate;
}

}
4 changes: 4 additions & 0 deletions src/Main.java → src/simulator/Main.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package simulator;

import simulator.Window;

public class Main {

public static void main(String[] args) {
Expand Down
4 changes: 2 additions & 2 deletions src/RerunButton.java → src/simulator/RerunButton.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package simulator;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RerunButton extends JButton {

Expand Down
2 changes: 2 additions & 0 deletions src/Setpoint.java → src/simulator/Setpoint.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package simulator;

import java.awt.*;

public class Setpoint extends Rectangle {
Expand Down
10 changes: 8 additions & 2 deletions src/Window.java → src/simulator/Window.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import textfields.BaseTextField;
package simulator;

import simulator.information.Status;
import simulator.textfields.BaseTextField;

import javax.swing.*;
import java.awt.*;
import java.util.Set;

public class Window extends JPanel {

Expand All @@ -17,6 +19,7 @@ public class Window extends JPanel {
private final BaseTextField kPField;
private final BaseTextField kIField;
private final BaseTextField kDField;
private final Status status;

private Window() {
super(IS_DOUBLE_BUFFERED);
Expand All @@ -27,13 +30,15 @@ private Window() {
character = Character.getInstance();
setpoint = Setpoint.getInstance();
rerunButton = RerunButton.getInstance();
status = Status.getInstance();
kPField = new BaseTextField("kP", 300, 30);
kIField = new BaseTextField("kI", 600, 30);
kDField = new BaseTextField("kD", 900, 30);
this.add(rerunButton);
this.add(kPField);
this.add(kIField);
this.add(kDField);
this.add(status);
}

@Override
Expand All @@ -59,6 +64,7 @@ private void update() {
delay(0.02);
character.update();
character.setPID(kPField.getValue(), kIField.getValue(), kDField.getValue());
status.update();
repaint();
}

Expand Down
63 changes: 63 additions & 0 deletions src/simulator/information/Status.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package simulator.information;

import simulator.Character;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;

public class Status extends JPanel {

private static final int WIDTH = 640;
private static final int HEIGHT = 640;
private static final int X = 800;
private static final int Y = 480;
private static final int FONT_SIZE = 20;
private JTextField characterError;
private JTextField errorRate;

private static Status instance;

public static Status getInstance() {
if (instance == null) {
instance = new Status();
}
return instance;
}

private Status() {
configureCharacterError();
configureErrorRate();
this.add(characterError);
this.add(errorRate);
this.setLayout(null);
this.setVisible(true);
this.setBounds(X, Y, WIDTH, HEIGHT);
this.setBackground(Color.GRAY);
}

public void configureCharacterError() {
characterError = new JTextField("Error: " + Character.getInstance().getError());
characterError.setSize(200, 50);
characterError.setEditable(false);
characterError.setFont(new Font(Font.MONOSPACED, Font.PLAIN, FONT_SIZE));
characterError.setLayout(null);
characterError.setBackground(Color.GRAY);
characterError.setBorder(new LineBorder(Color.GRAY));
}

public void configureErrorRate() {
errorRate = new JTextField("Error rate: " + Character.getInstance().getRate());
errorRate.setBounds(0, 50, 400, 50);
errorRate.setEditable(false);
errorRate.setFont(new Font(Font.MONOSPACED, Font.PLAIN, FONT_SIZE));
errorRate.setLayout(null);
errorRate.setBackground(Color.GRAY);
errorRate.setBorder(new LineBorder(Color.GRAY));
}

public void update() {
characterError.setText("Error: " + Character.getInstance().getError());
errorRate.setText("Error rate: " + Character.getInstance().getRate());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package textfields;
package simulator.textfields;

import javax.swing.*;
import java.awt.*;
Expand Down