Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: Kenneth on February 11, 2019, 01:42:17 PM

Title: Buttons and the script/gcode
Post by: Kenneth on February 11, 2019, 01:42:17 PM
Hello all,
i'm new to mach4(fourth day) and looking to make a button that will make Z go to 0.2 and then
X and Y go to 0. that's it. i tried gcode but it doesn't like dat. and i'm even newer to script.
any help or point in direction would be greatly appreciated.
THANKS



Mach4hobby
win 10
64g
3.4g processor
24 ram
Title: Re: Buttons and the script/gcode
Post by: MN300 on February 11, 2019, 03:26:23 PM
You need to use Lua script. In this example the first move is absolute, the second is incremental. The \n starts a new line so the G code blocks are processed separately.

--Spindle Offset
local inst = mc.mcGetInstance()
mc.mcCntlMdiExecute(inst, "G90 G00 Z0.1 \n G91 G00 X1.5 Y.8")
Title: Re: Buttons and the script/gcode
Post by: Kenneth on February 12, 2019, 07:40:53 AM
THANKS MN300, that's actually what i need. now i need to look up Lua.
Thanks again !
Title: Re: Buttons and the script/gcode
Post by: Kenneth on February 12, 2019, 08:27:45 AM
i downloaded LUA and can't even install it. looks like a Linux program.
Linux and i don't get alone :)
Thanks anyway MN300


p.s. the sample script you gave, i was able to work as i wanted. THANKS
Title: Re: Buttons and the script/gcode
Post by: MN300 on February 12, 2019, 08:49:57 AM
The LUA complier is part of Mach4, you don't need to download anything else to make LUA work.
One of the choices in the help menu is Documentation Folder. There, among much other useful info, you will find a list of the Mach4 functions like mc.mcCntlMdiExecute.
Title: Re: Buttons and the script/gcode
Post by: Kenneth on February 12, 2019, 08:51:32 AM
ok Thanks MN300.
need to start digging.
Title: Re: Buttons and the script/gcode
Post by: Kenneth on February 12, 2019, 09:32:00 AM
i read thru most of the documents and looking for something to tell me how to write
script from scratch as i have NO idea what your snippet means except the xyz movements.
for instance, i know that the -- means a comment. the rest of that is martian to me.
i need a list of commands like what does "mc.mcGetInstance() mean or do :)
again thanks for any help i can get !!!!





