Mach 4 lua capabilities

I just attempted to run through a sequence of steps to determine the tool length change measurement.

I did this several times to see what should happen.
If I gather the material z0 and then measure the 2nd plate z0, then get the difference recorded on paper.
Then make the tool stick out further and probe the 2nd plate again, record that position and subtract the last difference, I get the correct offset on paper.

However, if I then change the tool length in the settings, it doesn’t do anything when trying to return to z zero. Seems to be ignored. Is the tool table doing anything in this? I then just decided to change lengths and text where z 0 would be placed and nothing happens. Just returns to the original place.

If I update the g54 offset z position with the difference, then it works. So, I’m guessing that creating an m6 would actually need to update the current work offset z instead of the tool table to make the real change. What does the roll table do with length if not updating the z 0?

Are you using G43 H{insert tool number here} before each touch off?

Calculating tool offsets requires calculating from absolute machine cords so watch out about that.

Also, if you are using the AvidCNC touch off function it will always do a G43 H1 no matter what tool you use. I have complained about that but no response from the factory.

I think @Eric said he would look into it but I haven’t heard back on the topic.

Not when I did my test above. I just did a G31 probe and recorded that position. I do everything in machine coordinates too.

I assumed, which i should not ever do, that entering a different length in the tool screen would change the actual tool length that mach3 would read. i have not used g43 in my script.
I made a replacement m6 macro and I am definitely doing something wrong. I can touch a new tool off and record that but, have no idea how to store the first probing using the avid plate on the surface of the material and then doing the second one on the other plate and storing that difference for reference later.

For sure my m6 macro is basic now. I cannot figure out how to probe 2 times within it. First to search for the plate and then back off and go slow for the actual measurement. I can get it to back off but, it skips the second probing and records the tool length right away after backing off.

I have not tried using the g43 in the macro either, I bet that may help though. Still I do not have the first tool recorded from that second probe position and cannot calculate a difference because I only have surface of material z0.

I think you are talking about the difference in length between tools. I assume you are using a ATC because if you are doing this by hand, you are headed for troubled waters.

You have to precalculate the tool offsets from tool #1. Meaning that tool #1 is always zero (resist the urge to prove me wrong by adding a different value in your tool table, let us simplify and not make things more difficult).

You will need to go through a process of measuring each tool and determining the difference in size in each tool when compared to tool #1 AND THAT VALUE IS PLACED IN THE LENGTH FIELD IN THE TOOL TABLE. Not the actual length of the tool itself.

Clear as mud… I didn’t invent it so…

