|
| 1 | +package frc.robot.commands; |
| 2 | + |
| 3 | +import com.spikes2212.control.FeedForwardController; |
| 4 | +import com.spikes2212.control.FeedForwardSettings; |
| 5 | +import com.spikes2212.control.PIDSettings; |
| 6 | +import com.spikes2212.dashboard.RootNamespace; |
| 7 | +import edu.wpi.first.math.controller.PIDController; |
| 8 | +import edu.wpi.first.wpilibj.Timer; |
| 9 | +import edu.wpi.first.wpilibj2.command.Command; |
| 10 | +import frc.robot.subsystems.Drivetrain; |
| 11 | + |
| 12 | +public class CenterOnFeeder extends Command { |
| 13 | + |
| 14 | + public enum PossiblePose { |
| 15 | + |
| 16 | + FAR_LEFT(0, 0), MIDDLE_LEFT(0, 0), CLOSE_LEFT(0, 0), |
| 17 | + CENTER(0, 0), CLOSE_RIGHT(0, 0), MIDDLE_RIGHT(0, 0), FAR_RIGHT(0, 0); |
| 18 | + |
| 19 | + public final double xPose; |
| 20 | + public final double yPose; |
| 21 | + |
| 22 | + PossiblePose(double xPose, double yPose) { |
| 23 | + this.xPose = xPose; |
| 24 | + this.yPose = yPose; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + private static final RootNamespace namespace = new RootNamespace("center on feeder"); |
| 29 | + private static final PIDSettings pidSettings = namespace.addPIDNamespace("center on feeder"); |
| 30 | + private static final FeedForwardSettings feedForwardSettings = |
| 31 | + namespace.addFeedForwardNamespace("center on feeder", |
| 32 | + FeedForwardController.ControlMode.LINEAR_POSITION); |
| 33 | + |
| 34 | + private final Drivetrain drivetrain; |
| 35 | + |
| 36 | + private final PIDController xPIDController; |
| 37 | + private final FeedForwardController xFeedForwardController; |
| 38 | + private final PIDController yPIDController; |
| 39 | + private final FeedForwardController yFeedForwardController; |
| 40 | + |
| 41 | + private double lastTimeNotOnTarget; |
| 42 | + |
| 43 | + public CenterOnFeeder(Drivetrain drivetrain) { |
| 44 | + addRequirements(drivetrain); |
| 45 | + this.drivetrain = drivetrain; |
| 46 | + xPIDController = new PIDController(pidSettings.getkP(), pidSettings.getkI(), pidSettings.getkD()); |
| 47 | + xPIDController.setTolerance(pidSettings.getTolerance()); |
| 48 | + xFeedForwardController = new FeedForwardController(feedForwardSettings); |
| 49 | + yPIDController = new PIDController(pidSettings.getkP(), pidSettings.getkI(), pidSettings.getkD()); |
| 50 | + yPIDController.setTolerance(pidSettings.getTolerance()); |
| 51 | + yFeedForwardController = new FeedForwardController(feedForwardSettings); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void execute() { |
| 56 | + xPIDController.setPID(pidSettings.getkP(), pidSettings.getkI(), pidSettings.getkD()); |
| 57 | + xPIDController.setTolerance(pidSettings.getTolerance()); |
| 58 | + xFeedForwardController.setGains(feedForwardSettings); |
| 59 | + yPIDController.setPID(pidSettings.getkP(), pidSettings.getkI(), pidSettings.getkD()); |
| 60 | + yPIDController.setTolerance(pidSettings.getTolerance()); |
| 61 | + yFeedForwardController.setGains(feedForwardSettings); |
| 62 | + double shortestDistance = getDistanceFrom(PossiblePose.FAR_LEFT.xPose, PossiblePose.FAR_LEFT.yPose); |
| 63 | + PossiblePose pose = PossiblePose.FAR_LEFT; |
| 64 | + for (PossiblePose possiblePose : PossiblePose.values()) { |
| 65 | + if (shortestDistance > getDistanceFrom(possiblePose.xPose, possiblePose.yPose)) { |
| 66 | + shortestDistance = getDistanceFrom(possiblePose.xPose, possiblePose.yPose); |
| 67 | + pose = possiblePose; |
| 68 | + } |
| 69 | + } |
| 70 | + drivetrain.drive(xPIDController.calculate(drivetrain.getPose2d().getX(), pose.xPose) + |
| 71 | + xFeedForwardController.calculate(drivetrain.getPose2d().getX(), pose.xPose), |
| 72 | + yPIDController.calculate(drivetrain.getPose2d().getY(), pose.yPose) + |
| 73 | + yFeedForwardController.calculate(drivetrain.getPose2d().getY(), pose.yPose), |
| 74 | + 0, true, true, 0.02); |
| 75 | + } |
| 76 | + |
| 77 | + private double getDistanceFrom(double x, double y) { |
| 78 | + return Math.sqrt(Math.pow(x - drivetrain.getPose2d().getX(), 2) + Math.pow(y - drivetrain.getPose2d().getY(), 2)); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public boolean isFinished() { |
| 83 | + if (!xPIDController.atSetpoint() || !yPIDController.atSetpoint()) { |
| 84 | + lastTimeNotOnTarget = Timer.getFPGATimestamp(); |
| 85 | + } |
| 86 | + return pidSettings.getWaitTime() <= lastTimeNotOnTarget - Timer.getFPGATimestamp(); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void end(boolean interrupted) { |
| 91 | + drivetrain.stop(); |
| 92 | + } |
| 93 | +} |
0 commit comments