Hello Guest it is April 28, 2024, 12:52:48 PM

Author Topic: I cant understand what is wrong with my code?  (Read 1914 times)

0 Members and 1 Guest are viewing this topic.

I cant understand what is wrong with my code?
« on: July 19, 2023, 08:36:18 PM »
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



                         
Re: I cant understand what is wrong with my code?
« Reply #1 on: July 19, 2023, 08:52:30 PM »
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

Offline TPS

*
  •  2,505 2,505
    • View Profile
Re: I cant understand what is wrong with my code?
« Reply #2 on: July 20, 2023, 05:20:28 AM »
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. 
 
anything is possible, just try to do it.
if you find some mistakes, in my bad bavarian english,they are yours.
Re: I cant understand what is wrong with my code?
« Reply #3 on: July 20, 2023, 04:02:01 PM »
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

Offline TPS

*
  •  2,505 2,505
    • View Profile
Re: I cant understand what is wrong with my code?
« Reply #4 on: July 21, 2023, 03:54:54 AM »
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.
anything is possible, just try to do it.
if you find some mistakes, in my bad bavarian english,they are yours.
Re: I cant understand what is wrong with my code?
« Reply #5 on: July 21, 2023, 04:13:14 AM »
Oh your so right! Thats a big DERRR on me ha.

Hmmm. I am using low voltage SSRs so the BOB can operate them. I wonder if there is any leakage through a diode, or infact if one fails it would be a good size crash possibly on wrong tool selection. I might look for an opto coupler 4-5 way board to properly isolate those common inputs.

Ummm - thanks for the headsup you just saved me a bit of time when it got powered.
Re: I cant understand what is wrong with my code?
« Reply #6 on: July 22, 2023, 07:11:25 PM »
Ok so I took a look at think this opto board will work. I will also consider some more safety interlocks.

Interesting, and to be expected, I used a schneider contactor yesterday that come off Aliexpress. This is a 3p contactor to power the vfd I am using for my lathe motor. I did check to see if lots of people had purchased them off this seller. And another self smack on the head when I measured 50v between two of the phases (415v in NZ). The almost complete loss of a phase through that contactor, made my VFD error and shut down. Ive normally purchased genuine parts for line voltage work. This was an exception, and a lesson. Chinese copies are definately at your own risk.

Oh, and here I am purchasing a $5 opto board......

https://www.aliexpress.com/item/33017803867.html?spm=a2g0o.productlist.main.3.5d21584f54hRV8&algo_pvid=4aec9757-9819-4cd9-a94e-6967797c330c&algo_exp_id=4aec9757-9819-4cd9-a94e-6967797c330c-1&pdp_npi=3%40dis%21NZD%216.28%215.72%21%21%213.83%21%21%40210218bf16900666621192466d077b%2167157033790%21sea%21NZ%212099605941&curPageLogUid=JjJXcSfo8BKN

Re: I cant understand what is wrong with my code?
« Reply #7 on: August 25, 2023, 07:45:02 PM »
Now I understand what was wrong with my code. Hey, so, I managed to get my turret to operate. Pretty stoked it worked right off the bat for a new coder. Although I didnt want to spend too much time on VB I also have a router with Mach 4 so LUA is my focus. Cool.

My M6Start Macro is attached. It is specific to my machine in the fact I have very little outputs and have used up all inputs. So this macro is only going to work with hardware configuration like mine. Also, be very selective when you purchase your SSRs as they need to work with low voltages directly off a BOB. The wiring diagram is in this forum post. The turret is a LD6B Chinese one. Does the job.

But can someone help me finish this please I am not sure what to do I think its now my post that needs to be edited. I have seen something about options for Turret, QCTP on front, QCTP on rear with options to change but I don't know exactly what I am editing. Currently, instead of a tool change, my machine pauses with "CHANGE TO T3 ON REAR TOOL POST". I need to hit the cycle button and then it will change tools. There is a line in my machining file that does this - how do I edit my post so it removes that line. Its line N14.

Really hoping someone can hop in and help. Also, is my macro ok or do I need to look at more safety locks? I will always home the machine on startup and always have tooling set correct prior to a job. Second question is I have a 4 post turret. Many more tools that will number 1-4 with several with the same tool number but different offsets. Wondering how I program into the tool library tool #1 for example with offset #6 for example. Ie T0106 - that must be in Fusion 360.

Anyways. Sample code attached.