Now, when that is all correct, you will change tools as follows (assume current tool is #1 and you are going to tool #59);

T59 M6
G43 H59

Line one says change the tool to #59
Line two says to set the OFFSET (the length field in the tool table) using tool #59

Now the tip of tool #59 will be recalculated to be where the Zero was set with tool #1 before the change.

This way you don’t have to re-zero tool #59

If I misunderstood I am terribly sorry. If you are using a ATC, G43 is required. You will find G43 in Mach3 as well but its been so long since I used it I don’t remember the tool table and the rules it follows.

The good news in I believe that Mach3 like Mach4 are FUNAC clones so that would mean the tool table in Mach3 would follow the same rules as Mach4.

I am not using an ATC. That would be awesome but, cost prohibitive.

What I had in mach 3 was from a screenset called mach 2010 screenset. Basically, you have 2 probing points. One fixed and the other moveable for the material. You put in the first tool and run a script that touches the moveable surface plate. Then the tool moves over to the fixed plate and touches there. This establishes surface z0 and calculates the difference between the two plates.

When m6 runs, you switch tools and then the tool moves to the fixed plate and touches. Then it uses the stored difference value to determine this tools length. Return to job and run.

It works amazingly well. No need to have tool offsets. So much faster and allows using one gcode file for the entire job without having to fumble around with multiple files in order and making sure you have an original surface to zero with.
It sounds so simple up front but, it is not for me.

I have no idea how far I will get with this but, I finally took the time to figure out storing and retrieving registers. I have some time today. No idea when I will get more time to work on it. At least I know how to do this part.
next will be writing several functions to call from buttons to do setup. Then figure out the whole probing thing. Then do the math to do the rest. I will slowly get through this and, maybe in a year I will have this functioning again.

Thats pretty cool. If I ran multitool jobs I would definitely want that. It woudl be a nice addition to the touchplate routine. I would also like to have a set of selectable offsets in the routine for use with lasers and other things. I have asked, but I don’t think AVID has the resources to add stuff to that, especially since it probably won’t sell more machines (I agree though, it would be nice if we could get the source so we could edit our own versions instead of making workarounds in seperate functions).

The worst thing about editing screens in Mach4 is that there is literally no documentation for the screen APIs (and a couple others), only the core. So I end up spending a lot of time scanning through the examples in the modules directory looking for examples to figure it out.

Yeah. I have been at it since about 4:30 this morning.

I have been trying to familiarize myself with the layout of all the files that are only available while editing and trying to see the links between everything.

It seems that most of the functions available to other things from avid are stored in the PLC script. I have to understand this a bit more to make sure I do not break anything. I know it is called PLC but, do not know how it actually behaves.

Right now I can store and retrieve values from the register.
Next I am going to get all the pop ups and buttons for next steps in sequence working.
Then I will attempt to get functions I can call throughout to work. I was testing to see if I could just call an M code directly from a button and that then crashed mach and I could not open again because it could not compile. So, that does not work. i assume I need to write this as a function and then call that in the sequence instead.
If I were to call it using an M code, i guess I would just copy the same code into that file and run it from the MDI. I do not see that being needed.

I have values for the fixed plate locations being stored and returned.
next is to write a probing function for the material z0 location
After that is stored, it will run the next function that moves up and over to the fixed plate and probes that. Stores the value and then does the math to get the difference. Store the difference.
I suppose I would then use the first value to set my z0 location.

My M6 routine would then have to move to the mtc location, that I can recall from avids script in the PLC.
After the tool change, call the next function to move to the fixed plate and probe this tool and store that value again. Then do the math to get the difference and store that. Then set the z0 to that new number.

At least, i think that should work. z0 in g54 should be the changed number. When stepping through the routine manually and calculating, that is what I would change to get the new tool back to the correct location on resume.

I am just too lazy :rofl:

I would use a permanent mount and write a script to calculate the difference between the perm mount and the spoilboard.

Solves two problems; cutting through your spoilboard and if you still need the surface cords, run the script again from the surface of the stock.

If you need the STL for the AvidCNC touch-off plate mount I can repost it.

I have never used the spoil board for my output. Mostly because I’m lazy and would have to think differently.

However, thinking about that does actually make more sense in most situations and it would prevent me from cutting through the board. It would also solve some issues with ply being inconsistent and dealing with rabitts fitting. Just putting in the nominal thickness for stock.

That still doesn’t solve the tool change issue for me though. Other than being able to touch off the spoil board if it’s available. Just having the flow of it all without much thought after the fact is nice.

I also want to use this with my 4th and I tend to get stock dimensioned in the 4th to the correct size as part of my first pass.

I’ve worked through setting up an interface for my m6 setup. I’ve also created basic steps in code that calculate everyone and store/recall values for this.

I think I’m almost there with most of that. Just need to do probing instead of the inputs I have for testing. Then I’ll see where it needs work. Now I have to go do things that aren’t this. Hopefully I can revisit soon. Kept a bunch of notes to make sure I can get back on track quickly.

I would think using the 4th axis accurately without an ATC would require a permanent reference.

My 4th axis has a permanent touch-off plate attachment. Did they change that on the newer machines?

I built my 4th myself. I have a place on the setup for referencing my second touch off that I can get the difference from and use that for later tools.

I am working through this script some more now and I have looked through everything I can within the files from avid and the programming guide is not much help now.
How do I programmatically set work coordinate z to 0? I cannot find any reference other than tool offset. I want to change the current work offset z height from a script.

$190/hr + 3 hr minimum since you are a friend :laughing:

-- SNZ Probe X Axis --

local FastFeed = 200
local SlowFeed = 80
local BackOff = 3
local OffsetNum = 99

local ProbeSigTable = {
	[31] = mc.ISIG_PROBE,
	[31.0] = mc.ISIG_PROBE,
	[31.1] = mc.ISIG_PROBE1,
	[31.2] = mc.ISIG_PROBE2,
	[31.3] = mc.ISIG_PROBE3
}

function checkProbe(state, code)
	
	local inst = mc.mcGetInstance()
	
	local check = true
	
	local ProbeSignal = ProbeSigTable[code]
	
	if (ProbeSignal == nil) then
		mc.mcCntlSetLastError(inst, "ERROR: Invalid probing G code")
		mc.mcCntlEStop(inst)
		do return end
	end
	
	------------- Check Probe -------------
	local hsig = mc.mcSignalGetHandle(inst, ProbeSignal)
	local ProbeState = mc.mcSignalGetState(hsig)
	local errmsg = "ERROR: No contact with probe"
	
	if (state == 1) then
		errmsg = "ERROR: Probe obstructed"
	end
	
	if (ProbeState == state) then
		mc.mcCntlSetLastError(inst, errmsg)
		mc.mcCntlEStop(inst)
		check = false
	end
	
	return check
end


function setFixOffset(xval)
	
	local inst = mc.mcGetInstance()
    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))
    else --G54.1 P1 through G54.1 P100
        FixNum = (Pval + 6)
        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
	
	mc.mcCntlSetPoundVar(inst, PoundVarX, xval)
	
end


function m10101()
	
	local inst = mc.mcGetInstance()
	
	local rc = checkProbe(1, 31); if not rc then; do return end; end
	
	------------- Get current machine state -------------
	local CurFeed = mc.mcCntlGetPoundVar(inst, mc.SV_FEEDRATE)
	local CurZOffset = mc.mcCntlGetPoundVar(inst, mc.SV_ORIGIN_OFFSET_Z)
	local CurFeedMode = mc.mcCntlGetPoundVar(inst, mc.SV_MOD_GROUP_1)
	local CurAbsMode = mc.mcCntlGetPoundVar(inst, mc.SV_MOD_GROUP_3)
	local CurPosition = mc.mcAxisGetPos(inst, mc.Z_AXIS)
	
	------------- Set Probe Offset, Relative Positioning -------------
	mc.mcCntlGcodeExecuteWait(inst, string.format("G43 H%.0f", OffsetNum))
	mc.mcCntlGcodeExecuteWait(inst, "G91")
	
	------------- Fast Probe -------------
	mc.mcCntlGcodeExecuteWait(inst, "G31 X50 F200")
	
	rc = checkProbe(0, 31); if not rc then; do return end; end
	
	------------- Back Off Probe -------------
	mc.mcCntlGcodeExecuteWait(inst, "G01 X-3 F350")
	
	------------- Slow Probe -------------
	mc.mcCntlGcodeExecuteWait(inst, "G31 X10 F50")
	
	rc = checkProbe(0, 31); if not rc then; do return end; end
	
	------------- Get Position + Data -------------
	local XProbed = mc.mcCntlGetPoundVar(inst, mc.SV_PROBE_POS_X)
	local XMachProbed = mc.mcCntlGetPoundVar(inst, mc.SV_PROBE_MACH_POS_X)
	
	
	print("Mach Probe X "..XMachProbed)
	
	
	
	local probeRadius = (25.4 * 0.125) / 2
	
	print("Mach Probe X Radius "..probeRadius)
	
	------------- Calculate Offset -------------
	local NewWOVal = XMachProbed + probeRadius
	
	------------- Set Offset-------------
	setFixOffset(NewWOVal)
	
	------------- Safe Z-------------
	--mc.mcCntlGcodeExecuteWait(inst, "G01 X-10 F350")
	
	------------- Reset Machine State ------------------------------------
	mc.mcCntlSetPoundVar(inst, mc.SV_FEEDRATE, CurFeed)
	mc.mcCntlSetPoundVar(inst, mc.SV_MOD_GROUP_1, CurFeedMode)
	mc.mcCntlSetPoundVar(inst, mc.SV_MOD_GROUP_3, CurAbsMode)
end



if (mc.mcInEditor() == 1) then
	m10101()
end

I would eliminate the G43 H99. My probe is set to #99 and I don’t use a touch plate. I have been meaning to make that script a library but i never get around to it. It was written as a test file before I added the buttons to my screen.

EDIT;

local probeRadius = (25.4 * 0.125) / 2

can be replaced as well. That would be the diameter of your probe. Make it a constant of read from a register.

Very ugly script :rofl:

Well, you’ve given me some things to work through. I appreciate sharing.

I know the person at artsoft left. I’m just kind of surprised that there are so many intelisense pop ups when using the editor and I can’t find a reference for everything available. Going back and forth between all the places where code exists across the system looking for something in thousands of lines that could work, maybe.

That is a open source UI. I use the C/C++ documentation and references from the Lua site. It’s a mess.

You won’t find any of that with Newfangled docs.

Whatever it’s called in brain editor. I use vscode for most things. Not this though. How did you figure out what mc.mcCntlSetPoundVar did? I just need a real reference for everything exposed for use.
Did you seriously test everything and infet ?

I figured that accessing something like the current g54 z value would be straight forward. Something like get and set. I know get for current z location exists.

Oh, that is in the C/C++ docs for Mach4. None of the real details is in the regular docs. It’s in the compiled HTM file, help thingy.

Ahhhhhh. Well, I’ll have to look there. I didn’t know it existed. Not sure why I missed that

Word of warning, that is the doc that after reading it for a few days you will want to scratch your eyeballs out and shriek at the heavens, “Whhhhhhhhhhhy?!?!?!”

Mach4CoreAPI.chw

That’s the name of the thing! I was away from my computer and couldn’t remember the name of it.

You can tell they had a direction, a purpose but then, nothing… It all evaporated.

Here is the link to the docs for the UI shi… :zipper_mouth_face: …uff;

https://docs.wxwidgets.org/3.0.1/