Skip to content
learnFRC.org
programmingreference

Remote CANcoder Closed-Loop Pairing

Configure motor controllers to execute closed-loop position and velocity routines directly from remote absolute CANcoders without roboRIO software lag.

Pairing an absolute CANcoder directly with a Talon FX motor controller enables high-speed, 1 kHz onboard closed-loop control referenced to the true mechanism output shaft. Offloading the sensor loop to the motor controller eliminates roboRIO loop jitter, CAN communication delays, and backlash discrepancies between the motor rotor and final driven joint.

Feedback ModeMechanism LayoutBehavior
RemoteCANcoderCANcoder mounted on output shaftClosed loop controls position purely from CANcoder readings over CAN.
FusedCANcoderCANcoder on output shaft, motor rotor coupledFuses high-speed rotor velocity with absolute CANcoder position to eliminate gear lash.
SyncCANcoderMulti-turn mechanisms with backlashSynchronizes rotor position with CANcoder absolute angle on power-up and runs on rotor encoder.

The following example configures a Talon FX paired directly with a remote CANcoder mounted to an arm or turret output shaft using Phoenix 6 FusedCANcoder mode.

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.NeutralModeValue;
import com.ctre.phoenix6.signals.SensorDirectionValue;
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 RemoteSensorPivotSubsystem extends SubsystemBase {
private static final int MOTOR_ID = 21;
private static final int CANCODER_ID = 31;
private static final String CAN_BUS = "rio";
// 50:1 gear reduction between motor rotor and CANcoder output shaft
private static final double GEAR_RATIO = 50.0;
private final TalonFX m_motor = new TalonFX(MOTOR_ID, CAN_BUS);
private final CANcoder m_cancoder = new CANcoder(CANCODER_ID, CAN_BUS);
private final MotionMagicVoltage m_motionMagic = new MotionMagicVoltage(0).withSlot(0);
public RemoteSensorPivotSubsystem() {
configureSensorsAndMotor();
}
private void configureSensorsAndMotor() {
// 1. Configure the remote absolute CANcoder
CANcoderConfiguration encoderConfig = new CANcoderConfiguration();
encoderConfig.MagnetSensor.SensorDirection = SensorDirectionValue.CounterClockwise_Positive;
encoderConfig.MagnetSensor.MagnetOffset = 0.145; // Calibrated magnet offset in rotations
m_cancoder.getConfigurator().apply(encoderConfig);
// 2. Configure the Talon FX to track the CANcoder directly
TalonFXConfiguration motorConfig = new TalonFXConfiguration();
motorConfig.MotorOutput.NeutralMode = NeutralModeValue.Brake;
// Current limits to protect gearbox and motor
motorConfig.CurrentLimits.StatorCurrentLimitEnable = true;
motorConfig.CurrentLimits.StatorCurrentLimit = 40.0; // 40 Amps stator limit
// Set feedback source to remote CANcoder
motorConfig.Feedback.FeedbackSensorSource = FeedbackSensorSourceValue.RemoteCANcoder;
motorConfig.Feedback.FeedbackRemoteSensorID = CANCODER_ID;
// SensorToMechanismRatio defines mechanism rotations per sensor rotation (1:1 on shaft)
motorConfig.Feedback.SensorToMechanismRatio = 1.0;
// RotorToSensorRatio accounts for the reduction between motor and CANcoder
motorConfig.Feedback.RotorToSensorRatio = GEAR_RATIO;
// Slot 0 PID Gains (tuned in mechanism rotations)
motorConfig.Slot0.kP = 28.0;
motorConfig.Slot0.kI = 0.0;
motorConfig.Slot0.kD = 1.2;
motorConfig.Slot0.kV = 0.12;
// Motion Magic Profile constraints in mechanism rotations/sec
motorConfig.MotionMagic.MotionMagicCruiseVelocity = 0.75; // 0.75 rot/s (270 deg/s)
motorConfig.MotionMagic.MotionMagicAcceleration = 1.50; // 1.5 rot/s^2
m_motor.getConfigurator().apply(motorConfig);
}
@Override
public void periodic() {
SmartDashboard.putNumber("Pivot/MotorPosition_Rot", m_motor.getPosition().getValueAsDouble());
SmartDashboard.putNumber("Pivot/CANcoderPosition_Rot", m_cancoder.getPosition().getValueAsDouble());
SmartDashboard.putNumber("Pivot/Current_A", m_motor.getStatorCurrent().getValueAsDouble());
}
public Command moveToAngleCommand(Angle targetAngle) {
double targetMechanismRotations = targetAngle.in(Rotations);
return run(() -> m_motor.setControl(m_motionMagic.withPosition(targetMechanismRotations)))
.until(() -> isAtAngle(targetAngle))
.withName("MoveToAngle");
}
public boolean isAtAngle(Angle targetAngle) {
double currentAngleDeg = m_motor.getPosition().getValueAsDouble() * 360.0;
return Math.abs(currentAngleDeg - targetAngle.in(Degrees)) < 2.0;
}
}
  • Inverted Sensor Feedback Runaway: If the motor drives forward but the CANcoder counts backward, the controller interprets the motion as negative error and increases forward voltage to maximum until hardware breaks. Always verify direction before engaging closed loop.
  • Mismatched CAN Bus Names: If the Talon FX is on a CANivore (“rio” vs “canivore-name”) while the CANcoder is on the default roboRIO CAN bus, the motor controller cannot read the sensor frames. Remote sensors must share the same physical CAN bus.
  • Unset Rotor-to-Sensor Ratio: When using RemoteCANcoder or FusedCANcoder, failing to set RotorToSensorRatio causes rapid oscillation because the controller expects 1 motor turn per sensor turn.
  • Magnet Distance and Green LED Status: The CANcoder magnet must sit between 0.75 mm and 1.5 mm from the sensor casing. A yellow or red status LED indicates magnet misalignment and causes non-linear feedback errors.
Positive Direction Alignment

Before running closed loop on any remote sensor mechanism, physically rotate the mechanism by hand in the positive mechanical direction. Verify that both m_motor.getPosition() and m_cancoder.getPosition() increase positively in Phoenix Tuner X.

  1. Verify Shared CAN Bus: Confirm in Phoenix Tuner X that both the motor controller and CANcoder reside on the identical CAN bus with green health status indicators.
  2. Confirm Magnet Alignment: Check the physical CANcoder status LED while sweeping the mechanism through its full range of motion. Verify the LED stays solid green across all travel.
  3. Validate Open-Loop Sensor Tracking: Command 1.0V forward in open-loop control. Confirm both the rotor velocity and the CANcoder velocity register positive numbers in Tuner X graphs.
  4. Step-Response Test at 20% Travel: Set a small position step target (for example, 0.1 rotations). Confirm the motor smoothly settles without hunting or high-frequency buzzing.

Verified Sources & Standards

Evergreen Standard Verified: Oct 1, 2026