Machsupport Forum

Mach Discussion => VB and the development of wizards => Topic started by: drumsticksplinter on April 21, 2020, 05:32:54 AM

Title: Turn tool turret macro
Post by: drumsticksplinter on April 21, 2020, 05:32:54 AM
Hello,

I'm working on a tool changer turret for a lathe, but I'm having an issue I can't seem to overcome.

I have PLC controlling all of the logic behind the tool changer and this works absolutely perfectly! I am using a brain to send the value from CurrentTool through modbus. This works if I manually put a value in the Tool No DRO. However, If I run my macro, the Tool No DRO does not change and the PLC does not get the new tool number.

I input in the MDI: M6 T0101 for example

Am I missing something obvious here please?

Thanks,

M6 Start macro:

Code: [Select]
'-----------------------------------------------------------------------
'CYCLONE - Auto tool changer (8 pos turret type)
'V1.0
'-----------------------------------------------------------------------

'-----------------------------------------------------------------------
'CONFIG
'-----------------------------------------------------------------------


Tool = GetSelectedTool()
OldTool = GetCurrentTool()
NewTool = Tool
MaxToolNum = 8 'Max number of tools for the changer

'-----------------------------------------------------------------------
'MACRO START
'-----------------------------------------------------------------------
IsActive(INPUT1)
DeActivateSignal(OUTPUT6) 'Make sure signal to PLC is negative
'code"G91 G28 Z0" 'set incremental, return home via X first
'While IsMoving()
'Wend

While NewTool > MaxToolNum 'Dont allow tool number greater 8
Message("MAX TOOL NUMBER = 8")
Wend

While NewTool < 0 'Dont allow tool number less than 1
Message("INVALID TOOL NUMBER")
Wend

If NewTool = OldTool Then
Call ToolComplete
Else
If IsActive(INPUT1) Then 'ok signal from PLC
Call DoToolChange
Else
Message("TURRET NOT READY!")
End If
End If
End

'----------------------------------------------------------------------
'TOOL CHANGE
'----------------------------------------------------------------------

Sub DoToolChange()

SetCurrentTool(NewTool) 'Update the tool DRO
Call ActivateSignal(OUTPUT6) 'Set tool change allowed signal to PLC
SetTimer(1)
t = GetTimer(1)
sleep 10

If Not IsActive(INPUT1) Then  'If input goes low, turret is moving

While Not IsActive(INPUT1) 'While the turret is moving, count down

If IsActive(INPUT1) Then 'If input goes high again, tool has cycled
Call ToolComplete
Else
If t > 4000 Then
Call ToolFailed
End If
End If

sleep 10
Wend

End If


End Sub
     
 

'----------------------------------------------------------------------
'TOOL COMPLETE
'----------------------------------------------------------------------

Sub ToolComplete()
DeActivateSignal(OUTPUT6)
End Sub


'----------------------------------------------------------------------
'TOOL FAILED
'----------------------------------------------------------------------

Sub ToolFailed()
f = 1
DeActivateSignal(OUTPUT6)
While f = 1
Message("TURRET TIMEOUT")
Wend

End Sub               
Title: Re: Turn tool turret macro
Post by: TPS on April 21, 2020, 05:51:15 AM
you have to tell the System the new Toolnumber by:

    SetCurrentTool( Tool )
Title: Re: Turn tool turret macro
Post by: drumsticksplinter on April 21, 2020, 06:14:16 AM
Hi,

Thanks, yes the macro does this within the DoToolChange() subroutine, but it does not update the Tool No DRO for some reason...


Title: Re: Turn tool turret macro
Post by: Graham Waterworth on April 21, 2020, 06:54:20 AM
To change tool on Mach4 lathe there is no M6 command just call T0203 for tool 2 offset 3 and I think there are different #vars for the tool and the offset number so you could pick that up and send that to the PLC.
Title: Re: Turn tool turret macro
Post by: TPS on April 21, 2020, 07:16:53 AM
if i test here the SetCurrentTool() OEMDro(824) is updated imediatelly
Title: Re: Turn tool turret macro
Post by: drumsticksplinter on April 21, 2020, 07:32:46 AM
Hmm, That is strange...

If I edit the M6Start file to be just: SetCurrentTool(6) for example and type M6 into the MDI and enter, nothing happens... this should just make tool number 6, right?

@Graham Waterworth, Thanks I had considered moving over to mach4 eventually, but at the time I started the retrofit a fee years ago, CsLabs who make my controller hadn't got a stable plugin for mach4, I'm not sure if this is still the case now?

Title: Re: Turn tool turret macro
Post by: Graham Waterworth on April 21, 2020, 07:37:52 AM
Mach3 lathe is the same T0203
Title: Re: Turn tool turret macro
Post by: drumsticksplinter on April 21, 2020, 07:43:59 AM
Yeah, that does nothing either, I have tried this, maybe my version of mach3 isn't working properly?
Title: Re: Turn tool turret macro
Post by: Graham Waterworth on April 21, 2020, 07:50:23 AM
In Mach3 macros you need to give it time to update DRO's so when you set the SetCurrentTool() or any other DRO you need to do a short Sleep() of say 50 to 100 to allow Mach3 to update in the background.
Title: Re: Turn tool turret macro
Post by: TPS on April 21, 2020, 08:02:47 AM
put a MSGBox("M6 call") at the beginn of M6 macro just to see that it getting called.
Title: Re: Turn tool turret macro
Post by: TPS on April 21, 2020, 08:05:19 AM
what have you selected in Config -> General Config -> Toolchange?
Title: Re: Turn tool turret macro
Post by: drumsticksplinter on April 21, 2020, 11:34:19 AM
Thanks both, I will try those pointers shortly as I think my motion controller plugin is causing other issues when its not connected and i'm still trying to run mach. I will update on my findings  ;)
Title: Re: Turn tool turret macro
Post by: drumsticksplinter on April 22, 2020, 09:47:20 AM
Ok, an update  ;D

So I have added in some small sleep commands into my code to allow the dro to update, also added in the msgbox to see if M6 was actually being called. Long story short, I have ended up with the turret working correctly and the M6Start macro doing what it should.

One small shameful confession, the tool change was set to ignore in the general config screen, which had made life very difficult. However, this must have happened during a crash (as mach3 frequently does on my laptop) because this setting was set to auto tool changer prior to my first post in this thread. Stoopid mistake I know... but I never thought to check this as I'd already set it to atc!! Oh well, its all working great now  :D :D

Thanks all for your assistance
Title: Turn tool turret macro
Post by: drumsticksplinter on April 23, 2020, 05:17:03 AM
I spoke too soon!

I do have some problems with my code in the M6Start macro.....

I think the problem is with the while loop in the DoToolChange() sub.

What the code is supposed to do is:
- Check the tool number is valid, if its allowed proceed to DoToolChange()
-The tool number is sent to a PLC via modbus, that is giving a tool change allowed signal (INPUT1), but is waiting for the command to change the tool from mach (OUTPUT6).
-Once (OUTPUT6) goes high, the PLC sends (INPUT1) low while it cycles the tool. Once it has successfully located and locked the tool, it will set (INPUT1) high again. If it doesn't, something has gone wrong.
-The While loop is supposed to look at when (INPUT1) is low and wait for it to go high again, before it calls ToolComplete(), it should also start timing 4 seconds, by which time if it hasn't seen the signal it will call ToolFailed().

The while loop is doing something strange and won't go to either ToolComplete() or ToolFailed(), I think its something to do with the timer, but cant quite figure it out.

If anyone wouldn't mind looking over my code to see if anything jumps out please?

Thanks,

Code: [Select]
'-----------------------------------------------------------------------
'CYCLONE - Auto tool changer (8 pos turret type)
'V1.0
'-----------------------------------------------------------------------

'-----------------------------------------------------------------------
'CONFIG
'-----------------------------------------------------------------------


NewTool = GetSelectedTool() 'Commanded tool number
OldTool = GetCurrentTool() 'Current tool number
MaxToolNum = 8 'Max number of tools for the changer
PreviousSpeed = GetRPM() '
ToolSpeed = 500

'-----------------------------------------------------------------------
'MACRO START
'-----------------------------------------------------------------------

DeActivateSignal(OUTPUT6) 'Make sure signal to PLC is negative
code"G91 G28 Z0" 'set incremental, return home via X first
SetSpinSpeed(ToolSpeed) 'Spindle speed change
While IsMoving()
Wend

While NewTool > MaxToolNum 'Dont allow tool number greater 8
Message("MAX TOOL NUMBER = 8")
Wend

While NewTool < 0 'Dont allow tool number less than 1
Message("INVALID TOOL NUMBER")
Wend

If NewTool = OldTool Then
Call ToolComplete
Else
If IsActive(INPUT1) Then 'ok signal from PLC
Call DoToolChange
Else
Message("TURRET NOT READY!")
End If
End If
End

