Machsupport Forum

General CNC Chat => Share Your GCode => Topic started by: Evangelosemm on January 24, 2019, 09:38:22 AM

Title: Wait for signal
Post by: Evangelosemm on January 24, 2019, 09:38:22 AM
Hello forum.
Can you please advice me how can i write the g code so when i get a high signal in to an input to stop the g code from running and when the signal gets low the program to continue running automatic.
Thank you
Evangelos
Title: Re: Wait for signal
Post by: joeaverage on January 24, 2019, 11:57:20 AM
Hi,
Gcode as used by mach3 or Mach4Hobby does not include conditional statements. Without a conditional you cannot write
Gcode  to do it. Mach4Industrial enacts Marco B programming which does have conditionals and could do it in Gcode alone.

What you require is a macro running in the background that causes a <feedhold> when low and a <cyclestart> when high.

I use Mach4 and would code this in Lua easily enough. I presume you could do a similar thing in Mach3 but I can't be bothered
with Cypress Enable.

Craig
Title: Re: Wait for signal
Post by: Evangelosemm on January 24, 2019, 01:43:21 PM
Thank you very much for your answer. So I need mach 4 indutrial in order to be able to get signals and manipulate the g code. I am new to all this and I don't know if there is a manual for m conditional Instructions and how to do it.
I ll do a research and if cant do it maybe ask you again. Thank you in deed.
Title: Re: Wait for signal
Post by: TPS on January 24, 2019, 02:03:43 PM
Gcode as used by mach3 or Mach4Hobby does not include conditional statements. with Cypress Enable.
Craig

i am not familar with Mach4 (LUA Scripting),

but i am pretty sure that the is (Mach4Hobby), a posibility to create a macro call M123 or what ever,
witch is waiting for a stupit Input to get "High".
Title: Re: Wait for signal
Post by: Evangelosemm on January 24, 2019, 02:45:27 PM
Is there any manual for M commands at Mach 4? Is it possible an example? A small one from point a to point b with a wait at point b.
Thank you
Title: Re: Wait for signal
Post by: joeaverage on January 25, 2019, 01:34:50 AM
Hi,

Quote
So I need mach 4 indutrial in order to be able to get signals and manipulate the g code
No, absolute rubbish! What I said was that Mach4 Industrial supports Gcode that includes conditional statements.
IF you had conditional statements you could code what you require in Gcode.

With Mach3 OR Mach4Hobby which don't have conditionals that approach wont work. There is however another which will work
equally well.

In Mach4 you would write a script that test the input at regular intervals ( every few milliseconds) and issues a <feedhold>
instruction if the signal is low or a <cycle start> instruction if the signal is high.

This is conceptually easy. Another variation which is more programmatically efficient is to use the SigLIb table (Signal Library).
It takes more thinking about and displays the underlying strength of Mach4.

Irrespective of the approach you adopt, you might choose the easy way first AND THEN have a go at the SigLib idea, you
will have to learn a little Lua and use the API. Are you up for it?

Look in the Mach4Hobby/Docs directory and you will find all sorts of good stuff, the API.chm, Mach4 Scripting Manual and
Mach4 Gcode programming. Note that with Mach enabled the docs directory can be displayed with the <Help Docs> button,
too easy.
Craig
Title: Re: Wait for signal
Post by: Evangelosemm on January 25, 2019, 03:32:42 AM
To write a script for Mach 4 though i can type it in a notepad right? I shouldn't need any lua software to write a simple script of  if - else commands etc.
Also i found somewhere that in Mach 3 within the configuration i can register OEM input pin 1 as 1000 for start cycle and OEM input pin 2 as 1002 for freehold.
Would't this work?
Title: Re: Wait for signal
Post by: joeaverage on January 25, 2019, 12:49:19 PM
Hi,

Quote
To write a script for Mach 4 though i can type it in a notepad right? I shouldn't need any lua software to write a simple script of  if - else commands etc.

Yes you could write a script with a text editor but Zero Brane has been provided for the purpose. It has auto-complete,
context sensitive formatting, a compiler, and can debug, ie run in single step, to breakpoints etc.

You will be coding in Lua and using he API. I have this online manual open on the desktop when coding:
https://www.lua.org/manual/5.2/ (https://www.lua.org/manual/5.2/).

Open the Script Editor (or alternately Edit/Debug scripts). Open a new document.

I suggest that you make a temporary folder within you profile, called 'temp' say, in which to store your script,
'waitscript.mcs' say.

Using SaveAs, navigate to your profile and create a new folder 'temp'.

Open the new folder and save your script as waitscript.mcs. Its not the end of the world if you name it *********x.lua but the
debug features wont work, you'd have to rename it *********xx.mcs
Title: Re: Wait for signal
Post by: joeaverage on January 25, 2019, 12:52:24 PM
Hi, cont...

You should now see your open document in Zero Brane as waitscript.mcs and see the location in the navigation tree.

Craig
Title: Re: Wait for signal
Post by: Evangelosemm on January 25, 2019, 12:55:08 PM
Thank you Craig. I ll follow your advice. I also ordered the lua programming manual today. You very been very helpful. Appreciate it.
Title: Re: Wait for signal
Post by: joeaverage on January 25, 2019, 01:13:05 PM
Hi,
try pasting this code into your script:

Code: [Select]
local inst=mc.mcGetInstance()
local signalHandle=mc.mcSignalGetHandle(inst,mc.ISIG_INPUT4)
local wait= mc.mcSignalGetState(signalHandle)
if wait==0 then
mc.mcCntlFeedHold(inst)
else
mc.mcCntlCycleStart(inst)
end


