Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: Plasma629 on November 26, 2019, 06:02:07 PM

Title: Mach 4 - Manual Tool Change via Probe Touch off M6 Macro help
Post by: Plasma629 on November 26, 2019, 06:02:07 PM
Hey guys I have a custom M6 macro which has been working for about a year, i recently upgraded Mach 4 and ESS (likely not following best practices) and now the same M6 does not work the same, my Z does not even move and Mach 4 gives message "ESS Starting Probe 0" and system is stuck at this point.

I also had the functionality to adjust the X & Y location along with feed rate for probe move and probe thickness offset easily by going to Mach 4 top left menu -> diagnostic -> Regfile - New fangled Solutions and a popup would pop up where the above variables were listed with the option of editing, now when i do the same nothing opens up... i feel like i shot myself in the leg.
M6:
Quote
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

As always any help would be appreciated!
Title: Re: Mach 4 - Manual Tool Change via Probe Touch off M6 Macro help
Post by: Plasma629 on November 26, 2019, 06:04:08 PM
Double post.