'----------------------------------------------------------------------
'TOOL CHANGE
'----------------------------------------------------------------------

Sub DoToolChange()

SetCurrentTool(NewTool) 'Update the tool DRO
Call ActivateSignal(OUTPUT6) 'Set tool change allowed signal to PLC
SetTimer(1)

sleep 200
t = GetTimer(1)


While IsActive(INPUT1) = False 'While the turret is moving, count down
If IsActive(INPUT1) = True Then 'If input goes high again, tool has cycled
Call ToolComplete
'Else
If t > 4000 Then
Call ToolFailed
End If
End If

sleep 100
Wend

'End If


End Sub
     
 

'----------------------------------------------------------------------
'TOOL COMPLETE
'----------------------------------------------------------------------

Sub ToolComplete()
DeActivateSignal(OUTPUT6)
SetSpinSpeed(PreviousSpeed) 'RPM before tool change called
sleep 100
End Sub


'----------------------------------------------------------------------
'TOOL FAILED
'----------------------------------------------------------------------

Sub ToolFailed()
f = 1
DeActivateSignal(OUTPUT6)
While f = 1
Message("TURRET TIMEOUT")
Wend

End Sub                         
Title: Re: Turn tool turret macro
Post by: TPS on April 23, 2020, 05:46:07 AM
i think that is what you try to do

Code: [Select]

'Clear timer 1
SetTimer(1)



While IsActive(INPUT1) = False 'While the turret is moving, count down
t = GetTimer(1)

If t > 4000 Then
Call ToolFailed
End If
Sleep 100
Wend

Call ToolComplete



Title: Re: Turn tool turret macro
Post by: drumsticksplinter on April 23, 2020, 08:25:16 AM
Ahh..., Ok I'll try this asap!
Title: Re: Turn tool turret macro
Post by: drumsticksplinter on April 24, 2020, 04:35:02 AM
Had a chance to try this, this morning.

The While loop now works and unless it sees (INPUT1) it will not exit the loop.

The timer however does not work. I have tested the timer by sending a message when ToolComplete() is called and it always returns zero.

I've changed the timer number and also changed where SetTimer() is in the macro, but it only ever returns zero, is there a way it needs to be started?

Thanks

Code: [Select]
'----------------------------------------------------------------------
'TOOL CHANGE
'----------------------------------------------------------------------

Sub DoToolChange()

SetCurrentTool(NewTool) 'Update the tool DRO
'Call ActivateSignal(OUTPUT6) 'Set tool change allowed signal to PLC
SetTimer(20) 'Clear timer 1
sleep 500

While IsActive(INPUT1) = False 'While the turret is moving, count down

t = GetTimer(20)
If t > 4000 Then
Call ToolFailed
End If
Sleep 100
Wend

Call ToolComplete


End Sub
     
 

'----------------------------------------------------------------------
'TOOL COMPLETE
'----------------------------------------------------------------------

Sub ToolComplete()
DeActivateSignal(OUTPUT6)
SetSpinSpeed(PreviousSpeed) 'RPM before tool change called
Message("Tool Change Complete in " & GetTimer(20) & " seconds")
sleep 100
End Sub
Title: Re: Turn tool turret macro
Post by: TPS on April 24, 2020, 06:21:30 AM
did some tests Looks like the Timer Resolution is 10 seconds this code should work better

Code: [Select]
'----------------------------------------------------------------------
'TOOL CHANGE
'----------------------------------------------------------------------

Sub DoToolChange()

SetCurrentTool(NewTool) 'Update the tool DRO
'Call ActivateSignal(OUTPUT6) 'Set tool change allowed signal to PLC
SetTimer(20) 'Clear timer 1
sleep 500

While NOT IsActive(INPUT1)  'While the turret is moving, count down

t = GetTimer(20)
If t > 0.4 Then
Call ToolFailed
End If
Sleep 100
Wend

Call ToolComplete


End Sub
     
 

'----------------------------------------------------------------------
'TOOL COMPLETE
'----------------------------------------------------------------------

Sub ToolComplete()
DeActivateSignal(OUTPUT6)
SetSpinSpeed(PreviousSpeed) 'RPM before tool change called
Message("Tool Change Complete in " & GetTimer(20) & " seconds")
sleep 100
End Sub

Title: Re: Turn tool turret macro
Post by: drumsticksplinter on April 24, 2020, 08:58:19 AM
Thanks for testing this, Will give it a try later  ;)
Title: Re: Turn tool turret macro
Post by: drumsticksplinter on April 24, 2020, 09:52:55 AM
GetTimer() still returns zero...

Code: [Select]
SetTimer(1)

sleep 1000

MsgBox(GetTimer(1))

This should say 1000ms right? or 0.1 if the timer resolution is 10 seconds? I just get 0
Title: Re: Turn tool turret macro
Post by: TPS on April 24, 2020, 01:00:37 PM
if i run your testcode here in VB Scripter window, i get a result, see attachment
Title: Re: Turn tool turret macro
Post by: TPS on April 25, 2020, 02:53:10 AM
ok if the timer function is not working, you can try to use the time function.

example:
Code: [Select]
Dim TS As Single
Dim TE As Single
Dim TT As Single

'get the starttime
TS = Timer

Sleep 4000

'get the actual time
TE = Timer
TT = TE - TS



MsgBox TT



Title: Re: Turn tool turret macro
Post by: drumsticksplinter on April 27, 2020, 05:55:47 AM
That is great! Thank you very much for this!!

I have used this example in my code and it looks to be working, i will test on the machine when I get a minute and see if the whole macro works as it should.

Thanks again!!  :D :D :D
Title: Re: Turn tool turret macro
Post by: drumsticksplinter on April 28, 2020, 05:12:05 AM
Thanks TPS, that workaround for the timer has worked really well. I have done some tests this morning and my macro now seems to be working as it should, which I'm really happy about!!

Something that almost always catches me out is when setting and reading I/O's or DRO's, a sleep is needed as Mach either misses the command, or crashes if not.

I've posted the code below so others might be able to make use of it:

Code: [Select]
'-----------------------------------------------------------------------
'CYCLONE - Auto tool changer (8 pos turret type)
'V1.0
'-----------------------------------------------------------------------

'-----------------------------------------------------------------------
'CONFIG
'-----------------------------------------------------------------------

Dim TS As Single
Dim TE As Single
Dim TT As Single

NewTool = GetSelectedTool() 'Commanded tool number
OldTool = GetCurrentTool() 'Current tool number
MaxToolNum = 8 'Max number of tools for the changer
PreviousSpeed = GetRPM() '
ToolSpeed = 500

'-----------------------------------------------------------------------
'MACRO START
'-----------------------------------------------------------------------

DeActivateSignal(OUTPUT6) 'Make sure signal to PLC is negative

SetSpinSpeed(ToolSpeed) 'Spindle speed change
code"G91 G28 X0" 'set incremental, return home via X first
While IsMoving()
Wend

code"G91 G28 Z0"
While ISMoving()
Wend

While NewTool > MaxToolNum 'Dont allow tool number greater 8
Message("MAX TOOL NUMBER = 8")
Wend

While NewTool < 0 'Dont allow tool number less than 1
Message("INVALID TOOL NUMBER")
Wend

If NewTool = OldTool Then
Call ToolComplete
Else
If IsActive(INPUT1) Then 'ok signal from PLC

Call DoToolChange
Else
code"M00"
code"M5"
Message("TURRET NOT READY!")
End If
End If
End

'----------------------------------------------------------------------
'TOOL CHANGE
'----------------------------------------------------------------------

Sub DoToolChange()

TS = Timer 'get the start time

SetCurrentTool(NewTool) 'Update the tool DRO
ActivateSignal(OUTPUT6) 'Set tool change allowed signal to PLC
sleep 500

While IsActive(INPUT1) = False 'While the turret is moving, count down

TE = Timer
If (TE - TS) > 4 Then
Call ToolFailed
End If
Wend

Call ToolComplete


End Sub
     
 

'----------------------------------------------------------------------
'TOOL COMPLETE
'----------------------------------------------------------------------

Sub ToolComplete()
DeActivateSignal(OUTPUT6)
sleep 100
TT = TE - TS
SetSpinSpeed(PreviousSpeed) 'RPM before tool change called
Message("Tool Change Complete in " & Round(TT,1) & " seconds")
sleep 100
End Sub


'----------------------------------------------------------------------
'TOOL FAILED
'----------------------------------------------------------------------

Sub ToolFailed()
DeActivateSignal(OUTPUT6)
sleep 100
Code"M00"
Code"M5"
Message("TURRET TIMEOUT  TOOL  " & NewTool & "  NOT LOCATED" )
sleep 100
Title: Re: Turn tool turret macro
Post by: TPS on April 28, 2020, 09:51:39 AM
nice to hear that you got it working.