Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: dandonegan on August 30, 2015, 04:57:42 PM

Title: Looking for help enabling and disabling output with M code
Post by: dandonegan on August 30, 2015, 04:57:42 PM
I was hoping someone might be able to help me with what is probably the very basic question for most of you.

Could someone show me the code that I would use to have M101 turn on output number one?

I would then like to have M102 turn off output number one

Once I have an mcs firelight know how to load it into the proper folder to make it work but I don't know how to generate the code.

Any help would be greatly appreciated.

dan
Title: Re: Looking for help enabling and disabling output with M code
Post by: KingKerf on September 02, 2015, 04:53:01 PM
To create a simple M code that will turn an output on or off, you need to create a new LUA file. Within your Mach4Hobby folder, find the application called mcLuaEditor.exe and run that.
This is where you will begin coding.
Paste the following script in the editor and modify it as needed.

Code: [Select]

--This function will be used to turn on specific outputs.
function m101() --modify to create appropriate function. must follow "M-Code" format

local rc = 0; --return code.
local inst = mc.mcGetInstance(); --retrieve the current instance of MACH

--modify the following variables to match the output you wish to turn on/off.
local signal = mc.OSIG_OUTPUT0;
local message1 = "Output 0 ON";

-- Turns on/off the output.
hsig, rc = mc.mcSignalGetHandle(inst, signal); 
mc.mcSignalSetState(hsig, 1); --use 0 to turn the output off and 1 to turn the output on

--Creates a message in the History
mc.mcCntlSetLastError(inst, message1);

end --This is for the end of the function


next, click File -> Save/SaveAs and navigate to Mach4Hobby/Profiles/My Profile/Macros

name the file the same as the function name (M-Code format). in this case M101
then use your newly created MCode within your G-Code or MDI!

Repeat for any additional outputs turning on or off.
Title: Re: Looking for help enabling and disabling output with M code
Post by: dandonegan on September 03, 2015, 09:03:15 AM
KingKerf

Thank you very much for your help. That is an incredibly well-written and complete explanation. I hope the people over at newfangled included it in their LUA examples section for everyone to use. I understand that everyone is doing somewhat unique stuff with this software but it Would be great if there was a repository of some of the more basic functions most people will be using.

Now I'm wondering if I can up the ante? These signal is going to be used to both cut and notch a part. It would be great to call a single M code that would activate and output, dwell for a specified amount of time and then turn off the output.

Is there anyone that would like to comment on adding that functionality to KingKefs post?

dan

Title: Re: Looking for help enabling and disabling output with M code
Post by: KingKerf on September 03, 2015, 11:57:25 AM
If you are on the most current version, there is a Lua Example file that does exactly what you are looking for.

Mach4Hobby\LuaExamples\Activate Output
Title: Re: Looking for help enabling and disabling output with M code
Post by: dandonegan on September 03, 2015, 02:17:47 PM
This looks great I'm just not sure where to put what value. In the variables section it looks like it's looking for an input To trigger this as opposed to M code. Then it says "local hReg = 0 " is that where I would put the output that I want to turn on?

Could someone modify that example to show where you would put the M code that would activate it and the output that you are triggering?

For example if this was triggered by m120 and turned on output number 5 for 3 seconds.

Thanks for the continued help KingKerf

dan

local inst = mc.mcGetInstance() --Get the instance of Mach4
function m120() --Save the name of the Signal (as stored inside of Mach4)
ActivateSignalTime = 5000 --Time in milliseconds that we want the signal to be active.
local hReg = mc.OSIG_OUTPUT0 --The handle to the signal
-----------------------------

hReg = mc.mcIoGetHandle(inst, IOName)
if hReg == 0 then
   wx.wxMessageBox("Could not locate signal!")
else
   local rc = mc.mcIoGetState(hReg)
   if rc == 1 then --If rc equals 1, then the signal is active.
      wx.wxMessageBox("Signal is already active!")
   else --If rc does not equal 1, then the signal is not active.
      mc.mcIoSetState(hReg, true) --Activate a signal.
      wx.wxMilliSleep(ActivateSignalTime) --Sleep for the commanded time (so that the output stays on as long as we want).
      mc.mcIoSetState(hReg, false) --Set the state of the signal back to false.
   end
end
Title: Re: Looking for help enabling and disabling output with M code
Post by: dude1 on September 03, 2015, 03:22:44 PM
This looks great I'm just not sure where to put what value. In the variables section it looks like it's looking for an input To trigger this as opposed to M code. Then it says "local hReg = 0 " is that where I would put the output that I want to turn on?

Could someone modify that example to show where you would put the M code that would activate it and the output that you are triggering?

For example if this was triggered by m120 and turned on output number 5 for 3 seconds.

Thanks for the continued help KingKerf

dan
function m120() --Save the name of the Signal (as stored inside of Mach4)
local inst = mc.mcGetInstance() --Get the instance of Mach4
ActivateSignalTime = 5000 --Time in milliseconds that we want the signal to be active.
local hReg = mc.OSIG_OUTPUT0 --The handle to the signal  OUTPUT0  = output 0 if you wonted output 5 you would have mc.OSIG_ OUTPUT5
-----------------------------

