Hello Guest it is March 29, 2024, 10:05:59 AM

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

0 Members and 1 Guest are viewing this topic.

Re: lua in Mach 4
« Reply #10 on: August 11, 2022, 02:01:47 PM »
Just a matter of opinion, but I personally don't make a "GCode" string variable, I just have lines of mc.mcCntlGcodeExecuteWait with the code inside of it.  You are doing just that in some cases.  Just keep it going. 
Also, I don't think it is necessary to have %.4f every time you call a position.  Also, I'm assuming you are declaring your XPos1, YPos1 etc as local variables at the top of your macro?
Just declare each position as a variable and call it later in your mc.mcCntlGcodeExecuteWait. 

I'm also attaching the m6 I've been working on for my current mill in progress.

Here is how I would do what you've done.

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)
local DrawBarOut = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT7)
local ZPos = 1.2345
local XPos1 = 1.2345
local YPos1 = 1.2345
local XPos2 = 2.3456
local YPos2 = 2.3456

--Tool 1 Position
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z0.0")
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 X" .. (XPos1 + 1.3) .. "Y" .. YPos1)
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z" .. (ZPos1 + 1.0))
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z" .. Zpos1)
--Raise tool change holder #1
mc.mcSignalSetState(TSig1, 1)
--Move x axis to place too in change holder
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 X" .. XPos1)
--Release the drawbar
mc.mcSignalSetState(DrawBarOut, 1)
--drop tool change holder #1
mc.mcSignalSetState(TSig1, 0)

--Tool 2 Position
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z0.0")
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 X" .. (XPos2 + 1.3) "Y" .. YPos2)
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z" .. (ZPos2 + 1.0))
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z" .. ZPos)

--Raise tool change holder #1
mc.mcSignalSetState(TSig2, 1)
--Release the drawbar
mc.mcSignalSetState(DrawBarOut, 0)
wx.wxMilliSleep(1000)--1 Second
--Move X Axis to grab tool.
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 X" .. (XPos2 + 1.3))

--Drop tool change holder #2
mc.mcSignalSetState(TSig2, 0)

--Move to Z Clearance
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z0.0")
« Last Edit: August 11, 2022, 02:06:10 PM by Cbyrdtopper »
Chad Byrd
Re: lua in Mach 4
« Reply #11 on: August 11, 2022, 02:23:51 PM »
well that's nice and all but what about when the current tool is 2 and it calls for tool 4. I know what tool it has and wants but I need to move the correct change station. I need to get the number from the tool into the tool change number. in prior languages I have used I could just make a text string Tsig# whatever I wanted and use it as the variable apparently there is no way to use a text value as a variable in this language.
Thanks
« Last Edit: August 11, 2022, 02:26:41 PM by alanbredbury »
Re: lua in Mach 4
« Reply #12 on: August 11, 2022, 03:45:07 PM »
Got it, still needs a bit of cleanup, just had to get the way a function works in this thing
thanks

function TchgStation(SelectedTool, CurrentTool)

   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
      TsigSout = Tsig1
   elseif (SelectedTool == 2) then
      TsigSout = Tsig2
   elseif (SelectedTool == 3) then
      TsigSout = Tsig3
   elseif (SelectedTool == 4) then
      TsigSout = Tsig4
   end
   if (CurrentTool == 1) then
      TsigCout = Tsig1
   elseif (CurrentTool == 2) then
      TsigCout = Tsig2
   elseif (CurrentTool == 3) then
      TsigCout = Tsig3
   elseif (CurrentTool == 4) then
      TsigCout = Tsig4
   end   

   return TsigSout, TsigCout;
end
   
TchgStation(SelectedTool, CurrentTool)

