Hello Guest it is January 26, 2020, 09:09:19 PM

Author Topic: How to Emulate a Momentary switch?  (Read 1952 times)

0 Members and 1 Guest are viewing this topic.

Offline CRS

*
  •  61 61
    • View Profile
How to Emulate a Momentary switch?
« on: May 23, 2017, 11:48:32 PM »
Good day to all my fellow CNC enthusiasts.

It’s been a while since I have bugged you all, but here goes.

Is it possible to emulate a momentary switch in the Mach4 screen? Maybe some code in Lua that does a timed active state. I don't want a toggling switch like is on one of the screen sets for the spindle forward, reverse and stop.  It has to pull in a latching relay circuit for a spindle but not stay in an active condition.

Anyone seen anything written?

Craig.

Offline smurph

*
  • *
  •  1,187 1,187
  • "That there... that's an RV."
    • View Profile
Re: How to Emulate a Momentary switch?
« Reply #1 on: May 24, 2017, 03:08:24 AM »
One shot latch in the PMC.  The PMC is the Programmable Machine Controller.  It implements ladder logic. 

Offline CRS

*
  •  61 61
    • View Profile
Re: How to Emulate a Momentary switch?
« Reply #2 on: May 24, 2017, 03:33:20 AM »
Hi smurph,

You've come to my aid before.  Not sure if you can remeber, but I have little to no clue about anything Lua, and about the same for the PMC.  Where do I go looking?

Can you give me a hint.

Thanks Smurph.

Craig.

Offline DazTheGas

*
  •  752 752
  • DazTheGas
    • View Profile
Re: How to Emulate a Momentary switch?
« Reply #3 on: May 26, 2017, 03:28:21 PM »
Hi Craig, ive read both this thread and another of what you are trying to do....

Quote
If you look in your Mach4 Hobby Program folder and then in the Lua Examples, there is a file called Activate Output.mcs.  I think we may be able to use this.
from another thread.

Well there is a problem with that script as it uses wx.wxMilliSleep(ActivateSignalTime) this command will halt the GUI for the time specified. so the simplest way is to use a timer event that does not halt the GUI in any way try this

https://youtu.be/1Q5YxjsBfT4

DazTheGas

Theres always time for a choccy biccy

Offline CRS

*
  •  61 61
    • View Profile
Re: How to Emulate a Momentary switch?
« Reply #4 on: May 26, 2017, 06:22:27 PM »
Hi DazTheGas,

Thank you so much, that was fantastic.  Now to give it a try.

I forgot to ask this obvious question.

If I set up buttons with this timer for say Spindle FWD, REV and STOP, will spindle commands in the g code (M3, M4 and M5) also follow suit?

Craig

Offline CRS

*
  •  61 61
    • View Profile
Re: How to Emulate a Momentary switch?
« Reply #5 on: May 28, 2017, 02:25:54 AM »
Hello DazTheGas.

Your timer idea works very well.  Thank you, thank you!!  I made a host of buttons and had a great time experimenting and embellishing.  Don't know how you do it, but just thankful you do.

It became apparent very quickly that the M3 & M4 don't automatically activate the timer. Darn!  So I read up and can see I have to do a macro for them.

Spent most of today failing to incorporate the timer function into the M3 macro.  I watched your video on naming M functions and storing in Macros Folder, but I cannot get the timer to happen.  Error after error.  I have given it my best, but it’s beaten me.  ???

Daz, can I ask you to stretch just a little further with this one?

How to do the same with the M codes?  Not a video, just a bit of help with the code.

Thankful but perplexed

Craig.

Offline Chaoticone

*
  • *
  •  5,634 5,634
  • Precision Chaos
    • View Profile
Re: How to Emulate a Momentary switch?
« Reply #6 on: May 28, 2017, 09:17:43 AM »
For strictly momentary buttons you can use a standard button and add script to do what you want on the left down script and then stop that in the left up script. It will do what you want when you push the button and stop when you let off. Like the default jog buttons do. This is one of the greatest things about lua. It's smart enough to sense press and release.
;D If you could see the things I have in my head, you would be laughing too. ;D

My guard dog is not what you need to worry about!

Offline DazTheGas

*
  •  752 752
  • DazTheGas
    • View Profile
