Files
2023WayOfTheRobot/src/main/java/frc4388/robot/commands/PelvicInflammatoryDisease.java
T

61 lines
1.6 KiB
Java
Raw Normal View History

2023-01-14 10:37:09 -07:00
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package frc4388.robot.commands;
import edu.wpi.first.wpilibj2.command.CommandBase;
import frc4388.utility.Gains;
2023-01-15 20:25:07 -07:00
public abstract class PelvicInflammatoryDisease extends CommandBase {
2023-03-14 10:47:17 -06:00
protected Gains gains;
private double output = 0;
private double tolerance = 0;
2023-01-14 15:33:12 -07:00
2023-02-02 20:54:12 -07:00
/** Creates a new PelvicInflammatoryDisease. */
2023-03-14 10:47:17 -06:00
public PelvicInflammatoryDisease(double kp, double ki, double kd, double kf, double tolerance) {
gains = new Gains(kp, ki, kd, kf, 0);
this.tolerance = tolerance;
2023-01-14 15:33:12 -07:00
}
2023-03-14 10:47:17 -06:00
public PelvicInflammatoryDisease(Gains gains, double tolerance) {
this.gains = gains;
this.tolerance = tolerance;
2023-01-14 15:33:12 -07:00
}
/** produces the error from the setpoint */
public abstract double getError();
2023-03-15 15:25:06 -06:00
2023-01-14 15:33:12 -07:00
/** figure it out bitch */
public abstract void runWithOutput(double output);
2023-01-15 15:17:54 -07:00
// Called when the command is initially scheduled.
@Override
2023-03-15 15:25:06 -06:00
public final void initialize() {
2023-01-15 15:17:54 -07:00
output = 0;
}
2023-01-14 15:33:12 -07:00
2023-01-15 15:17:54 -07:00
private double prevError, cumError = 0;
// Called every time the scheduler runs while the command is scheduled.
@Override
2023-03-15 15:25:06 -06:00
public final void execute() {
2023-01-15 15:17:54 -07:00
double error = getError();
cumError += error * .02; // 20 ms
double delta = error - prevError;
2023-01-14 15:33:12 -07:00
2023-01-15 15:17:54 -07:00
output = error * gains.kP;
output += cumError * gains.kI;
output += delta * gains.kD;
output += gains.kF;
2023-01-14 15:33:12 -07:00
runWithOutput(output);
}
// Returns true when the command should end.
2023-01-15 15:17:54 -07:00
@Override
2023-03-15 15:25:06 -06:00
public final boolean isFinished() {
2023-03-14 10:47:17 -06:00
return Math.abs(getError()) < tolerance;
}
2023-01-14 10:37:09 -07:00
}