------ 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(TsigCout, 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(TsigCout, 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(TsigSout, 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(TsigSout, 0)
------ Move Z to home position ------
mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z0.0\n")
Re: lua in Mach 4
« Reply #13 on: August 12, 2022, 05:03:01 AM »
You're not really gaining anything by using a function there as you are only calling it once.

If you want to tidy things up you can replace that function with all the if statements with the following:

Code: [Select]
local inst = mc.mcGetInstance()
local SelectedTool = mc.mcToolGetSelected(inst)
local CurrentTool = mc.mcToolGetCurrent(inst)

local SigTable = {[1] = mc.OSIG_OUTPUT2, [2] = mc.OSIG_OUTPUT3, [3] = mc.OSIG_OUTPUT4, [4] = mc.OSIG_OUTPUT5}

local TsigSout = mc.mcSignalGetHandle(inst, SigTable[SelectedTool])
local TsigCout = mc.mcSignalGetHandle(inst, SigTable[CurrentTool])

All this does is look up the signal in the table based on the selected tool or current tool
Re: lua in Mach 4
« Reply #14 on: August 12, 2022, 10:26:33 AM »
That is so far from any of my prior programming experience that it is nearly incomprehensible. I do see what it is doing though I think. Thanks I will try to incorporate it into the code.
Here is another question. the code I have does one really irritating thing if I hit the stop button it drops the tool and raises and drops the tool change station. I assume its completing the code after the stop is pressed. I am willing to bet someone here knows how to fix that. It also changes the tool number even though it never happened kinda. Thoughts? everyone here has been a great help.
Re: lua in Mach 4
« Reply #15 on: August 13, 2022, 04:58:56 AM »
To stop it continuing if you press stop you need to check the return code of mc.mcCntlGcodeExecuteWait each time you call it. If you look up the mc.CntlGCodeExecuteWait in the Mach4API.chm file in the docs folder you will see it returns four possible errors one of those being "no error". So you can do something like the following to exit the M6 macro if something happens during execution. The CntlMacroStop call forces the control into idle

Code: [Select]
local rc
rc = mc.mcCntlGcodeExecuteWait(inst, "some gcode string")
if (rc ~= mc.MERROR_NOERROR) then
     mc.mcCntlMacroStop(inst, 6, "Error moving to tool change position")  --The 6 can be replaced with any number, it is just an error number you want to assign. Also custom message can be used
     return --exit the m6 macro
end

Ideally you should check the return codes of when you turn on outputs as well in the same way. Otherwise the activation of an output may fail, such as the draw bar opening, but the machine will continue to move to the next tool position.
« Last Edit: August 13, 2022, 05:06:57 AM by SwiftyJ »
Re: lua in Mach 4
« Reply #16 on: August 18, 2022, 02:45:14 PM »
got back to this, the code that you provided below just dumps me out of the macro when I paste it in to my M6 macro. Please keep in mind I do not want to become an expert lua programmer I just want a single usable piece of code to run my tool changer. first question do I need to put this code in my program every time I call a gcode string and every time I change an output? this seems really bulky and inefficient. I tried putting the  mc.mcSignalSetState(hsig, 1) in this function where it says "some gcode string" yup that just dumps me out of the program and un-clamps the drawbar. I am clearly not getting this language. thoughts? examples? Thanks

local rc
rc = mc.mcCntlGcodeExecuteWait(inst, "some gcode string")
if (rc ~= mc.MERROR_NOERROR) then
     mc.mcCntlMacroStop(inst, 6, "Error moving to tool change position")  --The 6 can be replaced with any number, it is just an error number you want to assign. Also custom message can be used
     return --exit the m6 macro
end
Re: lua in Mach 4
« Reply #17 on: August 18, 2022, 03:11:55 PM »
Did a bit more searching and I think these guys are going to do what I need once I get the usage through my thick skull. Me smart like tractor.

These next two functions are very helpful inside lua macros (scripts) since they will stop the machine if an error condition occurs (provided you check for and handle that error condition). Either of these functions will stop the execution of the GCode after the macro ends, but will not halt the execution of the macro itself. You will need to put "return" statements in after the calling of these functions, in order to halt the macro itself.
mcCntlMacroAlarm(...)
number: rc = mc.mcCntlMacroAlarm(number: inst, number: error_number, string: error_message)

mcCntlMacroStop(...)
    number: rc = mc.mcCntlMacroStop(number: inst, number: error_number, string: error_message)
Re: lua in Mach 4
« Reply #18 on: August 18, 2022, 04:55:51 PM »
Hi,
every API is a function, and every function has at least one result, the return code, and maybe other results of the function.

There is a list of return codes here:

https://www.machsupport.com/forum/index.php?topic=40051.0

The software test you are applying asks 'Is there an error in the function', if so stop the macro otherwise continue. That is a pretty crude test, there are many reasons that
a function can fail, one of the more common is:

-18     MERROR_NOT_NOW 

This means that for whatever reason Mach cannot execute the macro, and that in turn usually results because the screenscript is not executing at that particular moment. If you were to wait,
maybe a few milliseconds and try again then the function would proceed.

There are a few circumstances this error is common. They are characterised by control of the machine as it passes from the main Gcode interpreter to the screenscript,
and an m6 is a prime example. So in this circumstance it may be necessary to use another software test, and even devise a strategy for repeating the API call if it fails.
You may be concerned that ' this seems really bulky and inefficient.', and this further testing makes it worse....but you potentially can save the macro from faulting.

A great deal of effort and expertise in software development is less about 'normal and correct' operation but what happens in a fault condition? Having software to trap an error and ideally
correct it automatically, is the holy grail. You'd certainly hope nuclear launch facilities have robust error trapping routines for example, LOL.

Quote
Please keep in mind I do not want to become an expert lua programmer I just want a single usable piece of code to run my tool changer.

You may not want to become an expert and I imagine you'll get your wish, but you will have to be proficient, and that's nearly as tough.
The alternative is to pay NFS to write it for you,and they will...but at a cost. The expectation that you can write code to enact a tool changer and that it be
as easy as falling off a log is unrealistic.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: lua in Mach 4
« Reply #19 on: August 19, 2022, 02:35:32 AM »
i must agree with Craig spechieli in such cycle like change tool
that involve movement ,output comand,input check...
and there is big rule you must put on yours mnd " the unexpected behavior always will happened"
you must think in every movement what can be fault , not only by machine but also with worker ,
remember very important  point ( for me it was big headache)if you press stop while m function run ,Mach will stop
only movement ,all other continue run
in other word if you press stop while change tool ,the machine will stop move ,but the spindle will open and tool will fall and beake
so this one sample  why you must think about every option in every point of time in yours cycle