Hello Guest it is April 19, 2024, 02:50:21 AM

Author Topic: lua in Mach 4  (Read 2004 times)

0 Members and 1 Guest are viewing this topic.

lua in Mach 4
« on: August 09, 2022, 12:38:37 PM »

really simple question, I think.
I am building a m6 in Lua in Mach4
I have a 4 tool changer it has air cylinders that need to be activated, I have them on output 2 - 5
I want to build a variable name to activate the correct cylinder for the tool change
tool 1 is output 2
Line one does not work, it assigns the variable Tsig1 the text value
line two does work it returns a number

I am putting the text that is shown in parenthesis in a variable but it is text
how do I tell the program that I want it to see the text as a output variable thingy?

local Tsig1 = ("mc.OSIG_OUTPUT2")
local Tsig2 = mc.OSIG_OUTPUT3

So really x="mc.OSIG_OUTPUT2"
Local Tsig1 = x
or I tried Tsig1 = (x) and every other kind of bracket or parenthesis
I also tried string.format
and every other thing I could find on the web
No dice
Thanks
Re: lua in Mach 4
« Reply #1 on: August 09, 2022, 12:45:26 PM »
Here is how you turn on and turn off outputs in Mach4 Lua.

local TSig1 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5) --This get the handle for the output.
mc.mcSignalSetState(TSig1, 1) -- This turns on Output 5
mc.mcSignalSetState(TSig1, 0) -- This turns off Output 5

Once you make the TSig1, Tsig2, etc... variable you can call the mc.mcSignalSetState (Variable Name, 0 or 1) anywhere in your m6 without having to declare the variable again.
Chad Byrd
Re: lua in Mach 4
« Reply #2 on: August 09, 2022, 04:23:56 PM »
Well I am far past that, thanks anyway. What I an trying to do is build the mc.OSIG_OUTPUT2 through 5 based on the tool number. In other words I have successfully built a variable that holds the text string but when I out the variable in place of mc.Osig_output# it reads it as text and will not return the handle but throws an error because ots text not the machine function or whatever. There has to be a way to turn text into a usable variable. Thanks in most languages its some function, special character or brackets. For instance you combine text with .. in this language.
Re: lua in Mach 4
« Reply #3 on: August 09, 2022, 04:48:29 PM »
I'm confused on what you are trying to do.
You want to turn on a specific output based on what tool it is on?

If that is the case, just put an if statement.
--Example
function m200()
local inst = mc.mcGetInstance()
local selectedTool = mc.mcToolGetSelected(inst)
local TSig1 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2)
local TSig2 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT3)
local TSig3 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT4)
local TSig4 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5)
   
if selectedTool == 1 then
     mc.mcSignalSetState(TSig1, 1)
    mc.mcSignalSetState(TSig2, 0)
    mc.mcSignalSetState(TSig3, 0)
    mc.mcSignalSetState(TSig4, 0)
elseif selectedTool == 2 then
     mc.mcSignalSetState(TSig1, 0)
    mc.mcSignalSetState(TSig2, 1)
    mc.mcSignalSetState(TSig3, 0)
    mc.mcSignalSetState(TSig4, 0)
elseif selectedTool == 3 then
     mc.mcSignalSetState(TSig1, 0)
    mc.mcSignalSetState(TSig2, 0)
    mc.mcSignalSetState(TSig3, 1)
    mc.mcSignalSetState(TSig4, 0)    
elseif selectedTool == 4 then
     mc.mcSignalSetState(TSig1, 0)
    mc.mcSignalSetState(TSig2, 0)
    mc.mcSignalSetState(TSig3, 1)
    mc.mcSignalSetState(TSig4, 0)
else
    mc.mcCntlSetLastError(inst, "OUT OF RANGE")
end--Selected Tools

end--m200

if (mc.mcInEditor() == 1) then
   m200()
end
« Last Edit: August 09, 2022, 04:51:38 PM by Cbyrdtopper »
Chad Byrd
Re: lua in Mach 4
« Reply #4 on: August 09, 2022, 06:37:41 PM »
In short i want to build a text variable that contains mc.OSIG_OUTPUT# as text and use that value in the statement seems that would be the most efficient. Im fustrated that i dont seem to be able to do long command strings as i also have SQL background. Can I pass that function the tool number  and prefered status and get a return to raise or lower the tool change station as needed? I am accustomed to visual basic and before that foxpro it's pretty different both many years ago. I mean yes I can do it all with if statements but it seems like it's going to get pretty verbose. I will play with what you provided and see what I can do with it as a function. With the old languages I could just build a text string and use it as a variable and to make a call to an address. I saw some stuff about a load command, that didn't seem to work.
Re: lua in Mach 4
« Reply #5 on: August 09, 2022, 06:42:18 PM »
I have no idea. I have long since forgotten VB script. LUA is so much better.  Function based. I like it quite a lot.
There are some pretty smart people on here though, they may chime in. 
Move made some very very powerful tool change routines with LUA
Chad Byrd
Re: lua in Mach 4
« Reply #6 on: August 09, 2022, 07:29:48 PM »
Hi,
Quote
In short i want to build a text variable that contains mc.OSIG_OUTPUT# as text and use that value in the statement seems that would be the most efficient

That does not work in Lua. In order to address and either set or read a variable you must first get its 'handle', which in effect a number corresponding to the memory location of the
variable.

For example:

local VariableHandle=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT5)
mc.mcSignalSetState(VariableHandle,1)

So the only way you can address a signal is by using its handle. That's the way Lua is and always has been.

Unlike languages that you may be used to which have a variable table and using it you can address any variable you require and remains fixed for the duration of the program.
In Lua variables come and go and are located in different addresses as the program progresses, in fact many of them are in a stack which will be garbage collected once the variable
goes out of scope.

Lua is not really a computer language, but rather a scripting language, and is meant as an extension to the main program. In the case of Mach the main program is all C/C++, and
Lua is the scripting language laid on top of it for our use.

So when a Lua function is encountered  the PC sets up a stack and all variables (local) are put (called 'pushing') onto the stack while the function runs, so the memory location of a variable is changing
as new stack entities are 'pushed' onto the stack or 'popped' off it, ergo the repeated and common use of a 'handle' rather than a textual variable name.

You may find the PDF I wrote about using the SignalScript and covers some of these ideas useful:

https://www.machsupport.com/forum/index.php?topic=40051.msg267764#msg267764

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: lua in Mach 4
« Reply #7 on: August 09, 2022, 09:54:40 PM »
Hi,
when I'm coding Lua, this link is permanently open in my browser:

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

It tells you about the basic syntax, control structures, scope etc. My guess is you'll use the Standard Libraries quite a bit too.

The next thing you want open and you will consult dozens of times per hour is Mach4CoreAPI.chm, its the API Help file, found in C:/Mach4Hobby/Docs.

It helps to know about Machs structure and organisation but if you are reasonably familiar with the API and can refer to the Lua Reference Manual,
the link above, then you'll be able to handle 90% of what comes up.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: lua in Mach 4
« Reply #8 on: August 10, 2022, 09:10:40 PM »
Thanks for all the input, I had to do other stuff today and probably won't get back to it till Friday. I am sure I will succeed with all the advise here my code just may not be as elegant as I would like. I don't expect to do much more coding after this, I just want to run the machine so it's kinda a one shot deal I think. Thank you again for all the help.
Re: lua in Mach 4
« Reply #9 on: August 11, 2022, 11:54:50 AM »
Ok I got back at it today, here is my code that works to change from tool one to tool two.
I keep getting stuck as to how to use a tool change output variable that I can't build in a text string.
sure I can put a bunch of complex if statements for every time I use Tsig# but it will be so bulky
Tsig# needs to change based on

local SelectedTool = 2 ---mc.mcToolGetSelected(inst)
local CurrentTool = 1 --- mc.mcToolGetCurrent(inst)

I hard coded the 1 and 2 for debugging as you can see the tool changer is unfortunately more complex than would be optimal.
any help would be appreciated, thanks

local Tsig1 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2)
local Tsig2 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT3)
local Tsig3 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT4)
local Tsig4 = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5)
   
----mc.mcSignalSetState(Tsig1out, 1)

------ Move to current tool change position ------
local GCode = ""
GCode = GCode .. "G00 G90 G53 Z0.0\n"
GCode = GCode .. string.format("G00 G90 G53 X%.4f Y%.4f\n", XPos1 + 1.3, YPos1)
GCode = GCode .. string.format("G00 G90 G53 Z%.4f\n", ZPos1 + 1.0)
GCode = GCode .. string.format("G01 G90 G53 Z%.4f F15.0\n", ZPos1)
mc.mcCntlGcodeExecuteWait(inst, GCode)
---Raise tool change holder #1
mc.mcSignalSetState(Tsig1, 1)
---Move x axis to place too in change holder
GCode = ""
GCode = GCode .. string.format("G00 G90 G53 X%.4f\n", XPos1)
mc.mcCntlGcodeExecuteWait(inst, GCode)
------ Release drawbar ------
local DrawBarOut = mc.OSIG_OUTPUT7
local hsig = mc.mcSignalGetHandle(inst, DrawBarOut)
mc.mcSignalSetState(hsig, 1)
---drop tool change holder #1
mc.mcSignalSetState(Tsig1, 0)
------ Move to next tool change position ------
GCode = ""
GCode = GCode .. string.format("G00 G90 G53 X%.4f Y%.4f\n", XPos2, YPos2)
mc.mcCntlGcodeExecuteWait(inst, GCode)
----Raise tool change holder #2
mc.mcSignalSetState(Tsig2, 1)
--- give it a bit of time
wx.wxSleep(1)
------ Clamp drawbar ------
mc.mcSignalSetState(hsig, 0)
---- move x axis to slide tool out of holder #2
GCode = ""
GCode = GCode .. string.format("G00 G90 G53 X%.4f\n", XPos1 + 1.3)
mc.mcCntlGcodeExecuteWait(inst, GCode)
----- lower tool change holder #2
mc.mcSignalSetState(Tsig2, 0)
------ Move Z to home position ------
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z0.0\n")
------ Reset state ------