Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: Bill_O on July 28, 2020, 05:21:55 PM

Title: loop help
Post by: Bill_O on July 28, 2020, 05:21:55 PM
I am trying to make a loop that will turn off all outputs.
I know it is probably something I have wrong in the format but I have tried many different ways and can not get it.

local OutputNum = 0
   while (OutputNum <= 63) do
      OutputName = (mc.OSIG_Output .. 'OutputNum')
      hsig = mc.mcSignalGetHandle(inst, 'OutputName')
      mc.mcSignalSetState(hsig, 0)
      OutputNum = OutputNum + 1
   end

Any help is greatly appreciated.
Bill
Title: Re: loop help
Post by: jbuehn on July 28, 2020, 06:06:22 PM
There's an API function specifically for looping through signals, mc.mcSignalGetNextHandle, which the API docs have details for using.

I'd highly recommend using that API function, but with your current loop you'd probably need something like this in place of your lines 3-5:

local hsig, rc = mc.mcSignalGetHandle(inst, _G.mc["OSIG_OUTPUT"..OutputNum])
rc = mc.mcSignalGetState(hsig, 0)

Disclaimer...this isn't tested and you should add checks of the return codes ;)
Title: Re: loop help
Post by: Bill_O on July 29, 2020, 10:55:51 AM
Well I could not get the original to work.
It went in the loop but did not change the output states.

I tried using NextHandle.
It is not going into the loop.

Once again I am sure it is a format error but I have tried all that look possible to me.
Here is the last code.

HMCSIG, OutputSig = 0
   while (mcSignalGetNextHandle(inst, SIG_TYPE_OUTPUT, OutputSig, 63)) do
      mc.mcSignalSetState(OutputSig, 0)
      wx.wxMessageBox("In the loop")
   end


Bill
Title: Re: loop help
Post by: jbuehn on July 29, 2020, 08:18:24 PM
Try this. You can add some error reporting where rc ~= mc.MERROR_NOERROR.

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

-- Get first signal handle
local hsig, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT0)
if (rc ~= mc.MERROR_NOERROR) then
-- Failure to acquire signal handle, send error message?
else
-- Set state of mc.OSIG_OUTPUT0
mc.mcSignalSetState(hsig, 0)

-- Loop through remaining outputs
-- Change "63" to however many outputs you want to turn off
for i = 1, 63 do
hsig, rc = mc.mcSignalGetNextHandle(inst, mc.SIG_TYPE_OUTPUT, hsig)
if (rc ~= mc.MERROR_NOERROR) then
-- Failure to acquire signal handle, send error message?
break
else
mc.mcSignalSetState(hsig, 0)
end
end
end

Title: Re: loop help
Post by: Bill_O on July 30, 2020, 09:18:27 AM
jbuehn,

That works great.
Thanks for the help.

Bill
Title: Re: loop help
Post by: Sergio007 on July 30, 2020, 04:35:50 PM
Hello,

Thank you for sharing your knowledge and experience. Perhaps you could point me in the right direction too.

I'm trying to produce a couple M functions to turn on/off Output #2, but I can't get it to work. I will use it to trigger the arc on a welding machine. Here is the code I have for the ON function. I thought of adding the conditional at the end, as suggested in the manual, but that doesn't correct my error.

function m10()
inst=mc.mcGetInstance()
hsig, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2);
mc.mcSignalSetState(hsig, 1);
end

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


There is also the option presented in "Down and Dirty “mcLua” scripting, quick ref guide." But I lack the knowledge to initialize the variables at the beginning, so I can't really implement the code. Also, I do not need the IF in the middle, as I just want the state to be ON.

hsig, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT##);
local state = false;
local VarIamTestingFor = SomeThingIamWatchingOrSettingOrValue;
if VarIamTestingFor == WhatSetsTheOutputToOn then
state = true;
else
state = false;
end
mc.mcSignalSetState(hsig, state);


Any help or guidance will be much appreciated.

Cheers
Title: Re: loop help
Post by: Bill_O on July 30, 2020, 06:02:45 PM
Sergio,

This might help.
It goes over several basic things.

https://www.machsupport.com/forum/index.php?topic=43260.0 (https://www.machsupport.com/forum/index.php?topic=43260.0)

Bill
Title: Re: loop help
Post by: Sergio007 on July 30, 2020, 11:23:46 PM
Hi Bill,

That's a great document! Thank you for sharing it. The main bit I was missing is that the function name and the corresponding .mcs file are case sensitive. I'm putting the code here for other newbies like me who might find it useful.

function M10()

-- Instanciate the function
   local inst = mc.mcGetInstance()

-- Asign Output #2 to MyVariable
   MyVariable = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2)

-- Asign the value 1 to the signal state, which activates the output.
   mc.mcSignalSetState(MyVariable, 1)
end

-- As explained in the manual and in the document prepared by Bill
if (mc.mcInEditor() == 1) then
   M10()
end


It was also useful to verify that the wiring on my boards is correct, by using a toggle button. For other users who may find this thread, here is a quick example of how to set up a toggle button on the screen, to ON/OFF the Output## of choice.

https://www.youtube.com/watch?v=3XY0o0gXz2A

So glad to have something, even so small, to give back.
Title: Re: loop help
Post by: smurph on July 31, 2020, 02:39:52 PM
I see capital lettering on the M code.  Change M10 to m10 on the function name, the file name, and in the mcInEditor()  Test stub that calls M10.

M codes are ALWAYS lower case.  This is because the interpreter converts everything to lower case when it processes the G code line.

Steve
Title: Re: loop help
Post by: Bill_O on July 31, 2020, 03:07:26 PM
Steve,

I had done that and it still did not work but it did the last time I did it.
I am beginning to think I did not restart Mach4 after changing.

Bill
Title: Re: loop help
Post by: Sergio007 on August 01, 2020, 11:17:40 AM
I see capital lettering on the M code.  Change M10 to m10 on the function name, the file name, and in the mcInEditor()  Test stub that calls M10.

M codes are ALWAYS lower case.  This is because the interpreter converts everything to lower case when it processes the G code line.

Steve

Hi Steve,

Thank you for clarifying that m codes should always go with lowercase. What I tried to say was that I got the function to work. My problem was the inconsistency between the name of the mcs file <M10.mcs> and the name of the function <m10()>. If both are lowercase or both are capital, then it works. Anyway, I will swap everything to lowercase, as you say it is more correct.

Sergio