Note that this code is pretty crude. If your input signal is low AT ANY TIME it will stop whatever job is running
and if it goes high the job, or any job loaded will start whether you wanted it to or not.

Is this want you want?. If so then you can put it into the PLC script and try it. If it is too crude then you need to think about
'under what circumstances do I want the job to pause while waiting for this signal'

Craig
Title: Re: Wait for signal
Post by: Evangelosemm on January 25, 2019, 01:17:18 PM
I want it to wait when it is above one certain coordinate  x... y...z... and check one input only. So if you could add an example of coordinates at your script I ll buy you a beer. Thank you Craig
Title: Re: Wait for signal
Post by: Evangelosemm on January 25, 2019, 01:23:49 PM
I mean this is exactly what I want but only above a specific coordinate to do the checking.
Title: Re: Wait for signal
Post by: joeaverage on January 25, 2019, 01:28:31 PM
Hi,
OK the code I have posted wont do at all, it would run every few milliseconds the entire time Mach is running and
affecting the behavior of the machine all the while.

Quote
I want it to wait when it is above one certain coordinate  x... y...z... and check one input only

So in fact you want this 'wait on signal' to happen only during a specific Gcode job? So any other Gcode job or MDI
commands or jogging would not be affected?

Craig
Title: Re: Wait for signal
Post by: Evangelosemm on January 25, 2019, 01:30:46 PM
No just to check during a g code procedure a signal if it's triggered above a specific coordinate to wait or to go. That's all.
Title: Re: Wait for signal
Post by: Evangelosemm on January 25, 2019, 01:32:58 PM
Leave from point A go to point B return to point A if the signal is on then go to point B or just wait
Title: Re: Wait for signal
Post by: joeaverage on January 25, 2019, 01:35:53 PM
Hi,
so you want this 'wait on signal' to occur when any Gcode job is running and only if it strays into a particular area?

Is the area fixed (relative to the table), ie defined by machine coordinates or is I relative to the job, ie work coordinates?

Craig
Title: Re: Wait for signal
Post by: joeaverage on January 25, 2019, 01:38:58 PM
Hi,
what I'm trying to work out is whether you need to insert a macro at each instance in your Gcode file where you wish this
behavior to occur or whether you need the machine to behave in this way whenever Gcode is running?

Craig
Title: Re: Wait for signal
Post by: Evangelosemm on January 25, 2019, 01:44:29 PM
i want to make something like a pick and place. With work coordinates i leave from point A then i go leave something to place B then return to point A and if a sensor detects the item in part A, i proceed other wise i wait for the signal. So i think always when g code is running for the specific job.....
Title: Re: Wait for signal
Post by: joeaverage on January 25, 2019, 02:07:24 PM
Hi,
OK that makes sense.

How do you generate the Gcode for your pick-and-place machine?
What I am thinking is that after each move which requires a potential wait state that you would code a macro,
something like m199().

g0 x 123.456 y234.567                             --move to position A and be prepared to wait
m199()                                                    --macro that includes the wait code and when the signal is good then returns the machine
g0 x 567.890 y456.789                             --new move to position A and repeat
m199()

For every move which requires this 'wait on signal' behavior then you would have to add an extra line m199().
If you had many hundreds of moves that would be very tedious. Depending on how you generate the Gcode
you may be able to insert it automatically.

can you post a small excerpt of your code?

Craig
Title: Re: Wait for signal
Post by: Evangelosemm on January 25, 2019, 02:21:33 PM
G21 G40 G54
G80 G90 G94
G00 X7.841 Y78.301
G00 Z0
G01 F60 Z-3
G00 Z2
G00 Y73.45
G00 Z0
G01 F60 Z-3
G00 Z2
M47
%

Title: Re: Wait for signal
Post by: Evangelosemm on January 25, 2019, 02:22:36 PM
so ii want for example to wait for signal at this position:
G00 X7.841 Y78.301
Title: Re: Wait for signal
Post by: joeaverage on January 25, 2019, 03:04:12 PM
Hi,
OK that's easy enough. What you need to do is insert a 'wait on signal' macro immediately after each
move that requires it.

That can be done as a macro. So open the script editor and open a new document but save it in your macros folder
of your current profile m199.mcs
This is the code:

Code: [Select]
function m199()
local inst=mc.mcGetInstance()
local rc=mc.mcSignalWait(inst,mc.ISIG_INPUT4,mc.WAIT_MODE_HIGH,10)
if (rc==mc.MERROR_TIMED_OUT) then
mc.mcCntlSetLastError(inst,'Timed Out')
return
end


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

I have used this API (API.chm):
LUA Syntax:
rc = mc.mcSignalWait(
      number mInst,
      number sigId,
      number waitMode,
      number timeoutSecs);

Description:
Wait on a signal to change state.
Parameters: Parameter Description
mInst The controller instance.
sigId A valid signal ID. (NOT a signal handle)
waitMode An integer specifying whether to wait on the signal to go high (WAIT_MODE_HIGH) or low (WAIT_MODE_LOW).
timeoutSecs A double specifying a timeout period.


So in the code I have posted there is a 10 second TimedOut period while it waits for ISIG_INPUT4 to go high.

Craig
Title: Re: Wait for signal
Post by: Evangelosemm on January 25, 2019, 03:42:19 PM
Thank you Craig. You are the best mate and thank you for the time you spent for me. I am going to study a bit about Lua when i recieve the book. Maybe one day i can be EXPERT like you!!!!
Title: Re: Wait for signal
Post by: joeaverage on January 25, 2019, 04:04:48 PM
Hi,
I am by no means an expert, scarely much more than a beginner.

I never bothered with the book, I just have the link open and delve into it at need.

Craig