Hello Guest it is May 13, 2024, 02:41:37 AM

Show Posts

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.


Messages - Bill_O

231
Mach4 General Discussion / Re: Scripting Custom Control Panel
« on: February 02, 2021, 10:52:40 AM »
I made a pendant not a control panel but same thing for button.
Here is how I make my buttons work.
You can do a lot with these and how Lua/Mach4 function.

[mc.ISIG_INPUT0] = function (state)
    if (state == 1) then
      CycleStart()
   end
end,

[mc.ISIG_INPUT1] = function (state)
    if (state == 1) then
      mc.mcCntlRewindFile(inst)
   end
end,

[mc.ISIG_INPUT2] = function (state)
    if (state == 1) then
      CycleStop()
   end
end,

[mc.ISIG_INPUT3] = function (state)
   if (state == 1) then
      if (Pause == 1) then
         mc.mcCntlCycleStart(inst)
         Pause = 0
      elseif (Pause == 0) then
         mc.mcCntlFeedHold(inst)
         Pause = 1
      end
   end
end,

[mc.ISIG_INPUT4] = function (state)
      if (state == 0) then
         mc.mcCntlEnable(inst, true)
      else
         mc.mcCntlEnable(inst, false)
      end
end,

[mc.ISIG_INPUT20] = function (state)
    local AltRegH = mc.mcRegGetHandle(inst, 'iRegs0/Alt')
   if (state == 1) then
      mc.mcRegSetValue(AltRegH, 1)      
   else
      mc.mcRegSetValue(AltRegH, 0)   
   end
end,

[mc.ISIG_INPUT5] = function (state)
    local AltRegH = mc.mcRegGetHandle(inst, 'iRegs0/Alt')
   local AltVal = mc.mcRegGetValue(AltRegH)
   if (state == 1) then
      if (AltVal == 0) then
      GoToWorkZero()
      elseif (AltVal == 1) then
      SetWorkZero()
      end
   end
end,

[mc.ISIG_INPUT6] = function (state)
    local AltRegH = mc.mcRegGetHandle(inst, 'iRegs0/Alt')
   local AltVal = mc.mcRegGetValue(AltRegH)
   if (state == 1) then
      if (AltVal == 0) then
      ReturnToPosition()
      elseif (AltVal == 1) then
      RememberPosition()
      end
   end
end,

[mc.ISIG_INPUT7] = function (state)
    local AltRegH = mc.mcRegGetHandle(inst, 'iRegs0/Alt')
   local AltVal = mc.mcRegGetValue(AltRegH)
   if (state == 1) then
      if (AltVal == 1) then
         wait = coroutine.create (RefAllHome)
      end
   end
end,




[mc.ISIG_INPUT8] = function (state)
   local AltRegH = mc.mcRegGetHandle(inst, 'iRegs0/Alt')
   local AltVal = mc.mcRegGetValue(AltRegH)
      if (state == 1) then
      if (AltVal == 0) then
      Surface()
      elseif (AltVal == 1) then
      MaxDepth()
      end      
   end
end,

[mc.ISIG_INPUT11] = function (state)
      local AltRegH = mc.mcRegGetHandle(inst, 'iRegs0/Alt')
   local AltVal = mc.mcRegGetValue(AltRegH)
   if (state == 1) then
      if (AltVal == 0) then
      CutSpeedMinus()
      elseif (AltVal == 1) then
      CutSpeedPlus()
      end            
   end
end,

[mc.ISIG_INPUT12] = function (state)
   local AltRegH = mc.mcRegGetHandle(inst, 'iRegs0/Alt')
   local AltVal = mc.mcRegGetValue(AltRegH)   
      if (state == 1) then
      if (AltVal == 1) then
      ExtraDepth()
      end      
   end
end,

[mc.ISIG_INPUT15] = function (state)
      if (state == 1) then
      SpeedsReset()
   end
end,

[mc.ISIG_INPUT16] = function (state)
      local AltRegH = mc.mcRegGetHandle(inst, 'iRegs0/Alt')
   local AltVal = mc.mcRegGetValue(AltRegH)   
      if (state == 1) then
      if (AltVal == 0) then
      MistOnOff()
      elseif (AltVal == 1) then
      SpindleOnOff()
      end                  
   end
end,

[mc.ISIG_INPUT19] = function (state)
      local AltRegH = mc.mcRegGetHandle(inst, 'iRegs0/Alt')
   local AltVal = mc.mcRegGetValue(AltRegH)
   if (state == 1) then
      if (AltVal == 0) then
      JogSpeedMinus()
      elseif (AltVal == 1) then
      JogSpeedPlus()
      end            
   end
end,

[mc.ISIG_INPUT21] = function (state)
    local AltRegH = mc.mcRegGetHandle(inst, 'iRegs0/Alt')
   local AltVal = mc.mcRegGetValue(AltRegH)
   if (state == 1) then
      if (AltVal == 0) then
         local MachHmdreg = mc.mcRegGetHandle(inst, 'iRegs0/MachHmd')
         local MachHmd = mc.mcRegGetValue(MachHmdreg)
         if (MachHmd == 0) then
            wx.wxMessageBox("Must set Machine Home")
         elseif (MachHmd == 1) then
