Hello Guest it is April 25, 2024, 08:57:25 PM

Author Topic: Z home before cycle start  (Read 1131 times)

0 Members and 1 Guest are viewing this topic.

Z home before cycle start
« on: February 22, 2022, 03:26:14 PM »
Hi Guys,

I'm absolutely new to CNC and dumb at MACH4. And even dumber to scripting.
Can somebody blease help me how to modify the cycle strat script, like when I press the cycle start button every time the Z axis should go to 0 and than execute the G code?

I've managed to do that with the go to work zero:

-- Go To Work Zero() function.
---------------------------------------------------------------
function GoToWorkZero()
    --mc.mcCntlMdiExecute(inst, "G00 X0 Y0 A0")--Without Z moves
    mc.mcCntlMdiExecute(inst, "G00 G53 Z0\nG00 X0 Y0\nG00 Z0")--With Z moves
end
---------------------------------------------------------------
-- Cycle Start() function.
---------------------------------------------------------------
function CycleStart()   
   local rc
    local tab, rc = scr.GetProperty("MainTabs", "Current Tab")
    local tabG_Mdione, rc = scr.GetProperty("nbGCodeMDI1", "Current Tab")
   local tabG_Mditwo, rc = scr.GetProperty("nbGCodeMDI2", "Current Tab")
   local state = mc.mcCntlGetState(inst)
   mc.mcCntlMdiExecute(inst, "G00 Z0")--With Z moves
   --mc.mcCntlSetLastError(inst,"tab == " .. tostring(tab))
   
   if (state == mc.MC_STATE_MRUN_MACROH) then
      mc.mcCntlCycleStart(inst)
   elseif ((tonumber(tab) == 0 and tonumber(tabG_Mdione) == 1)) then 
      scr.ExecMdi('mdi1')
   elseif ((tonumber(tab) == 5 and tonumber(tabG_Mditwo) == 1)) then 
      scr.ExecMdi('mdi2')
   else
      mc.mcCntlCycleStart(inst)   
   end
end

Thank you for your help in advance!

Offline Bill_O

*
  •  563 563
    • View Profile
Re: Z home before cycle start
« Reply #1 on: February 23, 2022, 12:28:02 PM »
I am far from a Lua expert but here is my custom cycle start.
You might be able to find what you need it it.

---------------------------------------------------------------
-- Cycle Start() function.
---------------------------------------------------------------
function CycleStart()
    local rc;
    local tab, rc = scr.GetProperty("MainTabs", "Current Tab")
    local tabG_Mdione, rc = scr.GetProperty("nbGCodeMDI1", "Current Tab")
    local tabG_Mditwo, rc = scr.GetProperty("nbGCodeMDI2", "Current Tab")
    ------------------------------------------------------------------------------------
   --if machine home not done exit run with message
   MachHmdRegH = mc.mcRegGetHandle(inst, 'iRegs0/MachHmd')
   MachHmd = mc.mcRegGetValue(MachHmdRegH)
   ATCOnRegH = mc.mcRegGetHandle(inst, 'iRegs0/ATCOnOff')
   ATCOn = mc.mcRegGetValue(ATCOnRegH)
   if (MachHmd == 0) and (ATCOn == 1)then
      wx.wxMessageBox('Please Set Machine Home Before Clicking Run')
   else
   ------------------------------------------------------------------------------------
      --See if we have to do an MDI command and if so, which one
      if ((tonumber(tab) == 0 and tonumber(tabG_Mdione) == 1) or (tonumber(tab) == 2 and tonumber(tabG_Mditwo) == 1 )) then
         local state = mc.mcCntlGetState(inst);
         if (state == mc.MC_STATE_MRUN_MACROH) then
            mc.mcCntlCycleStart(inst);
            --mc.mcCntlSetLastError(inst, "Do Cycle Start");
         else
            if (tonumber(tab) == 0) then 
               scr.ExecMdi('mdi1');
               --mc.mcCntlSetLastError(inst, "Do MDI 1");
            else
               scr.ExecMdi('mdi2');
               --mc.mcCntlSetLastError(inst, "Do MDI 2");
            end
         end
      elseif tonumber(tab) > 2 then --No G Code or MDI panel is displayed so Do Nothing
         --mc.mcCntlSetLastError(inst, "Nothing to Start");
      else --Do CycleStart
         --mc.mcCntlSetLastError(inst, "Do Cycle Start");
         
         --if project home or surface not set exit run with message
         hregHm1 = mc.mcRegGetHandle(inst, 'iRegs0/ProjHm1Set')
         ProjHmd1 = mc.mcRegGetValue(hregHm1)
         hregHm2 = mc.mcRegGetHandle(inst, 'iRegs0/ProjHm2Set')
         ProjHmd2 = mc.mcRegGetValue(hregHm2)
         hreg2 = mc.mcRegGetHandle(inst, 'iRegs0/SurfSet')
         SurfHmd = mc.mcRegGetValue(hreg2)
         
         --Set time on Maintinance timer
         local hMaint = mc.mcRegGetHandle(inst, 'iRegs0/MaintTime')
         local MaintTime = mc.mcRegGetValue(hMaint)
         local hRunning = mc.mcRegGetHandle(inst, 'iRegs0/RunningTime')
         local RunningTime = mc.mcRegGetValue(hRunning)
         MaintTime = (MaintTime + RunningTime)
         mc.mcRegSetValue(hMaint, MaintTime)
         mc.mcRegSetValue(hRunning, 0.0)
         
         --get offset number
         local hregPHN = mc.mcRegGetHandle(inst, 'iRegs0/ProjHmPos')
         local ProjHmNum = mc.mcRegGetValue(hregPHN)
         
         --get if in tk mode and if homed
         local TKOnOff = mc.mcCntlGetParameter(502)
         local TKHmdH = mc.mcSignalGetHandle(inst, mc.OSIG_HOMED_A)
         local TKHmdV = mc.mcSignalGetState(TKHmdH)
               
            if (ProjHmNum == 1) then
               if (ProjHmd1 == 0) then
                  wx.wxMessageBox('Please Set Project 1 Home Before Clicking Run')
               elseif (SurfHmd == 0) then
                  wx.wxMessageBox('Please Set Surface Before Clicking Run')
               elseif (TKOnOff == 1) and (TKHmdV == 0) then
                  wx.wxMessageBox('Please Home TK Before Clicking Run')
               else               
                  --get and set home 1 offsets
                  hregX1 = mc.mcRegGetHandle(inst, 'iRegs0/ProjHm1X')
                  ValX1 = mc.mcRegGetValue(hregX1)
                  hregY1 = mc.mcRegGetHandle(inst, 'iRegs0/ProjHm1Y')
                  ValY1 = mc.mcRegGetValue(hregY1)
                  MachX = mc.mcAxisGetMachinePos(inst, 0)
                  MachY = mc.mcAxisGetMachinePos(inst, 1)
                  PosSetX1 = (MachX - ValX1)
                  PosSetY1 = (MachY - ValY1)
               
                  mc.mcAxisSetPos(inst, 0, PosSetX1)
                  mc.mcAxisSetPos(inst, 1, PosSetY1)
                  
                  --regenerate toolpath
