Hello Guest it is November 20, 2019, 06:05:57 PM

Author Topic: Understanding Mach 4 LUA scripting capabilities  (Read 472 times)

0 Members and 1 Guest are viewing this topic.

Understanding Mach 4 LUA scripting capabilities
« on: January 16, 2019, 11:22:07 AM »
Hello all,

I am preparing to switch to Mach 4 upon completion of a machine rebuild and I am trying learn the extents of the capabilities of the LUA scripting included with Mach 4. I have a few questions which I'll list below:

1) Where can I find an inclusive list of the LUA functions included within Mach 4? I know LUA is a general purpose language, but I'm looking for something akin to the Mach 3 macro programmers reference which the Mach specific instructions and constructs are enumerated.

2) I'd like to formulate ASCII serial strings to communicate with 3rd party servo drives. This would be for diagnostic information, not motion control. Is LUA capable of communicating through a hardware RS232 serial port? What about USB?

3) What is the update rate of the LUA main script? I know there are screen load and unload scripts, but I understand there to be one that runs fairly continuously. Will this hamper machine performance, or is it only executed with the remaining computational power after motion control?

4) Are there any good examples of LUA scripts which others have written that can be referenced to help create my own?

Thanks in advance for any information you can provide. These are the questions which I couldn't find answers to so far, but if I missed them, a link to the page would be very much appreciated.

Re: Understanding Mach 4 LUA scripting capabilities
« Reply #1 on: January 16, 2019, 11:48:48 AM »
Hey Mcardoso,
I'll answer your questions best I can.
1)  If you download Mach4 there will be a folder in the main Mach4 Directory called Docs.  In here there are manuals, one of which is a LUA scripting manual and there is another API File that has all the Mach Specific Lua Calls.
2)  I don't know anything about ASCII except that it is for communications.  Mach4 has a modbus protocol, RS232 is one of the protocols.  You can utilize the information received over modbus inside LUA functions and scripts. 
3)  The PLC Script is the script that continuously runs in the background, you can change the scan time inside Mach4.  You have other functions that will run from the screen load script only when they are called from another source.  Example, a button press can call a function.  Some functions can lock up, or take over, Mach's GUI so you can make coroutines that will run the functions and keep everything else rolling as well.  There is a video about coroutines on YouTube from another Mach4 user, DaztheGas, go check it out; highly informational.
3.  There are TONS of examples that have been shared and Mach4 comes with several examples from Automatic Tool Change routines, to turning on and off outputs, to advanced file functions. 
This forum is a great place to learn and share your experiences with Mach4.
Chad Byrd
Re: Understanding Mach 4 LUA scripting capabilities
« Reply #2 on: January 16, 2019, 12:45:01 PM »
Chad,

Thanks for the response. I've been using Mach 3 for years and I am very comfortable with the VB scripting. I've done many custom applications over the years including a pre-load tool changer on MODBUS. The LUA script looks very powerful, but it is a little intimidating to a non-programmer who hasn't used it before and needs to recreate my old Mach 3 scripts  :D

The serial port question was mostly focused around writing raw text out of the port, it wouldn't need to be a MODBUS protocol. I will download and read through the documentation.

5) It looks like the generalized LUA (not necessarily for Mach 4) has tons of libraries and can interface with compiled C++ code. Does the Mach 4 version of LUA allow you to add libraries? Can you write compiled C++ code and call it from the LUA script?
Re: Understanding Mach 4 LUA scripting capabilities
« Reply #3 on: January 16, 2019, 01:11:56 PM »
I started my programming with Mach3 and VB.   LUA wasn't too bad to get a grasp of, especially with all thr examples on the forum.   
I'm not sure if you add libraries or not, I think so, but that's a question for a few other people on this forum.
Chad Byrd
Re: Understanding Mach 4 LUA scripting capabilities
« Reply #4 on: January 16, 2019, 01:51:00 PM »
Hi,

I have this link open whenever I'm coding:

https://www.lua.org/manual/5.2/

