Package frc.spectrumLib.mechanism
Class Mechanism
java.lang.Object
frc.spectrumLib.mechanism.Mechanism
- All Implemented Interfaces:
Subsystem
- Direct Known Subclasses:
FuelIntake,Hood,IndexerBed,IndexerTower,IntakeExtension,IntakeExtension.IntakeExtensionRight,Launcher
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classTalonFX hardware configuration, the control request objects, and the mechanism-level parameters (gear ratio, soft limits, PID/FF gains, Motion Magic profile, current limits).static classA TalonFX follower that mirrors the leader's output.static enumHowlogStandard(java.lang.String, boolean, frc.spectrumLib.mechanism.Mechanism.RpmLog)logs<prefix>/RPM. -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedMechanism(Mechanism.Config config) Builds the leader TalonFX and any followers ifMechanism.Config.isAttached()is true.protectedMechanism(Mechanism.Config config, boolean attached) Overrides theattachedflag in the config before the hardware is built. -
Method Summary
Modifier and TypeMethodDescriptionaboveCurrent(DoubleSupplier target, DoubleSupplier tolerance) Trigger true while the stator current is above target - tolerance amps.aboveDegrees(DoubleSupplier target, DoubleSupplier tolerance) Trigger true while the position is above target - tolerance, in degrees.abovePercentage(DoubleSupplier target, DoubleSupplier tolerance) Trigger true while the position is above target - tolerance, as a percentage of maxRotations.aboveRotations(DoubleSupplier target, DoubleSupplier tolerance) Trigger true while the position is above target - tolerance, in rotations.aboveVelocityRPM(DoubleSupplier target, DoubleSupplier tolerance) Trigger true while the velocity is above target - tolerance RPM.voidapplyCurrentLimit(DoubleSupplier supplyLimit, DoubleSupplier statorLimit) Applies new supply and stator current limits, in amps, but only when they differ from the configured ones.atCurrent(DoubleSupplier target, DoubleSupplier tolerance) Trigger true while the stator current is within tolerance amps of target.atDegrees(DoubleSupplier target, DoubleSupplier tolerance) Trigger true while the position is within tolerance degrees of target.atPercentage(DoubleSupplier target, DoubleSupplier tolerance) Trigger true while the position is within tolerance percent of target, both of maxRotations.atRotations(DoubleSupplier target, DoubleSupplier tolerance) Trigger true while the position is within tolerance rotations of target.atTargetPosition(DoubleSupplier tolerance) Trigger true while the motor is within tolerance rotations ofgetTarget().atVelocityRPM(DoubleSupplier target, DoubleSupplier tolerance) Trigger true while the velocity is within tolerance RPM of target.belowCurrent(DoubleSupplier target, DoubleSupplier tolerance) Trigger true while the stator current is below target + tolerance amps.belowDegrees(DoubleSupplier target, DoubleSupplier tolerance) Trigger true while the position is below target + tolerance, in degrees.belowPercentage(DoubleSupplier target, DoubleSupplier tolerance) Trigger true while the position is below target + tolerance, as a percentage of maxRotations.belowRotations(DoubleSupplier target, DoubleSupplier tolerance) Trigger true while the position is below target + tolerance, in rotations.belowVelocityRPM(DoubleSupplier target, DoubleSupplier tolerance) Trigger true while the velocity is below target + tolerance RPM.checkAvgCurrent(DoubleSupplier expectedCurrent, DoubleSupplier tolerance) Averages the stator current over the command's runtime and raises a warningAlertif the average is further than tolerance from expectedCurrent.checkMaxCurrent(DoubleSupplier expectedCurrent) Tracks the peak stator current and raises a warningAlertif it exceeds expectedCurrent.checkMinThresholdCurrent(DoubleSupplier expectedCurrent) Tracks the peak stator current and raises a warningAlertif it never reaches expectedCurrent, which checks that a mechanism drew at least its expected load.Coasts while the command runs, then returns to brake.doubledegreesToRotations(DoubleSupplier degrees) Returns to brake if the mechanism is coasting.protected StringThe running command's name, or"none"when nothing is scheduled.TalonFX[]Follower TalonFXs, empty when the mechanism runs on the leader alone.doubledoubleMotor position in rotations, projected from the position signal's own timestamp to now along the velocity signal.getMotor()Leader TalonFX, the motor this mechanism commands.getName()The mechanism's name, as itsMechanism.Configspells it.doubleMotor position in degrees, sampled once per loop.doubleMotor position as a percentage of maxRotations, sampled once per loop.doubleMotor position in rotations, sampled once per loop.doubleMotor stator current, in amps, sampled once per loop.doubleMotor supply current, in amps, sampled once per loop.doubleLast closed-loop position setpoint this class sent, in rotations.doublegetTemp()Motor temperature in Celsius, sampled once per loop.doubleMotor velocity in RPM, sampled once per loop.doubleLast closed-loop velocity setpoint this class sent, in rotations per second.doubleApplied motor voltage, in volts, sampled once per loop.booleanisAtRotations(DoubleSupplier target, DoubleSupplier tolerance) True while the position is within tolerance rotations of target.booleanTrue when the mechanism has hardware and should send motor commands.booleanisAtTargetPosition(DoubleSupplier tolerance) True while the motor is within tolerance rotations ofgetTarget().booleanTrue when the leader is attached and its status frames are arriving over CAN.voidReports the leader's and followers' combined supply current to the battery logger, and checks each follower for life.protected voidlogDiagnostics(String prefix) Logs voltage, stator and supply current, temperature and connection underprefix/...at 10 Hz.protected voidlogDiagnostics(String prefix, boolean dashboard) AslogDiagnostics(String), optionally publishing to the dashboard as well.protected voidlogStandard(String prefix, boolean dashboardDiagnostics, Mechanism.RpmLog rpm) The per-loop logging every mechanism does: battery use, the current command,logDiagnostics(String, boolean)and RPM.moveToDegrees(DoubleSupplier degrees) AsmoveToRotations(java.util.function.DoubleSupplier), with the target in degrees.moveToPercentage(DoubleSupplier percent) AsmoveToRotations(java.util.function.DoubleSupplier), with the target as a percentage of maxRotations.moveToRotations(DoubleSupplier rotations) Moves the mechanism to a position in rotations with Motion Magic, in torque current FOC, which requires Phoenix Pro.doublepercentToRotations(DoubleSupplier percent) Percentage ofMechanism.Config.getMaxRotations()to an absolute rotation count.voidperiodic()Called once per scheduler loop.doublerotationsToDegrees(DoubleSupplier rotations) doublerotationsToPercent(DoubleSupplier rotations) Absolute rotation count to a percentage ofMechanism.Config.getMaxRotations().protected CommandrunCurrentLimits(DoubleSupplier supplyLimit, DoubleSupplier statorLimit) Applies new supply and stator current limits, in amps, to the mechanism.runFocRotations(DoubleSupplier rotations) Same asmoveToRotations(java.util.function.DoubleSupplier), which is the clearer name.runPercentage(DoubleSupplier percent) Runs the mechanism at an open-loop percent output, in [-1, 1].runStop()runTorqueCurrentFoc(DoubleSupplier current) Holds a torque current in amps through FOC, which requires Phoenix Pro.runVelocity(DoubleSupplier velocityRPM) Runs the mechanism at a constant velocity, in closed-loop voltage control.runVelocityTcFocRPM(DoubleSupplier velocityRPM) AsrunVelocity(java.util.function.DoubleSupplier), in torque current FOC, which requires Phoenix Pro.runVoltage(DoubleSupplier voltage) Applies a constant voltage in volts, bypassing closed-loop control.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.voidsetBrakeMode(boolean isInBrake) Sets brake or coast mode and applies it to the hardware immediately.protected voidsetCurrentLimits(DoubleSupplier supplyLimit, DoubleSupplier statorLimit) protected voidsetDynMMPositionFoc(DoubleSupplier rotations, DoubleSupplier velocity, DoubleSupplier acceleration, DoubleSupplier jerk) Dynamic Motion Magic in torque current FOC, which requires Phoenix Pro.protected voidsetDynMMPositionVoltage(DoubleSupplier rotations, DoubleSupplier velocity, DoubleSupplier acceleration, DoubleSupplier jerk) Dynamic Motion Magic with voltage compensation.protected voidsetMMPosition(DoubleSupplier rotations) Position in rotations, via Motion Magic in voltage control on slot 0.protected voidsetMMPosition(DoubleSupplier rotations, int slot) Position in rotations, via Motion Magic in voltage control on a chosen gain slot.protected voidsetMMPositionFoc(DoubleSupplier rotations) Position in rotations, via Motion Magic in torque current FOC (requires Phoenix Pro).protected voidsetMMVelocityFOC(DoubleSupplier velocityRPS) Velocity in rotations per second, via Motion Magic in torque current FOC (Phoenix Pro).protected voidsetMotorPosition(DoubleSupplier rotations) Writes the motor's internal position register, in rotations, without moving the motor.protected voidsetPercentOutput(DoubleSupplier percent) Open-loop percent output, in [-1, 1].protected voidsetPosition(DoubleSupplier rotations) Position control with voltage compensation and no velocity feedforward.protected voidsetPositionFocWithVelocity(DoubleSupplier rotations, DoubleSupplier velocityRPS) AssetPositionWithVelocity(java.util.function.DoubleSupplier, java.util.function.DoubleSupplier), inPositionTorqueCurrentFOC, which requires Phoenix Pro.protected voidsetPositionWithVelocity(DoubleSupplier rotations, DoubleSupplier velocityRPS) Position control with an explicit velocity feedforward.voidsetTorqueCurrentFoc(DoubleSupplier current) Torque current in amps through FOC, which requires Phoenix Pro.protected voidsetVelocity(DoubleSupplier velocityRPS) Velocity in rotations per second, in closed-loop voltage control.protected voidsetVelocityRPM(DoubleSupplier velocityRPM) AssetVelocity(java.util.function.DoubleSupplier), taking RPM.protected voidsetVelocityTCFOCrpm(DoubleSupplier velocityRPM) AssetVelocityTorqueCurrentFOC(java.util.function.DoubleSupplier), taking RPM.protected voidsetVelocityTorqueCurrentFOC(DoubleSupplier velocityRPS) Velocity in rotations per second, in torque current FOC (requires Phoenix Pro).protected voidsetVoltageOutput(DoubleSupplier voltage) Open-loop voltage control.protected voidOpen-loop voltage control that ignores the software limit switches, so it can drive the mechanism past its configured travel.voidCalled once per simulation loop, for physics model inputs.voidstop()Stops the motor output.protected voidSets the mechanism's reported position to zero (tares the motor encoder).voidtoggleReverseSoftLimit(boolean enabled) Enables or disables the reverse software limit and applies the change immediately.voidtoggleSupplyCurrentLimit(DoubleSupplier enabledLimit, boolean enabled) Enables or disables the supply current limit and applies the change immediately.voidtoggleTorqueCurrentLimit(DoubleSupplier enabledLimit, boolean enabled) Enables or disables a forward and reverse torque current limit and applies the change immediately.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface edu.wpi.first.wpilibj2.command.Subsystem
defer, getCurrentCommand, getDefaultCommand, idle, register, removeDefaultCommand, run, runEnd, runOnce, setDefaultCommand, startEnd, startRun
-
Field Details
-
motor
Leader TalonFX, the motor this mechanism commands. -
followerMotors
Follower TalonFXs, empty when the mechanism runs on the leader alone. -
config
Motor IDs, Talon settings, and mechanism parameters.
-
-
Constructor Details
-
Mechanism
Builds the leader TalonFX and any followers ifMechanism.Config.isAttached()is true. Sensor caches exist either way, so an unattached mechanism reads 0. -
Mechanism
Overrides theattachedflag 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. -
simulationPeriodic
public void simulationPeriodic()Called once per simulation loop, for physics model inputs.- Specified by:
simulationPeriodicin interfaceSubsystem
-
getName
The mechanism's name, as itsMechanism.Configspells it. -
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
The running command's name, or"none"when nothing is scheduled. -
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
Trigger true while the motor is within tolerance rotations ofgetTarget(). -
isAtTargetPosition
True while the motor is within tolerance rotations ofgetTarget(). -
atRotations
Trigger true while the position is within tolerance rotations of target. -
isAtRotations
True while the position is within tolerance rotations of target. -
belowRotations
Trigger true while the position is below target + tolerance, in rotations. -
aboveRotations
Trigger true while the position is above target - tolerance, in rotations. -
atPercentage
Trigger true while the position is within tolerance percent of target, both of maxRotations. -
belowPercentage
Trigger true while the position is below target + tolerance, as a percentage of maxRotations. -
abovePercentage
Trigger true while the position is above target - tolerance, as a percentage of maxRotations. -
atDegrees
Trigger true while the position is within tolerance degrees of target. -
belowDegrees
Trigger true while the position is below target + tolerance, in degrees. -
aboveDegrees
Trigger true while the position is above target - tolerance, in degrees. -
atVelocityRPM
Trigger true while the velocity is within tolerance RPM of target. -
belowVelocityRPM
Trigger true while the velocity is below target + tolerance RPM. -
aboveVelocityRPM
Trigger true while the velocity is above target - tolerance RPM. -
atCurrent
Trigger true while the stator current is within tolerance amps of target. -
belowCurrent
Trigger true while the stator current is below target + tolerance amps. -
aboveCurrent
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
Logs voltage, stator and supply current, temperature and connection underprefix/...at 10 Hz. Subclasses call this fromperiodic(), and the log keys are built on the first call. -
logDiagnostics
AslogDiagnostics(String), optionally publishing to the dashboard as well.- Parameters:
dashboard- true to publish throughTelemetry.logDash(java.lang.String, double)
-
logStandard
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
Percentage ofMechanism.Config.getMaxRotations()to an absolute rotation count. -
rotationsToPercent
Absolute rotation count to a percentage ofMechanism.Config.getMaxRotations(). -
degreesToRotations
-
rotationsToDegrees
-
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
Runs the mechanism at a constant velocity, in closed-loop voltage control. -
runVelocityTcFocRPM
AsrunVelocity(java.util.function.DoubleSupplier), in torque current FOC, which requires Phoenix Pro. -
runPercentage
Runs the mechanism at an open-loop percent output, in [-1, 1]. -
runVoltage
Applies a constant voltage in volts, bypassing closed-loop control. -
runVoltageNoSoftLimit
Applies a constant voltage in volts and ignores the software limit switches, so it can drive the mechanism past its configured travel. -
runTorqueCurrentFoc
Holds a torque current in amps through FOC, which requires Phoenix Pro. -
moveToRotations
Moves the mechanism to a position in rotations with Motion Magic, in torque current FOC, which requires Phoenix Pro. -
moveToPercentage
AsmoveToRotations(java.util.function.DoubleSupplier), with the target as a percentage of maxRotations. -
moveToDegrees
AsmoveToRotations(java.util.function.DoubleSupplier), with the target in degrees. -
runFocRotations
Same asmoveToRotations(java.util.function.DoubleSupplier), which is the clearer name. Requires Phoenix Pro. -
runStop
-
coastMode
Coasts while the command runs, then returns to brake. Safe to run while the robot is disabled. -
ensureBrakeMode
Returns to brake if the mechanism is coasting. Safe to run while the robot is disabled. -
runCurrentLimits
Applies new supply and stator current limits, in amps, to the mechanism. -
setCurrentLimits
-
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
Writes the motor's internal position register, in rotations, without moving the motor. -
setMMVelocityFOC
Velocity in rotations per second, via Motion Magic in torque current FOC (Phoenix Pro). -
setVelocityTorqueCurrentFOC
Velocity in rotations per second, in torque current FOC (requires Phoenix Pro). -
setVelocityTCFOCrpm
AssetVelocityTorqueCurrentFOC(java.util.function.DoubleSupplier), taking RPM. Requires Phoenix Pro. -
setVelocity
Velocity in rotations per second, in closed-loop voltage control. -
setVelocityRPM
AssetVelocity(java.util.function.DoubleSupplier), taking RPM. -
setPosition
Position control with voltage compensation and no velocity feedforward. -
setPositionWithVelocity
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 rotationsvelocityRPS- the feedforward in mechanism rotations per second
-
setMMPositionFoc
Position in rotations, via Motion Magic in torque current FOC (requires Phoenix Pro). -
setPositionFocWithVelocity
AssetPositionWithVelocity(java.util.function.DoubleSupplier, java.util.function.DoubleSupplier), inPositionTorqueCurrentFOC, which requires Phoenix Pro.- Parameters:
rotations- the target position in mechanism rotationsvelocityRPS- 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 secondacceleration- in rotations per second squaredjerk- 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 secondacceleration- in rotations per second squaredjerk- in rotations per second cubed
-
setMMPosition
Position in rotations, via Motion Magic in voltage control on slot 0. -
setMMPosition
Position in rotations, via Motion Magic in voltage control on a chosen gain slot.- Parameters:
slot- the gain slot, 0, 1 or 2
-
setPercentOutput
Open-loop percent output, in [-1, 1]. The applied voltage is percent timesMechanism.Config.getVoltageCompSaturation(). -
setVoltageOutput
Open-loop voltage control. Applies the requested volts with no scaling. -
setVoltageOutputNoSoftLimit
Open-loop voltage control that ignores the software limit switches, so it can drive the mechanism past its configured travel. -
setTorqueCurrentFoc
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
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
Enables or disables the supply current limit and applies the change immediately.- Parameters:
enabledLimit- the supply current limit in amps
-
applyCurrentLimit
Applies new supply and stator current limits, in amps, but only when they differ from the configured ones. The apply is retried whileCanConfigBudget.maxAttempts()allows it. -
checkAvgCurrent
Averages the stator current over the command's runtime and raises a warningAlertif the average is further than tolerance from expectedCurrent.- Parameters:
expectedCurrent- the expected average stator current in ampstolerance- the largest acceptable deviation in amps
-
checkMaxCurrent
Tracks the peak stator current and raises a warningAlertif it exceeds expectedCurrent.- Parameters:
expectedCurrent- the highest acceptable peak stator current in amps
-
checkMinThresholdCurrent
Tracks the peak stator current and raises a warningAlertif 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
Leader TalonFX, the motor this mechanism commands. -
getFollowerMotors
Follower TalonFXs, empty when the mechanism runs on the leader alone.
-