Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: Cartierusm on February 13, 2017, 03:25:20 AM

Title: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on February 13, 2017, 03:25:20 AM
I've got my PLC and Tool Changer up and running. I have no idea of how to have Mach 4 control the PLC for tool changes.

I'm using a Click PLC, which has Modbus. I also am using a Pokeys 57E if that helps.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Chaoticone on February 13, 2017, 10:00:55 AM
Here is an example of using a ModBus capable PLC with Mach4.

http://www.machsupport.com/forum/index.php/topic,27660.0.html
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on February 16, 2017, 04:21:46 PM
Thanks for the link, but I'm not a programmer by any means so a lot of that is gobbly gook to me.

I'm assuming there are 3 things that need to happen:

1. Mach 4 needs to be programmed for automatic tool changes.
2. Mach 4 needs to be setup to talk to the PLC.
3. The plc needs programming to operate the tool changer.

So I do know how to program PLCs and have already done so. I've never used MODBUS or anything other than internal bits, inputs and outputs. I've already written my PLC program and have it operating my tool changer perfectly. Its uses external inputs, buttons, to change positions.

Here's what I need help with:

1. What area of Mach 4 gets the programming to make automatic tool changes? Not sure how to program it or where the programming goes.
2. I don't know how to setup mach 4 to talk to the PLC.
3. Although I've written the PLC program I don't know how modbus works, do I assign internal bits instead of using external inputs? I'm sure I can figure this part out.

If anyone can help I'd appreciate it. I'd need it to be pretty dumbed down for me to understand.

Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on February 20, 2017, 09:16:15 AM
We just finished getting a tool changer going with the Click PLC, communicating between the Click and Mach4 via TCP Modbus.

Through testing we learned that there is no need to setup Modbus "Send or Receive" functions in the Click PLC.  There is a direct relationship between the Click "Modbus Address" and the "Register#" in the Mach Modbus Configuration.

I will attach a few pictures to help you visualize what's going on.  

In the Click PLC you have a list of the Modbus Addresses.  Those will be used in the Mach4 Modbus Configuration.

In Mach4 Modbus Configuration, Start a new Modbus Connection and then add a new function.  I'll start with reading inputs from the Click.  (Refer to picture)  Start with "Read Coils", Slave Address didn't matter to me so we set it to 1, Modbus Register is what is important.  Referring to the "Modbus Addresses" Picture, look at X301, the Address is 100097, in the Modbus Register put "97" as the starting point, this is a 16 Point Input Block to for # of registers I put 16, the configuration setup will automatically realize that the #97 is the starting point.  I then named them X301-X316 to keep it a simple relationship with the Click IO Points.  

Outputs are done the same way, except the new function will be "Force Multiple Coils" and you will use the full Address Exp. 8272  (I also added a picture of Outputs Configuration)

Registers are what we use to tell the Click PLC which tool to change to by using the "Read Input Register" and "Write Single Register" functions in Mach4 Modbus.  In the setup, the Register# you use will be a direct relationship to the DS# (Data Bit) in the Click.  


Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on February 20, 2017, 09:23:56 AM
To setup the Mach IO Config with modbus is very simple, you will notice that you have a new Device to choose from in the IO config in Mach4. 

We use Y001 in the click as a starting point for the Click PLC.  In the M6 Macro we turn on the Y001 Output on and then off.  We set a C# bit in the Click when Y001 Comes on, this C# Bit is what we use to start our tool change subprogram. 

There is also a lua function to wait for a signal before moving on.   http://www.machsupport.com/forum/index.php/topic,31092.0.html  here is thread that will help with this.  In our M6 macro, we wait for the Orient Release signal from the Click to signal Mach4 to continue its own M6 cycle to finish up the tool change on Mach's end.  And don't forget to Reset your C# bit in the last function of your tool change. 

I can add more examples if you need them later. 

Hope this helps.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on February 20, 2017, 09:31:08 AM
Here is the M6 macro we have so far, this is an example of writing the register that the Click will use for the tool # and an example of the signal wait function.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on February 20, 2017, 12:13:15 PM
I haven't looked it over completely yet cause Mach 4 is now not working at all. Estop condition all the time, but THANKS A BUNCH. That's awesome. I'll tackle this when I solve this other problem.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on February 20, 2017, 07:02:19 PM
Ok I read over all of what you said many time. Still have a few questions.

What is the pic you posted of the Write Single Register, referring to Write Requested Carousel Position, what's that for?

How does Mach talk to the PLC do you have to put the IP address of the PLC somewhere in Mach?

Where do I put the LUA script, what folder and how do I open it to view it?


I wrote my PLC program already with internal bits 'C' to trigger each tool position. Here's some pics.

I don't get the DS registers you're talking about, so mach triggers an output for a tool change "X001" for example that inturn triggers a DS register in Click? So in my program pics you see here I would just add a DS register in front of the X001? Or does the register do something else?

My program works very simple a press of a button, virtual or physical, let's say X001 will run the program for tool change position 1. So I only need 8 inputs (8 outputs from Mach to the PLC). Then (I don't have it programmed in yet) when the tool change is finished Y106 turns on to show the tool change is completed. So I only have one output from the PLC to Mach. How would I set that up?

BTW this helps tremendously.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on February 20, 2017, 10:42:17 PM
Cartierusm,
There is already an M6 Macro started in your Mach4 Profile.  Once Mach4 is opened, go to "Operator" then "Edit/Debug scripts"  it should open up to the Profile Macros.  In here will be the M6 that has already been provided.  I would suggest copying the original to keep a solid reference point.
 
Functions, write single register and read register.  I use these Registers to tell the PLC what tool to change to, I'm using a side mount tool changer that has an arm to remove the tool from the spindle and put the requested tool in the spindle all in one move.   I can give the Click and actual integer from mach4 to do this.  Registers in Mach4 can be very useful.  They were a bit tricky at first to understand how to get the information from a register and how to write to the register (change it in Mach4)
The Mach4 forum has several examples of how to use them.  
But in the Click PLC, you have different types of data bits.  You are using "C" bits these are on or off, 1 or 0.  There also data bits that can hold real values, integers.  These are labeled DS in the Click PLC.  In Mach4 you can have Modbus Registers that can be read by the Click and visa versa, Click registers that can be read by Mach.  
In your case, You could setup a Write Single Register Modbus Function to tell the Click which tool to change to.  In the ladder, you would use the "Compare" Function.  
          DS1 = 1 -----> Set C1
          DS1 = 2 -----> Set C2
          ETC.  
You would use just one DS#.

I truly am no expert at PLCs or Mach4.  I just played around until I figured out what was working well.  This is my first go at a PLC.  There are several ways to do this.  This just worked well for us.  

In the morning I will try and remember to take some screenshots of where the registers are in Mach4.  

Last thing for the night; to setup a Modbus connection.  Go to Configure.  Plugins.  Modbus.  Here you will create a Modbus connection.  Click the first Icon, here you will name your Modbus connection, I named mine Click because I'm talking to a Click.  You will setup what type of Connection it is.  I'm using the Ethernet click so I just had to put in the IP Address of the Click in the second setup screen.  
Hope this helps.  
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on February 21, 2017, 12:17:22 PM
Great thanks. I'll let all this sink in.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on February 22, 2017, 04:46:39 PM
Sorry, I've been busy at work.  Not been able to take screenshots of the machine. 
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on February 22, 2017, 04:58:57 PM
No worries. I'm still trying to digest what you wrote. Plus I don't have access to my garage right now, getting a new driveway.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 22, 2018, 12:04:55 AM
Cbyrdtopper, thanks again for sending me all you did. I'm ready to revisit this issue. I've read through what you posted many times and now have a better handle on what I'm doing. I still have questions, but will play around with it first.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 22, 2018, 01:31:16 AM
Was thinking about this some more. I could use some more clarification. I'll break it down into two parts; setting up Mach for Modbus and the M6 Script. I take it that's all I need to do is those two things? I"ll worry about the tool change script last. I want to focus on the Modbus first.

