Skip to content

Python Example: Override Target Rotation

Michael Jansen edited this page Dec 7, 2023 · 1 revision

There are some cases where you may wish to override the target rotation while path following, such as targeting a game piece. This can be accomplished by providing a function to the PPHolonomicDriveController class that will supply an optional rotation override. This is a static method so it will apply to all path following commands.

NOTE: If you supply a method to override the rotation target, it will be called every loop. You must make sure to return None when you do not wish to override the rotation.

from pathplannerlib.controller import PPHolonomicDriveController
from wpimath.geometry import Rotation2d

class SwerveSubsystem(Subsystem):
    def __init__(self):
        # ... other initialization

        # Set the method that will be used to get rotation overrides
        PPHolonomicDriveController.setRotationTargetOverride(self.getRotationTargetOverride);

    def getRotationTargetOverride(self) -> Rotation2d:
        # Some condition that should decide if we want to override rotation
        if Limelight.hasGamePieceTarget():
            # Return the rotation override (this should be a field relative rotation)
            return Limelight.getRobotToGamePieceRotation()
        else:
            # return None when we don't want to override the path's rotation
            return None
Clone this wiki locally