Hello Guest it is April 19, 2024, 03:23:01 AM

Author Topic: Event Script for Enable Button  (Read 3811 times)

0 Members and 1 Guest are viewing this topic.

Event Script for Enable Button
« on: May 03, 2016, 04:18:34 PM »
I have an DSPMC that I'm trying to script a button that will toggle Enable.

Here is the code I am trying per the Mach4 Core API, but it fails to compile...any help would be appreciated:
Code: [Select]
-- Enable button
if (sig == mc.ISIG_INPUT4) and (state == 1) then
    MINSTANCE mInst = 0;
    int rc = mcCntlEnable(mInst, TRUE);
end

I had also tried the following code from the Scripting Manual which gave an error about the second parameter, which is why I moved onto the API syntax:
Code: [Select]
-- Enable button
if (sig == mc.ISIG_INPUT4) and (state == 1) then
    local inst = mc.mcGetInstance()
    mc.mcCntlEnable(inst)
end
Re: Event Script for Enable Button
« Reply #1 on: May 03, 2016, 05:01:45 PM »
I'm not sure I understand:
Quote
I'm trying to script a button that will toggle Enable
That sounds like you want to create a new button that you can click on to enable Mach4 (like the current "Enable/Disable" button when Mach4 is disabled).  Is that correct?

But then you post code that looks like it is attempting to look at the INPUT4 signals and enable Mach4 when INPUT4 is asserted.  And you don't say WHERE you are placing that code, though it looks like maybe you intend it to be part of the screen load script?

If you want to monitor the INPUT4 signal and have it cause Mach4 to become enabled  when INPUT4 goes active, add the following code to the screen load script in the signal library section:
Code: [Select]
[mc.ISIG_INPUT4] = function (state)
   if (state == 1) then
      -- mc.mcCntlLog( inst, "INPUT4 asserted", "", -1 );
      mc.mcCntlEnable( inst, 1 );
   else
      -- mc.mcCntlLog( inst, "INPUT4 de-asserted", "", -1 );
   end
end,

Note that the "inst" variable used is one that is set at the start of the screen load script, so for code *IN* the screen load script you don't need to call mc.mcGetInstance() again.  I ran a quick test of this code and it appears to work.  It also appears (again, in a QUICK test) that re-enabling Mach4 when it is already enabled and while it is jogging, does not interfere with the jogging move.  To be safer, you would want to check to see if Mach4 was already enabled, and only call mc.mcCntlEnable() if Mach4 was disabled.

Bob
Re: Event Script for Enable Button
« Reply #2 on: May 03, 2016, 05:41:18 PM »
It is a HARD button wired to input 4
Re: Event Script for Enable Button
« Reply #3 on: May 03, 2016, 05:58:24 PM »
Are you the same person as the original poster using a different user name?

It is a HARD button wired to input 4
Is this "input 4" on your motion control board?  Because there is nothing "hard wired" to the Mach4 "Input #4" signal (on the Configure->Mach dialog in the "Input Signals" tab), which is what your sample scripts were checking.

If your motion controller plug-in calls its input signals "Input 1", etc. (like our PMDX-424), that can be confusing as Mach4 *ALSO* calls some of its input signals "Input 1", etc.  If this is "input 4" on your motion controller board, then you will need to map that physical input to one of the Mach4 input signals.  To do so, go to the Configure menu and select "Mach...".  Click on the "Input signals" tab and scroll down on one of the "Input #0" through "Input #31" inputs.  Choose one ("Input #4" in the sample code I posted above), enable that signal, choose your motion device and input signal name.

Bob
Re: Event Script for Enable Button
« Reply #4 on: May 03, 2016, 06:07:04 PM »
No, I work with him.
Yes, this is a momentary contact button wired to the motion control board. This is then mapped to be Input #4" on the input signals. So when I push the button, it lights up input 4 in Mach4. We have confirmed this in the diagnostics screen.
Re: Event Script for Enable Button
« Reply #5 on: May 03, 2016, 11:22:57 PM »
Then the above script should work for you - presuming that the screen set you are using is based on the wx4 screen set.  The wxMach screen set does not have the "signal library" section in the screen load script.

NOTE: If you are using the "wx4" screen set (if you are, it should say so in the lower right corner of the screen), make a copy of the screen set and edit that copy.  If you make changes in the wx4 screen set they may (probably WILL) get overwritten if/when you install a new version of Mach4.  There are two ways to copy the screen set:

(1) Using Windows Explorer, go to the C:\Mach4Hobby\Screens directory and copy your screen set.  Then when you start Mach4 with your profile, go to the "View" menu and select "Load Screen", then load the new file that you just created.

or...

(2) Start Mach4 with your profile.  Go to the "Operator" menu and select "Edit Screen".  Once in the screen editor, go to the "Screen" menu and select "Save Screen As..." option, and enter a new name.
Re: Event Script for Enable Button
« Reply #6 on: May 04, 2016, 09:29:41 AM »
bob,

we are going to test your code shortly, but i wanted to ask why should the code be placed into the screen load script in the signal library section rather than the event script section?

i should note that while i have done lua programming before, i am an extreme noob with regards to mach4. yesterday was my first day to read up on any of it. so my questions come from a desire to learn more.

Offline DazTheGas

*
  •  778 778
  • DazTheGas
    • View Profile
Re: Event Script for Enable Button
« Reply #7 on: May 04, 2016, 09:52:40 AM »
The signal script is loaded at the start of mach4 and is best place for what you need it is then run internally like a windows event/function so a lot faster than the plc script.

The script that bob posted will work fine although to toggle the enable button and allow it to run side by side with the gui i would use

Code: [Select]
[mc.ISIG_INPUT4] = function (state)
   if (state == 1) then
      -- mc.mcCntlLog( inst, "INPUT4 asserted", "", -1 );
      mc.mcCntlEnable( inst, 0 ) -- Changed
   else
      -- mc.mcCntlLog( inst, "INPUT4 de-asserted", "", -1 );
      mc.mcCntlEnable( inst, 1 )  -- Added
   end
end,

DazTheGas
New For 2022 - Instagram: dazthegas
Re: Event Script for Enable Button
« Reply #8 on: May 04, 2016, 10:11:53 AM »
By "event script section" do you mean the "Signal Script"?  Or the PLC script?  I'm not an expert (yet) on the Lua side of Mach4, but my understanding of the way things are arranged in the wx4 screen set (but NOT the wxMach screen set) is this:

PLC script - this runs at a periodic interval (default is 50ms).  You could place the code there, but you would have to check for a change in state of the input signal.  Do-able but more work and not as efficient as the following methods as this code would run even if input signal hasn't changed states.  And there would be a variable delay (from 0ms to the "PLC Interval" defined in the top level of the screen set) in responding to the input signal.  In your case that would not be an issue as all you are doing is enabling Mach4.

Signal script - this script, in the wx4 screen set, is just the dispatcher for Mach4 signal events.  Every event in Mach4 runs this script, which simply checks to see if there is a script associated with that event (in the "signal library", which is just an array of functions indexed by event number) and if so, it runs the script.  Again, you could place the code here, but that doesn't fit with the overall design of the event flow in the wx4 screen set.

Screen Load Script - This is where the "signal library" is defined, and where the screen set places code for its event handlers.  The screen load script is run once at start up.  Part of what is does is initialize the SigLib[] array with code to handle the various events.  SigLib[] is then used by the signal script to process events.  This code is only run when its associated event is triggered.