Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: R.Ring on January 21, 2017, 02:08:54 PM

Title: Preload-Tool Macro
Post by: R.Ring on January 21, 2017, 02:08:54 PM
Hello everybody,

I have a question and I have no clue how to deal with that problem... I hope you can help me with that:

Is there a function for looking up the next tool-number in the loaded g-code file?
I am using a PLC to control the automatic tool changer and I need to tell it what tool comes next so it can preload the next tool already.

Do you know a way of doing so?

Have a nice day!
Title: Re: Preload-Tool Macro
Post by: Steve Stallings on January 21, 2017, 02:45:50 PM
Quoted from Mach4CoreAPI.chm which you will find in the C:\Mach4Hobby\Docs folder if
your system is installed in the default folders. You can view this file by simply clicking
on it. It will open a window that behaves like a Windows help file, which it is. While
the quote below has the essential information, it is difficult to read as unformatted
text and I recommend opening it as a Windows help file for clearer presentation.


mcToolGetSelected
C/C++ Syntax:
int mcToolGetSelected(
      MINSTANCE mInst,
      int *toolnum);

LUA Syntax:
toolnum, rc = mc.mcToolGetSelected(
      number mInst)

Description:
Retrieve the selected (next) tool number.

Parameters: Parameter Description
mInst The controller instance.
toolnum The address of an integer to receive the selected tool number.


Returns: Return Code Description
MERROR_NOERROR No Error.
MERROR_INVALID_INSTANCE The mInst parameter was out of range.

Notes:
None.

Usage:

// Get the selected tool number.
MINSTANCE mInst = 0;
int toolnum = 0;
int rc = mcToolGetSelected(mInst, &toolnum);




Title: Re: Preload-Tool Macro
Post by: DazTheGas on January 21, 2017, 05:15:44 PM
Theres also a setting in the mach config under the tools tab that changes between m6 being tool in use or next tool to use.

DazTheGas
Title: Re: Preload-Tool Macro
Post by: R.Ring on February 11, 2017, 01:51:46 PM
That didn't quite help me that much...

My CAM software supports a preload-tool-feature, so in G-Code it prints the next tool number under the M6 command. With "next tool number" I mean the tool which is going to be used after that current toolchange.

Can I somehow "read" the next line after a M6 command? I am sure there is something like that, but I couldn't find it yet...

Title: Re: Preload-Tool Macro
Post by: smurph on February 11, 2017, 08:02:14 PM
N10 T10 (Load first tool)
N20 M6 (change to T10)
N30 T12 (load next tool)
...
N100 M6  (change to T12)

Something like that? 

You can accomplish the preload by making a T macro.  Call it t.mcs and drop it into your profile's macro folder. 

The file's contents:
Code: [Select]
function t(tool)
inst = mc.mcGetInstance('T macro');
local msg = "Tool Changer Selecting tool #" ..  tostring(tool)
mc.mcCntlSetLastError(inst, msg);
        -- You must do something with tool here.  Send to PLC, etc...
end

if (mc.mcInEditor() == 1) then
    t(10); --simulate T10
end

Every time the interpreter executes a block with T in it, this macro will run.

Steve
Title: Re: Preload-Tool Macro
Post by: R.Ring on February 12, 2017, 07:12:11 AM
That's more like it, thanks!
So that problem is solved, now I have to figure out how to transfer the tool number to my PLC.

Is there a way of setting up a serial communication between mach and a slave device?
And where can I find more about that topic?

Title: Re: Preload-Tool Macro
Post by: Steve Stallings on February 12, 2017, 09:48:36 AM
Most people communicate with a PLC using ModBus.

Here is an example of using a ModBus capable PLC with Mach4.

http://www.machsupport.com/forum/index.php/topic,27660.0.html

See the link to a tutorial ZIP file.
Title: Re: Preload-Tool Macro
Post by: R.Ring on February 13, 2017, 11:07:46 AM
That's interesting. I'll try to get that working.

Is there any more information about that topic?
Maybe is there an other way of setting up a serial communication without modbus?

Title: Re: Preload-Tool Macro
Post by: R.Ring on February 24, 2017, 03:08:32 PM
Alright, I did some research about using Modbus and Mach4. That documentation is terrible...

Modbus is industry standard, right? And there is not a single proper tutorial of how to implement that into Mach4. Is there a other way of sending data to a simple PLC? RS232, RS485, Ethernet, IC2, SPI, anything?
Title: Re: Preload-Tool Macro
Post by: smurph on February 24, 2017, 05:58:16 PM
Sending data to a PLC almost ALWAYS uses Modbus.  Modbus is the protocol.  It can be used over any transport method that both the Modbus master and Modbus slave have in common.  RS232 and Ethernet are what Mach 4 supports as a Modbus master.  You can use RS485 or RS422 with the appropriate RS323 converter, if you wish. 

