585,690 active members*
4,817 visitors online*
Register for free
Login
IndustryArena Forum > Machine Controllers Software and Solutions > G-Code Programing > How to rotate G-code in the G-code (to simulate 4th and 5th axis) or axis indexing?
Results 1 to 15 of 15
  1. #1
    Join Date
    May 2013
    Posts
    10

    Question How to rotate G-code in the G-code (to simulate 4th and 5th axis) or axis indexing?

    Hi, I've got a 3 axis homebuilt CNC router. I have built a head adapter that can be manually indexed to "simulate" a 4th and 5th axis. The main reason I've done this is to get access to undercut areas on parts.

    My additional "4th axis" is around the Z and can be indexed to 0,45,90,135,180,225,270,315 (45 degree increments). The "5th axis" is a bracket that tips the spindle to 60 degrees from the Z axis. Imagine if the 5th is at zero (spindle straight down) the 4th axis really won't do anything but index the spindle around the Z. Here is a similar machine, but mine has "static indexed" axis for the extra 2 axis:

    https://www.youtube.com/watch?v=nITLI_WcnuM

    And the beginning of this video I would call 2.5 axis milling (facing, pocketing) with the 2 additional axis "statically indexed".

    https://www.youtube.com/watch?v=kGCZUO_E0AU

    What I would like to be able to do is rotate the whole G-code program to a new coordinate system that represents the new angle and rotation of the head.

    So if I manually tip my head -60 degrees (around the x) my cutter will be pointed at a vector of x0 y-.866 z-.500.

    So I now want the "controller" to think the whole coordinate system has shifted and/or rotated. Maybe something like this:

    G92 X0 Y0 Z0 A-60 B0 C0 This would just rotate around the X maybe?

    Then starting from X0Y0Z0 I would expect a command like:

    G1Z-1F50

    would result in both Y and Z moving in sync to actual location y-.866 z-.500 (sin and cos of tipped angle -60)

    I'm using Linuxcnc for a controller and it doesn't like the A, B or C if I try that. I assume because the machine is only set-up as "3-axis". I don't really understand what U,V and W mean either (in a G92 context)

    Other solutions are to tip the entire part, say on a sine plate, which would work for some items, but I only have 10" of z travel so that gets eaten up pretty quick if you try to tip the part. Or I could write a translator that could "rotate" g-code around a point using some trig, not easy but doable.

    It would be neat if it could also account for G2, G3 moves in any plane, but I would settle for just G0 and G1.

    I may not know the right terminology for what I'm trying to do which is probably making it harder to find the solution.

    Thanks for any ideas.

  2. #2
    Join Date
    Jul 2009
    Posts
    419

    Re: How to rotate G-code in the G-code (to simulate 4th and 5th axis) or axis indexin

    Not sure if the software goes to changing the work plane but take a look at scorch's g-code ripper:
    G-Code Ripper
    Sven
    http://www.puresven.com/?q=building-cnc-router

  3. #3
    Join Date
    May 2013
    Posts
    10

    Re: How to rotate G-code in the G-code (to simulate 4th and 5th axis) or axis indexin

    Something like that might be the way I have to go. It looks like it would need to be modified to accept x and y axis. The math is easy, it's parsing each line and all that redtape is the hard part. You'd thing if there's not a more general coor-sys rotation command that is could be added into LinuxCNC, hint hint. ;-)

  4. #4
    Join Date
    Jan 2009
    Posts
    103

    Re: How to rotate G-code in the G-code (to simulate 4th and 5th axis) or axis indexin

    NCPlot Editor can rotate code about an axis, it also can segment all the arcs first which you need to do before rotating the code, works great

  5. #5
    Join Date
    Jul 2003
    Posts
    1220

    Re: How to rotate G-code in the G-code (to simulate 4th and 5th axis) or axis indexin

    Quote Originally Posted by Johnsinski View Post
    .....The main reason I've done this is to get access to undercut areas on parts...
    I can't see how rotating the code is giving what you need.

    I think you need to generate separate code for the problem areas with the plane re-orientated (to suit one of your head angles) in the CAM operation. .
    Set the controller to the new plane. ( Fagor G49)
    Set the head to the new plane and run the code.

  6. #6
    Join Date
    May 2013
    Posts
    10

    Re: How to rotate G-code in the G-code (to simulate 4th and 5th axis) or axis indexin

    Kiwi, I think that is exactly what I want to do. I want to program it as if the cutter were tipped to get the undercut areas, then manually tip my head and set the controller (LinuxCNC) to machine from a new tipped coordinate system. I've posted this question on the LinuxCNC forums, but have been told I can't rotate the machine coordinate system other that around the Z. Seems silly, but that's what I'm going with since they are the LinuxCNC experts.

    I'm trying to use MasterCAM and I'm quite a newb at it. I've played with WCS, tool plane and construction plane and have gotten some promising results. But the closest I can get it is NC G-code with a G90 A60, which LinuxCNC complains about since it is only a 3axis machine (with manual tipped and rotated head).

    So they are also suggesting that I need to make a new "5axis" machine .ini file so it will accept axis rotation commands, I think. Haha.

  7. #7
    Join Date
    Jul 2003
    Posts
    1220

    Re: How to rotate G-code in the G-code (to simulate 4th and 5th axis) or axis indexin

    John. If LinuxCNC doesn't allow 'incline plane' that does make things a little trickier.

    To set the new plane in the CAM, add a small flat area on the new plane in the 3D model to set off.
    Generate your code from this plane as if it is the XY plane.
    With some CAM programs (BobCAD V21), this path cam be displayed on the PC screen and then rotated to the incline plane.
    From this rotated display, generated new code from the standard XY plane.
    Adjust your machine head and run this code.

    This code may need to be run separately until you get the origin and tool length sorted to work with your other code.

  8. #8
    Join Date
    Jan 2007
    Posts
    1795

    Re: How to rotate G-code in the G-code (to simulate 4th and 5th axis) or axis indexin

    your problem not the generated code, rather to align the code to the machine and the already machined parts..

    in my opinion..
    set your model into the 0,0,0 point.... say with its gravity points, or make a sphere in the 0,0,0 and place inside your model..

    from here, simply set example head tilted on X axis to Y positive 30 deg..
    model with sphere tilt opposite, and calculate a regular 3 axis toolpath..

    now the written gcode you have to tilt 30 deg.. and your machine should cut without repositioning..

    since your B and C axis not driven by motors, not controlled it makes incredibly difficult and timeconsuming the job..
    but true, I also like to play with:-)

    it just very timeconsuming..


    your another way... when don't need any ""gcode rotating"""

    offseting surface with the radius of ball endmill and projecting a vector pattern on.. sure no arcs involved.. now this stuff virtually can be saved as cordinatas (x-y-z file) and that need to edit a little adding rapids and ready to run..
    for this, the zeroing hard..

    this method also require that youre 100 percent sure what your machine doing.. if you were not understanding of this description, then just simply skip it..

  9. #9
    Join Date
    May 2013
    Posts
    10

    Re: How to rotate G-code in the G-code (to simulate 4th and 5th axis) or axis indexin

    Kiwi, I was able to change my WCS/constuction plane in CAM to the angle and location like you said, by putting a surface there. And the backplot looks how I want it, but when it posts, the NC code has a rotation command like it's trying to rotate an axis.

    LinuxCNC has UV, VW and WU planes, but I'm not exactly sure what they are. I thought I read they are just parallel to the XYZ planes, which seems pointless to me.

    Vic, When I worked in a machine shop a long time ago, haha, we would rotate the geometry in CAM to get at the undercuts and post it just like that. Then tip the mold to that angle on a sine plate and cut it. I would usually just machine angled slots in the fixture plate so I could locate 0,0,0 after the mold was tipped. It would be an option to do it like that, but I would have to raise the whole gantry to get more clearance for tipping up the part.

    So that method is in the back of my mind, I just thought it would be easy to rotate the coordinate system on my LinuxCNC controller. Maybe there is an easy way, but I don't know it.

    It will take 20-30 minutes to tip and rotate my head manually and find 0,0,0.

    I'm trying to learn Python, to write a program to rotate the code.

    The gist of how to tip coordinates around the X axis (assuming the center of the tip of the cutter is at 0,0,0)

    A2 = -60 (tilt)
    A1 = ATN (Z1/Y1) (angle to original point)
    R1 = SQRT(Z1*Z1+Y1*Y1) (radius to original point)
    A3 = A1+A2 (new angle to rotated point)
    Y3 = R1*COS(A3) (new point)
    Z3 = R1*SIN(A3) (new point)

    And then it can be rotated around Z axis if desired.

    I'm up to reading in each line of gcode and outputting it to a new file in Python. Now I have to parse the line to get x,y,z info. Wish me luck! Like you said, it's fun to experiment.

  10. #10
    Join Date
    Jan 2007
    Posts
    1795

    Re: How to rotate G-code in the G-code (to simulate 4th and 5th axis) or axis indexin

    im glad you see I tried to saying..
    I cant say mine would be simplest..

    I like cnczone because it is like mythbusters, brings down to a simpler level very complicated things..

    beyond the simple 3axis everything just barely complicated..

    recently I checked on robot arms.. and surprisingly they real alternatives for 5 axis routers..

    irbcam what is the core for operating them cost way less than it would be expectaple..
    and it working together with visualmill..

    irbcam makes the simulation and converts the 3axis codes to 5-6 or more axis ..
    that might also worth to check..

    so just keep up with your updates good to see your project..

    by the way, have you checked cnctoolkit?

  11. #11
    Join Date
    May 2013
    Posts
    10

    Re: How to rotate G-code in the G-code (to simulate 4th and 5th axis) or axis indexin

    Well I got it done. It seems to be working. A few hours learning Python. My program only rotates g0, g1, g2 and g3 (splits arc into line segments) around the x axis and it's probably not super robust, but it worked. I just edit stock .nc code to remove a few header lines, run my rotating program on it and use the new code.

    It probably only works from the absolute zero of the machine, 0 TLO. Original cutter paths are posted with "center of BEM" offset. And the machine zero is the center of the BEM.

    The hardest parts was figuring out where the heck the center of the arc is from code with just a radius, like "g2 x4 y3 r5", of course you know the starting point, but it was still tough.

    For the super geeks, here it is:

    import math

    def pull_number (search_line,letter):
    letter_pos = search_line.find(letter)
    space_after_pos = search_line.find(' ',letter_pos)
    if letter_pos == -1:
    return -999999
    elif space_after_pos == -1:
    space_after_pos = len(search_line)
    return float(search_line[letter_pos+1:space_after_pos])
    else:
    return float(search_line[letter_pos+1:space_after_pos])

    def get_angle (z, y):
    if y == 0:
    if z >= 0:
    return my_pi_90
    else:
    return my_pi_270
    elif z == 0:
    if y >= 0:
    return 0
    else:
    return my_pi_180
    else:
    angle = math.atan(z/y)
    if y>0:
    return angle
    else:
    angle=math.pi+angle
    return angle


    def get_radius (a, b):
    return math.sqrt((a*a+b*b))


    #program start

    my_pi = math.pi# 3.1415926535
    my_pi_90 = math.pi/2
    my_pi_180 = math.pi
    my_pi_270 = math.pi*3/2

    angle_to_radians = 180/math.pi
    x_rotation_angle = 60/angle_to_radians
    z_rotation_angle = 0/angle_to_radians


    current_g = 0
    current_x = 0
    current_y = 0
    current_z = 0
    current_r = 0
    current_f = 0

    input_file = open(r'c:\temp\gcode.ngc','r')
    output_file = open(r'c:\temp\gcoderotated.ngc','w')

    current_line = str.lower(input_file.readline())

    while current_line != "m2\n":

    # print (current_line)

    line_length = len(current_line)

    new_g = pull_number(current_line, 'g')
    if new_g == -999999:
    new_g = current_g
    new_x = pull_number(current_line, 'x')
    if new_x == -999999:
    new_x = current_x
    new_y = pull_number(current_line, 'y')
    if new_y == -999999:
    new_y = current_y
    new_z = pull_number(current_line, 'z')
    if new_z == -999999:
    new_z = current_z
    new_r = pull_number(current_line, 'r')
    if new_r == -999999:
    new_r = current_r
    new_f = pull_number(current_line, 'f')
    if new_f == -999999:
    new_f = current_f



    if new_g == 0:
    angle_to_original = get_angle(new_z,new_y)
    radius = (get_radius(new_y,new_z))
    angle_to_new_point = angle_to_original+x_rotation_angle
    new_rotated_y = round(radius * math.cos(angle_to_new_point),4)
    new_rotated_z = round(radius * math.sin(angle_to_new_point),4)
    new_line = "G0 X"+str (new_x)+" Y"+str(new_rotated_y)+" Z"+str(new_rotated_z)
    output_file.write(new_line+'\n')

    elif new_g == 1:
    angle_to_original = get_angle(new_z,new_y)
    radius = (get_radius(new_y,new_z))
    angle_to_new_point = angle_to_original+x_rotation_angle
    new_rotated_y = round(radius * math.cos(angle_to_new_point),4)
    new_rotated_z = round(radius * math.sin(angle_to_new_point),4)
    new_line = "G1 X"+str (new_x)+" Y"+str(new_rotated_y)+" Z"+str(new_rotated_z)+" F"+str(new_f)
    output_file.write(new_line+'\n')

    elif new_g == 2:
    xd=new_x-current_x# x distance from start of arc for end of arc
    yd=new_y-current_y# y distance from start of arc for end of arc
    # print (xd,yd)
    a1=get_angle(yd,xd)# angle to circle end point from circle start point
    d=math.sqrt(xd*xd+yd*yd)/2# hyponenus from start of arc to end of arc (chord length)
    # print (a1*angle_to_radians)

    angle_back_to_center = a1-my_pi_90# pointing to center from middle of chord

    # print(angle_back_to_center*angle_to_radians)

    x_middle_rel=xd/2# middle of chord from start
    y_middle_rel=yd/2# middle of chord from start
    # print (new_r,d)

    y_temp=math.sqrt(new_r*new_r-d*d)# distance from middle of chord back to center

    a2 = get_angle(d,y_temp)# angle plus and minus from middle of arc

    # print (d,y_temp,a2*angle_to_radians)

    angle_start=my_pi_90+a1+a2
    angle_end=my_pi_90+a1-a2

    # print (angle_start*angle_to_radians,angle_end*angle_to_r adians)

    x_middle_abs = current_x+x_middle_rel# x absolute center of chord
    y_middle_abs = current_y+y_middle_rel# y absolute center of chord

    # print (x_middle_abs,y_middle_abs)

    i=x_middle_abs+y_temp*math.cos(angle_back_to_cente r)# absolute center of arc
    j=y_middle_abs+y_temp*math.sin(angle_back_to_cente r)# absolute center of arc

    # print (i,j)

    angle_temp1 = get_angle(.08,new_r)# desired angle step through arc
    angle_steps=int((angle_start-angle_end)/angle_temp1)# get interger number of steps

    if angle_steps < 2:# have a minimum number of steps through arc
    angle_steps = 2

    angle_step=(angle_start-angle_end)/angle_steps# get angle step

    angle_dynamic=angle_start-angle_step# first point in arc+step

    while angle_dynamic > angle_end:
    arc_x=round(i+new_r*math.cos(angle_dynamic),4)
    arc_y=j+new_r*math.sin(angle_dynamic)

    angle_to_original = get_angle(new_z,arc_y)
    radius = (get_radius(arc_y,new_z))
    angle_to_new_point = angle_to_original+x_rotation_angle
    new_rotated_y = round(radius * math.cos(angle_to_new_point),4)
    new_rotated_z = round(radius * math.sin(angle_to_new_point),4)
    new_line = "G1 X"+str (arc_x)+" Y"+str(new_rotated_y)+" Z"+str(new_rotated_z)+" F"+str(new_f)
    output_file.write(new_line+'\n')
    angle_dynamic=angle_dynamic-angle_step


    elif new_g == 3:
    xd=new_x-current_x# x distance from start of arc for end of arc
    yd=new_y-current_y# y distance from start of arc for end of arc
    # print (xd,yd)
    a1=get_angle(yd,xd)# angle to circle end point from circle start point
    d=math.sqrt(xd*xd+yd*yd)/2# hyponenus from start of arc to end of arc (chord length)
    # print (a1*angle_to_radians)

    angle_back_to_center = a1+my_pi_90# pointing to center from middle of chord

    # print(angle_back_to_center*angle_to_radians)

    x_middle_rel=xd/2# middle of chord from start
    y_middle_rel=yd/2# middle of chord from start
    # print (new_r,d)

    y_temp=math.sqrt(new_r*new_r-d*d)# distance from middle of chord back to center

    a2 = get_angle(d,y_temp)# angle plus and minus from middle of arc

    # print (d,y_temp,a2*angle_to_radians)

    angle_start=-my_pi_90+a1-a2
    angle_end=-my_pi_90+a1+a2

    # print (angle_start*angle_to_radians,angle_end*angle_to_r adians)

    x_middle_abs = current_x+x_middle_rel# x absolute center of chord
    y_middle_abs = current_y+y_middle_rel# y absolute center of chord

    # print (x_middle_abs,y_middle_abs)

    i=x_middle_abs+y_temp*math.cos(angle_back_to_cente r)# absolute center of arc
    j=y_middle_abs+y_temp*math.sin(angle_back_to_cente r)# absolute center of arc

    # print (i,j)

    angle_temp1 = get_angle(.08,new_r)# desired angle step through arc
    angle_steps=int((angle_end-angle_start)/angle_temp1)# get interger number of steps

    if angle_steps < 2:# have a minimum number of steps through arc
    angle_steps = 2

    angle_step=(angle_end-angle_start)/angle_steps# get angle step
    # print (angle_step)

    angle_dynamic=angle_start+angle_step# first point in arc+step

    while angle_dynamic < angle_end:
    arc_x=round(i+new_r*math.cos(angle_dynamic),4)
    arc_y=j+new_r*math.sin(angle_dynamic)

    angle_to_original = get_angle(new_z,arc_y)
    radius = (get_radius(arc_y,new_z))
    angle_to_new_point = angle_to_original+x_rotation_angle
    new_rotated_y = round(radius * math.cos(angle_to_new_point),4)
    new_rotated_z = round(radius * math.sin(angle_to_new_point),4)
    new_line = "G1 X"+str (arc_x)+" Y"+str(new_rotated_y)+" Z"+str(new_rotated_z)+" F"+str(new_f)
    output_file.write(new_line+'\n')
    angle_dynamic=angle_dynamic+angle_step



    # print (new_line)
    current_g = new_g
    current_x = new_x
    current_y = new_y
    current_z = new_z
    current_r = new_r
    current_f = new_f
    current_line = str.lower(input_file.readline())

  12. #12
    Join Date
    Jul 2003
    Posts
    1220

    Re: How to rotate G-code in the G-code (to simulate 4th and 5th axis) or axis indexin

    Well done, much more complicated than I imagined.

  13. #13
    Join Date
    Sep 2010
    Posts
    1230

    Re: How to rotate G-code in the G-code (to simulate 4th and 5th axis) or axis indexin

    Quote Originally Posted by Johnsinski View Post
    The hardest parts was figuring out where the heck the center of the arc is from code with just a radius, like "g2 x4 y3 r5", of course you know the starting point, but it was still tough.
    Following is a simple method. A more elegant method, resulting in more compact code, is to use a Simultaneous Equation.
    'Example Code
    G01 X1.500 Y 12.330 (Start of Arc)
    G02 X4.0 Y3.0 R5.0 (End of Arc)

    X1 = 1.5 'X Starting Point
    Y1 = 12.330 'Y Starting Point
    X2 = 4.0 'X End from Circular Interpolation block
    Y2 = 3.0 'Y End from Circular Interpolation block
    r = 5.0 ' Radius read from Circular Interpolation block

    'Get distance between 1st and 2nd points

    q = Sqr((X2 - X1) ^ 2 + (Y2 - Y1) ^ 2)

    'Find mid point of q (X3,Y3)

    X3 = (X1 + X2) / 2
    Y3 = (Y1 + Y2) / 2

    'Find centre point of circle

    X = X3 + Sqr(r ^ 2 - (q / 2) ^ 2) * (Y1 - Y2) / q (= 4.000219)
    Y = Y3 + Sqr(r ^ 2 - (q / 2) ^ 2) * (X2 - X1) / q (= 8.0)

    X = X3 - Sqr(r ^ 2 - (q / 2) ^ 2) * (Y1 - Y2) / q (= 1.499781)
    Y = Y3 - Sqr(r ^ 2 - (q / 2) ^ 2) * (X2 - X1) / q (= 7.33)

    For every set of Start, End points and Radii, there are two solutions. The correct one is determined by whether G2 or G3 is used in the Circular Interpolation block.

    Regards,

    Bill

  14. #14
    Join Date
    Jan 2007
    Posts
    1795

    Re: How to rotate G-code in the G-code (to simulate 4th and 5th axis) or axis indexin

    youre awesome..

    hope you get time to continue posting

    best regards
    viktor

  15. #15
    Join Date
    May 2013
    Posts
    10

    Re: How to rotate G-code in the G-code (to simulate 4th and 5th axis) or axis indexin

    Quote Originally Posted by angelw View Post
    Following is a simple method. A more elegant method, resulting in more compact code, is to use a Simultaneous Equation.
    'Example Code
    G01 X1.500 Y 12.330 (Start of Arc)
    G02 X4.0 Y3.0 R5.0 (End of Arc)

    X1 = 1.5 'X Starting Point
    Y1 = 12.330 'Y Starting Point
    X2 = 4.0 'X End from Circular Interpolation block
    Y2 = 3.0 'Y End from Circular Interpolation block
    r = 5.0 ' Radius read from Circular Interpolation block

    'Get distance between 1st and 2nd points

    q = Sqr((X2 - X1) ^ 2 + (Y2 - Y1) ^ 2)

    'Find mid point of q (X3,Y3)

    X3 = (X1 + X2) / 2
    Y3 = (Y1 + Y2) / 2

    'Find centre point of circle

    X = X3 + Sqr(r ^ 2 - (q / 2) ^ 2) * (Y1 - Y2) / q (= 4.000219)
    Y = Y3 + Sqr(r ^ 2 - (q / 2) ^ 2) * (X2 - X1) / q (= 8.0)

    X = X3 - Sqr(r ^ 2 - (q / 2) ^ 2) * (Y1 - Y2) / q (= 1.499781)
    Y = Y3 - Sqr(r ^ 2 - (q / 2) ^ 2) * (X2 - X1) / q (= 7.33)

    For every set of Start, End points and Radii, there are two solutions. The correct one is determined by whether G2 or G3 is used in the Circular Interpolation block.

    Regards,

    Bill
    Hi Bill, thanks, I may try to implement your code. I initially tried to do it your way, but was having trouble with the algebraic manipulation. I'm more comfortable with trig. I always like more compact, elegant code. My first programming in a long time, it shows. ;-)

Similar Threads

  1. G-Code Ripper - Scale, Rotate and Split G-code
    By scorch in forum OpenSource Software
    Replies: 112
    Last Post: 08-26-2020, 03:28 PM
  2. 4 axis g-code help
    By THend in forum G-Code Programing
    Replies: 3
    Last Post: 03-26-2009, 12:27 AM
  3. c-axis indexing in g-code
    By Plunker in forum Mazak, Mitsubishi, Mazatrol
    Replies: 3
    Last Post: 08-13-2008, 12:39 AM
  4. G code help C-X axis
    By slkret in forum G-Code Programing
    Replies: 1
    Last Post: 05-10-2008, 02:53 PM
  5. Z-Axis Arc G-Code?
    By GTmike400 in forum G-Code Programing
    Replies: 16
    Last Post: 01-27-2006, 06:15 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •