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

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

G-Code Canned Cycle: Simple Drilling Pattern

CNC G-Code

Goal -- WPM

Ready
Exercise Algorithm Area
1package main
2
3import "fmt"
4
5// generateLinearDrillCycle generates G-code commands for a linear drilling pattern.
6// It takes the starting X and Y coordinates, the number of holes, the spacing
7// between holes, and the Z depth for drilling.
8func generateLinearDrillCycle(startX, startY, numHoles, spacing, zDepth float64) []string {
9 var gcodeCommands []string
10
11 // Safety Z height (can be made a parameter)
12 szPosition := 5.0
13
14 // G17: XY plane selection (optional, depending on machine config)
15 gcodeCommands = append(gcodeCommands, "G17")
16 // G21: Metric units (optional, depending on machine config)
17 gcodeCommands = append(gcodeCommands, "G21")
18 // G90: Absolute positioning
19 gcodeCommands = append(gcodeCommands, "G90")
20
21 // Move to initial safe Z position
22 gcodeCommands = append(gcodeCommands, fmt.Sprintf("G0 Z%.3f", nzPosition))
23
24 // Loop to generate each hole position
25 for i := 0; i < int(numHoles); i++ {
26 currentX := startX + float64(i)*spacing
27 currentY := startY // For a linear pattern along X
28
29 // Move to the X,Y position of the hole
30 gcodeCommands = append(gcodeCommands, fmt.Sprintf("G0 X%.3f Y%.3f", currentX, currentY))
31
32 // Move down to drilling depth
33 gcodeCommands = append(gcodeCommands, fmt.Sprintf("G1 Z%.3f F200.0", zDepth)) // F200.0 is feed rate
34
35 // Retract to safety Z position
36 gcodeCommands = append(gcodeCommands, fmt.Sprintf("G0 Z%.3f", nzPosition))
37 }
38
39 // Return to home or a safe position (optional)
40 gcodeCommands = append(gcodeCommands, "G0 X0 Y0")
41 gcodeCommands = append(gcodeCommands, "G0 Z5.0")
42
43 return gcodeCommands
44}
45
46func main() {
47 // Example usage:
48 startX := 10.0
49 startY := 20.0
50 numHoles := 4.0
51 spacing := 5.0
52 zDepth := -2.0
53
54 gcodeOutput := generateLinearDrillCycle(startX, startY, numHoles, spacing, zDepth)
55
56 fmt.Println("--- Generated G-Code for Linear Drilling ---")
57 for _, line := range gcodeOutput {
58 fmt.Println(line)
59 }
60
61 // Edge case: 1 hole
62 fmt.Println("\n--- Edge Case: 1 Hole ---")
63 gcodeOutputOneHole := generateLinearDrillCycle(startX, startY, 1.0, spacing, zDepth)
64 for _, line := range gcodeOutputOneHole {
65 fmt.Println(line)
66 }
67
68 // Edge case: 0 holes
69 fmt.Println("\n--- Edge Case: 0 Holes ---")
70 gcodeOutputZeroHoles := generateLinearDrillCycle(startX, startY, 0.0, spacing, zDepth)
71 for _, line := range gcodeOutputZeroHoles {
72 fmt.Println(line)
73 }
74}
Algorithm description viewbox

G-Code Canned Cycle: Simple Drilling Pattern

Algorithm description:

This algorithm generates a sequence of G-code commands to perform a linear drilling operation. It simulates a custom canned cycle by outputting commands for rapid moves to position, plunging to depth, and retracting. This is useful for automating repetitive drilling tasks along a straight line, reducing programming time and potential errors.

Algorithm explanation:

The `generateLinearDrillCycle` function creates a list of G-code strings. It starts by setting common G-code modes like `G17` (XY plane), `G21` (metric units), and `G90` (absolute positioning). It then moves to a safe Z height. The core logic is a loop that iterates `numHoles` times. In each iteration, it calculates the `currentX` coordinate based on the `startX`, `spacing`, and loop index `i`. It then generates a `G0` (rapid move) command to the calculated `X,Y` position, followed by a `G1` (linear feed) command to plunge to `zDepth` at a specified feed rate. Finally, it retracts to the safe Z height using `G0`. After the loop, it adds commands to return to origin. The time complexity is O(n), where n is `numHoles`, as each hole requires a constant number of G-code commands. The space complexity is O(n) to store the generated G-code strings. Edge cases like 0 or 1 hole are handled correctly by the loop structure.

Pseudocode:

FUNCTION generateLinearDrillCycle(startX, startY, numHoles, spacing, 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

  FOR i FROM 0 TO numHoles - 1:
    currentX = startX + i * spacing
    currentY = startY

    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