Search in sources :

Example 1 with Gamepad

use of me.wobblyyyy.pathfinder2.utils.Gamepad in project Pathfinder2 by Wobblyyyy.

the class ExampleListeners method examplePositionListeners.

@SuppressWarnings({ "CodeBlock2Expr", "unchecked", "InfiniteLoopStatement" })
public void examplePositionListeners() {
    Pathfinder pathfinder = Pathfinder.newSimulatedPathfinder(0.01);
    Gamepad gamepad = new Gamepad();
    // first up, some listeners for the robot's actual position. you can
    // create listeners fairly easily:
    // (also, notice how method chaining is used to make code more
    // readable - you don't have to do this!)
    pathfinder.addListener(new Listener(ListenerMode.CONDITION_NEWLY_MET, () -> {
        // this will be executed ONCE whenever x exceeds 500.
        // in order for this to be executed again, x will have to
        // dip below 500 and then come back over 500 again
        System.out.println("x crossed over 500!");
    }, () -> {
        return pathfinder.getPosition().x() > 500;
    })).addListener(new Listener(// here's a nicer-looking way to do the same exact thing.
    ListenerMode.CONDITION_NEWLY_MET, () -> System.out.println("y crossed over 500!"), () -> pathfinder.getPosition().y() > 500)).addListener(new Listener(ListenerMode.CONDITION_NEWLY_MET, () -> System.out.println("x AND y crossed over 500! wow!"), () -> {
        PointXY position = pathfinder.getPosition();
        return position.x() > 500 && position.y() > 500;
    }));
    // time for more listeners! this time, these listeners demonstrate
    // how you could use listeners to bind functionality to a button.
    pathfinder.addListener(new Listener(ListenerMode.CONDITION_NEWLY_MET, () -> {
        System.out.println("the A button has been pressed!");
    }, gamepad::a));
    pathfinder.addListener(new Listener(ListenerMode.CONDITION_NEWLY_NOT_MET, () -> {
        System.out.println("the A button has been released!");
    }, gamepad::a));
    // here's a more complex condition...
    // notice how we make use of the oh-so-lovely lambda syntax available
    // in java to make this code significantly more tolerable
    pathfinder.addListener(new Listener(ListenerMode.CONDITION_NEWLY_MET, () -> System.out.println("the right and left joysticks have" + "magnitudes above 0.5!"), () -> gamepad.joysticks.right().getMagnitude() > 0.5 && gamepad.joysticks.left().getMagnitude() > 0.5));
    // and here's an alternative way to have multiple preconditions.
    pathfinder.addListener(new Listener(ListenerMode.CONDITION_NEWLY_NOT_MET, () -> {
        System.out.println("the right and left joysticks have" + "magnitudes below 0.5!");
    }, // precondition #1
    () -> {
        return gamepad.joysticks.right().getMagnitude() > 0.5;
    }, // precondition #2
    () -> {
        return gamepad.joysticks.left().getMagnitude() > 0.5;
    }));
    // implementation, but yeah.
    while (true) {
        pathfinder.tick();
    }
}
Also used : Pathfinder(me.wobblyyyy.pathfinder2.Pathfinder) Gamepad(me.wobblyyyy.pathfinder2.utils.Gamepad) Listener(me.wobblyyyy.pathfinder2.listening.Listener) PointXY(me.wobblyyyy.pathfinder2.geometry.PointXY)

Example 2 with Gamepad

use of me.wobblyyyy.pathfinder2.utils.Gamepad in project Pathfinder2 by Wobblyyyy.

the class ExamplePathfinder method loop.

/**
 * This is a traditional loop method - it's meant to be run dozens of times
 * per second, over and over and over again. The general premise for this
 * loop is as follows: if Pathfinder is NOT active (meaning it isn't
 * following any trajectories), it'll check for user input using the
 * A, B, X, and Y gamepad buttons. If any of those buttons are pressed,
 * the robot will begin automatically navigating to an associated position.
 * <p>
 * Let's say Pathfinder IS active... what happens then? There will be times
 * when Pathfinder is attempting to control your robot while you'd like to
 * be the one who's in control of it. In this case, you should clear
 * Pathfinder, meaning it will no longer try to follow any paths, meaning
 * you have control over the robot.
 */
@SuppressWarnings("UnnecessaryLocalVariable")
public void loop() {
    if (!pathfinder.isActive()) {
        // If Pathfinder isn't active, let's take a look at our
        // controller inputs.
        PointXYZ targetPoint = null;
        // Pretty cool, right?
        if (gamepadA)
            targetPoint = TARGET_A;
        else if (gamepadB)
            targetPoint = TARGET_B;
        else if (gamepadX)
            targetPoint = TARGET_X;
        else if (gamepadY)
            targetPoint = TARGET_Y;
        if (targetPoint != null) {
            pathfinder.goTo(targetPoint);
        } else {
            // Based on some joysticks, generate a translation.
            // This translation will then be used to drive the robot.
            double moveForwards = joystick1y;
            double moveStrafe = joystick1x;
            double moveRotate = joystick2x;
            Translation translation = new Translation(moveForwards, moveStrafe, moveRotate);
            pathfinder.getRobot().drive().setTranslation(translation);
        }
    }
    if (gamepadStart) {
        // Let's say we want to manually override Pathfinder and regain
        // control of the robot. All we'd have to do:
        pathfinder.clear();
    }
    // ... any other code that you would need in your main loop
    // ex. sensor updates, other motors, you know the deal
    // Tick or update Pathfinder once. Remember, this is absolutely
    // essential - if you don't tick Pathfinder, nothing can happen.
    pathfinder.tick();
}
Also used : Translation(me.wobblyyyy.pathfinder2.geometry.Translation) PointXYZ(me.wobblyyyy.pathfinder2.geometry.PointXYZ)

Aggregations

Pathfinder (me.wobblyyyy.pathfinder2.Pathfinder)1 PointXY (me.wobblyyyy.pathfinder2.geometry.PointXY)1 PointXYZ (me.wobblyyyy.pathfinder2.geometry.PointXYZ)1 Translation (me.wobblyyyy.pathfinder2.geometry.Translation)1 Listener (me.wobblyyyy.pathfinder2.listening.Listener)1 Gamepad (me.wobblyyyy.pathfinder2.utils.Gamepad)1