Motion Profiling: Trapezoidal
Template Project
Start with the Command Robot template and add a subsystem under subsystems.
Overview
Trapezoidal profiling moves in three phases: accelerate at a fixed rate, cruise at max velocity, then decelerate at a fixed rate. It is faster than commanding a bare position setpoint and easier to reason about than exponential curves.
What is Trapezoidal Profiling?
Velocity vs time forms a trapezoid: ramps up, flat top, ramps down. Acceleration is piecewise constant (not smooth at the phase boundaries unless you add jerk limits on Talon FX).
Hardware Support
Built-in controller features:
- REV (SPARK MAX): Uses
MAXMotion(Trapezoidal). - CTRE (Talon FX): Uses
Motion Magic®(Trapezoidal/S-Curve).
Step 1: Understand Configuration Parameters
Max velocity caps cruise speed. Max acceleration sets how quickly you enter and leave cruise. On Talon FX, optional jerk limits how abruptly acceleration can change.
Step 2: Configure Motion Profile Constraints
Set limits in motor config. Talon FX often uses rotations per second (RPS) and RPS²; SPARK MAX MAXMotion may use RPM and RPM/s in the config API. Start conservative and increase while watching the graph.
public void configureMotionProfile() {
MotionMagicConfigs mm = new MotionMagicConfigs();
// Max velocity in rotations/sec
mm.MotionMagicCruiseVelocity = 10;
// Max acceleration in rotations/sec²
mm.MotionMagicAcceleration = 20;
// Optional: Jerk limit for smoothing (rotations/sec³)
mm.MotionMagicJerk = 100;
motor.getConfigurator().apply(mm);
}Step 3: Use Motion Profile Control
Command position with Motion Magic or MAXMotion position mode. The controller plans the trapezoid from current position to the target.
private final MotionMagicVoltage m_request = new MotionMagicVoltage(0);
public void setPosition(double target) {
// Use Motion Magic® position control
motor.setControl(m_request.withPosition(target));
}Tuning Trapezoidal Profiles
Find the fastest profile that still looks clean on the velocity graph. Raise limits until you see overshoot, vibration, or brownouts, then back off.
Tuning Process
Follow this order:
- 1. Find Maximum Velocity: Set acceleration very high, gradually increase velocity until the mechanism struggles to maintain speed or hits physical limits. Back off 10-20% for safety.
- 2. Find Maximum Acceleration: Keep velocity at your maximum, increase acceleration until you see overshoot, vibration, or mechanical stress. Reduce by 15-25%.
- 3. Balance for Performance: If movement is too slow, increase both values proportionally. If there’s overshoot or vibration, reduce acceleration first.
Tuning Tips
Plot position, velocity, and current while the mechanism moves. Velocity should look like a trapezoid. Overshoot at the end often means acceleration or kP is high. Sluggish motion means limits are low or PID is weak.
Common Issues and Solutions
Overshoot at end: Lower acceleration, add kD, or enable jerk on Talon FX. Too slow: Raise velocity and acceleration together if mechanics and current draw allow. Vibration: Lower acceleration; check loose mechanisms. Never reaches target: Raise max velocity or shorten the move so the profile can finish in time.
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.MotionMagicVoltage;
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);
private final MotionMagicVoltage m_request = new MotionMagicVoltage(0);
public Elevator() {
configureMotionProfile();
}
public void configureMotionProfile() {
// Configure Motion Magic® (trapezoidal profile)
MotionMagicConfigs mm = new MotionMagicConfigs();
mm.MotionMagicCruiseVelocity = 10; // Max velocity in rotations/sec
mm.MotionMagicAcceleration = 20; // Max acceleration in rotations/sec²
mm.MotionMagicJerk = 100; // Jerk limit for smoothing
motor.getConfigurator().apply(mm);
}
public void setPosition(double target) {
// Use Motion Magic® position control
motor.setControl(m_request.withPosition(target));
}
}