Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added RotateWithPID.java #19

Merged
merged 16 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/main/java/frc/robot/commands/RotateWithPID.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package frc.robot.commands;

import com.spikes2212.control.FeedForwardController;
import com.spikes2212.control.FeedForwardSettings;
import com.spikes2212.control.PIDSettings;
import com.spikes2212.dashboard.RootNamespace;
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj2.command.Command;
import frc.robot.subsystems.Drivetrain;

import java.util.function.Supplier;

public class RotateWithPID extends Command {

private static final RootNamespace namespace = new RootNamespace("rotate with pid");
private static final PIDSettings pidSettings = namespace.addPIDNamespace("rotate with pid");
private static final FeedForwardSettings feedForwardSettings = namespace.addFeedForwardNamespace("rotate with pid",
FeedForwardSettings.EMPTY_FF_SETTINGS);

private final Drivetrain drivetrain;
private final Supplier<Double> setpoint;
private final PIDController pidController;
private final FeedForwardController feedForwardController;

private double lastTimeNotOnTarget = 0;

public RotateWithPID(Drivetrain drivetrain, Supplier<Double> setpoint) {
this.drivetrain = drivetrain;
this.setpoint = setpoint;
pidController = new PIDController(pidSettings.getkP(), pidSettings.getkI(), pidSettings.getkD());
pidController.setTolerance(pidSettings.getTolerance());
feedForwardController = new FeedForwardController(feedForwardSettings);
}

@Override
public void execute() {
pidController.setPID(pidSettings.getkP(), pidSettings.getkI(), pidSettings.getkD());
pidController.setTolerance(pidSettings.getTolerance());
feedForwardController.setGains(feedForwardSettings);
drivetrain.drive(0, 0, pidController.calculate(drivetrain.getYaw(), setpoint.get()) +
feedForwardController.calculate(drivetrain.getYaw(), setpoint.get()),
false, false);
}

@Override
public boolean isFinished() {
if (!pidController.atSetpoint()) {
lastTimeNotOnTarget = Timer.getFPGATimestamp();
}
return pidSettings.getWaitTime() <= Timer.getFPGATimestamp() - lastTimeNotOnTarget;
}
}
4 changes: 4 additions & 0 deletions src/main/java/frc/robot/subsystems/Drivetrain.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ public void resetGyro() {
gyro.reset();
}

public double getYaw() {
return gyro.getAngle();
}

@Override
public void configureDashboard() {
namespace.putNumber("gyro yaw", gyro::getAngle);
Expand Down