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
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);
}
}