LogoLogo
REVLib Docs
  • REV ION Brushless
  • Quick Links
  • Frequently Asked Questions
  • NEO Brushless Motors
    • Brushless DC Motor Basics
    • NEO Vortex
      • Docking a SPARK Flex
      • Vortex Shafts
      • Installing a Shaft
      • NEO Vortex Solo Adapter
    • NEO V1.1
      • NEO V1
      • Pinion Pressing Guides
    • NEO 550
      • Pinion Pressing Guide
    • Dynamometer Testing
    • Motor Comparison
  • SPARK Flex Motor Controller
    • SPARK Flex Overview
      • SPARK Flex Dock
    • SPARK Flex Specifications
    • SPARK Flex Feature Description
      • Power and Motor Connections
      • Control Connections
      • Data Port
      • Mounting Holes
      • Control Interfaces
      • Mode Button
      • Operating Modes
    • SPARK Flex Getting Started
      • Wiring the SPARK Flex
      • Make it Spin!
      • Basic Configurations
    • SPARK Flex Status LED Patterns
    • SPARK Flex Troubleshooting
    • SPARK Flex Operating Modes
  • SPARK MAX Motor Controller
    • SPARK MAX Overview
    • SPARK MAX Specifications
      • Power and Motor Connections
      • Control Connections
      • Encoder Port
      • Data Port
    • SPARK MAX Getting Started
      • Wiring the SPARK MAX
      • Make it Spin!
      • Basic Configurations
    • SPARK MAX Status LED Patterns
    • SPARK MAX Troubleshooting
    • SPARK MAX Operating Modes
    • SPARK MAX Control Interfaces
    • SPARK MAX Configuration Parameters
    • Using Encoders with the SPARK MAX
      • Absolute Encoders
      • Alternate Encoder Mode
      • Securing the Encoder Adapters
      • Calibration for MAXSwerve
  • REVLib
    • REVLib Overview
      • REVLib Changelog
      • Migrating to REVLib 2025
    • Closed-Loop Control Overview
      • Closed Loop Control Getting Started
      • Getting Started with PID Tuning
      • Position Control Mode
      • Velocity Control Mode
      • Current Control Mode
      • Smart Motion Control
      • Smart Velocity Control
    • Code Examples
    • Migrating to REVLib
    • Device Firmware Changelogs
  • Tips and Tricks
    • Anderson Powerpole Connectors
    • REV Hardware Client Documentation
  • Legacy Documentation
    • SPARK Motor Controller
    • SPARK MAX Client
      • Navigating the SPARK MAX Client
      • Updating Device Firmware
      • Recovery Mode with the SPARK MAX Client
      • SPARK MAX Client Troubleshooting
Powered by GitBook
On this page
  • Setting up Closed-Loop Control
  • PID Constants and Configuration
  • Slots
  • PID Parameters
  • F Parameter
  • Smart Motion Parameters

Was this helpful?

Export as PDF
  1. REVLib
  2. Closed-Loop Control Overview

Closed Loop Control Getting Started

PreviousClosed-Loop Control OverviewNextGetting Started with PID Tuning

Last updated 4 months ago

Was this helpful?

January 4, 2025 Update -

Documentation for REVLib 2025 can now be found at:

Please bookmark our new page as we transition over and add more information there!

Setting up Closed-Loop Control

Closed-loop control in REVLib is accessed through the SPARK's PID Controller object. This object is specific to each motor and tracks the PID gains and closed-loop constants. It also contains all the methods needed to configure and control your motor. It can be accessed as shown below:

// initialize the motor (Flex/MAX are setup the same way)
CANSparkFlex m_motor = new CANSparkFlex(deviceID, MotorType.kBrushless);

// initialize the PID controller
SparkPIDController m_pidController = m_motor.getPIDController();

API Docs: ,

// initialize the motor (Flex/MAX are setup the same way)
rev::CANSparkMax m_motor{deviceID, rev::CANSparkMax::MotorType::kBrushless};

// initialize the PID Controller
rev::SparkPIDController m_pidController = m_motor.GetPIDController();

API Docs: ,

To drive your motor in a Closed-Loop control mode, address the PID Controller object and give it a set point (a target in whatever units are required by your control mode: , , or ) and a control mode as shown below:

This will run your motor in the provided mode, but it won't move until you've configured the

