# =====================================================================
#  ORBITA – load test for Klipper printers · by Revotech · https://revo3d.pl/orbita/
#  All axes move at once, no heating, no filament: tilted orbits, a climbing spiral, transfer orbits,
#  a figure eight and a helix down – all as G2/G3 arcs with Z moving along each arc. Ends with a spiral
#  re-entry, a Z "bow" and XY homing. Every Z change is spread over the arcs, so Z stays under its limit
#  and XY never slows down for Z.
#
#  Settings: 0 = read from your printer.cfg (axis limits, max_z_velocity). Set your own values below if you like.
#  Install:  save as orbita.cfg next to printer.cfg, add  [include orbita.cfg]  to printer.cfg,
#            make sure printer.cfg has  [gcode_arcs]  (resolution: 1.0), restart Klipper.
#  Run:      ORBITA                          all rounds with the speeds set below
#            ORBITA ROUNDS=1                 first round only
#            ORBITA SPEEDS=80,160,240        your own round speeds, mm/s (any number of rounds)
#            ORBITA SPEEDS=150 Z_SPEED=8     one round at 150 mm/s, Z at most 8 mm/s
#  Safety:   empty the bed, stay at the machine, keep your hand near the emergency stop.
#            For Cartesian and CoreXY machines (not delta). Check the settings below before the first run.
#  License:  GPL-3.0 (like Klipper). Keep this header when you share it.
# =====================================================================
[gcode_macro ORBITA]
description: ORBITA load test by Revotech (revo3d.pl) – all axes, no heating
variable_x_min: 0          # area the orbits may use, mm (x_max 0 = whole X axis minus 10 mm)
variable_x_max: 0
variable_y_min: 0          # (y_max 0 = whole Y axis minus 10 mm)
variable_y_max: 0
variable_z_min: 0          # lowest Z the test goes to, mm (0 = 5 mm above Z min)
variable_z_max: 0          # highest Z the test goes to, mm (0 = 85% of the Z axis)
variable_speeds: [100, 200, 300]        # default round speeds, mm/s (SPEEDS=… overrides; never above max_velocity)
variable_z_speed: 0      # Z speed the test may use, mm/s (0 = max_z_velocity; Z_SPEED=… overrides)
variable_home: 'G28'          # homing command before the test
gcode:
  {% set v = printer["gcode_macro ORBITA"] %}
  {% set speeds = params.SPEEDS.replace(' ', '').split(',')|map('float')|select('>', 0)|list if params.SPEEDS else v.speeds %}
  {% set z_speed = params.Z_SPEED|default(v.z_speed)|float %}
  {% if printer.print_stats.state in ['printing', 'paused'] %}
    {action_raise_error("ORBITA: the printer is printing")}
  {% endif %}
  {% if 'gcode_arcs' not in printer.configfile.settings %}
    {action_raise_error("ORBITA needs [gcode_arcs] in printer.cfg")}
  {% endif %}
  {% set th = printer.toolhead %}
  {% set x0, x1 = (v.x_min, v.x_max) if v.x_max > v.x_min else (th.axis_minimum.x + 10, th.axis_maximum.x - 10) %}
  {% set y0, y1 = (v.y_min, v.y_max) if v.y_max > v.y_min else (th.axis_minimum.y + 10, th.axis_maximum.y - 10) %}
  {% set z0 = v.z_min if v.z_min > 0 else [th.axis_minimum.z, 0]|max + 5 %}
  {% set z1 = v.z_max if v.z_max > z0 else th.axis_maximum.z * 0.85 %}
  {% set zmv = printer.configfile.settings.printer.max_z_velocity %}
  {% if not speeds %}
    {action_raise_error("ORBITA: SPEEDS needs at least one speed above 0, e.g. SPEEDS=100,200,300")}
  {% endif %}
  {% set n = [1, [params.ROUNDS|default(speeds|length)|int, speeds|length]|min]|max %}
  {% set cx = (x0 + x1) / 2 %}
  {% set cy = (y0 + y1) / 2 %}
  {% set k = [x1 - x0, y1 - y0]|min / 2 / 150 %}
  {% set vz = ([z_speed, zmv]|min if z_speed > 0 else zmv) * 0.95 %}
  {% set span = z1 - z0 %}
  {% set vmax = th.max_velocity %}
  {% if k < 0.2 or span < 40 %}
    {action_raise_error("ORBITA: the test area is too small (needs 60 mm in X/Y and 40 mm in Z)")}
  {% endif %}
  M117 ORBITA · homing
  G90
  {v.home}
  G1 Z{z0 + span * 0.1} F{(vz * 60)|int}
  G1 X{cx} Y{cy} F6000
  {% for r in range(n) %}
    _ORBITA_ROUND R={r + 1} N={n} F={([speeds[r], vmax]|min * 60)|int} Z={(z0 + span * (0.13 + 0.04 * [r, 2]|min))|round(2)} CX={cx} CY={cy} K={k} VZ={vz} SPAN={span}
  {% endfor %}
  _ORBITA_FINAL CX={cx} CY={cy} K={k} VZ={vz} F={([200, vmax]|min * 60)|int} ZT={(z0 + span * 0.05 + 6)|round(2)}
  RESPOND MSG="ORBITA: mission complete ({n} rounds) · ORBITA by Revotech · revo3d.pl"

