Class Mechanism

java.lang.Object
frc.spectrumLib.mechanism.Mechanism
All Implemented Interfaces:
Subsystem
Direct Known Subclasses:
FuelIntake, Hood, IndexerBed, IndexerTower, IntakeExtension, IntakeExtension.IntakeExtensionRight, Launcher

public abstract class Mechanism extends Object implements Subsystem
Base class for a CTRE TalonFX-driven mechanism: motor creation, cached sensor reads, control mode helpers, threshold triggers, and periodic current reporting.

With Mechanism.Config.isAttached() false, the mechanism builds no hardware, commands nothing, and every sensor read returns 0.

target and velocityTarget hold the last setpoint this class sent to the motor, which is what the at-target triggers compare against.

Everything here is in CTRE Phoenix 6 units, so rotations and rotations per second rather than degrees, and the mechanism's gearing is Config.talonConfig.Feedback.SensorToMechanismRatio. A subclass supplies a Mechanism.Config holding its motor IDs, Talon settings, followers, and rotation bounds.

  • Field Details

    • motor

      protected TalonFX motor
      Leader TalonFX, the motor this mechanism commands.
    • followerMotors

      protected TalonFX[] followerMotors
      Follower TalonFXs, empty when the mechanism runs on the leader alone.
    • config

      public Mechanism.Config config
      Motor IDs, Talon settings, and mechanism parameters.
  • Constructor Details

    • Mechanism

      protected Mechanism(Mechanism.Config config)
      Builds the leader TalonFX and any followers if Mechanism.Config.isAttached() is true. Sensor caches exist either way, so an unattached mechanism reads 0.
    • Mechanism

      protected Mechanism(Mechanism.Config config, boolean attached)
      Overrides the attached flag in the config before the hardware is built.
      Parameters:
      attached - false to run the mechanism in software-only mode
  • Method Details

    • periodic

      public void periodic()
      Called once per scheduler loop. Subclasses override this for their state machine, telemetry and sensor work.
      Specified by:
      periodic in interface Subsystem
    • simulationPeriodic

      public void simulationPeriodic()
      Called once per simulation loop, for physics model inputs.
      Specified by:
      simulationPeriodic in interface Subsystem
    • getName

      public String getName()
      The mechanism's name, as its Mechanism.Config spells it.
      Specified by:
      getName in interface Subsystem
    • isAttached

      public boolean isAttached()
      True when the mechanism has hardware and should send motor commands.
    • isMotorConnected

      public boolean isMotorConnected()
      True when the leader is attached and its status frames are arriving over CAN. A mechanism that is commanded but reports 0 V with this false has dropped off the bus, which is what the hood did intermittently on the 2026-09-04 bench.
    • logBatteryUsage

      public void logBatteryUsage()
      Reports the leader's and followers' combined supply current to the battery logger, and checks each follower for life. Does nothing if the mechanism is not attached.
    • getCurrentCommandName

      protected String getCurrentCommandName()
      The running command's name, or "none" when nothing is scheduled.
    • runningDefaultCommand

      public Trigger runningDefaultCommand()
    • getTarget

      public double getTarget()
      Last closed-loop position setpoint this class sent, in rotations.
    • getVelocityTargetRPS

      public double getVelocityTargetRPS()
      Last closed-loop velocity setpoint this class sent, in rotations per second.
    • atTargetPosition

      public Trigger atTargetPosition(DoubleSupplier tolerance)
      Trigger true while the motor is within tolerance rotations of getTarget().
    • isAtTargetPosition

      public boolean isAtTargetPosition(DoubleSupplier tolerance)
      True while the motor is within tolerance rotations of getTarget().
    • atRotations

      public Trigger atRotations(DoubleSupplier target, DoubleSupplier tolerance)
      Trigger true while the position is within tolerance rotations of target.
    • isAtRotations

      public boolean isAtRotations(DoubleSupplier target, DoubleSupplier tolerance)
      True while the position is within tolerance rotations of target.
    • belowRotations

      public Trigger belowRotations(DoubleSupplier target, DoubleSupplier tolerance)
      Trigger true while the position is below target + tolerance, in rotations.
    • aboveRotations

      public Trigger aboveRotations(DoubleSupplier target, DoubleSupplier tolerance)
      Trigger true while the position is above target - tolerance, in rotations.
    • atPercentage

      public Trigger atPercentage(DoubleSupplier target, DoubleSupplier tolerance)
      Trigger true while the position is within tolerance percent of target, both of maxRotations.
    • belowPercentage

      public Trigger belowPercentage(DoubleSupplier target, DoubleSupplier tolerance)
      Trigger true while the position is below target + tolerance, as a percentage of maxRotations.
    • abovePercentage

      public Trigger abovePercentage(DoubleSupplier target, DoubleSupplier tolerance)
      Trigger true while the position is above target - tolerance, as a percentage of maxRotations.
    • atDegrees

      public Trigger atDegrees(DoubleSupplier target, DoubleSupplier tolerance)
      Trigger true while the position is within tolerance degrees of target.
    • belowDegrees

      public Trigger belowDegrees(DoubleSupplier target, DoubleSupplier tolerance)
      Trigger true while the position is below target + tolerance, in degrees.
    • aboveDegrees

      public Trigger aboveDegrees(DoubleSupplier target, DoubleSupplier tolerance)
      Trigger true while the position is above target - tolerance, in degrees.
    • atVelocityRPM

      public Trigger atVelocityRPM(DoubleSupplier target, DoubleSupplier tolerance)
      Trigger true while the velocity is within tolerance RPM of target.
    • belowVelocityRPM

      public Trigger belowVelocityRPM(DoubleSupplier target, DoubleSupplier tolerance)
      Trigger true while the velocity is below target + tolerance RPM.
    • aboveVelocityRPM

      public Trigger aboveVelocityRPM(DoubleSupplier target, DoubleSupplier tolerance)
      Trigger true while the velocity is above target - tolerance RPM.
    • atCurrent

      public Trigger atCurrent(DoubleSupplier target, DoubleSupplier tolerance)
      Trigger true while the stator current is within tolerance amps of target.
    • belowCurrent

      public Trigger belowCurrent(DoubleSupplier target, DoubleSupplier tolerance)
      Trigger true while the stator current is below target + tolerance amps.
    • aboveCurrent

      public Trigger aboveCurrent(DoubleSupplier target, DoubleSupplier tolerance)
      Trigger true while the stator current is above target - tolerance amps.
    • getStatorCurrent

      public double getStatorCurrent()
      Motor stator current, in amps, sampled once per loop.
    • getSupplyCurrent

      public double getSupplyCurrent()
      Motor supply current, in amps, sampled once per loop.
    • getVoltage

      public double getVoltage()
      Applied motor voltage, in volts, sampled once per loop.
    • getTemp

      public double getTemp()
      Motor temperature in Celsius, sampled once per loop.
    • logDiagnostics

      protected void logDiagnostics(String prefix)
      Logs voltage, stator and supply current, temperature and connection under prefix/... at 10 Hz. Subclasses call this from periodic(), and the log keys are built on the first call.
    • logDiagnostics

      protected void logDiagnostics(String prefix, boolean dashboard)
      As logDiagnostics(String), optionally publishing to the dashboard as well.
      Parameters:
      dashboard - true to publish through Telemetry.logDash(java.lang.String, double)
    • logStandard

      protected void logStandard(String prefix, boolean dashboardDiagnostics, Mechanism.RpmLog rpm)
      The per-loop logging every mechanism does: battery use, the current command, logDiagnostics(String, boolean) and RPM.
      Parameters:
      dashboardDiagnostics - whether the diagnostics also go to the dashboard
    • percentToRotations

      public double percentToRotations(DoubleSupplier percent)
      Percentage of Mechanism.Config.getMaxRotations() to an absolute rotation count.
    • rotationsToPercent

      public double rotationsToPercent(DoubleSupplier rotations)
      Absolute rotation count to a percentage of Mechanism.Config.getMaxRotations().
    • degreesToRotations

      public double degreesToRotations(DoubleSupplier degrees)
    • rotationsToDegrees

      public double rotationsToDegrees(DoubleSupplier rotations)
    • getPositionRotations

      public double getPositionRotations()
      Motor position in rotations, sampled once per loop.
    • getLatencyCompensatedPositionRotations

      public double getLatencyCompensatedPositionRotations()
      Motor position in rotations, projected from the position signal's own timestamp to now along the velocity signal. A mechanism moving at speed then reads where it is rather than where it was when the CAN frame left the motor. Returns 0 when unattached.
    • getLatencyCompensatedPositionDegrees

      public double getLatencyCompensatedPositionDegrees()
    • getPositionPercentage

      public double getPositionPercentage()
      Motor position as a percentage of maxRotations, sampled once per loop.
    • getPositionDegrees

      public double getPositionDegrees()
      Motor position in degrees, sampled once per loop.
    • getVelocityRPM

      public double getVelocityRPM()
      Motor velocity in RPM, sampled once per loop.
    • runVelocity

      public Command runVelocity(DoubleSupplier velocityRPM)
      Runs the mechanism at a constant velocity, in closed-loop voltage control.
    • runVelocityTcFocRPM

      public Command runVelocityTcFocRPM(DoubleSupplier velocityRPM)
      As runVelocity(java.util.function.DoubleSupplier), in torque current FOC, which requires Phoenix Pro.
    • runPercentage

      public Command runPercentage(DoubleSupplier percent)
      Runs the mechanism at an open-loop percent output, in [-1, 1].
    • runVoltage

      public Command runVoltage(DoubleSupplier voltage)
      Applies a constant voltage in volts, bypassing closed-loop control.
    • runVoltageNoSoftLimit

      public Command runVoltageNoSoftLimit(DoubleSupplier voltage)
      Applies a constant voltage in volts and ignores the software limit switches, so it can drive the mechanism past its configured travel.
    • runTorqueCurrentFoc

      public Command runTorqueCurrentFoc(DoubleSupplier current)
      Holds a torque current in amps through FOC, which requires Phoenix Pro.
    • moveToRotations

      public Command moveToRotations(DoubleSupplier rotations)
      Moves the mechanism to a position in rotations with Motion Magic, in torque current FOC, which requires Phoenix Pro.
    • moveToPercentage

      public Command moveToPercentage(DoubleSupplier percent)
      As moveToRotations(java.util.function.DoubleSupplier), with the target as a percentage of maxRotations.
    • moveToDegrees

      public Command moveToDegrees(DoubleSupplier degrees)
    • runFocRotations

      public Command runFocRotations(DoubleSupplier rotations)
      Same as moveToRotations(java.util.function.DoubleSupplier), which is the clearer name. Requires Phoenix Pro.
    • runStop

      public Command runStop()
    • coastMode

      public Command coastMode()
      Coasts while the command runs, then returns to brake. Safe to run while the robot is disabled.
    • ensureBrakeMode

      public Command ensureBrakeMode()
      Returns to brake if the mechanism is coasting. Safe to run while the robot is disabled.
    • runCurrentLimits

      protected Command runCurrentLimits(DoubleSupplier supplyLimit, DoubleSupplier statorLimit)
      Applies new supply and stator current limits, in amps, to the mechanism.
    • setCurrentLimits

      protected void setCurrentLimits(DoubleSupplier supplyLimit, DoubleSupplier statorLimit)
    • stop

      public void stop()
      Stops the motor output. Does nothing if the mechanism is not attached.
    • tareMotor

      protected void tareMotor()
      Sets the mechanism's reported position to zero (tares the motor encoder). Does nothing if the mechanism is not attached.
    • setMotorPosition

      protected void setMotorPosition(DoubleSupplier rotations)
      Writes the motor's internal position register, in rotations, without moving the motor.
    • setMMVelocityFOC

      protected void setMMVelocityFOC(DoubleSupplier velocityRPS)
      Velocity in rotations per second, via Motion Magic in torque current FOC (Phoenix Pro).
    • setVelocityTorqueCurrentFOC

      protected void setVelocityTorqueCurrentFOC(DoubleSupplier velocityRPS)
      Velocity in rotations per second, in torque current FOC (requires Phoenix Pro).
    • setVelocityTCFOCrpm

      protected void setVelocityTCFOCrpm(DoubleSupplier velocityRPM)
    • setVelocity

      protected void setVelocity(DoubleSupplier velocityRPS)
      Velocity in rotations per second, in closed-loop voltage control.
    • setVelocityRPM

      protected void setVelocityRPM(DoubleSupplier velocityRPM)
    • setPosition

      protected void setPosition(DoubleSupplier rotations)
      Position control with voltage compensation and no velocity feedforward.
    • setPositionWithVelocity

      protected void setPositionWithVelocity(DoubleSupplier rotations, DoubleSupplier velocityRPS)
      Position control with an explicit velocity feedforward. This is the right primitive for a continuously moving setpoint, such as a turret tracking a target while the robot drives, where a profiled motion would leave steady-state lag.
      Parameters:
      rotations - the target position in mechanism rotations
      velocityRPS - the feedforward in mechanism rotations per second
    • setMMPositionFoc

      protected void setMMPositionFoc(DoubleSupplier rotations)
      Position in rotations, via Motion Magic in torque current FOC (requires Phoenix Pro).
    • setPositionFocWithVelocity

      protected void setPositionFocWithVelocity(DoubleSupplier rotations, DoubleSupplier velocityRPS)
      Parameters:
      rotations - the target position in mechanism rotations
      velocityRPS - the feedforward in mechanism rotations per second
    • setDynMMPositionFoc

      protected void setDynMMPositionFoc(DoubleSupplier rotations, DoubleSupplier velocity, DoubleSupplier acceleration, DoubleSupplier jerk)
      Dynamic Motion Magic in torque current FOC, which requires Phoenix Pro. The trajectory parameters can change every loop cycle.
      Parameters:
      velocity - cruise velocity in rotations per second
      acceleration - in rotations per second squared
      jerk - in rotations per second cubed
    • setDynMMPositionVoltage

      protected void setDynMMPositionVoltage(DoubleSupplier rotations, DoubleSupplier velocity, DoubleSupplier acceleration, DoubleSupplier jerk)
      Dynamic Motion Magic with voltage compensation. The trajectory parameters can change every loop cycle.
      Parameters:
      velocity - cruise velocity in rotations per second
      acceleration - in rotations per second squared
      jerk - in rotations per second cubed
    • setMMPosition

      protected void setMMPosition(DoubleSupplier rotations)
      Position in rotations, via Motion Magic in voltage control on slot 0.
    • setMMPosition

      protected void setMMPosition(DoubleSupplier rotations, int slot)
      Position in rotations, via Motion Magic in voltage control on a chosen gain slot.
      Parameters:
      slot - the gain slot, 0, 1 or 2
    • setPercentOutput

      protected void setPercentOutput(DoubleSupplier percent)
      Open-loop percent output, in [-1, 1]. The applied voltage is percent times Mechanism.Config.getVoltageCompSaturation().
    • setVoltageOutput

      protected void setVoltageOutput(DoubleSupplier voltage)
      Open-loop voltage control. Applies the requested volts with no scaling.
    • setVoltageOutputNoSoftLimit

      protected void setVoltageOutputNoSoftLimit(DoubleSupplier voltage)
      Open-loop voltage control that ignores the software limit switches, so it can drive the mechanism past its configured travel.
    • setTorqueCurrentFoc

      public void setTorqueCurrentFoc(DoubleSupplier current)
      Torque current in amps through FOC, which requires Phoenix Pro.
    • setBrakeMode

      public void setBrakeMode(boolean isInBrake)
      Sets brake or coast mode and applies it to the hardware immediately.
    • toggleReverseSoftLimit

      public void toggleReverseSoftLimit(boolean enabled)
      Enables or disables the reverse software limit and applies the change immediately. The threshold comes from the current configuration.
    • toggleTorqueCurrentLimit

      public void toggleTorqueCurrentLimit(DoubleSupplier enabledLimit, boolean enabled)
      Enables or disables a forward and reverse torque current limit and applies the change immediately. Disabling resets the peak to plus or minus 300 A, which is effectively unlimited.
      Parameters:
      enabledLimit - the torque current limit in amps, applied when enabled is true
    • toggleSupplyCurrentLimit

      public void toggleSupplyCurrentLimit(DoubleSupplier enabledLimit, boolean enabled)
      Enables or disables the supply current limit and applies the change immediately.
      Parameters:
      enabledLimit - the supply current limit in amps
    • applyCurrentLimit

      public void applyCurrentLimit(DoubleSupplier supplyLimit, DoubleSupplier statorLimit)
      Applies new supply and stator current limits, in amps, but only when they differ from the configured ones. The apply is retried while CanConfigBudget.maxAttempts() allows it.
    • checkAvgCurrent

      public Command checkAvgCurrent(DoubleSupplier expectedCurrent, DoubleSupplier tolerance)
      Averages the stator current over the command's runtime and raises a warning Alert if the average is further than tolerance from expectedCurrent.
      Parameters:
      expectedCurrent - the expected average stator current in amps
      tolerance - the largest acceptable deviation in amps
    • checkMaxCurrent

      public Command checkMaxCurrent(DoubleSupplier expectedCurrent)
      Tracks the peak stator current and raises a warning Alert if it exceeds expectedCurrent.
      Parameters:
      expectedCurrent - the highest acceptable peak stator current in amps
    • checkMinThresholdCurrent

      public Command checkMinThresholdCurrent(DoubleSupplier expectedCurrent)
      Tracks the peak stator current and raises a warning Alert if it never reaches expectedCurrent, which checks that a mechanism drew at least its expected load.
      Parameters:
      expectedCurrent - the lowest acceptable peak stator current in amps
    • getMotor

      public TalonFX getMotor()
      Leader TalonFX, the motor this mechanism commands.
    • getFollowerMotors

      public TalonFX[] getFollowerMotors()
      Follower TalonFXs, empty when the mechanism runs on the leader alone.