Re: How to Emulate a Momentary switch?
« Reply #7 on: May 28, 2017, 02:57:34 PM »
The good thing with Mach4 is that it runs multiple instances of wxLua, there is one for the Gui itself and a very clean one for Macros. The instance for Macros does not allow for creating windows or panels, so using the wxMilliSleep within a macro would be absolute fine and will not hold up the Gui or PLC etc.

Code: [Select]
function m3()
    local inst = mc.mcGetInstance()
    local sig = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON) -- set to desired signal
    mc.mcSignalSetState(sig,1) --turn on signal
    wx.wxMilliSleep(2000) -- sleep for 2 sec
    mc.mcSignalSetState(sig,0) -- turn off signal
end
if (mc.mcInEditor() == 1) then
 m3()
end


DazTheGas
Theres always time for a choccy biccy

Offline CRS

*
  •  61 61
    • View Profile
Re: How to Emulate a Momentary switch?
« Reply #8 on: May 29, 2017, 06:29:55 AM »
Daz fantastic.  That did the trick.  Thanks again you legend.

Chaoticone, I had thought of the Jog buttons and never followed it up, thinking it might cause arking issues with the relays not quite latching if you don't hold the button on long enough.  But I went ahead and gave it a try.  Well, guess what, the relays actually do latch without an issue.  Silly me.  Thank you for taking the time to chime in, much appreciated.

DazTheGas thanks for your work on this, I now have two options and can try both over time.  Definitely needed the time delay on the M codes and that just works a treat.

I need three separate switches for three relays (FWD, REV and STOP), but ah ha, the fact that Spindle CW, CCW and Stop (1,-1, 0) are all tied to the same function has thrown up a lemon.  There is no OSIG_SPINDLESTOP.  Isn’t this fun….

Thanks Guys, when I get this all sorted and working, I will put up a bit on how it was done and what worked out the best.

Thanks again Daz and Chaoticone.

Craig.

Offline CRS

*
  •  61 61
    • View Profile
Re: How to Emulate a Momentary switch?
« Reply #9 on: June 04, 2017, 02:10:07 AM »
Just to finish off.

I thought it may be worth spelling out the process of how I have arrived at a satisfactory result using on-screen buttons as momentary switches to control the Spindle in what is most likely an uncommon requirement in this day and age.  However, I am sure there will be others in the future that may have a similar requirement of activating latching relays for spindle control or another function where the buttons can’t remain activate or in an on state.

The Bridgeport Mill that I am helping a friend convert has a relay system for spindle control and all three relays are mechanically interlocked so only one of the three (Spindle FWD, REV or STOP) can be on/latched at any one time.

For the Spindle FWD (CW) and REV (CCW), I ended up achieving a small win by using the on-screen standard buttons as Chaoticone suggested in one of the earlier posts in this thread.  I first tried the timer system that DazTheLegendaryGas suggested using toggle buttons, and that worked very well.  Although a bit more complex than necessary, which was my fault.  I asked Daz to help me get a timed system for an on screen button. Then I got to thinking, I should at least try the-afore mentioned simple method.  And that ended up being what we have stayed with.

This are the steps I took.

Put the screen into edit mode, then select the button for programing.  In my case it was the Spindle FWD or REV button, the STOP button has different requirements and so I will get to that after.
On the bottom left of screen select the event properties window.  You will see the function “SpindleCW()” in the Left Up Script.  You can either Comment that out, which means putting “—“ (two or three hyphens in front of the word), or just delete it.  Nice to leave it there for future reference though.

Then In the top row, Left Down Action, which refers to the left mouse button, select “Spindle CW” from the supplied drop-down list, and underneath that in the Left Up Action, select “Spindle Off”.

Do the same for the Spindle REV button, but of course select “Spindle CCW” for the Left Down Action and “Spindle Off” for the Left Up.  Come out of Screen Edit Mode and that’s it.  Momentary buttons that for all intents and purposes act like their physical counterparts.  This is actually covered quite well in the Mach4 Screen Editing Guide, albeit not for spindle though.

My friend with the Bridgeport, who I will call Andrew, as that’s his name, has a touch screen.  My concern with this was that the momentary action may not be enough to energise the latching coil on the relays in the mill. However, that concern was short lived, because just like a physical button, you simply push, in the case of the touch screen, or mouse left click down as long as is needed.  I have actually attempted to get the relays not to latch by being too quick, but as yet haven’t been able to trick it.

