Single-jointed rotating arms experience variable gravity torque that depends directly on the cosine of the arm’s angle relative to the horizontal plane (τ = m · g · r · cos(θ)). When horizontal (θ = 0°), gravity torque is at its maximum; when vertical (θ = 90°), gravity torque drops to zero.
This subsystem uses a Talon FX paired with an absolute CANcoder and CTRE’s onboard Arm_Cosine gravity feedforward.
Subsystem Implementation
Section titled “Subsystem Implementation”package frc.robot.subsystems;
import com.ctre.phoenix6.configs.CANcoderConfiguration;import com.ctre.phoenix6.configs.TalonFXConfiguration;import com.ctre.phoenix6.controls.MotionMagicVoltage;import com.ctre.phoenix6.hardware.CANcoder;import com.ctre.phoenix6.hardware.TalonFX;import com.ctre.phoenix6.signals.FeedbackSensorSourceValue;import com.ctre.phoenix6.signals.GravityTypeValue;import com.ctre.phoenix6.signals.NeutralModeValue;
import edu.wpi.first.units.measure.Angle;import static edu.wpi.first.units.Units.*;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;import edu.wpi.first.wpilibj2.command.Command;import edu.wpi.first.wpilibj2.command.SubsystemBase;
public class ArmSubsystem extends SubsystemBase { private final TalonFX m_motor = new TalonFX(14, "rio"); private final CANcoder m_cancoder = new CANcoder(24, "rio");
private final MotionMagicVoltage m_motionMagic = new MotionMagicVoltage(0).withSlot(0);
// High reduction gearbox (e.g. 60:1 reduction) private static final double GEAR_RATIO = 60.0;
public ArmSubsystem() { configureHardware(); setDefaultCommand(holdCurrentAngleCommand()); }
private void configureHardware() { // 1. Configure Absolute CANcoder CANcoderConfiguration encoderCfg = new CANcoderConfiguration(); encoderCfg.MagnetSensor.MagnetOffset = -0.125; // Calibrated offset in rotations m_cancoder.getConfigurator().apply(encoderCfg);
// 2. Configure Motor Controller TalonFXConfiguration motorCfg = new TalonFXConfiguration(); motorCfg.MotorOutput.NeutralMode = NeutralModeValue.Brake;
// Current limits motorCfg.CurrentLimits.StatorCurrentLimitEnable = true; motorCfg.CurrentLimits.StatorCurrentLimit = 40.0; // 40 Amps
// Soft limits: Restrict arm between -15° (intake) and 105° (scoring) motorCfg.SoftwareLimitSwitch.ReverseSoftLimitEnable = true; motorCfg.SoftwareLimitSwitch.ReverseSoftLimitThreshold = (-15.0 / 360.0) * GEAR_RATIO; motorCfg.SoftwareLimitSwitch.ForwardSoftLimitEnable = true; motorCfg.SoftwareLimitSwitch.ForwardSoftLimitThreshold = (105.0 / 360.0) * GEAR_RATIO;
// Slot 0 Gains with Cosine Gravity Feedforward motorCfg.Slot0.GravityType = GravityTypeValue.Arm_Cosine; motorCfg.Slot0.kG = 0.55; // 0.55V holding voltage when arm is horizontal (0°) motorCfg.Slot0.kS = 0.20; // Friction compensation motorCfg.Slot0.kV = 0.14; // Velocity feedforward motorCfg.Slot0.kP = 6.0; // Proportional stiffness motorCfg.Slot0.kD = 0.25; // Derivative damping
// Motion Magic Trajectory Limits (at mechanism output) motorCfg.MotionMagic.MotionMagicCruiseVelocity = 0.5 * GEAR_RATIO; // 0.5 rot/s (180 deg/s) motorCfg.MotionMagic.MotionMagicAcceleration = 1.0 * GEAR_RATIO; // 1.0 rot/s² motorCfg.MotionMagic.MotionMagicJerk = 10.0 * GEAR_RATIO;
m_motor.getConfigurator().apply(motorCfg);
// Seed motor position from absolute CANcoder at power-on double initialArmRotations = m_cancoder.getAbsolutePosition().getValueAsDouble(); m_motor.setPosition(initialArmRotations * GEAR_RATIO); }
@Override public void periodic() { double currentAngleDeg = (m_motor.getPosition().getValueAsDouble() / GEAR_RATIO) * 360.0; SmartDashboard.putNumber("Arm/Angle_deg", currentAngleDeg); SmartDashboard.putNumber("Arm/Current_A", m_motor.getStatorCurrent().getValueAsDouble()); }
public Command moveToAngleCommand(Angle targetAngle) { double targetRotations = (targetAngle.in(Degrees) / 360.0) * GEAR_RATIO; return run(() -> m_motor.setControl(m_motionMagic.withPosition(targetRotations))) .until(() -> isAtAngle(targetAngle)) .withName("MoveToAngle"); }
public boolean isAtAngle(Angle targetAngle) { double currentDegrees = (m_motor.getPosition().getValueAsDouble() / GEAR_RATIO) * 360.0; return Math.abs(currentDegrees - targetAngle.in(Degrees)) < 2.0; // 2 degree tolerance }
public Command holdCurrentAngleCommand() { return run(() -> m_motor.setControl(m_motionMagic)).withName("HoldArmAngle"); }}Cosine Scaling Zero Reference
For CTRE’s Arm_Cosine feedforward, ensure 0° corresponds to the horizontal arm position where gravity torque is maximized. If your zero is vertical, offset your sensor reading accordingly!