Hello Guest it is January 28, 2020, 09:31:45 AM

Author Topic: Two second output signal on M5 command.  (Read 388 times)

0 Members and 1 Guest are viewing this topic.

Two second output signal on M5 command.
« on: May 22, 2019, 06:11:12 AM »
Hi. I never needed to do anything special on Mach4, so i`m pretty dumb outside user interface. But now i need to have output1 to be activated for two seconds on M5 command. I have bare idea how to do that, but i cannot locate a script M5. Do i have to create a new one in profile folder? How big are my chances to ruin everything? I will be happy about any reply, thank you.
Re: Two second output signal on M5 command.
« Reply #1 on: May 22, 2019, 03:07:14 PM »
Hi,
yes it will be necessary to write a new m5 macro and put it in the macros folder of your profile.

When the Gcode interpreter encounters a macro call like m5() it starts searching for it, first in your macros folder.
If it finds it then Mach will execute the code in it. If it does not find it, it searches in higher folders of the search tree
until it finds Machs built in m5().

If you wish to write your own m5() you will need to include all the functionality of the built in m5() plus whatever
functionality you want.

The trick is trying to discover what the 'original' code is and THEN add to it.

This is the code from the m3/m5 button on the screen and is found in the screen load script:
Code: [Select]
[---------------------------------------------------------------
-- Spin CW function.
---------------------------------------------------------------
function SpinCW()
    local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON);
    local sigState = mc.mcSignalGetState(sigh);
   
    if (sigState == 1) then
        mc.mcSpindleSetDirection(inst, 0);
    else
        mc.mcSpindleSetDirection(inst, 1);
    end
end
/code]

The spindle is stopped when this instruction is executed:
mc.mcSpindleSetDirection(inst, 0), pretty simple.

You could make your own m5() by using that instruction plus whatever timer you want.

Craig
My wife left with my best friend...
     and I miss him!
Re: Two second output signal on M5 command.
« Reply #2 on: May 23, 2019, 09:04:49 AM »
Thank you! Helps a lot.
Re: Two second output signal on M5 command.
« Reply #3 on: May 24, 2019, 03:27:08 AM »
Apparently i still have a problem with this. I don`t know, is this recently changed or what, but in the latest version of Mach4, there is no code under spindle cw and ccw buttons. All it is now, SpinCW() and SpinCCW(). When i tried to put a code in there, buttons just don`t work.
Re: Two second output signal on M5 command.
« Reply #4 on: May 24, 2019, 04:00:49 AM »
Hi,

Quote
there is no code under spindle cw and ccw buttons. All it is now, SpinCW() and SpinCCW()

That's correct, and its been that way for years. SpinCW() is a Lua function that turns the spindle on clockwise if its stopped
or stops it if its on. The function is defined and found in the screen load script, I posted a copy of it last post as an example
so you could see how to use it and what instructions Mach uses to do things.

I will repeat it here:

Code: [Select]
-- Spin CW function.
---------------------------------------------------------------
function SpinCW()
    local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON);
    local sigState = mc.mcSignalGetState(sigh);
   
    if (sigState == 1) then
        mc.mcSpindleSetDirection(inst, 0);
    else
        mc.mcSpindleSetDirection(inst, 1);
    end
end

The only instruction you need is this one:

mc.mcSpindleSetDirection(inst, 0).....it sets the spindle direction to zero, ie it stops it.

If you have an m5() macro execute that line the spindle will stop. Thus you simplest m5() macro would be:

Code: [Select]
function m5()
      local inst=mc.mcGetInstance()
      mc.mcSpindleSetDirection(inst,0)                                   --Stops the spindle
      local hsig=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT12)             --assumes your output is output#12
      mc.mcSignalSetState(hsig,1)                                        --Sets the output high
      wx.wxMilliSleep(2000)                                              --Wait for two seconds
      mc.mcSignalSetState(hsig,0)                                        -- Sets the output low         
end

if (mc.mcInEditor()==1)then
      m5()
end

My wife left with my best friend...
     and I miss him!
Re: Two second output signal on M5 command.
« Reply #5 on: May 29, 2019, 04:23:00 PM »
Thank you for kind answers. My last struggle is, can i call M code from the the script? Basically on a machine i`m doing, i need to stop and orientate spindle to 0 position, if the homing switch is hit. So in my case, call M5 and then M111.
Had a dummy crash last Friday, broke one of the toolholders. Thanks god, it is repairable.
Re: Two second output signal on M5 command.
« Reply #6 on: May 30, 2019, 03:56:48 AM »
Hi,

Quote
My last struggle is, can i call M code from the the script?

No, I don't think that is possible.

The reasoning goes like this. The machine is executing a Gcode program so the Gcode interpreter within Machs core is
producing P(osition)V(elocity) over T(ime) data to the motion controller. When the interpreter encounters an M code
it loads the macro code and starts to execute the instruction therein.

If when running the macro it encounters another call to a yet another macro Mach can't do that because its already in a macro
and cannot run another UNTIL the previous one finishes.

Craig
My wife left with my best friend...
     and I miss him!
Re: Two second output signal on M5 command.
« Reply #7 on: November 11, 2019, 01:04:31 PM »
The post is a bit older ...
To my understanding - I would like to have a macro (eg M7) which switches a compressor. I want to do that with a button and an M7 command from the G-code.
Currently I have this with a module (CustFunc.lua in the module folder) >> CustFunc.m7 ()
and call cf.m7 () made via button. In addition, I have in the macro folder a same macro m6 () - that's what I call about the M command z.B from the MDI. It works both. Image button and MDI. Is it also more elegant about just one function?