Hello Guest it is March 29, 2024, 02:33:46 AM

Author Topic: Demonstration of basic probing program in Lua using G31 (G31.1)  (Read 2260 times)

0 Members and 1 Guest are viewing this topic.

Hope this can help some of you learning to work with Lua.  Several issues are addressed for the beginners like me.
This is a single probe move in Y to identify the coordinates at a probe strike and store the coordinates in a file I created and in the registry.
The probing in G31 or G31.1 etc automatically loads the #variables with the x,y,and z coordinates of the probe strike,
I have tried to document the file for descriptions of what is happening.


Code: [Select]


----------------------
--CHECK diameter of a grinding WHEEL
--I have a grinding wheel in the chuck of a Z axis spindle motor.  It is moving toward a switch.
--  The switch is wired as a second probe in my setup
--    so I am using G31.1  It you are only using a single probe then use G31
--This program is to help learn how to probe as well as some basics of using Lua in Mach4
--I am just learning as well and I hope this can help other Mach4 users
----------------------

local inst = mc.mcGetInstance();

wx.wxMessageBox("Ready for zeroing all axes?")

--xero all axes
mc.mcCntlGcodeExecuteWait(inst,"G90 G00 Y0.0")
mc.mcCntlGcodeExecuteWait(inst,"G90 G00 X0.0")
mc.mcCntlGcodeExecuteWait(inst,"G90 G00 Z0.0")

--this path business is TRICKY   notice the double \\ and the .. 
--the path that the Lua code sees will be c:\Mach4Hobby\GcodeFiles\KFCProbeWheel.csv
--notice the formatting  the probing routine will automatically load the probe file
-- with coordinates of the probe eg.: -.4657,-.8949,9.000

local path = wx.wxGetCwd() .. "\\GcodeFiles\\KFCProbeWheel.csv"--this file is where I store the probed coordinates of the grinding wheel
mc.mcCntlProbeFileOpen(inst, path, "%.4AXIS_X,%.4AXIS_Y,%.4AXIS_Z", true);

---------Set up for 31.1 only  Using probe #2
local ProbeCode=31.1

------------- Check Probe -------------
--This is the function from the probing module Called CheckProbe that I just copied, renamed and put in my screen load script
-- it is not really mandatory to use this but it checks to see which probe and if the probe is working properly
-- to find the source look at the mcProbing.lua file in the Modules directory.  Look for the function called Probing.CheckProbe

local rc = KFCCheckProbe(1, ProbeCode); if not rc then; do return end; end

--move to set position near limit switch to check diameter
    rc=mc.mcCntlGcodeExecuteWait(inst,"G90 G01 X-.3652 Z-.6170 Y-.25 F30.0")
        mm.ReturnCode(rc)
        rc = KFCCheckProbe(1, ProbeCode); if not rc then; do return end; end

-- probe 31.1 for probe1 (limit switch which is the SECOND probe in my system)
-- Mach4 - IMPORTANT TO REALIZE THAT Mach4 USES "probe" for the first probe.  If you have a second probe it will be "probe1")   
-- G31 or G31.0 if you have a single probe.  G31.1 for the second probe called probe1, G31.2 for a third probe called probe2)

rc=mc.mcCntlGcodeExecuteWait(inst,"G31.1 Y-.525 F10.0")
        mm.ReturnCode(rc)
        rc = KFCCheckProbe(0, ProbeCode); if not rc then; do return end; end

--the probing routine places the machine coordinates in these #variables  5071 for x, 5072 for y, 5073 for z
local ProbedMeasXAbs1 = mc.mcCntlGetPoundVar(inst, 5071)
local ProbedMeasYAbs1 = mc.mcCntlGetPoundVar(inst, 5072)
local ProbedMeasZAbs1 = mc.mcCntlGetPoundVar(inst, 5073)

--position near the switch and aligned with the switch so it can probe
mc.mcCntlGcodeExecuteWait(inst,"G91G01 Y.05 F10.0")
mc.mcCntlGcodeExecuteWait(inst,"G90")

    mc.mcCntlSetLastError(inst,string.format("%1.4f:%1.4f:%1.4f",ProbedMeasXAbs1,ProbedMeasYAbs1,ProbedMeasZAbs1))

