Guest User

auto align command help

a guest
Apr 7th, 2025
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.79 KB | Source Code | 0 0
  1. package frc.robot.commands;
  2.  
  3. import com.ctre.phoenix6.mechanisms.swerve.SwerveRequest.RobotCentric;
  4. import com.ctre.phoenix6.mechanisms.swerve.SwerveRequest;
  5. import com.ctre.phoenix6.controls.DriveRequestType;
  6.  
  7. import edu.wpi.first.math.geometry.Pose2d;
  8. import edu.wpi.first.math.geometry.Rotation2d;
  9. import edu.wpi.first.math.geometry.Translation2d;
  10. import edu.wpi.first.wpilibj2.command.Command;
  11.  
  12. import java.util.function.Supplier;
  13.  
  14. import frc.robot.subsystems.CommandSwerveDrivetrain;
  15. import frc.robot.LimelightHelpers; // make sure you have this helper class
  16.  
  17. public class DriveToPoseCommand extends Command {
  18.     private final CommandSwerveDrivetrain drivetrain;
  19.     private final Supplier<Pose2d> targetPoseSupplier;
  20.  
  21.     private final RobotCentric robotCentric = new RobotCentric()
  22.             .withDriveRequestType(DriveRequestType.OpenLoopVoltage);
  23.  
  24.     private final double kP_xy = 1.5;
  25.     private final double kP_theta = 4.0;
  26.     private final double toleranceMeters = 0.05;
  27.     private final double toleranceDegrees = 2.0;
  28.  
  29.     public DriveToPoseCommand(CommandSwerveDrivetrain drivetrain, Supplier<Pose2d> targetPoseSupplier) {
  30.         this.drivetrain = drivetrain;
  31.         this.targetPoseSupplier = targetPoseSupplier;
  32.         addRequirements(drivetrain);
  33.     }
  34.  
  35.     @Override
  36.     public void execute() {
  37.         Pose2d currentPose = getLimelightPose();
  38.         Pose2d targetPose = targetPoseSupplier.get();
  39.  
  40.         Translation2d error = targetPose.getTranslation().minus(currentPose.getTranslation());
  41.         double errorTheta = targetPose.getRotation().minus(currentPose.getRotation()).getDegrees();
  42.  
  43.         double vx = kP_xy * error.getX();
  44.         double vy = kP_xy * error.getY();
  45.         double omega = kP_theta * Math.toRadians(errorTheta);
  46.  
  47.         drivetrain.setControl(
  48.             robotCentric
  49.                 .withVelocityX(vx)
  50.                 .withVelocityY(vy)
  51.                 .withRotationalRate(omega)
  52.         );
  53.     }
  54.  
  55.     @Override
  56.     public boolean isFinished() {
  57.         Pose2d currentPose = getLimelightPose();
  58.         Pose2d targetPose = targetPoseSupplier.get();
  59.  
  60.         boolean posClose = currentPose.getTranslation().getDistance(targetPose.getTranslation()) < toleranceMeters;
  61.         boolean angleClose = Math.abs(currentPose.getRotation().minus(targetPose.getRotation()).getDegrees()) < toleranceDegrees;
  62.  
  63.         return posClose && angleClose;
  64.     }
  65.  
  66.     @Override
  67.     public void end(boolean interrupted) {
  68.         drivetrain.setControl(
  69.             robotCentric
  70.                 .withVelocityX(0)
  71.                 .withVelocityY(0)
  72.                 .withRotationalRate(0)
  73.         );
  74.     }
  75.  
  76.     private Pose2d getLimelightPose() {
  77.         double[] botpose = LimelightHelpers.getBotPose("limelight");
  78.         double x = botpose[0];
  79.         double y = botpose[1];
  80.         double yaw = botpose[5]; // degrees
  81.  
  82.         return new Pose2d(x, y, Rotation2d.fromDegrees(yaw));
  83.     }
  84. }
  85.  
  86.  
  87.  
Advertisement
Add Comment
Please, Sign In to add comment