Hello Guest it is March 28, 2024, 12:43:04 PM

Author Topic: Help with Mach 4 controlling a PLC for tool changes.  (Read 18969 times)

0 Members and 1 Guest are viewing this topic.

Offline Chaoticone

*
  • *
  •  5,624 5,624
  • Precision Chaos
    • View Profile
Re: Help with Mach 4 controlling a PLC for tool changes.
« Reply #40 on: January 27, 2018, 09:58:07 PM »
 :)
;D If you could see the things I have in my head, you would be laughing too. ;D

My guard dog is not what you need to worry about!
Re: Help with Mach 4 controlling a PLC for tool changes.
« Reply #41 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.
Re: Help with Mach 4 controlling a PLC for tool changes.
« Reply #42 on: January 27, 2018, 10:07:41 PM »
Thanks to Chaoticone as well, duh.
Re: Help with Mach 4 controlling a PLC for tool changes.
« Reply #43 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.

Offline Chaoticone

*
  • *
  •  5,624 5,624
  • Precision Chaos
    • View Profile
Re: Help with Mach 4 controlling a PLC for tool changes.
« Reply #45 on: January 28, 2018, 12:04:16 AM »
I would put the move in the Gcode just before the M6 line.
;D If you could see the things I have in my head, you would be laughing too. ;D

My guard dog is not what you need to worry about!
Re: Help with Mach 4 controlling a PLC for tool changes.
« Reply #46 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.

Offline Chaoticone

*
  • *
  •  5,624 5,624
  • Precision Chaos
    • View Profile
Re: Help with Mach 4 controlling a PLC for tool changes.
« Reply #47 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?
;D If you could see the things I have in my head, you would be laughing too. ;D

My guard dog is not what you need to worry about!
Re: Help with Mach 4 controlling a PLC for tool changes.
« Reply #48 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
Chad Byrd
Re: Help with Mach 4 controlling a PLC for tool changes.
« Reply #49 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")