Skip to content

Instantly share code, notes, and snippets.

@wolfmanjm
Created November 2, 2019 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolfmanjm/3840e8e95c03362bda54ffeb191b4378 to your computer and use it in GitHub Desktop.
Save wolfmanjm/3840e8e95c03362bda54ffeb191b4378 to your computer and use it in GitHub Desktop.
# issue probe commands to smoothie
require 'optparse'
require 'ostruct'
require 'rubyserial'
class Optparse
def self.parse(args)
options = OpenStruct.new
options.verbose = false
options.port= '/dev/ttyACM1'
options.baud= 115200
options.job= 'size'
options.width= 3*25.4;
options.length= 2*25.4;
options.z= 10;
options.dia= 4
options.feed_rate= 1200 # mm/min
options.steps_mm= 80;
opt_parser= OptionParser.new do |opts|
opts.banner = "Usage: probe-macros.rb [options]"
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
options.verbose = v
end
opts.on( "-b", "--baud BAUD", Integer,
"set baud rate" ) do |opt|
options.baud= opt
end
opts.on( "-p", "--port SERIALPORT", String,
"port to open (#{options.port})" ) do |n|
options.port= n
end
opts.on( "-j", "--job TYPE", String,
"The job to run one of [size|center|radius]" ) do |n|
options.job= n
end
opts.on( "-w", "--width WIDTH", Float,
"The width of the object being probed (#{options.width})" ) do |n|
options.width= n
end
opts.on( "-l", "--length LENGTH", Float,
"The length of the object being probed (#{options.length})" ) do |n|
options.length= n
end
opts.on( "-z", "--safe-height Z", Float,
"Safe Height to move to above object (#{options.z})" ) do |opt|
options.z= opt
end
opts.on( "-t", "--tool-diameter DIA", Float,
"diameter of tool (#{options.dia})" ) do |opt|
options.dia= opt
end
opts.on( "-f", "--feed-rate FR", Float,
"feed rate to use" ) do |opt|
options.feed_rate= opt
end
opts.on( "-s", "--step-per-mm", Float,
"steps per mm (#{options.steps_mm})" ) do |opt|
options.steps_mm= opt
end
end
opt_parser.parse!(ARGV)
options
end
end
$options = Optparse.parse(ARGV)
puts "Opening Smoothie at #{$options.port}"
@verbose= $options.verbose
@serialport = Serial.new $options.port, $options.baud
# read from the port until we get the [PRB:1.000,80.137,10.000:0] response
def readPRB
prb= OpenStruct.new
prb.ok= false
loop do
l= @serialport.gets # read a line
puts "DEBUG: #{l}" if @verbose
# [PRB:1.000,80.137,10.000:0]
if l.start_with?("[PRB:")
a= l.split(':')
if a[2][0] == '1'
c= a[1].split(',')
prb.ok= true
prb.x= c[0].to_f
prb.y= c[1].to_f
prb.z= c[2].to_f
end
break
end
end
prb
end
def send(arg)
puts "DEBUG: sending #{arg}" if @verbose
@serialport.write(arg + "\n")
end
def moveBy(x: 0, y: 0, down: true)
send("G91 G0 Z#{$options.z} F#{$options.feed_rate} G0 X#{x} Y#{y}")
send("G0 Z#{-$options.z}") if down
end
def probe(axis, amount)
send("G38.3 #{axis.to_s.upcase}#{amount}")
r= readPRB
puts "DEBUG: got: #{r}" if $verbose
raise "Probe failed" unless r.ok
r
end
def probe_size
puts "Position tool about 10mm to the left of the object to measure"
r1= probe(:x, 20)
moveBy(x: $options.width+10)
send("G4 P2")
r2= probe(:x, -20) # probe left
width= r2.x - r1.x - $options.dia;
puts "Width= #{width}, expected= #{$options.width}, difference= #{$options.width-width}, steps= #{($options.width-width)*$options.steps_mm}"
# center in X and in front of Y face
moveBy(x: -width/2-$options.dia/2, y: -$options.length/2-10)
send("G4 P2")
r1= probe(:y, 20)
moveBy(y: $options.length+10)
send("G4 P2")
r2= probe(:y, -20) # probe negative Y
length= r2.y - r1.y - $options.dia;
puts "Length= #{length}, expected= #{$options.length}, difference= #{$options.length-length}, steps= #{($options.length-length)*$options.steps_mm}"
# center in Y
moveBy(y: -length/2-$options.dia/2, down: false)
end
if $options.job == 'size'
probe_size
else
puts "job #{$options.job} Not yet supported"
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment