Hello Guest it is May 14, 2021, 03:29:08 AM

Author Topic: Auto Tool Zero Script  (Read 6290 times)

0 Members and 1 Guest are viewing this topic.

Auto Tool Zero Script
« on: April 26, 2015, 03:09:08 PM »
Hey guys!  I know a lot of you have been looking for this!  Hopefully this will help some of you and I'd be happy to help you adjust it.  

I'm pretty sure it will work though. I have tested everything I can in the simulator, but as the simulator does not support G31 I cannot test a few lines.  If any of you guys would want to volunteer that would be awesome.

Just copy and past the script into the button.  I have included the MCS files for the button, or if you want to use it as a macro.  If you wish to use it as a macro you will need to rename it "m10.mcs".

Do note Mach4 may seem to lock up during movement, as it's receiving the "GCodeExecuteWait" command.  That can be easily changed, but I didn't know how the "GCodeExecute" command would work on a real machine.

At any rate it should get a lot of you started on your own scripts.

Code: [Select]
Author: GB Ward, Create! 2015
Rev: 0.1
Date: 4/25/15
Mach4 4.0.1, Build 2336

Public Domain

Mach4 Core API Version 1.01 (C:\Mach4Hobby\Docs\Mach4CoreAPI.chm)

Mach4 CNC Controller Lua Scripting Guide
Newfangled Solutions 2014

"Motion plugin probing procedure." (C:\Mach4Hobby\Docs\probing.txt)

Down and Dirty "mcLua scripting, quick ref guide"
poppabear, Mach4 English Forums, under "Mach4 Toolbox" board.

Pound Var List - poppabear, Mach4 Forums

McLua mc scripting reference - poppabear, Mach4 Forums


    local mInst = 0;                                        --Sets current controller instance to 0, if running multiple controller instances, this will need to change
    local rc = 0;                                           --Clear rc
    local hSig = 0;                                         --Clear hSig
    local inst = mc.mcGetInstance(mInst);                   --Captures current instance of Mach 4

    local probe = mc.ISIG_PROBE;                            --Set input here, mc.<INPUT_NAME>; input needs to be mapped in Mach4
    local strikePlateThickness = 0.060;                     --What is the thickness of the strikeplate, or how much do you want to offset 0?

    --Function to check to see if probe already grounded
    function checkProbe ()

        hSig, rc = mc.mcSignalGetHandle(inst, probe);       --Load hReg with probe state
        state = mc.mcSignalGetState(hSig);                  --Get Signal State
        if (state == 1) then                                --if hReg true ('if probe is activated then')
            return true
            return false
    end  --checkProbe()

    --Get current Feed rate, G90/G91, G0/G1 states to return to later.
    --Mach4 stores these variables as 'Pound Variables'
    --See Mach4 Lua Scripting Manual Page 23
    local CurrFeedRate = mc.mcCntlGetPoundVar(inst, 2134);              --Gets current Feed Rate, #var 2134
    local CurrFeedMode = mc.mcCntlGetPoundVar(inst, 4001);              --Gets current G0/G1 state, #var 4001
    local CurrPositionMode = mc.mcCntlGetPoundVar(inst, 4003);          --Gets current G90/G91 state, #var 4003
    local zProbeStrikePos = 0;                                          --clear variable
    if checkProbe() then                                                --Check probe status
        mc.mcCntlSetLastError(inst, "ERROR: Probe is already triggered.  Cannot continue.");
        do return end                                                   --"do return end" stops program.  Not sure how this works
    end --check if probe is grounded

    mc.mcAxisSetPos(inst, 2, 0);                                        --"Zero Z" (0 is x, 1 y, and so on)
    mc.mcCntlGcodeExecuteWait(inst, 'G01 G90 G31 Z-4 F4');              --G31.  Machine will stop on probe strike or go to Z-4, store in #var 5063
    local zProbeStrikePos = mc.mcCntlGetPoundVar(inst, 5063);           --Get Z strike position
    local zAxisCurrentPos = mc.mcAxisGetPos(inst, 2);                   --Get Current Z position, sometimes the probe will continue past strike point.
    local zAxisDifference = (zProbeStrikePos - ZAxisCurrentPos);        --Gets overshoot, returns positive
    local zAxisNewOffset = strikePlateThickness - zAxisDifference;      --Subracts overshoot from known thickness of strikeplate
    mc.mcAxisSetPos(inst, 2, zAxisNewOffset);                           --Sets DRO to current position
    --mc.mcAxisSetMachinePos(inst, 2, zAxisNewOffset);                  --Sets Machine to offset if anyone finds this necessary
    mc.mcCntlGcodeExecuteWait(inst, 'G00 G91 Z1');                      --Rapid move to 1 inch above current pos

    local isProbeSafe = checkProbe();                                   --Checks to see if probe is safe
    if isProbeSafe == true then
        wx.wxMessageBox("Probe is still activated!  Check to make sure probe is not damaged or still contacting strike plate.");
        mc.mcCntlSetLastError(inst, "Z Axis now referenced.");
    end --if
« Last Edit: April 26, 2015, 03:16:47 PM by Gerber Baby »
Re: Auto Tool Zero Script
« Reply #1 on: April 26, 2015, 05:34:20 PM »
couple issues I fixed and documented
works in wxmach does not fully work in mach4gui have to kill process
Re: Auto Tool Zero Script
« Reply #2 on: May 14, 2015, 11:27:39 AM »
Will this work in Mach 3?  or does someone have a code which will?  I want to program a button  on a microsoft controller

Thanks in advance

Offline dude1

  •  1,253 1,253
    • View Profile
Re: Auto Tool Zero Script
« Reply #3 on: May 14, 2015, 08:01:35 PM »
its M4 only