Usage TalonFX - captainsoccer/BasicMotor GitHub Wiki
TalonFX Usage
This page explains how to use the BasicTalonFX
class with the BasicMotor library. BasicTalonFX
provides a wrapper around CTRE’s TalonFX motor controller with automatic logging, measurement handling, and sensor integration.
Initialization
To create a TalonFX motor, you'll need to provide the following:
- PID gains (
ControllerGains
) - CAN ID (
int
) - Gear ratio (
double
) - Unit conversion factor (
double
) - Logging name (
String
)
Example on a simple constructor
ControllerGains gains = new ControllerGains(0.1, 0.0, 0.0); // Example PID values
BasicTalonFX motor = new BasicTalonFX(
gains,
1, // CAN ID
10.0, // Gear ratio (e.g., 10:1 reduction)
1.0, // Unit conversion (e.g., 1 rotation = 1 unit)
"Shooter Motor", // Logging/debugging name
);
Current and Torque Control
When using current control mode or torque control mode, the TalonFX does not rely on a PID loop to achieve the desired current.
Instead, it uses Field-Oriented Control (FOC) to control the motor’s current directly.
This means there is no need for PID tuning in these modes.
However, using FOC for current or torque control requires the motor to be licensed with Phoenix Pro.
Advanced Configuration
For advanced use cases, BasicTalonFXConfig
provides TalonFX-specific options such as setting the CAN bus name (for multi-bus systems like CANivore), enabling Field-Oriented Control (FOC) for precise torque control, waiting for all can bus signals, and customizing current limits.
Current limits Configuration
using BasicTalonFXConfig
you can configure the following fields:
statorCurrentLimit
: The current limit applied on the stator (the output current limit), effect torque output.supplyCurrentLimit
: The current limit applied on the supply (the input current limits), effects power draw.lowerLimitTime
: The time the motor is allowed to stay at thesupplyCurrentLimit
before dropping down to thelowerCurrentLimit
.lowerCurrentLimit
: The supply current limit the motor drops to after stayinglowerLimitTime
at thesupplyCurrentLimit
.
Example for a current limit configuration
BasicTalonFXConfig config = new BasicTalonFXConfig();
config.currentLimitConfig.statorCurrentLimit = 70;
config.currentLimitConfig.supplyCurrentLimit = 50;
config.currentLimitConfig.lowerLimitTime = 1.0;
config.currentLimitConfig.lowerCurrentLimit = 30;
setCurrentLimits
function
Using the CurrentLimits currentLimits = new CurrentLimitsTalonFX(
/* statorCurrentLimit */ 70,
/* supplyCurrentLimit */ 50,
/* lowerLimitTime */ 1,
/* lowerCurrentLimit */ 30
);
motor.setCurrentLimits(currentLimits);
CAN Bus, FOC, and Status Signal Waiting
canBusName
Use this field to specify a custom CAN bus name, such as "canivore"
. This is required when your TalonFX is connected to a CANivore instead of the default "rio"
bus.
config.canBusName = "canivore";
⚠️ To set the can Bus name you must use the
BasicTalonFXConfig
and set it there.
enableFOC
Enables Field-Oriented Control (FOC) for smoother and more precise low-speed control. Requires Phoenix Pro to be installed and licensed, otherwise it will be ignored.
config.enableFOC = true;
enableFOC
function
using the motor.enableFOC(true);
waitForAllSignals
Enables the control loop to wait for all the status signals to arrive at the same time, (also known as time sync) this can increase accuracy and loop time. This will only work if the motor is connected to a canivore with a valid phoenix pro license, otherwise it will significantly impact performance.
config.waitForAllSignals = true;
enableTimeSync
using the motor.enableTimeSync(true);
Use Remote/Fused CanCoder
The BasicTalonFX class offers setting a Can Coder as the motor's direct measurement source. Using the following function will enable the motor to use the can Coder`s readings for the on-motor PID controller.
⚠️ The Can Coder must be initialized before and be on the same can bus chain.
Using The remote can Coder
Here, 6.75 Is the ratio between the motor's rotation and the can coders rotation, and the unit conversion is the wheel`s diameter times pi (converting to meters).
talonFX.useRemoteCanCoder(canCoder, 6.75, 2 * Math.PI * wheelRadiusMeters);
Using the Fused can Coder
To use a remote canCoder both the motor and canCoder must be licensed with phoenix pro. Here, 6.75 Is the ratio between the motor's rotation and the can coders rotation, and the unit conversion is the wheel`s diameter times pi (converting to meters).
talonFX.useFusedCanCoder(canCoder, 6.75, 2 * Math.PI * wheelRadiusMeters);