1
Mach4 General Discussion / Re: Mach 4 - Manual Tool Change via Probe Touch off M6 Macro help
« on: November 26, 2019, 06:04:08 PM »
Double post.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
function m6()
local inst = mc.mcGetInstance('M6 Manual Tool Change Macro')
local selectedtool = mc.mcToolGetSelected(inst)
local currenttool = mc.mcToolGetCurrent(inst)
-- Get register handles, add the registers if they don't exist
local hXpos, hYpos, hZpos, hPfeed, rc, response
hXpos, rc = mc.mcRegGetHandle(inst, "iRegs0/M6/m6Xpos")
if (rc ~= mc.MERROR_NOERROR) then
-- Register does not exis add it
mc.mcCntlSetLastError(inst, "M6: Xpos register does not exist, adding")
response, rc = mcRegAddDel(inst, "ADD", "iRegs0", "M6/m6Xpos", "Machine position for tool change, X Axis", 0, 1)
end
hYpos, rc = mc.mcRegGetHandle(inst, "iRegs0/M6/m6Ypos")
if (rc ~= mc.MERROR_NOERROR) then
-- If register does not exis add it
mc.mcCntlSetLastError(inst, "M6: Ypos register does not exist, adding")
response, rc = mcRegAddDel(inst, "ADD", "iRegs0", "M6/m6Ypos", "Machine position for tool change, Y Axis", 0, 1)
end
hZpos, rc = mc.mcRegGetHandle(inst, "iRegs0/M6/m6Zpos")
if (rc ~= mc.MERROR_NOERROR) then
-- If register does not exis add it
mc.mcCntlSetLastError(inst, "M6: Zpos register does not exist, adding")
response, rc = mcRegAddDel(inst, "ADD", "iRegs0", "M6/m6Zpos", "Machine position for tool change, Z Axis", 0, 1)
end
hPfeed, rc = mc.mcRegGetHandle(inst, "iRegs0/M6/m6Pfeed")
if (rc ~= mc.MERROR_NOERROR) then
-- If register does not exist add it
mc.mcCntlSetLastError(inst, "M6: Probe feed register does not exist, adding")
response, rc = mcRegAddDel(inst, "ADD", "iRegs0", "M6/m6Pfeed", "Probe feed rate for tool touch off, manual tool change", 10, 1)
end
hTouchHeight, rc = mc.mcRegGetHandle(inst, "iRegs0/M6/m6TouchHeight")
if (rc ~= mc.MERROR_NOERROR) then
-- If register does not exist add it
mc.mcCntlSetLastError(inst, "M6: Touch height register does not exist, adding")
response, rc = mcRegAddDel(inst, "ADD", "iRegs0", "M6/m6TouchHeight", "Touch height of the probe, manual tool change", 0, 1)
end
if selectedtool == currenttool then -- Tool already selected, so do nothing
mc.mcCntlSetLastError(inst, "M6: Current tool = selected tool")
else
-- Start tool change sequence
-- Get values for park position for manual tool change
local Zpos = mc.mcRegGetValue(hZpos)
local Xpos = mc.mcRegGetValue(hXpos)
local Ypos = mc.mcRegGetValue(hYpos)
-- Set up gcode line to move machine to the park position
local gline = string.format("G0 G90 G53 Z%.3f\nG0 G53 X%.3f Y%.3f", Zpos, Xpos, Ypos)
-- Execute the gcode line and move machine to the park position
mc.mcCntlGcodeExecuteWait(inst, gline)
-- Execute manual tool change and wait for user to press cycle start button to continue
mc.mcCntlToolChangeManual(inst)
-- Notify user of the tool change and set the current tool to the new tool
mc.mcCntlSetLastError(inst, "M6: Current tool == " .. tostring(selectedtool) .. " Previous Tool == " .. tostring(currenttool))
mc.mcToolSetCurrent(inst, selectedtool)
-- Start tool touch off sequence
local pFeed = mc.mcRegGetValue(hPfeed)
if (pFeed == nil) or (pFeed <= 0) then
-- If pFeed if not specifed or zero or negative, then set a default value
pFeed = 10
end
-- Get Z axis softlimit minimum
local zMin = mc.mcAxisGetSoftlimitMin(inst, mc.Z_AXIS)
zMin = zMin + .001
-- Start probe move
gline = string.format("G31 G90 Z%.3f F%.3f", zMin, pFeed)
mc.mcCntlGcodeExecuteWait(inst, gline)
-- Get probed machine position
local probePos = mc.mcCntlGetPoundVar(inst, 5073)
-- Check that we contacted the probe before reaching the end of the move, with a 0.001 tolerance window
if (math.abs(probePos - zMin) < .001) then
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "M6: Tool probing failed, reached end of travel")
do return end
end
-- Set the offset
XVar, YVar, ZVar, FixNum, CurrentFixture = GetFixOffsetVars() -- Get current fixture information
local touchHeight = mc.mcRegGetValue(hTouchHeight)
local zOffset = tonumber(probePos) - tonumber(touchHeight)
mc.mcCntlSetPoundVar(inst, ZVar, zOffset)
mc.mcCntlSetLastError(inst, string.format("M6: Z axis %s offset set to %.3f", CurrentFixture, probePos))
-- Return to previous Z level
gline = string.format("G0 G53 Z%.3f", Zpos)
mc.mcCntlGcodeExecuteWait(inst, gline)
-- End m6 macro
end
end
-- Function to get the current fixture offset variables
function GetFixOffsetVars()
local FixOffset = mc.mcCntlGetPoundVar(inst, mc.SV_MOD_GROUP_14)
local Pval = mc.mcCntlGetPoundVar(inst, mc.SV_BUFP)
local FixNum, whole, frac
if (FixOffset ~= 54.1) then --G54 through G59
whole, frac = math.modf (FixOffset)
FixNum = (whole - 53)
PoundVarX = ((mc.SV_FIXTURES_START - mc.SV_FIXTURES_INC) + (FixNum * mc.SV_FIXTURES_INC))
CurrentFixture = string.format('G' .. tostring(FixOffset))
else --G54.1 P1 through G54.1 P100
FixNum = (Pval + 6)
CurrentFixture = string.format('G54.1 P' .. tostring(Pval))
if (Pval > 0) and (Pval < 51) then -- G54.1 P1 through G54.1 P50
PoundVarX = ((mc.SV_FIXTURE_EXPAND - mc.SV_FIXTURES_INC) + (Pval * mc.SV_FIXTURES_INC))
elseif (Pval > 50) and (Pval < 101) then -- G54.1 P51 through G54.1 P100
PoundVarX = ((mc.SV_FIXTURE_EXPAND2 - mc.SV_FIXTURES_INC) + (Pval * mc.SV_FIXTURES_INC))
end
end
PoundVarY = (PoundVarX + 1)
PoundVarZ = (PoundVarX + 2)
return PoundVarX, PoundVarY, PoundVarZ, FixNum, CurrentFixture
--PoundVar(Axis) returns the pound variable for the current fixture for that axis (not the pound variables value).
--CurrentFixture returned as a string (examples G54, G59, G54.1 P12).
--FixNum returns a simple number (1-106) for current fixture (examples G54 = 1, G59 = 6, G54.1 P1 = 7, etc).
end
-- Function to add or delete registers
function mcRegAddDel(hInst, mode, device, path, desc, intialVal, persistent) --"mode = ADD" or "DEL",
local hReg
local rc
--local inst = bit32.band(hInst, 0xFF)
local inst = hInst
local cmdstring = mode .. "|" .. path
device = tostring(device)
desc = tostring(desc)
local val = tostring(intialVal)
local persist = "0"
if (type(persistent) == "boolean") then
if (persistent ~= false) then
persist = "1"
end
elseif (type(persistent) == "number") then
if (persistent ~= 0) then
persist = "1"
end
end
--Check to see if the device is available
hReg, rc = mc.mcRegGetHandle(inst, device .. "/command")
if (rc ~= mc.MERROR_NOERROR) then --The device is not available.
return "The command register for the instance registers is not available. Device = " .. device, mc.MERROR_NOT_IMPLEMENTED
end
--Check to see if the register exist
hReg, rc = mc.mcRegGetHandle(inst, device .. "/" .. path)
if (rc == mc.MERROR_NOERROR) and (mode == "ADD") then --Reg already exist so don't create it
--mc.mcCntlSetLastError(inst, device .. "/" .. path .. " Register already exist so we won't try to create it")
return "The register already exist.", mc.MERROR_NOERROR
elseif (rc == mc.MERROR_REG_NOT_FOUND) and (mode == "DEL") then --Reg does not exist so don't try to delete it
--mc.mcCntlSetLastError(inst, device .. "/" .. path .. " Register does not exist so we won't try to delete it")
return "The register does not exist.", mc.MERROR_NOERROR
elseif (rc == mc.MERROR_REG_NOT_FOUND) and (mode == "ADD") then --Reg doesn't exist so add the rest of the parameters and create it.
cmdstring = cmdstring .. "|" .. desc .. "|" .. val .. "|" .. persist
else
end
hReg, rc = mc.mcRegGetHandle(inst, device .. "/command")
response, rc = mc.mcRegSendCommand(hReg, cmdstring)
return response, rc
end
if (mc.mcInEditor() == 1) then
m6()
end
The code below is very similar to the default above. Only instead of just pausing the Gcode it allows you to jog to a probing position and once over the probe, click cycle start. This will begin a probing move. If that probe move is successful it will update the tools height in the tool table. It is very important to have the default move after the probing in the Gcode....... or you could add this default move to the m6 itself but it is not in it as is.Code: [Select]function m6()
local inst = mc.mcGetInstance()
local selectedTool = mc.mcToolGetSelected(inst)
local currentTool = mc.mcToolGetCurrent(inst)
local maxDown = -4.0000 --Max distance Z will go down in the touch routine
local rate = 30 --Feed rate the Z will go down in the touch routine
local safeZ = 0 --Machine coordinates the Z will rapid to after touch move
if selectedTool == currentTool then
mc.mcCntlSetLastError(inst, "Current tool == Selected tool so there is nothing to do")
else
--mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 Z0.0") --Uncomment this line if you would like to move the Z axis to machine coords 0
mc.mcCntlSetLastError(inst, "Change to tool " .. tostring(selectedTool) .. " and press start to touch off") --Message at beginning of tool change
mc.mcCntlToolChangeManual(inst, true) --This will pause the tool change here and wait for a press of cycle start to continue
mc.mcToolSetCurrent(inst, selectedTool)
mc.mcCntlSetLastError(inst, "Performing touch routine")
mc.mcCntlGcodeExecuteWait(inst, string.format("G31 Z%0.4f F%0.4f\nG0 G53 Z%0.4f", tostring(maxDown), tostring(rate), tostring(safeZ))) --Probe move
mc.mcCntlSetLastError(inst, "Touch move complete")
mc.mcCntlSetLastError(inst, "Current tool == " .. tostring(selectedTool) .. " Previous Tool == " .. tostring(currentTool)) --Message that shows after Cycle Start
local didStrike, rc = mc.mcCntlProbeGetStrikeStatus(inst)
if (didStrike == 1) then
--#5063 = User position #5073 = Machine position
value, rc = mc.mcCntlGetPoundVar(inst, mc.SV_PROBE_POS_Z)
--value, rc = mc.mcCntlGetPoundVar(inst, 5063) --Same as line above but line above uses the Mach constant instead of the #var
mc.mcToolSetData(inst, mc.MTOOL_MILL_HEIGHT, selectedTool, value) --Set the tool height to what was determined by probe move
mc.mcCntlGcodeExecute(inst, string.format("G43 H" .. tostring(selectedTool)))
mc.mcCntlSetLastError(inst, string.format("Tool " .. tostring(selectedTool) .. " H offset set to %0.4f", value))
else
mc.mcCntlSetLastError(inst, "The touch move did not touch so we did not set a tool offset.") --Message that shows after Cycle Start
end
end
end
if (mc.mcInEditor() == 1) then
m6()
end
The THC3T is a hardware controller, it adds or subtracts pulses in addition to those coming from the Bob/ESS to the pulse stream
going to the Z axis driver. You realize that the Z axis will lose reference in Mach. Mach has no way to know how many extra
pulses have been added or subtracted and thus the Z axis will lose reference.
Hi,Quote2 start plasma (type:input) -> I have these 2 wired to input pins # 11 & 15 of BoB
No, that is not correct. The manual tells you that you need a dry contact between the two wires. That does not
mean two pins on your BoB. You need one pin on your BoB to turn on a relay and a normally open contact pair
of the relay are hooked to the two start wires of the plasma.
Are you able to guide me as to which pins/relay i should use on my BoB? I have the attachment with all the pins identifiedQuote2 start machine motion (Type:output)-> I have these 2 wired to output pins # 16 & 17 of BoB
No, that is not correct. The manual tells you that this signal is the same as 'Arc OK' and it is signaled by a dry contact closure
between the two wires of the plasma. You would hook one to the 0V of your BoB and the other to one input pin of your
BoB assuming a pull up resistor at the input pin.
Can you guide me as to which pin is 0V? im assuming the 0-10V pin on my BoB?QuoteDIR_IN -> Wired to BoB same pin which is connected to Z axis servo motor DIR-
STEP_IN -> Wired to BoB same pin which is connected to Z axis servo motor STEP-
The THC3T is a hardware controller, it adds or subtracts pulses in addition to those coming from the Bob/ESS to the pulse stream
going to the Z axis driver. You realize that the Z axis will lose reference in Mach. Mach has no way to know how many extra
pulses have been added or subtracted and thus the Z axis will lose reference.QuoteARC_OK -> Wired to BoB input pin # 12
EXT_COM -> BoB ground pin
You could hoo these direct to the 'Arc OK' dry contact closure of the plasma OR have the 'Arc OK' hooked to a Mach input
and have that input reflected to a Mach output which in turn signals the THC3T.
So hook the "Arc OK" from THC to on of the wires from"start plasma input"?
Craig