Hello Guest it is April 25, 2024, 04:55:43 PM

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.


Messages - rrc1962

71
Mach4 General Discussion / Re: Using Modules
« on: May 29, 2016, 08:15:53 PM »
Maybe a step in the right direction.  When I run the code below in the editor...

Code: [Select]
inst = mc.mcGetInstance()
local profile = mc.mcProfileGetName(inst)
local path = mc.mcCntlGetMachDir(inst)
package.path = path .. "\\Profiles\\" .. profile .. "\\Modules\\?.lua;" .. path .. "\\Modules\\?.lua;"
local M = require "Module1"
local rc = theFunction()

It opens a file dialog with Module1.lua already entered.  I click open and the module opens up allowing me to step through it.  When it gets to the bottom of the module, it returns to the calling script.  The next line in the script is the call to theFunction(), which generates this error.

Code: [Select]
mcLua ERROR: Lua: Error while running chunk
[string "C:\Users\Richard\AppData\Local\Temp\le4431.mc..."]:6: attempt to call global 'theFunction' (a nil value)

72
Mach4 General Discussion / Re: Using Modules
« on: May 29, 2016, 07:47:51 PM »
So just to recap, here is the module...It is called Module1.lua

Code: [Select]
local Module1 = {}

function Module1.theFunction()
wx.wxMessageBox('Module Test')
end

return Module1

This is the button code calling the function in the module...I've tried this...

Code: [Select]
inst = mc.mcGetInstance()
M.theFunction()

And this...

Code: [Select]
inst = mc.mcGetInstance()
local profile = mc.mcProfileGetName(inst)
local path = mc.mcCntlGetMachDir(inst)
package.path = path .. "\\Profiles\\" .. profile .. "\\Modules\\?.lua;" .. path .. "\\Modules\\?.lua;"
local M = require "Module1"
M.theFunction()

Also tried adding this to the screen load script...

Code: [Select]
package.path = "./?.lua;c:/Mach4Hobby/Modules/?.mcc;"  
M = require "Module1"

As well as this....

Code: [Select]
local profile = mc.mcProfileGetName(inst)
local path = mc.mcCntlGetMachDir(inst)
package.path = path .. "\\Profiles\\" .. profile .. "\\Modules\\?.lua;" .. path .. "\\Modules\\?.lua;"
M = require "Module1"

Still nothing....I also tried putting a copy of Module1.lua in C:\Mach4Hobby, C:\Mach4Hobby\Modules and C:\Mach4Hobby\Profiles\Mach4Mill\Modules

73
Mach4 General Discussion / Re: Using Modules
« on: May 29, 2016, 06:41:21 PM »
last page on the M4 post

Not sure what that means.

74
Mach4 General Discussion / Re: Using Modules
« on: May 29, 2016, 06:08:50 PM »
When I run it in debug I get this...

Code: [Select]
[string "C:\Users\Richard\AppData\Local\Temp\le53EB.mc..."]:2: module 'Module1' not found:
no field package.preload['Module1']
no file 'C:\Mach4Hobby\lua\Module1.lua'
no file 'C:\Mach4Hobby\lua\Module1\init.lua'
no file 'C:\Mach4Hobby\Module1.lua'
no file 'C:\Mach4Hobby\Module1\init.lua'
no file '.\Module1.lua'
no file 'C:\Mach4Hobby\Module1.dll'
no file 'C:\Mach4Hobby\loadall.dll'
no file '.\Module1.dll'
stack traceback:
[C]: in function 'require'
[string "C:\Users\Richard\AppData\Local\Temp\le53EB.mc..."]:2: in main chunk

Do I need to be loading the module in the screen load script or providing a path to it somewhere?

If I put Module1.lua in C:\Mach4Hobby\, it opens a file dialog when I debug, then seems to run OK.  When I try to run it outside the editor, it doesn't do anything.

75
Mach4 General Discussion / Using Modules
« on: May 29, 2016, 03:56:04 PM »
Can't seem to find any documentation covering the basics.  Lets say I have a module "Module1.lua" and it has a function in it called "theFunction()".  Heres what I have so far.  This is the module...

Code: [Select]
local Module1 = {}

function theFunction()
wx.wxMessageBox('Module Test')
end

return Module1

This is the code in a button event that I want to call "theFuntion()"

Code: [Select]
sw = require('Module1')
inst = mc.mcGetInstance()
sw.theFunction()

I suspect the reason it's not working is because the module is not in the right directory...or one or both files need a path specified...or the module needs to be loaded on screen load.  Maybe a combination of these.

Thanks

