Introduction to Motors
Overview
Motors drive your robot's movement. In FRC, you will primarily use motor controllers from CTRE (Talon FX) and REV (SPARK MAX). This lesson covers creating and controlling these motors in
Robot.java.Objectives
You will learn to:
- Create motor instances in code.
- Control motor speed and direction.
- Stop motors safely.
- Work with CTRE and REV hardware.
Robot.java Lifecycle
robotInit(): Runs once at startup. Create your motors here.
teleopPeriodic(): Runs repeatedly during driver control. Update motor outputs here.
teleopPeriodic(): Runs repeatedly during driver control. Update motor outputs here.
CTRE: Talon FX
The Talon FX is a high-performance motor controller with a built-in encoder. It requires the CTRE Phoenix 6 library.
Talon FX Example
package frc.robot;
import edu.wpi.first.wpilibj.TimedRobot;
import com.ctre.phoenix6.hardware.TalonFX;
import com.ctre.phoenix6.controls.DutyCycleOut;
public class Robot extends TimedRobot {
// Create motor instance with CAN ID 1
private TalonFX m_motor = new TalonFX(1);
// Create control request for duty cycle output
private DutyCycleOut m_output = new DutyCycleOut(0);
@Override
public void teleopPeriodic() {
// Set motor to 50% speed forward (range: -1.0 to 1.0)
m_motor.setControl(m_output.withOutput(0.5));
}
}REV: SPARK MAX
The SPARK MAX supports both brushless (NEO) and brushed motors. It requires the REVLib library.
SPARK MAX Example
package frc.robot;
import edu.wpi.first.wpilibj.TimedRobot;
import com.revrobotics.spark.SparkMax;
import com.revrobotics.spark.SparkLowLevel.MotorType;
public class Robot extends TimedRobot {
// Create motor instance: CAN ID 1, Brushless motor type
private SparkMax m_motor = new SparkMax(1, MotorType.kBrushless);
@Override
public void teleopPeriodic() {
// Set motor to 50% speed forward (range: -1.0 to 1.0)
m_motor.set(0.5);
}
}Control Basics
Speed: Values range from -1.0 (full reverse) to 1.0 (full forward). 0.0 stops the motor.
Multiple Motors: Create separate instances for each motor (e.g., left and right drive motors).
Multiple Motors: Create separate instances for each motor (e.g., left and right drive motors).
Multiple Motors
Controlling two motors:
package frc.robot;
import edu.wpi.first.wpilibj.TimedRobot;
import com.ctre.phoenix6.hardware.TalonFX;
import com.ctre.phoenix6.controls.DutyCycleOut;
public class Robot extends TimedRobot {
// Create left and right motors with different CAN IDs
private TalonFX m_left = new TalonFX(1);
private TalonFX m_right = new TalonFX(2);
private DutyCycleOut m_output = new DutyCycleOut(0);
@Override
public void teleopPeriodic() {
// Set both motors to 50% speed
m_left.setControl(m_output.withOutput(0.5));
m_right.setControl(m_output.withOutput(0.5));
}
}Key Points
Remember:
- Create motors as private fields.
- Use unique CAN IDs.
- Talon FX uses
setControl. - SPARK MAX uses
set. - Range is -1.0 to 1.0.