--Spindle Offset
local inst = mc.mcGetInstance()
mc.mcCntlMdiExecute(inst, "G90 G00 Z0.1 \n G91 G00 X1.5 Y.8")
Title: Re: Buttons and the script/gcode
Post by: MN300 on February 12, 2019, 11:48:07 AM
list of commands (API's)
YourDriveLetter:\Mach4Hobby\Docs\Mach4CoreAPI.chm
Title: Re: Buttons and the script/gcode
Post by: Kenneth on February 12, 2019, 11:55:03 AM
in this script below. i know the first is a comment.
does it need the second line at all to work ?
is the second line a declaration that is not used ?


--Spindle Offset
local inst = mc.mcGetInstance()
mc.mcCntlMdiExecute(inst, "G90 G00 Z0.1 \n G91 G00 X1.5 Y.8")


Thank you MN300.
that gets me started.
Kenneth
Title: Re: Buttons and the script/gcode
Post by: MN300 on February 12, 2019, 01:11:41 PM
In programming, an instance is one occurrence of a class or object. Does that help? Probably not. Scan the links to get an idea of these terms can mean.
https://www.computerhope.com/jargon/i/instance.htm
https://en.wikipedia.org/wiki/Object_(computer_science)

Google "programming instance" to be overwhelmed with similar links.

It's enough to know that you need to determine what the instance of the current environment is and to use it with API calls.
Title: Re: Buttons and the script/gcode
Post by: Kenneth on February 12, 2019, 01:14:37 PM
THANKS again MN300
On my way.
Title: Re: Buttons and the script/gcode
Post by: Kenneth on February 12, 2019, 01:39:23 PM
one more thing please ?
can i get mach4 to reopen to the last start it was in ?
like x0 y0 z.2.   right now it starts all axis at zero.
THANKS
Title: Re: Buttons and the script/gcode
Post by: joeaverage on February 12, 2019, 02:49:57 PM
Hi,
some of those questions are answered here:

https://www.machsupport.com/forum/index.php?topic=39111.msg263977#msg263977 (https://www.machsupport.com/forum/index.php?topic=39111.msg263977#msg263977)

Craig
Title: Re: Buttons and the script/gcode
Post by: joeaverage on February 12, 2019, 03:27:35 PM
Hi Kenneth,
this thread should be in the Mach4 General Discussion board to get maximum exposure.

If I understand you want the machine to move to 0,0,0.1......is that correct?

Do you want a button on Mach's screen OR do you want a macro like m100 that you can MDI?

Quote
can i get mach4 to reopen to the last start it was in ?
like x0 y0 z.2.   right now it starts all axis at zero.

Yes, that can be done, although I suspect your intention has  flawed logic.

If you want the DRO's to read a certain set of numbers on start up, that can be done. What happens though if the machine
has stopped hard to the left, front and top at the end of the last session. Now you start a new session and the DRO's
display 0,0,0.1 when the machine is in its last position? It doesn't make sense.

I think that what you are trying to do, exactly as I did or at least tried years ago, was to devise some means of being able to
turn Mach on and it magically knew where it is and display its position on the DROs. If that is the case you are going to
be disappointed. There are a few workarounds and tricks that can be used to accomplish what you want with fair accuracy,
but those tricks and workarounds are actually harder to understand and implement than using 'homing' or 'referencing'
in the first place.

Can you explain what it you want to achieve and why?

Craig
Title: Re: Buttons and the script/gcode
Post by: Kenneth on February 12, 2019, 04:31:04 PM
i'm sorry, i should have explained better. when i finish a job. i have the axis
going tom Z.2 then X0 Y0. when i finish for the day, i leave it there. i do turn
off the controller and understand it's not a good idea to assume that
the machine is still in that exact position when turned back on. but my job doesn't require the precise
measurement as i make boxes from stock and the the zero of the stock is a little
off from my vice(corner X0 Y0 Z.2) where i start my cut.
      thanks for ALL ya'll help !
Title: Re: Buttons and the script/gcode
Post by: joeaverage on February 12, 2019, 04:35:45 PM
Hi,
yes that is what I thought, many newcomers have tried to do exactly that.

The problem is even if you can cause the DROs to reflect the actual position, that is where you left it,
you cannot programmatically set the machine coordinates.

https://www.machsupport.com/forum/index.php?topic=39309.0 (https://www.machsupport.com/forum/index.php?topic=39309.0)

Craig
Title: Re: Buttons and the script/gcode
Post by: Kenneth on February 12, 2019, 05:17:08 PM
it worked great in mach3. always x0 y0 z.2 where i left it.
why mach4 different? script i guess. but thanks.... i'll have to live with it.
Title: Re: Buttons and the script/gcode
Post by: joeaverage on February 12, 2019, 06:10:27 PM
Hi,
no, you can't set the machine coordinates in Mach3 programmatically either. If all you want is the DROs to read
0,0,0.1 when you start up, that's easy. In Mach4 it requires a script of a few lines, easy.

That does not accommodate the machine coordinates however. The fact that you used this method in Mach3 suggests that you
don't care about machine coordinates and so it would work in Mach4 in exactly the same way.

Woe betide you if you issue a G28 or similar!

The correct way to do it, and if you worked on an industrial machine you would, when you turn it on you would 'reference'
or 'home' it and then MDI g0 x0 y0 z0.1. The machine coordinates would remain referenced for the remainder of your
working day.

If you fit home switches to your machine you could do exactly the same thing.

If you don't have or don't want to fit home switches, then your machine coordinates are all 'over the shop' and if you crash
because of it its your fault.

If you want a script to write the DROs on start up let me know, I will help.

Craig
Title: Re: Buttons and the script/gcode
Post by: Kenneth on February 12, 2019, 06:28:11 PM
THANKS joeaverage, i really don't use machine coordinates as i never reach my limits
or make anything bigger than say ten inches in the X and three inches in the Y. my limits are
X fifty four and Y a little over ten(a knee mill). i just use the readout from the DRO and
do nicely. i really have NO idea where my coordinates really are.(have to make a coor DRO i guess)
   i made a button to make the mill move to X0 Y0 Z.2, but then i'm .4" from my zero  :)
i think i just have to leave my mill all at zero and live with it.
Thanks again !

p.s. i DO have a coordinate DRO in mach3, but never look at it.
i make boxes for PC boards out of aluminum(small boards:)
Title: Re: Buttons and the script/gcode
Post by: joeaverage on February 12, 2019, 07:21:31 PM
Hi,
OK you don't really care about machine coordinates. To be honest I think that is a mistake. I used to have a few
crashes until I fitted Home switches and 'took control' of my machine coordinates and have had only one crash in the four
years since.

Quote
p.s. i DO have a coordinate DRO in mach3, but never look at it.

You have the same in Mach4. See the pic attached.

What you really require is when you turn on Mach (and enable it) that the machine 'Homes in Place' and then
applies a work offset so that your DROs read 0,0,0.2

Here is some code to do it:

Code: [Select]
function m666()
local inst=mc.mcGetInstance()
mc.mcAxisDerefAll(inst)
mc.mcAxisSetHomeInPlace(inst,mc.X_AXIS,1)
mc.mcAxisSetHomeInPlace(inst,mc.Y_AXIS,1)
mc.mcAxisSetHomeInPlace(inst,mc.Z_AXIS,1)
mc.mcAxisHomeAll(inst)
mc.mcAxisSetPos(inst,mc.X_AXIS,0.0)
mc.mcAxisSetPos(inst,mc.Y_AXIS,0.0)
mc.mcAxisSetPos(inst,mc.Z_AXIS,0.2)
end
if (mc.mcInEditor()==1) then
m666()
end

By way of explanation;
mc.mcAxisDeRefAll()   dereferences any axis that happens to be already homed, unlikely at start up but just to be sure....
mc.mcAxisSetHomeInPlace() just is to make sure that your machine can home in place, I know that you don't have home switches
so the machine should therefore home in place by default.....its just the 'fault' part of 'default' that I don't trust!
mc.mcAxisHomeAll() will now cause your machine to be referenced TO ITS CURRENT LOCATION. Thus if you were to look at the
machine coordinates you would see that the position 0.2 above the corner of your vice will be 0,0,0 (machine coordinates)
mc.mcAxisSetPos() sets the DRO and implies a matching work offset.

This code should be in Mach4Hobby/Profiles/<your profile name>/Macros as:
m666.mcs  The reason for the m666 is because its 'devil' code, to be used at your own risk ::)

