Skip to content

Commit 7107c77

Browse files
author
Peter Brier
committed
BMP reading, gcode changes
1 parent e14278f commit 7107c77

File tree

5 files changed

+81
-78
lines changed

5 files changed

+81
-78
lines changed

README

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ a circular shape, with the contour you have drawn in the bitmap.
1313
The bitmap should have a WHITE BACKGROUND and the contour of the shape should
1414
be drawn in BLACK.
1515

16-
LUA (www.lua.org) is used to convert the contour into a shape. Download the right
16+
lua (www.lua.org) is used to convert the contour into a shape. Download the right
1717
lua version (at least version 5.1.4) for your platform, and extract all the files in
1818
the same folder as the revolver scripts. Some versions of linux ship with an older
1919
version of lua (5.1) this may not work. Download the right version (in source

gcode.lua

+75-72
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,19 @@ gcode =
2929
-- create gcode file writer class, return instance of the gcode class
3030
function new(name)
3131
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
3434
lx=0, ly=0, lz=0, le=0, lf=0; -- last emitted position
3535
decimals = 2; -- nr of decimal places in output coordinates
3636
edecimals = 3; -- nr of decimal places E axis output
3737
epermm = 0.1; -- extrusion per mm of travel
3838
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
4041
};
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)
4445
gcode.__index = gcode;
4546
return o
4647
end
@@ -49,29 +50,29 @@ end
4950
function gcode.close(self)
5051
self:emit();
5152
io.close(self.file);
52-
self.file = nil;
53+
self.file = nil;
5354
end;
5455

5556
-- copy the contect of a file into the current gcode file
5657
function gcode.copyfile(self,name)
5758
self:emit();
58-
self:comment("<-- Including file: " .. name .. " -->");
59+
self:comment("<-- Including file: " .. name .. " -->");
5960
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 .. " -->");
6768
end;
6869

6970
-- Calculate the feedrate factor, based on layer width,height and filament diameter, assume square profile
7071
function gcode.escale(self,w,h,d,speed)
7172
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;
7576
end
7677

7778
-- write data to file
@@ -83,39 +84,39 @@ end;
8384
local function dumptable(t)
8485
local s = " {";
8586
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
9091
s = s .. " " ..i .. "=" .. v .. " ";
91-
end;
92-
end;
93-
return s .. "} ";
92+
end;
93+
end;
94+
return s .. "} ";
9495
end;
9596

9697

9798
-- write comment to file
9899
function gcode.comment(self,s,t)
99100
self:emit();
100101
self:write("( " .. s .. " )" );
101-
if t == nil then return; end;
102+
if t == nil then return; end;
102103
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;
110111
end;
111112

112113

113114
-- move current position, pass nil values to axis values to -not- change this field
114115
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;
117118
self.z = z or self.z;
118-
self.e = e or self.e;
119+
self.e = e or self.e;
119120
self.f = f or self.mspeed; -- default is move-speed
120121
end;
121122

@@ -124,37 +125,39 @@ end;
124125
function gcode.setpos(self, x, y, z, e)
125126
self:emit();
126127
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;
130131
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;
132133
end;
133134

134135
-- move home, and set units to mm abs
135136
function gcode.home(self, x, y, z)
136137
self:emit();
137138
s = "";
138139
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)");
142143
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);
145146
end;
146147

147148
-- move to a position, while extruding
148149
function gcode.extrude(self, x, y, z)
149150
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;
154156
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;
158161
self:emit();
159162
end;
160163

@@ -174,39 +177,39 @@ function gcode.fan(self,fan)
174177
self:emit();
175178
fan = fan or 0; -- default is off
176179
if fan < 0 then fan = 0; end;
177-
if fan > 100 then fan = 100; end;
180+
if fan > 100 then fan = 100; end;
178181
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;
183186
end;
184187

185188
-- emit the current position to the file, emit nothing if no value has changed (or if only F has changed)
186189
function gcode.emit(self, extra)
187190
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;
201204
end;
202205

203206
-- 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)
204207
function gcode.hop(self, x, y, dz, z)
205208
dz = dz or 1.0; -- default is 1 mm
206209
cz = self.z;
207210
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();
210213
end;
211214

212215
-- round to nearest decimal places

rendervase.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ end;
117117

118118
function render(self,gc)
119119

120-
self.image = pgm.load(self.contour);
121-
self.timage = pgm.load(self.texture);
120+
self.image = image.load(self.contour);
121+
self.timage = image.load(self.texture);
122122
local img = self.image;
123123
local pos = self.pos;
124124
local w = self.size.width;

revolver.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
-- use: lua revolver.lua [config file]
2525
--
2626
--
27-
require "pgm" -- PGM bitmap file reader
27+
require "image" -- bitmap file reader
2828
require "gcode" -- gcode file writer
2929
require "materials" -- material definitions
3030
require "styles" -- material definitions
@@ -68,7 +68,7 @@ gc:move(0,0);
6868
gc:fan(0);
6969
gc:temperature(shape.material.tstandby);
7070
gc:copyfile("end.gcode");
71-
print("\nDone.\n");
71+
print("\nDone, " .. gcode.round(gc.totale,1) .. "mm extruded.\n");
7272

7373
-- Conversion for visualisation
7474
-- os.execute("../gcode2vtk/gcode2vtk.exe " .. oname);

s-helloworld.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ shape =
3131
min_thickness = 1.0; -- [mm] minimal wall thickness (default is 1 shell) Whem specified, no wall will be made thinner than this thickness, even
3232
-- if the bitmap specifies otherwise
3333
contour = "square.pgm"; -- the image for the shape use "BINARY PGM" image with white background, and black line that defines the shape
34-
texture = "hello.pgm"; -- the image for the surface. Use "BINARY PGM" image. BLACK is "0.0" and White is "1.0" modulation of the radius. Use "modulation" parameter to scale this
34+
texture = "hello.bmp"; -- the image for the surface. Use "BINARY PGM" image. BLACK is "0.0" and White is "1.0" modulation of the radius. Use "modulation" parameter to scale this
3535
modulation = -1; -- [mm] depth of the surface modulation for the texture (0..255 pixel intensity is 0..modulation mm radius change)
3636
-- modulator = function(r,a,z) return 0.1*z* math.sin(7*a); end; -- Specify a function to modulate the surface
3737
-- frequency = 3.0; -- Use default "sine wave" modulation of the radius, with specified frequenty, and "modulation" amplitude

0 commit comments

Comments
 (0)