First off if I'm using X001 as an input into my Click to trigger my tool change program and Mach Modbus directly works with Click as in your Example (i.e. Register 97) then do I have to use the DS#s? I figure when a tool change is called out in the Lua Script for tool changes Mach will send a signal to Click for X001 (register 97), which will run my Click tool change program. Then when Click receives an input for X006, click will send out an output signal to Mach (X006 is a proximity switch when tool changer is locked). Mach reads that input and knows the tool change was completed successfully and continues on with the rest of the tool change.

So first question, when Click receives a physical input, let's say X001, does Mach read that as an Input or Output?

So if all that makes sense then I don't need any outputs from Click because in my click program the inputs trigger the outputs. All Mach needs to do is send a signal to Click for that specific tool change and then wait for X006 input in Click.

So what Functions do I ned in Modbus in Mach Inputs and ?
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on January 23, 2018, 10:18:13 AM
Cartierusm,
I ran into this problem when I was setting up the tool change on our Hurco VMC.  I don't think you can send an input from Mach to the Click; what I did was send an arbitrary output to the Click to start the tool change process.
Example:  
From the M6 Macro, turn on Output# (Y###) for 500ms and turn off
In the Click, when it reads the Y### set a bit (C#) that will latch the tool change sequence on.  When you are finished with the tool change sequence, reset this bit (C#).
The C# Bit will need to be in each rung of your tool change sequence.  If the C# Bit is not on then it will not run your tool change.
This is just how I did it and it works for us, there are more ways to accomplish this goal, I am by no means an expert on ladder logic.      

To answer your first question:  "So first question, when Click receives a physical input, let's say X001, does Mach read that as an Input or Output?"  
Mach will read X001 as an Input.  

"So if all that makes sense then I don't need any outputs from Click because in my click program the inputs trigger the outputs. All Mach needs to do is send a signal to Click for that specific tool change and then wait for X006 input in Click."
Like I first mentioned, You will need to setup Modbus Output to get your Tool Change Sequence started in the Click.  The Click will handle the I/O internally from that point on.

"So what Functions do I ned in Modbus in Mach Inputs and ?"
Use "Read Input Discreates" for Input  and "Write Coil" and/or "Force Multiple Coils" for outputs.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 23, 2018, 01:36:23 PM
Ok I think I'm getting it, maybe, possibly, ok maybe not. Just kidding. I'm starting to comprehend. Thanks for the help.

I don't need any other functions under Modbus in Mach, in your picture originally you have a whole bunch of functions, i.e. Carousel Pos, Write Request Carousel, Click CPU out, etc.., but you're saying I only need Read Input Discretes and Write Coil?



Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on January 23, 2018, 02:37:02 PM
No worries, it takes a little bit to wrap your head around it.
I'm using registers to tell it which tool to go to, and now that I'm thinking about it, I guess you will also need to tell the Click WHICH tool to change to.  Is this correct?
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 23, 2018, 03:13:14 PM
Right now I use a physical button going to click to make tool changes, i.e. X001-X008. That in turn triggers a C# as you mentioned that resets once the tool change is made. I just added in parallel to the X001 a Y101 for Mach to control. This way I can control the tool change manually or with Mach. Manually would only be when switching out tools.

I figure in the M6 I would tell Mach: For T0101 Output Y101 for 500ms, For T0202 Output Y102 for 500ms, etc... Then Mach would need to have specific X### inputs to verify the tool change has been made.

Can you answer what I asked previously, when setting up Modbus do I need all those functions as in your picture or just the read and write as you mention? Thanks.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on January 23, 2018, 03:24:01 PM
Using different Y Outputs from the M6 is a good idea.  Since you are going to do that, you don't need any of the register functions, you just need the Input and Output functions. 
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 23, 2018, 03:27:29 PM
Ok great. Now I need to figure out how to program! I have very little idea of how to program M6. I guess it wouldn't be that difficult, right? If I find an example of a tool change sequence I can just repeat that for the other tools but change the inputs and outputs.

Do you mind posting the script you for the tool change or maybe an example of how to write it? If that's not too much trouble. I'll do my own research later today.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: TOTALLYRC on January 23, 2018, 04:36:12 PM
The nice thing about using a PLC if you like ladder logic is that all you have to do is have Mach send the Tool change signal along with which tool and then the PLC will do all the work. After it is done it will report back to Mach that the tool change is done and them Mach will go back to work.
If you are going to do a lot of LUA coding it doesn't make sense to use a PLC unless you just don't have the I/O.

P.S. I have never actually done this yet but that is what I have read and it makes sense to me.

Mike
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on January 23, 2018, 04:52:51 PM
Mike,
We used to use breakout boards with the ESS, but we have found that the HiCON matched with a PLC is a very stable and reliable solution.  
Our equipment is all for production, so we needed/need a reliable source of I/O.  The ESS breakout board's signal was not very stable for what we want to accomplish.  So a PLC is well worth the money for us, and PLCs from Automation Direct are inexpensive and easy to use and easy to learn. 
The HiCON has plenty of input for Home and Limit switches as well as E Stop and Probing and the relay outputs we use for all the spindle control.    
A PLC connected to Mach4 VIA TCP Modbus is very fast and reliable for the rest of our I/O; and since its a PLC, might as well take advantage of it, use ladder logic to control lube pumps, tool changes, misc functions, etc... that way the PLC Script and Mach4 won't have to take any more memory away from motion control.  

Cartierusm,
Here is our entire M6 macro for the tool changer on our lathe.  You will have to edit it to send the correct output per the tool you desire, as you will see, we use registers to tell the Click which tool to change to.
Since the Bit inside the Click will latch the tool change sequence, we turn on Outpu22 for 1 second and turn it back off.

                     rc = mc.mcSignalWait(inst, mc.ISIG_INPUT0, mc.WAIT_MODE_HIGH, 20)

This SignalWait call is the heart of the M6.  It will force Mach4 to wait until it gets the desired input from the Click to finish the macro.  
Also, the "20" after the comma is the amount of time it will wait until it gets the signal before proceeding.  
So, we timed our Tool Changer, it takes about 6 seconds, occasionally up to 10 seconds depending on how stiff the gears are in the morning, but it never takes 20 seconds!! So if it takes more than that there is clearly an issue.  So we use the rc (forgive me, but I don't remember what RC stands for, return call, return code, whatever) to return a code if it was successful or not.  If not, we alarm out the machine and pop up a message box.

--Big Red Tool Change Macro
function m6()

local inst = mc.mcGetInstance()
local Requested = mc.mcToolGetSelected(inst)
local Current = mc.mcToolGetCurrent(inst)
local ToolChange = mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT22) --This is the signal that will start the click tool change.


   if Requested == Current then
       mc.mcCntlSetLastError(inst, "Current tool == Requested tool so there is nothing to do")
   else
         --Tell the Click to change to requested tool.
         local DS1 = mc.mcRegGetHandle(inst,"Clcik PLC/Requested Tool")
         mc.mcRegSetValue(DS1,Requested)
        
         --Send signal to the Click to start the tool change.
         mc.mcSignalSetState(ToolChange,1) --Y001
         wx.wxMilliSleep(1000)
         mc.mcSignalSetState(ToolChange,0) --Y001
        
         --Wait for the signal from the CLick.        
         mc.mcCntlSetLastError(inst, "Wait for Click to complete tool change.")
         rc = mc.mcSignalWait(inst, mc.ISIG_INPUT0, mc.WAIT_MODE_HIGH, 20)--Time in seconds the Click has to complete the tool change.
         --Error check.  If the Click took too long.  Alarm out Mach4.
         if (rc~= 0) then
             wx.wxMessageBox("The Click did not complete the tool change in time.  Check for malfunction.")
             local Alarm = mc.mcSignalGetHandle(inst,mc.OSIG_ALARM)
             mc.mcSignalSetState(Alarm,1)
             CycleStop()
         else
             mc.mcCntlSetLastError(inst, "Current tool == " .. tostring(Requested))
             mc.mcToolSetCurrent(inst, Requested)
             mc.mcCntlSetLastError(inst, "Tool Change Complete.")
         end

    end--Request == Current

end --m6

if (mc.mcInEditor() == 1) then
 m6()