--                  mc.mcToolPathGenerate(inst)
                  
                  --if machine homed check if file larger than soft limits
                  hregMHmd = mc.mcRegGetHandle(inst, 'iRegs0/MachHmd')
                  valMHmd = mc.mcRegGetValue(hregMHmd)
                  if (valMHmd == 1) then
                     MaxX = mc.mcAxisGetSoftlimitMax(inst, 0)
                     MaxY = mc.mcAxisGetSoftlimitMax(inst, 1)
                     hregPathXMax = mc.mcRegGetHandle(inst, 'core/inst/PathXmax')
                     PathXmax = mc.mcRegGetValue(hregPathXMax)
                     hregPathYMax = mc.mcRegGetHandle(inst, 'core/inst/PathYmax')
                     PathYmax = mc.mcRegGetValue(hregPathYMax)
                     
                     MinX = mc.mcAxisGetSoftlimitMin(inst, 0)
                     MinY = mc.mcAxisGetSoftlimitMin(inst, 1)
                     hregPathXMin = mc.mcRegGetHandle(inst, 'core/inst/PathXmin')
                     PathXmin = mc.mcRegGetValue(hregPathXMin)
                     hregPathYMin = mc.mcRegGetHandle(inst, 'core/inst/PathYmin')
                     PathYmin = mc.mcRegGetValue(hregPathYMin)
                     
                     if (PathXmax >= MaxX) or (PathYmax >= MaxY) or (PathXmin <= MinX) or (PathYmin <= MinY) then
                        wx.wxMessageBox("Cut file is outside cut area.\nContinuing may damage machine.")
                     end
                  end
                  
               mc.mcCntlCycleStart(inst);

               end
            end
            if (ProjHmNum == 2) then
               if (ProjHmd2 == 0) then
                  wx.wxMessageBox('Please Set Project 2 Home Before Clicking Run')
               elseif (SurfHmd == 0) then
                  wx.wxMessageBox('Please Set Surface Before Clicking Run')
               elseif (TKOnOff == 1) and (TKHmdV == 0) then
                  wx.wxMessageBox('Please Home TK Before Clicking Run')
               else               
                  --get and set home 2 offsets
                  hregX2 = mc.mcRegGetHandle(inst, 'iRegs0/ProjHm2X')
                  ValX2 = mc.mcRegGetValue(hregX2)
                  hregY2 = mc.mcRegGetHandle(inst, 'iRegs0/ProjHm2Y')
                  ValY2 = mc.mcRegGetValue(hregY2)
                  MachX = mc.mcAxisGetMachinePos(inst, 0)
                  MachY = mc.mcAxisGetMachinePos(inst, 1)
                  PosSetX2 = (MachX - ValX2)
                  PosSetY2 = (MachY - ValY2)
               
                  mc.mcAxisSetPos(inst, 0, PosSetX2)
                  mc.mcAxisSetPos(inst, 1, PosSetY2)
                  
                  --regenerate toolpath
