From 97b0b3bd0732fe4a33b7c50406a09eb29dbb8b2f Mon Sep 17 00:00:00 2001 From: Matthias Mueller Date: Thu, 17 Feb 2022 19:05:27 +0100 Subject: [PATCH 1/2] add test for game controller --- .../java/org/openbot/env/GameController.java | 10 +--- .../org/openbot/env/GameControllerTest.java | 50 +++++++++++++++++++ 2 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 android/app/src/test/java/org/openbot/env/GameControllerTest.java diff --git a/android/app/src/main/java/org/openbot/env/GameController.java b/android/app/src/main/java/org/openbot/env/GameController.java index 06e4d47ef..4cae24aa5 100644 --- a/android/app/src/main/java/org/openbot/env/GameController.java +++ b/android/app/src/main/java/org/openbot/env/GameController.java @@ -2,8 +2,6 @@ package org.openbot.env; -import static java.lang.Math.max; -import static java.lang.Math.min; import android.util.Pair; import android.view.InputDevice; @@ -159,7 +157,7 @@ public Control convertGameToControl(float leftTrigger, float rightTrigger, float if (right >= 0) right -= steeringOffset; else right += steeringOffset; - return new Control(enforceLimits(left), enforceLimits(right)); + return new Control(left, right); } public Control convertJoystickToControl(float xAxis, float yAxis) { @@ -171,7 +169,7 @@ public Control convertJoystickToControl(float xAxis, float yAxis) { if (right >= 0) right -= xAxis; else right += xAxis; - return new Control(enforceLimits(left), enforceLimits(right)); + return new Control(left, right); } public static Pair processJoystickInputLeft(MotionEvent event, int historyPos) { @@ -203,8 +201,4 @@ public static Pair processJoystickInputRight(MotionEvent event, in return new Pair<>(x, y); } - - private float enforceLimits(float control) { - return max(-1.f, min(control, 1.f)); - } } diff --git a/android/app/src/test/java/org/openbot/env/GameControllerTest.java b/android/app/src/test/java/org/openbot/env/GameControllerTest.java new file mode 100644 index 000000000..1a2c3fb06 --- /dev/null +++ b/android/app/src/test/java/org/openbot/env/GameControllerTest.java @@ -0,0 +1,50 @@ +package org.openbot.env; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.openbot.utils.Enums; +import org.openbot.vehicle.Control; + +public class GameControllerTest { + @Test + public void convertDualToControl_test() { + GameController gameController = new GameController(Enums.DriveMode.DUAL); + assertEquals(Enums.DriveMode.DUAL, gameController.getDriveMode()); + Control control = gameController.convertDualToControl(0.5f, -0.5f); + assertEquals(control.getLeft(), -0.5f, 0.0f); + assertEquals(control.getRight(), 0.5f, 0.0f); + } + + @Test + public void convertGameToControl_test() { + GameController gameController = new GameController(Enums.DriveMode.GAME); + assertEquals(Enums.DriveMode.GAME, gameController.getDriveMode()); + Control control; + control = gameController.convertGameToControl(0.0f, 0.5f, 1.0f); + assertEquals(control.getLeft(), 1.0f, 0.0f); + assertEquals(control.getRight(), -0.5f, 0.0f); + + control = gameController.convertGameToControl(0.0f, 0.5f, -1.0f); + assertEquals(control.getLeft(), -0.5f, 0.0f); + assertEquals(control.getRight(), 1.0f, 0.0f); + + control = gameController.convertGameToControl(0.0f, 0.5f, 0.0f); + assertEquals(control.getLeft(), 0.5f, 0.0f); + assertEquals(control.getRight(), 0.5f, 0.0f); + } + + @Test + public void convertJoystickToControl_test() { + GameController gameController = new GameController(Enums.DriveMode.JOYSTICK); + assertEquals(Enums.DriveMode.JOYSTICK, gameController.getDriveMode()); + Control control; + control = gameController.convertJoystickToControl(0.5f, -0.5f); + assertEquals(control.getLeft(), 1.0f, 0.0f); + assertEquals(control.getRight(), 0.0f, 0.0f); + + control = gameController.convertJoystickToControl(-0.5f, -0.5f); + assertEquals(control.getLeft(), 0.0f, 0.0f); + assertEquals(control.getRight(), 1.0f, 0.0f); + } +} From 6ce2df357a11b2376e193bc2aa005a6fccf53178 Mon Sep 17 00:00:00 2001 From: Matthias Mueller Date: Fri, 18 Feb 2022 00:39:51 +0100 Subject: [PATCH 2/2] fix style --- android/app/src/main/java/org/openbot/env/GameController.java | 1 - 1 file changed, 1 deletion(-) diff --git a/android/app/src/main/java/org/openbot/env/GameController.java b/android/app/src/main/java/org/openbot/env/GameController.java index 4cae24aa5..432f6e1c0 100644 --- a/android/app/src/main/java/org/openbot/env/GameController.java +++ b/android/app/src/main/java/org/openbot/env/GameController.java @@ -2,7 +2,6 @@ package org.openbot.env; - import android.util.Pair; import android.view.InputDevice; import android.view.KeyEvent;