forked from hdznrrd/hpgl2gcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhpgl2gcode.pl
executable file
·125 lines (89 loc) · 2.15 KB
/
hpgl2gcode.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/perl -W
use strict;
use Switch;
# assumptions:
# - absolute position mode
# - machine origin (0,0,0) placed at surface of work
# - machine set to use millimeter
## CONFIGURATION
# Z positions in mm
my $PEN_DOWN_Z="-2";
my $PEN_UP_Z="5";
# feed rates in mm/min
my $FEED_RATE_XY="1200";
my $FEED_RATE_Z="900";
## STATE TRACKING FOR OPTIMIZATION
my $STATE__PEN_IS_UP=1;
sub is_pen_up() { return $STATE__PEN_IS_UP; }
sub pen_up() { $STATE__PEN_IS_UP=1; }
sub pen_down() { $STATE__PEN_IS_UP=0; }
## TOP LEVEL PARSER DISPATCH
sub transmute() {
switch(shift) {
case 'IN' { &transmute_init($_) }
case /^SP/ { &transmute_pen_select($_) }
case /^PU/ { &transmute_pen_up($_) }
case /^PD/ { &transmute_pen_down($_) }
else { &unknown_code($_) }
}
}
## PARSER FUNCTIONS
# something wrong? stop fatally.
sub unknown_code() {
die("unknown code: $_$/");
}
# nothing to do here, that's handled in the gcode header already
sub transmute_init() {}
# Select Pen mapped to tool-change command
# note: HPGL pens start at 1, while gcode tools start at 0
sub transmute_pen_select() {
/^SP(\d+)/;
# $1 tool id
&emit_tool_change($1-1);
}
# Pen Up is mapped to a Z+ movement (if needed) and rapid X/Y movement
sub transmute_pen_up() {
/^PU(\d+),(\d+)/;
# $1 x *25um
# $2 y *25um
&emit_rapid_z($PEN_UP_Z) if(!&is_pen_up());
&emit_rapid_xy(&hpgl2mm($1),&hpgl2mm($2));
&pen_up();
}
# Pen Down is mapped to a Z- movement (if needed) and regular X/Y movement
sub transmute_pen_down() {
/^PD(\d+),(\d+)/;
# $1 x *25um
# $2 y *25um
&emit_rapid_z(0) if(&is_pen_up());
&emit_z($PEN_DOWN_Z) if(&is_pen_up());
&emit_xy(&hpgl2mm($1),&hpgl2mm($2));
&pen_down();
}
# coordinate conversion helper
# HPGL units are 25um aka 40 units per mm
sub hpgl2mm() {
return ((shift) / 40.0);
}
## GCODE OUTPUT FUNCTIONS
sub emit_tool_change() {
printf "T%sM06$/",shift;
}
sub emit_rapid_z() {
printf "G00Z%s$/",shift;
}
sub emit_rapid_xy() {
printf "G00X%sY%s$/",shift,shift;
}
sub emit_z() {
printf "G1Z%sF%s$/",shift,$FEED_RATE_Z;
}
sub emit_xy() {
printf "G1X%sY%sF%s$/",shift,shift,$FEED_RATE_XY;
}
## MAIN LOOP
while(<>) {
foreach (split /;/) {
&transmute($_)
}
}