--                  mc.mcToolPathGenerate(0)
                  
                  --if machine homed check if file larger than soft limits
                  hregMHmd = mc.mcRegGetHandle(inst, 'iRegs0/MachHmd')
                  valMHmd = mc.mcRegGetValue(hregMHmd)
                  if (valMHmd == 1) then
                     MaxX = mc.mcAxisGetSoftlimitMax(inst, 0)
                     MaxY = mc.mcAxisGetSoftlimitMax(inst, 1)
                     hregPathXMax = mc.mcRegGetHandle(inst, 'core/inst/PathXmax')
                     PathXmax = mc.mcRegGetValue(hregPathXMax)
                     hregPathYMax = mc.mcRegGetHandle(inst, 'core/inst/PathYmax')
                     PathYmax = mc.mcRegGetValue(hregPathYMax)
                     
                     MinX = mc.mcAxisGetSoftlimitMin(inst, 0)
                     MinY = mc.mcAxisGetSoftlimitMin(inst, 1)
                     hregPathXMin = mc.mcRegGetHandle(inst, 'core/inst/PathXmin')
                     PathXmin = mc.mcRegGetValue(hregPathXMin)
                     hregPathYMin = mc.mcRegGetHandle(inst, 'core/inst/PathYmin')
                     PathYmin = mc.mcRegGetValue(hregPathYMin)
                     
                     if (PathXmax >= MaxX) or (PathYmax >= MaxY) or (PathXmin <= MinX) or (PathYmin <= MinY) then
                        wx.wxMessageBox("Cut file is outside cut area.\nContinuing may damage machine.")
                     end
                  end
                  
               mc.mcCntlCycleStart(inst);
               end
            end
      end
   end
end
Re: Z home before cycle start
« Reply #2 on: March 03, 2022, 08:04:05 PM »
hi bill
i try many options but cant change the machine x position in any way
this function that you use:
mc.mcAxisSetPos(inst, 0, PosSetX1)
not make any change on x value
not talk about:
mc.mcAxisHomeAll(   number mInst) ===>>> that do nothing
 also i try with modify the pound parameter:
local er =mc.mcCntlSetPoundVar(0, 5021, 100)====>>do nothing


several month ago i asked about this ,but not real help from mach side ,only suggest like use G52 ( sorry but its bad advice)
thanks

yaakov


Re: Z home before cycle start
« Reply #3 on: March 03, 2022, 09:45:49 PM »
Hi,

Quote
try many options but cant change the machine x position in any way
this function that you use:
mc.mcAxisSetPos(inst, 0, PosSetX1)
not make any change on x value

That is 100% correct. The only way to change the machine coordinate is to move that axis. When you <home> or <Ref> the axis it will move to the homes switch,
if fitted and enabled, and then zero the machine coordinate or set to some offset value if programmed. Thats it. The ONLY two means of changing the machine coorinate
of an axis are to move it OR <Ref> it. Mach4 has always been like that and so was Mach3 before it.

There is one work-around trick that you can use, although its not recommended. If no Home switch is assigned or enabled for an axis you can 'Home in Place', and
in combination with a programmable offset value you can in effect set the machine coordinate of an axis at will.

The following API's will help:

Code: [Select]
rc = mc.mcAxisGetHomeInPlace(
number mInst,
number axisId,
number homeInPlace)

rc = mcAxisSetHomeOffset(
number mInst,
number axisId,
number offset);


Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Z home before cycle start
« Reply #4 on: March 04, 2022, 01:39:02 AM »
craig
actually i use this "trick" always ,because i use absolute encoder ,and this only way i can "homing"
BUT this work ONLY when press button,not on M function,not on Events ...nothing
but its must have some way do it automatic not manually....

Offline Bill_O

*
  •  563 563
    • View Profile
Re: Z home before cycle start
« Reply #5 on: March 04, 2022, 08:56:12 AM »
Quote
this function that you use:
mc.mcAxisSetPos(inst, 0, PosSetX1)
not make any change on x value

PosSetX1 is a variable.
If you did not set PosSetX1 to a number first it will not work.
Re: Z home before cycle start
« Reply #6 on: March 04, 2022, 08:59:59 AM »
I set of course but nothing help
Nothing changes
Re: Z home before cycle start
« Reply #7 on: March 04, 2022, 09:17:08 AM »
i even cheack the  return value .its give 0 mean no erro but actualy do nothing

  local er=mc.mcAxisSetPos(0, 0, 2000) 
            
mc.mcCntlSetLastError(0,tostring(er))
Re: Z home before cycle start
« Reply #8 on: March 04, 2022, 10:16:19 AM »
this main problem with mach if steave not online ...its to hard solve problem  ):
Re: Z home before cycle start
« Reply #9 on: March 04, 2022, 10:20:19 AM »
You can home axes via gcode using G28.1