Motion Profiling: Exponential
Template Project
Start with the Command Robot template. Motion profiling lives in subsystems; add a new class under subsystems.
Overview
Exponential profiling ramps acceleration gradually instead of stepping it at fixed rates. Velocity follows a smooth curve rather than the flat-top trapezoid you get from constant-accel segments. That reduces jerk on elevators and arms when you care about smooth motion.
What is Exponential Profiling?
Acceleration changes continuously, so the velocity plot curves instead of meeting sharp corners at cruise speed. CTRE implements this as Motion Magic Expo on Talon FX; REV teams often use WPILib’s ExponentialProfile with on-controller PID.
Hardware Support
Built-in controller features:
- SPARK MAX: Use WPILib’s
ExponentialProfileclass with PID control. - CTRE (Talon FX): Uses
Motion Magic® Expo(built-in exponential profile).
Step 1: Understand Exponential Profile Parameters
You need feedforward and a speed cap: kV (volts per unit velocity), kA (volts per unit acceleration), and kMaxV (maximum velocity for the profile).
Step 2: Configure Exponential Profile (Talon FX)
Set Motion Magic Expo constants on the Talon FX. They work together with slot PID gains.
Configure exponential profile for Talon FX:
public void configureExponentialProfile() {
// Configure Motion Magic® Exponential profile
MotionMagicConfigs mm = new MotionMagicConfigs();
mm.MotionMagicExpo_kV = 0.12; // Velocity feedforward
mm.MotionMagicExpo_kA = 0.01; // Acceleration feedforward
motor.getConfigurator().apply(mm);
}
Step 3: Use Exponential Profile Control (Talon FX)
After configuration, command position with MotionMagicExpoVoltage. The controller generates the profile.
Set position using exponential profile:
public void setPosition(double target) {
// Use exponential motion profile
motor.setControl(new MotionMagicExpoVoltage(target));
}
Step 4: Configure Exponential Profile (SPARK MAX with WPILib)
On SPARK MAX, run WPILib ExponentialProfile in your subsystem periodic() and feed each setpoint to closed-loop position control.
Step 4a: Create Profile Constraints
Define kMaxV, kV, kA and build constraints:
// Define profile constraints from system characteristics
private static final double kMaxV = 10.0; // Max velocity
private static final double kV = 0.12; // Velocity feedforward
private static final double kA = 0.01; // Acceleration feedforward
private ExponentialProfile.Constraints constraints =
ExponentialProfile.Constraints.fromCharacteristics(kMaxV, kV, kA);
private ExponentialProfile profile = new ExponentialProfile(constraints);
Step 4b: Calculate Profile Setpoints
Each loop, call profile.calculate with timestep, current position, and goal. Apply the returned position setpoint to the motor controller.
Calculate profile setpoint each loop:
@Override
public void periodic() {
// Calculate next setpoint from profile
double dt = 0.02; // 20ms loop time
double current = getCurrentPosition();
double goal = getTargetPosition();
var state = profile.calculate(dt, current, goal);
// Apply position setpoint to PID controller
pid.setReference(state.position, ControlType.kPosition);
}
Tuning Exponential Profiles
Tune feedforward (kG, kV, kA), then kP and kD. Use simulation, Elastic, or AdvantageScope and graph position and velocity together.
Feedforward Tuning
Physical forces:
- kG (Gravity): Set setpoint high, increase kG from 0. Find value where mechanism just starts to creep upward, then back off slightly.
- kV (Velocity): Run at fixed duty cycle, observe voltage and max velocity. kV = Voltage / Max Velocity (RPS). Use first 4-5 decimal places.
- kA (Acceleration): Trigger movement, measure time from 0 to max velocity. kA = kV / Time to Max Velocity.
Feedback Tuning
Position correction:
- kP (Proportional): Start small (0.1). Double values until mechanism moves. Too low = slow/sluggish curve. Too high = overshoot/oscillation. Ideal = straight line to setpoint.
- kD (Derivative): Start tiny (0.005-0.05). Increase to dampen oscillations. Too high = high-frequency jitter/noise causing motor heating. Goal = smooth flat line.
Tuning Goals
On the position graph you want a fast rise, a steady hold, and a fast return without ringing at the corners. If the mechanism hits hard stops or oscillates, back off kP or kD. Sim values are a starting point; friction and load on the real robot will differ.
Full Code Example
package frc.robot.subsystems;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import com.ctre.phoenix6.configs.MotionMagicConfigs;
import com.ctre.phoenix6.controls.MotionMagicExpoVoltage;
import com.ctre.phoenix6.hardware.TalonFX;
// Create this class in the subsystems package (template provides ExampleSubsystem as reference)
public class Elevator extends SubsystemBase {
private TalonFX motor = new TalonFX(1);
public Elevator() {
configureExponentialProfile();
}
public void configureExponentialProfile() {
// Configure Motion Magic® Exponential profile
MotionMagicConfigs mm = new MotionMagicConfigs();
mm.MotionMagicExpo_kV = 0.12; // Velocity feedforward
mm.MotionMagicExpo_kA = 0.01; // Acceleration feedforward
motor.getConfigurator().apply(mm);
}
public void setPosition(double target) {
// Use exponential motion profile
motor.setControl(new MotionMagicExpoVoltage(target));
}
}