ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

G-Code Drilling Pattern Automation: Circular Array

CNC G-Code

Goal -- WPM

Ready
Exercise Algorithm Area
1package main
2
3import (
4 "fmt"
5 "math"
6)
7
8const PI = math.Pi
9
10// generateCircularDrillPattern generates G-code commands for a circular drilling pattern.
11// It takes the center X, Y coordinates, radius, number of holes, and Z depth.
12func generateCircularDrillPattern(centerX, centerY, radius, numHoles, zDepth float64) []string {
13 var gcodeCommands []string
14
15 // Safety Z height
16 szPosition := 5.0
17
18 // G-code setup: G17 (XY plane), G21 (metric), G90 (absolute)
19 gcodeCommands = append(gcodeCommands, "G17")
20 gcodeCommands = append(gcodeCommands, "G21")
21 gcodeCommands = append(gcodeCommands, "G90")
22
23 // Move to initial safe Z position
24 gcodeCommands = append(gcodeCommands, fmt.Sprintf("G0 Z%.3f", nzPosition))
25
26 // Calculate the angle increment per hole
27 angleIncrement := 2.0 * PI / numHoles
28
29 // Loop to generate each hole position
30 for i := 0.0; i < numHoles; i++ {
31 currentAngle := i * angleIncrement
32
33 // Calculate X and Y coordinates for the current hole
34 currentX := centerX + radius*math.Cos(currentAngle)
35 currentY := centerY + radius*math.Sin(currentAngle)
36
37 // Move to the X,Y position of the hole
38 gcodeCommands = append(gcodeCommands, fmt.Sprintf("G0 X%.3f Y%.3f", currentX, currentY))
39
40 // Move down to drilling depth
41 gcodeCommands = append(gcodeCommands, fmt.Sprintf("G1 Z%.3f F200.0", zDepth)) // F200.0 is feed rate
42
43 // Retract to safety Z position
44 gcodeCommands = append(gcodeCommands, fmt.Sprintf("G0 Z%.3f", nzPosition))
45 }
46
47 // Return to home or a safe position (optional)
48 gcodeCommands = append(gcodeCommands, "G0 X0 Y0")
49 gcodeCommands = append(gcodeCommands, "G0 Z5.0")
50
51 return gcodeCommands
52}
53
54func main() {
55 // Example usage:
56 centerX := 50.0
57 centerY := 50.0
58 radius := 20.0
59 numHoles := 6.0
60 zDepth := -3.0
61
62 gcodeOutput := generateCircularDrillPattern(centerX, centerY, radius, numHoles, zDepth)
63
64 fmt.Println("--- Generated G-Code for Circular Drilling ---")
65 for _, line := range gcodeOutput {
66 fmt.Println(line)
67 }
68
69 // Edge case: 1 hole
70 fmt.Println("\n--- Edge Case: 1 Hole ---")
71 gcodeOutputOneHole := generateCircularDrillPattern(centerX, centerY, radius, 1.0, zDepth)
72 for _, line := range gcodeOutputOneHole {
73 fmt.Println(line)
74 }
75
76 // Edge case: 3 holes
77 fmt.Println("\n--- Edge Case: 3 Holes ---")
78 gcodeOutputThreeHoles := generateCircularDrillPattern(centerX, centerY, radius, 3.0, zDepth)
79 for _, line := range gcodeOutputThreeHoles {
80 fmt.Println(line)
81 }
82
83 // Edge case: 0 holes (should produce only setup and return commands)
84 fmt.Println("\n--- Edge Case: 0 Holes ---")
85 gcodeOutputZeroHoles := generateCircularDrillPattern(centerX, centerY, radius, 0.0, zDepth)
86 for _, line := range gcodeOutputZeroHoles {
87 fmt.Println(line)
88 }
89}
Algorithm description viewbox

G-Code Drilling Pattern Automation: Circular Array

Algorithm description:

This algorithm automates the generation of G-code for drilling holes arranged in a circular pattern. It calculates the precise X and Y coordinates for each hole based on the circle's center, radius, and the desired number of holes. This is a common requirement for creating bolt hole patterns, bearing mounts, or other radially symmetric features.

Algorithm explanation:

The `generateCircularDrillPattern` function produces G-code for drilling holes in a circle. It begins with standard G-code setup commands (`G17`, `G21`, `G90`) and moves to a safe Z height. The core of the algorithm is a loop that iterates `numHoles` times. In each iteration, it calculates the `currentAngle` for the hole using `2 * PI / numHoles`. Trigonometric functions (`math.Cos` and `math.Sin`) are then used to determine the `currentX` and `currentY` coordinates based on the `centerX`, `centerY`, `radius`, and `currentAngle`. Similar to the linear drill cycle, it generates `G0` moves to the hole's XY position, `G1` to plunge to `zDepth`, and `G0` to retract to the safe Z height. The time complexity is O(n), where n is `numHoles`, as each hole requires a fixed set of G-code commands. The space complexity is O(n) for storing the generated G-code strings. Edge cases like 0, 1, or 3 holes are handled correctly by the loop and angle calculation. The use of `math.Cos` and `math.Sin` ensures accurate placement of holes on the circle's circumference.

Pseudocode:

FUNCTION generateCircularDrillPattern(centerX, centerY, radius, numHoles, zDepth):
  commands = empty list of strings
  zPosition = 5.0 // Safe Z height

  ADD "G17" TO commands
  ADD "G21" TO commands
  ADD "G90" TO commands

  ADD "G0 Z" + zPosition TO commands

  IF numHoles > 0:
    angleIncrement = 2 * PI / numHoles
    FOR i FROM 0 TO numHoles - 1:
      currentAngle = i * angleIncrement
      currentX = centerX + radius * cos(currentAngle)
      currentY = centerY + radius * sin(currentAngle)

      ADD "G0 X" + currentX + " Y" + currentY TO commands
      ADD "G1 Z" + zDepth + " F200.0" TO commands // Feed rate 200
      ADD "G0 Z" + zPosition TO commands

  ADD "G0 X0 Y0" TO commands
  ADD "G0 Z5.0" TO commands

  RETURN commands