Skip to content
This repository was archived by the owner on Jan 17, 2026. It is now read-only.

Commit 173b6ba

Browse files
committed
[drive] Speedup auto align
Signed-off-by: Jade Turner <spacey-sooty@proton.me>
1 parent 7d3b557 commit 173b6ba

File tree

5 files changed

+46
-34
lines changed

5 files changed

+46
-34
lines changed

.wpilib/wpilib_preferences.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"enableCppIntellisense": false,
33
"currentLanguage": "java",
4-
"projectYear": "2025beta",
4+
"projectYear": "2025",
55
"teamNumber": 4788
66
}

src/main/java/org/curtinfrc/frc2025/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* (log replay from a file).
1616
*/
1717
public final class Constants {
18-
public static final RobotType robotType = RobotType.COMPBOT;
18+
public static final RobotType robotType = RobotType.SIMBOT;
1919
public static final double ROBOT_X = 0.705;
2020
public static final double ROBOT_Y = 0.730;
2121
public static final double FIELD_LENGTH = aprilTagLayout.getFieldLength();

src/main/java/org/curtinfrc/frc2025/Robot.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ public Robot() {
299299

300300
autoChooser.addCmd("Test Auto", this::testAuto);
301301

302+
autoChooser.addCmd("Align to A", () -> drive.autoAlign(A::getPose));
303+
302304
// Set up SysId routines
303305
autoChooser.addCmd(
304306
"Drive Wheel Radius Characterization", () -> drive.wheelRadiusCharacterization());

src/main/java/org/curtinfrc/frc2025/subsystems/drive/Drive.java

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import edu.wpi.first.math.numbers.N3;
3030
import edu.wpi.first.math.system.plant.DCMotor;
3131
import edu.wpi.first.math.trajectory.TrapezoidProfile;
32+
import edu.wpi.first.math.trajectory.TrapezoidProfile.State;
3233
import edu.wpi.first.math.util.Units;
3334
import edu.wpi.first.wpilibj.Alert;
3435
import edu.wpi.first.wpilibj.Alert.AlertType;
@@ -77,33 +78,34 @@ public class Drive extends SubsystemBase {
7778
private SwerveDrivePoseEstimator poseEstimator =
7879
new SwerveDrivePoseEstimator(kinematics, rawGyroRotation, lastModulePositions, Pose2d.kZero);
7980

80-
private final PIDController xController = new PIDController(3.5, 0, 0);
81-
private final PIDController yController = new PIDController(3.5, 0, 0);
82-
private final PIDController headingController = new PIDController(3.5, 0, 0);
81+
private final TrapezoidProfile magnitudeProfile =
82+
new TrapezoidProfile(
83+
new TrapezoidProfile.Constraints(
84+
getMaxLinearSpeedMetersPerSec(), MAX_LINEAR_ACCELERATION));
85+
private final TrapezoidProfile headingController =
86+
new TrapezoidProfile(
87+
new TrapezoidProfile.Constraints(getMaxAngularSpeedRadPerSec(), ANGLE_MAX_ACCELERATION));
8388

8489
private final PIDController xFollower = new PIDController(1, 0, 0);
8590
private final PIDController yFollower = new PIDController(1, 0, 0);
8691
private final PIDController headingFollower = new PIDController(1.5, 0, 0);
8792

8893
@AutoLogOutput(key = "Drive/Setpoint")
89-
public DriveSetpoints setpoint = DriveSetpoints.A;
94+
public Pose2d setpoint = Pose2d.kZero;
9095

9196
@AutoLogOutput(key = "Drive/AtSetpoint")
9297
public Trigger atSetpoint =
9398
new Trigger(
94-
() ->
95-
xController.atSetpoint()
96-
&& yController.atSetpoint()
97-
&& headingController.atSetpoint());
98-
99-
@AutoLogOutput(key = "Drive/AlmostAtSetpoint")
100-
public Trigger almostAtSetpoint =
101-
new Trigger(
102-
() -> {
103-
return getPose().minus(setpoint.getPose()).getTranslation().getNorm() < 1;
104-
});
105-
106-
private final RepulsorFieldPlanner repulsorFieldPlanner = new RepulsorFieldPlanner();
99+
() -> {
100+
var error = setpoint.minus(getPose());
101+
Logger.recordOutput("Drive/error", error);
102+
var translationOk = Math.abs(error.getTranslation().getNorm()) < 0.03;
103+
var rotationOk = Math.abs(error.getRotation().getRadians()) < 0.05;
104+
Logger.recordOutput("Drive/translationOK", translationOk);
105+
Logger.recordOutput("Drive/rotationOK", rotationOk);
106+
return translationOk && rotationOk;
107+
})
108+
.debounce(0);
107109

108110
public Drive(
109111
GyroIO gyroIO,
@@ -145,11 +147,6 @@ public Drive(
145147
new SysIdRoutine.Mechanism(
146148
(voltage) -> runSteerCharacterization(voltage.in(Volts)), null, this));
147149

148-
xController.setTolerance(0.02);
149-
yController.setTolerance(0.02);
150-
headingController.setTolerance(0.02);
151-
headingController.enableContinuousInput(-Math.PI, Math.PI);
152-
153150
headingFollower.enableContinuousInput(-Math.PI, Math.PI);
154151
}
155152

@@ -242,10 +239,6 @@ public void periodic() {
242239

243240
// Update gyro alert
244241
gyroDisconnectedAlert.set(!gyroInputs.connected && Constants.getMode() != Mode.SIM);
245-
246-
Logger.recordOutput("xPidAtSetpoint", xController.atSetpoint());
247-
Logger.recordOutput("yPidAtSetpoint", yController.atSetpoint());
248-
Logger.recordOutput("omegaPidAtSetpoint", headingController.atSetpoint());
249242
}
250243

251244
/**
@@ -664,7 +657,6 @@ public Command autoAlignWithOverride(
664657
DoubleSupplier omegaSupplier) {
665658
return run(
666659
() -> {
667-
this.setpoint = _setpoint.get();
668660
if (Math.abs(xSupplier.getAsDouble()) > 0.05
669661
|| Math.abs(ySupplier.getAsDouble()) > 0.05
670662
|| Math.abs(omegaSupplier.getAsDouble()) > 0.05) {
@@ -676,16 +668,29 @@ public Command autoAlignWithOverride(
676668
}
677669

678670
private void autoAlign(Pose2d _setpoint) {
679-
Logger.recordOutput("Drive/AutoAlignSetpoint", _setpoint);
671+
setpoint = _setpoint;
680672
var robotPose = getPose();
673+
var robotSpeed = getChassisSpeeds();
674+
var robotSpeedV = new Translation2d(robotSpeed.vxMetersPerSecond, robotSpeed.vyMetersPerSecond);
675+
676+
var mag =
677+
magnitudeProfile.calculate(
678+
0.02,
679+
new State(robotPose.getTranslation().getNorm(), robotSpeedV.getNorm()),
680+
new State(setpoint.getTranslation().getNorm(), 0));
681+
var dir = setpoint.getTranslation().minus(robotPose.getTranslation()).getAngle();
682+
var targetSpeed = new Translation2d(Math.abs(mag.velocity), dir);
681683

682684
var omega =
683685
headingController.calculate(
684-
getRotation().getRadians(), _setpoint.getRotation().getRadians());
685-
var x = xController.calculate(robotPose.getX(), _setpoint.getX());
686-
var y = yController.calculate(robotPose.getY(), _setpoint.getY());
686+
0.02,
687+
new State(getRotation().getRadians(), robotSpeed.omegaRadiansPerSecond),
688+
new State(setpoint.getRotation().getRadians(), 0));
689+
687690
runVelocity(
688-
ChassisSpeeds.fromFieldRelativeSpeeds(-x, -y, -omega, getRotation()), new double[4]);
691+
ChassisSpeeds.fromFieldRelativeSpeeds(
692+
targetSpeed.getX(), targetSpeed.getY(), omega.velocity, getRotation()),
693+
new double[4]);
689694
}
690695

691696
public Command autoAlign(Supplier<Pose2d> _setpoint) {

src/main/java/org/curtinfrc/frc2025/subsystems/drive/DriveConstants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import edu.wpi.first.math.geometry.Pose3d;
99
import edu.wpi.first.math.geometry.Rotation2d;
1010
import edu.wpi.first.math.geometry.Rotation3d;
11+
import edu.wpi.first.math.system.plant.DCMotor;
1112
import edu.wpi.first.util.struct.Struct;
1213
import edu.wpi.first.util.struct.StructGenerator;
1314
import edu.wpi.first.util.struct.StructSerializable;
@@ -30,6 +31,10 @@ public final class DriveConstants {
3031
public static final double FF_RAMP_RATE = 0.1; // Volts/Sec
3132
public static final double WHEEL_RADIUS_MAX_VELOCITY = 0.25; // Rad/Sec
3233
public static final double WHEEL_RADIUS_RAMP_RATE = 0.05; // Rad/Sec^2
34+
public static final double KT = DCMotor.getKrakenX60Foc(1).KtNMPerAmp * 5.99;
35+
// max acceleration = 4 * torque / r / robotWeight
36+
// torque = kT * statorCurrentLimit * gearRatio
37+
public static final double MAX_LINEAR_ACCELERATION = 4 * KT * 60 / 0.0508 / 38.5554;
3338
public static final double ODOMETRY_FREQUENCY =
3439
new CANBus(CompTunerConstants.DrivetrainConstants.CANBusName).isNetworkFD() ? 250.0 : 100.0;
3540
public static final double DRIVE_BASE_RADIUS =

0 commit comments

Comments
 (0)