76
Mach4 General Discussion / Re: M Code Parameters
« on: May 28, 2016, 06:34:46 PM »
Finally got it.  The return data type is a number, which is why 1 didn't equal 1.  The other issue was this....

mc.mcSignalSetState(mc.OSIG_OUTPUT1, true)

I thought that when I changed the if statement to evaluate 1 instead of '1', it was crashing M4.  What it was doing was finally working correctly.  mc.mcSignalSetState needs the handle to the output, not the output.  That was crashing M4.  Don't know how I missed that one.  I've written plenty of macros to manipulate outputs.

Here's the code.  When you call this M code, like M162 A3 B1, it turns on output 3.  M162 A3 B0 would turn it off.  I only use outputs 0 - 20 but the table could be expanded to include all of them.

Code: [Select]
function m162(args)
    inst = mc.mcGetInstance()
    ---------------------------------------------------------------
    -- A = OutputNumber, B = OutputState
    ---------------------------------------------------------------
       OutputTable = {
        [0] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT0),
        [1] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1),
        [2] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2),
        [3] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT3),
        [4] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT4),
        [5] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5),
        [6] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6),
        [7] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT7),
        [8] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT8),
        [9] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT9),
        [10] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT10),
        [11] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT11),
        [12] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT12),
        [13] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT13),
        [14] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT14),
        [15] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT15),
        [16] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT16),
        [17] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT17),
        [18] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT18),
        [19] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT19),
        [20] = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT20)
        }
    if (args ~= nil) then
        OutputNumber = mc.mcCntlGetLocalVar(inst, args, mc.SV_A)
        OutputState = mc.mcCntlGetLocalVar(inst, args, mc.SV_B)
        if (OutputState == 1) then
            mc.mcSignalSetState(OutputTable[OutputNumber], true)
        else
            mc.mcSignalSetState(OutputTable[OutputNumber], false)
        end
    else
        mc.mcCntlSetLastError(inst, 'No Arguments Found')
    end
end

if (mc.mcInEditor() == 1) then
    m162(nil)
end

77

I have just spent good 20 hrs to fix this... Is there another good way?

Don't feel bad.  I've been all day trying to figure out why 1 does not equal 1 in an M code.  Frustrating when things work fine in the editor, then go south when you run it in MDI with no error messages.

78
Mach4 General Discussion / Re: M Code Parameters
« on: May 28, 2016, 05:13:44 PM »
When I run the M code  "M162 A1 B1", it displays "Output out of rangeA1B1".  A1 clearly = 1, so...

if (x1 == '1') then

Should evaluate true, but it's not.

If I call M162 A2 B3, it displays "Output out of rangeA2B3".  I would expect it to do this since x1 does not equal 1, but it demonstrates that it is seeing the parameters being passed to the macro.

What data type does mc.mcCntlGetLocalVar return?  Maybe it just mismatched data type.  I know a string 1 does not equal a number 1.  I tried converting x1 to a string with the same results.

79
Mach4 General Discussion / M Code Parameters
« on: May 28, 2016, 02:48:05 PM »
Can any one explain why this is happening?  In the code below, it always jumps to ....

mc.mcCntlSetLastError(inst, 'Output out of range' .. 'A' .. x1 .. 'B' .. x2)

Yet x1 does equal 1.  I've tried converting to and from string and number and I either get the same result or M4 just crashes when the M code is called.  The code below runs, but it doesn't recognize that x1 == '1'.  If I change x1 == '1' to x1 == 1, M4 crashes.   mc.mcCntlGetLocalVar is retrieving the correct parameters.  Just can't seem to use them.


Code: [Select]
   if (args ~= nil) then
        x1  = mc.mcCntlGetLocalVar(inst, args, mc.SV_A)
        x2  = mc.mcCntlGetLocalVar(inst, args, mc.SV_B)
        if (x1 == '1') then
            if (x2 == '1') then
                mc.mcSignalSetState(mc.OSIG_OUTPUT1, true)
            else
                mc.mcSignalSetState(mc.OSIG_OUTPUT1, false)
            end    
        else
            mc.mcCntlSetLastError(inst, 'Output out of range' .. 'A' .. x1 .. 'B' .. x2)
        end
    else
        mc.mcCntlSetLastError(inst, 'No Arguments Found')
    end

80
Mach4 General Discussion / Re: M Codes in MDI
« on: May 27, 2016, 10:01:09 PM »
Screen elements must not be visible to a normally running M code.  The following code works....

    inst = mc.mcGetInstance()
    val = mc.mcProfileGetString(inst,'PersistentDROs', 'droTest1', '1')
    gCode1 = 'G00 Z' .. val
    mc.mcCntlGcodeExecute(inst, gCode1)