Hello Guest it is May 11, 2021, 08:03:16 PM

Author Topic: Mach4 Function Primer  (Read 1347 times)

0 Members and 1 Guest are viewing this topic.

Offline Osker

*
  •  41 41
    • View Profile
Mach4 Function Primer
« on: May 21, 2018, 11:57:38 AM »
I am migrating from Mach3 to Mach4 and have several M functions that need to be re-written to work with Mach4.  After looking at several code examples and DazTheGas videos (which are very informative)  my progress is still painfully slow.  ls there a primer for using the common functions called in Mach4 scripting and the general logic behind them?  In the absence of a primer, it would be helpful if the following code from the Lua examples was explained.  No need to explain what a function is, declaration of variables,  or the use of if then statements.  just what the functions are doing.  I see the function mc.mcGetInstance() used in a lot of Mach4 macros. I  assume that is polling some register and code execution depends of the result, but what are the possible results?


The macros I use emulate  G77:  Variables are defined in Gcode for start and stop axis positions, feed rates, and cutting depth per pass.  A macro is then called which executes until the final depth is achieved.  Nothing major, but it does reduce code.
  
I am not looking to be taught how to fish, just to bait the hook.
 
local inst = mc.mcGetInstance()
local a,b
function GCodeExecute(GCodeString)
   local rc = mc.mcCntlGcodeExecuteWait(inst, GCodeString) --This is the standard function call for executing gcode. it waits for motor feedback before continuing in the file.
   if rc ~= mc.MERROR_NOERROR then return "GCode failed", false end
end

a,b = GCodeExecute("G0 X1 Y2.5 Z3.5")
if b == true then
   mc.mcCntlSetLastError(inst, "Motion Succeeded")
else
   mc.mcCntlSetLastError(inst, a)
end
--You can even incorporate variables into the function call.
local travelxdistance = 1.45
a,b = GCodeExecute("G1 X"..travelxdistance.."Y0")--You can concatenate to the end of the gcode string using '..'
if b == true then
   mc.mcCntlSetLastError(inst, "Motion Succeeded")
else
   mc.mcCntlSetLastError(inst, a)
end
Re: Mach4 Function Primer
« Reply #1 on: May 21, 2018, 12:43:32 PM »
Hi,
in the documents folder look for the API.chm. It is the best resource for API calls. Regrettably it is somewhat out of date, there are new APIs which have not yet been documented.

If you want to scream 'why!!!'...go for your life...many others have and nothing has changed. One of the recurring complaints is that the documentation seems always out of date and
NFS do nothing about it. What they do instead is work on new things like the Zero Brane editor instead. While that policy causes me as much frustration as anyone else I've also come
to realize and forced to accept the situation....I can have good documentation but then have no new features (Zero Brane, PMC, Probing Module etc), what I can't have is both.

The Mill GCode Programming.pdf is the most up to date Gcode reference for Mach4. Beyond that Gcode in Mack corresponds very closely to CNC Programming Handbook by Peter Smid.
All the rest you will have to glean through the forum.

Craig
My wife left with my best friend...
     and I miss him!

Offline Osker

*
  •  41 41
    • View Profile
Re: Mach4 Function Primer
« Reply #2 on: May 21, 2018, 04:36:56 PM »
Craig,

Thanks for the assistance.  I will check out the API document as well as continue digging through the forum and posted videos.  I do agree with others, the documentation is lacking, but that has been the history with ArtSoft and Machx software.  As Willie Nelson philosophized in the movie Barborosa, "What cannot be changed must be endured".

I have ordered the CNC Programming Handbook.  Hopefully, that will shed a little more light.


Dan
Re: Mach4 Function Primer
« Reply #3 on: May 21, 2018, 06:25:33 PM »
Hi,

Quote
I have ordered the CNC Programming Handbook.  Hopefully, that will shed a little more light.
Its a dry read but definitive, not cheap though...

Craig
My wife left with my best friend...
     and I miss him!
Re: Mach4 Function Primer
« Reply #4 on: May 24, 2018, 11:47:02 AM »
Some helpful things for me getting started:
Make sure you are using a registered copy of Mach4 to test your scripts!! I don't know why but weird things happen with an unregistered copy. Maybe someone out there knows why.

Link to wxlua homepage
http://wxlua.sourceforge.net/

using wxmessagebox is an easier way of getting some feedback from your code than running messages through the error box: wx.wxmessagebox("hello world") or wx.wxmessagebox(tostring(some_variable))

hope that helps!

Offline Osker

*
  •  41 41
    • View Profile
Re: Mach4 Function Primer
« Reply #5 on: May 24, 2018, 01:13:27 PM »
Thanks for the suggestions and the links.  I am running a licensed copy of Mach4.  I will try using wxmessagebox.

Dan
Re: Mach4 Function Primer
« Reply #6 on: May 24, 2018, 02:57:56 PM »
Hi,
the only place you DON'T use wxMessageBox() is in the PLC script. It locks the GUI in a recurring message loop where the messages come way faster than you can clear them.

Craig
My wife left with my best friend...
     and I miss him!