Now for the Spindle OFF or STOP button.  This one presented a challenge as the Lua code for the spindle function within the Screen Load Script doesn’t have a separate function for each state. It is all contained in the one function.  Either a “1” for FWD, “-1” for REV or a “0” for STOP.  The “0” (Zero) represents a non-energised state, but in this case I needed an energised state to activate an output which in turn would energise the coil on the STOP relay at the machine.

Like a lot of other guys who have come into this arena knowing nothing of programming, it was just a lot of trial and error to get this working.  I have posted this in another thread by Soruud, titled “Need some minor Lua adjustments”, but here it is again.

Code: [Select]
--Left Down Script for Spindle Stop & Cycle Stop, but Left up for Enable/Disable.


local inst = mc.mcGetInstance()

local hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT0)
spinoff = mc.mcSignalGetState(hsig)
if (spinoff == 0) then
    mc.mcSignalSetState(hsig, 1)
else
    mc.mcSignalSetState(hsig, 0)
end

Code: [Select]
--Left Up Script for Spindle STOP and Cycle Stop, but Left Down for Enable/Disable.


local inst = mc.mcGetInstance()

local hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT0)
spinoff = mc.mcSignalGetState(hsig)
if (spinoff ==1) then
    mc.mcSignalSetState(hsig, 0)
end

Using the above code in the Left Down Script (“Script” not “Action”) and the Left Up Script respectively, makes the Spindle STOP button perform just as the FWD and REV buttons.  You can’t just select Spindle Off from the Up and Down Action drop down lists, as that is associated with the original Spindle Function in the Screen Load Script, which will return a “0” and has nothing to do with the new selected output which this Spindle STOP button controls.
As you can see, I have it assigned it to Output 0, so remember to change it to whatever output you want it assigned or Mapped to in Mach.  So a bit more success. The Stop button acting like a momentary switch, can now activate an external relay and then turn off.

After this, there are still a couple of other things to modify. Normally if you click or press Cycle Stop or the Disable buttons, there is code in those two functions to stop the spindle.  But as I created a different method of stopping the spindle, there is nothing present in the Screen Load Script that will activate my chosen output and stop the Spindle when either Cycle Stop or Disable is pressed.

I tried to implement some of the code that I used in the STOP button within the Screen Load Script and it did partially work.  I could get the spindle to stop when Cycle Stop was pushed, but then Spindle Stop would remain activated, requiring an additional click to deactivate it.   I don’t know or understand enough to figure that out, so I went back to the Cycle Stop button.

If you look at the Event Properties for the Cycle Stop button, you will see it has “Cycle Stop()” in the Left Up Script.  This is where we need to put a bit of code.  So once again comment out, or delete the “Cycle Stop()” and then up above that in the Left Down Action, choose Cycle Stop from the drop-down list.  Actually it didn’t seem to matter if I put the Cycle Stop in the Left Up or Left Down Action.  So I just put it in both.

Copy the same code as the Spindle STOP button into the Left Down and Left UP Scripts.  If this is done correctly, the Cycle Stop button should now stop the spindle and the Spindle STOP button will there after deactivate as it should.

From this I learned that you can use all four or these event properties.  The Left Down and Up Action can have two different things like Spin FWD and Spin OFF, and the UP and Down Scrips can also call for something else to happen concurrently, actually it can be quite unrelated, like a light turning on or a vacuum etc..

The last one was the Disable/Enable button.  This button is a toggle and required a bit of trial and error with the Scripts.  I ended up reversing their placement.  So the Up Script went in the Down Script and visa versa.  The end result is that when you press the Disable button, the spindle STOP button is activated and actually stays activated until you click on the STOP button one more time, or press the Enable button.   In fact probably not such a bad thing.
I tried to get the Spindle STOP button to automatically deactivate, but that is once again above my pay grade.  If anyone can get that happen, please share with me.  But all in all, I call this…

Success!

You also need the M3, M4 and M5 to replicate the same kind of action as the on screen buttons.  DazTheGas was kind enough to show me how to do that using a timer.  All work as they should and are attached below.  All three M codes must be placed in the Macros folder of the Profile that you are using.

If this saves just one person from hours of frustration, it was worth me over explaining what I am sure is quite a simple process to the educated…

Have fun.
Local inst = mc.mcGetInstance()
CRS = Craig Smith