Macro Below
Sub Main()
    OldTool = GetCurrentTool() 'Sets old tool variable
    NewTool = GetSelectedTool() 'Sets the new tool variable
    MaxToolNum = 4 'Max tools on the turret

    If MaxToolNum < 1 Or MaxToolNum > 4 Then
        Message "Incorrect Tool Selected"
    ElseIf OldTool = NewTool Then
        ' No toolchange required, exit sub
        Exit Sub
    End If

'If GetOEMLED(87) Or GetOEMLED(809) Then
    'Message "Machine not homed Dork"
    'Code "M30"
'End If

   
   
    If NewTool = OldTool Then
    Message "No Change"
    End If

    Code "G53 F1800 X-10" 'Move to change position
    While IsMoving() 'Wait for movement to complete
    Wend



  ' Tool Change Time
   
   
   If NewTool = 1 Then
      ActivateSignal(OUTPUT3) 'Open Tool #1 Relay for selection input to BOB
      Sleep(500)
      ActivateSignal(OUTPUT7) 'Begin rotating turret'
      While Not IsActive(INPUT2) 'Turret is rotating until the selected tool is in position'
      Wend
      DeactivateSignal(OUTPUT7) 'Stop turret from spinning
      Sleep(100)
      ActivateSignal(OUTPUT8) 'Reverse Turret
      Sleep(1400)
      DeactivateSignal(OUTPUT3)
      DeactivateSignal(OUTPUT8)
   
     
    ElseIf NewTool = 2 Then
      ActivateSignal(OUTPUT4) 'Open Tool #1 Relay for selection input to BOB
      Sleep(500)
      ActivateSignal(OUTPUT7) 'Begin rotating turret'
      While Not IsActive(INPUT2) 'Turret is rotating until the selected tool is in position'
      Wend
      DeactivateSignal(OUTPUT7) 'Stop turret from spinning
      Sleep(100)
      ActivateSignal(OUTPUT8) 'Reverse Turret
      Sleep(1400)
      DeactivateSignal(OUTPUT4)
      DeactivateSignal(OUTPUT8)
     
     
     ElseIf NewTool = 3 Then
      ActivateSignal(OUTPUT5) 'Open Tool #1 Relay for selection input to BOB
      Sleep(500)
      ActivateSignal(OUTPUT7) 'Begin rotating turret'
      While Not IsActive(INPUT2) 'Turret is rotating until the selected tool is in position'
      Wend
      DeactivateSignal(OUTPUT7) 'Stop turret from spinning
      Sleep(100)
      ActivateSignal(OUTPUT8) 'Reverse Turret
      Sleep(1400)
      DeactivateSignal(OUTPUT5)
      DeactivateSignal(OUTPUT8)
     
     
    ElseIf NewTool = 4 Then
      ActivateSignal(OUTPUT6) 'Open Tool #1 Relay for selection input to BOB
      Sleep(500)
      ActivateSignal(OUTPUT7) 'Begin rotating turret'
      While Not IsActive(INPUT2) 'Turret is rotating until the selected tool is in position'
      Wend
      DeactivateSignal(OUTPUT7) 'Stop turret from spinning
      Sleep(100)
      ActivateSignal(OUTPUT8) 'Reverse Turret
      Sleep(1400)
      DeactivateSignal(OUTPUT6)
      DeactivateSignal(OUTPUT8)
 

      SetCurrentTool(NewTool) 'Set the new tool to current
    End If
End Sub                     
Re: I cant understand what is wrong with my code?
« Reply #8 on: August 25, 2023, 08:24:37 PM »
Hmmm. Its not updating the new tool to the old tool. Must study more I thought that was the easy part.

Offline TPS

*
  •  2,505 2,505
    • View Profile
Re: I cant understand what is wrong with my code?
« Reply #9 on: August 26, 2023, 03:43:15 AM »
your SetCurrentTool(NewTool) is at the wrong place:

origin:
Code: [Select]
...
...
      SetCurrentTool(NewTool) 'Set the new tool to current
    End If
End Sub                   

should be:
Code: [Select]
...
...
    End If
    SetCurrentTool(NewTool) 'Set the new tool to current
End Sub                   

if still not working try:
Code: [Select]
...
...
    End If
    SetCurrentTool(NewTool) 'Set the new tool to current
    Code "G43H" & NewTool
End Sub                   
anything is possible, just try to do it.
if you find some mistakes, in my bad bavarian english,they are yours.