Skip to content

Instantly share code, notes, and snippets.

@aegis1980
Created October 17, 2025 05:35
Show Gist options
  • Select an option

  • Save aegis1980/45f8a1b351884dc727dc92a40cb64671 to your computer and use it in GitHub Desktop.

Select an option

Save aegis1980/45f8a1b351884dc727dc92a40cb64671 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 10,
"id": "c1322e98",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The autoreload extension is already loaded. To reload it, use:\n",
" %reload_ext autoreload\n"
]
}
],
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "ee5a37ce",
"metadata": {},
"outputs": [],
"source": [
"from typing import List\n",
"\n",
"def generate_helix_gcode(radii: List[float], depth_per_rev: float, total_depth: float, safe_ht: float = 5.0000) -> str:\n",
" \"\"\"\n",
" Generate and save G-code for a helical spiral motion around the origin (X0, Y0).\n",
" The motion is counter-clockwise (G03) in quadrants, dropping in Z per revolution,\n",
" with a final constant-depth revolution at the end.\n",
"\n",
" Args:\n",
" radius (float): Radius of the helix in mm.\n",
" depth_per_rev (float): Z-axis drop per full revolution (mm/rev).\n",
" total_depth (float): Final Z depth to reach before last constant revolution.\n",
" filename (str): Name of the output G-code file (e.g., \"helix.nc\" or \"spiral.gcode\").\n",
"\n",
" Returns:\n",
" str: The generated G-code text.\n",
" \"\"\"\n",
" lines = []\n",
" lines.append(\"(Helical CCW spiral motion only — no spindle)\")\n",
" lines.append(\"\")\n",
"\n",
" for radius in radii:\n",
" lines.append(\"\")\n",
" lines.append(f\"(Helix with radius {radius} mm, depth per rev {depth_per_rev} mm, total depth {total_depth} mm)\")\n",
" lines.append(f\"G00 Z{safe_ht:.4f}\")\n",
" lines.append(f\"G00 X{radius:.4f} Y0.0000 (start at radius)\")\n",
" lines.append(f\"G00 Z0.5000 (plunge to just above cut ht height)\")\n",
" lines.append(f\"Z0.0000 F5.000 S0 (move to start height)\")\n",
" \n",
" # Compute number of descending revolutions\n",
" num_revs = int(total_depth / depth_per_rev)\n",
" z = 0.0\n",
"\n",
" def add_revolution(z_start: float, z_end: float, last=False):\n",
" \"\"\"Helper: Add one full 360° CCW revolution (four 90° arcs).\"\"\"\n",
" z_steps = [z_start + (z_end - z_start) * i / 4 for i in range(1, 5)]\n",
" if last:\n",
" z_steps = [z_end] * 4 # Constant-depth revolution\n",
"\n",
" lines.extend([\n",
" f\"G03 X0.0000 Y{ radius:.4f} I-{ radius:.4f} J0.0000 Z{z_steps[0]:.4f}\",\n",
" f\"G03 X-{radius:.4f} Y0.0000 I0.0000 J-{radius:.4f} Z{z_steps[1]:.4f}\",\n",
" f\"G03 X0.0000 Y-{radius:.4f} I{ radius:.4f} J0.0000 Z{z_steps[2]:.4f}\",\n",
" f\"G03 X{radius:.4f} Y0.0000 I0.0000 J{ radius:.4f} Z{z_steps[3]:.4f}\",\n",
" ])\n",
"\n",
" # Add descending revolutions\n",
" for _ in range(num_revs):\n",
" z_next = z - depth_per_rev\n",
" add_revolution(z, z_next)\n",
" z = z_next\n",
"\n",
" # Final constant-depth revolution\n",
" add_revolution(z, z, last=True)\n",
"\n",
" # End move\n",
" lines.append(\"\")\n",
" lines.append(\"G00 Z5.0000 (retract)\")\n",
" lines.append(\"M05 (spindle stop)\")\n",
" lines.append(\"G00 X0 Y0\")\n",
" lines.append(\"M30 (end of program)\")\n",
"\n",
"\n",
" gcode_text = \"\\n\".join(lines)\n",
"\n",
"\n",
" return gcode_text\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "c70294bd",
"metadata": {},
"outputs": [],
"source": [
"def radius_correction(radius_req: float, knife_offset: float) -> float:\n",
" \"\"\"Adjust radius for knife offset.\"\"\"\n",
" return (radius_req**2 + knife_offset**2) ** 0.5"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "c66d523f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[18.170906691742157, 39.73900759958658]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"knife_offset = 6.215 # mm, from center to cutting edge\n",
"filename = \"flywheel.nc\"\n",
"\n",
"radii=[17.075,39.25]\n",
"\n",
"corrected_radii = [radius_correction(r, knife_offset) for r in radii]\n",
"corrected_radii"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2c9100a6",
"metadata": {},
"outputs": [
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31mThe Kernel crashed while executing code in the current cell or a previous cell. \n",
"\u001b[1;31mPlease review the code in the cell(s) to identify a possible cause of the failure. \n",
"\u001b[1;31mClick <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. \n",
"\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
]
}
],
"source": [
"\n",
"gcode_text = generate_helix_gcode(radii=corrected_radii, depth_per_rev=0.5, total_depth=2.0)\n",
"\n",
"# Save to file in current working directory\n",
"with open(filename, \"w\", encoding=\"utf-8\") as f:\n",
" f.write(gcode_text)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment