Package frc.spectrumLib.mechanism
Class Mechanism
java.lang.Object
frc.spectrumLib.mechanism.Mechanism
- All Implemented Interfaces:
Subsystem,SpectrumSubsystem
- Direct Known Subclasses:
FuelIntake,Hood,IndexerBed,IndexerTower,IntakeExtension,Launcher
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
Triggerfactories (at/above/below thresholds) - Basic periodic current reporting to a battery/current logger
Attachment semantics
IfMechanism.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
Thetarget 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 aMechanism.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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic class -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedMechanism(Mechanism.Config config) protectedMechanism(Mechanism.Config config, boolean attached) -
Method Summary
Modifier and TypeMethodDescriptionaboveCurrent(DoubleSupplier target, DoubleSupplier tolerance) aboveDegrees(DoubleSupplier target, DoubleSupplier tolerance) abovePercentage(DoubleSupplier target, DoubleSupplier tolerance) aboveRotations(DoubleSupplier target, DoubleSupplier tolerance) aboveVelocityRPM(DoubleSupplier target, DoubleSupplier tolerance) voidapplyCurrentLimit(DoubleSupplier supplyLimit, DoubleSupplier statorLimit) atCurrent(DoubleSupplier target, DoubleSupplier tolerance) atDegrees(DoubleSupplier target, DoubleSupplier tolerance) atPercentage(DoubleSupplier target, DoubleSupplier tolerance) atRotations(DoubleSupplier target, DoubleSupplier tolerance) atTargetPosition(DoubleSupplier tolerance) atVelocityRPM(DoubleSupplier target, DoubleSupplier tolerance) belowCurrent(DoubleSupplier target, DoubleSupplier tolerance) belowDegrees(DoubleSupplier target, DoubleSupplier tolerance) belowPercentage(DoubleSupplier target, DoubleSupplier tolerance) belowRotations(DoubleSupplier target, DoubleSupplier tolerance) belowVelocityRPM(DoubleSupplier target, DoubleSupplier tolerance) checkAvgCurrent(DoubleSupplier expectedCurrent, DoubleSupplier tolerance) checkMaxCurrent(DoubleSupplier expectedCurrent) checkMinThresholdCurrent(DoubleSupplier expectedCurrent) Temporarily sets the mechanism to coast mode.doubledegreesToRotations(DoubleSupplier degrees) Degrees to RotationsSets the motor to brake mode if it is in coast mode.protected StringTalonFX[]getMotor()getName()doubleGets the position of the motor in degreesdoubleGets the position of the motor in percentage of max rotationsdoubleGets the position of the motor in rotationsdoubleGets the stator current of the motordoubleGets the supply current of the motordoubledoublegetTemp()Gets the temperature of the motordoubleGets the velocity of the motor in RPMdoubleGets the voltage of the motorbooleanvoidmoveToDegrees(DoubleSupplier degrees) Move to the specified position.moveToPercentage(DoubleSupplier percent) Move to the specified position.moveToRotations(DoubleSupplier rotations) Run to the specified position.doublepercentToRotations(DoubleSupplier percent) Percentage to Rotationsvoidperiodic()doublerotationsToDegrees(DoubleSupplier rotations) Rotations to DegreesdoublerotationsToPercent(DoubleSupplier rotations) Rotations to Percentageprotected CommandrunCurrentLimits(DoubleSupplier supplyLimit, DoubleSupplier statorLimit) runFocRotations(DoubleSupplier rotations) Runs to the specified Motion Magic position using FOC control.runPercentage(DoubleSupplier percent) Open-loop percent output control with voltage compensationrunStop()Stops the mechanism.runTorqueCurrentFoc(DoubleSupplier current) Run the mechanism at the specified torque current in FOC control.runVelocity(DoubleSupplier velocityRPM) Runs the Mechanism at a given velocityrunVelocityTcFocRPM(DoubleSupplier velocityRPM) Run the mechanism at given velocity rpm in TorqueCurrentFOC moderunVoltage(DoubleSupplier voltage) Apply a voltage output to the mechanism, bypassing any closed-loop control.runVoltageNoSoftLimit(DoubleSupplier voltage) Apply a voltage output to the mechanism, bypassing any closed-loop control and ignoring software limits.voidsetBrakeMode(boolean isInBrake) protected voidsetCurrentLimits(DoubleSupplier supplyLimit, DoubleSupplier statorLimit) protected voidsetDynMMPositionFoc(DoubleSupplier rotations, DoubleSupplier velocity, DoubleSupplier acceleration, DoubleSupplier jerk) Closed-loop Position Motion Magic with torque control (requires Pro).protected voidsetDynMMPositionVoltage(DoubleSupplier rotations, DoubleSupplier velocity, DoubleSupplier acceleration, DoubleSupplier jerk) Closed-loop Position Motion Magic with voltage control.protected voidsetMMPosition(DoubleSupplier rotations) Closed-loop Position Motion MagicvoidsetMMPosition(DoubleSupplier rotations, int slot) Closed-loop Position Motion Magic using a slot other than 0protected voidsetMMPositionFoc(DoubleSupplier rotations) Closed-loop Position Motion Magic with torque control (requires Pro)protected voidsetMMVelocityFOC(DoubleSupplier velocityRPS) Closed-loop Velocity Motion Magic with torque control (requires Pro)protected voidsetMotorPosition(DoubleSupplier rotations) Sets the mechanism position of the motorvoidsetPercentOutput(DoubleSupplier percent) Open-loop Percent output control with voltage compensationvoidsetTorqueCurrentFoc(DoubleSupplier current) protected voidsetVelocity(DoubleSupplier velocityRPS) Closed-loop velocity control with voltage compensationprotected voidsetVelocityTCFOCrpm(DoubleSupplier velocityRPS) Closed-loop Velocity with torque control (requires Pro)protected voidsetVelocityTorqueCurrentFOC(DoubleSupplier velocityRPS) Closed-loop Velocity with torque control (requires Pro)voidsetVoltageOutput(DoubleSupplier voltage) Open-loop voltage controlvoidOpen-loop voltage control that ignores software limitsvoidprotected voidstop()protected voidSets the mechanism position of the motor to 0voidtoggleReverseSoftLimit(boolean enabled) voidtoggleSupplyCurrentLimit(DoubleSupplier enabledLimit, boolean enabled) voidtoggleTorqueCurrentLimit(DoubleSupplier enabledLimit, boolean enabled) doubleUpdate the value of the stator current for the motordoubleUpdate the value of the supply current for the motordoubleUpdate the value of the temperature for the motordoubleUpdate the value of the voltage for the motorMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface frc.spectrumLib.SpectrumSubsystem
setupDefaultCommand, setupStatesMethods inherited from interface edu.wpi.first.wpilibj2.command.Subsystem
defer, getCurrentCommand, getDefaultCommand, idle, register, removeDefaultCommand, run, runEnd, runOnce, setDefaultCommand, startEnd, startRun
-
Field Details
-
motor
-
followerMotors
-
config
-
-
Constructor Details
-
Mechanism
-
Mechanism
-
-
Method Details
-
periodic
public void periodic() -
simulationPeriodic
public void simulationPeriodic()- Specified by:
simulationPeriodicin interfaceSubsystem
-
getName
-
isAttached
public boolean isAttached() -
logBatteryUsage
public void logBatteryUsage() -
getCurrentCommandName
-
runningDefaultCommand
-
getTarget
public double getTarget() -
atTargetPosition
-
atRotations
-
belowRotations
-
aboveRotations
-
atPercentage
-
belowPercentage
-
abovePercentage
-
atDegrees
-
belowDegrees
-
aboveDegrees
-
atVelocityRPM
-
belowVelocityRPM
-
aboveVelocityRPM
-
atCurrent
-
belowCurrent
-
aboveCurrent
-
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
Percentage to Rotations- Returns:
- rotations based on percentage of max rotations
-
rotationsToPercent
Rotations to Percentage- Parameters:
rotations-- Returns:
- percentage of max rotations
-
degreesToRotations
Degrees to Rotations- Returns:
- rotations
-
rotationsToDegrees
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
Runs the Mechanism at a given velocity- Parameters:
velocityRPM- in revolutions per minute
-
runVelocityTcFocRPM
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
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
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
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
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
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
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
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
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
Stops the mechanism.- Returns:
- A Command that stops the mechanism.
-
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
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
-
setCurrentLimits
-
stop
protected void stop() -
tareMotor
protected void tareMotor()Sets the mechanism position of the motor to 0 -
setMotorPosition
Sets the mechanism position of the motor- Parameters:
rotations- rotations
-
setMMVelocityFOC
Closed-loop Velocity Motion Magic with torque control (requires Pro)- Parameters:
velocityRPS- rotations per second
-
setVelocityTorqueCurrentFOC
Closed-loop Velocity with torque control (requires Pro)- Parameters:
velocityRPS- rotations per second
-
setVelocityTCFOCrpm
Closed-loop Velocity with torque control (requires Pro)- Parameters:
velocityRPS- rotations per second
-
setVelocity
Closed-loop velocity control with voltage compensation- Parameters:
velocityRPS- rotations per second
-
setMMPositionFoc
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
Closed-loop Position Motion Magic- Parameters:
rotations- rotations
-
setMMPosition
Closed-loop Position Motion Magic using a slot other than 0- Parameters:
rotations- rotationsslot- gains slot
-
setPercentOutput
Open-loop Percent output control with voltage compensation- Parameters:
percent- fractional units between -1 and +1
-
setVoltageOutput
Open-loop voltage control- Parameters:
voltage- volts
-
setVoltageOutputNoSoftLimit
Open-loop voltage control that ignores software limits- Parameters:
voltage- volts
-
setTorqueCurrentFoc
-
setBrakeMode
public void setBrakeMode(boolean isInBrake) -
toggleReverseSoftLimit
public void toggleReverseSoftLimit(boolean enabled) -
toggleTorqueCurrentLimit
-
toggleSupplyCurrentLimit
-
applyCurrentLimit
-
checkAvgCurrent
-
checkMaxCurrent
-
checkMinThresholdCurrent
-
getMotor
-
getFollowerMotors
-