---
title: Tutorial 44 Motion Planning Algorithms
description: How the firmware decides when to accelerate.
---
Tutorial 44 Motion Planning Algorithms
1. Lesson Header
- Lesson Number: 44
- Level: Master
- Title: Motion Planning Algorithms
- Estimated Duration: 60 Minutes
- Prerequisites: Lesson 42 (Physics), Lesson 20 (Accel)
- What You Will Build: A "Trapezoidal Profile" Generator.
2. Concept Introduction
The Planner.
G-code says "Go to X100 F3000".
Physics says "You can't jump to 3000mm/min instantly."
The Motion Planner breaks the move into 3 phases:
1. Acceleration: Ramp up speed (Constant Accel).
2. Cruise: Constant speed (Feedrate).
3. Deceleration: Ramp down to stop (or corner speed).
This forms a Trapezoid in the Velocity-Time graph.
3. Machine State Explanation
Cornering Speed (Jerk).
If the next move is in the same direction, we don't need to stop.
If it's a 90-degree turn, we must slow down to Jerk_Speed.
The planner looks ahead at the next moves to determine the safe exit speed of the current move.
4. Command Breakdown
- Math:
v = a * t.d = 0.5 * a * t^2. - Lookahead: Analyzing a queue of moves.
5. Minimal Working Example
The 1D Move.
Distance = 100mm. F = 60mm/s. Accel = 100mm/s².
Time to reach F: t = v / a = 60 / 100 = 0.6s.
Dist to reach F: d = 0.5 * 100 * 0.6^2 = 18mm.
Decel is symmetric (18mm).
Cruise Dist: 100 - 18 - 18 = 64mm.
Cruise Time: 64 / 60 = 1.06s.
Total Time: 0.6 + 1.06 + 0.6 = 2.26s.
6. Visual Representation
Interactive preview is available in the interactive reader.
7. Build Exercise
Task: Write a Python script to calculate the exact duration of a G-code file, considering acceleration.
1. Parse G1 moves.
2. For each move, calculate accel_dist, cruise_dist, decel_dist.
3. Handle short moves (Triangle profile: never reach full speed).
4. Assume start/end speed is 0 for simplicity.
Algorithm:
If 2 * accel_dist > total_dist:
// Triangle Profile
v_peak = sqrt(a * total_dist)
t = 2 * v_peak / a
Else:
// Trapezoid Profile
t = (2 * v_target / a) + (cruise_dist / v_target)
8. Deep Insight Section
S-Curve Acceleration.
Trapezoidal acceleration has infinite Jerk (instant change in accel).
This causes vibration.
S-Curve (Bell Curve) acceleration ramps the acceleration itself smoothly.
This is 3rd order motion control. Marlin uses Trapezoidal. TinyG/Klipper use S-Curve or similar smoothing.
9. Common Failure Modes
- Short Segments: If you have 1000 tiny segments (0.1mm), the planner might never reach full speed because it's constantly accelerating/decelerating. This is why "Arc Fitting" (G2/G3) is better than lines.
- Jerk Violation: If you ignore cornering speed, the machine will bang at every vertex.
10. Real-World Application
Industrial Robots.
6-axis arms use complex motion planning to avoid singularities and joint limits while minimizing cycle time.
They optimize the entire path globally, not just locally like 3D printers.
11. Final Clean Version
The Planner Script:
import math
accel = 1000 # mm/s^2
target_v = 100 # mm/s (F6000)
def calculate_time(dist):
# Time to accelerate to target_v
t_accel = target_v / accel
d_accel = 0.5 * accel * t_accel**2
if 2 * d_accel > dist:
# Triangle Profile (Short move)
# d = 0.5 * a * t_half^2 * 2 = a * t_half^2
# t_half = sqrt(d / a)
t_total = 2 * math.sqrt(dist / accel)
return t_total
else:
# Trapezoid Profile
d_cruise = dist - 2 * d_accel
t_cruise = d_cruise / target_v
return 2 * t_accel + t_cruise
# Test
moves = [10, 100, 1] # Distances
total_time = sum(calculate_time(d) for d in moves)
print(f"Total Time: {total_time:.3f}s")
12. Stretch Challenge
Challenge: Implement Lookahead.
Consider a sequence: Move(100) -> Move(100).
Instead of stopping between them, the end speed of Move 1 should be target_v.
The start speed of Move 2 should be target_v.
This makes the total time much shorter.
Requires a linked list of moves and a backward/forward pass to determine entry/exit velocities.