There are lot's of threads about Modbus and even the how-to that Steve referred to.  However, we don't attempt to document industry standards that are already very well documented.  Here is a link that summarizes Modbus quite nicely:  http://www.modbusdriver.com/doc/libmbusmaster/modbus.html (http://www.modbusdriver.com/doc/libmbusmaster/modbus.html)  Once you understand the mechanism that is Modbus, and along with your PLC documentation, you will then have the glue to put the pieces together. 

And you never mentioned what PLC you are using.  We have no idea and can't read your mind so...  :)

Steve
Title: Re: Preload-Tool Macro
Post by: R.Ring on February 25, 2017, 03:37:40 AM
Of course, I understand that. But it seems like nobody has done it before. And those who did, dont really refer to something I can learn from. It's just tough to get into that, especially when you only have time on the weekends.

The PLC I am using is a Controllino Maxi PLC. You can program it for example with standard arduino IDE or others. Here's the link: https://controllino.biz

Title: Re: Preload-Tool Macro
Post by: smurph on February 25, 2017, 04:07:27 PM
No modbus on that thing at all.  I see where someone is working on Modbus integration, but nothing more.  I'm not sure how one would communicate with that PLC at all at the moment.  I can assure you, there will be NO threads about how to hook Mach 4 to that one.  So you are own you own.  Nothing like being the first to try and do something!!  :)

We do have serial comms via LUA.  http://lua-users.org/lists/lua-l/2012-09/msg00554.html (http://lua-users.org/lists/lua-l/2012-09/msg00554.html)  That may be what you have to do.  

For the price (in fact, cheaper), the Automation Direct Click PLCs are comparable and a LOT more powerful.  Stackable.  Need more I/O in the future, no problem.  https://www.automationdirect.com/adc/Shopping/Catalog/Programmable_Controllers/CLICK_Series_PLCs_(Stackable_Micro_Brick)/PLC_Units (https://www.automationdirect.com/adc/Shopping/Catalog/Programmable_Controllers/CLICK_Series_PLCs_(Stackable_Micro_Brick)/PLC_Units)

Steve
Title: Re: Preload-Tool Macro
Post by: TOTALLYRC on February 26, 2017, 06:02:02 AM
+1 on the Click.
I have one just to play with and the software is pretty good.
Looks like lots of people are using them for Mach3 and 4.

Mike
Title: Re: Preload-Tool Macro
Post by: Cbyrdtopper on March 03, 2017, 11:54:52 AM
+1 more for the Click
I'm using the Click for the tool changer on a mill here at the shop, as well as 90% of my I/O.  Easy to use and easy to setup modbus with Mach4.  I'm using the ethernet click for the speed on the I/O.
Title: Re: Preload-Tool Macro
Post by: Cbyrdtopper on March 03, 2017, 05:17:49 PM
Steve, I've been wanting to add the Preload function to our mill for weeks now, today I finally jumped in and did it, it took all of 5 minutes with your "T" macro you posted on this topic.  It works great.
I'm talking to the Click PLC via Modbus Register.  Here is the code I'm using, hopefully someone else can get some use out of it.  =)

--Hurco Pocket Preload
function t(tool)

local inst = mc.mcGetInstance()
local Requested = mc.mcToolGetSelected(inst)
local Current = mc.mcToolGetCurrent(inst)

local tonumber ToolNum = Requested
local Pocket = mc.mcToolGetData (inst, mc.MTOOL_MILL_POCKET, ToolNum)
local DS2 = mc.mcRegGetHandle(inst,"Click/Requested Car Pos")
mc.mcRegSetValue(DS2,Pocket)

mc.mcCntlSetLastError(inst, "Preload Complete.")

end --t

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

Thanks again Steve!
Title: Re: Preload-Tool Macro
Post by: Cbyrdtopper on March 03, 2017, 06:23:10 PM
Videos are fun.  Here is the end of the tool change and the start of the next tool.  Shows the prechanger working great. 

https://youtu.be/8Bj_U1tUhTo

ENJOY!
Title: Re: Preload-Tool Macro
Post by: smurph on March 03, 2017, 06:35:27 PM
Nice!!!  I love it when a plan comes together.  Can you feel the powah?!?!  :)

You gotta love those Hurcos! 

Steve
Title: Re: Preload-Tool Macro
Post by: Cbyrdtopper on March 03, 2017, 07:40:06 PM
Yes!   It's great!  =)  This is an older Hurco, but man it's heavy duty!!