-
Notifications
You must be signed in to change notification settings - Fork 136
PathPlannerLib: Java Usage
The Java version of PathPlannerLib is designed as an extension of the WPILib trajectory library. This means that a path generated with PathPlanner should function as a drop-in replacement for a WPILib trajectory in any code that can that uses one, such as WPILib's included path followers. For custom path followers, each state in the path can be casted to the PathPlanner specific version to unlock added functionality such as holonomic mode.
// This will load the file "Example Path.path" and generate it with a max velocity of 4 m/s and a max acceleration of 3 m/s^2
PathPlannerTrajectory examplePath = PathPlanner.loadPath("Example Path", new PathConstrains(4, 3));
// This trajectory can then be passed to a path follower such as a PPSwerveControllerCommand
// Or the path can be sampled at a given point in time for custom path following
// Sample the state of the path at 1.2 seconds
PathPlannerState exampleState = (PathPlannerState) examplePath.sample(1.2);
// Print the velocity at the sampled time
System.out.println(exampleState.velocityMetersPerSecond);
This example will load a path file as a path group. The path group will be separated into paths based on which waypoints are marked stop points.
// This will load the file "Example Path Group.path" and generate it with a max velocity of 4 m/s and a max acceleration of 3 m/s^2
// for every path in the group
ArrayList<PathPlannerTrajectory> pathGroup1 = PathPlanner.loadPathGroup("Example Path Group", new PathConstraints(4, 3));
// This will load the file "Example Path Group.path" and generate it with different path constraints for each segment
ArrayList<PathPlannerTrajectory> pathGroup2 = PathPlanner.loadPathGroup(
"Example Path Group",
new PathConstraints(4, 3),
new PathConstraints(2, 2),
new PathConstraints(3, 3));
This example will use PPLib's auto builder functionality to generate a full autonomous command. This will make use of path groups, event markers, and stop events to build a full auto. This specific example will use the SwerveAutoBuilder, but other options are available for Mecanum and Ramsete. Custom auto builders can easily be created by extending the BaseAutoBuilder class and overriding its methods to customize functionality.
// This will load the file "FullAuto.path" and generate it with a max velocity of 4 m/s and a max acceleration of 3 m/s^2
// for every path in the group
ArrayList<PathPlannerTrajectory> pathGroup = PathPlanner.loadPathGroup("FullAuto", new PathConstraints(4, 3));
// This is just an example event map. It would be better to have a constant, global event map
// in your code that will be used by all path following commands.
HashMap<String, Command> eventMap = new HashMap<>();
eventMap.put("marker1", new PrintCommand("Passed marker 1"));
eventMap.put("intakeDown", new IntakeDown());
// Create the AutoBuilder. This only needs to be created once when robot code starts, not every time you want to create an auto command. A good place to put this is in RobotContainer along with your subsystems.
SwerveAutoBuilder autoBuilder = new SwerveAutoBuilder(
driveSubsystem::getPose, // Pose2d supplier
driveSubsystem::resetPose, // Pose2d consumer, used to reset odometry at the beginning of auto
driveSubsystem.kinematics, // SwerveDriveKinematics
new PIDConstants(5.0, 0.0, 0.0), // PID constants to correct for translation error (used to create the X and Y PID controllers)
new PIDConstants(0.5, 0.0, 0.0), // PID constants to correct for rotation error (used to create the rotation controller)
driveSubsystem::setModuleStates, // Module states consumer used to output to the drive subsystem
eventMap,
true, // Should the path be automatically mirrored depending on alliance color. Optional, defaults to true
driveSubsystem // The drive subsystem. Used to properly set the requirements of path following commands
);
Command fullAuto = autoBuilder.fullAuto(pathGroup);
This example shows how to use the FollowPathWithEvents command. This command will run a given path following command, as well as trigger events at markers along the path.
// This will load the file "Example Path.path" and generate it with a max velocity of 4 m/s and a max acceleration of 3 m/s^2
PathPlannerTrajectory examplePath = PathPlanner.loadPath("Example Path", new PathConstrains(4, 3));
// This is just an example event map. It would be better to have a constant, global event map
// in your code that will be used by all path following commands.
HashMap<String, Command> eventMap = new HashMap<>();
eventMap.put("marker1", new PrintCommand("Passed marker 1"));
eventMap.put("intakeDown", new IntakeDown());
FollowPathWithEvents command = new FollowPathWithEvents(
getPathFollowingCommand(examplePath),
examplePath.getMarkers(),
eventMap
);
This example uses the generatePath
method to generate paths that have not been planned in advance with a .path file
// Simple path without holonomic rotation. Stationary start/end. Max velocity of 4 m/s and max accel of 3 m/s^2
PathPlannerTrajectory traj1 = PathPlanner.generatePath(
new PathConstraints(4, 3),
new PathPoint(new Translation2d(1.0, 1.0), Rotation2d.fromDegrees(0)), // position, heading
new PathPoint(new Translation2d(3.0, 3.0), Rotation2d.fromDegrees(45)) // position, heading
);
// Simple path with holonomic rotation. Stationary start/end. Max velocity of 4 m/s and max accel of 3 m/s^2
PathPlannerTrajectory traj2 = PathPlanner.generatePath(
new PathConstraints(4, 3),
new PathPoint(new Translation2d(1.0, 1.0), Rotation2d.fromDegrees(0), Rotation2d.fromDegrees(0)), // position, heading(direction of travel), holonomic rotation
new PathPoint(new Translation2d(3.0, 3.0), Rotation2d.fromDegrees(45), Rotation2d.fromDegrees(-90) // position, heading(direction of travel), holonomic rotation
);
// More complex path with holonomic rotation. Non-zero starting velocity of 2 m/s. Max velocity of 4 m/s and max accel of 3 m/s^2
PathPlannerTrajectory traj3 = PathPlanner.generatePath(
new PathConstraints(4, 3),
new PathPoint(new Translation2d(1.0, 1.0), Rotation2d.fromDegrees(0), Rotation2d.fromDegrees(0), 2), // position, heading(direction of travel), holonomic rotation, velocity override
new PathPoint(new Translation2d(3.0, 3.0), Rotation2d.fromDegrees(45), Rotation2d.fromDegrees(-90), // position, heading(direction of travel), holonomic rotation
new PathPoint(new Translation2d(5.0, 3.0), Rotation2d.fromDegrees(0), Rotation2d.fromDegrees(-30) // position, heading(direction of travel), holonomic rotation
);
This example shows a drivetrain subsystem method that will create command to follow a path and reset odometry if it is the first auto path.
// Assuming this method is part of a drivetrain subsystem that provides the necessary methods
public Command followTrajectoryCommand(PathPlannerTrajectory traj, boolean isFirstPath) {
return new SequentialCommandGroup(
new InstantCommand(() -> {
// Reset odometry for the first path you run during auto
if(isFirstPath){
this.resetOdometry(traj.getInitialHolonomicPose());
}
}),
new PPSwerveControllerCommand(
traj,
this::getPose, // Pose supplier
this.kinematics, // SwerveDriveKinematics
new PIDController(0, 0, 0), // X controller. Tune these values for your robot. Leaving them 0 will only use feedforwards.
new PIDController(0, 0, 0), // Y controller (usually the same values as X controller)
new PIDController(0, 0, 0), // Rotation controller. Tune these values for your robot. Leaving them 0 will only use feedforwards.
this::setModuleStates, // Module states consumer
true, // Should the path be automatically mirrored depending on alliance color. Optional, defaults to true
this // Requires this drive subsystem
)
);
}
This example shows a drivetrain subsystem method that will create command to follow a path and reset odometry if it is the first auto path.
// Assuming this method is part of a drivetrain subsystem that provides the necessary methods
public Command followTrajectoryCommand(PathPlannerTrajectory traj, boolean isFirstPath) {
return new SequentialCommandGroup(
new InstantCommand(() -> {
// Reset odometry for the first path you run during auto
if(isFirstPath){
this.resetOdometry(traj.getInitialHolonomicPose());
}
}),
new PPMecanumControllerCommand(
traj,
this::getPose, // Pose supplier
this.kinematics, // MecanumDriveKinematics
new PIDController(0, 0, 0), // X controller. Tune these values for your robot. Leaving them 0 will only use feedforwards.
new PIDController(0, 0, 0), // Y controller (usually the same values as X controller)
new PIDController(0, 0, 0), // Rotation controller. Tune these values for your robot. Leaving them 0 will only use feedforwards.
3.0, // Max wheel velocity meters per second
this::outputWheelSpeeds, // MecanumDriveWheelSpeeds consumer
true, // Should the path be automatically mirrored depending on alliance color. Optional, defaults to true
this // Requires this drive subsystem
)
);
}
This example shows a drivetrain subsystem method that will create command to follow a path and reset odometry if it is the first auto path.
// Assuming this method is part of a drivetrain subsystem that provides the necessary methods
public Command followTrajectoryCommand(PathPlannerTrajectory traj, boolean isFirstPath) {
return new SequentialCommandGroup(
new InstantCommand(() -> {
// Reset odometry for the first path you run during auto
if(isFirstPath){
this.resetOdometry(traj.getInitialPose());
}
}),
new PPRamseteCommand(
traj,
this::getPose, // Pose supplier
new RamseteController(),
new SimpleMotorFeedforward(KS, KV, KA),
this.kinematics, // DifferentialDriveKinematics
this::getWheelSpeeds, // DifferentialDriveWheelSpeeds supplier
new PIDController(0, 0, 0), // Left controller. Tune these values for your robot. Leaving them 0 will only use feedforwards.
new PIDController(0, 0, 0), // Right controller (usually the same values as left controller)
this::outputVolts, // Voltage biconsumer
true, // Should the path be automatically mirrored depending on alliance color. Optional, defaults to true
this // Requires this drive subsystem
)
);
}
- Controls & Shortcuts
- Editing Paths & Autos
- Navigation Menu
- Settings
- Project Browser
- Telemetry Page
- Navigation Grid Editor
- Register Named Commands
- Build an Auto
- Follow a Single Path
- Create a Path On-the-fly
- Path Groups
- Automatic Pathfinding
Advanced Usage
- Register Named Commands
- Build an Auto
- Follow a Single Path
- Create a Path On-the-fly
- Path Groups
- Automatic Pathfinding
Advanced Usage
- Register Named Commands
- Build an Auto
- Follow a Single Path
- Create a Path On-the-fly
- Path Groups
- Automatic Pathfinding