Cog/Gcode.pm

filter: Statementstmt 13.0% 19 / 146 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 17.3% 4 / 23 Podpod 0.0% 0 / 19 totaltotal 8.2% 23 / 279 CC 40 SCAR 32.4
CC
40
Cov
8%
gmove
72.0
setcuttersize
42.0
garccw
30.0
CRAP 25.5
« Cog/Gdcode.pm ↑ Summary Cog/examples/eg1/eg1.pl »
1 # Written by Mark Winder, mark.winder4@btinternet.com
2 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $VERSION=0.061;
4
5 package Gcode;
6 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 @ISA=qw(CNC::Cog::Gcode);
8 # I define another package Gcode, this enables you to say new Gcode(...
9 # instead of new CNC::Cog::Gcode(...;
10
11
12 package CNC::Cog::Gcode;
13 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 @ISA=qw(Exporter);
15 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16
17 1 my $f="%9f ";
18 1 my $ff="%2.1f";
19
20 1 my $lineno=0;
21 # effectively providesone level of buffereing for commands. Needed to make sure recursive calls do what you think they should.
22 sub proc
23 0 ▶{
Subroutine proc: not called CC=2 SCAR=17.9
Pod undocumented
24 0 my ($g,$c)=@_; # params are gcode object, code
25
26 0 my ($file)=$g->{file};
27
28 0 ▶ printf($file "%s\n",$g->{pending}) if ($g->{pending});
Branch: 1 branch
BranchTrueFalseTotal
if $g->{"pending"} 0 0 0
29 0 $g->{pending}=$c;
30 0 return $c;
31 }
32 # object creator
33 sub new
34 0 ▶{
Subroutine new: not called CC=4 SCAR=30.0
Pod undocumented
35 0 my ($class,$file,$feed,$toolnumber)=@_;
36 0 ▶ $class=ref($class) || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
37 0 my ($x)={};
38 0 $x->{file}=$file;
39 0 ▶ open($file,">".$file) or croak("Unable to open file $file for write");
Branch: 1 branch
BranchTrueFalseTotal
open $file, '>' . $file or croak("Unable to open file $file for write") 0 0 0
40 0 $x->{pending}="%\nG40 G17";
41 0 $x->{feed}=$feed;
42 0 $x->{cuttersize}=0;
43 0 $x->{toolnumber}=1;
44 0 ▶ $x->{toolnumber}=$toolnumber if (defined $toolnumber);
Branch: 1 branch
BranchTrueFalseTotal
if defined $toolnumber 0 0 0
45 0 return bless $x,$class;
46 }
47 # initialisation code at the start of gcode
48 sub ginit
49 0 ▶{
Subroutine ginit: not called CC=1 SCAR=0.0
Pod undocumented
50
51 }
52 sub setcuttersize
53 0 ▶{
Subroutine setcuttersize: not called CC=6 SCAR=37.4
Pod undocumented
54 0 my ($g,$s)=@_; # set cutter diameter default to inches.
55 # can add pt for point, mm for millimetres, cm for centimetres
56 # can add i for inches (default)
57 # can add t for thous of an inch
58 0 $s=~s/i//;
59 0 ▶ $s=~s/pt// and $s/=72;
Branch: 1 branch
BranchTrueFalseTotal
$s =~ s/pt// and $s /= 72 0 0 0
60 0 ▶ $s=~s/mm// and $s/=25.4;
Branch: 1 branch
BranchTrueFalseTotal
$s =~ s/mm// and $s /= "25.4" 0 0 0
61 0 ▶ $s=~s/cm// and $s/=2.54;
Branch: 1 branch
BranchTrueFalseTotal
$s =~ s/cm// and $s /= "2.54" 0 0 0
62 0 ▶ $s=~s/t// and $s/=1000.0;
Branch: 1 branch
BranchTrueFalseTotal
$s =~ s/t// and $s /= 1000 0 0 0
63
64 0 ▶ $s=~/[a-zA-Z]/ and die "Invalid unit specification $s";
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /[a-zA-Z]/ and die "Invalid unit specification $s" 0 0 0
65
66 0 $g->{cuttersize}=$s;
67
68 }
69 sub getcuttersize
70 0 ▶{
Subroutine getcuttersize: not called CC=1 SCAR=6.9
Pod undocumented
71 0 my ($g)=@_;
72
73 0 return $g->{cuttersize};
74 }
75
76
77 # produces a comment protected by gcodes comment convention
78 sub gcomment
79 0 ▶{
Subroutine gcomment: not called CC=3 SCAR=24.8
Pod undocumented
80 0 my $gc=shift;
81 0 my ($c)=@_;
82
83 0 $c=~s/\n$//;
84 0 my @c=split("\n",$c);
85 0 @c=grep { $_ ne ''} @c;
86 0 ▶ return "" if (@c==0);
Branch: 1 branch
BranchTrueFalseTotal
if @c == 0 0 0 0
87 0 ▶ while (@c>1)
Branch: 1 branch
BranchTrueFalseTotal
if (@c > 1) 0 0 0
88 {
89 0 $c=shift @c;
90 0 proc($gc,"( $c )");
91 }
92 0 $c=shift @c;
93 0 return proc($gc,"( $c )");
94 }
95 # rapid move command.
96 sub grapid
97 0 ▶{
Subroutine grapid: not called CC=4 SCAR=30.0
Pod undocumented
98 0 my $g="G0";
99 0 my $c;
100 0 my $gc=shift;
101 0 ▶ while (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
102 {
103 0 ▶ $c.=sprintf("%s $f",uc($_[0]),$_[1]) if ($_[0] =~/^[xyz]$/i);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^[xyz]$/i 0 0 0
104 # $c.=sprintf("F $ff",$_[1]) if ($_[0] =~/^f$/i);
105 0 shift; shift;
106 }
107 0 ▶ return proc($gc, "$g $c") if ($c);
Branch: 1 branch
BranchTrueFalseTotal
if $c 0 0 0
108 0 return "";
109 }
110 # move command. perhaps this would be a good point to explain the calling convention here.
111 # its a bit odd. In order to preserve the useful feature of gcode that you can provide what
112 # ever parameters you want to provide (and in whatever order) the convention is that
113 # that you pass an x followed by the x value and so on.
114 # can be intollerent of faulty calls
115 sub gmove
116 0 ▶{
Subroutine gmove: not called CC=8 SCAR=42.8
Pod undocumented
117 0 my $g="G1";
118 0 my $c;
119 0 my $gc=shift;
120 0 my $hasfeed=0;
121 0 ▶ while (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
122 {
123 0 ▶ $c.=sprintf("%s $f",uc($_[0]),$_[1]) if ($_[0] =~/^[xyz]$/i);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^[xyz]$/i 0 0 0
124 0 ▶ $c.=sprintf("F $ff",$hasfeed=$_[1]) if ($_[0] =~/^f$/i);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^f$/i 0 0 0
125 0 shift; shift;
126 }
127 0 ▶ $gc->{feedsent}||=0;
Condition0%0/2
$gc->{"feedsent"} ||= 0
l!l
00
MC/DC0%0/1
$gc->{"feedsent"} ||= 0
$gc->{"feedsent"}
Truth table0%0/2
A
A= $gc->{"feedsent"}
Aresult
11
00
128 0 ▶ $c.=sprintf("F $ff",$gc->{feed}) if (!$hasfeed and !$gc->{feedsent});
Branch: 1 branch
BranchTrueFalseTotal
unless $hasfeed or $gc->{"feedsent"} 0 0 0
Condition0%0/3
$hasfeed or $gc->{"feedsent"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$hasfeed or $gc->{"feedsent"}
$hasfeed $gc->{"feedsent"}
Truth table0%0/3
A or B
A= $hasfeedB= $gc->{"feedsent"}
ABresult
1X1
011
000
129 0 $gc->{feedsent}=1;
130 0 ▶ return proc($gc, "$g $c") if ($c);
Branch: 1 branch
BranchTrueFalseTotal
if $c 0 0 0
131 0 return "";
132 }
133 sub gdwell
134 0 ▶{
Subroutine gdwell: not called CC=4 SCAR=30.0
Pod undocumented
135 0 my $g="G4 ";
136 0 my $c='';
137 0 my $gc=shift;
138 0 ▶ while (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
139 {
140 0 ▶ if ($_[0] =~/^[p]$/i) # we adopting a slightly different aroach here to other functions
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^[p]$/i) { } 0 0 0
141 { # if provided, ignore it, otherwise assume arg is dwell in seconds
142 0 shift; # so can do gdwell('p',2) or gdwell(2)
143 }
144 else
145 {
146 0 $c.=sprintf(" P$f",$_[0]);
147 0 shift;
148 }
149 }
150 0 ▶ return proc($gc, "$g $c") if ($c);
Branch: 1 branch
BranchTrueFalseTotal
if $c 0 0 0
151 0 return "";
152 }
153
154 # arc clockwise, x,y and r radius only implemented.
155 sub garccw
156 0 ▶{
Subroutine garccw: not called CC=5 SCAR=34.0
Pod undocumented
157 # clockwise arc
158 0 my $g="G2 ";
159 0 my $c;
160 0 my $gc=shift;
161 0 ▶ while (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
162 {
163 0 ▶ $c.=sprintf("%s $f",uc($_[0]),$_[1]) if ($_[0] =~/^[xyzrij]$/i);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^[xyzrij]$/i 0 0 0
164 0 ▶ $c.=sprintf("F $ff",$_[1]) if ($_[0] =~/f/i);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /f/i 0 0 0
165 0 shift; shift;
166 }
167 0 ▶ return proc($gc,"$g $c\n") if ($c);
Branch: 1 branch
BranchTrueFalseTotal
if $c 0 0 0
168 0 return "";
169 }
170 # arc clockwise
171 sub garcccw
172 0 ▶{
Subroutine garcccw: not called CC=5 SCAR=34.0
Pod undocumented
173 # counter clockwise arc
174 0 my $g="G3 ";
175 0 my $c;
176
177 0 my $gc=shift;
178 0 ▶ while (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
179 {
180 0 ▶ $c.=sprintf("%s $f",uc($_[0]),$_[1]) if ($_[0] =~/^[xyzrij]$/i);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^[xyzrij]$/i 0 0 0
181 0 ▶ $c.=sprintf("F $ff",$_[1]) if ($_[0] =~/f/i);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /f/i 0 0 0
182 0 shift; shift;
183 }
184 0 ▶ return proc($gc,"$g $c\n") if ($c);
Branch: 1 branch
BranchTrueFalseTotal
if $c 0 0 0
185 0 return "";
186 }
187 # cutter compensation on driving on the righ
188 # you can supply an additional function if you want the compensation to linearly
189 # come into effect as a move is performed.
190 sub gcompr
191 0 ▶{
Subroutine gcompr: not called CC=4 SCAR=30.0
Pod undocumented
192 # cutter compensation on, cutting to the right
193
194
195 0 my ($c)="G42 ";
196 0 my ($gc)=shift;
197
198 0 ▶ while ($_[0] =~/^[d]$/i)
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^[d]$/i) 0 0 0
199 {
200 0 $c.=sprintf("%s %d",uc($_[0]),$_[1]) ;
201 0 shift; shift;
202 }
203
204 0 ▶ while (@_>0 and $_[0]=~/^G/i)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0 and $_[0] =~ /^G/i) 0 0 0
Condition0%0/3
@_ > 0 and $_[0] =~ /^G/i
!ll&&!rl&&r
000
MC/DC0%0/2
@_ > 0 and $_[0] =~ /^G/i
@_ > 0 $_[0] =~ /^G/i
Truth table0%0/3
A and B
A= @_ > 0B= $_[0] =~ /^G/i
ABresult
0X0
100
111
205 {
206 0 $c.=" ".$_[0];
207 0 shift;
208 0 $gc->{pending}=''; # we clear this if additional values are passed
209 }
210 0 return proc($gc,$c);
211 }
212 # cutter (radius) compensation, drive on the left.
213 sub gcompl
214 0 ▶{
Subroutine gcompl: not called CC=3 SCAR=24.8
Pod undocumented
215 # cutter compensation on, cutting to the left
216
217 0 my ($c)="G41 ";
218 0 my ($gc)=shift;
219 0 ▶ while ($_[0] =~/^[d]$/i)
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^[d]$/i) 0 0 0
220 {
221 0 $c.=sprintf("%s %d",uc($_[0]),$_[1]) ;
222 0 shift; shift;
223 }
224 0 ▶ while ($_[0]=~/G/i)
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /G/i) 0 0 0
225 {
226 0 $c.=" ".$_[0];
227 0 shift;
228 0 $gc->{pending}=''; # we clear this if additional values are passed
229 }
230 0 return proc($gc,$c);
231 }
232 # switch off compensation.
233 sub gcomp0
234 0 ▶{
Subroutine gcomp0: not called CC=3 SCAR=24.8
Pod undocumented
235 # cutter compensation off
236
237 0 my ($c)="G40 ";
238 0 my ($gc)=shift;
239 0 ▶ while (@_>0 and $_[0]=~/G/i)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0 and $_[0] =~ /G/i) 0 0 0
Condition0%0/3
@_ > 0 and $_[0] =~ /G/i
!ll&&!rl&&r
000
MC/DC0%0/2
@_ > 0 and $_[0] =~ /G/i
@_ > 0 $_[0] =~ /G/i
Truth table0%0/3
A and B
A= @_ > 0B= $_[0] =~ /G/i
ABresult
0X0
100
111
240 {
241 0 $c.=" ".$_[0];
242 0 shift;
243 0 $gc->{pending}=''; # we clear this if additional values are passed
244 }
245 0 return proc($gc,$c);
246 }
247 # end of program.
248 sub gend
249 0 ▶{
Subroutine gend: not called CC=1 SCAR=6.9
Pod undocumented
250 0 my ($gc)=@_;
251 0 $gc->proc('');
252 0 my $file= $gc->{file};
253 0 print $file "%\n";
254 0 close $file;
255 }
256
257 # The following routines are used for debug purposes. In this package they should always do nothing.
258 0 ▶sub gmark {} # make a cross mark at a given point
Subroutine gmark: not called CC=1 SCAR=0.0
Pod undocumented
259 0 ▶sub gline {} # draw a line between 2 points.
Subroutine gline: not called CC=1 SCAR=0.0
Pod undocumented
260 0 ▶sub gruler{} # draw a ruler for sizing purposes.
Subroutine gruler: not called CC=1 SCAR=0.0
Pod undocumented
261 sub rednext # make the next line red, not used in g code output produces comment **** red ****
262 0 ▶{
Subroutine rednext: not called CC=1 SCAR=6.9
Pod undocumented
263 0 my ($g)=@_;
264 0 $g->gcomment("**** red ****");
265 }
266 1 1;
267
« Cog/Gdcode.pm ↑ Summary Cog/examples/eg1/eg1.pl »