Skip to content

Instantly share code, notes, and snippets.

@oddstr13
Created September 30, 2017 18:23
Show Gist options
  • Save oddstr13/99aebd9e71bcaa49dd30e1e4e5a6f7a3 to your computer and use it in GitHub Desktop.
Save oddstr13/99aebd9e71bcaa49dd30e1e4e5a6f7a3 to your computer and use it in GitHub Desktop.
Generates a pattern for testing laser PWM functionality
function readnumber(::Type{T}, default=nothing, prompt=nothing) where T<:Number
while true
if prompt == nothing
prompt = "$T>"
end
if default == nothing
print("$prompt> ")
else
print("$prompt ($default)> ")
end
line = readline()
if length(line) == 0 && default != nothing
return default
end
try
return parse(T,line)
end
end
end
readnumber(default=nothing, prompt=nothing) = readnumber(Int128, default, prompt)
pwmmax = readnumber(UInt32, 255, "PWM Max value")
pwmincrement = readnumber(1, "PWM Increment")
cellsize = readnumber(8, "Cell size in mm")
resolution = readnumber(10, "Resolution in px/mm")
offset = readnumber(Float64, 0.5, "Box offset from border in mm")
workspeed = readnumber(1200, "Laser speed in mm/min")
cells = Array(0:pwmincrement:pwmmax)
d = ceil(Integer, sqrt((pwmmax+1)/pwmincrement))
append!(cells, fill(-1, d * d - length(cells)))
matrix = transpose(reshape(cells, (d, d)))
display(matrix)
println()
fh = open("testpattern_0-$(pwmmax)($(pwmincrement))_$(cellsize)mm-$(offset)mm_$(resolution)pxmm_$(workspeed)mmmin.gcode", "w")
off() = println(fh, "M5")
on() = println(fh, "M3")
on(power) = println(fh, "M3 S$(power)")
function goto(x, y, fast::Bool, s::Union{Number,Void})
c = 1
if fast
c = 0
end
if s == nothing
println(fh, "G$c X$x Y$y")
else
println(fh, "G$c X$x Y$y F$s")
end
end
goto(x, y, s::Union{Number,Void}) = goto(x, y, false, s)
goto(x, y, fast::Bool) = goto(x, y, fast, nothing)
goto(x, y) = goto(x, y, false, nothing)
println(fh, "M306 X0 Y0; Set origin to current position")
println(fh, "G21; Units in mm")
println(fh, "G90; Use absolute coordinates")
off()
goto(0, 0, true)
println(fh, "")
println((d, cellsize, d*cellsize))
for x in 0:1:d
if x%2 == 0
goto(x*cellsize, 0, true)
on()
goto(x*cellsize, d*cellsize, workspeed)
off()
else
goto(x*cellsize, d*cellsize, true)
on()
goto(x*cellsize, 0, workspeed)
off()
end
end
for y in d:-1:0
if y%2 == 0
goto(0, y*cellsize, true)
on()
goto(d*cellsize, y*cellsize, workspeed)
off()
else
goto(d*cellsize, y*cellsize, true)
on()
goto(0, y*cellsize, workspeed)
off()
end
end
goto(0, 0, true)
for y in 0:d-1, x in 0:d-1
value = matrix[y+1,x+1]
if value < 0
continue
end
xs = Float64(x*cellsize)
ys = Float64(y*cellsize)
for (i, ly) in enumerate(ys+offset:1/resolution:ys+cellsize-offset)
if i % 2 == 0
goto(xs+cellsize-offset, ly, true)
on(value)
goto(xs+offset, ly, workspeed)
off()
else
goto(xs+offset, ly, true)
on(value)
goto(xs+cellsize-offset, ly, workspeed)
off()
end
end
end
off()
flush(fh)
close(fh)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment