Machsupport Forum

Mach Discussion => General Mach Discussion => Topic started by: stephanbrunker on August 18, 2014, 06:42:48 AM

Title: Bug in G77 / M1077 macro
Post by: stephanbrunker on August 18, 2014, 06:42:48 AM
Because mentionend in the Turn Handbook, i tried the G77 / G78 canned cycles. But in G77 I found two bugs.

Line 48-52 must be:
Code: [Select]
If( Taper = 0 ) Then
  ZCalc = Abs(StartZ - EndZ)   'was ZCalc = 0, which ignores EndZ further on
Else                     
  ZCalc = Abs(StartX - EndX) /Tan((Taper * PI)/180) 
End if

and Line 71-74 must be:
Code: [Select]
  If IDOD =1 And PassDia <= EndX Or IDOD =-1 And PassDia >= EndX Then        'was < and > instead of <= >=, which causes to repeat the final cut
   PassDia = EndX           
   LoopExit = 1           
  End If

seems that nobody has used these two since creation (2009).

Title: Re: Bug in G77 / M1077 macro
Post by: stephanbrunker on August 29, 2014, 10:51:40 AM
Seems that they were some other bugs in that macro too. I'm going to rewrite it with a roughing and a sizing stage - like in the wizard. But the OD Turning wizard has also a kind of bug inside. If you've a tool with an angle of around 80°, then you'll produce a nice crash in the sizing stage. Because after the last turning, it continues the z axis move to Z End and removes all the material at once because it takes the turn from inside out - it has to be the other way.
Title: Re: Bug in G77 / M1077 macro
Post by: stephanbrunker on October 01, 2014, 07:15:24 AM
I've solved my problems by rewriting the M1077 and M1078 macros. I use them quite often, because it's the simplest way to machine standard parts on the lathe - by combining G77 / G78 words. It first does roughing with the values in the general options when no h word is given and makes a sizing move with 40% feed and 40% of the roughing amount. Also, it evens the roughing passes out so that they are all the same amount. So, here is my code, without warranty of course:

M0177:
' G77   
'(Xx.*********x XDia NEEDED)
'(Zx.*********x End Z NEEDED)
'(Fx.*********x Feedrate)
'(can be Set In the settings page: 'Hx.*********x Depth of cut)
'(Cx.*********x Clearance In the X)
'(Qx.*********x Clearance In the Z)
'(Kx.*********x ZStartpoint)
'(Rx.*********x XStartpoint)
'(Tx.*********x Taper In Deg)   

Sub Main()                     
ZClearance = Abs(tZClear()) 'This one it not avalable                     
XClearance = Abs(tClearX())                     
StartX = tXStart()                     
StartZ = tZStart()                     
EndX = tEndX()                       
EndZ = tEndZ()                     
Taper = tTaper()                     
FirstPassDepth = tFirstPass()                     
RoughAmount =  abs(tCutDepth())                   
Feed = Feedrate()                     

SizingAmount = RoughAmount * 0.4
SizeFeed = Feed * 0.4
PI = 3.1415926

If StartZ > EndZ Then                     
    RightLeft = 1                     
Else                     
    RightLeft = -1                     
    ZClearance = ZClearance * -1                     
End If 

If StartX < EndX Then   'Inside Turning                 
    IDOD = -1                     
    XClearance = XClearance * -1
Else                     
    IDOD = 1           'Outside         
End If   
                                     
'Calc the distance X needed to make the taper angle                     
TaperX = Abs(StartZ - EndZ)* Tan((Taper * PI)/180)                     
If TaperX > Abs(StartX - EndX) Then
    EndZ = StartZ - Abs(StartZ - EndZ) * RightLeft * (  Abs(StartX-EndX) / TaperX )
    TaperX = Abs(StartX - EndX)
End If             
                               
code "G0 X" & StartX + XClearance & " Z" & StartZ +  ZClearance           
code "F" & Feed           
PassNum = 1                     
LastPassDia = StartX                   

'optimize roughing value
Passes = (Abs(StartX - EndX) - SizingAmount) / RoughAmount
If CInt(Passes) <> Passes Then
    passes = CInt(Passes) + 1
    RoughAmount = (Abs(StartX - EndX) - SizingAmount) / Passes
End If

