001/************************ PROJECT ZIXI ************************/
002/* Copyright (c) 2026 StuyPulse Robotics. All rights reserved.*/
003/* This work is licensed under the terms of the MIT license.  */
004/**************************************************************/
005package com.stuypulse.robot.opmodes.sysid;
006
007import static org.wpilib.units.Units.Volts;
008
009import com.stuypulse.robot.subsystems.swerve.Swerve;
010import com.stuypulse.robot.subsystems.swerve.SwerveConstants.SwerveCharacterization.DriveSysId;
011
012import org.wpilib.command3.Command;
013import org.wpilib.command3.Scheduler;
014import org.wpilib.command3.sysid.SysIdRoutine;
015import org.wpilib.command3.sysid.SysIdRoutine.Direction;
016import org.wpilib.opmode.OpMode;
017
018import java.util.function.BiFunction;
019import org.littletonrobotics.junction.Logger;
020
021public class SwerveDriveSysId implements OpMode {
022    private final Command routine;
023
024    public SwerveDriveSysId(
025            Swerve swerve,
026            Direction direction,
027            BiFunction<SysIdRoutine, Direction, Command> sysIdFunction) {
028        SysIdRoutine.Config config =
029                new SysIdRoutine.Config(
030                        DriveSysId.RAMP_RATE,
031                        DriveSysId.STEP_VOLTAGE,
032                        DriveSysId.QUASISTATIC_TIMEOUT,
033                        state -> Logger.recordOutput("SysId/SwerveDrive/State", state.toString()));
034
035        SysIdRoutine.SysIdMechanism mechanism =
036                new SysIdRoutine.SysIdMechanism(
037                        volts -> swerve.runCharacterization(volts.in(Volts)),
038                        null,
039                        swerve,
040                        "SwerveDrive");
041        SysIdRoutine sysId = new SysIdRoutine(config, mechanism);
042
043        routine = sysIdFunction.apply(sysId, direction);
044    }
045
046    @Override
047    public void start() {
048        Scheduler.getDefault().schedule(routine);
049    }
050
051    @Override
052    public void end() {
053        Scheduler.getDefault().cancel(routine);
054    }
055}