From c20ee9fd23d606c84f16c9c4b91287e383ca85b0 Mon Sep 17 00:00:00 2001 From: TurtleMinecraft Date: Mon, 12 Aug 2024 19:56:04 +0300 Subject: [PATCH 1/4] created the randomness factor --- src/simulator/Character.java | 8 ++++++-- src/simulator/information/ErrorGraph.java | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/simulator/Character.java b/src/simulator/Character.java index 1a38cd5..eb05292 100644 --- a/src/simulator/Character.java +++ b/src/simulator/Character.java @@ -18,8 +18,10 @@ public class Character extends Rectangle { private static final int VELOCITY_SOURCE_Y = 320; private static final int MAX_SPEED = 120; - private static final int MAX_ACCELERATION = 2; + private static final int MAX_ACCELERATION = 6; private static final int FRICTION = 3; + private static final double POSITION_RANDOM_FACTOR = 3.6; + private static final double VELOCITY_RANDOM_FACTOR = 0.12; private final PIDSettings pidSettings; private final PIDController pidController; @@ -121,11 +123,12 @@ private void runPosition() { pidSettings.getWaitTime() * MILLISECONDS_IN_SECOND && pidController.isOnTarget()); int moveValue = pidController.calculate(this.x, Setpoint.getInstance().x) + feedForwardController.calculate(this.x, Setpoint.getInstance().x); - moveValue = (int) normalizeSpeed(moveValue); + moveValue += (Math.random() * 1.33 - 0.33) * POSITION_RANDOM_FACTOR * Math.pow(moveValue, 2) / MAX_SPEED; if (Math.abs(moveValue - lastSpeed) > MAX_ACCELERATION) { if (lastSpeed > moveValue) moveValue = (int) (lastSpeed - MAX_ACCELERATION); if (lastSpeed < moveValue) moveValue = (int) (lastSpeed + MAX_ACCELERATION); } + moveValue = (int) normalizeSpeed(moveValue); this.translate(moveValue, 0); lastSpeed = moveValue; } @@ -133,6 +136,7 @@ private void runPosition() { private void runVelocity() { int moveValue = pidController.calculate(lastSpeed, Setpoint.getInstance().x) + feedForwardController.calculate(lastSpeed, Setpoint.getInstance().x); + moveValue += (Math.random() * 1.33 - 0.33) * VELOCITY_RANDOM_FACTOR * Math.pow(moveValue, 2) / MAX_SPEED; moveValue = (int) normalizeSpeed(moveValue); if (Math.abs(moveValue - lastSpeed) > MAX_ACCELERATION) { if (lastSpeed > moveValue) moveValue = (int) (lastSpeed - MAX_ACCELERATION); diff --git a/src/simulator/information/ErrorGraph.java b/src/simulator/information/ErrorGraph.java index cac6f1c..ad1994b 100644 --- a/src/simulator/information/ErrorGraph.java +++ b/src/simulator/information/ErrorGraph.java @@ -4,6 +4,7 @@ import javax.swing.*; import java.awt.*; +import java.awt.geom.Line2D; import java.util.ArrayList; public class ErrorGraph extends JPanel { From 782d9264dd8846b0329f60bcb983b5629e0649f5 Mon Sep 17 00:00:00 2001 From: TurtleMinecraft Date: Tue, 10 Sep 2024 01:40:22 +0300 Subject: [PATCH 2/4] remove unneeded repaint --- src/simulator/Window.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/simulator/Window.java b/src/simulator/Window.java index b8536fd..beb69bf 100644 --- a/src/simulator/Window.java +++ b/src/simulator/Window.java @@ -106,7 +106,6 @@ private void delay(double seconds) { long targetTime = (long) (System.currentTimeMillis() + seconds * MILLISECONDS_IN_SECOND); while (targetTime > currentTime) { currentTime = System.currentTimeMillis(); - repaint(character); } } From 00713248cadb83b7c236a8be1b52d10b6900eea1 Mon Sep 17 00:00:00 2001 From: TurtleMinecraft Date: Mon, 30 Sep 2024 22:46:19 +0300 Subject: [PATCH 3/4] fucking hate this normalizeSpeed() method --- src/simulator/Character.java | 7 ++++--- src/simulator/control/FeedForwardController.java | 1 - 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/simulator/Character.java b/src/simulator/Character.java index eb05292..542b3fc 100644 --- a/src/simulator/Character.java +++ b/src/simulator/Character.java @@ -147,10 +147,11 @@ private void runVelocity() { private double normalizeSpeed(double speed) { if (speed != 0) { - if (speed > 0) speed -= FRICTION; - if (speed < 0) speed += FRICTION; + if (speed > FRICTION) speed -= FRICTION; + else if (speed < -FRICTION) speed += FRICTION; + else return 0; if (Math.abs(speed) > MAX_SPEED) speed = (int) (MAX_SPEED * Math.signum(speed)); - } else if (Math.abs(speed) < FRICTION) speed = 0; + } return speed; } } diff --git a/src/simulator/control/FeedForwardController.java b/src/simulator/control/FeedForwardController.java index 2a602f3..610b9b6 100644 --- a/src/simulator/control/FeedForwardController.java +++ b/src/simulator/control/FeedForwardController.java @@ -63,7 +63,6 @@ public int calculate(double source, double setpoint) { double error = setpoint - source; double targetDerivative = (setpoint - previousTarget) / Window.PERIODIC_FRAME; previousTarget = setpoint; - double staticVal; if (ControlType.getInstance().getSelectedIndex() == ControlType.Types.VELOCITY.index) { return (int) (kS + kV * setpoint + kA * targetDerivative); } else { From ae1e68ceff9ea3255fd1a5bff47d4a71dbc9dfda Mon Sep 17 00:00:00 2001 From: TurtleMinecraft Date: Tue, 1 Oct 2024 00:22:43 +0300 Subject: [PATCH 4/4] velocity random factor is not so random anymore :3 (thanks tuval) --- src/simulator/Character.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/simulator/Character.java b/src/simulator/Character.java index 542b3fc..b1376ba 100644 --- a/src/simulator/Character.java +++ b/src/simulator/Character.java @@ -21,7 +21,6 @@ public class Character extends Rectangle { private static final int MAX_ACCELERATION = 6; private static final int FRICTION = 3; private static final double POSITION_RANDOM_FACTOR = 3.6; - private static final double VELOCITY_RANDOM_FACTOR = 0.12; private final PIDSettings pidSettings; private final PIDController pidController; @@ -123,7 +122,7 @@ private void runPosition() { pidSettings.getWaitTime() * MILLISECONDS_IN_SECOND && pidController.isOnTarget()); int moveValue = pidController.calculate(this.x, Setpoint.getInstance().x) + feedForwardController.calculate(this.x, Setpoint.getInstance().x); - moveValue += (Math.random() * 1.33 - 0.33) * POSITION_RANDOM_FACTOR * Math.pow(moveValue, 2) / MAX_SPEED; + moveValue += (Math.random() - 0.5) * POSITION_RANDOM_FACTOR * Math.pow(moveValue, 2) / MAX_SPEED; if (Math.abs(moveValue - lastSpeed) > MAX_ACCELERATION) { if (lastSpeed > moveValue) moveValue = (int) (lastSpeed - MAX_ACCELERATION); if (lastSpeed < moveValue) moveValue = (int) (lastSpeed + MAX_ACCELERATION); @@ -134,9 +133,9 @@ private void runPosition() { } private void runVelocity() { - int moveValue = pidController.calculate(lastSpeed, Setpoint.getInstance().x) + - feedForwardController.calculate(lastSpeed, Setpoint.getInstance().x); - moveValue += (Math.random() * 1.33 - 0.33) * VELOCITY_RANDOM_FACTOR * Math.pow(moveValue, 2) / MAX_SPEED; + int moveValue = pidController.calculate(lastSpeed, Setpoint.getInstance().x); + double feedForward = feedForwardController.calculate(lastSpeed, Setpoint.getInstance().x); + moveValue += feedForward * Math.pow(Math.E, -(Math.pow(feedForward / MAX_SPEED, 2) / Math.pow(MAX_SPEED, 0.25))); moveValue = (int) normalizeSpeed(moveValue); if (Math.abs(moveValue - lastSpeed) > MAX_ACCELERATION) { if (lastSpeed > moveValue) moveValue = (int) (lastSpeed - MAX_ACCELERATION);