Note that there is a later release (5.3) but Mach has the 5.2 version, there are a few differences.

The Standard Libraries, section 6, contains a list of all the included functions of Lua.

In addition there are a whole raft of libraries written in Lua, mathematical, graphic, communication you name it!

http://lua-users.org/wiki/LibrariesAndBindings

A while back I tried to import a graphical library into Mach. I did quite a bit of work on it but ultimately failed to get the
module incorporated into Mach. I'm sure it can be done but requires more skill than I have.  In this particular
case I was just trying to see whether I could do it in response to a query on the forum, not that I actually required
it.

In the early days I was convinced I would have to download and incorporate a POSIX compliant string editing
module. I subsequently found that the string functions table built in to 5.2 while not POSIX compliant can do
just about everything required.

I have found that anything that I require to do on my machine has NOT demanded that I add other Lua libraries to
Mach. May I suggest exploring the extents of Lua as supplied before concerning yourself with extra libraries and/or
C++ functions.

When scripting Lua represents only a portion (small) of the effort/understanding required the remainder of the
effort/understanding is the use of the API and making intelligent use of Machs structure.

There are a couple of areas where I believe an understanding of Lua will greatly assist your efforts.
The first is to understand and be familiar with is 'functions as a first class value', that subtle but powerful idea occurs
repeatedly throughout Mach. The second area is the use of Lua's one and only data structure...the table.
When you recognize the power of the table you will also see that many of the standard libraries (math and string
for instance) are in fact just tables....tables of functions!

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

Offline smurph

*
  • *
  •  1,151 1,151
  • "That there... that's an RV."
    • View Profile
Re: Understanding Mach 4 LUA scripting capabilities
« Reply #5 on: January 17, 2019, 03:46:19 AM »
In addition to what Chad and Craig have written:

For #2.  In the Modules directory, you will find several useful LUA modules.  One of which is luars232, a serial library for LUA
For #3.  The PLC script will not hamper machine performance.  LUA is fast fast fast!  It is why we chose it.  Most PLC scripts run in well less than a millisecond.  Now, that doesn't mean that bad code could not cause a problem.  So the fix for that is "just don't write bad code."  :)
For #5.  Yes.  But check the Modules directory of the install (#2 above) because we provide several of these "libraries" (but more properly called modules).  LUA modules ARE the way to call C code.  Writing one is beyond the scope of any of our manuals though.  But there are tons of resources out there about that. 

BTW guys, the development versions of Mach are now using LUA 5.3.  :)  Be sure to read up on the difference between 5.2 and 5.3. 

Steve

Steve
Re: Understanding Mach 4 LUA scripting capabilities
« Reply #6 on: February 04, 2019, 12:08:36 PM »
Thanks for everyone's responses her and sorry it has taken me so long to get back. Work picked up and I haven't had a ton of time to commit to projects lately.

But I think it is time to take the leap into Mach 4 (I will be purchasing my license in the next few days).

cbyrdtopper, joeaverage, and smurph. It sounds (based on what you guys have said) that there is nothing I posted about that I shouldn't be able to accomplish. I've spent a lot of time reading the manuals and I feel that I have at least a rough understanding of how LUA will work. I have a few very simple scripts that I want to write first, and will work my way into the more complicated stuff. Its great to know that LUA is fast, and it seems to be a lot more flexible that VBS/cypress enable was in MACH 3.

For now I'll stick to the basics and avoid trying to incorporate what I'm comfortable in (C++) into what seems to work well (LUA). Thanks again for all the advice!
Re: Understanding Mach 4 LUA scripting capabilities
« Reply #7 on: February 05, 2019, 01:07:35 AM »
Hi,

Quote
Its great to know that LUA is fast, and it seems to be a lot more flexible that VBS/cypress enable was in MACH 3.

The reason I shifted to Mach4 in the first place was to avoid VB/Cypress. Lua, the APIs and Machs modular structure have a distinct
learning curve but the result far, FAR, FAR outshines VB/Cypress.

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