end
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: TOTALLYRC on January 23, 2018, 04:58:10 PM
Hi Chad,
Nice Macro. If I build my tool changer for the lathe I will give it a try. I already have the click.Great little PLC for the money.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 23, 2018, 05:05:45 PM
Thanks a bunch. I'll give this a look and see if I can figure it out.

I'm no programmer, but I love the Click. I was able to program it pretty easy for all sorts of projects. And yes it's not expensive at all.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 23, 2018, 06:02:24 PM
Ok now we're cooking with gas!

So is this the whole script or do I have to repeat this code 8 times for each of my tool positions, because I don't see where the different tool change positions are referenced?

I assume that the M6 program would have an instance for each of my tools and the corresponding inputs to verify the tool change was made. I'm using Y101 = T0101, Y102=T0202, Y103=T0303, Y104=T0404, Y105=T0505, Y106=T0606, Y107=T0707, Y108=T0808.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on January 23, 2018, 10:51:16 PM
Mike,
Thanks!  I want to grow this forum and help others, so by all means use the code! Hope it works well for you!  I do love the Click, we've been playing with the BRX as well, a little more expensive but I want to try out the software.
Cartierusm,

You can set up an output variable to make your M6 shorter.  
For example:
if requested == 1 then
     local ToolOutput = mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT##) --Y001
end

if requested == 2 then
     local ToolOutput = mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT##) --Y002
end

ETC....

Then when you're ready to start the tool change toggle the "ToolOutput" on then off.  Just like I am doing with Output 22

I'm not sure how you are finishing the tool change in your Click, once it reaches your requested tool input?
If so, I think you can set up the same type of code for the input.
Otherwise you will have to pass each tool through an if statement.
If requested == 1 then
 rc = mc.mcSignalWait(inst, mc.ISIG_INPUT##, mc.WAIT_MODE_HIGH, 20) --X001
end
If requested == 2 then
 rc = mc.mcSignalWait(inst, mc.ISIG_INPUT##, mc.WAIT_MODE_HIGH, 20) --X002
end
ETC

This way it will only look for your Requested Tool Input that the Click will end the tool change sequence with.



Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 24, 2018, 12:16:14 AM
Ok this doesn't seem totally correct to me. What do you think?

I'm using the limit switches on my Tool Changer to verify that the tool change has completed correctly, those inputs run X101-X106.

I basically took your tool change program and changed it to match what I think is correct.

--Mori Seiki SL-1 ATC
function m6()

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

if requested == 1 then
     local ToolOutput = mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT01) –Y101
end

if requested == 2 then
     local ToolOutput = mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT02) –Y102
end

if requested == 3 then
     local ToolOutput = mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT03) –Y103
end

if requested == 4 then
     local ToolOutput = mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT04) –Y104
end

if requested == 5 then
     local ToolOutput = mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT05) –Y105
end

if requested == 6 then
     local ToolOutput = mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT06) –Y106
end

if requested == 7 then
     local ToolOutput = mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT07) –Y107
end

if requested == 8 then
     local ToolOutput = mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT08) –Y108
end

   if Requested == Current then
       mc.mcCntlSetLastError(inst, "Current tool == Requested tool so there is nothing to do")
   else

         --Tell the Click to change to requested tool.
         local Y101 = mc.mcRegGetHandle(inst,"Clcik PLC/Requested Tool")
         mc.mcRegSetValue(Y101,Requested)
end

         --Tell the Click to change to requested tool.
         local Y102 = mc.mcRegGetHandle(inst,"Clcik PLC/Requested Tool")
         mc.mcRegSetValue(Y102,Requested)
end

         --Tell the Click to change to requested tool.
         local Y103 = mc.mcRegGetHandle(inst,"Clcik PLC/Requested Tool")
         mc.mcRegSetValue(Y103,Requested)
end

         --Tell the Click to change to requested tool.
         local Y104 = mc.mcRegGetHandle(inst,"Clcik PLC/Requested Tool")
         mc.mcRegSetValue(Y104,Requested)
end

         --Tell the Click to change to requested tool.
         local Y105 = mc.mcRegGetHandle(inst,"Clcik PLC/Requested Tool")
         mc.mcRegSetValue(Y105,Requested)
end

         --Tell the Click to change to requested tool.
         local Y106 = mc.mcRegGetHandle(inst,"Clcik PLC/Requested Tool")
         mc.mcRegSetValue(Y106,Requested)
end

         --Tell the Click to change to requested tool.
         local Y107 = mc.mcRegGetHandle(inst,"Clcik PLC/Requested Tool")
         mc.mcRegSetValue(Y107,Requested)
end

         --Tell the Click to change to requested tool.
         local Y108 = mc.mcRegGetHandle(inst,"Clcik PLC/Requested Tool")
         mc.mcRegSetValue(Y108,Requested)
end

         --Send signal to the Click to start the tool change.
         mc.mcSignalSetState(ToolChange,1) –Y101
         wx.wxMilliSleep(1000)
         mc.mcSignalSetState(ToolChange,0) –Y101
end

         --Send signal to the Click to start the tool change.
         mc.mcSignalSetState(ToolChange,1) –Y102
         wx.wxMilliSleep(1000)
         mc.mcSignalSetState(ToolChange,0) –Y102
end

         --Send signal to the Click to start the tool change.
         mc.mcSignalSetState(ToolChange,1) –Y103
         wx.wxMilliSleep(1000)
         mc.mcSignalSetState(ToolChange,0) –Y103
end

         --Send signal to the Click to start the tool change.
         mc.mcSignalSetState(ToolChange,1) –Y104
         wx.wxMilliSleep(1000)
         mc.mcSignalSetState(ToolChange,0) –Y104
end

         --Send signal to the Click to start the tool change.
         mc.mcSignalSetState(ToolChange,1) –Y105
         wx.wxMilliSleep(1000)
         mc.mcSignalSetState(ToolChange,0) –Y105
end

         --Send signal to the Click to start the tool change.
         mc.mcSignalSetState(ToolChange,1) –Y106
         wx.wxMilliSleep(1000)
         mc.mcSignalSetState(ToolChange,0) –Y106
end

         --Send signal to the Click to start the tool change.
         mc.mcSignalSetState(ToolChange,1) –Y107
         wx.wxMilliSleep(1000)
         mc.mcSignalSetState(ToolChange,0) –Y107
end

         --Send signal to the Click to start the tool change.
         mc.mcSignalSetState(ToolChange,1) –Y108
         wx.wxMilliSleep(1000)
         mc.mcSignalSetState(ToolChange,0) –Y108
end

         
         --Wait for the signal from the CLick.         
         mc.mcCntlSetLastError(inst, "Wait for Click to complete tool change.")

If requested == 1 then
 rc = mc.mcSignalWait(inst, mc.ISIG_INPUT03, mc.ISIG_INPUT04, mc.ISIG_INPUT05, mc.ISIG_INPUT06, mc.WAIT_MODE_HIGH, 20) --X103, X104, X105, X106
end

If requested == 2 then
 rc = mc.mcSignalWait(inst, mc.ISIG_INPUT01, mc.ISIG_INPUT04, mc.ISIG_INPUT05, mc.ISIG_INPUT06, mc.WAIT_MODE_HIGH, 20) --X101, X104, X105, X106
end

If requested == 3 then
 rc = mc.mcSignalWait(inst, mc.ISIG_INPUT01, mc.ISIG_INPUT02, mc.ISIG_INPUT05, mc.ISIG_INPUT06, mc.WAIT_MODE_HIGH, 20) --X101, X102, X105, X106
end

If requested == 4 then
 rc = mc.mcSignalWait(inst, mc.ISIG_INPUT01, mc.ISIG_INPUT03, mc.ISIG_INPUT05, mc.ISIG_INPUT06, mc.WAIT_MODE_HIGH, 20) --X101, X103, X105, X106
end

If requested == 5 then
 rc = mc.mcSignalWait(inst, mc.ISIG_INPUT02, mc.ISIG_INPUT04, mc.ISIG_INPUT05, mc.ISIG_INPUT06, mc.WAIT_MODE_HIGH, 20) --X102, X104, X105, X106
end

If requested == 6 then
 rc = mc.mcSignalWait(inst, mc.ISIG_INPUT01, mc.ISIG_INPUT03, mc.ISIG_INPUT04, mc.ISIG_INPUT06, mc.WAIT_MODE_HIGH, 20) --X101, X103, X104, X106
