@@ -29,18 +29,19 @@ gcode =
29
29
-- create gcode file writer class, return instance of the gcode class
30
30
function new (name )
31
31
o =
32
- {
33
- x = 0 ; y = 0 ; z = 0 ; e = 0 , f = 0 ; -- current position
32
+ {
33
+ x = 0 ; y = 0 ; z = 0 ; e = 0 , f = 0 ; -- current position
34
34
lx = 0 , ly = 0 , lz = 0 , le = 0 , lf = 0 ; -- last emitted position
35
35
decimals = 2 ; -- nr of decimal places in output coordinates
36
36
edecimals = 3 ; -- nr of decimal places E axis output
37
37
epermm = 0.1 ; -- extrusion per mm of travel
38
38
mspeed = 60 * 60 ; -- move speed (between extrusions)
39
- espeed = 60 * 30 ; -- Extrusion speed}; -- create object
39
+ espeed = 60 * 30 ; -- Extrusion speed}; -- create object
40
+ totale = 0 ; -- total extruded length
40
41
};
41
- o .filename = name ;
42
- o .file = io.open (name , " wb" );
43
- setmetatable (o , gcode )
42
+ o .filename = name ;
43
+ o .file = io.open (name , " wb" );
44
+ setmetatable (o , gcode )
44
45
gcode .__index = gcode ;
45
46
return o
46
47
end
49
50
function gcode .close (self )
50
51
self :emit ();
51
52
io.close (self .file );
52
- self .file = nil ;
53
+ self .file = nil ;
53
54
end ;
54
55
55
56
-- copy the contect of a file into the current gcode file
56
57
function gcode .copyfile (self ,name )
57
58
self :emit ();
58
- self :comment (" <-- Including file: " .. name .. " -->" );
59
+ self :comment (" <-- Including file: " .. name .. " -->" );
59
60
local fh = io.open (name , " r" );
60
- if fh ~= nil then
61
- for l in fh :lines () do
62
- gc :write (l );
63
- end ;
64
- fh :close ();
65
- end ;
66
- self :comment (" <-- End of include file: " .. name .. " -->" );
61
+ if fh ~= nil then
62
+ for l in fh :lines () do
63
+ gc :write (l );
64
+ end ;
65
+ fh :close ();
66
+ end ;
67
+ self :comment (" <-- End of include file: " .. name .. " -->" );
67
68
end ;
68
69
69
70
-- Calculate the feedrate factor, based on layer width,height and filament diameter, assume square profile
70
71
function gcode .escale (self ,w ,h ,d ,speed )
71
72
self .epermm = w * h / ((math.pi * d * d )/ 4 );
72
- if speed ~= nil then
73
- self .espeed = speed ;
74
- end ;
73
+ if speed ~= nil then
74
+ self .espeed = speed ;
75
+ end ;
75
76
end
76
77
77
78
-- write data to file
83
84
local function dumptable (t )
84
85
local s = " {" ;
85
86
for i ,v in pairs (t ) do
86
- if type (v ) == " table" then
87
- s = s .. " " .. dumptable (v );
88
- elseif type (v ) == " function" then s = s .. " (function) " ;
89
- else
87
+ if type (v ) == " table" then
88
+ s = s .. " " .. dumptable (v );
89
+ elseif type (v ) == " function" then s = s .. " (function) " ;
90
+ else
90
91
s = s .. " " .. i .. " =" .. v .. " " ;
91
- end ;
92
- end ;
93
- return s .. " } " ;
92
+ end ;
93
+ end ;
94
+ return s .. " } " ;
94
95
end ;
95
96
96
97
97
98
-- write comment to file
98
99
function gcode .comment (self ,s ,t )
99
100
self :emit ();
100
101
self :write (" ( " .. s .. " )" );
101
- if t == nil then return ; end ;
102
+ if t == nil then return ; end ;
102
103
for i ,v in pairs (t ) do
103
- if type (v ) == " function" then s = " (function) " ;
104
- elseif type (v ) == " table" then s = dumptable (v );
105
- else
106
- s = v ;
107
- end ;
108
- self :write (" ( " .. i .. " = " .. s .. " )" )
109
- end ;
104
+ if type (v ) == " function" then s = " (function) " ;
105
+ elseif type (v ) == " table" then s = dumptable (v );
106
+ else
107
+ s = v ;
108
+ end ;
109
+ self :write (" ( " .. i .. " = " .. s .. " )" )
110
+ end ;
110
111
end ;
111
112
112
113
113
114
-- move current position, pass nil values to axis values to -not- change this field
114
115
function gcode .move (self ,x ,y ,z ,e ,f )
115
- self .x = x or self .x ;
116
- self .y = y or self .y ;
116
+ self .x = x or self .x ;
117
+ self .y = y or self .y ;
117
118
self .z = z or self .z ;
118
- self .e = e or self .e ;
119
+ self .e = e or self .e ;
119
120
self .f = f or self .mspeed ; -- default is move-speed
120
121
end ;
121
122
@@ -124,37 +125,39 @@ end;
124
125
function gcode .setpos (self , x , y , z , e )
125
126
self :emit ();
126
127
s = " " ;
127
- if x ~= nil then self .x = x ; s = s .. " X" .. x ; end ;
128
- if y ~= nil then self .y = y ; s = s .. " Y" .. y ; end ;
129
- if z ~= nil then self .z = z ; s = s .. " Z" .. z ; end ;
128
+ if x ~= nil then self .x = x ; s = s .. " X" .. x ; end ;
129
+ if y ~= nil then self .y = y ; s = s .. " Y" .. y ; end ;
130
+ if z ~= nil then self .z = z ; s = s .. " Z" .. z ; end ;
130
131
if e ~= nil then self .e = e ; s = s .. " E" .. e ; end ;
131
- if s ~= " " then self :write (" G92 " .. s .. " (set current position)" ); end ;
132
+ if s ~= " " then self :write (" G92 " .. s .. " (set current position)" ); end ;
132
133
end ;
133
134
134
135
-- move home, and set units to mm abs
135
136
function gcode .home (self , x , y , z )
136
137
self :emit ();
137
138
s = " " ;
138
139
if x ~= nil then s = s .. " X" ; end ;
139
- if y ~= nil then s = s .. " Y" ; end ;
140
- if z ~= nil then s = s .. " Z" ; end ;
141
- self :write (" G28" .. s .. " (home)" );
140
+ if y ~= nil then s = s .. " Y" ; end ;
141
+ if z ~= nil then s = s .. " Z" ; end ;
142
+ self :write (" G28" .. s .. " (home)" );
142
143
self :write (" G21 (units mm)" );
143
- self :write (" G90 (abs pos)" );
144
- self :setpos (x ,y ,z ,0 );
144
+ self :write (" G90 (abs pos)" );
145
+ self :setpos (x ,y ,z ,0 );
145
146
end ;
146
147
147
148
-- move to a position, while extruding
148
149
function gcode .extrude (self , x , y , z )
149
150
self :emit (); -- emit any moves or previous extrude commands
150
- dx = (x or self .x ) - self .x ;
151
- dy = (y or self .y ) - self .y ;
152
- dz = (z or self .z ) - self .z ;
153
- self .f = self .espeed ;
151
+ local dx = (x or self .x ) - self .x ;
152
+ local dy = (y or self .y ) - self .y ;
153
+ local dz = (z or self .z ) - self .z ;
154
+ local de = self .epermm * math.sqrt ( dx * dx + dy * dy + dz * dz );
155
+ self .f = self .espeed ;
154
156
self .x = self .x + dx ;
155
- self .y = self .y + dy ;
156
- self .z = self .z + dz ;
157
- self .e = self .e + self .epermm * math.sqrt ( dx * dx + dy * dy + dz * dz );
157
+ self .y = self .y + dy ;
158
+ self .z = self .z + dz ;
159
+ self .e = self .e + de ;
160
+ self .totale = self .totale + de ;
158
161
self :emit ();
159
162
end ;
160
163
@@ -174,39 +177,39 @@ function gcode.fan(self,fan)
174
177
self :emit ();
175
178
fan = fan or 0 ; -- default is off
176
179
if fan < 0 then fan = 0 ; end ;
177
- if fan > 100 then fan = 100 ; end ;
180
+ if fan > 100 then fan = 100 ; end ;
178
181
if fan == 0 then
179
- self :write (" M107 (fan off)" );
180
- else
181
- self :write (" M106 S" .. fan * 2.55 .. " (set fan on S=0..255)" );
182
- end ;
182
+ self :write (" M107 (fan off)" );
183
+ else
184
+ self :write (" M106 S" .. fan * 2.55 .. " (set fan on S=0..255)" );
185
+ end ;
183
186
end ;
184
187
185
188
-- emit the current position to the file, emit nothing if no value has changed (or if only F has changed)
186
189
function gcode .emit (self , extra )
187
190
s = " " ;
188
- if self .lx - self .x ~= 0 then s = s .. " X" .. round (self .x ,self .decimals ); end ;
189
- if self .ly - self .y ~= 0 then s = s .. " Y" .. round (self .y ,self .decimals ); end ;
190
- if self .lz - self .z ~= 0 then s = s .. " Z" .. round (self .z ,self .decimals ); end ;
191
- if self .le - self .e ~= 0 then s = s .. " E" .. round (self .e ,self .edecimals ); end ;
192
- self .lx = self .x ;
193
- self .ly = self .y ;
194
- self .lz = self .z ;
195
- self .le = self .e ;
196
- if s ~= " " then
197
- if self .lf - self .f ~= 0 then s = s .. " F" .. round (self .f ,1 ); self .lf = self .f ; end ;
198
- s = " G1" .. s .. (extra or " " );
199
- self :write (s );
200
- end ;
191
+ if self .lx - self .x ~= 0 then s = s .. " X" .. round (self .x ,self .decimals ); end ;
192
+ if self .ly - self .y ~= 0 then s = s .. " Y" .. round (self .y ,self .decimals ); end ;
193
+ if self .lz - self .z ~= 0 then s = s .. " Z" .. round (self .z ,self .decimals ); end ;
194
+ if self .le - self .e ~= 0 then s = s .. " E" .. round (self .e ,self .edecimals ); end ;
195
+ self .lx = self .x ;
196
+ self .ly = self .y ;
197
+ self .lz = self .z ;
198
+ self .le = self .e ;
199
+ if s ~= " " then
200
+ if self .lf - self .f ~= 0 then s = s .. " F" .. round (self .f ,1 ); self .lf = self .f ; end ;
201
+ s = " G1" .. s .. (extra or " " );
202
+ self :write (s );
203
+ end ;
201
204
end ;
202
205
203
206
-- Hop to new location, this causes z to raise, xy to move and z to lower again (to original z, or new z if supplied)
204
207
function gcode .hop (self , x , y , dz , z )
205
208
dz = dz or 1.0 ; -- default is 1 mm
206
209
cz = self .z ;
207
210
self :move (nil ,nil ,self .z + dz ); self :emit ();
208
- self :move (x ,y ); self :emit ();
209
- self :move (nil ,nil ,z or cz ); self :emit ();
211
+ self :move (x ,y ); self :emit ();
212
+ self :move (nil ,nil ,z or cz ); self :emit ();
210
213
end ;
211
214
212
215
-- round to nearest decimal places
0 commit comments