Hello Guest it is March 28, 2024, 07:55:51 AM

Author Topic: Turn OFF output based on input status OR timed value  (Read 3921 times)

0 Members and 1 Guest are viewing this topic.

Offline dah79

*
  •  35 35
    • View Profile
Turn OFF output based on input status OR timed value
« on: May 16, 2016, 03:59:29 PM »
Have an output that will turn on from a Mcode and can switch it off when an input goes high.  I would like to add a set timed value (30sec) to turn off the output if the input doesn't go high.  Not sure how to do this.  I am using Mach4.

Thanks
Re: Turn OFF output based on input status OR timed value
« Reply #1 on: May 17, 2016, 03:01:44 PM »
Here is the outline of code I use to generate an output pulse based on the cycle time of the PLC scriptrunning at 20Hz. I use this pulse to reset my VFD. You may be able to do something similar.

--Declare global counter in Screen Load script:
count= 0

--Include this where you set your output high
-- Initialise timer to begin count of 600x50ms
 count  = 600 

--Add the following code to PLC Script
if (count > 0) then
    -- count down to 0
    count = count - 1;
end
if count <= 0) then
    --set your output low after 30 seconds
    hSig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUTxx);
    rc = mc.mcSignalSetState(hSig, 0);
end

-- add this where you ordinarily reset your output
count = 0

I'm not claiming that this is optimum, but it works for me.

Allan

Offline dah79

*
  •  35 35
    • View Profile
Re: Turn OFF output based on input status OR timed value
« Reply #2 on: May 17, 2016, 03:16:07 PM »
Thank you for the reply.  I am not sure what you mean by "declare global counter".  This is my first project using Mach and this type of programming.
Re: Turn OFF output based on input status OR timed value
« Reply #3 on: May 18, 2016, 11:04:31 AM »
A variable declared in the screen load script has global scope. This means that it is visible to code in all other scripts and modules of Mach4. Its value persists throughout the Mach4 session.

Don't worry too much about the above detail for now. You will get to know it in time. All you have to do is include a variable declaration similar to the one shown at the start of the screen load script. You will see various similar declarations at the beginning of the script, so just add one of your own to that block. The variable name isn't important: use whatever you like providing it is unique.

Hope this helps.

Allan
Re: Turn OFF output based on input status OR timed value
« Reply #4 on: May 18, 2016, 12:05:35 PM »
Oops! Reading the above, I see I pasted the last if statement incorrectly. The PLC script  should be as follows, not as above, as that would keep setting your output low whenever the count was zero.

if  (count > 0)  then
    -- count down to 0
    count = count - 1;
    if count <= 0)  then
        --set your output low after 30 seconds
        hSig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUTxx);
        rc = mc.mcSignalSetState(hSig, 0);
    end
end

Place this near the end of the PLC script.

The idea is to start counting when you set your output high, then set the output low after 30 seconds, or beforehand via your own code. Once the count is zero, counting stops and the PLC section is skipped by virtue of the opening if statement. Being global, the count variable is seen within the PLC script and any other code you  include, wherever that might be.

Allan

Offline dah79

*
  •  35 35
    • View Profile
Re: Turn OFF output based on input status OR timed value
« Reply #5 on: May 18, 2016, 12:38:07 PM »
Thank you.  I will try that.

Offline DazTheGas

*
  •  778 778
  • DazTheGas
    • View Profile
Re: Turn OFF output based on input status OR timed value
« Reply #6 on: May 19, 2016, 05:27:18 PM »
Sori for the lateness, was going to post this earlier but workload is high at moment but this might be of interest for both of ya.

http://www.machsupport.com/forum/index.php/topic,32573.new.html#new

DazTheGas
New For 2022 - Instagram: dazthegas
Re: Turn OFF output based on input status OR timed value
« Reply #7 on: May 20, 2016, 04:16:26 PM »
Hi Daz

Many thanks for taking the time to produce that informative little video. You certainly have the knack of demystifying this stuff. I haven't delved into the treasures of wx widgets as yet. Too much to learn at my age! I'll try this for my 500ms output pulse. One question though: does the PLC script itself not run via one of these WX timers, or are you implying that the separate timer would be more robust because it runss in its own thread (hope I am using the correct terminology here)?

Allan

Offline DazTheGas

*
  •  778 778
  • DazTheGas
    • View Profile
Re: Turn OFF output based on input status OR timed value
« Reply #8 on: May 22, 2016, 06:05:51 AM »
Never too old - im no spring chicken myself ;-) Yes the PLC is an event running in its own thread.

you are not limited to using the editor my own screen is written in pure  wxlua / wxwidgets no editor used and is fully event driven

heres a small extract from my enable button that toggles the animation on the button depending on the state of OSIG_MACHINE_ENABLED and an event for each animation for when clicked.

Code: [Select]
 
    DTG.m_animCtrl1:Connect( wx.wxEVT_LEFT_UP, function(event)
    mc.mcCntlEnable(inst, 1);
    SetMessage(2, "Main control enabled")
    end )

    DTG.m_animCtrl2:Connect( wx.wxEVT_LEFT_UP, function(event)
    mc.mcCntlEnable(inst, 0);
SetMessage(2, "Main control disabled")
    end )


 CheckEnabled_timer = wx.wxTimer(DTG.cont_enable)
    DTG.cont_enable:Connect(wx.wxEVT_TIMER,
            function (event)
            if mc.mcSignalGetState (mc.mcSignalGetHandle (inst, mc.OSIG_MACHINE_ENABLED)) == 1 then
              DTG.m_animCtrl2:Show();
              else
              DTG.m_animCtrl2:Hide();
            end
    end)

    CheckEnabled_timer:Start(20)

DazTheGas
New For 2022 - Instagram: dazthegas