Skip to content

Commit

Permalink
Merge pull request #261 from isl-org/thias15/gamecontroller-test
Browse files Browse the repository at this point in the history
add test for game controller
  • Loading branch information
thias15 authored Feb 17, 2022
2 parents 5231639 + 6ce2df3 commit 23bd8ef
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
11 changes: 2 additions & 9 deletions android/app/src/main/java/org/openbot/env/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +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;
import android.view.KeyEvent;
Expand Down Expand Up @@ -159,7 +156,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) {
Expand All @@ -171,7 +168,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<Float, Float> processJoystickInputLeft(MotionEvent event, int historyPos) {
Expand Down Expand Up @@ -203,8 +200,4 @@ public static Pair<Float, Float> processJoystickInputRight(MotionEvent event, in

return new Pair<>(x, y);
}

private float enforceLimits(float control) {
return max(-1.f, min(control, 1.f));
}
}
50 changes: 50 additions & 0 deletions android/app/src/test/java/org/openbot/env/GameControllerTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 23bd8ef

Please sign in to comment.