--I am putting the coordinates into the registry file
-- write to ireg0 in register
WriteRegister("Probe2XWheel",ProbedMeasXAbs1)
WriteRegister("Probe2YWheel",ProbedMeasYAbs1)
WriteRegister("Probe2ZWheel",ProbedMeasZAbs1)

--move all axes to zero
mc.mcCntlGcodeExecuteWait(inst,"G90 G00 Y0.0")
mc.mcCntlGcodeExecuteWait(inst,"G90 G00 X0.0")
mc.mcCntlGcodeExecuteWait(inst,"G90 G00 Z0.0")

 --close the probe file   
mc.mcCntlProbeFileClose(inst)


Re: Demonstration of basic probing program in Lua using G31 (G31.1)
« Reply #1 on: May 14, 2017, 03:26:31 PM »
Writing and reading to the register file is an extra just to show the process.
These are functions copied from the doc written by Mach4 on Lua scripting.
The read and write functions have been placed in the screen load file through the editor.

There is an explanation of how to read and write to the .ini file as well

I am not sure which is the best way to store information but have shown
in this code how to save to a probe file you create, how to save to the register,
how to get #variable data saved by Mach4, and the docs show the .ini file manipulation.

Code: [Select]

WriteRegister("Probe2XWheel",ProbedMeasXAbs1)

Re: Demonstration of basic probing program in Lua using G31 (G31.1)
« Reply #2 on: May 14, 2017, 03:33:12 PM »
One big question I have at this point in my development is properly using some of the error checking methods.
Here I have shown the probe checking routine copied from the probing module.

Code: [Select]
rc=mc.mcCntlGcodeExecuteWait(inst,"G31.1 Y-.525 F10.0")
        mm.ReturnCode(rc)
        rc = KFCCheckProbe(0, ProbeCode); if not rc then; do return end; end

Potentially every move, every file open, and several other instances can be checked so I hope to figure out as I go along.
Re: Demonstration of basic probing program in Lua using G31 (G31.1)
« Reply #3 on: May 14, 2017, 03:37:00 PM »
Another topic is where to save these programs.

This program is saved in the screen I am using in a button click script.   The program is edited through the editor.
Such programs can also be saved (or reside) as a function in a module, as an M command, in the screen load or PLC script.
I am still learning the reasons for which is more appropriate. 

Re: Demonstration of basic probing program in Lua using G31 (G31.1)
« Reply #4 on: May 14, 2017, 03:58:04 PM »
Here I am using only 1 wx.widgets commands, because this is a whole 'nother country...
This line shows another place to place data on the fly - the error file which is opened shows in the
history button and wit the last line visible in the nearby window in the wx4 screen set.
Code: [Select]
mc.mcCntlSetLastError(inst,string.format("%1.4f:%1.4f:%1.4f",ProbedMeasXAbs1,ProbedMeasYAbs1,ProbedMeasZAbs1))

At first I really hated wx.widgets, however there is potentially great power to be had in controlling Mach4 though file selection by the user, keyboard entry, etc.
The wx.widgets programming process is very foreign to me but some of the commands can be copied from examples that come
with Mach 4 like the one above and the message feature below.   I find the formatting to be tricky but trial and error rules...

Notice the literal string in quotes and the ..x to concatenate the value of a variable x to the message line.  This took me a long time to figure out.
That is the value of the book or having access to the online docs if you can find the proper info.

Code: [Select]
wx.wxMessageBox("this is a message  x= "..x)

Finding info has been hard for me online with wx.widgets (thus the book listed)
and easier for Lua.  

The biggest problem with wx.widgets is that the docs for it on the internet are really designed for developers not beginners.  Taking apart the examples
seems to be the only way to learn it.  The docs are more like a dictionary than a guide to how it works.  I found a book that seems to help written
by the inventor and available on Amazon.  "Cross-Platform GUI Programming with xsWidgets" by Smart and Hock. 2006 by Pearson Education Inc.

Programming in Lua Fourth Edition by Roberto Lerusalimschy can be obtained through Lua.org

There alot of other resources posted on this forum that I have come across by accident.  It would be great to use this thread to put those helpful sources and posts for
all to view in a central location.  I hope others more expert than I will drop their favorite helpful links into the thread.  I will continue to do so as well.




« Last Edit: May 14, 2017, 04:37:55 PM by thespindoctor »