Hello Guest it is April 18, 2024, 06:13:51 PM

Author Topic: Help defining outputs in LUA script for tool change  (Read 1313 times)

0 Members and 1 Guest are viewing this topic.

Help defining outputs in LUA script for tool change
« on: January 21, 2019, 02:03:33 PM »
I am writing my tool change script for my CNC. I am a little confused about how to handle the outputs. I am using a smoothStepper board. For a simple first test I want to just turn on the blow off, lower the z axis 1" and turn off the blow off. But I don't know how to assign the output.

I have been reading through the Mach4 Scripting Manual and I am using the sample code in there for reference. (In the code it uses a variable(?) "inst" what is "inst"?)

I am using port 1, pin 7 on the smooth stepper for the blow off. I have named the pin "Blow Off" in the pin assignments.

So what is the "mc." designation? Is there a list of these somewhere that I can find that maps the I/O and other signals I can read?

Here is my code:

function m6()
    ------ Turn on Blow off ------
    local BlowOff = mc.?????????????????
    local bosig = mc.mcSignalGetHandle(inst, BlowOff )
    mc.mcSignalSetState(bosig, 1)

    ------ Move to current tool change position ------
    local GCode = ""
    GCode = GCode .. "G00 G91 G53 Z-1.0\n"
    mc.mcCntlGcodeExecuteWait(inst, GCode)

    ------ Turn off Blow off ------
    mc.mcSignalSetState(bosig, 0)
end
if (mc.mcInEditor() == 1) then
    m6()
end
Re: Help defining outputs in LUA script for tool change
« Reply #1 on: January 21, 2019, 04:26:12 PM »
TTalma,
The code you supplied was almost there; make the following changes and it should work fine.

You have to assign your "Blow Off" output to an Output# in the Mach4 Configuration.
Once you assign that to an output you can then use the following code in a macro to get the handle of the output to change it's state.

function m6()
 ------ Turn on Blow off ------
    local BlowOff = mc.mcSignalGetHandle (inst, mc.mcOSIG_OUTPUT#) --Use the Output # you assigned the Blow Off in Mach4 configuration.
    mc.mcSignalSetState(BlowOff , 1)

    ------ Move to current tool change position ------
    local GCode = ""
    GCode = GCode .. "G00 G91 G53 Z-1.0\n"
    mc.mcCntlGcodeExecuteWait(inst, GCode)

    ------ Turn off Blow off ------
    mc.mcSignalSetState(BlowOff , 0)
end
if (mc.mcInEditor() == 1) then
    m6()
end


To answer your question about "inst".  Here is a post that Craig made this past week that sums it up nicely.

"It is possible and may become fact one day, that multiple instances of Mach can run at once. This was allowed for
in the original 'design' of Mach. As a consequence there are many instructions in Mach which need to be applied
to a particular instance of Mach. As it turns out Mach as currently deployed allows only one instance, usually instance
'0' and thus if you used the variable 'inst' in any part of Mach it will probably work. The safe way is to ensure that
the proper and current instance is used....ergo mc.mcGetInstance() is used within each scope."
Chad Byrd
Re: Help defining outputs in LUA script for tool change
« Reply #2 on: January 22, 2019, 01:59:06 AM »
Hi,
I always have this online manual open when I am coding Lua:

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

Unlike a lot of computer languages Lua has only one data structure, the table.

Section 6.5 covers some of the table manipulation functions but note the syntax:
table.function(). Does that look familiar? Have a look at the maths functions that come with Lua. Again look at the syntax:
maths.cos(x) for instance, or another example is the string functions like: string.find(x.....) for instance.

In short all the maths functions are member functions of the table 'math' and the string functions are all member functions
of the table 'string'

Quote
So what is the "mc." designation? Is there a list of these somewhere that I can find that maps the I/O and other signals I can read?
the answer is that mc. stands for the table mach core. So ALL the API functions ( API.chm in the Mach4 Docs folder)
are all member functions of the table 'mc'

You will encounter others like wx. The 'wx' table is a table of wxWidgets functions which does a lot of the graphical heavy
lifting in Machs screen. Another you may see from time to time is scr. The 'scr' table has functions for populating Machs
screen with data and manipulating it.

So despite Lua having only one data structure it is in fact extremely versatile. In  fact a simple single variable, like
MyNumber in Lua, is in fact a table, its just a table of one object, ie your number.

So Mach is a whole bunch of tables, in the case of the mc. table it has many hundreds of objects like functions,
mcSignalGetHandle() is an example of one, but also data objects like mc.OSIG_OUTPUT1 and hundreds more right down
to tables with a single data object like MyNumber as I have given above.

This is on the one hand an extremely simple arrangement and at the same time extremely crafty.

Lua has surprised and delighted me after what might have been called a 'difficult beginning', I hope your experience has
a positive outcome also.

Craig

'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Help defining outputs in LUA script for tool change
« Reply #3 on: January 22, 2019, 08:21:40 AM »
Thanks for the help! Things came up last night and I didn't get out to the shop to try this.

Byrd, The assignments in mach is quite obvious now that you pointed it out, and I had a "duh" moment when I read your comment. Thanks for cleaning up my code!

Joe, your explanation is very helpful. I have never used LUA before, but I am a software engineer for my day job. I use C, and C# primarily. But I have have found that almost all programming languages are conceptually the same. It's learning the syntax that takes time. I did read that I can write a library in C. If I ever need to do something real complicated I may pursue that route. I will probably just stick to the LUA for the tool change, since the sample is what I need to do, with the addition of the blow off.
Re: Help defining outputs in LUA script for tool change
« Reply #4 on: January 22, 2019, 12:21:23 PM »
Hi,
smurph has described 'Lua syntax in something only a mother could love'.

What has surprised me about Lua is that while it has such a small footprint and such an apparently slim 'feature
matrix' is that those features it does have allow such a wide range of programming solutions.

One which impresses me and is used extensively throughout Mach is 'function as a first class value'. So a function is
represented as just a number.....I think it is in fact a pointer to the function code.

Imagine that if you call a function, ie provide the pointer to it, the function executes and then it returns
another pointer to yet another function. The function it points to will depend on the inputs to the machine say and will vary
with those inputs. Thus functions execute 'head to tail' in a never ending cycle WITHOUT the stack growing. Thus Lua can
implement a 'state machine'. Other languages with which I am familiar  would call that situation  'nested subroutines'
and this approach would fail because each time a function was entered the stack would grow, ultimately exceeding the memory
allocated for the stack.

It a good example how Lua an apparently simple language can produce a solution that other more sophisticated languages can't.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'