Search in sources :

Example 11 with Pathfinder

use of net.minecraft.server.v1_15_R1.Pathfinder in project Pathfinder2 by Wobblyyyy.

the class GenericTrajectoryBenchmarker method followTrajectories.

public void followTrajectories(Blackhole blackhole, Trajectory... trajectories) {
    SimulatedRobot robot = new SimulatedRobot();
    Pathfinder pathfinder = new Pathfinder(robot, -0.05);
    PointXYZ lastPosition = PointXYZ.ZERO;
    double distance = 0;
    for (Trajectory trajectory : trajectories) {
        robot.setPosition(PointXYZ.ZERO);
        pathfinder.followTrajectory(trajectory);
        while (distance < 10) {
            pathfinder.tick();
            distance += pathfinder.getPosition().absDistance(lastPosition);
        }
    }
}
Also used : Pathfinder(me.wobblyyyy.pathfinder2.Pathfinder) SimulatedRobot(me.wobblyyyy.pathfinder2.robot.simulated.SimulatedRobot) PointXYZ(me.wobblyyyy.pathfinder2.geometry.PointXYZ)

Example 12 with Pathfinder

use of net.minecraft.server.v1_15_R1.Pathfinder in project Pathfinder2 by Wobblyyyy.

the class ExampleListeners method exampleShifter.

@SuppressWarnings("InfiniteLoopStatement")
public void exampleShifter() {
    Pathfinder pathfinder = Pathfinder.newSimulatedPathfinder(0.01);
    Shifter shifter = new Shifter(1, 1, 5, false, s -> {
    });
    pathfinder.getListenerManager().bind(ListenerMode.CONDITION_NEWLY_MET, this::aButton, b -> b, b -> shifter.shift(ShifterDirection.UP));
    pathfinder.getListenerManager().bind(ListenerMode.CONDITION_NEWLY_MET, this::bButton, b -> b, b -> shifter.shift(ShifterDirection.DOWN));
    pathfinder.getListenerManager().bind(ListenerMode.CONDITION_NEWLY_MET, this::xButton, b -> b, b -> shifter.setGear(1));
    pathfinder.getListenerManager().bind(ListenerMode.CONDITION_NEWLY_MET, this::yButton, b -> b, b -> shifter.setGear(5));
    while (true) {
        pathfinder.tick();
    }
}
Also used : Pathfinder(me.wobblyyyy.pathfinder2.Pathfinder) Shifter(me.wobblyyyy.pathfinder2.utils.Shifter)

Example 13 with Pathfinder

use of net.minecraft.server.v1_15_R1.Pathfinder 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 14 with Pathfinder

use of net.minecraft.server.v1_15_R1.Pathfinder in project Pathfinder2 by Wobblyyyy.

the class TestHeadingLock method testHeadingLockAlongLine.

@Test
public void testHeadingLockAlongLine() {
    Pathfinder pathfinder = Pathfinder.newSimulatedPathfinder(0.01);
    SimulatedRobot odometry = (SimulatedRobot) pathfinder.getOdometry();
    pathfinder.loadBundledPlugins();
    pathfinder.lockHeading(new PointXY(10, 10));
    pathfinder.followTrajectory(new LinearTrajectory(new PointXYZ(0, 10, 0), 0.5, 2, Angle.fromDeg(5)));
    odometry.setPosition(new PointXYZ(0, 0, 0));
    pathfinder.tick();
    Assertions.assertEquals(-0.45, pathfinder.getTranslation().vz());
    odometry.setPosition(new PointXYZ(0, 0, 45));
    pathfinder.tick();
    Assertions.assertEquals(0, pathfinder.getTranslation().vz());
    odometry.setPosition(new PointXYZ(0, 0, 90));
    pathfinder.tick();
    Assertions.assertEquals(0.45, pathfinder.getTranslation().vz());
    odometry.setPosition(0, 5, 0);
    pathfinder.tick();
    Assertions.assertTrue(Equals.soft(-0.265, pathfinder.getTranslation().vz(), 0.01));
    odometry.setPosition(5, 5, 0);
    pathfinder.tick();
    Assertions.assertTrue(Equals.soft(-0.45, pathfinder.getTranslation().vz(), 0.01));
}
Also used : Pathfinder(me.wobblyyyy.pathfinder2.Pathfinder) LinearTrajectory(me.wobblyyyy.pathfinder2.trajectory.LinearTrajectory) PointXY(me.wobblyyyy.pathfinder2.geometry.PointXY) SimulatedRobot(me.wobblyyyy.pathfinder2.robot.simulated.SimulatedRobot) PointXYZ(me.wobblyyyy.pathfinder2.geometry.PointXYZ) Test(org.junit.jupiter.api.Test)

Example 15 with Pathfinder

use of net.minecraft.server.v1_15_R1.Pathfinder in project Pathfinder2 by Wobblyyyy.

the class ExampleRecording method init.

public void init() {
    drive = new SimulatedDrive();
    odometry = new SimulatedOdometry();
    robot = new Robot(drive, odometry);
    pathfinder = new Pathfinder(robot, 0.02);
}
Also used : Pathfinder(me.wobblyyyy.pathfinder2.Pathfinder) SimulatedDrive(me.wobblyyyy.pathfinder2.robot.simulated.SimulatedDrive) SimulatedOdometry(me.wobblyyyy.pathfinder2.robot.simulated.SimulatedOdometry) Robot(me.wobblyyyy.pathfinder2.robot.Robot)

Aggregations

Pathfinder (me.wobblyyyy.pathfinder2.Pathfinder)17 PointXYZ (me.wobblyyyy.pathfinder2.geometry.PointXYZ)7 Translation (me.wobblyyyy.pathfinder2.geometry.Translation)5 SimulatedRobot (me.wobblyyyy.pathfinder2.robot.simulated.SimulatedRobot)5 SimulatedOdometry (me.wobblyyyy.pathfinder2.robot.simulated.SimulatedOdometry)4 Test (org.junit.jupiter.api.Test)4 ProportionalController (me.wobblyyyy.pathfinder2.control.ProportionalController)2 PointXY (me.wobblyyyy.pathfinder2.geometry.PointXY)2 Robot (me.wobblyyyy.pathfinder2.robot.Robot)2 SimulatedDrive (me.wobblyyyy.pathfinder2.robot.simulated.SimulatedDrive)2 ElapsedTimer (me.wobblyyyy.pathfinder2.time.ElapsedTimer)2 SplineBuilderFactory (me.wobblyyyy.pathfinder2.trajectory.spline.SplineBuilderFactory)2 Shifter (me.wobblyyyy.pathfinder2.utils.Shifter)2 TimedRobot (edu.wpi.first.wpilibj.TimedRobot)1 HashMap (java.util.HashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Controller (me.wobblyyyy.pathfinder2.control.Controller)1 Listener (me.wobblyyyy.pathfinder2.listening.Listener)1 ListenerMode (me.wobblyyyy.pathfinder2.listening.ListenerMode)1