--            mc.mcCntlGcodeExecute(inst, "m6")
            mc.mcCntlMdiExecute(inst, 'm6')
         end
      elseif (AltVal == 1) then
         local hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6)
         local sigOut6 = mc.mcSignalGetState(hsig)
         if (sigOut6 == 0) then
            mc.mcSignalSetState(hsig, 1)
         else
            mc.mcSignalSetState(hsig, 0)
         end
      end
   end
end,

[mc.ISIG_INPUT22] = function (state)
      local AltRegH = mc.mcRegGetHandle(inst, 'iRegs0/Alt')
   local AltVal = mc.mcRegGetValue(AltRegH)
   if (state == 1) then
      if (AltVal == 0) then
      
      elseif (AltVal == 1) then
      
      end            
   end
end,

[mc.ISIG_INPUT25] = function (state)
    if (state == 1) then
      local Tool = mc.mcToolGetSelected(inst)
      --get max tools
      local ATCMaxTlsReg = mc.mcRegGetHandle(inst, 'iRegs0/ATCMaxTools')
      local ATCMaxTlsVal = mc.mcRegGetValue(ATCMaxTlsReg)   
      --if tool is 100(cam), 101(tks) or 102(tko) set tool to 0
      if (Tool == 100) or (Tool == 101) or (Tool == 102) then
         Tool = 0
      end
      --if tool is in range add one to tool #
      if (Tool <= (ATCMaxTlsVal - 1)) then
         Tool = (Tool + 1)
         mc.mcCntlSetPoundVar(inst, mc.SV_CUR_SELECTED_TOOL, Tool)
      end
   end
end,

[mc.ISIG_INPUT26] = function (state)
   local AltRegH = mc.mcRegGetHandle(inst, 'iRegs0/Alt')
   local AltVal = mc.mcRegGetValue(AltRegH)   
      if (state == 1) then
      if (AltVal == 1) then
         local TKSReg = mc.mcRegGetHandle(inst, 'iRegs0/TKStdOnOff')
         local TKSRegVal = mc.mcRegGetValue(TKSReg)
         local TKOReg = mc.mcRegGetHandle(inst, 'iRegs0/TKOscOnOff')
         local TKORegVal = mc.mcRegGetValue(TKOReg)
         local TKSSigH = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT7)
         local TKOSigH = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT8)
         local TKOscSigH = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT9)

         local ATCOnOffReg = mc.mcRegGetHandle(inst, 'iRegs0/ATCOnOff')
         local ATCOnOffVal = mc.mcRegGetValue(ATCOnOffReg)
         local ATCToolHReg = mc.mcRegGetHandle(inst, 'iRegs0/ATCToolHolder')

         local SMReg = mc.mcRegGetHandle(inst, 'iRegs0/SMOnOff')
         local SMRegVal = mc.mcRegGetValue(SMReg)   
         
         mc.mcRegSetValue(TKSReg, 0)
         mc.mcSignalSetState(TKSSigH, 0)
         mc.mcRegSetValue(TKOReg, 0)
         mc.mcSignalSetState(TKOSigH, 0)
         mc.mcSignalSetState(TKOscSigH, 0)
         
         --if atc on change current tool to tool holder tool
         if (ATCOnOffVal == 1) then
            local CurTool = mc.mcToolGetCurrent(inst)
            local ATCToolHVal = mc.mcRegGetValue(ATCToolHReg)
            if (CurTool == 100) or (CurTool == 101) or (CurTool == 102) then
               mc.mcToolSetCurrent(inst, ATCToolHVal)
               mc.mcCntlGcodeExecuteWait(inst, string.format("G43 H" .. tostring(ATCToolHVal)))
               mc.mcCntlSetPoundVar(inst, mc.SV_HEAD_SHIFT_X, 0.000)
               mc.mcCntlSetPoundVar(inst, mc.SV_HEAD_SHIFT_Y, 0.000)
            end
         end
         
         --turn off tang knife mode
         mc.mcCntlSetParameter(502, 0)
         mc.mcCntlSetLastError(inst, 'TK OFF')
         if (SMTempOn == 1) then
            mc.mcRegSetValue(SMReg, 1)
            SMTempOn = 0
         end
         
         --mc.mcRegSetValue(TKSReg, TKSRegVal)
         KeyTKSTemp = 0
         --mc.mcRegSetValue(TKOReg, TKORegVal)
         KeyTKOTemp = 0
      end      
   end
end,

[mc.ISIG_INPUT27] = function (state)
    local AltRegH = mc.mcRegGetHandle(inst, 'iRegs0/Alt')
   local AltVal = mc.mcRegGetValue(AltRegH)
   if (state == 1) then
      if (AltVal == 1) then
         KeyTKSTemp = 1
         TKSOnOff()
      end
   end
end,

