Hello Guest it is April 19, 2024, 06:54:28 PM

Author Topic: loop help  (Read 1726 times)

0 Members and 1 Guest are viewing this topic.

Offline Bill_O

*
  •  563 563
    • View Profile
loop help
« 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

Offline jbuehn

*
  •  101 101
    • View Profile
Re: loop help
« Reply #1 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 ;)

Offline Bill_O

*
  •  563 563
    • View Profile
Re: loop help
« Reply #2 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

Offline jbuehn

*
  •  101 101
    • View Profile
Re: loop help
« Reply #3 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

Offline Bill_O

*
  •  563 563
    • View Profile
Re: loop help
« Reply #4 on: July 30, 2020, 09:18:27 AM »
jbuehn,

That works great.
Thanks for the help.

Bill
Re: loop help
« Reply #5 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

Offline Bill_O

*
  •  563 563
    • View Profile
Re: loop help
« Reply #6 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

Bill
Re: loop help
« Reply #7 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.

Offline smurph

*
  • *
  •  1,546 1,546
  • "That there... that's an RV."
    • View Profile
Re: loop help
« Reply #8 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

Offline Bill_O

*
  •  563 563
    • View Profile
Re: loop help
« Reply #9 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