end

If requested == 7 then
 rc = mc.mcSignalWait(inst, mc.ISIG_INPUT01, mc.ISIG_INPUT02, mc.ISIG_INPUT04, mc.ISIG_INPUT06, mc.WAIT_MODE_HIGH, 20) --X101, X102, X104, X106
end

If requested == 8 then
 rc = mc.mcSignalWait(inst, mc.ISIG_INPUT02, mc.ISIG_INPUT03, mc.ISIG_INPUT05, mc.ISIG_INPUT06, mc.WAIT_MODE_HIGH, 20) --X102, X103, X105, X106
end

         --Error check.  If the Click took too long.  Alarm out Mach4.
         if (rc~= 0) then
             wx.wxMessageBox("The Click did not complete the tool change in time.  Check for malfunction.")
             local Alarm = mc.mcSignalGetHandle(inst,mc.OSIG_ALARM)
             mc.mcSignalSetState(Alarm,1)
             CycleStop()
         else
             mc.mcCntlSetLastError(inst, "Current tool == " .. tostring(Requested))
             mc.mcToolSetCurrent(inst, Requested)
             mc.mcCntlSetLastError(inst, "Tool Change Complete.")
         end

    end--Request == Current

end --m6

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

Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Chaoticone on January 24, 2018, 12:45:27 AM
I would write the PLC ladder to look at a register to determine position to go to. Then in your M6 write the selected tool to that register.

Then write a 1 to another register to tell the PLC to start a tool change (it already knows what tool to go to because you just told it). This is the "go to work" signal in the PLC. The PLC will watch for the status of this register every scan.

Have your PLC do everything needed to change the tool. Get this working first. I have used a program named Qmodbus to read/write to the PLC for testing. Until you can do this, no reason to have Mach and the PLC talking to one another. You write the tool number to the register, then set the go to work bit to 1. The plc does all of the tool change then sets that go to work bit back to 0.

Once the tool change is complete, the PLC changes the go to work register back to 0 that started the PLC doing the tool change.

Mach will be watching this register and once it comes back as 0 (it can only == 0 after the PLC sets it to 0 because you just set it to 1) the M6 ends and Mach takes back off running your Gcode.

Think I remember reading and writing to C registers in the click for the stuff the PLC and Mach needed to be able to read and write.

Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 24, 2018, 01:23:03 AM
Chaoticone, I don't completely get what you mean, but it did make me realize that on my Click Input X106 is only triggered after a successful tool change.

So I can loose some code. I also added for tool change position.

--Mori Seiki SL-1 ATC
function m6()

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

if requested == 1 then
     local ToolOutput = mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT01) –Y101
mc.mcCntlGcodeExecuteWait (inst, “G90 G53 G00 X0.25 Z0.25”)
end

if requested == 2 then
     local ToolOutput = mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT02) –Y102
mc.mcCntlGcodeExecuteWait (inst, “G90 G53 G00 X0.25 Z0.25”)
end

if requested == 3 then
     local ToolOutput = mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT03) –Y103
mc.mcCntlGcodeExecuteWait (inst, “G90 G53 G00 X0.25 Z0.25”)
end

if requested == 4 then
     local ToolOutput = mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT04) –Y104
mc.mcCntlGcodeExecuteWait (inst, “G90 G53 G00 X0.25 Z0.25”)
end

if requested == 5 then
     local ToolOutput = mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT05) –Y105
mc.mcCntlGcodeExecuteWait (inst, “G90 G53 G00 X0.25 Z0.25”)
end

if requested == 6 then
     local ToolOutput = mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT06) –Y106
mc.mcCntlGcodeExecuteWait (inst, “G90 G53 G00 X0.25 Z0.25”)
end

if requested == 7 then
     local ToolOutput = mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT07) –Y107
mc.mcCntlGcodeExecuteWait (inst, “G90 G53 G00 X0.25 Z0.25”)
end

if requested == 8 then
     local ToolOutput = mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT08) –Y108
mc.mcCntlGcodeExecuteWait (inst, “G90 G53 G00 X0.25 Z0.25”)
end

   if Requested == Current then
       mc.mcCntlSetLastError(inst, "Current tool == Requested tool so there is nothing to do")
   else

         --Tell the Click to change to requested tool.
         local Y101 = mc.mcRegGetHandle(inst,"Clcik PLC/Requested Tool")
         mc.mcRegSetValue(Y101,Requested)
end

         --Tell the Click to change to requested tool.
         local Y102 = mc.mcRegGetHandle(inst,"Clcik PLC/Requested Tool")
         mc.mcRegSetValue(Y102,Requested)
end

         --Tell the Click to change to requested tool.
         local Y103 = mc.mcRegGetHandle(inst,"Clcik PLC/Requested Tool")
         mc.mcRegSetValue(Y103,Requested)
end

         --Tell the Click to change to requested tool.
         local Y104 = mc.mcRegGetHandle(inst,"Clcik PLC/Requested Tool")
         mc.mcRegSetValue(Y104,Requested)
end

         --Tell the Click to change to requested tool.
         local Y105 = mc.mcRegGetHandle(inst,"Clcik PLC/Requested Tool")
         mc.mcRegSetValue(Y105,Requested)
end

         --Tell the Click to change to requested tool.
         local Y106 = mc.mcRegGetHandle(inst,"Clcik PLC/Requested Tool")
         mc.mcRegSetValue(Y106,Requested)
end

         --Tell the Click to change to requested tool.
         local Y107 = mc.mcRegGetHandle(inst,"Clcik PLC/Requested Tool")
         mc.mcRegSetValue(Y107,Requested)
end

         --Tell the Click to change to requested tool.
         local Y108 = mc.mcRegGetHandle(inst,"Clcik PLC/Requested Tool")
         mc.mcRegSetValue(Y108,Requested)
end

         --Send signal to the Click to start the tool change.
         mc.mcSignalSetState(ToolChange,1) –Y101
         wx.wxMilliSleep(1000)
         mc.mcSignalSetState(ToolChange,0) –Y101
end

         --Send signal to the Click to start the tool change.
         mc.mcSignalSetState(ToolChange,1) –Y102
         wx.wxMilliSleep(1000)
         mc.mcSignalSetState(ToolChange,0) –Y102
end

         --Send signal to the Click to start the tool change.
         mc.mcSignalSetState(ToolChange,1) –Y103
         wx.wxMilliSleep(1000)
         mc.mcSignalSetState(ToolChange,0) –Y103
end

         --Send signal to the Click to start the tool change.
         mc.mcSignalSetState(ToolChange,1) –Y104
         wx.wxMilliSleep(1000)
         mc.mcSignalSetState(ToolChange,0) –Y104
end

         --Send signal to the Click to start the tool change.
         mc.mcSignalSetState(ToolChange,1) –Y105
         wx.wxMilliSleep(1000)
         mc.mcSignalSetState(ToolChange,0) –Y105
end

         --Send signal to the Click to start the tool change.
         mc.mcSignalSetState(ToolChange,1) –Y106
         wx.wxMilliSleep(1000)
         mc.mcSignalSetState(ToolChange,0) –Y106
end

         --Send signal to the Click to start the tool change.
         mc.mcSignalSetState(ToolChange,1) –Y107
         wx.wxMilliSleep(1000)
         mc.mcSignalSetState(ToolChange,0) –Y107
end

         --Send signal to the Click to start the tool change.
         mc.mcSignalSetState(ToolChange,1) –Y108
         wx.wxMilliSleep(1000)
         mc.mcSignalSetState(ToolChange,0) –Y108
end

         
         --Wait for the signal from the CLick.         
         mc.mcCntlSetLastError(inst, "Wait for Click to complete tool change.")

 rc = mc.mcSignalWait(inst, mc.ISIG_INPUT06, mc.WAIT_MODE_HIGH, 20) --X106