Do  'Roughing Loop         
    PassDia = StartX - (RoughAmount * IDOD * PassNum)           
         
    code  "G0 X" & PassDia           

    If TaperX <> 0 And Abs(PassDia-EndX) < TaperX + SizingAmount Then
        'Taper turn section                         
        ZCalc = StartZ - RightLeft * Abs(( (StartZ - EndZ) * (Abs(PassDia - EndX) - SizingAmount)) / TaperX )         
        code "G1 Z" & nFmt(ZCalc,4)   
           
        If (Abs(PassDia-EndX) < TaperX + SizingAmount) And (Abs(LastPassDia-EndX) > TaperX + SizingAmount) Then 
            code  "X" & EndX + (TaperX + SizingAmount) * IDOD & " Z" & EndZ + SizingAmount * RightLeft           
            code  "X" & LastPassDia     
            PosZ = EndZ + SizingAmount * RightLeft       
        Else   
            ZCalc = StartZ - RightLeft * Abs(( (StartZ - EndZ) * (Abs(LastPassDia - EndX) - SizingAmount)) / TaperX )           
            code "X" & LastPassDia & " Z" & nFmt(ZCalc,4)           
            PosZ = nFmt(ZCalc,4)           
           
        End If
         
        If PassNum = Passes Then Exit Do

        code  "G0 X" & LastPassDia + XClearance & " Z" & PosZ + ZClearance   
       
    Else           
        'Straight turn section           
        code  "G1 Z" & EndZ + SizingAmount * RightLeft         
        PosZ = EndZ + SizingAmount * RightLeft
        code  "X" & LastPassDia                     

        If PassNum = Passes Then Exit Do
       
        If Abs(LastPassDia - PassDia) < Abs(XClearance) Then
            code  "G0 X" & PassDia + XClearance & " Z" & PosZ + ZClearance   
        End If
    End If           

    code  "G0 Z" & StartZ + ZClearance           
   
    LastPassDia = PassDia           
    PassNum = PassNum + 1           
Loop           

'sizing
code "F" & SizeFeed   

If Abs(EndX - StartX) > TaperX Then
    If TaperX = 0 Then
        code "G0 X" & PassDia + XClearance & " Z" & PosZ + ZClearance
    End If
    code "X" & StartX + XClearance     
    code "Z" & EndZ       
    If TaperX <> 0 Then
        code "G1 X" & EndX + (TaperX + SizingAmount) * IDOD
        code "G0 X" & EndX + XClearance + (TaperX + SizingAmount) * IDOD & " Z" & EndZ + ZClearance
    Else       
        code "G1 X" & PassDia
        code "G0 X" & PassDia + XClearance & " Z" & EndZ + ZClearance
    End If
End If

code "Z" & StartZ + ZClearance     
code "X" & EndX       
If TaperX = 0 Then
    code "G1 Z" & EndZ
    code "X" & PassDia
    code "G0 X" & PassDia + XClearance & " Z" & EndZ + ZClearance
Else
    code "G1 Z" & StartZ   
    If Abs(StartX-EndX) > TaperX Then
        code "G1 Z" & EndZ & " X" & EndX + TaperX * IDOD 
        code "X" & EndX + (TaperX + SizingAmount) * IDOD 
        code "G0 X" & EndX + (TaperX + SizingAmount) * IDOD + XClearance & " Z" & EndZ + ZClearance   
        code "X" & StartX + XClearance & " Z" & EndZ + ZClearance   
    Else
        code "G1 Z" & EndZ & " X" & EndX + TaperX * IDOD 
        code "G0 X" & StartX + XClearance & " Z" &  EndZ + ZClearance   
    End If
End If

code  "G0 X" & StartX & " Z" & StartZ           
code "F" & Feed
End Sub                     
Main

 

=================================================================================


M0178:
'G78
'(Xx.*********x XDia NEEDED)
'(Zx.*********x End Z NEEDED)
'(Fx.*********x Feedrate)
'(can be Set In the settings page: 'Hx.*********x Depth of cut)
'(Cx.*********x Clearance In the X)
'(Qx.*********x Clearance In the Z)
'(Kx.*********x ZStartpoint)
'(Rx.*********x XStartpoint)
'(Tx.*********x Taper In Deg)

Sub Main()
ZClearance =  Abs(tZClear()) 'This one it not avalable
XClearance = Abs(tClearX())
StartX = tXStart()
StartZ = tZStart()
EndX = tEndX()
EndZ = tEndZ()
Taper = tTaper()
FirstPassDepth = tFirstPass()
RoughAmount =  tCutDepth()
Feed = FeedRate()
 
SizingAmount = RoughAmount * 0.4
SizeFeed = Feed * 0.4
PI = 3.1415926                                                     

If StartX < EndX Then
    IDOD = -1
    XClearance = XClearance * -1
Else
    IDOD = 1
End If
 
If StartZ > EndZ Then
    RightLeft = 1
Else
    RightLeft = -1
    ZClearance = ZClearance * -1 
End If

If Taper <> 0 Then
    TaperZ = Abs(StartX - EndX) /  Tan((Taper * PI)/180)
    If TaperZ < Abs(StartZ - EndZ) Then
        TaperX = Abs(StartX - EndX)
        EndZ = StartZ - TaperZ * RightLeft
    Else
        TaperX = Abs(StartZ - EndZ) * Tan((Taper * PI)/180)         
    End If
End If

code "G0 X" & StartX + XClearance & " Z" & StartZ +  ZClearance
code "F" & Feed
PassNum = 1                     
LastPassDepth = StartZ                                       

