Hello Guest it is April 19, 2024, 12:03:53 PM

Author Topic: Lua Macro Parameters  (Read 6522 times)

0 Members and 1 Guest are viewing this topic.

Re: Lua Macro Parameters
« Reply #10 on: March 07, 2017, 04:45:17 PM »
Chasing my tail again. ???

I created a module 'rtModule.lua' in the modules folder
...
local rtModule = {}
  function rtModule.rtMessage(msg)
   wx.wxMessageBox(msg)
  end

return rtModule
...

Added this to the screen load script under the load modules section
...
package.loaded.rtModule = nil
rt = require "rtModule"
...



I created, since none existed, a load_modules.mcs file and put it in the same macro folder as my profile macros 'C:\Mach4Hobby\Profiles\My4Lathe\Macros'
...
package.loaded.rtModule = nil
rt = require "rtModule"
...



Created a macro 'm9002.mcs' under the profile macros folder
...
function m9002()
  rt.rtMessage('Another Hello')
end

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


Added 2 buttons to the screen

btn1 leftup script
...
rt.rtMessage('Hello from a Button')
...


btn2 leftup script
...
inst=mc.mcGetInstance()
mc.mcCntlMdiExecute(inst,'m9002')
...


btn1 gives me the expected 'Hello from a Button'
btn2 does nothing
put m9002 in the mdi and press cycle start, nothing.

So I know that the rtModule.lua is getting loaded and functiong correctly.
Do I have a syntax error in the macro?  Compiles.
Is there more to the load_modules.mcs that I am missing?

HELP!

Offline DazTheGas

*
  •  778 778
  • DazTheGas
    • View Profile