[mc.ISIG_INPUT28] = function (state)
    local AltRegH = mc.mcRegGetHandle(inst, 'iRegs0/Alt')
   local AltVal = mc.mcRegGetValue(AltRegH)
   if (state == 1) then
      if (AltVal == 1) then
         KeyTKOTemp = 1
         TKOOnOff()
      end
   end
end,

[mc.ISIG_INPUT29] = function (state)
    if (state == 1) then
      local Tool = mc.mcToolGetSelected(inst)
      --get max tools
      local ATCMaxTlsReg = mc.mcRegGetHandle(inst, 'iRegs0/ATCMaxTools')
      local ATCMaxTlsVal = mc.mcRegGetValue(ATCMaxTlsReg)   
      --if tool is 100(cam), 101(tks) or 102(tko) set tool to MaxTool
      if (Tool == 100) or (Tool == 101) or (Tool == 102) then
         Tool = (ATCMaxTlsVal + 1)
      end
      --if tool is in range subtract one from tool #
      if (Tool >= 2) then
         Tool = (Tool - 1)
         mc.mcCntlSetPoundVar(inst, mc.SV_CUR_SELECTED_TOOL, Tool)
      end
   end
end,

[mc.ISIG_INPUT30] = function (state)
      local AltRegH = mc.mcRegGetHandle(inst, 'iRegs0/Alt')
   local AltVal = mc.mcRegGetValue(AltRegH)
   if (state == 1) then
      if (AltVal == 0) then
      
      elseif (AltVal == 1) then
         local hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5)
         local sigOut5 = mc.mcSignalGetState(hsig)
         if (sigOut5 == 0) then
            mc.mcSignalSetState(hsig, 1)
         else
            mc.mcSignalSetState(hsig, 0)
         end
      end            
   end
end,

[mc.ISIG_INPUT31] = function (state)
      if (state == 1) then
      wait = coroutine.create (RefTKHome)
   end
end,

[mc.ISIG_INPUT32] = function (state)
    local AltRegH = mc.mcRegGetHandle(inst, 'iRegs0/Alt')
   local AltVal = mc.mcRegGetValue(AltRegH)
   local Sig9H = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT9)
   local Sig9Val = mc.mcSignalGetState(Sig9H)
   if (state == 1) then
      if (AltVal == 1) then
         if (Sig9Val == 0) then
            mc.mcSignalSetState(Sig9H, 1)
            --wx.wxMilliSleep(1000)
         else
            mc.mcSignalSetState(Sig9H, 0)
            --wx.wxMilliSleep(1000)
         end
      end
   end
end,


232
Brian,

The head shifts work great.

Bill

233
The register  hreg = mc.mcRegGetHandle(inst, 'iRegs0/MachHmd')  is just a register i made and use in other scripts to ensure the machine has been Referenced.

I do the G53 and G54 two times because i was noticing occasionally depending on what happened before some settings were not actually changing to zero. This fixed that problem and it is no time at all for the pc to perform. I do not remember why i put the sleep in.

I do know how to do the multiple lines of code like you showed it is just harder for me to read so i dont. Lazy

