Motion Magic

Motion Magic - Profiled Motion Control

Motion Magic builds on PID control by adding smooth acceleration and deceleration profiles. This prevents jerky movements and reduces mechanical stress while maintaining precise positioning.

🎯 Key Concept: Motion Magic automatically generates smooth velocity profiles to reach target positions with controlled acceleration.

Understanding Motion Magic Profiles

πŸ“ˆ Trapezoidal Profile

Motion Magic creates a trapezoidal velocity profile with three phases:

1. Acceleration: Ramp up to cruise velocity
2. Cruise: Maintain constant max velocity
3. Deceleration: Smoothly brake to target

βš™οΈ Key Parameters

Motion Magic Cruise Velocity

Maximum velocity during cruise phase (rotations/second)

Motion Magic Acceleration

Rate of acceleration/deceleration (rotations/secondΒ²)

Motion Magic Jerk

Rate of change of acceleration (rotations/secondΒ³)

πŸ“š Official Motion Magic Documentation

For complete Motion Magic reference, configuration examples, and advanced tuning techniques:

πŸ“– CTRE Motion Magic API Reference

Motion Magic Implementation in Code

πŸ”§ Motion Magic Configuration Example
Motion Magic Setup in Subsystem ConstructorJAVA
1// In your subsystem constructor
2public ArmSubsystem() {
3    TalonFXConfiguration config = new TalonFXConfiguration();
4    
5    // PID Configuration (same as before)
6    Slot0Configs slot0 = config.Slot0;
7    slot0.kP = 24.0;    // Proportional gain
8    slot0.kI = 0.0;     // Integral gain  
9    slot0.kD = 0.1;     // Derivative gain
10    
11    // Feedforward gains
12    slot0.kS = 0.25;    // Static friction compensation
13    slot0.kG = 0.12;    // Gravity compensation
14    slot0.kV = 0.12;    // Velocity feedforward
15    slot0.kA = 0.01;    // Acceleration feedforward
16    
17    // Motion Magic Configuration
18    MotionMagicConfigs motionMagic = config.MotionMagic;
19    motionMagic.MotionMagicCruiseVelocity = 2.0;    // 2 rot/s max velocity
20    motionMagic.MotionMagicAcceleration = 8.0;      // 8 rot/sΒ² acceleration
21    motionMagic.MotionMagicJerk = 80.0;             // 80 rot/sΒ³ jerk limit
22    
23    motor.getConfigurator().apply(config);
24    
25    // Create Motion Magic control request
26    motionMagicRequest = new MotionMagicVoltage(0).withSlot(0);
27}
28
29// Method to set target position with Motion Magic
30public void setTargetPosition(double positionRotations) {
31    motor.setControl(motionMagicRequest.withPosition(positionRotations));
32}
33
34

Workshop Implementation: Motion Magic

πŸ”„ Before β†’ After: Implementation

πŸ“‹ Before

  • β€’ PID position control with PositionVoltage
  • β€’ Instant acceleration to target
  • β€’ Potential mechanical stress from jerky movements
  • β€’ No velocity planning or profiling
  • β€’ Abrupt start/stop motions

βœ… After

  • β€’ Motion Magic profiled motion with MotionMagicVoltage
  • β€’ Smooth acceleration and deceleration curves
  • β€’ Reduced mechanical stress and wear
  • β€’ Configurable cruise velocity and acceleration
  • β€’ Professional, smooth motion profiles

Loading file...

πŸ” Code Walkthrough

Motion Magic Parameters:

  • β€’ Cruise Velocity (2.0): Maximum speed during motion
  • β€’ Acceleration (8.0): How quickly to reach cruise speed
  • β€’ Jerk (80.0): Smoothness of acceleration changes
  • β€’ MotionMagicVoltage: Replaces PositionVoltage for profiled control

Enhanced Features:

  • β€’ Setpoint Detection: Checks both position AND velocity
  • β€’ Smooth Motion: Eliminates jerky arm movements
  • β€’ Mechanical Safety: Reduces stress on gearboxes
  • β€’ Predictable Timing: Known motion duration

πŸ’‘ Next Step: Motion Magic gives us professional-grade position control! Next, we'll explore tuning methods to get optimal performance from our control algorithms.

βš–οΈ Motion Magic vs Basic PID

When to Use Basic PID:

  • Simple positioning tasks
  • Continuous control (like maintaining angle)
  • When speed of response is critical
  • Mechanisms with very low inertia

When to Use Motion Magic:

  • Large, heavy mechanisms (arms, elevators)
  • When smooth motion is important
  • Preventing mechanical stress
  • Predictable motion timing needed
βš™οΈ Motion Magic Tuning Steps

1. Find Maximum Velocity:

  • Plot velocity without Motion Magic
  • Move mechanism the maximum distance it will travel
  • Record the maximum velocity it reaches
  • Store this value in your code as a constant
  • MAX_VELOCITY = 8.5; // rps from plot

2. Set Motion Magic Parameters:

  • Cruise Velocity: Use 80% of max velocity
  • cruiseVel = MAX_VELOCITY * 0.8
  • Acceleration: Use 4x cruise velocity for smooth motion
  • Acceleration: Use 10x cruise velocity for quicker motion
  • acceleration = cruiseVel * 4.0

πŸ’‘ Why This Method Works:

By measuring actual mechanism performance first, you set realistic motion limits that prevent oscillation and ensure smooth, achievable motion profiles. The 80% cruise velocity provides headroom for control corrections while maintaining good performance.