end

         --Error check.  If the Click took too long.  Alarm out Mach4.
         if (rc~= 0) then
             wx.wxMessageBox("The Click did not complete the tool change in time.  Check for malfunction.")
             local Alarm = mc.mcSignalGetHandle(inst,mc.OSIG_ALARM)
             mc.mcSignalSetState(Alarm,1)
             CycleStop()
         else
             mc.mcCntlSetLastError(inst, "Current tool == " .. tostring(Requested))
             mc.mcToolSetCurrent(inst, Requested)
             mc.mcCntlSetLastError(inst, "Tool Change Complete.")
         end

    end--Request == Current

end --m6

if (mc.mcInEditor() == 1) then
 m6()
end
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Chaoticone on January 24, 2018, 01:48:27 AM
If you do it like I'm suggesting, you can get rid of just about all of that script. It does require the PLC ladder to handle all of the tool change internally though but how it should be IMO.

function m6()
    local current = mc.mcGetCurret(inst)
    local selected = mc.mcGetSelected(inst)

    if (current ~= selected) then --do a tool change
        Set tool number reg in PLC to selected
        Set go to work bit to 1 --Maybe fire a PLC output to a Mach input instead or map that register to an input so can do a wait for signal
        rc = mc.mcSignalWait(signal, state, time)
        if (rc ~= 0) then --we had some kind of error
            mc.mcCntlSetLastError(inst, "Tool Change Failed" )
            mc.mcCntlCycleStop(inst)
        end
    end
end

That's not exactly right but about all the script you would need.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 24, 2018, 02:36:05 AM
I appreciate the help, but as I said I'm no programmer. I understand what you mean and can program my PLC for the most part, but I'm not understanding the code.

How does Mach know what signal to send to my PLC with the identity of the tool that needs changing? Is that the register you're talking about?

Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Chaoticone on January 24, 2018, 09:09:45 AM
Quote
I understand what you mean and can program my PLC for the most part

Good, get that done first like I said. Once you have the tool changer working right and it can change tools when you tell it 2 things....

1) What tool to get through a register
2) Change to that tool (start tool change) through a register

you're virtually finished and you can do all of this before you ever even install Mach4.

Then you will need the PLC to tell you that the tool change is complete (by changing that register that told it to change tools).

Once you have that done getting the m6 to do 3 things......

1) Tell the PLC those 2 things (what tool to get and start tool change).
2) Then wait for conformation that the PLC has finished changing tools.
3) Then update your tool offsets or error out depending on what the confirmation check comes back with.

will be super easy with Mach4. I have given you most of it (though not completely accurate) already. But you get the PLC changing tools as I described using only the PLC and Qmodbus (or similar) someone here will help you get the scripting in your m6 (if I have to do it myself). And do note I said help. Help means you will learn too. But if someone else does it all for you it will take all the fun out of it. Don't want that. Hobbies are supposed to be fun.  :)

Quote
How does Mach know what signal to send to my PLC with the identity of the tool that needs changing?

local current, rc = mc.mcToolGetCurret(inst)
local selected, rc = mc.mcToolGetSelected(inst)

But, like I said, you need not even worry about that yet. Use Qmodbus to write to a register in the PLC telling the PLC what tool to change to.

Quote
but I'm not understanding the code.

You can fix that. Look up those 2 api calls in the Mach4 core API doc. It's in your C:\Mach4 Hobby\Docs directory.

Trust me, doing this in Mach4 will be much easier than you think if you can get the PLC doing its thing. But I wont be helping much with that. Others here probably will but if not I'm sure you can work through it between Click's docs and AutomationDirects forum, support and other resources.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on January 24, 2018, 10:52:03 AM
Take a look at my macro again.  It uses registers to tell the Click which tool is requested.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Chaoticone on January 24, 2018, 05:34:40 PM
Take a look at my macro again.  It uses registers to tell the Click which tool is requested.

Yup, Chads' m6 looks like it does exactly all you will need to do in script. Get the PLC doing its thing and your done!  :)
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 24, 2018, 07:57:52 PM
Thanks both of you! I'm starting to get a handle on the Lua. I don't understand how register's work, but I'm sure I can figure it out. PLC's are pretty easy to program.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 24, 2018, 10:35:30 PM
Ok I'm starting to wrap my head around this. I've re-read this whole thread a bunch of times tonight.

Chad, you said use the compare function in Click DS1 = 1 turn on C1. Got that, that's easy. I'm already using C bits for each tool, C1-C8.

Now, if I tell Mach M6 T0101, I'm guessing it will send a 1 to DS1 and the click will goto tool 1, then a 2 for T0202, etc..?

In your macro you mention Output 22, DS1 and Input 0. Input 0 I take it is the signal going to the click verifying a tool change has completed? and that in turn tells mach that the tool change has completed and to continue with gcode?

Is the DS1 referenced in your macro sending a signal to click or just getting the value and output 22 is actually sending the tool number?

I also don't get what this code is referencing?  --Send signal to the Click to start the tool change.
         mc.mcSignalSetState(ToolChange,1) --Y001
         wx.wxMilliSleep(1000)
         mc.mcSignalSetState(ToolChange,0) --Y001

to me it seems like there are 3 different things in your macro to get the tool changing started.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on January 25, 2018, 09:22:39 AM
Cartierusm,
Now, if I tell Mach M6 T0101, I'm guessing it will send a 1 to DS1 and the click will goto tool 1, then a 2 for T0202, etc..?
Yes.  You will need to set up a write register function in Mach4 Modbus.  Register 1 (from Mach) will be DS1 in the Click.  So, in the M6, you will set Register DS1 to the Requested Tool, Chaoticone has referenced this above.
So T101 --> DS1 = 1   T202 --> DS1 = 2  ETC. 

I also don't get what this code is referencing?  --Send signal to the Click to start the tool change.
         mc.mcSignalSetState(ToolChange,1) --Y001
         wx.wxMilliSleep(1000)
         mc.mcSignalSetState(ToolChange,0) --Y001

I am sending a signal to start my tool change.  In my code, you will see that I made the variable "ToolChange" to be set to Output22, Output22 is Y001 in the Click.  So, with the SignalSetState command, I am turning on Output22 for 1 second and then off.  This starts my tool change.  The signal triggers Y001, which sets a bit in the Click, this bit is the Tool Change Start bit.  If I don't turn off Output22 (ToolChange), the Click will read the signal again and restart the tool change (it shouldn't do anything but it will certainly run the routine again).

The DS1 is a register that is actual sending integers to the Click.  You set it up in the modbus function.  I think it is Write Register.  You can name it whatever you want, I named mine DS1 because it is directly linked to DS1 in the Click.

So, to recap how I start my tool change. 
I Set DS1 register in Mach to the requested tool.
I send a register to the Click (DS1) with the tool number.  DS1 in the Click reads that number so it knows which tool to change to.
I Toggle my "ToolChange" output  which is actually Output22 in Mach4 which is linked to Y001 in the Click.
Then I wait for the Input from the Click to continue G Code.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on January 25, 2018, 09:32:07 AM
In the Click, here is an idea of what the ladder looks like.  
Also, you will need to set up another Memory type (probably DS2) to compare to your DS1.  This DS2 will be your actual tool position on your turret.
X001 -------- MATH  DS2 = 1
X002 -------- MATH  DS2 = 2
ETC

Click LADDER
Y001 ------------ SET C1

C1 ---------------- SUB ROUTINE  TOOL CHANGE
(At this point C1 is set, this is why I turn off Y001)

SUB ROUTINE TOOL CHANGE LADDER
COMPARE  DS1 NE DS2 -------------- Y##  (This will turn on the output that changes your tools)
once it reaches your desired tool it will turn off the output
Once this input is reached
X00# -------------- RESET C1
                       RETURN
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 25, 2018, 02:34:44 PM
Ok I'm clearly missing things. I understand what Chaoticone says about the bits and setting them and resetting them. Got that. Chad, I understand essentially what you're saying. I'm not understanding all the relationships in Click.

I've guessed now that a DS register does not trigger a physical action in Click. That's where I was getting hung up on. I figured that when mach sets a 1, for instance for T0101, to DS1 in click that would automatically start the tool change. But what you're implying is it just tells click this is the next tool to change to, but doesn't actually start the tool change program which rotates the tool changer.

Right now I'm using a physical button for each tool. So for tool 1 I'm using X001 to set C1 > then C1 starts the tool change program > when the requested tool, in this case tool 1, is completed it resets C1 and stops the motion.

