diff --git a/src/main/java/frc/team3128/commands/CmdAlign.java b/src/main/java/frc/team3128/commands/CmdAlign.java index 698e99fc..67efe5bc 100644 --- a/src/main/java/frc/team3128/commands/CmdAlign.java +++ b/src/main/java/frc/team3128/commands/CmdAlign.java @@ -1,6 +1,7 @@ package frc.team3128.commands; import edu.wpi.first.math.MathUtil; +import edu.wpi.first.math.controller.PIDController; import edu.wpi.first.wpilibj.RobotController; import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; import edu.wpi.first.wpilibj2.command.CommandBase; @@ -25,6 +26,7 @@ private enum VisionState { private boolean isAligned; private VisionState aimState = VisionState.SEARCHING; + private PIDController controller; /** * Aligns the robot to the hub using the limelight @@ -45,6 +47,7 @@ public void initialize() { // limelights.turnShooterLEDOn(); prevTime = RobotController.getFPGATime() / 1e6; plateauCount = 0; + controller = new PIDController(VISION_PID_kP, VISION_PID_kI, VISION_PID_kD); } @Override @@ -62,6 +65,7 @@ public void execute() { currHorizontalOffset = limelights.getShooterTX(); prevError = goalHorizontalOffset - currHorizontalOffset; aimState = VisionState.FEEDBACK; + controller.enableContinuousInput(-180, 180); } break; @@ -70,16 +74,17 @@ public void execute() { if(!limelights.getShooterHasValidTarget()) { aimState = VisionState.SEARCHING; plateauCount = 0; + controller.disableContinuousInput(); break; } // turn with PID loop using input as horizontal tx error to target currHorizontalOffset = limelights.getShooterTX(); currError = goalHorizontalOffset - currHorizontalOffset; // currError is positive if we are too far left - - double ff = Math.signum(currError) * VISION_PID_kF; - double feedbackPower = VISION_PID_kP * currError + VISION_PID_kD * (currError - prevError) / (currTime - prevTime) + ff; + double ff = Math.signum(currError) * VISION_PID_kF; + double feedbackPower = controller.calculate(currError) + ff; + feedbackPower = MathUtil.clamp(feedbackPower, -1, 1); drive.tankDrive(-feedbackPower, feedbackPower);