|
| 1 | +// Copyright (c) FIRST and other WPILib contributors. |
| 2 | +// Open Source Software; you can modify and/or share it under the terms of |
| 3 | +// the WPILib BSD license file in the root directory of this project. |
| 4 | + |
| 5 | +package frc.robot.commands.autodrive; |
| 6 | + |
| 7 | +import edu.wpi.first.math.MathUtil; |
| 8 | +import edu.wpi.first.math.controller.ProfiledPIDController; |
| 9 | +import edu.wpi.first.math.geometry.Pose2d; |
| 10 | +import edu.wpi.first.math.kinematics.ChassisSpeeds; |
| 11 | +import frc.robot.commands.drive.DriveCommandBase; |
| 12 | +import frc.robot.subsystems.swerve.SwerveConstants; |
| 13 | +import frc.robot.subsystems.swerve.SwerveConstants.TrajectoryConstants; |
| 14 | +import frc.robot.subsystems.swerve.SwerveDrive; |
| 15 | +import frc.robot.subsystems.vision.VisionSubsystem; |
| 16 | + |
| 17 | +/* Auto Align takes in Pose2d and moves robot to it */ |
| 18 | +public class AutoAlign extends DriveCommandBase { |
| 19 | + |
| 20 | + private final SwerveDrive swerveDrive; |
| 21 | + private final VisionSubsystem visionSubsystem; |
| 22 | + |
| 23 | + private Pose2d targetPose; |
| 24 | + |
| 25 | + private final ProfiledPIDController rotationController = |
| 26 | + new ProfiledPIDController( |
| 27 | + SwerveConstants.TrajectoryConstants.AUTO_LINEUP_ROTATION_P, |
| 28 | + SwerveConstants.TrajectoryConstants.AUTO_LINEUP_ROTATION_I, |
| 29 | + SwerveConstants.TrajectoryConstants.AUTO_LINEUP_ROTATION_D, |
| 30 | + SwerveConstants.TrajectoryConstants.AUTO_LINEUP_ROTATION_CONSTRAINTS); |
| 31 | + |
| 32 | + private final ProfiledPIDController xTranslationController = |
| 33 | + new ProfiledPIDController( |
| 34 | + SwerveConstants.TrajectoryConstants.AUTO_LINEUP_TRANSLATION_P, |
| 35 | + SwerveConstants.TrajectoryConstants.AUTO_LINEUP_TRANSLATION_I, |
| 36 | + SwerveConstants.TrajectoryConstants.AUTO_LINEUP_TRANSLATION_D, |
| 37 | + SwerveConstants.TrajectoryConstants.AUTO_LINEUP_TRANSLATION_CONSTRAINTS); |
| 38 | + |
| 39 | + private final ProfiledPIDController yTranslationController = |
| 40 | + new ProfiledPIDController( |
| 41 | + SwerveConstants.TrajectoryConstants.AUTO_LINEUP_TRANSLATION_P, |
| 42 | + SwerveConstants.TrajectoryConstants.AUTO_LINEUP_TRANSLATION_I, |
| 43 | + SwerveConstants.TrajectoryConstants.AUTO_LINEUP_TRANSLATION_D, |
| 44 | + SwerveConstants.TrajectoryConstants.AUTO_LINEUP_TRANSLATION_CONSTRAINTS); |
| 45 | + |
| 46 | + /** |
| 47 | + * Creates a new AutoAlign. |
| 48 | + * |
| 49 | + * @param visionSubsystem The subsystem for vision |
| 50 | + * @param swerveDrive The subsystem for the swerve drive |
| 51 | + * @param targetPose The target pose for the robot to align to |
| 52 | + */ |
| 53 | + public AutoAlign(SwerveDrive swerveDrive, VisionSubsystem visionSubsystem, Pose2d targetPose) { |
| 54 | + super(swerveDrive, visionSubsystem); |
| 55 | + this.targetPose = targetPose; |
| 56 | + this.swerveDrive = swerveDrive; |
| 57 | + this.visionSubsystem = visionSubsystem; |
| 58 | + // Use addRequirements() here to declare subsystem dependencies. |
| 59 | + addRequirements(swerveDrive, visionSubsystem); |
| 60 | + // Enables continuous input for the rotation controller |
| 61 | + rotationController.enableContinuousInput(-Math.PI, Math.PI); |
| 62 | + } |
| 63 | + |
| 64 | + // Called every time the scheduler runs while the command is scheduled. |
| 65 | + @Override |
| 66 | + public void execute() { |
| 67 | + super.execute(); |
| 68 | + // Gets the error between the desired pos (the target) and the current pos of the robot |
| 69 | + Pose2d drivePose = swerveDrive.getEstimatedPose(); |
| 70 | + double xPoseError = targetPose.getX() - drivePose.getX(); |
| 71 | + double yPoseError = targetPose.getY() - drivePose.getY(); |
| 72 | + double thetaPoseError = |
| 73 | + targetPose.getRotation().getRadians() - drivePose.getRotation().getRadians(); |
| 74 | + |
| 75 | + // Uses the PID controllers to calculate the drive output |
| 76 | + double xOutput = |
| 77 | + MathUtil.applyDeadband( |
| 78 | + xTranslationController.calculate(xPoseError, 0), TrajectoryConstants.DEADBAND_AMOUNT); |
| 79 | + double yOutput = |
| 80 | + MathUtil.applyDeadband( |
| 81 | + yTranslationController.calculate(yPoseError, 0), TrajectoryConstants.DEADBAND_AMOUNT); |
| 82 | + double turnOutput = |
| 83 | + MathUtil.applyDeadband( |
| 84 | + rotationController.calculate(thetaPoseError, 0), TrajectoryConstants.DEADBAND_AMOUNT); |
| 85 | + |
| 86 | + // Gets the chassis speeds for the robot using the odometry rotation (not alliance relative) |
| 87 | + ChassisSpeeds chassisSpeeds = |
| 88 | + ChassisSpeeds.fromFieldRelativeSpeeds( |
| 89 | + xOutput, yOutput, turnOutput, swerveDrive.getOdometryRotation2d()); |
| 90 | + |
| 91 | + // Drives the robot towards the target pose |
| 92 | + swerveDrive.drive( |
| 93 | + chassisSpeeds.vxMetersPerSecond, |
| 94 | + chassisSpeeds.vyMetersPerSecond, |
| 95 | + chassisSpeeds.omegaRadiansPerSecond, |
| 96 | + false); |
| 97 | + } |
| 98 | + |
| 99 | + // Called once the command ends or is interrupted. |
| 100 | + @Override |
| 101 | + public void end(boolean interrupted) { |
| 102 | + swerveDrive.drive(0, 0, 0, false); |
| 103 | + } |
| 104 | + |
| 105 | + // Returns true when the command should end. |
| 106 | + @Override |
| 107 | + public boolean isFinished() { |
| 108 | + return false; |
| 109 | + } |
| 110 | +} |
0 commit comments