After creating a robot project, you need to import the motor controller libraries and declare motors. The following creates a Falcon 500 motor controllers called motor 1 in a class called Shooter. The motor has previously been assigned CAN ID 2.
If using talonSRX's, replace TalonFX with TalonSRX. .
package frc.robot;
import com.ctre.phoenix.motorcontrol.can.TalonFX;
public class Shooter{
TalonFX motor1 = new TalonFX(2);
Shooter(){
motor1.configFactoryDefault();
}
To run the motor, use
br.set(ControlMode.mode, value)
where mode is PercentOutput, Velocityt, Position, or one of the other available control modes. Value is the motor output scaled from -1 to +1. Use PercentOutput unless you are using feedback from a sensor. The value can come from a joystick input or set in the program.
There are a number of setting we want to apply, which can be done in the constructor. The settings you need depend on what you are doing. The drive system motors need all of these. A motor that just spins a shooter wheel with no encoder feedback may just need to have factory default settings applied.
bl.config_kP(0, 0.2);
// restore factory default settings
br.configFactoryDefault();
fr.configFactoryDefault();
bl.configFactoryDefault();
fl.configFactoryDefault();
// Set Brake Mode;
bl.setNeutralMode(NeutralMode.Brake);
br.setNeutralMode(NeutralMode.Brake);
fl.setNeutralMode(NeutralMode.Brake);
fr.setNeutralMode(NeutralMode.Brake);
// set Voltage Compensation Mode
br.configVoltageCompSaturation(12);
bl.configVoltageCompSaturation(12);
fr.configVoltageCompSaturation(12);
bl.configVoltageCompSaturation(12);
// invert the right side
br.setInverted(true);
fr.setInverted(true);
bl.setInverted(false);
fl.setInverted(false);
// set the sensor phase (repeat for other motors as needed.
bl.setSensorPhase(true);
br.setSensorPhase(true);
// front follows back
fr.follow(br);
fl.follow(bl);
// set closed loop period for PID loop 0 to 1 millisecond
bl.configClosedLoopPeriod(0, 1,20);
br.configClosedLoopPeriod(0, 1,20);
// Set relevant frame periods */ (from CTRE Githib examples)
br.setStatusFramePeriod(StatusFrame.Status_12_Feedback1, 20, 20);
br.setStatusFramePeriod(StatusFrame.Status_13_Base_PIDF0, 20, 20);
br.setStatusFramePeriod(StatusFrame.Status_14_Turn_PIDF1, 20,20);
br.setStatusFramePeriod(StatusFrame.Status_10_Targets, 20,20);
br.setStatusFramePeriod(StatusFrame.Status_2_Feedback0, 5, 20);
gyro.setStatusFramePeriod(PigeonIMU_StatusFrame.CondStatus_9_SixDeg_YPR , 5, 20);
// load PID gains into slot 0, repeat for other motors as needed
bl.config_kP(0, 0.2);
bl.config_kI(0, 0);bl.config_kD(0, 0);
bl.config_IntegralZone(0, 50);
bl.config_kF(0, 2.15);
bl.configClosedLoopPeakOutput(0,1);
// use the velocity PID gains in slot 0 for PID loop 0
br.selectProfileSlot(0, 0);
bl.selectProfileSlot(0, 0);