My tool changer works perfectly like this.

I don't understand what you wrote in your last post about the ladder program.

I think I need to use DS1 to set the tool, then I'm assuming that the DS1 is set, after the correct tool has been physically changed then the DS1 would reset? In between I'll need to start the actual tool change program?

Yeah, I'm not sure how I would write that. I get that's what you wrote about. I'm just not getting it. Let mull it over maybe it'll come to me.

Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 27, 2018, 09:36:49 PM
Ok this was as they said, pretty easy. You just have to understand what's going on. I don't understand everything, but I got it to work.

A BIG thanks to Chad!!

So I'll try to explain what Chad said so other can figure it out.

First thing: Get your tool changer working with a PLC. I think the click does everything you need, is inexpensive, easy to use and figure out and come with Tech Support from Automation Direct. I used an 8 Position Rotary switch with a regular momentary button to make my tool changer go manually.

After you get your PLC to change tools perfectly you can add to the same line as your manual switch the following code:

X001-------------------C1
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Chaoticone on January 27, 2018, 09:58:07 PM
 :)
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 27, 2018, 10:04:35 PM
Ok it wouldn't let me post, weird. SO here's what I wrote before.


Ok this was as they said, pretty easy. You just have to understand what's going on. I don't understand everything, but I got it to work.

A BIG thanks to Chad!!

So I'll try to explain what Chad said so other can figure it out.

First thing: Get your tool changer working with a PLC. I think the click does everything you need, is inexpensive, easy to use and figure out and come with Tech Support from Automation Direct. I used an 8 Position Rotary switch with a regular momentary button to make my tool changer go manually.

After you get your PLC to change tools perfectly you can add to the same line as your manual switch the following code:

X001---------------------C1
                          |
                          |
DS1 = 1-----Y001

what's going on here is my manual switch triggers an internal bit in the click "C1" that runs my program for tool changes. So I added a DS1 which is a data set, when Mach sends a tool change, Tool 1 for instance, it will send a 1 to the DS1. That's how the Click will know what tool to change to. DS1 just holds a number that's it, the Y001 is the actual signal to make the tool changer move. So in the M6 Macro, the one Chad Posted is perfect other than having to change the variables for how you program your PLC, Mach sends a number for the tool change it's going to make then send an output command, Y001, and that makes the tool change. Then at the end of the tool change in the plc I have it set an output of Y005 to verify the tool change was completed successfully. This output in the PLC also is read by Mach to continue with the rest of macro and thereby continue your GCode.

Now you've got a tool change program working in the PLC and it can take an input to make the tool change to the correct one and an output to verify the tool was changed.

In mach it's really easy to setup Modbus. I used an Ethernet PLC. When I start my Click PLC software a screen pops up to connect to the PLC or open a project. If you connect it will show you all the information you need for the PLC's address, I'm assuming it will have the same info for a PLC that is connected via serial cable. Write that down.

Open Mach 4. Goto Configure > Plugins > Modbus. In mine there were some test modbus' I deleted them. After taking a picture of each in case.

Click the Add a new modbus connection > I named Mine something simple, you should too as it matters later on. I called mine Click PLC, set the connection type to what you're using, for ethernet you'll just put in the IP addy. Leave the other settings the same unless they pertain to your PLC.

Then click on the yellow add modbus function > Read coil > name the function Y005 (or whatever your verification signal is in your click, it has to be an output or input that has a modbus address, you can find if it is in the PLC software) > the address of your input or output you put that in the modbus register (or a Y output use the full address for an X input use just the last 2 numbers, mind you this is for a click PLC) > Register Prefix I put the same Y005. Click next. Then click on the name fields and change them to Y005, Mach adds a 0 for some reason.

Next add a write single register 16 > function name DS1 > modbus register 1 (in a Click PLC a DS1 is a 1 automatically in Mach) > click next and change the names in the fields to DS1 again as Mach adds a 0.

Then add a write coil for Y001 and fill out as with the Y005. Close out of Modbus.

Then open up your PLC software and get it to a state where you can see the functions working in real time. In a click plc software you have to upload the project to a PLC then it will show you in real time the functions.

Then in Mach open up Diagnostic >Click PLC > click on the + sign and go down to DS1 > double click on it and change the value from 0 to 1 > in your PLC software the DS1 value should now show a 1. You can try this with any number and it should change in the PLC software. This is really helpful because it bypasses lots of stuff and shows that mach is correctly communicating with your PLC.

Next go to Mach > config > inputs > use one of the inputs to click and the Y005. Then goto Outputs and use one for Y001. DS1 is a register and will automatically work.

Then just copy Chad's macro and change the values to suit you. Such as:

--Your Tool Changer
function m6()

local inst = mc.mcGetInstance()
local Requested = mc.mcToolGetSelected(inst)
local Current = mc.mcToolGetCurrent(inst)
local ToolChange = mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT01) --Change the output number to the one you used above.


   if Requested == Current then
       mc.mcCntlSetLastError(inst, "Current tool == Requested tool so there is nothing to do")
   else
         --Tell the Click to change to requested tool.
         local DS1 = mc.mcRegGetHandle(inst,"Click PLC/DS1 ") --Type in here the actual address of the PLC - what that means is when you setup Modbus you call it something. I called mine Click PLC, so the address of the DS1 is and what you type here is "Click PLC/DS1"
         mc.mcRegSetValue(DS1,Requested)
         
         --Send signal to the Click to start the tool change.
         mc.mcSignalSetState(ToolChange,1) --Y001, this spot is using the words "toolchange" to trigger outpout 01 above. Basically it's shorthand. You set output 01 above to be called out as toolchange. So you don't really need to change this, but I thought I'd explain.
         wx.wxMilliSleep(1000)
         mc.mcSignalSetState(ToolChange,0) --Y001
         
         --Wait for the signal from the CLick.         
         mc.mcCntlSetLastError(inst, "Wait for Click to complete tool change.")
         rc = mc.mcSignalWait(inst, mc.ISIG_INPUT0, mc.WAIT_MODE_HIGH, 20)--Time in seconds the Click has to complete the tool change. You also have to change the input to the input you setup in Mach Config.
         --Error check.  If the Click took too long.  Alarm out Mach4.
         if (rc~= 0) then
             wx.wxMessageBox("The Click did not complete the tool change in time.  Check for malfunction.")
             local Alarm = mc.mcSignalGetHandle(inst,mc.OSIG_ALARM)
             mc.mcSignalSetState(Alarm,1)
             CycleStop()
         else
             mc.mcCntlSetLastError(inst, "Current tool == " .. tostring(Requested))
             mc.mcToolSetCurrent(inst, Requested)
             mc.mcCntlSetLastError(inst, "Tool Change Complete.")
         end

    end--Request == Current

end --m6

if (mc.mcInEditor() == 1) then
 m6()
end'


Couple of notes Mach can read an output from the click as an input.

Hope that helps someone.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 27, 2018, 10:07:41 PM
Thanks to Chaoticone as well, duh.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 27, 2018, 10:11:31 PM
I still have questions. I don't quite understand the layout of Lua.

I tried adding mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G00 X0.25 Z0.25") to make the tool changer goto this position before changing tools; I added it like this

--Mori Seiki Macro
function m6()

local inst = mc.mcGetInstance()
local Requested = mc.mcToolGetSelected(inst)
local Current = mc.mcToolGetCurrent(inst)
local ToolChange = mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT01) --This is the signal that will start the click tool change.


   if Requested == Current then
       mc.mcCntlSetLastError(inst, "Current tool == Requested tool so there is nothing to do")
   else
         mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G00 X0.25 Z0.25")

         --Tell the Click to change to requested tool.
         local DS1 = mc.mcRegGetHandle(inst,"Clcik PLC/Requested Tool")
         mc.mcRegSetValue(DS1,Requested)

When I added this to the code and ran it, the tool changer moved, but not to the correct position and then gave me an error. When I tried this multiple times it moved to a different position every time. It's weird. I was trying to follow DaZ's tutorial on tool changer.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 27, 2018, 10:16:09 PM
Oh and here's some pics of the finished lathe.