[gcode_macro _ORBITA_ROUND]
gcode:
  {% set r, n = params.R|int, params.N|int %}
  {% set f = params.F|int %}{% set v = f / 60 %}
  {% set z = params.Z|float %}
  {% set cx, cy, k = params.CX|float, params.CY|float, params.K|float %}
  {% set vz = params.VZ|float * 0.85 %}
  {% set span = params.SPAN|float %}
  {% set pi = 3.14159 %}
  M117 ORBITA {r}/{n} · {v|int} mm/s · tilted orbits
  G1 X{cx + 150 * k} Y{cy} Z{z} F{f}
  {% for i in range(8) %}
    {% set rr = (150 - i * 12) * k %}
    {% set a = [span * 0.12, vz * pi * rr / v / 2]|min|round(2) %}
    {% if i > 0 %}G1 X{cx + rr} Y{cy} F{f}{% endif %}
    G2 X{cx - rr} Y{cy} I{0 - rr} J0 Z{z + a} F{f}
    G2 X{cx + rr} Y{cy} I{rr} J0 Z{z - a} F{f}
  {% endfor %}
  G1 X{cx + 150 * k} Y{cy} Z{z} F{f}
  M117 ORBITA {r}/{n} · spiral in, climbing
  {% set s = 6.0 * k %}{% set m = 22 %}
  {% set rsum = (150 * m - 6 * m * (m - 1) / 2) * k %}
  {% set H = [span * 0.55, vz * pi * rsum / v]|min %}
  {% set ns = namespace(zc = z) %}
  {% for i in range(m) %}
    {% set rr = (150 - i * 6) * k %}
    {% set ns.zc = ns.zc + H * rr / rsum %}
    {% if i % 2 == 0 %}
      G2 X{cx - rr} Y{cy} I{0 - rr} J0 Z{ns.zc|round(3)} F{f}
    {% else %}
      G2 X{cx - s + rr} Y{cy} I{rr} J0 Z{ns.zc|round(3)} F{f}
    {% endif %}
  {% endfor %}
  {% set ztop = (z + H)|round(3) %}
  M117 ORBITA {r}/{n} · transfer orbits
  {% set dz = [span * 0.15, vz * pi * 100 * k / v]|min|round(2) %}
  G1 X{cx + 60 * k} Y{cy} Z{ztop} F{f}
  {% for rep in range(2) %}
    G2 X{cx - 60 * k} Y{cy} I{0 - 60 * k} J0 F{f}
    G2 X{cx + 140 * k} Y{cy} I{100 * k} J0 Z{ztop + dz} F{f}
    G2 X{cx - 140 * k} Y{cy} I{0 - 140 * k} J0 F{f}
    G2 X{cx + 140 * k} Y{cy} I{140 * k} J0 F{f}
    G2 X{cx - 140 * k} Y{cy} I{0 - 140 * k} J0 F{f}
    G2 X{cx + 60 * k} Y{cy} I{100 * k} J0 Z{ztop} F{f}
  {% endfor %}
  M117 ORBITA {r}/{n} · figure eight
  {% set e = [span * 0.08, vz * pi * 70 * k / v / 2]|min|round(2) %}
  G1 X{cx} Y{cy} F{f}
  {% for rep in range(2) %}
    G2 X{cx + 140 * k} Y{cy} I{70 * k} J0 Z{ztop + e} F{f}
    G2 X{cx} Y{cy} I{0 - 70 * k} J0 Z{ztop} F{f}
    G3 X{cx - 140 * k} Y{cy} I{0 - 70 * k} J0 Z{ztop - e} F{f}
    G3 X{cx} Y{cy} I{70 * k} J0 Z{ztop} F{f}
  {% endfor %}
  M117 ORBITA {r}/{n} · helix down
  {% set step = vz * 2 * pi * 120 * k / v %}
  {% set nh = [((ztop - z) / step)|round(0, 'ceil')|int, 1]|max %}
  G1 X{cx + 120 * k} Y{cy} F{f}
  {% for h in range(nh) %}
    G2 X{cx - 120 * k} Y{cy} I{0 - 120 * k} J0 Z{(ztop - (ztop - z) * (h + 0.5) / nh)|round(3)} F{f}
    G2 X{cx + 120 * k} Y{cy} I{120 * k} J0 Z{(ztop - (ztop - z) * (h + 1) / nh)|round(3)} F{f}
  {% endfor %}

[gcode_macro _ORBITA_FINAL]
gcode:
  {% set cx, cy, k = params.CX|float, params.CY|float, params.K|float %}
  {% set vz = params.VZ|float %}
  {% set f0 = params.F|int %}
  {% set zt = params.ZT|float %}
  {% set z0 = printer.toolhead.position.z %}
  M117 ORBITA · re-entry
  G1 X{cx + 140 * k} Y{cy} F{f0}
  {% set s = 10.0 * k %}{% set m = 13 %}
  {% for i in range(m) %}
    {% set rr = (140 - i * 10) * k %}{% set fv = (f0 - f0 * 0.67 * i / m)|int %}
    {% if i % 2 == 0 %}
      G2 X{cx - rr} Y{cy} I{0 - rr} J0 Z{(z0 + (zt - z0) * (i + 1) / m)|round(3)} F{fv}
    {% else %}
      G2 X{cx - s + rr} Y{cy} I{rr} J0 Z{(z0 + (zt - z0) * (i + 1) / m)|round(3)} F{fv}
    {% endif %}
  {% endfor %}
  G1 X{cx} Y{cy} Z{zt} F3000
  M117 ORBITA · touchdown
  G4 P800
  {% for i in range(3) %}
    G1 Z{zt - 6} F{(vz * 40)|int}
    G1 Z{zt} F{(vz * 40)|int}
  {% endfor %}
  M117 ORBITA · docking
  G28 X Y
  M117 MISSION COMPLETE · revo3d.pl
