Hello Guest it is March 28, 2024, 05:20:44 AM

Author Topic: How to determine enabled/disabled state?  (Read 1470 times)

0 Members and 1 Guest are viewing this topic.

How to determine enabled/disabled state?
« on: February 11, 2020, 12:15:49 PM »
Just wondering how I can determine if the Enable/Disable button is enabled?  For example, when I click a button that involves a move, I thought I might check and see if the enable button is down before I even try to move.  Maybe flash a message - "Hey dummy...."

I am new to this so I don't really understand how all the parts work, so thanks for your patience and effort to teach me.  I really appreciate it.  I do a lot of looking before I post, but I haven't had any success with what I was trying.  I read about GetState and it reports the state of the controller, but I wondered what that enable button really is doing?

I thought that the GetState request would tell me, but all I ever get is that the machine is idle, so I thought maybe I'm asking the wrong question? 

I tried this code this morning.  All I got was a state of 0 regardless of the state of the enable button.  That's when I thought hmmmm....

Code: [Select]
local mcState, rc = mc.mcCntlGetState(inst)
if (mcState == mc.MC_STATE_IDLE) then
msg="Machine is IDLE  "..mcState
else
msg="Machine is not idle  "..mcState
end

wx.wxMessageBox(msg)

Then reverted back to my VB days and wondering if I could just figure out the property of the button (up or down)?

Thanks for any help. 

Tony
Re: How to determine enabled/disabled state?
« Reply #1 on: February 11, 2020, 11:55:21 PM »
Well, I've been reading a lot (my eyes are going crossed and my head hurts).  I see where I can use the scr calls to get/set button properties.  That might be helpful if/when I can figure it out.  And then I see there is a signal  mc.OSIG_MACHINE_ENABLED that might be what  I am after, but I am still trying to figure out just how to read the state of that signal.

Going to bed now.....
Re: How to determine enabled/disabled state?
« Reply #2 on: February 12, 2020, 01:58:11 AM »
Hi,
by and large the recommendation is not to the scr. table of functions, they are not well documented for a reason.

You are correct to use the mc.OSIG_MACHINE_ENABLED signal.

To read the state of a signal you first need to find its handle, another way of saying its address. Lua is a dynamic language and variables
come and go throughout the Mach session and there is NO certainty that a variables address is constant across start-ups.This is done by:

local desiredVariableHandle=mc.mcSignalGetHandle(inst,mc.OSIG_MACHINE_ENABLED)

Now you can read the variables state:

local desiredVariableState=mc.mcSignalGetState(inst,desiredVariableHandle)

You can read the variables state as many times as you like but really all you want to know is when it changes.
In Mach3 you would read the variable multiple times a second and 'do something' when the signal changed, this is called 'polling'.
Its old school and inefficient. Mach4 has a new way of doing this called the Signal Library Table and Signal Script. It takes quite a bit
of thinking about and you may think that its too complicated to do what you want but you need to make the effort because it
reveals the true power of Lua....and its far FAR in excess of VB.

May I suggest reading my .pdf on the Signal Library and Signal Script:

https://www.machsupport.com/forum/index.php?topic=40051.msg267764#msg267764

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: How to determine enabled/disabled state?
« Reply #3 on: February 12, 2020, 02:46:33 AM »
If your aim is to prevent yourself pressing buttons that require Mach4 to be enabled, another option is to select the 'Enable with Machine' property for the button. When this is selected the button will disable when Mach is disabled, and enable when Mach is enabled.

Thank you!
« Reply #4 on: February 12, 2020, 12:41:34 PM »
That gives me plenty to chew on.  I'll head out to the shop this afternoon and play/learn a little.  I really appreciate the help.  At some point I'll get comfortable enough with this stuff to so the few things I want and just make stuff! 

I'm trying to go a little deeper and add more safety/error capture/etc. to this stuff.  In most of the VB and other stuff I have done, the consequences are a nuisance.  With this, bad things can happen!  I used to work at a aftermarket automotive camshaft company.  We made cam lobe grinding masters on a mill with a rotary table.  My co-hort ran the mill right through the table!  We learned that the machine does what you tell it and tried very hard to get there - regardless of what is in the way.

Thanks again,

Tony
Crash!
« Reply #5 on: February 12, 2020, 04:51:44 PM »
Well, I tried to just get the state and it runs in the debugger ok, but when I save it and go back to Mach and try the button, it crashes MAch4.  The button is on a panel if that makes any difference?

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

local mHandle = mc.mcSignalGetHandle(inst,mc.OSIG_MACHINE_ENABLED)
local mState = mc.mcSignalGetState(inst,mHandle)

msg = "Current machine state is "..mState
wx.wxMessageBox(msg)
This works
« Reply #6 on: February 12, 2020, 05:38:42 PM »
I cut and pasted an example from the Mach4 Scripting manual (for spindle) and it worked fine.  So, I changed the signal name and it works just fine.  Maybe my variable names were not good?

Anyway, thanks again. 

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

local hsig = mc.mcSignalGetHandle(inst, mc.OSIG_MACHINE_ENABLED)
local Mstate = mc.mcSignalGetState(hsig)

local msg = "Machine state = "..Mstate
wx.wxMessageBox(msg)

Ahh..I just noticed that in the crash version, I used this  mc.mcSignalGetState(inst,variable) and in the version that works, I just used the handle variable. 
« Last Edit: February 12, 2020, 05:48:00 PM by TonyMemphis »
Thanks again....
« Reply #7 on: February 17, 2020, 10:51:18 PM »
Thanks for the link to your Signals tutorial Craig.  Appreciate the link and the effort.  And thanks for the info on "Enable with machine" swifty.  I have all my scripts running now.  Oh what a feeling!

I found myself repeating code, so I'm now reading up on functions.

Tony
Re: How to determine enabled/disabled state?
« Reply #8 on: February 18, 2020, 12:28:00 AM »
Hi,

Quote
Oh what a feeling!

Great stuff. Many newcomers to Mach4 bemoan the apparently confusing structure of Mach4, Lua and the use of API's etc.
In truth if you don't need any 'frilly bits' then you don't have to do any coding at all. But you have persevered and have got a result
and I share your sentiment 'Oh what a feeling!'.

Learning to code in Lua and Mach4 is a learning challenge, no doubt. The satisfaction in mastering it was, for me at least, very much an unanticipated
pleasure.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'