Intake Example (Basic)
Template Project
Use the Timed Robot WPILib template so Robot.java already defines the lifecycle methods.
Overview
Configure one intake motor in Robot.java and run it forward on A, reverse on B, and stop when neither is pressed.
Requirements
The intake must:
- Run forward (collect) on ‘A’ press.
- Run reverse (eject) on ‘B’ press.
- Stop when no button is pressed.
- Be configured (Coast mode, Current Limit).
Step 1: Imports and Class Setup
Add imports for TimedRobot, the gamepad, and your motor vendor classes. The template already declares public class Robot extends TimedRobot.
package frc.robot;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.XboxController;
import com.ctre.phoenix6.hardware.TalonFX;
import com.ctre.phoenix6.configs.TalonFXConfiguration;
import com.ctre.phoenix6.signals.InvertedValue;
import com.ctre.phoenix6.signals.NeutralModeValue;
import com.ctre.phoenix6.controls.DutyCycleOut;
public class Robot extends TimedRobot {
// The template provides all lifecycle methods
// We'll add fields here next
}
Step 2: Declare Hardware
Declare the controller on USB port 1 and the intake motor on CAN ID 5.
public class Robot extends TimedRobot {
// Controller on USB port 1
private XboxController m_controller = new XboxController(1);
// Intake motor on CAN ID 5
private TalonFX m_motor = new TalonFX(5);
private DutyCycleOut m_out = new DutyCycleOut(0);
}
Step 3: Configure Motor in robotInit()
In robotInit(), set inversion, Coast idle mode (free spin at zero output), and a 30 A current limit.
@Override
public void robotInit() {
// Configure motor settings
TalonFXConfiguration config = new TalonFXConfiguration();
// Set motor direction (adjust if needed for your mechanism)
config.MotorOutput.Inverted = InvertedValue.CounterClockwise_Positive;
// Coast mode allows free spinning when stopped (good for intakes)
config.MotorOutput.NeutralMode = NeutralModeValue.Coast;
// Enable current limiting to protect motor (30A limit)
config.CurrentLimits.StatorCurrentLimitEnable = true;
config.CurrentLimits.StatorCurrentLimit = 30.0;
m_motor.getConfigurator().apply(config);
}Step 4: Add Button Control Logic
In teleopPeriodic(), read A and B and set motor output for collect, eject, or stop.
@Override
public void teleopPeriodic() {
// Button A: Run intake forward (collect)
if (m_controller.getAButton()) {
m_motor.setControl(m_out.withOutput(0.6));
}
// Button B: Run intake reverse (eject)
else if (m_controller.getBButton()) {
m_motor.setControl(m_out.withOutput(-0.4));
}
// No button: Stop motor
else {
m_motor.setControl(m_out.withOutput(0.0));
}
}Understanding the Control Logic
Only one branch runs at a time. Forward uses 60% output; reverse uses 40%. Tune both on your mechanism.
Power Values
0.6 and -0.4 are starting points—adjust after testing on the real intake.
Full Code
package frc.robot;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.XboxController;
import com.ctre.phoenix6.hardware.TalonFX;
import com.ctre.phoenix6.configs.TalonFXConfiguration;
import com.ctre.phoenix6.signals.InvertedValue;
import com.ctre.phoenix6.signals.NeutralModeValue;
import com.ctre.phoenix6.controls.DutyCycleOut;
public class Robot extends TimedRobot {
// Controller on USB port 1
private XboxController m_controller = new XboxController(1);
// Intake motor on CAN ID 5
private TalonFX m_motor = new TalonFX(5);
private DutyCycleOut m_out = new DutyCycleOut(0);
// The template provides robotInit() - add your configuration here
@Override
public void robotInit() {
// Configure motor settings
TalonFXConfiguration config = new TalonFXConfiguration();
// Set motor direction (adjust if needed for your mechanism)
config.MotorOutput.Inverted = InvertedValue.CounterClockwise_Positive;
// Coast mode allows free spinning when stopped (good for intakes)
config.MotorOutput.NeutralMode = NeutralModeValue.Coast;
// Enable current limiting to protect motor (30A limit)
config.CurrentLimits.StatorCurrentLimitEnable = true;
config.CurrentLimits.StatorCurrentLimit = 30.0;
m_motor.getConfigurator().apply(config);
}
// The template provides teleopPeriodic() - add your control logic here
@Override
public void teleopPeriodic() {
// Button A: Run intake forward (collect)
if (m_controller.getAButton()) {
m_motor.setControl(m_out.withOutput(0.6));
}
// Button B: Run intake reverse (eject)
else if (m_controller.getBButton()) {
m_motor.setControl(m_out.withOutput(-0.4));
}
// No button: Stop motor
else {
m_motor.setControl(m_out.withOutput(0.0));
}
}
// The template provides other lifecycle methods (autonomousInit, disabledInit, etc.)
// You can leave them empty or add code as needed
}Troubleshooting
Wrong Direction: Change inversion setting in robotInit().
No Movement: Check CAN IDs and power values.
Motor Drifts: Ensure the else \{ stop \} block exists to stop the motor when no button is pressed.