Search in sources :

Example 1 with Control

use of org.openbot.vehicle.Control in project OpenBot by isl-org.

the class ControlsFragment method handlePhoneControllerEvents.

private void handlePhoneControllerEvents() {
    ControllerToBotEventBus.subscribe(this.getClass().getSimpleName(), event -> {
        String commandType = "";
        if (event.has("command")) {
            commandType = event.getString("command");
        } else if (event.has("driveCmd")) {
            commandType = Constants.CMD_DRIVE;
        }
        switch(commandType) {
            case Constants.CMD_DRIVE:
                JSONObject driveValue = event.getJSONObject("driveCmd");
                vehicle.setControl(new Control(Float.parseFloat(driveValue.getString("l")), Float.parseFloat(driveValue.getString("r"))));
                break;
            case Constants.CMD_INDICATOR_LEFT:
                toggleIndicatorEvent(Enums.VehicleIndicator.LEFT.getValue());
                break;
            case Constants.CMD_INDICATOR_RIGHT:
                toggleIndicatorEvent(Enums.VehicleIndicator.RIGHT.getValue());
                break;
            case Constants.CMD_INDICATOR_STOP:
                toggleIndicatorEvent(Enums.VehicleIndicator.STOP.getValue());
                break;
            // We re connected to the controller, send back status info
            case Constants.CMD_CONNECTED:
                // PhoneController class will receive this event and resent it to the
                // controller.
                // Other controllers can subscribe to this event as well.
                // That is why we are not calling phoneController.send() here directly.
                BotToControllerEventBus.emitEvent(ConnectionUtils.getStatus(false, false, false, currentDriveMode.toString(), vehicle.getIndicator()));
                break;
            case Constants.CMD_DISCONNECTED:
                vehicle.setControl(0, 0);
                break;
        }
        processControllerKeyData(commandType);
    }, error -> {
        Log.d(null, "Error occurred in ControllerToBotEventBus: " + error);
    }, // filter out everything else
    event -> event.has("command") || event.has("driveCmd"));
}
Also used : Control(org.openbot.vehicle.Control) JSONObject(org.json.JSONObject)

Example 2 with Control

use of org.openbot.vehicle.Control in project OpenBot by isl-org.

the class GameControllerTest method convertDualToControl_test.

@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);
}
Also used : Control(org.openbot.vehicle.Control) Test(org.junit.Test)

Example 3 with Control

use of org.openbot.vehicle.Control in project OpenBot by isl-org.

the class GameControllerTest method convertGameToControl_test.

@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);
}
Also used : Control(org.openbot.vehicle.Control) Test(org.junit.Test)

Example 4 with Control

use of org.openbot.vehicle.Control in project OpenBot by isl-org.

the class GameControllerTest method convertJoystickToControl_test.

@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);
}
Also used : Control(org.openbot.vehicle.Control) Test(org.junit.Test)

Example 5 with Control

use of org.openbot.vehicle.Control in project OpenBot by isl-org.

the class MultiBoxTracker method updateTarget.

public synchronized Control updateTarget() {
    if (!trackedObjects.isEmpty()) {
        // Pick person with highest probability
        final RectF trackedPos = new RectF(trackedObjects.get(0).location);
        final boolean rotated = sensorOrientation % 180 == 90;
        float imgWidth = (float) (rotated ? frameHeight : frameWidth);
        float centerX = (rotated ? trackedPos.centerY() : trackedPos.centerX());
        // Make sure object center is in frame
        centerX = Math.max(0.0f, Math.min(centerX, imgWidth));
        // Scale relative position along x-axis between -1 and 1
        float x_pos_norm = 1.0f - 2.0f * centerX / imgWidth;
        // Scale to control signal and account for rotation
        float x_pos_scaled = rotated ? -x_pos_norm * 1.0f : x_pos_norm * 1.0f;
        if (x_pos_scaled < 0) {
            leftControl = 1.0f;
            rightControl = 1.0f + x_pos_scaled;
        } else {
            leftControl = 1.0f - x_pos_scaled;
            rightControl = 1.0f;
        }
    } else {
        leftControl = 0.0f;
        rightControl = 0.0f;
    }
    return new Control((0 > sensorOrientation) ? rightControl : leftControl, (0 > sensorOrientation) ? leftControl : rightControl);
}
Also used : RectF(android.graphics.RectF) Control(org.openbot.vehicle.Control)

Aggregations

Control (org.openbot.vehicle.Control)7 Test (org.junit.Test)5 RectF (android.graphics.RectF)1 JSONObject (org.json.JSONObject)1