Mach Discussion > VB and the development of wizards
I cant understand what is wrong with my code?
boydage:
Novice coder. Lots of time using machines. I thought I would be able to write a specific piece of code for a 4 turret lathe toolchanger made from other samples I have found online. So many different ways to do it. But no easy path to learn from scratch. I plan to do it with a three phase reverse relay, spin the turret until the selected tool shows an input, reverse the turret to lock it. Toolchange done.
My problem is the Mach 3 VB editor only shows a "syntax error" every time but wont highlight or show where the problem is. Quite similar to troubleshooting an elec system with no meter - all it does is frustrate me.
I need some help please either in what I am doing wrong with the editor so its more useful, or, well thats it in a nutshell - what am I doing wrong? I thought the editor was supposed to jump through each line and highlight the piece of code that wont work. Hey if there is a more suitable editor would I be inclined to use that?
I dont even know the basic laws I am supposed to follow. This is the code I am trying to understand and get to work so I can at least test it on my machine.
'Tool change macro for 4 tool turret
Sub Main()
'Sets variable OldTool to what is currently loaded
OldTool=GetCurrentTool()
'Sets Variable MaxToolNum to the max number of tools possible
MaxToolNum=4
'Sets variable Newtool to the one being selected with M6 T#
tool = GetSelectedTool()
NewTool = tool
'Get positions before moving to do tool change
x = GetToolChangeStart( 0 )
'If the current tool loaded is 0 or greater than 4 then tool has been lost
'so need to ask what tool is currently loaded
While OldTool=0 Or OldTool>4
OldTool=Question ("Current tool unknown, enter tool in spindle 1 to " & MaxToolNum)
Wend
'Sets CurrentTool to Oldtool in case it was lost and entered above
SetCurrentTool(OldTool)
'When the tool asked for is invalid then this makes you select a valid tool
While NewTool > MaxToolNum Or NewTool <1
NewTool = Question ("Invalid tool chosen, enter tool number 1 to " & MaxToolNum)
Wend
'If the tool asked for is the same one that is already loaded then exit macro
If NewTool=OldTool Then
Message "Tool already loaded or tool not specified with T# (ex:M6 T4)"
Exit Sub
End If
'Turn off soft limits if they are on
If GetOEMLED(23) Then
DoOEMButton(119)
End If
'Moves X axis to toolchange position
End If
Code"G53 X0 0" 'Move to tool change location
While Ismoving()
Wend
If NewTool= 1 Then
ActivateSignal(OUTPUT4) 'Begin rotating turret'
If IsActive(INPUT1) Then 'Turret has rotated until selected tool is in position'
DeActivateSignal(OUTPUT4) 'Stop turret from spinning"
Sleep(1000)
ActivateSignal (OUTPUT8) 'Reverse Turret"
If IsActive(INPUT2) Then 'Tool locked signal'
DeactivateSignal(OUTPUT8)
Exit Sub
If NewTool= 2 Then
ActivateSignal(OUTPUT5) 'Begin rotating turret'
If IsActive(INPUT1) Then 'Turret has rotated until selected tool is in position'
DeActivateSignal(OUTPUT5) 'Stop turret from spinning"
Sleep(1000)
ActivateSignal (OUTPUT8) 'Reverse Turret"
If IsActive(INPUT2) Then 'Tool locked signal'
DeactivateSignal(OUTPUT8)
Exit Sub
If NewTool= 3 Then
ActivateSignal(OUTPUT6) 'Begin rotating turret'
If IsActive(INPUT1) Then 'Turret has rotated until selected tool is in position'
DeActivateSignal(OUTPUT6) 'Stop turret from spinning"
Sleep(1000)
ActivateSignal (OUTPUT8) 'Reverse Turret"
If IsActive(INPUT2) Then 'Tool locked signal'
DeactivateSignal(OUTPUT8)
Exit Sub
If NewTool= 4 Then
ActivateSignal(OUTPUT7) 'Begin rotating turret'
If IsActive(INPUT1) Then 'Turret has rotated until selected tool is in position'
DeActivateSignal(OUTPUT7) 'Stop turret from spinning"
Sleep(1000)
ActivateSignal (OUTPUT8) 'Reverse Turret"
If IsActive(INPUT2) Then 'Tool locked signal'
DeactivateSignal(OUTPUT8)
Exit Sub
SetCurrentTool(NewTool)
End
boydage:
With zero ideas of where to go I tried asking ChatGPT and this is the code it responded with after a few attempts. The syntax error has gone, I am just not sure it will work?
Curious indeed.
'Tool change macro for 4 tool turret
Sub Main()
'Sets variable OldTool to what is currently loaded
OldTool = GetCurrentTool()
'Sets Variable MaxToolNum to the max number of tools possible
MaxToolNum = 4
'Sets variable Newtool to the one being selected with M6 T#
tool = GetSelectedTool()
NewTool = tool
'Get positions before moving to do tool change
x = GetToolChangeStart(0)
'If the current tool loaded is 0 or greater than 4 then the tool has been lost
'so need to ask what tool is currently loaded
While OldTool = 0 Or OldTool > MaxToolNum
OldTool = Question("Current tool unknown, enter tool in spindle 1 to " & MaxToolNum)
Wend
'Sets CurrentTool to Oldtool in case it was lost and entered above
SetCurrentTool(OldTool)
'When the tool asked for is invalid, this makes you select a valid tool
While NewTool > MaxToolNum Or NewTool < 1
NewTool = Question("Invalid tool chosen, enter tool number 1 to " & MaxToolNum)
Wend
'If the tool asked for is the same one that is already loaded, then exit the macro
If NewTool = OldTool Then
Message "Tool already loaded or tool not specified with T# (ex:M6 T4)"
Exit Sub
End If
'Turn off soft limits if they are on
If GetOEMLED(23) Then
DoOEMButton(119)
End If
'Moves X axis to tool change position
' Assuming the following line is the correct way to specify the position:
' Code "G53 X0 Y0"
Code "G53 X0 Y0"
While IsMoving()
Wend
' Perform the tool change actions based on the selected tool number
If NewTool = 1 Then
ActivateSignal(OUTPUT4) 'Begin rotating turret'
While Not IsActive(INPUT1) 'Turret has not yet rotated until the selected tool is in position'
Wend
DeactivateSignal(OUTPUT4) 'Stop turret from spinning
Sleep(1000)
ActivateSignal(OUTPUT8) 'Reverse Turret
While Not IsActive(INPUT2) 'Tool locked signal is not active
Wend
DeactivateSignal(OUTPUT8)
ElseIf NewTool = 2 Then
ActivateSignal(OUTPUT5) 'Begin rotating turret'
While Not IsActive(INPUT1) 'Turret has not yet rotated until the selected tool is in position'
Wend
DeactivateSignal(OUTPUT5) 'Stop turret from spinning
Sleep(1000)
ActivateSignal(OUTPUT8) 'Reverse Turret
While Not IsActive(INPUT2) 'Tool locked signal is not active
Wend
DeactivateSignal(OUTPUT8)
ElseIf NewTool = 3 Then
ActivateSignal(OUTPUT6) 'Begin rotating turret'
While Not IsActive(INPUT1) 'Turret has not yet rotated until the selected tool is in position'
Wend
DeactivateSignal(OUTPUT6) 'Stop turret from spinning
Sleep(1000)
ActivateSignal(OUTPUT8) 'Reverse Turret
While Not IsActive(INPUT2) 'Tool locked signal is not active
Wend
DeactivateSignal(OUTPUT8)
ElseIf NewTool = 4 Then
ActivateSignal(OUTPUT7) 'Begin rotating turret'
While Not IsActive(INPUT1) 'Turret has not yet rotated until the selected tool is in position'
Wend
DeactivateSignal(OUTPUT7) 'Stop turret from spinning
Sleep(1000)
ActivateSignal(OUTPUT8) 'Reverse Turret
While Not IsActive(INPUT2) 'Tool locked signal is not active
Wend
DeactivateSignal(OUTPUT8)
Else
' Handle any other tool number that might be unexpected
' ...
End If
' Set the current tool to the new selected tool
SetCurrentTool(NewTool)
End Sub
TPS:
first of all you have to get clear with your hardwaresignal's.
make a list what you have. for example:
OUTPUT1 'turn ATC CW
OUTPUT2 'turn ATC CCW
INPUT1 'ATC in position tool 1
INPUT2 'ATC in position tool 2
INPUT3 'ATC in position tool 3
INPUT4 'ATC in position tool 4
OEMTRIGGER1 'ATC locked
after all signal's are clear defined, get those connected and tested.
and after that we will have a look for the code.
boydage:
I really appreciate that. I am using relay logic for tool selection as I only have 5 inputs total into my BOB but have lots of outputs which will go directly to a set of DIN SSRs. I will do that, wire it up, test like you say and report back also after my homing limit switches are wired too. Infact the machine will be complete. I am reasonably confident the wiring design is sound, I am just not positive on the interface with the turret that is not here yet whether its PNP or NPN and how it will connect.
Wiring diagram on my initial plan attached for info.
Please standby for about 4-5 weeks lol
TPS:
when i look to your wiring diagram i expect that whatever OUTPUT (T1-T4) will be enabled
it will allways enegice all relay's (K1,K3,K4,K5,K6).
the minimum is to put diodes into each OUTPUT line (T1-T4) in front of K1.
Navigation
[0] Message Index
[#] Next page
Go to full version