'optimize roughing value
Passes = (Abs(StartZ - EndZ) - SizingAmount) / RoughAmount
If CInt(Passes) <> Passes Then
    Passes = CInt(Passes) + 1
    RoughAmount = (Abs(StartZ - EndZ) - SizingAmount) / Passes
End If

Do     
    PassDepth = StartZ - (RoughAmount * PassNum * RightLeft)                                       
    code "G0 Z" & PassDepth                     
    If taper <> 0 Then
        XCalc = EndX + IDOD * Abs((StartX - EndX) * (Abs(StartZ - PassDepth) + SizingAmount) / TaperZ )                     
        code "G01 X" & XCalc             
        XCalc = EndX + IDOD * Abs((StartX - EndX) * (Abs(StartZ - LastPassDepth) + SizingAmount) / TaperZ )                     
        code "X" & XCalc & " Z" & LastPassDepth                       
        PosX = XCalc
        If PassNum = Passes Then Exit Do
        code "G0 X" & PosX + XClearance & " Z" & LastPassDepth + ZClearance
    Else
        code "G1 X" & EndX + SizingAmount * IDOD 
        PosX = EndX + SizingAmount * IDOD
        code "G1 Z" & LastPassDepth   
        If PassNum = Passes Then Exit Do
        If Abs(LastPassDepth - PassDepth) < Abs(ZClearance) Then
            code "G0 X" & PosX + XClearance & " Z" & PassDepth + ZClearance   
        End If
    End If
   
    code "G0 X" & StartX + XClearance                   
    LastPassDepth = PassDepth                     
                   
    PassNum = PassNum + 1                     
Loop                     

'sizing
code "F" & SizeFeed   
If Taper = 0 Then
    code "G0 X" & EndX + SizingAmount * IDOD + XClearance & " Z" & EndZ + SizingAmount * RightLeft + ZClearance
    code "Z" & StartZ + ZClearance
    code "X" & EndX
    code "G1 Z" & EndZ + SizingAmount * RightLeft
    code "G0 X" & EndX + XClearance & " Z" & EndZ + SizingAmount * RightLeft + ZClearance
Else
    code "G0 X" & PosX + XClearance & " Z" & EndZ + SizingAmount * RightLeft + ZClearance
End If
code "X" & StartX + XClearance
code "Z" & EndZ
code "G1 X" & StartX
If taper = 0 Then
    code "X" & EndX
    code "Z" & EndZ + SizingAmount * RightLeft
    code "G0 X" & EndX + XClearance & " Z" & EndZ + ZClearance
Else
    If Abs(StartX - EndX) > TaperX Then
        code "X" & EndX + IDOD * TaperX
    End If
    code "X" & EndX & " Z" & StartZ
    code "G0 X" & EndX + XClearance & " Z" & StartZ + ZClearance
End If

code "G0 X" & StartX & " Z" & StartZ                     
code "F" & Feed

End Sub                     
Main             
         

Title: Re: Bug in G77 / M1077 macro
Post by: stephanbrunker on October 02, 2014, 12:31:19 PM
If you use the Tooltable, then these macros have an additional error: The "T" word initiates a tool change and/or the taper. So, you have to rewrite them with a variable for the taper.
Title: Re: Bug in G77 / M1077 macro
Post by: DICKEYBIRD on October 02, 2014, 08:28:06 PM
Just a quick note to say thanks for taking the time to document this.  I'm not far enough along in my CNC lathe odyssey to fully understand what you're talking about but enough so that I feel a little less responsible for some of the unexplained crashes I've had.  Keep up the good work! ;D
Title: Re: Bug in G77 / M1077 macro
Post by: stephanbrunker on April 24, 2015, 02:50:33 PM
Solving the taper issue is easy - i forgot to post it here:

in the macro, you should edit the line

taper = tTaper()

to

Taper = GetVar(101)
SetVar(101,0)
If Taper > 90 or Taper < 0 Then
   message "Error, Taper out of Bounds"
   code "M30"
   Exit Sub
End If


so it takes the taper angle from a user variable and resets it after use. in the GCode, program prior to your macro call #101=45 , if you want something different than zero.
Title: Re: Bug in G77 / M1077 macro
Post by: stephanbrunker on September 07, 2016, 08:59:02 AM
Because I don't want to update the code at two locations here the link to my personal page with the last version of the macros:

http://www.stephan-brunker.de/Maschinen/Software/body_software.html#M1077 (http://www.stephan-brunker.de/Maschinen/Software/body_software.html#M1077)
Title: Re: Bug in G77 / M1077 macro
Post by: RICH on September 07, 2016, 06:04:36 PM
stephanbrunker,

Never used the lathe macros for G77 and G78 in all these years. :D

Thanks for pointing out the bugs and fixes.
Much appreciated......keep up the good work,

RICH