Skip to content
learnFRC.org
programmingexample

REVLib Spark Subsystem (Spark Max & Spark Flex)

Production REVLib brushless motor template featuring SparkMax and SparkFlex configuration, smart current limiting, soft limits, and closed-loop control.

This template provides a standard, production-ready subsystem pattern for REV Robotics NEO and NEO Vortex brushless motors driven by SPARK MAX or SPARK Flex controllers using modern REVLib. It encapsulates hardware handles, applies persistent flash configurations with safe resets, enforces smart current and soft limits, and integrates WPILib Units for setpoints.

ParameterValuePurpose
Smart Current Limit40A continuous (20A to 60A typical)Prevents NEO thermal runaway and PDP/PDH brownouts
Idle ModeBrake modeResists external backdrive when command terminates
Persist ModekPersistParametersSaves settings through brownout reboots
Feedback LoopPrimary internal hall encoder42 counts/rev (NEO) or integrated Vortex encoder
package frc.robot.subsystems;
import com.revrobotics.spark.SparkBase.ControlType;
import com.revrobotics.spark.SparkBase.PersistMode;
import com.revrobotics.spark.SparkBase.ResetMode;
import com.revrobotics.spark.SparkClosedLoopController;
import com.revrobotics.spark.SparkLowLevel.MotorType;
import com.revrobotics.spark.SparkMax;
import com.revrobotics.spark.config.ClosedLoopConfig.FeedbackSensor;
import com.revrobotics.spark.config.SparkBaseConfig.IdleMode;
import com.revrobotics.spark.config.SparkMaxConfig;
import edu.wpi.first.units.measure.Angle;
import static edu.wpi.first.units.Units.*;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
public class SparkMechanismSubsystem extends SubsystemBase {
private static final int CAN_ID = 15;
private static final double GEAR_RATIO = 25.0; // 25:1 reduction to mechanism shaft
private final SparkMax m_motor;
private final SparkClosedLoopController m_closedLoop;
public SparkMechanismSubsystem() {
m_motor = new SparkMax(CAN_ID, MotorType.kBrushless);
m_closedLoop = m_motor.getClosedLoopController();
configureHardware();
setDefaultCommand(holdCurrentPositionCommand());
}
private void configureHardware() {
SparkMaxConfig config = new SparkMaxConfig();
// 1. Idle mode and current limiting
config.idleMode(IdleMode.kBrake);
config.smartCurrentLimit(40); // 40A smart stator limit
// 2. Unit scaling: convert motor rotations to mechanism output rotations
config.encoder
.positionConversionFactor(1.0 / GEAR_RATIO)
.velocityConversionFactor((1.0 / GEAR_RATIO) / 60.0); // Mech rot/s
// 3. Software travel limits (in mechanism rotations)
config.softLimit
.reverseSoftLimit(0.0)
.reverseSoftLimitEnabled(true)
.forwardSoftLimit(1.5) // 1.5 mechanism rotations max travel
.forwardSoftLimitEnabled(true);
// 4. Closed-loop position gains
config.closedLoop
.feedbackSensor(FeedbackSensor.kPrimaryEncoder)
.p(2.4)
.i(0.0)
.d(0.1)
.outputRange(-1.0, 1.0);
// 5. Apply configuration persistently, resetting any dirty stale states
m_motor.configure(config, ResetMode.kResetSafeParameters, PersistMode.kPersistParameters);
}
@Override
public void periodic() {
double currentPositionRot = m_motor.getEncoder().getPosition();
SmartDashboard.putNumber("SparkMech/Position_Rot", currentPositionRot);
SmartDashboard.putNumber("SparkMech/Angle_Deg", currentPositionRot * 360.0);
SmartDashboard.putNumber("SparkMech/Current_A", m_motor.getOutputCurrent());
SmartDashboard.putNumber("SparkMech/Temp_C", m_motor.getMotorTemperature());
}
public Command moveToAngleCommand(Angle targetAngle) {
double targetMechRotations = targetAngle.in(Rotations);
return run(() -> m_closedLoop.setReference(targetMechRotations, ControlType.kPosition))
.until(() -> isAtAngle(targetAngle))
.withName("MoveToAngle");
}
public boolean isAtAngle(Angle targetAngle) {
double currentAngleDeg = m_motor.getEncoder().getPosition() * 360.0;
return Math.abs(currentAngleDeg - targetAngle.in(Degrees)) < 3.0; // 3 degree tolerance
}
public Command holdCurrentPositionCommand() {
return run(() -> {
double currentMechRotations = m_motor.getEncoder().getPosition();
m_closedLoop.setReference(currentMechRotations, ControlType.kPosition);
}).withName("HoldPosition");
}
public Command stopCommand() {
return runOnce(m_motor::stopMotor).withName("StopMechanism");
}
}
  • Volatile Configuration Loss: Calling setter methods directly on SparkMax rather than configuring via SparkMaxConfig and PersistMode.kPersistParameters causes settings to wipe if the motor controller browns out mid-match.
  • Sensor Phasing on Custom Gearboxes: If the motor and mechanism travel in opposite directions, the software soft limits will trip immediately or fail to trigger. Always verify positive voltage creates positive encoder velocity.
  • NEO Stator Burnout Without Limits: The NEO 550 and standard NEO will smoke within 3 seconds when stalled at full battery voltage without a smart current limit. Always keep NEO 550 current limits at 20A or lower, and NEO limits at 40A to 50A.
  • Encoder Multiplier Units Conflict: Ensure positionConversionFactor matches the units expected by your PID setpoints. If setpoints use mechanism rotations, ensure soft limits use the identical mechanism rotations scale.
Follower Controllers in REVLib

When configuring a follower motor on REVLib, create a separate SparkMaxConfig for the follower, configure config.follow(leaderMotor, isInverted), and apply it using followerMotor.configure(config, ...). Never call follower commands directly in user code.

  1. Verify Identification and Firmware: Confirm the SPARK MAX or SPARK Flex communicates on the CAN bus via REV Hardware Client and runs the latest season firmware.
  2. Validate Direction Under Open Loop: Run the motor slowly forward using open-loop duty cycle (0.10) and verify mechanism moves in the positive intended direction and encoder position increments positively.
  3. Trigger Forward and Reverse Soft Limits: Command motion past the software limits with light manual pressure or low power. Confirm the controller cuts output in the limit direction while permitting reverse recovery.
  4. Thermal and Stall Current Sanity Check: Command a hard stop against a mechanical barrier for 1 second. Confirm current peaks at the configured 40A threshold without exceeding breaker trip curves.

Verified Sources & Standards

Evergreen Standard Verified: Oct 1, 2026