234
And last my M6;
function m6()
   local inst = mc.mcGetInstance()
   --get registers
   local hregMHmd = mc.mcRegGetHandle(inst, 'iRegs0/MachHmd')
   local valMHmd = mc.mcRegGetValue(hregMHmd)
   local ATCReg = mc.mcRegGetHandle(inst, 'iRegs0/ATCOnOff')
   local ATCRegVal = mc.mcRegGetValue(ATCReg)
   local MTCReg = mc.mcRegGetHandle(inst, 'iRegs0/ManToolChg')
   local MTCRegVal = mc.mcRegGetValue(MTCReg)
   local ATCClrReg = mc.mcRegGetHandle(inst, 'iRegs0/ATCZClear')
   local ATCClrVal = mc.mcRegGetValue(ATCClrReg)
   local ATCDnReg = mc.mcRegGetHandle(inst, 'iRegs0/ATCZDown')
   local ATCDnVal = mc.mcRegGetValue(ATCDnReg)
   local ATCYP1Reg = mc.mcRegGetHandle(inst, 'iRegs0/ATCYPos1')
   local ATCYP1Val = mc.mcRegGetValue(ATCYP1Reg)
   local ATCYPDReg = mc.mcRegGetHandle(inst, 'iRegs0/ATCYPosDist')
   local ATCYPDVal = mc.mcRegGetValue(ATCYPDReg)
   local ATCMaxTlsReg = mc.mcRegGetHandle(inst, 'iRegs0/ATCMaxTools')
   local ATCMaxTlsVal = mc.mcRegGetValue(ATCMaxTlsReg)
   
   local ATCToolHReg = mc.mcRegGetHandle(inst, 'iRegs0/ATCToolHolder')
   
   --tk signals
   local TKSSigH = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT7)
   local TKOSigH = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT8)
   local TKOscSigH = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT9)
   
   --tk registers
   local TKSReg = mc.mcRegGetHandle(inst, 'iRegs0/TKStdOnOff')
   local TKOReg = mc.mcRegGetHandle(inst, 'iRegs0/TKOscOnOff')
   local TKOnOffReg = mc.mcRegGetHandle(inst, 'iRegs0/TKOnOff')
   local TKOnOffVal = mc.mcRegGetValue(TKOnOffReg)
   
   --get tool numbers
   local NewTool = mc.mcToolGetSelected(inst)
   local OldTool = mc.mcToolGetCurrent(inst)
   
   --if max depth set turn off
   local MinZRegH = mc.mcRegGetHandle(inst, 'iRegs0/MaxDepth')
   local MinZ2RegH = mc.mcRegGetHandle(inst, 'iRegs0/MaxDepth2')
   local MinZ = mc.mcCntlGetParameter(1801)
   if (MinZ >= -20.0) then
      mc.mcRegSetValue(MinZ2RegH, MinZ)
      mc.mcCntlSetParameter(1801, -22.0)
      mc.mcRegSetValue(MinZRegH, -22.0)
   end
   
   --if new tool is in range do atc / mtc
   if (NewTool <= ATCMaxTlsVal) or (NewTool == 100) or (NewTool == 101) or (NewTool == 102) then
      --if manual tool change off do atc
      if (MTCRegVal == 0) then
         --if new tool is same as old tool
         if (NewTool == OldTool) then
            mc.mcCntlSetLastError(inst, "Same Tool")
         else   
            --turn off tool comp and change to machine coordanants
            mc.mcCntlGcodeExecuteWait(inst, "g49")
            mc.mcCntlGcodeExecuteWait(inst, "g53")
            
            --if mist on turn off and set MistWasON
            local hMistWas = mc.mcSignalGetHandle(inst, mc.OSIG_MISTON)
            local MistWasVal = mc.mcSignalGetState(hMistWas)
            mc.mcSignalSetState(hMistWas, 0)
            if (MistWasVal == 1) then
               local hrMistWas = mc.mcRegGetHandle(inst, 'iRegs0/MistWasOn')
               mc.mcRegSetValue(hrMistWas, 1)
            elseif (MistWasVal == 0) then
               local hrMistWas = mc.mcRegGetHandle(inst, 'iRegs0/MistWasOn')
               mc.mcRegSetValue(hrMistWas, 0)
            end
            
            --if spindle on turn off and set SpinWasOn
            local hSpinWas = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON)
            local SpinWasVal = mc.mcSignalGetState(hSpinWas)
            mc.mcSignalSetState(hSpinWas, 0)
            if (SpinWasVal == 1) then
               local hrSpinWas = mc.mcRegGetHandle(inst, 'iRegs0/SpinWasOn')
               mc.mcRegSetValue(hrSpinWas, 1)
               
               local hrSpinType = mc.mcRegGetHandle(inst, 'iRegs0/SpindleType')
               local SpinType = mc.mcRegGetValue(hrSpinType)
               if (SpinType == 0) then
                  wx.wxMilliSleep(1500)
               elseif (SpinType == 1) then
                  wx.wxMilliSleep(5500)
               elseif (SpinType == 2) then
                  wx.wxMilliSleep(28000)
               end
               
            elseif (SpinWasVal == 0) then
               local hrSpinWas = mc.mcRegGetHandle(inst, 'iRegs0/SpinWasOn')
               mc.mcRegSetValue(hrSpinWas, 0)
            end
            
            --if machine homed and atc on do tool change
            if (valMHmd == 1) and (ATCRegVal == 1) then
               local NewTool = mc.mcToolGetSelected(inst)
               local OldTool = mc.mcToolGetCurrent(inst)
            
               --is new tool camera
               if (NewTool == 100) then
                  --wx.wxMessageBox("Camera")
                  mc.mcCntlSetLastError(inst, "CAMERA")
            
                  -- do shift to cam position
                  headshift()

                  --put current tool in tool holder and set current tool to 100
                  if (OldTool <= ATCMaxTlsVal) then
                     mc.mcRegSetValue(ATCToolHReg, OldTool)
                  end
                  mc.mcToolSetCurrent(inst, 100)

               --is new tool Tang Knife Standard
               elseif (NewTool == 101) then
            
                  -- do shift to tks position
                  headshift()
                  --lift head to clear for knife lower
                  local TempTKPos = string.format("G0 G53 Z"..ATCDnVal)
                  mc.mcCntlGcodeExecuteWait(inst, TempTKPos)
                  
                  --wx.wxMessageBox("Tang Knife Std")
                  mc.mcCntlSetLastError(inst, "TANG KNIFE STD")
                  --put current tool in tool holder and set current tool to 101
                  if (OldTool <= ATCMaxTlsVal) then
                     mc.mcRegSetValue(ATCToolHReg, OldTool)
                  end
                  mc.mcToolSetCurrent(inst, 101)
                  --turn on tk mode
                  mc.mcCntlSetParameter(502, 1)
                  mc.mcRegSetValue(TKOnOffReg, 1)
                  
                  --turn on and off tks reg and signals
                  mc.mcRegSetValue(TKOReg, 0)
                  mc.mcRegSetValue(TKSReg, 1)
                  mc.mcSignalSetState(TKSSigH, 1)
                  mc.mcSignalSetState(TKOSigH, 0)
                  mc.mcSignalSetState(TKOscSigH, 0)
                  
               --is new tool Tang Knife Oscillating
               elseif (NewTool == 102) then
         
                  headshift()
                  --lift head to clear for knife lower
                  local TempTKPos = string.format("G0 G53 Z"..ATCDnVal)
                  mc.mcCntlGcodeExecuteWait(inst, TempTKPos)
                  
                  mc.mcCntlSetLastError(inst, "TANG KNIFE OSC")
                  
                  --put current tool in tool holder and set current tool to 102
                  if (OldTool <= ATCMaxTlsVal) then
                     mc.mcRegSetValue(ATCToolHReg, OldTool)
                  end
                  mc.mcToolSetCurrent(inst, 102)
                  --turn on tk mode
                  mc.mcCntlSetParameter(502, 1)
                  mc.mcRegSetValue(TKOnOffReg, 1)
                  
                  --turn on and off tko reg and signals
                  mc.mcRegSetValue(TKOReg, 1)
                  mc.mcRegSetValue(TKSReg, 0)
                  mc.mcSignalSetState(TKSSigH, 0)
                  mc.mcSignalSetState(TKOSigH, 1)
                  mc.mcSignalSetState(TKOscSigH, 1)
                  
                  -- do shift to tko position
                  
               --is new tool atc tool
               elseif (NewTool >= 1) and (NewTool <= ATCMaxTlsVal) then
                  
                  --if old tool is cam, tks or tko then put old tool back to tool holder value
                  if (OldTool == 100) or (OldTool == 101) or (OldTool == 102) then
               
                     headshift()
                     
                     local ATCToolHolder = mc.mcRegGetValue(ATCToolHReg)
                     OldTool = ATCToolHolder
                     --turn off tk mode
                     mc.mcCntlSetParameter(502, 0)
                     --retract tks and tko
                     local TKSRetReg = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT7)
                     local TKORetReg = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT8)
                     local TKOOscReg = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT9)
                     mc.mcSignalSetState(TKSRetReg, 0)
                     mc.mcSignalSetState(TKORetReg, 0)
                     mc.mcSignalSetState(TKOOscReg, 0)
                     --set registers to tk off
                     local TKStdReg = mc.mcRegGetHandle(inst, 'iRegs0/TKStdOnOff')
                     local TKOscReg = mc.mcRegGetHandle(inst, 'iRegs0/TKOscOnOff')
                     local TKOOReg  = mc.mcRegGetHandle(inst, 'iRegs0/TKOsc')
                     mc.mcRegSetValue(TKStdReg, 0)
                     mc.mcRegSetValue(TKOscReg, 0)
                     mc.mcRegSetValue(TKOOReg, 0)
                     --turn off tk mode
                     mc.mcCntlSetParameter(502, 0)
                     mc.mcRegSetValue(TKOnOffReg, 0)
                     --turn on and off tks and tko reg and signals
                     mc.mcRegSetValue(TKOReg, 0)
                     mc.mcRegSetValue(TKSReg, 0)
                  end
                  
                  --if old tool is not cam, tks or tko drop old tool
                  if (NewTool == OldTool) then
                     mc.mcCntlSetLastError(inst, "SAME TOOL TO DROP")
                  else
                  if (OldTool <= ATCMaxTlsVal) then
                     --get tool drop position
                     local TempToolMinus = (OldTool - 1)
                     local TempToolDist = (TempToolMinus * ATCYPDVal)
                     local YTempPos = (ATCYP1Val + TempToolDist)
                     --move to tool drop position
                     local TempDropPos = string.format("G0 G53 Y"..YTempPos.."Z"..ATCDnVal)
                     mc.mcCntlGcodeExecuteWait(inst, TempDropPos)
                     --fire rack
                     local Rackhsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6)
                     mc.mcSignalSetState(Rackhsig, 1)
                     wx.wxMilliSleep(3000)
                     --drop tool
                     local DBarhsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5)
                     mc.mcSignalSetState(DBarhsig, 1)
                     wx.wxMilliSleep(1000)
                     --raise z to clear
                     ZTempUpPos = string.format("G0 G53 Z"..ATCClrVal)
                     mc.mcCntlGcodeExecuteWait(inst, ZTempUpPos)
                     --retract rack to clear
                     local Rackhsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6)
                     mc.mcSignalSetState(Rackhsig, 0)
                     wx.wxMilliSleep(2250)
                  end
                  end
                  -- if new tool is standard atc number pick up new tool
                  if (NewTool == OldTool) then
                     mc.mcCntlSetLastError(inst, "SAME TOOL TO PICK")
                  else
                  if (NewTool <= ATCMaxTlsVal) then   
                     --fire draw bar
                     local DBarhsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5)
                     mc.mcSignalSetState(DBarhsig, 1)
                     --get tool pickup position
                     local TempToolMinus = (NewTool - 1)
                     local TempToolDist = (TempToolMinus * ATCYPDVal)
                     local YTempPos = (ATCYP1Val + TempToolDist)
                     --move to tool pickup position
                     local TempGrabPos = string.format("G0 G53 Y"..YTempPos.."Z"..ATCClrVal)
                     mc.mcCntlGcodeExecuteWait(inst, TempGrabPos)
                     --fire rack
                     local Rackhsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6)
                     mc.mcSignalSetState(Rackhsig, 1)
                     wx.wxMilliSleep(3000)
                     --lower z to grab
                     ZTempDnPos = string.format("G0 G53 Z"..ATCDnVal)
                     mc.mcCntlGcodeExecuteWait(inst, ZTempDnPos)
                     --grab tool
                     local DBarhsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5)
                     mc.mcSignalSetState(DBarhsig, 0)
                     wx.wxMilliSleep(1000)
                     --retract rack to clear
                     local Rackhsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6)
                     mc.mcSignalSetState(Rackhsig, 0)
                     wx.wxMilliSleep(2250)
                  end
                  end
               end
            end
         end
         mc.mcToolSetCurrent(inst, NewTool)
         wx.wxMilliSleep(10)
         mc.mcCntlGcodeExecuteWait(inst, "g54")
         mc.mcCntlGcodeExecuteWait(inst, string.format("G43 H" .. tostring(NewTool)))
         --if mist was on turn back on
         local hrMistWas = mc.mcRegGetHandle(inst, 'iRegs0/MistWasOn')
         local MistWasVal = mc.mcRegGetValue(hrMistWas)
         if (MistWasVal == 1) then
            local hMistWas = mc.mcSignalGetHandle(inst, mc.OSIG_MISTON)
            mc.mcSignalSetState(hMistWas, 1)
         end   
      else
         --manual tool change
         mc.mcCntlGcodeExecuteWait(inst, "G49")
         --raise z
         MTCPos = mc.mcAxisGetPos(inst, 2)
         MTCPos = (MTCPos + .5)
         mc.mcCntlGcodeExecuteWait(inst, string.format("G0 Z" .. tostring(MTCPos)))
         --stop spindle
         local hSpindle = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON)
         mc.mcSignalSetState(hSpindle, 0)         
         --wx.wxMessageBox("Manual Tool Change")
         mc.mcCntlToolChangeManual(inst, true)
