Guest User

drive to pose command help 2

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