https://www.dropbox.com/s/zibvc1uiy50b6mg/IMG_0607.jpg?dl=0
https://www.dropbox.com/s/wsku0ylgw91hg5y/IMG_0608.jpg?dl=0
https://www.dropbox.com/s/3c7bz0d7n0kneis/IMG_0609.jpg?dl=0
https://www.dropbox.com/s/0jjxe2m6jfsrw4o/IMG_0669.jpg?dl=0
https://www.dropbox.com/s/dy9efgrtym8hd4e/IMG_0671.jpg?dl=0
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Chaoticone on January 28, 2018, 12:04:16 AM
I would put the move in the Gcode just before the M6 line.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 28, 2018, 12:11:40 AM
yeah but when I'm doing manual automatic tool changes I would have to type that in the MDI instead of pressing a button.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Chaoticone on January 28, 2018, 11:35:36 AM
Quote
yeah but when I'm doing manual automatic tool changes I would have to type that in the MDI instead of pressing a button.

I know what your saying but lets think about that for just a minute.

"manual automatic tool changes"

Most machine manufacturers wont have the machine move to any position during a tool change unless it is required for the tool change. When an operator gets ready to do something manually they assume the worst case scenario (it is due to some problem). They want to put as much control into the users hands as they can and rely on the user to know enough about what they are doing to be able to recover. I would really think about it. For example, what would happen if a mill drove a carbide end mill into a chunk of steel spinning way to fast. It might friction weld, essential turning the table and spindle into a solid block now. How does the operator recover if the first thing the manual tool change does is move to a position in Z? This can get ugly fast.

Another thing to think about...... shouldn't you have a Manual tool change page or tab? This would make it so you could manually go through steps in the tool change sequence (including moving to tool change position which is normally done by the gcode)?

Just be careful not to paint yourself into a corner that the only way out is to break something.

But anyway, you can certainly automate it if you want to. This too can get ugly fast. It will require some scripting in the PLC script, a custom module with a co-routine and some edits to the load modules macro in the macros folder. You can look at the default wx4 or wx6 screen sets Ref all axis (Home) button and the function it calls. It uses a coroutine. You will need very similar but in a module so you can call it from a macro.

It might be a lot easier to add a button that simply moves the machine to the position you want or just type it into the MDI before the M6. You might be able to just use the return to position button already in the default screens too. Lots of ways to do it. The question is, do you want exactly what you want bad enough to do what it takes to get it or will some of the out of the box features get you close enough to avoid a lot of extra work, time and effort?
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on January 28, 2018, 07:10:10 PM
Cartierusm,
First off, nice job on the lathe! It looks great and I'm glad you got it working!  

Second, Mach4 lathe doesn't read G90 or G91 like the Mill does.  G90 is implied when using X or Z.  To move incremental you use U for X and W for Z.
Example:     G00 U3.00 W3.00     is the same as       G91 G00 X3.00 Z3.00.  

Third, Although I agree with Chaoticone to move to a tool change position in the code, I can see why you would want to put it in your M6 macro.
Like Chaoticone said, it will be easier to put it in a button but having it embedded in the M6 would be cool.  You will indeed have to make a module to utilize a function in a macro.  Daz has a video on modules that you can watch, its pretty comprehensive and I was able to make a module after watching his video.  

Again.  Great work on the tool changer!  :D
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 28, 2018, 08:22:51 PM
To me a basic function of the tool changer is to have it move to a safe position to make a change. With my lathe it's near Machine Coord Home, G53, I just back it off some X.25 Z.25. I don't want to have to alter Gcode every time I post and what if I forget and all my tools go crashing into a 3000 RPM lathe chuck.

AH!! I was watching DaZ's post and didn't even think about it.

I do have it in a button, but the point of automation is to automate. I don't want to have to do anything for a tool change except execute the tool change.

I'm not sure I understand the module thing? I'll watch the video. Can't I just put in the M6 after Else?

mc.mcCntlGcodeExecuteWait(inst,"G53 G00 X0.25 Z0.25")
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 28, 2018, 08:46:01 PM
DERP!!! AHHHHH Well I feel stupid. I did program a button to make the carriage go to the tool change position and I knew it then, but forgot....My Mach Coord is 0,0, so it would be negative!!! I was trying to go positive! I feel stupid.

I put in the code above but, mc.mcCntlGcodeExecuteWait(inst,"G53 G00 X-0.25 Z-0.25") and it works perfectly.

Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Chaoticone on January 29, 2018, 01:43:27 PM
Quote
and it works perfectly

Does it really? Just curious, do the dros update while moving to the tool change position the M6 told it to go to?
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 29, 2018, 02:03:18 PM
Don't know. I'll check and let you know.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 29, 2018, 06:59:09 PM
Yes the DROs update. Why do you ask, you seemed surprised?
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on January 29, 2018, 07:43:15 PM
Are the DROs showing the move rather than just updating the DRO when it arrives?
Using a mc.mcCntlGcodeExecuteWait can lock up the GUI until the move is finished, then the DROs will populate.
I was curious about this today, I'm guessing that's why Chaticone asked as well. 
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Chaoticone on January 29, 2018, 09:21:49 PM
Yes the DROs update. Why do you ask, you seemed surprised?

Chad explained it pretty well. I do not think the DROs will update as the move is being performed. They will update once the location is reached but I think using the GcodeExecuteWait call from your mCode macro will lock up the gui until that call is finished.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 29, 2018, 09:25:30 PM
Ah, I didn't notice.

Does that make a difference if it's locks up the GUI? I mean I'm running code and have to let it complete first anyway.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on January 29, 2018, 09:41:01 PM
I mean, In my opinion its fine, it just doesn't allow the GUI to do anything else until the move is completed.  No longer than that will take to move the machine to a "Tool Change Position" you should be fine.
But, like I said earlier in this post, Daz put that video up on modules.   Chatoicone, correct me if I'm wrong, but I believe that this will let you put a function() in the macro to handle this without interrupting the GUI.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 29, 2018, 09:53:12 PM
If I'm running Gcode I can't use the gui anyway, right?

I'll look at the modules thing.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Chaoticone on January 29, 2018, 09:53:43 PM
Quote
Chatoicone, correct me if I'm wrong, but I believe that this will let you put a function() in the macro to handle this without interrupting the GUI.

Yup, that's right. Or more of what I was thinking is it would allow you to call a function in a module from the macro. In the module you would have your coroutine and other bits.

Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 30, 2018, 10:04:42 PM
Who's lovin' Modbus??? Me. Thanks a bunch all of you. Lathe is a treat.

Assigning modus and getting it work with buttons in Mach 4 is actually rather easy. Took me all of like 5 minutes. Most of that was cut and paste of the script. Ran my first g-code with multiple tool changes just now. Pretty cool!
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Chaoticone on January 30, 2018, 10:54:58 PM
 :)
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Overloaded on January 31, 2018, 09:05:37 AM
This has been a valuable learning experience, thanks to all who contributed to this post.
Job well done regarding the lathe Cartierusm, VERY nice !  ;)

I have one question for RT Chad and/or Brett. Thanks RT for contributing your M6 btw.
You initiate the PLC sequence by basically setting/resetting the physical output (Y1) with an output of Mach4.
Is there a particular reason for choosing a physical Y (output) at the PLC ?

I realize that that would work, but I typically send the output of one device to the input of the other.
If physical, I'd use an X input.
Being this is over ModBus, would using an internal flag, C bit to initiate the sequence work as well ?
This would free up all of the physical points, Y and X on the Click for other uses.
Just curious. Thanks,
Russ  :)


Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on January 31, 2018, 10:41:53 AM
I'm not sure why Chad used a Y, but I've found that Mach can at least read a Y output on a PLC and X. If you can find an address in your PLC software for an internal bit, I never looked, then I'm sure it can be done. I'm sure it must be as a PLC using Modbus can be pretty robust if using internal commands.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Chaoticone on January 31, 2018, 11:22:57 AM
I can't speak for Chads logic so can't say why he did it like that. I bet he explains it somewhere though. Might be he fires a physical output of the PLC from mach to start the tool change and when tool change is finished it turns that output off, telling Mach the tool change finished. This manual link could act as a pseudo watchdog in a way too...... maybe, just thinking out loud.

