The CTRE TalonSRX and TalonFX controllers can be assigned sensors. The sensor can be attached directly to the talon, or it can be a remote sensor (attached somewhere else).
Each motor gets assigned ONE configured "selected sensor" for EACH of the 2 PID loops. The sensor can be
The "selected sensor" are assigned to one of two possible PID loops on each controller. PID loop 0 is the primary loop and PID loop 1 is the secondary loop. More about the 2 PID loops later.
One connected sensor can be assigned for each PID loop .
Assign a sensor with configSelectedFeedbackSensor:
This stamement assigns a QuadEncoder to motor 1, PID loop 0. The encoder must be attached to the motor.
motor1.configSelectedFeedbackSensor(FeedbackDevice.QuadEncoder,
0, 20);
For talonFX's use FeedbackDevice.IntegratedSensor
2. Assign a Remotely Connected Sensor
Before assigning a remotely connected sensor, you need to configure a remote sensor filter, which tells where to find the sensor and what type it is:
motor1.configRemoteFeedbackFilter( gyro.getDeviceID(),
RemoteSensorSource.Pigeon_Yaw, 0, 20;
A remote sensor can also be the selected connected sensor of another motor. This line assigns the connected encoder of motor 2 to be a remote sensor #1 of motor 1.
motor1.configRemoteFeedbackFilter( motor2.getDeviceID(),
RemoteSensorSource.TalonSRXSelectedSensor, 1, 20;
Once the remote filter is configured, we can assign the remote sensor. This line assigns the gyro to be the sensor for PID loop 1 on motor 1.
motor1.configSelectedFeedbackSensor( FeedbackDevice.RemoteSensor0,
1, 20);
3. Sensor Sums and Differences
We can use the sum of difference of two different sensors to be a remote sensor. Why would you do this? One reason would be to average the right and left side encoders.
To set up a sum, we need to configure two sensors to be the first term (Sum0) and second term (Sum 1). The sum will be Sum0 + Sum1. The sum is then assigned to be the selected sensor for motor 1.
motor1.configRemoteFeedbackFilter( motor2.getDeviceID(),
RemoteSensorSource.TalonSRXSelectedSensor, 0, 20;
motor1.configSensorTerm(SensorTerm.Sum0,
FeedbackDevice.RemoteSensor0, 0)
motor1.configSensorTerm(SensorTerm.Sum1,
FeedbackDevice.FeedbackDevice.QuadEncoder , 0)
motor1.configSelectedFeedbackSensor( FeedbackDevice.SensorSum,
0, 20,);
To get the average, assign a sensor coefficient of 1/2 to the sum. This assigns a coefficient of 0.5 to the sensor assigned to PID0. The first argument is the coefficient, the second is the PID loop, and the third is the timeout.
motor1.configSelectedFeedbackCoefficient( 0.5, 0, 20)
4. PID Loops and Putting it All Together
One way to make use of the two available PID loops is to use the primary (PID0) for controlling either distance or velocity, and the a auxiliary (PID1) for control angle.
For position, you could use the average of the two encoders, see section 3, for PID 0, and the gyro for PID1.