// Set the setpoint of the PID controller in raw position mode
m_pidController.setReference(setPoint, CANSparkBase.ControlType.kPosition);

API Docs: ,

// Set the setpoint of the PID controller in raw position mode
m_pidController.SetReference(SetPoint, rev::CANSparkBase::ControlType::kPosition);

API Docs: ,

The provided example above runs the motor in position control mode, which is just a conventional PID loop reading the motor's current position from the configured encoder and taking a setpoint in rotations.

Use caution when running motors in Closed-Loop modes, as they may move very quickly and unexpectedly if improperly tuned.

PID Constants and Configuration

To run a PID loop, several constants are required. For a more advanced controller, even more parameters need set and tuned.

Slots

The SPARK MAX and SPARK Flex each have 4 closed-loop slots, each with their own set of constants. These slots are numbered 0-3. You can pass the slot number as an argument to each of the parameter setting methods. When applying the setpoint, pass the slot number and the motor controller will switch to the appropriate config.

PID Parameters

These gains can be configured on the SparkPIDController object as shown below:

// Set the P value of the PID controller on slot 0 to 0.25
m_pidController.SetP(kP);
m_pidController.SetI(kI);
m_pidController.SetD(kD);
m_pidController.SetIZone(kIz);

// Set the minimum and maximum outputs of the motor [-1, 1]
m_pidController.SetOutputRange(kMinOutput, kMaxOutput);
// Set the P value of the PID controller on slot 0 to 0.25
m_pidController.SetP(kP);
m_pidController.SetI(kI);
m_pidController.SetD(kD);
m_pidController.SetIZone(kIz);

// Set the minimum and maximum outputs of the motor [-1, 1]
m_pidController.SetOutputRange(kMinOutput, kMaxOutput);

F Parameter

The SPARK family of motor controllers also offer an F term, which is a velocity feed-forward. This is unique to each type of motor, and can be calculated by taking the reciprocal of the motor's velocity constant (Kv), in other words 1/Kv.

For a NEO Vortex, this value is 1/565. This is only needed when running a velocity-based control loop (velocity mode, Smart Motion, and Smart Velocity). The F parameter can be set as seen below:

// Set kFF
m_pidController.SetFF(1/Kv);
// Set kFF
m_pidController.SetFF(1/Kv);

The F parameter should only be set when using a velocity-based PID controller, and should be set to zero otherwise to avoid unwanted behavior.

Smart Motion Parameters

There are another set of parameters available for the SparkPIDController, which only apply to Smart Motion and Smart Velocity. These allow you to configure and tune the motion profiles generated by Smart Motion. They can be set as seen below:

// Set Smart Motion / Smart Velocity parameters
int smartMotionSlot = 0;
m_pidController.setSmartMotionMaxVelocity(maxVel, smartMotionSlot);
m_pidController.setSmartMotionMinOutputVelocity(minVel, smartMotionSlot);
m_pidController.setSmartMotionMaxAccel(maxAcc, smartMotionSlot);
m_pidController.setSmartMotionAllowedClosedLoopError(allowedErr, smartMotionSlot);
// Set Smart Motion / Smart Velocity parameters
m_pidController.SetSmartMotionMaxVelocity(maxVel);
m_pidController.SetSmartMotionMinOutputVelocity(minVel);
m_pidController.SetSmartMotionMaxAccel(maxAcc);
m_pidController.SetSmartMotionAllowedClosedLoopError(allowedErr);

Maximum Velocity is in units of Revolutions per Minute (RPM)

Maximum Acceleration is in units of RPM per Second (RPM/s)

These parameters have no effect on modes other than Smart Motion and Smart Velocity

A PID controller has 3 core parameters or gains. For more information on these gains and how to tune them, see .

API Docs:

API Docs:

The Kv values for the NEO family of Brushless Motors are documented within each motor's specifications table: , ,

API Docs:

API Docs:

API Docs:

API Docs:

Getting Started with PID Tuning
SparkPIDController
SparkPIDController
NEO Vortex
NEO V1.1
NEO 550
SparkPIDController
SparkPIDController
SparkPIDController
SparkPIDController
https://docs.revrobotics.com/revlib
CANSparkFlex
SparkPIDController
CANSparkMax
SparkPIDController
setReference
ControlType
SetReference
ControlType
PID constants.