Class Mechanism

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

public abstract class Mechanism extends Object implements SpectrumSubsystem
Abstract base class representing a CTRE TalonFX-driven robot mechanism with common control modes, telemetry, and convenience Command factories.

This class centralizes:

  • Motor creation/configuration (leader + optional followers) via the provided Mechanism.Config
  • Cached sensor readings (position, velocity, voltage, current) for efficient access
  • Common unit conversions (rotations/percent/degrees; RPS/RPM)
  • Standard closed-loop and open-loop control helpers (Motion Magic, velocity, voltage, percent, torque current)
  • Convenience Trigger factories (at/above/below thresholds)
  • Basic periodic current reporting to a battery/current logger

Attachment semantics

If Mechanism.Config.isAttached() is false, the mechanism will not attempt to construct or command hardware and will return safe default sensor values (typically 0).

Target tracking

The target field tracks the last commanded closed-loop setpoint sent by this class. It is used by helper triggers such as atTargetPosition(...).

Extending

Concrete mechanisms should provide a Mechanism.Config describing motor IDs, Talon configuration, follower configuration, and mechanism-specific min/max rotation bounds as needed.

Note: This class assumes CTRE Phoenix 6 units (rotations, rotations/sec, etc.) and uses Config.talonConfig.Feedback.SensorToMechanismRatio as the mechanism gearing ratio.

  • Field Details

  • Constructor Details

  • Method Details

    • periodic

      public void periodic()
      Specified by:
      periodic in interface Subsystem
    • simulationPeriodic

      public void simulationPeriodic()
      Specified by:
      simulationPeriodic in interface Subsystem
    • getName

      public String getName()
      Specified by:
      getName in interface Subsystem
    • isAttached

      public boolean isAttached()
    • logBatteryUsage

      public void logBatteryUsage()
    • getCurrentCommandName

      protected String getCurrentCommandName()
    • runningDefaultCommand

      public Trigger runningDefaultCommand()
    • getTarget

      public double getTarget()
    • atTargetPosition

      public Trigger atTargetPosition(DoubleSupplier tolerance)
    • atRotations

      public Trigger atRotations(DoubleSupplier target, DoubleSupplier tolerance)
    • belowRotations

      public Trigger belowRotations(DoubleSupplier target, DoubleSupplier tolerance)
    • aboveRotations

      public Trigger aboveRotations(DoubleSupplier target, DoubleSupplier tolerance)
    • atPercentage

      public Trigger atPercentage(DoubleSupplier target, DoubleSupplier tolerance)
    • belowPercentage

      public Trigger belowPercentage(DoubleSupplier target, DoubleSupplier tolerance)
    • abovePercentage

      public Trigger abovePercentage(DoubleSupplier target, DoubleSupplier tolerance)
    • atDegrees

      public Trigger atDegrees(DoubleSupplier target, DoubleSupplier tolerance)
    • belowDegrees

      public Trigger belowDegrees(DoubleSupplier target, DoubleSupplier tolerance)
    • aboveDegrees

      public Trigger aboveDegrees(DoubleSupplier target, DoubleSupplier tolerance)
    • atVelocityRPM

      public Trigger atVelocityRPM(DoubleSupplier target, DoubleSupplier tolerance)
    • belowVelocityRPM

      public Trigger belowVelocityRPM(DoubleSupplier target, DoubleSupplier tolerance)
    • aboveVelocityRPM

      public Trigger aboveVelocityRPM(DoubleSupplier target, DoubleSupplier tolerance)
    • atCurrent

      public Trigger atCurrent(DoubleSupplier target, DoubleSupplier tolerance)
    • belowCurrent

      public Trigger belowCurrent(DoubleSupplier target, DoubleSupplier tolerance)
    • aboveCurrent

      public Trigger aboveCurrent(DoubleSupplier target, DoubleSupplier tolerance)
    • updateStatorCurrent

      public double updateStatorCurrent()
      Update the value of the stator current for the motor
      Returns:
      motor stator current in amps
    • getStatorCurrent

      public double getStatorCurrent()
      Gets the stator current of the motor
      Returns:
      motor stator current in amps
    • updateSupplyCurrent

      public double updateSupplyCurrent()
      Update the value of the supply current for the motor
      Returns:
      motor supply current in amps
    • getSupplyCurrent

      public double getSupplyCurrent()
      Gets the supply current of the motor
      Returns:
      motor supply current in amps
    • updateVoltage

      public double updateVoltage()
      Update the value of the voltage for the motor
      Returns:
      motor voltage in volts
    • getVoltage

      public double getVoltage()
      Gets the voltage of the motor
      Returns:
      motor voltage in volts
    • updateTemp

      public double updateTemp()
      Update the value of the temperature for the motor
      Returns:
      motor temperature in Celsius
    • getTemp

      public double getTemp()
      Gets the temperature of the motor
      Returns:
      motor temperature in Celsius
    • percentToRotations

      public double percentToRotations(DoubleSupplier percent)
      Percentage to Rotations
      Returns:
      rotations based on percentage of max rotations
    • rotationsToPercent

      public double rotationsToPercent(DoubleSupplier rotations)
      Rotations to Percentage
      Parameters:
      rotations -
      Returns:
      percentage of max rotations
    • degreesToRotations

      public double degreesToRotations(DoubleSupplier degrees)
      Degrees to Rotations
      Returns:
      rotations
    • rotationsToDegrees

      public double rotationsToDegrees(DoubleSupplier rotations)
      Rotations to Degrees
      Parameters:
      rotations -
      Returns:
      degrees
    • getPositionRotations

      public double getPositionRotations()
      Gets the position of the motor in rotations
      Returns:
      motor position in rotations
    • getPositionPercentage

      public double getPositionPercentage()
      Gets the position of the motor in percentage of max rotations
      Returns:
      motor position in percentage of max rotations
    • getPositionDegrees

      public double getPositionDegrees()
      Gets the position of the motor in degrees
      Returns:
      motor position in degrees
    • getVelocityRPM

      public double getVelocityRPM()
      Gets the velocity of the motor in RPM
      Returns:
      motor velocity in RPM
    • runVelocity

      public Command runVelocity(DoubleSupplier velocityRPM)
      Runs the Mechanism at a given velocity
      Parameters:
      velocityRPM - in revolutions per minute
    • runVelocityTcFocRPM

      public Command runVelocityTcFocRPM(DoubleSupplier velocityRPM)
      Run the mechanism at given velocity rpm in TorqueCurrentFOC mode
      Parameters:
      velocityRPM - rotations per minute
      Returns:
      A Command that runs the mechanism at the given velocity in rpm using torque current FOC control.
    • runPercentage

      public Command runPercentage(DoubleSupplier percent)
      Open-loop percent output control with voltage compensation
      Parameters:
      percent - A fractional units between -1 and +1
      Returns:
      A Command that runs the mechanism at the given percentage output.
    • runVoltage

      public Command runVoltage(DoubleSupplier voltage)
      Apply a voltage output to the mechanism, bypassing any closed-loop control.
      Parameters:
      voltage - volts
      Returns:
      A Command that applies the specified voltage output to the mechanism.
    • runVoltageNoSoftLimit

      public Command runVoltageNoSoftLimit(DoubleSupplier voltage)
      Apply a voltage output to the mechanism, bypassing any closed-loop control and ignoring software limits.
      Parameters:
      voltage - volts
      Returns:
      A Command that applies the specified voltage output to the mechanism, ignoring software limits.
    • runTorqueCurrentFoc

      public Command runTorqueCurrentFoc(DoubleSupplier current)
      Run the mechanism at the specified torque current in FOC control.
      Parameters:
      current - torque current
      Returns:
      A Command that runs the mechanism at the given torque current in FOC control.
    • moveToRotations

      public Command moveToRotations(DoubleSupplier rotations)
      Run to the specified position.
      Parameters:
      rotations - position in revolutions
      Returns:
      A Command that runs the mechanism to the specified position in revolutions using FOC control.
    • moveToPercentage

      public Command moveToPercentage(DoubleSupplier percent)
      Move to the specified position.
      Parameters:
      percent - position in percentage of max revolutions
      Returns:
      A Command that runs the mechanism to the specified position in percentage of max revolutions using FOC control.
    • moveToDegrees

      public Command moveToDegrees(DoubleSupplier degrees)
      Move to the specified position.
      Parameters:
      degrees - position in degrees
      Returns:
      A Command that runs the mechanism to the specified position in degrees using FOC control.
    • runFocRotations

      public Command runFocRotations(DoubleSupplier rotations)
      Runs to the specified Motion Magic position using FOC control. Will require different PID and feedforward configs
      Parameters:
      rotations - position in revolutions
      Returns:
      A Command that runs the mechanism to the specified position in revolutions using Motion Magic FOC control.
    • runStop

      public Command runStop()
      Stops the mechanism.
      Returns:
      A Command that stops the mechanism.
    • coastMode

      public Command coastMode()
      Temporarily sets the mechanism to coast mode. The configuration is applied when the command is started and reverted when the command is ended.
      Returns:
      A Command that sets the mechanism to coast mode.
    • ensureBrakeMode

      public Command ensureBrakeMode()
      Sets the motor to brake mode if it is in coast mode.
      Returns:
      A Command that sets the mechanism to brake mode if it is in coast mode, ignoring disable.
    • runCurrentLimits

      protected Command runCurrentLimits(DoubleSupplier supplyLimit, DoubleSupplier statorLimit)
    • setCurrentLimits

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

      protected void stop()
    • tareMotor

      protected void tareMotor()
      Sets the mechanism position of the motor to 0
    • setMotorPosition

      protected void setMotorPosition(DoubleSupplier rotations)
      Sets the mechanism position of the motor
      Parameters:
      rotations - rotations
    • setMMVelocityFOC

      protected void setMMVelocityFOC(DoubleSupplier velocityRPS)
      Closed-loop Velocity Motion Magic with torque control (requires Pro)
      Parameters:
      velocityRPS - rotations per second
    • setVelocityTorqueCurrentFOC

      protected void setVelocityTorqueCurrentFOC(DoubleSupplier velocityRPS)
      Closed-loop Velocity with torque control (requires Pro)
      Parameters:
      velocityRPS - rotations per second
    • setVelocityTCFOCrpm

      protected void setVelocityTCFOCrpm(DoubleSupplier velocityRPS)
      Closed-loop Velocity with torque control (requires Pro)
      Parameters:
      velocityRPS - rotations per second
    • setVelocity

      protected void setVelocity(DoubleSupplier velocityRPS)
      Closed-loop velocity control with voltage compensation
      Parameters:
      velocityRPS - rotations per second
    • setMMPositionFoc

      protected void setMMPositionFoc(DoubleSupplier rotations)
      Closed-loop Position Motion Magic with torque control (requires Pro)
      Parameters:
      rotations - rotations
    • setDynMMPositionFoc

      protected void setDynMMPositionFoc(DoubleSupplier rotations, DoubleSupplier velocity, DoubleSupplier acceleration, DoubleSupplier jerk)
      Closed-loop Position Motion Magic with torque control (requires Pro). Dynamic allows you to set velocity, acceleration, and jerk during the command.
      Parameters:
      rotations - The target position in rotations.
      velocity - The maximum velocity in rotations per second.
      acceleration - The maximum acceleration in rotations per second squared.
      jerk - The maximum jerk in rotations per second cubed.
    • setDynMMPositionVoltage

      protected void setDynMMPositionVoltage(DoubleSupplier rotations, DoubleSupplier velocity, DoubleSupplier acceleration, DoubleSupplier jerk)
      Closed-loop Position Motion Magic with voltage control. Dynamic allows you to set velocity, acceleration, and jerk during the command.
      Parameters:
      rotations - The target position in rotations.
      velocity - The maximum velocity in rotations per second.
      acceleration - The maximum acceleration in rotations per second squared.
      jerk - The maximum jerk in rotations per second cubed.
    • setMMPosition

      protected void setMMPosition(DoubleSupplier rotations)
      Closed-loop Position Motion Magic
      Parameters:
      rotations - rotations
    • setMMPosition

      public void setMMPosition(DoubleSupplier rotations, int slot)
      Closed-loop Position Motion Magic using a slot other than 0
      Parameters:
      rotations - rotations
      slot - gains slot
    • setPercentOutput

      public void setPercentOutput(DoubleSupplier percent)
      Open-loop Percent output control with voltage compensation
      Parameters:
      percent - fractional units between -1 and +1
    • setVoltageOutput

      public void setVoltageOutput(DoubleSupplier voltage)
      Open-loop voltage control
      Parameters:
      voltage - volts
    • setVoltageOutputNoSoftLimit

      public void setVoltageOutputNoSoftLimit(DoubleSupplier voltage)
      Open-loop voltage control that ignores software limits
      Parameters:
      voltage - volts
    • setTorqueCurrentFoc

      public void setTorqueCurrentFoc(DoubleSupplier current)
    • setBrakeMode

      public void setBrakeMode(boolean isInBrake)
    • toggleReverseSoftLimit

      public void toggleReverseSoftLimit(boolean enabled)
    • toggleTorqueCurrentLimit

      public void toggleTorqueCurrentLimit(DoubleSupplier enabledLimit, boolean enabled)
    • toggleSupplyCurrentLimit

      public void toggleSupplyCurrentLimit(DoubleSupplier enabledLimit, boolean enabled)
    • applyCurrentLimit

      public void applyCurrentLimit(DoubleSupplier supplyLimit, DoubleSupplier statorLimit)
    • checkAvgCurrent

      public Command checkAvgCurrent(DoubleSupplier expectedCurrent, DoubleSupplier tolerance)
    • checkMaxCurrent

      public Command checkMaxCurrent(DoubleSupplier expectedCurrent)
    • checkMinThresholdCurrent

      public Command checkMinThresholdCurrent(DoubleSupplier expectedCurrent)
    • getMotor

      public TalonFX getMotor()
    • getFollowerMotors

      public TalonFX[] getFollowerMotors()