Autonomous Pathing

There are many different ways to implement pathing. In this guide, we will use PathPlanner to generate and follow paths.

Installation

Note

Offical documentation can be found here.

To start, install PathPlannerLib using this link:

https://3015rangerrobotics.github.io/pathplannerlib/PathplannerLib.json

Next, download PathPlanner from the Microsoft or Mac App Store.

Path Generation

  1. Open PathPlanner, click “Switch Project” in the menu, and select the root of your project.

../../_images/PathPlanner1.PNG
  1. Go to “Settings” in the menu, and configure everything according to your robot. Make sure to also select “Holonomic Mode”.

../../_images/PathPlanner2.PNG
  1. Finally, draw the paths you want the robot to follow.

Path Following

We will now extract the path made in PathPlanner and use it in our robot code.

In RobotContainer.java, create an instance of PathPlannerTrajectory by extracting the path you made in PathPlanner.

1private PathPlannerTrajectory mTrajectory = PathPlanner.loadPath(
2    "PATH NAME",
3    SwerveConstants.kMaxVelocityMetersPerSecond,
4    SwerveConstants.kMaxAccelerationMetersPerSecond
5);

Next, create 3 different PID controllers that will be used to control robot heading and movement in the x and y directions.

 1// PID controller for movement in the X direction
 2private PIDController mXController = new PIDController(
 3    SwerveConstants.kPathingX_kP,
 4    SwerveConstants.kPathingX_kI,
 5    SwerveConstants.kPathingX_kD
 6);
 7
 8// PID controller for movement in the Y direction
 9private PIDController mYController = new PIDController(
10    SwerveConstants.kPathingY_kP,
11    SwerveConstants.kPathingY_kI,
12    SwerveConstants.kPathingY_kD
13);
14
15// PID controller for robot heading
16private PIDController mThetaController = new PIDController(
17    SwerveConstants.kPathingTheta_kP,
18    SwerveConstants.kPathingTheta_kI,
19    SwerveConstants.kPathingTheta_kD
20);

Warning

All constants used in the PID controllers must be tuned. X and Y controllers usually have the same constants, but the theta controller usually has different constants. Tune the X and Y controller constants by making the robot follow long straight paths. Once the X and Y controllers are tuned, tune the theta controller by making the robot follow a short path with a sharp turn.

Finally, create a SwerveControllerCommand that will be used to follow the path.

 1private PPSwerveControllerCommand mCommand = new PPSwerveControllerCommand(
 2    mTrajectory,
 3    mSwerve::getPose,
 4    SwerveConstants.kSwerveKinematics,
 5    mXController,
 6    mYController,
 7    mThetaController,
 8    mSwerve::setModuleStates,
 9    mSwerve
10);

Return the command in getAutonomousCommand().

1@Override
2public Command getAutonomousCommand() {
3    // `andThen...` is used to stop the robot after the path is finished
4    return command.andThen(() -> mSwerve.drive(0, 0, 0, false));
5}

This allows the robot to follow the path you made in PathPlanner in autonomous.

Note

If the path following is inaccurate or violent, try changing some of the PID controller constants.

Note

Check out the code we use in these docs on our Github!