Re: Lua Macro Parameters
« Reply #11 on: March 07, 2017, 05:16:35 PM »
Mach4 Lua/wxLua has 2 instances of it, one is for the gui and one is for gcode engine. you will need to load the modules into the gcode instance of lua.
When Mach4 starts up it compiles everything in the macro`s folder into 1 file, you could place a functions.lua within the macro directory that loads the modules and this will get compiled along with the mcodes.

DazTheGas
New For 2022 - Instagram: dazthegas
Re: Lua Macro Parameters
« Reply #12 on: March 07, 2017, 05:40:23 PM »
Daz thanks for the reply,

Mach4 has 2 instances of what?

I followed Chaticione's example to a T, at least I think I did.  I'm missing some basic concept/information on how mach/lua deals differently with screen scripts versus macro scripts.  

when to use mcCntlMDIexecute versus mcCntlScriptExecute

wx, sc and mc are just libs that have been loaded at startup and now my rt is as well.  I get that the load screen scripts instantiates my rt variable and read that lua only loads a 'lib' once even if called more than once.  Your reference to creating a functions.lua in the macro folder seems to be what the load_modules.mcs is doing now and which just seems to be loading the same lib again.

Sorry for the terseness but I'm getting dizzy and frustrated after spending hours reading and researching something that no one seems to be able to explain to people that are new to lua, and yes I have read/studied a lot on lua in the last month.  Not real fond of the compiler or the debugger.

Offline smurph

*
  • *
  •  1,546 1,546
  • "That there... that's an RV."
    • View Profile
Re: Lua Macro Parameters
« Reply #13 on: March 07, 2017, 05:59:41 PM »
The reason for building a module is to place common code in it.  But let's investigate why we need common code in the first place.  It is safe to assume, if one is writing common code, that the common code will be used by multiple things.  As DazTheGas stated, there are two instances of LUA running.  One instance in the GUI and one instance that the M codes (macros) use.  So these are the multiple things that will be using your common code.  However, both of them need to know about the common code!  You do this with the "require" LUA keyword.

1. For the GUI, you "require" the module in the screen load script.  
2. For the macros, you place a file in the macros directory that you "require" the module in.  DasTheGas' suggestion is a file that you create named "functions.lua".  But it could be any file name as long as it has a .lua or .mcs extention.  

example functions.lua:
Code: [Select]
package.loaded.rtModule = nil
rt = require "rtModule"

When Mach is restarted, it will include functions.lua when it builds the mcLua.mcc file from ALL of the files in the macros directory.  

Once that is done, you will be able to call rt.rtMessage() from both a screen script AND a M code macro.  

Steve
Re: Lua Macro Parameters
« Reply #14 on: March 07, 2017, 06:12:50 PM »
Steve,

Thanks for the explanation that there are two instances of Lua running and controlling Mach4.  I couldn't tell whether Daz was referring to two instances of rt or Lua or whatever.

As I put in my previous post:


I created, since none existed, a load_modules.mcs file and put it in the same macro folder as my profile macros 'C:\Mach4Hobby\Profiles\My4Lathe\Macros'
...
package.loaded.rtModule = nil
rt = require "rtModule"
...


Is this not accomplishing the same thing as you and Daz suggested?  The only difference is .mcs versus .lua  Are they both not compiled into the one file?

Offline smurph

*
  • *
  •  1,546 1,546
  • "That there... that's an RV."
    • View Profile
Re: Lua Macro Parameters
« Reply #15 on: March 07, 2017, 06:53:51 PM »
Yeah, that should be fine.  However, I forgot to mention that you might have to set the package path so that the LUA instance can find it.

For the stock profiles, we have a laod_modules.mcs that can be modified (or added to, etc...)

Code: [Select]
---------------------------------------------------------------
-- Load modules that you want to be able to use from Mcodes
---------------------------------------------------------------
inst = mc.mcGetInstance()
local profile = mc.mcProfileGetName(inst)
local path = mc.mcCntlGetMachDir(inst)

package.path = path .. "\\Profiles\\" .. profile .. "\\Modules\\?.lua;" .. path .. "\\Modules\\?.lua;"
--package.path = path .. "\\Modules\\?.lua;" .. path .. "\\Profiles\\" ..  profile .. "\\Modules\\?.lua;"

--rt module
package.loaded.rtModule = nil
rt = require "rtModule"

That is a modified "load_modules.mcs" file that is distributed in the stock Mach4Mill profile. 

Steve
Re: Lua Macro Parameters
« Reply #16 on: March 07, 2017, 07:43:31 PM »
Checked all profiles' macros folders and no load_modules.mcs in any of them.  There is a moduleload.mcs in the Mach4_Industrial_Mill macros folder.

When I added your package path code to the Load_modules.mcs that I created and then load Mach4 (lathe by the way) I see a quick flash of a windows command prompt window opening and closing.  Probably showing an error message.

My head hurts so I need to call it a night.  Thanks for trying.

RT

Offline Chaoticone

*
  • *
  •  5,624 5,624
  • Precision Chaos
    • View Profile
Re: Lua Macro Parameters
« Reply #17 on: March 08, 2017, 09:25:54 AM »
Quote
Checked all profiles' macros folders and no load_modules.mcs in any of them.

The load_modules.mcs is a relatively new addition. May be why it isn't in your profiles macros folder. It will be in coming releases default profiles if it isn't already. But for now, here it is as an attachment. Edit it as needed amd place it in your "Mach4Hobby\Profile\ProfileName\Macros" folder.





;D If you could see the things I have in my head, you would be laughing too. ;D

My guard dog is not what you need to worry about!
Re: Lua Macro Parameters
« Reply #18 on: March 08, 2017, 05:33:29 PM »
Chaoticone, sorry for mangling your name in my previous post.  Thanks for the file, deleted the lines:

package.loaded.mcErrorCheck = nil
ec = require "mcErrorCheck"

since I don't have that module either.

Still no joy.

Code: [Select]
function m9002()
 local inst = mc.mcGetInstance()
 mc.mcCntlGcodeExecuteWait(inst,"g1F4 x1")
 if rt==nil then
    wx.wxMessageBox("rt==nil")
  else
    rt.rtMessage('Another Hello')
 end
end

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

When I call this macro from a button or through the mdi the dros move correctly and then I get the message rt==nil
If I call rt.rtMessage("Button Message") from a button I get 'Button Message'   so the GUI lua is loading the rtModule and seting it's rt but the load_modules.mcs for macro rt must be failing to load the same module as the screen.  How can you debug this?  

Has anyone tested this with Mach4Lathe?  

The UC100 plugin failed because they hadn't tested it with lathe so lathe must have some different code.

TIA

RT

Offline DazTheGas

*
  •  778 778
  • DazTheGas
    • View Profile
Re: Lua Macro Parameters
« Reply #19 on: March 09, 2017, 02:03:41 AM »
The package.path needs to match the location and extension of your module, so if its working with a button check that line is the same as the one in the screen load script

screen load script normally points to mach4 dir/modules
the one in the file is pointing to mach4 dir/profiles/your profile/modules

If the package.path is incorrect then it will not find and load the module.

DazTheGas
New For 2022 - Instagram: dazthegas