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.Robot;
010import com.stuypulse.robot.subsystems.swerve.Swerve;
011import com.stuypulse.robot.subsystems.swerve.SwerveConstants.SwerveCharacterization;
012import com.stuypulse.robot.subsystems.swerve.SwerveConstants.SwerveCharacterization.SteerSysId;
013
014import org.wpilib.command3.Command;
015import org.wpilib.command3.Scheduler;
016import org.wpilib.command3.sysid.SysIdRoutine;
017import org.wpilib.command3.sysid.SysIdRoutine.Config;
018import org.wpilib.command3.sysid.SysIdRoutine.Direction;
019import org.wpilib.command3.sysid.SysIdRoutine.SysIdMechanism;
020import org.wpilib.opmode.OpMode;
021import org.wpilib.opmode.Utility;
022import org.wpilib.units.measure.Time;
023
024import org.littletonrobotics.junction.Logger;
025
026@Utility(
027        name = "Swerve Steer",
028        group = "SysId",
029        description = "Runs only the steer motors. Put the robot on blocks.")
030public class SwerveSteerSysId implements OpMode {
031    private final Swerve swerve;
032    private final Command routine;
033
034    public SwerveSteerSysId(Robot robot) {
035        swerve = robot.swerve;
036
037        Config config =
038                new SysIdRoutine.Config(
039                        SteerSysId.RAMP_RATE,
040                        SteerSysId.STEP_VOLTAGE,
041                        SteerSysId.QUASISTATIC_TIMEOUT,
042                        state -> Logger.recordOutput("SysId/SwerveSteer/State", state.toString()));
043
044        SysIdMechanism mechanism =
045                new SysIdRoutine.SysIdMechanism(
046                        volts -> swerve.runSteerCharacterization(volts.in(Volts)),
047                        log ->
048                                Logger.recordOutput(
049                                        "SysId/SwerveSteer/PositionRad",
050                                        swerve.getSteerCharacterizationPositionRad()),
051                        swerve,
052                        "SwerveSteer");
053
054        SysIdRoutine sysId = new SysIdRoutine(config, mechanism);
055        Time dynamicTimeout = SteerSysId.DYNAMIC_TIMEOUT;
056
057        routine =
058                Command.sequence(
059                                rest(),
060                                sysId.quasistatic(Direction.FORWARD),
061                                rest(),
062                                sysId.quasistatic(Direction.REVERSE),
063                                rest(),
064                                sysId.dynamic(Direction.FORWARD).withTimeout(dynamicTimeout),
065                                rest(),
066                                sysId.dynamic(Direction.REVERSE).withTimeout(dynamicTimeout))
067                        .named("Swerve Steer SysId");
068    }
069
070    private Command rest() {
071        return swerve.run(
072                        coroutine -> {
073                            swerve.runSteerCharacterization(0.0);
074                            coroutine.wait(SwerveCharacterization.SYSID_REST);
075                        })
076                .named("Swerve Steer SysId Rest");
077    }
078
079    @Override
080    public void start() {
081        Scheduler.getDefault().schedule(routine);
082    }
083
084    @Override
085    public void end() {
086        Scheduler.getDefault().cancel(routine);
087    }
088}