Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: Zvo on May 09, 2019, 02:27:19 PM

Title: A few MCode questions - Mach 4
Post by: Zvo on May 09, 2019, 02:27:19 PM
Hi All,

I'm new to scripting and have a few questions if anyone can help. (I tried to search the forum but didn't find my answer - I'm sure it's here somewhere :-[)

Question1:

I did my simple M script :

function m25()
local inst=mc.mcGetInstance()
local hsig=mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT0)
local bla=mc.mcSignalGetState(hsig)

if (bla == 0) then
mc.mcSignalSetState(hsig, 1)
else
mc.mcSignalSetState(hsig, 0)
end

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

and when I try to run it in MDI, for example:

X10M25

it skips the X goto 10 command and executes only M25 script. Why?

Question 2:

I need to create a loop - something like this:

When I get M25 command
repeat the command in every next line until i give M20 command

GCode example:

X100M25
x90Y100
x80Y80
x70Y50
...
X50Y50M20

Is there a command in lua that will execute the command once ,every line, till something?

Question 3 :

This is more for discussion, can I for example give out an command that every time there is an M25 code - in the toolpath view it draws a small square on the exact coordinates?

Thanks in advance,

Zvo
Title: Re: A few MCode questions - Mach 4
Post by: joeaverage on May 09, 2019, 03:04:57 PM
Hi,
a few things that might help.

First name all your m  code macros m100 or higher. All of Machs built in macros are m99 or less. If you name your macro
in the same namespace you risk corrupting a in built function.

Second all macro names an calls should be lowercase without leading zeros. On a windows PC
M25 and m25
are the same, however Machs Gcode interpreter is looking for 'm25', I suggest using lowercase because you will end up with
impossible to detect errors otherwise. On a Linux PC which is case sensitive the equivalence fails.

Quote
X10M25

There is no 'goto' command contained within that line. I assume you mean g0 or g1?
The linear movement commands are modal thus:
g0 x10
x0
x10
x0
x10
x0

will cause the x axis to move back an forth 10 units at rapid traverse rate established by the modal g0 at the top.

The line that you posted:
X10M25
is relying on Mach already being in g0 (or g1) mode already, but because you have include an m25 command on the same line
the previously prevalent mode is lost.

This would work:
x10
m25

In this circumstance the x10 'goto' command is executed under the existing motion mode and then the second (m25) line is executed.

Look up mcCntlGcodeExecuteWait() and mcCntlGcodeExecute() in the API.chm

Craig
Title: Re: A few MCode questions - Mach 4
Post by: Chaoticone on May 09, 2019, 03:10:46 PM
1) Try your M code on a new line not with a line that is motion.

2) You could use 2 Mcodes to set a register. Say one sets it to 1 and the other sets it to 0.

In the plc script look at it and the current Gcode line number.
if the register == 1 and the current line ~= to last check, do this.

3) Yup. In the M125 have it draw the square. In the top of script you should also check the feed rate and G90/91 mode and set them back to what they were before M125 changes them. CV will not work on the square doing it like this. But, why not just run a sb-program each time you want the square? Why reinvent the wheel? Doing it in a macro seems like going around your elbow to get to your thumb.

Code: [Select]
function m125()
local inst = mc.mcGetInstance()
local rc = mc.mcCntlGcodeExecuteWait(inst, "G91\nG1 F5.00 X1.00\nY1.00\nX-1.00\nY-1.00\nG90")
end

if (mc.mcInEditor() == 1) then
    m125()
end
Title: Re: A few MCode questions - Mach 4
Post by: Zvo on May 09, 2019, 03:48:24 PM
Thanks for the ideas...

1st question - yes the G0 in front of the line solves the problem but... My post processor does not issue the G0 in front of the code (it is going to be a punching machine so no G0 is in front of the line.) and off course M25/M20 are fixed commands and in the same line as movement commands.

Maybe something like this can solve it:

if (mc.mcInEditor()==1) then
mc.mcCntlGcodeExecuteWait(inst,"G0""X"..go to code.."Y"..go to code)
m25()
end

off course ..go to code.. has to be something that makes sence :P

2nd Question:

The script is just an example - the real script should make the movement on every line an then fire the output on 50ms and turn it off and repeat this procedure on every line until it hits M20.

3rd question:


3) Yup. In the M125 have it draw the square. In the top of script you should also check the feed rate and G90/91 mode and set them back to what they were before M125 changes them. CV will not work on the square doing it like this. But, why not just run a sb-program each time you want the square? Why reinvent the wheel? Doing it in a macro seems like going around your elbow to get to your thumb.

Code: [Select]
function m125()
local inst = mc.mcGetInstance()
local rc = mc.mcCntlGcodeExecuteWait(inst, "G91\nG1 F5.00 X1.00\nY1.00\nX-1.00\nY-1.00\nG90")
end

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


I may have explained wrong - I was thinking about the the tool path display on the screen.

Thanks,

Zvo