Hello Guest it is March 28, 2024, 10:21:40 PM

Author Topic: Mach4 Macros to enable outputs in an order  (Read 3784 times)

0 Members and 1 Guest are viewing this topic.

Mach4 Macros to enable outputs in an order
« on: August 18, 2015, 11:49:29 AM »
Hey Everyone!
I am new to the forum. Figured after reading and getting so much help from all the topics, that I should share with people what I am working on. For my senior project, me and several other students designed and built a 30 foot laser cutting machine. Absolutely massive. I am using Mach4 to get it going. I wanted to get some feedback on some of the coding that I have stirred up. Let me know if I am wayyyy off or if it looks just about right. The laser has to receive three outputs in the order given below. The M codes will be called out in the post and should be run at every pen down and pen up from the cam program. I am using a Hicon Integra board as well. The board I am setting up today and that is why I have not tested yet. If the code is right, then I am happy I posted it for everyone to share if they need to make something similar happen.

When M101 is called:
1) Turn on gas (output 3)
2) Open shutter (output 2)
3) pause for 0.25 seconds
4) Enable beam (output 1)

Code for M101:

function m101()
    local inst= mc.mcGetInstance();

    local out1= mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1);
    local out2= mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2);
    local out3= mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT3);

    mc.mcSignalSetState(out3, true);
    mc.mcSignalSetState(out2, true);
    wx,wxMicroSleep(250);--250ms delay
    mc.mcSignalSetState(out1, true);
end

When M102 is called:
1) Disable beam (output 1)
2) pause for 0.25 seconds
3) Close Shutter (output 2)
4) Turn gas off (output 3)


Code for M102:

function m102()
    local inst= mc.mcGetInstance();

    local out1= mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1);
    local out2= mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2);
    local out3= mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT3);

    mc.mcSignalSetState(out1, false);
    wx,wxMicroSleep(250);--250ms delay
    mc.mcSignalSetState(out2, false);
    mc.mcSignalSetState(out3, false);
end

Offline simpson36

*
  •  1,369 1,369
    • View Profile
Re: Mach4 Macros to enable outputs in an order
« Reply #1 on: August 22, 2015, 10:19:29 AM »
Since you are a student, I will offer some general guidelines that you might consider in all of your scripting.

What I see that is of concern in your scripting is the use of long delays. This method can cause problems ranging from inconvenient to disastrous.

There are facilities in MACH4 where you can define whether the action should complete or not before the CNC controls continue, but making the CNC wait on your script may solve a timing problem with your script, but cause problems wherever the CNC control's attention has been diverted from.

If you have tasks that require waiting on some action to occur, it is a better practice, in my opinion, to set up a counter and let the program pass thru rather then stop and wait. In this way, the system is not starved for what may be critically important attention.

In practical terms, with MACH4, I would put your script in the PLC and set up a counter  . . .  here is a verbose example:

Instead of

Do something
Wait some amount of time <- this is potentially a problem maker
Do the next thing I want in this script

Do something
Increment a counter
Has the counter reached 1000?
No: go on your merry way and do other important stuff
Yes: Do the next thing I want in this script

Functionally, there two methods result in the same action, however in the second method, the system continues to run and cycle thru all of the other scripts.

.25 seconds is a long time, but even if you have short delays, if they are sequential, they will 'stack' and you might end up with a very long period. By looping thru, you can have literally hundreds of scripts running and not 'pause' the system in a significant way.

There are better ways to scan for state changes, but trading in the delays for counters is the biggest step in the right direction.





« Last Edit: August 22, 2015, 10:21:50 AM by simpson36 »
Re: Mach4 Macros to enable outputs in an order
« Reply #2 on: October 24, 2019, 04:43:30 PM »
Hi I tried switching my Pokey port with registers but it just couldn't lit on ...but the register seems to be changing from 1 and zero ..... I guess my problem is in the understanding signal library I really don't fully comprehend the necessity of having the signal library and on what logic will the code in the library work with micro for example
Re: Mach4 Macros to enable outputs in an order
« Reply #3 on: October 27, 2019, 12:00:26 AM »
...
Do something
Increment a counter
Has the counter reached 1000?
No: go on your merry way and do other important stuff
Yes: Do the next thing I want in this script
...

If you just use an arbitrary counter then you won't know how long the program will actually wait.

Code: [Select]
        os = require "os"
        local currentTime = os.clock()

        repeat
            state = mc.mcSignalGetState(someSensor) ~= 0
        until state or (os.clock() - currentTime > 2)

Offline Chaoticone

*
  • *
  •  5,624 5,624
  • Precision Chaos
    • View Profile
Re: Mach4 Macros to enable outputs in an order
« Reply #4 on: October 30, 2019, 06:56:30 AM »
Might be able to do it all with a single string of Gcode. There are Mcodes to set the state of outputs and Gcode for dwell.
;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!