hReg = mc.mcIoGetHandle(inst, IOName)
if hReg == 0 then
   wx.wxMessageBox("Could not locate signal!")
else
   local rc = mc.mcIoGetState(hReg)
   if rc == 1 then --If rc equals 1, then the signal is active.
      wx.wxMessageBox("Signal is already active!")
   else --If rc does not equal 1, then the signal is not active.
      mc.mcIoSetState(hReg, true) --Activate a signal.
      wx.wxMilliSleep(ActivateSignalTime) --Sleep for the commanded time (so that the output stays on as long as we want).
      mc.mcIoSetState(hReg, false) --Set the state of the signal back to false.
   end
end
Title: Re: Looking for help enabling and disabling output with M code
Post by: dandonegan on September 03, 2015, 05:39:25 PM
I copied and pasted that into the editor. I then saved it as m120.

When I open MachI get this error.

failed to retrieve file times for C;\Mach4Hobby\Profiles\Mach4Mill\Macros\m120.mcc ( error zero; the operation completed successfully,)

This is what it looked like when I pasted it in. Thoughts anyone?

dan

function m120() --Save the name of the Signal (as stored inside of Mach4)
local inst = mc.mcGetInstance() --Get the instance of Mach4
ActivateSignalTime = 5000 --Time in milliseconds that we want the signal to be active.
local hReg = mc.OSIG_OUTPUT0 --The handle to the signal  OUTPUT0  = output 0 if you wonted output 5 you would have mc.OSIG_ OUTPUT5
-----------------------------

hReg = mc.mcIoGetHandle(inst, IOName)
if hReg == 0 then
   wx.wxMessageBox("Could not locate signal!")
else
   local rc = mc.mcIoGetState(hReg)
   if rc == 1 then --If rc equals 1, then the signal is active.
      wx.wxMessageBox("Signal is already active!")
   else --If rc does not equal 1, then the signal is not active.
      mc.mcIoSetState(hReg, true) --Activate a signal.
      wx.wxMilliSleep(ActivateSignalTime) --Sleep for the commanded time (so that the output stays on as long as we want).
      mc.mcIoSetState(hReg, false) --Set the state of the signal back to false.
   end
end
Title: Re: Looking for help enabling and disabling output with M code
Post by: dude1 on September 03, 2015, 06:13:35 PM
have a look in the examples folder, stuff what is pasted to a forum page does not work it changes the symbols

 Activate Output
Title: Re: Looking for help enabling and disabling output with M code
Post by: dandonegan on September 03, 2015, 06:24:10 PM
Still getting the same error message. Am I correct in naming it m120?

dan
Title: Re: Looking for help enabling and disabling output with M code
Post by: dude1 on September 04, 2015, 02:03:53 AM
name needs changed
Title: Re: Looking for help enabling and disabling output with M code
Post by: KingKerf on September 04, 2015, 09:54:54 AM
you are missing an "end" at the end of the file.
A good way to check this stuff is before you save the file, in the Debug menu, click Compile. If there are any major errors with the file, it will display them at the bottom of the window.
Title: Re: Looking for help enabling and disabling output with M code
Post by: dandonegan on September 04, 2015, 10:15:04 AM
Good morning,

I added another end to the end and am no longer getting an error. Thanks for the tip on checking the code. Unfortunately when I run it in either MDI or in a program it  makes the buttons on the screen blank for a second like you are normally running a program but it does not trigger an output. This is what I have now.

function m121() --Save the name of the Signal (as stored inside of Mach4)
local inst = mc.mcGetInstance() --Get the instance of Mach4
ActivateSignalTime = 10000 --Time in milliseconds that we want the signal to be active.
local hReg = mc.OSIG_OUTPUT5 --The handle to the signal
-----------------------------

hReg = mc.mcIoGetHandle(inst, IOName)
if hReg == 0 then
   wx.wxMessageBox("Could not locate signal!")
else
   local rc = mc.mcIoGetState(hReg)
   if rc == 1 then --If rc equals 1, then the signal is active.
      wx.wxMessageBox("Signal is already active!")
   else --If rc does not equal 1, then the signal is not active.
      mc.mcIoSetState(hReg, true) --Activate a signal.
      wx.wxMilliSleep(ActivateSignalTime) --Sleep for the commanded time (so that the output stays on as long as we want).
      mc.mcIoSetState(hReg, false) --Set the state of the signal back to false.
   end
end
end

dan
Title: Re: Looking for help enabling and disabling output with M code
Post by: KingKerf on September 04, 2015, 10:24:56 AM
Is the Output mapped within your Mach Configuration? Are you using an ESS or USS? Is the Output mapped in the ESS config? are all of your wires grounded properly?
Also, check the Diagnostics page to see if the proper Output LED is being lit up.
Title: Re: Looking for help enabling and disabling output with M code
Post by: dandonegan on September 04, 2015, 11:31:30 AM
I am working on this code on a development machine which is just running the simulator. I am only referencing the diagnostic screen regarding outputs when I say that I'm not seeing anything.

The actual machine is running a CSMIO/IP-M from CS labs..

dan