Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: frankg521 on February 01, 2023, 10:28:41 PM

Title: adding a simple relay
Post by: frankg521 on February 01, 2023, 10:28:41 PM
Hello I would like to add a simole relay to Mach4 and ESS to control dust collection
any help would be Appreciated
Title: Re: adding a simple relay
Post by: joeaverage on February 01, 2023, 10:52:41 PM
Hi,
this post should be on the Mach4 General Discussion Board and I would guess Tweakie will shift it there in due course.

Mach has quite a range of output signals, and while defined and usually behave in certain sensible ways they don't actually do anything until
or unless you logically connect that signal to a device.

In this case I would suggest you use mc.OSIG_COOLANT_ON. Naturally you don't have coolant but often Gcode will include an m8 which turns the coolant
ON and consequently Mach's mc.OSIG_COOLANT_ON goes active and the Gcode concludes with an m9 which turn the coolant off and the output signal
mc.OSIG_COOLANT_ON goes inactive.

In order to use this signal you need to connect a spare output on the ESS/breakout board combination to Machs output signal. First you assign an pin
in the ESS plugin PinsConfig tab, and then enable Machs coolant/dust output and logically connect the output the the pin you intend to use. See the pics.

Now you can use the output of the pin on the breakout bpoard to turn on a relay which would turn on your coolant pump if you had one, or your dust extractor,
but you could get really creative and have it turn on the beer pump....or better still a Bourbon pump....who said CNC had to be soberboring!

Craig
Title: Re: adding a simple relay
Post by: frankg521 on February 24, 2023, 09:39:50 AM
Hi Craig
thanks for the reply firstly the beer keg sounds like a good idea!! M8 is already in use
and i would like to use a button on the mark4 screen set i have done the research on adding the button
but I seem to be stuck on making the relay turn on and off i can get the output to light up but it stays on
is this something you can help me with
thx in advance

Frank
Title: Re: adding a simple relay
Post by: joeaverage on February 24, 2023, 03:58:07 PM
Hi,
there are a number of approaches to doing that, and which one you choose will depend on what conditions you want to apply.

For instance, you may want a pump to turn on by a physical button, or an on-screen button, but ONLY while the spindle is operating say.
This would be a good example of using Machs programming to introduce logic.

Lets start with just the simplest of the solutions to turn a relay on or off. This would be a toggle button on screen.

I have attached a pic of a toggle button. Note I just put it on the FileOps tab  simply for the sake of room, but you can put it anywhere you want or need.
Note that you have access to all the properties of the button, things like its size and location, its text, both button Up and button Down, colour etc but most importantly
look near the lower end of the properties list and you'll see that I have assigned Output#20 to the button from the drop down list of Machs available and defined
output signals.

Note that many of the properties are just so you can arrange the button  on the screen to look and feel right with your control. The assignment of a matching output signal
is the ONLY thing thus far that means the visual representation of the button actually 'do anything' Anyway the upshot now is that every time you operate the button Output#20
will change state. To turn your relay on and off requires that you connect Output#20 via your motion control through the breakout board to the relay.

I use an ESS and I'm going to picture how this is done with an ESS.

In the ESS plugin on the PinsConfig tab I have assigned an ESS output, Port1, Pin9 and given it an alias 'MyNewRelay'. In the same ESS plugin I have
now assigned and enabled Output#20 from the drop down menu to my newly configured pin 'MyNewRelay'.

Now whenever Output#20 changes state, which reflects the on-screen toggle button, the Port1Pin9 of the ESS will also change state, which will of course be connected to your
BoB and that BoB output will turn on or off the realy, or beer pump or whatever.

Craig
Title: Re: adding a simple relay
Post by: joeaverage on February 24, 2023, 04:15:48 PM
Hi,
you might be thinking 'is that all a toggle button can do', well the answer is most emphatically no.

In the first instance I introduced you assigning one of Machs many outputs to reflect the state of the toggle button, but can can do much more than that. Its a good place to start
and experiment with. Notice on the same properties page there is an InputSignal assignment. With an Input assigned you could use a physical push button to operate the on-screen button,
and the on-screen button has all the properties that you have already experimented with.

Not only can you assign both Input and Output signals to a toggle button each operation of the button can cause an Action (pre-defined in Machs repertoire) AND/OR a script. This is seen on the Events tab
of the toggle button. For instance you could cause a script to run on activation of the button and that script might apply some logic, say whether the spindle is running say,
rather than the straight On/Off  of the Output signal assignment.

Craig
Title: Re: adding a simple relay
Post by: frankg521 on February 24, 2023, 09:15:25 PM
Craig
good thing I got this working I really needed Beer..
I got it working under pin 14, P2-pin14. when I had it on pin17 it would not work. I would set it to active low and the relay would come on but button
would not switch. I have C25 BOB otherwise same as you. pin 17 must be smoked.. i like the physical button idea. So if i connect a external button to an input and select that input in the screen button properties it will do the same as the onscreen button?? I can do that. as far as the scripting for the on screen button what would be an example of that? by the way thanks' for the help much appreciated.. this is a good thread I'm sure it will help a lot of people..

Cheers
Frank
Title: Re: adding a simple relay
Post by: joeaverage on February 25, 2023, 05:01:03 PM
Hi,
a toggle button is just an on/off switch. In Mach you can make an on-screen button....but there is no requirement that the button be visible. Then you would use something like a physical button
to operate the toggle switch.

Going one step further why have a toggle button at all?. All you require is an output, in the example I gave I chose Output#20 at random. If you look at all of Machs defined output
signals there a hundreds of them, some have a specific meaning and are tied to certain events, but many are just defined signals, they do nothing unless you code them to.

A good example of a defined signal and a predetermined output is:

OSIG_SPINDLEON.

It will go active every time the spindle is turned on. There is to my knowledge any way to change that, its part of Machs DNA.

An example of a defined signal that has no effect is:

OSIG_OUTPUT21.

The signal is defined but it has no specific meaning until you attach some meaning to it. Attaching it to a toggle switch is an obvious example, but by no means the only way it can be used.
ALL of Machs signals, both inputs and outputs have the property that if any one of them change state then the SignalScript runs. Many of Machs signals are internal, say between Mach and a motion
controller and you'll never see them, and some like push button inputs you have contrived yourself.

Imagine you have some macro within your Gcode job and in that macro you assert OSIG_OUTPUT21 thereby changing its state. The SignalScript will run. If there is an entry in the SigLIb table
[mc.OSIG_OUTPUT21] then the function attached to that entry will run. It may apply some logic before turning on yet another output which in turn control a pump.

Look at the SignalScript.pdf that I wrote hopefully to make it a bit clearer.

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

The SignalScript and Lua property 'functions as a first class value' offer an incredible leap forward over Mach3 which could only ever poll inputs. Understanding this feature is a key milestone
in coding within Mach4.

Craig