Yup, I would have the top section of my ladder setting the state of bits I could get and set through modbus. The bits would mirror the physical IO states. Then I would drive the outputs and read the inputs in Mach over modbus by changing or reading those bits. Hope that makes sense. This enables you to drive the tool changer through physical inputs or inputs over modbus from Mach.

Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Overloaded on January 31, 2018, 12:11:09 PM
Might be he fires a physical output of the PLC from mach to start the tool change and when tool change is finished it turns that output off, telling Mach the tool change finished.  
 


 --Send signal to the Click to start the tool change.
         mc.mcSignalSetState(ToolChange,1) --Y001
         wx.wxMilliSleep(1000)
         mc.mcSignalSetState(ToolChange,0) --Y001

Y1 is only ON for 1 second, just to initiate the PLC cycle. If I read it correctly. Then the Click would pulse a different bit back to M4 indicating completion of the cycle.

I like the second part of your post Brett, all the more reason to free up that "Y".

BTW, sorry ... I meant to name Chad in my inquiry instead of RT. (I have so many heroes, its difficult to keep them in order  :) )

Thanks guys,
Russ
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on January 31, 2018, 12:19:35 PM
Hey guys,
I used Y because at the time I Didn't know I could send over different signals to the click.   I'm curious to see if I can enable a C bit to the Click over mosbus.   That would be my goto choice.   I will test this when I get home from Texas next week. 
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Overloaded on January 31, 2018, 12:25:13 PM
Excellent Chad, thanks for the reply.
I feel certain that it will work.
It works fine with Click -  ModBus between multiple AD products.
Let us know.
Thanks again,
Russ
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Chaoticone on January 31, 2018, 12:55:17 PM
Quote
I like the second part of your post Brett, all the more reason to free up that "Y".

Yup, it does add some complexity to the ladder in the PLC but it does work nice. On some machines its not as big a benefit. But if the tool changer is attached to the gantry and the gantry has a 40 foot travel being able to do something like disable Mach (which puts the tool changer in the PLC in manual mode) and then work that tool changer with the manual tool change buttons attached to the gantry can be worth a lot over the life of a machine. On small machines its still nice to free up IO. Either way, in almost all cases its best to do things as efficient as possible.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on January 31, 2018, 07:40:54 PM
I'm not able to test this.  But just looking at the Click addressing, the C memory bits have a pretty clear address, so I don't see any reason why we won't be able to see changes in bits from the click as well as set them from Mach over modbus. 

The Y001 Output I used was an arbitrary output in the Click, it was totally unused and I could probably have used a memory bit for it(will test next week).  The only thing I lost was 1 of the 64 Mach4 outputs. 

I like the thought of having all the information being sent back and forth from the Click to Mach with bits.  It would make everything synced and truthfully act like a high dollar machine.  And with the way they have Modbus in Mach4, it would be very easy to do, it may take some time mind you, but it wouldn't be difficult. 
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Chaoticone on January 31, 2018, 08:27:04 PM
Yup, then set your stuff in motion in the PLC with the C bits.

If you do it right you can drive the tool changer from 2 sources. Inputs (like push buttons) and modbus (changing bits from Mach). However your right may not require it to be driven from multiple sources so the rest of this may just be noise you have no need to hear....

Input ********* comes on, C bit ********* goes high.

In ladder, if C bit goes high, do your thing, turn that C bit off and turn on the next C bit so it does its next thing. But what happens if that C bit you just turned off is still high because the input is still on? Well, you need to check its state before you do the next thing. But you know if you check in the next rung of the PLC ladder the bit is off because you just turned it off. The ladder will turn the bit back on but not until it gets back up to the top again (which takes about as long as your scan time...... so its real quick) where the input turns that C bit on.

You get around things like this by having a register you write to for stages. Its value is used to check that things are right before doing the next step or stage. Can be a lot to think about but can be worth the effort too.

Another way to do it is have a ladder for each mode (manual/auto). One wont work if the other is on. Modbus ladder is driven by C bits, manual ladder is driven by inputs.

Lots of ways to do it. Just have to play with things until you get it working just like you want it to. When it does that, its right.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on February 01, 2018, 09:27:32 AM
That's an interesting point, using registers to keep track of progress.   I like it.     On the next VMC I'm going yk have the carousel rotate with buttons on the back of the machine as well as Mach.   We did this in a hurco to remove tools and replace tools in the carousel while the machine is running.   Our foreman has found this to be very helpful so we're going to so it again on this next machine.   I'm going to keep registers in mind with this one.   Should be a nice project!   Also, Carter is using Mach and physical buttons on his lathe tool changer.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on February 01, 2018, 07:04:52 PM
Yeah, I originally had the tool changer working with an 8 position rotary switch and push button to activate the tool changes. Then when you people taught me about Modbus I just too the switch function out of my ladder program and now Mach sends those commands to the PLC. SO I can let the gcode change the tool, or type it in MDI or still use the rotary switch, but now the rotary switch now does a tool change in mach so it's changing the tool number in mach and moving it to the correct tool change position.

I'm really liking modbus and PLCs, when you realize how easy it is and how you can impliment PLCs with it, the possibilities are endless.

Almost picked up TWO Fadal 4020 VMCs yesterday!!! Auction, perfect condition, went for about $5K each, one 1992 and one 1998. Rigging and transport kept me from buying them. $4000 from LA to SF. or $3200 for one sooo.... I really need to buy a 15K trailer so I can just rent a truck and pick this stuff up myself. They had some OLD but Working perfectly Kitamura Mycenter 1's, could have got one for $50.00!! But again transport.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Chaoticone on February 01, 2018, 08:29:37 PM
 :)
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on February 01, 2018, 11:31:39 PM
Nice!   We've got a fadal 4020.  Only downfall we ran into was the homing.   They homed inside the servo, so we added home switches.   Other than that, it's a good machine.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on February 02, 2018, 10:32:51 AM
I'm guessing you retro fitted that machine as well? If so why not home inside the servo too? That's what I did on Mori Lathe. My new servos has that option if you want to set it in it's own software. I'm guessing you would have done that if it was possible using the old servos?

I think though my limit switches on the Mori are pretty accurate, but I never bothered to test them because I always though that limit/homing switches weren'' that accurate. I always thought professional machines used an index on a linear scale or servo. I mean some do and some don't, I expect. Plus my machine is from 1978 who knows how worn those are.

The manuf. of the servos said I can enact the pulse index by setting it up in Mach to do it  for me. At the time I didn't know how to maybe I'll explore that now; making a one button startup routine.

Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on February 02, 2018, 01:20:16 PM
I never thought about using the index pulse when I first started doing retrofits.   Home switches have always been adequate for our applications.   
The HiCON can use the index pulse of a servo motor.   I plan on utilizing this function on our current lathe project. I've thought about a 1 button startup as well.   Our Haas has a power up button that changes to tool 1 after it homes the machine.   Should be pretty easy to make this type of function.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cbyrdtopper on February 05, 2018, 03:40:45 PM
Just because I was intrigued by Carter's thought of moving the Turret to a safe position inside the M6 macro, I decided to make a module to do this.  

I've never made a module like this, so it was a nice challenge, but I got it figured out today.  
Thanks to Daz for his Module's video, it was helpful getting it setup.
Once I got it finished, I got rid of all my unnecessary notes and code, took a step back and looked at it.  It is quite straight forward and easy to understand how to use a module.  The hardest part was getting it set up, which was easy enough to do.

local CustomFunction = {}

function CustomFunction.ToolChangePosition()
     local inst = mc.mcGetInstance()
     mc.mcCntlGcodeExecuteWait(inst, "G53 G00 X20.00 Z10.00")
     mc.mcCntlSetLastError(inst, "At tool change position.")
end --ToolChangePosition

return CustomFunction

This is all the code I have in my CustomFunction Module to move to a safe tool change position.  What is nice about this is the ability to add more Custom Functions right into this module.  I had no idea modules were  this powerful.
Title: Re: Help with Mach 4 controlling a PLC for tool changes.
Post by: Cartierusm on February 05, 2018, 05:50:06 PM
Good job.