--         wx.wxMessageBox("Replace Tool\nSurface Tool")
--         --set surface register to 0
--         hreg = mc.mcRegGetHandle(inst, 'iRegs0/SurfSet')
--         mc.mcRegSetValue(hreg, 0)
--         --turn surface set led off
--         scr.SetProperty('ledSurfaceSet', 'Value', '0')
         
--         SMHit == 0
         
         --surface
--         --get sm on or off register
--         local SMReg = mc.mcRegGetHandle(inst, 'iRegs0/SMOnOff')
--         local SMRegVal = mc.mcRegGetValue(SMReg)
--         mc.mcCntlGcodeExecuteWait(inst, "G49")
--         mc.mcToolSetCurrent(inst, 0)
--         --surface tool
--         --If SM turned OFF
--         if (SMRegVal == 0) then
            --set to 0 and lift 0.5
            wx.wxMilliSleep(100)
            mc.mcAxisSetPos(inst, 2, 0.0)
            wx.wxMilliSleep(100)
            mc.mcCntlGcodeExecuteWait(inst, "g0 z0.5")
--            mc.mcCntlMdiExecute(inst, "g0 z0.5")
            wx.wxMilliSleep(500)
--            SMHit = 1
--         end
--         --If SM turned ON
--         if (SMRegVal == 1) then
--            --set jog speed to 100%
--            scr.SetProperty('sliJogRate', 'Value', tostring(100))
--            --wx.wxMessageBox("SM Used")
--            mc.mcCntlGcodeExecuteWait(inst, "g31 z-8.0 f20.0")
--            --check to see if sm hit
--            SMHit = 0
--            SMHit, rc = mc.mcCntlProbeGetStrikeStatus(inst)
--            if (SMHit == 1) then
--               mc.mcAxisSetPos(inst, 2, 1.5)
--               --wx.wxMilliSleep(100)
--               mc.mcCntlGcodeExecuteWait(inst, "g0 z2.0")
--            else
--               wx.wxMessageBox("DID NOT HIT SURFACE MOUSE")
--               wx.wxMessageBox("MUST CLICK RESET BEFORE NEXT ATTEMPT")
--            end
--         end

