Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: JeffMach3 on August 06, 2019, 03:14:47 PM

Title: Creating M-Code to activate Outputs
Post by: JeffMach3 on August 06, 2019, 03:14:47 PM
Hi I am putting together a machine that requires a number of outputs,  I have figured out how to turn them on and off using buttons on the screen,  but I cannot turn them on in the program.  I tried learning how to create an M-code in Mach4 Scripting Manual but it is not helpful for someone of my caliber.   If you can help me make one that will work I can copy and make the changes for the additional outputs.    I attached my feeble attempt, but it did not work (See attached)   I noticed that there were M-codes that turned on/off outputs (M200-M228) but that did not seem to work.  Any help would be appreciated.   I am using Mach4 and Pokeys 57E

Jeff
Title: Re: Creating M-Code to activate Outputs
Post by: joeaverage on August 06, 2019, 08:59:00 PM
Hi,
have a look at this thread.....they are sort of related:

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

So the question is whether you want to be able to turn the outputs on or off using m codes which are embedded
in your Gcode program or whether, like the other guy, use a GUI button to turn them on or off.

Note you could do both but would require macros in the macros folder that will be used by Gcode and a basically
indentical function in the screen load script that can be used by the GUI thread. Another way of 'skinning the same cat' is to
have a module.....but start simply and work up to that.

Craig
Title: Re: Creating M-Code to activate Outputs
Post by: JeffMach3 on August 06, 2019, 09:13:14 PM
Hi Craig,
 Thanks for your response.  That is more complex than what I am looking for all I want to do is create an M-code that will turn on/off outputs.  But I am not understanding how to create them. If I could be given an example of one then I could learn from what they did.
Title: Re: Creating M-Code to activate Outputs
Post by: joeaverage on August 06, 2019, 10:00:56 PM
Hi,

Quote
That is more complex than what I am looking for all I want to do is create an M-code that will turn on/off outputs

Then you need to answer the question....do you want to be able to turn off from a button OR do you want to turn them on or
off as part of a Gcode job?

Craig
Title: Re: Creating M-Code to activate Outputs
Post by: JeffMach3 on August 06, 2019, 10:44:29 PM
As part of the Goode
Title: Re: Creating M-Code to activate Outputs
Post by: joeaverage on August 06, 2019, 11:50:25 PM
Hi,
OK thats easy.

Open the script editor and enter:

Code: [Select]
function m101()
local inst=mc.mcGetInstance()
local hsig=mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1)
local current=mc.mcSignalGetState(inst,hsig)
if current ==1 then
      mc.mcSignalSetSate(inst,hsig,0)
else
      mc.mcSignalSetState(inst,hsig,1)
end
end
if (mc.mcInEditor()==1)then
     m101()
end

Save in the macros folder of your profile as m101.mcs

If in your Gcode file you code m101 then output#1 will turn on, code it a second time and it will turn off.
You will have to use your PoKeys plugin to assign a pin to the output.

Note I am at work and cannot run this through the debugger...may do so when I get home.

Craig
Title: Re: Creating M-Code to activate Outputs
Post by: JeffMach3 on August 07, 2019, 12:02:03 AM
Hi Craig, thank you very much.  It looks Greek to me.  So if I copy that and just change OSIG_OUTPUT1 to OSIG_OUTPUT2 and the m101's to m102's I can control 2 outputs and so on?
Title: Re: Creating M-Code to activate Outputs
Post by: joeaverage on August 07, 2019, 12:32:49 AM
Hi,

Quote
So if I copy that and just change OSIG_OUTPUT1 to OSIG_OUTPUT2 and the m101's to m102's I can control 2 outputs and so on?

Yes, up to output#63

Craig
Title: Re: Creating M-Code to activate Outputs
Post by: Chaoticone on August 07, 2019, 01:38:40 AM
Think you could do them all with a single macro and just set the output number using a p var. Something like M201P1, M201P2, etc. Will have to look. I know I did it before.
Title: Re: Creating M-Code to activate Outputs
Post by: joeaverage on August 07, 2019, 05:10:44 AM
Hi,
whoops, made a mistake, the mc.mcSignalSetState and mc.mcSignalGetState don't need instance parameters:

Code: [Select]
function m101()
local inst=mc.mcGetInstance()
local hsig=mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1)
local current=mc.mcSignalGetState(hsig)
if current ==1 then
      mc.mcSignalSetSate(hsig,0)
else
      mc.mcSignalSetState(hsig,1)
end
end
if (mc.mcInEditor()==1)then
     m101()
end

Craig
Title: Re: Creating M-Code to activate Outputs
Post by: Bill_O on August 07, 2019, 08:18:46 AM
this might help

https://www.machsupport.com/forum/index.php?topic=39763.msg266718#msg266718

bill
Title: Re: Creating M-Code to activate Outputs
Post by: JeffMach3 on August 07, 2019, 11:50:27 PM
I tried running it and the M code did not do anything.  I opened it up in the editor and hit (start debugging) and it said C:\Machhobby\ZeroBraneStudio\bin\lua53. exe: error loading module 'machip' from file 'C:\Mach4hobby\Modules/machip. dll' :  the specified module could not be found.
Title: Re: Creating M-Code to activate Outputs
Post by: joeaverage on August 08, 2019, 12:06:37 AM
Hi,
well that a mystery to me, I ran this macro last night through the debugger and MDI and it worked in both instances.

Sounds like some part of the Mach installation is missing.

Craig
Title: Re: Creating M-Code to activate Outputs
Post by: JeffMach3 on August 08, 2019, 12:08:50 AM
Oh fun, do I try re install it
Title: Re: Creating M-Code to activate Outputs
Post by: joeaverage on August 08, 2019, 12:11:52 AM
Hi,
h'mmmm....I might wait until smurph, Chaoticone or Daz read this post....I'd guess they know abit more about it.

What build Mach are you using?

Craig
Title: Re: Creating M-Code to activate Outputs
Post by: JeffMach3 on August 08, 2019, 12:13:36 AM
Version 4.2.0.4162
Build 4162
Title: Re: Creating M-Code to activate Outputs
Post by: joeaverage on August 09, 2019, 05:01:19 PM
Hi,
that's the same build as I'm using.

Have a look in Mach4Hobby/Modules for the file machip.dll  Is it there?

Craig
Title: Re: Creating M-Code to activate Outputs
Post by: joeaverage on August 09, 2019, 05:05:53 PM
Hi,
I notice the file is machipc.dll not machip.dll........just the sort of typo that can catch you out.

Craig
Title: Re: Creating M-Code to activate Outputs
Post by: JeffMach3 on August 10, 2019, 02:29:07 AM
I checked and the file is there.
Title: Re: Creating M-Code to activate Outputs
Post by: Cbyrdtopper on August 10, 2019, 01:51:17 PM
If all you want to do is to turn on and off an output from G Code, you should try using M64 and M65 
Look on page 60 of the Mach4 G Code programming manual for mill and router style machines.   
M64 P2   this will turn on output 2
M65 P2   this will turn off output 2
Title: Re: Creating M-Code to activate Outputs
Post by: JeffMach3 on August 10, 2019, 03:59:21 PM
Awesome... It works Thank you
Title: Re: Creating M-Code to activate Outputs
Post by: Chaoticone on August 10, 2019, 04:17:38 PM
If all you want to do is to turn on and off an output from G Code, you should try using M64 and M65 
Look on page 60 of the Mach4 G Code programming manual for mill and router style machines.   
M64 P2   this will turn on output 2
M65 P2   this will turn off output 2

Yup, that is what I was talking about earlier. Forgot they ended up being rolled into Mcodes though.