You could open the Zero Brane editor, paste the code in and then <save as> and browse to your profiles macro directory.

The last thing you need to do is ensure that the macro will run at start up, or more specifically the initial Enable of
Mach after start up. This has been provided for in Configure/Control/General/Initialisation Codes.

Craig
Title: Re: Buttons and the script/gcode
Post by: Kenneth on February 13, 2019, 07:41:50 AM
joeaverage, that is JUST what i was looking for. i really appreciate your help in this.
that's a lot of work for someone you don't know. THANKS. but i will keep an eye on those
coordinates just to be safe. now that i know i have a button for it.
    Again, thanks for your help. it gives me a script that i can edit (for other reasons)
to learn more. i can't learn by reading, mostly by doing :)
Title: Re: Buttons and the script/gcode
Post by: MN300 on February 13, 2019, 09:53:16 AM
It may help if you are aware for the difference between machine coordinates and work coordinates. The DRO display normally does not show the machine coordinates. It shows the work coordinates which are the machine coordinates plus the fixture offsets. The "Machine Coordinates" button toggles between the two. (There are other things that can offset the work coordinates but we will ignore them for now.) The default fixture is G54 and has zero values until you do something to change them. The menu choice "View" -- "Fixture Offsets .." opens a window that lets you view and set the offsets.

The "Zero" <axis> buttons by each DRO line set the offsets to the machine coordinate which makes the work offset read zero. The offset values are stored and returned when the program is restarted.
The mc.mcAxisSetPos(inst,mc.X_AXIS,0.0) lines  in Joeaverage's script also sets the fixture offset. If you do more than one job you could store different fixture offsets and change them with the MDI or even put it in your G code. This would work if you home to the exact same physical location each time.

Setting home consistently would let you use soft limits and protect your machine against crashes.
Title: Re: Buttons and the script/gcode
Post by: Kenneth on February 13, 2019, 09:58:59 AM
Thanks MN300,
i work completely in G54. never use offset or G55.G56....etc.
the only thing that changes is the tool diameter and the length i set when
i set the tool in the spindle(when i rezero Z.
i have a simp0le life but love my job.
THANKS again !