--         if (SMHit == 1) then
--            hreg = mc.mcRegGetHandle(inst, 'iRegs0/SurfSet')
--            mc.mcRegSetValue(hreg, 1)
--            --turn surface set led on
--            scr.SetProperty('ledSurfaceSet', 'Value', '1')
--         end
--         mc.mcCntlToolChangeManual(inst, true)
      end   
   else
      --tool number out of range
      wx.wxMessageBox("New Tool Number is too large.")
      mc.mcCntlCycleStop(inst)
   end   
   local MinZTemp = mc.mcRegGetValue(MinZ2RegH)
   if (MinZTemp >= -20.0) then
      mc.mcCntlSetParameter(1801, tonumber(MinZTemp))
      mc.mcRegSetValue(MinZRegH, MinZTemp)
      mc.mcRegSetValue(MinZ2RegH, -22.0)
   end
end
if (mc.mcInEditor() == 1) then
   m6()
end

235
And here is my zero for the Zero X.
I do both x and y at the same time.

Button;
wait = coroutine.create (SetWorkZero)

Function script;
Once again a lot of extra stuff.
---------------------------------------------------------------------------
--Set Work Zero() function
-----------------------------------------------------------------------------
function SetWorkZero()
   --remove head shifts
   mc.mcCntlSetPoundVar(inst, mc.SV_HEAD_SHIFT_X, 0.000)
   mc.mcCntlSetPoundVar(inst, mc.SV_HEAD_SHIFT_Y, 0.000)
   --get offset number
   local hregPHN = mc.mcRegGetHandle(inst, 'iRegs0/ProjHmPos')
   local ProjHmNum = mc.mcRegGetValue(hregPHN)
   if (ProjHmNum == 1) then
      --turn project home 1 set led off
      scr.SetProperty('ledProjHm1Set', 'Value', '0')
      --set to project coordinates
      mc.mcCntlGcodeExecute (inst, "g54")
      --set coordinates to 0
      mc.mcAxisSetPos(inst, 0, 0.0)
      mc.mcAxisSetPos(inst, 1, 0.0)
      --get and set 1x offset
      local Offset1X = mc.mcCntlGetOffset(inst, 0, 5201)
      hregX1 = mc.mcRegGetHandle(inst, 'iRegs0/ProjHm1X')
      mc.mcRegSetValue(hregX1, Offset1X)
      --get and set 1y offset
      local Offset1Y = mc.mcCntlGetOffset(inst, 1, 5201)
      hregY1 = mc.mcRegGetHandle(inst, 'iRegs0/ProjHm1Y')
      mc.mcRegSetValue(hregY1, Offset1Y)
      
      --set Project Home 1 register to 1
      hreg = mc.mcRegGetHandle(inst, 'iRegs0/ProjHm1Set')
      mc.mcRegSetValue(hreg, 1)
      --turn project home 1 set led on
      scr.SetProperty('ledProjHm1Set', 'Value', '1')
      
      --rewind file
      mc.mcCntlRewindFile(inst)
      
      --regenerate toolpath
      mc.mcToolPathGenerate(inst)
      
   end
   if (ProjHmNum == 2) then
      --turn project home 2 set led off
      scr.SetProperty('ledProjHm2Set', 'Value', '0')
      --set to project coordinates
      mc.mcCntlGcodeExecute (inst, "g54")
      --set coordinates to 0
      mc.mcAxisSetPos(inst, 0, 0.0)
      mc.mcAxisSetPos(inst, 1, 0.0)
      --get and set 1x offset
      local Offset2X = mc.mcCntlGetOffset(inst, 0, 5201)
      hregX2 = mc.mcRegGetHandle(inst, 'iRegs0/ProjHm2X')
      mc.mcRegSetValue(hregX2, Offset2X)
      --get and set 2y offset
      local Offset2Y = mc.mcCntlGetOffset(inst, 1, 5201)
      hregY2 = mc.mcRegGetHandle(inst, 'iRegs0/ProjHm2Y')
      mc.mcRegSetValue(hregY2, Offset2Y)
      
      --set Project Home 2 register to 1
      hreg = mc.mcRegGetHandle(inst, 'iRegs0/ProjHm2Set')
      mc.mcRegSetValue(hreg, 1)
      --turn project home 2 set led on
      scr.SetProperty('ledProjHm2Set', 'Value', '1')
      
      --rewind file
      mc.mcCntlRewindFile(inst)
      
      --regenerate toolpath
      mc.mcToolPathGenerate(inst)
      
   end
