Hello Guest it is June 07, 2024, 06:30:20 AM

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - iko79

Pages: 1
1
Mach4 General Discussion / Making use of P values in macros
« on: July 27, 2023, 06:46:24 AM »
Hi,

Mach4/Lua/Gcode beginner here. I'm trying to write a macro for Mach4Hobby, that I've had a look at the exemplary m162.mcs in Profiles/Mach4Mill/Macros/ folder and I came across this in line 10
Code: [Select]
function m162(hParam) --The param is a P value called from Gcode. M162P3 for example.
which leads me to believe that I can specify parameters that are passed to my macros with a "P", following my M-code. Now I've written the following for M100 into m100.mcs:
Code: [Select]
local inst = mc.mcGetInstance()

function m100(hParam)
if (hParam ~= nil) then
mc.mcCntlLog(inst, string.format("hParam %d", hParam), "",0)
end
end

if (mc.mcInEditor() == 1) then
    m100(1)
end
From what I inferred from m162.mcs, if I add a line "M100P10" to my MDI, the called macro should get a numeric 10 as an argument and this should show up in the log. However, when I run it, the output is always "hParam 466550640", no matter what value I add as a "P value", even if I don't pass a parameter at all (i.e., just putting "M100").

There is apparently an argument passed to the macro, but it seems like is contains the wrong value, or some handle/reference or whatever. Why is that? Is this not how it's supposed to be used? If not, what is a P value and how can I get a parameter into my macro from G-code/MDI? Unfortunately, I cannot find any documentation on that, but maybe I just missed it?

(Using Mach4Hobby 4.2.0.5036)

2
Hi,

Mach4 and Lua beginner here. I've seen several related questions here but I cannot really translate the answers to my specific issue, so I start a new thread, sorry if this is a duplicate.

I've been trying something very simple for some time now and cannot get it to work, I suspect the reason is I don't understand how Lua scripts are run and how the entire Mach4 environment is set together. I made a module that I am successfully using from ScreenScript, it is made for sending OSC messages to another application via UDP. The module is placed in Modules/OSCDRO.lua and uses some Lua libraries that I added to the Mach4Hobby root folder.

Short Version:
I am trying to call a function that I defined in the ScreenScript from out of ta Module script. Is that even possible? Since I failed, I tried to use module directly in my macro and not take the detour of calling a ScreenScript-function, as I understand that the module loaded in the ScreenScript should also be available in the module (?). But I also failed with that. So, what would already solve my problem would be a way to call a function defined in the ScreenScript out of a macro.

Detailed Version:
I load and initialize it in the screen load script simply with
Code: [Select]
package.loaded.OSCDRO = nil
osc = require "OSCDRO"
oscIntervalMS = 10 --ms

--Open the osc socket
if(osc.Setup()=="") then
    scr.StartTimer(0,oscIntervalMS,0)
end

In the screen timer script I send my packages as follows:
Code: [Select]
if timer == 0 then
if(machEnabled~=0) then
osc.SendXYZ()
end
end

This works fine so far. Now I would like to make use of M-Codes and call, e.g., osc.SendM(100) from my M100 macro. I tried calling a global function from the macro and could not get it to work, apparently it was not found (?), then I tried to access the osc object, but . My Profiles/Mach4Mill/Macros/m100.mcs looks as follows:
Code: [Select]
function m100()

local inst = mc.mcGetInstance()
mc.mcCntlLog(inst, "M100" ,"" ,0 )

        osc=require "OSCDRO"
mc.mcCntlLog(inst, "require okay" ,"" ,0 )

if(osc == nil) then
mc.mcCntlSetLastError(inst, "osc not loaded")
end

if(osc.Setup()=="") then
osc.SendM(100)
else
mc.mcCntlSetLastError(inst, "osc setup failed")
end
end

if (mc.mcInEditor() == 1) then
m100()
end
What's particularly strange here is, that the script execution seems to stop at the line containing the "require", since I don't get any log messages that come after this line.

Note that I would actually prefer to do something like this, because I don't want another instance of osc in every macro, since I would prefer to use one single UDP sender instead of dozens:
Code: [Select]
function m100()
sendM(100) --calling function in ScreenScript, that calls osc.SendM(100)
end

if (mc.mcInEditor() == 1) then
m100()
end

What's also strange is that when I run the debugger for my m100.mcs in ZeroBrane everything works well, osc is loaded via require and SendM is called perfectly, but when I run using the "Cycle Start MDI" button, it doesn't: the macro script seems to run, but the require fails.

I don't understand this whole behavior at all, I tried so many things by now by trial-and-error, but I keep failing. I would therefore highly appreciate some advice. Apparently it has to do with something I'm not aware of, e.g., the script loading order, the interaction between ScreenScript and macros, maybe different Lua environments they are run in, execution order, some Lua syntax or scope pitfalls that I'm oblivious about... idk.

(Using Mach4Hobby 4.2.0.5036)

Pages: 1