end

236
I did a lot of custom stuff in our mach4.
The script has a lot of extra stuff but it does work to zero everything and then do a move.

Here is the code from my Referance button;
-RefAllHome()
wait = coroutine.create (RefAllHome) --Run the RefAllHome function as a coroutine named wait.
--See RefAllHome function in screen load script for coroutine.yield and PLC script for coroutine.resume


Here is my script from RefAllHome;
---------------------------------------------------------------
-- Ref All Home() function.
---------------------------------------------------------------
function RefAllHome()
   
   --turn off max depth
   local MaxDepRegH = mc.mcRegGetHandle(inst, 'iRegs0/MaxDepth')
   mc.mcRegSetValue(MaxDepRegH, -22.0)
   mc.mcCntlSetParameter(1801, -22.0)
   scr.SetProperty('ledMaxDepth', 'Value', '1')
   --  set head shift to 0
   mc.mcCntlSetPoundVar(inst, mc.SV_HEAD_SHIFT_X, 0.000)
   mc.mcCntlSetPoundVar(inst, mc.SV_HEAD_SHIFT_Y, 0.000)
   mc.mcCntlSetPoundVar(inst, mc.SV_HEAD_SHIFT_Z, 0.000)
   --turn off Tang
   mc.mcCntlGetParameter(502)
   mc.mcCntlSetParameter(502, 0) --tk off
   mc.mcCntlSetLastError(inst, 'TK OFF')
    mc.mcAxisDerefAll(inst)  --Just to turn off all ref leds
   mc.mcCntlGcodeExecuteWait(inst, "G49") --turn off tool height comp
   --set Project Home 1 registers and led to 0
   hreg = mc.mcRegGetHandle(inst, 'iRegs0/ProjHm1Set')
   mc.mcRegSetValue(hreg, 0)
   scr.SetProperty('ledProjHm1Set', 'Value', '0')
   hreg1X = mc.mcRegGetHandle(inst, 'iRegs0/ProjHm1X')
   mc.mcRegSetValue(hreg1X, 0)
   hreg1Y = mc.mcRegGetHandle(inst, 'iRegs0/ProjHm1Y')
   mc.mcRegSetValue(hreg1Y, 0)
   --set Project Home 2 registers and led to 0
   hreg = mc.mcRegGetHandle(inst, 'iRegs0/ProjHm2Set')
   mc.mcRegSetValue(hreg, 0)
   scr.SetProperty('ledProjHm2Set', 'Value', '0')
   hreg2X = mc.mcRegGetHandle(inst, 'iRegs0/ProjHm2X')
   mc.mcRegSetValue(hreg2X, 0)
   hreg2Y = mc.mcRegGetHandle(inst, 'iRegs0/ProjHm2Y')
   mc.mcRegSetValue(hreg2Y, 0)
   --set Surface register and led to 0
   hreg = mc.mcRegGetHandle(inst, 'iRegs0/SurfSet')
   mc.mcRegSetValue(hreg, 0)
   scr.SetProperty('ledSurfaceSet', 'Value', '0')
   --reference all
    mc.mcAxisHomeAll(inst)
    coroutine.yield() --yield coroutine so we can do the following after motion stops
    ----See ref all home button and plc script for coroutine.create and coroutine.resume
    -------------------------------------------------
   --set machine home register to 1
   hreg = mc.mcRegGetHandle(inst, 'iRegs0/MachHmd')
   mc.mcRegSetValue(hreg, 1)
   -----------------------------------------------
   --set extra cut depth to 0 and turn off leds
   local Extrareg = mc.mcRegGetHandle(inst, 'iRegs0/ExtraDepth')
   mc.mcRegSetValue(Extrareg, 0)
   mc.mcCntlSetPoundVar(inst, mc.SV_HEAD_SHIFT_Z, 0.000)
   scr.SetProperty('led010', 'Value', '0')
   scr.SetProperty('led030', 'Value', '0')
   scr.SetProperty('led050', 'Value', '0')
   --Set all positions to 0
   mc.mcCntlGcodeExecute(inst, "G53")
   mc.mcCntlGcodeExecute(inst, "G92 x0.0y0.0z0.0a0.0")
   mc.mcCntlGcodeExecute(inst, "G54")
   mc.mcCntlGcodeExecute(inst, "G92 x0.0y0.0z0.0a0.0")
   mc.mcAxisSetPos(inst, 0, 0.0)
   mc.mcAxisSetPos(inst, 1, 0.0)
   mc.mcAxisSetPos(inst, 2, 0.0)
   mc.mcAxisSetPos(inst, 3, 0.0)
   mc.mcAxisSetHomeOffset(inst, 0, 0.0)
   mc.mcAxisSetHomeOffset(inst, 1, 0.0)
   mc.mcAxisSetHomeOffset(inst, 2, 0.0)
   mc.mcAxisSetHomeOffset(inst, 3, 0.0)
   mc.mcCntlGcodeExecute(inst, "G53")
   mc.mcCntlGcodeExecute(inst, "G92 x0.0y0.0z0.0a0.0")
   mc.mcCntlGcodeExecute(inst, "G54")
   mc.mcCntlGcodeExecuteWait(inst, "G92 x0.0y0.0z0.0a0.0")
   mc.mcCntlGcodeExecuteWait(inst, "G1 x0.1 y0.1 F100.0")
   wx.wxMilliSleep(100)
   --rewind file
   mc.mcCntlRewindFile(inst)
   --regenerate toolpath
   mc.mcToolPathGenerate(inst)
end

237
Mach4 General Discussion / Merry Christmas
« on: December 18, 2020, 11:10:32 AM »
Well I am off for the next 3 weeks so I wanted to wish everyone a Merry Christmas and a Happy New Year.

Bill

238
Mach4 General Discussion / Re: G31 not working in screen function
« on: December 18, 2020, 08:56:07 AM »
True, but I do not need to know the exact position Mach does.
When it is done is fine for me.

239
Mach4 General Discussion / Re: G31 not working in screen function
« on: December 17, 2020, 03:45:58 PM »
Use mc.mcCntlGcodeExecuteWait

240
Mach4 General Discussion / Re: G31 not working in screen function
« on: December 17, 2020, 10:53:15 AM »
Glad I could help someone.
I have gotten a lot of help on here.