1. Hey guyz. Welcome to the All New Phlatforum!



    Sign Up and take a look around. There are so many awesome new features.

    The Phlatforum is a place we can all hang out and

    have fun sharing our RC adventures!

  2. Dismiss Notice

G2, G3 in Marlin firmware, a maths challenge

Discussion in 'SketchUcam 'Most wanted Feature'' started by blamarpa, Jun 3, 2015.

  1. blamarpa

    blamarpa New Member

    Offline
    Messages:
    6
    Trophy Points:
    1
    Location:
    Valladolid - SPAIN
    Hy boyzs!
    I have a mathematical challenge. sketchUcam sends g-code arcs as radius format acr like this:
    G01 X1 Y2
    G02 X2 Y1 R1
    So this is wath happens:
    [​IMG]
    but my Marlin 3d printer converted in a little CNC taking out the extruder an putting a little motor as a drill, dont understand this. It likes to use center format arc:
    G02 X2 Y1 I0 J-1​
    So when the new option "Force all Gcodes for Marling" is set to "true", the center of the arc has to be calculated in order to obtain the I and J parameters.
    the start of each arc is (cx,xy) and the end is (ox,oy) so the center (centerx, centery) are in one of the two intersections betwen this circunferences:
    (centerx-cx)^2 + (centery-cy)^2 = R
    (centerx-ox)^2 + (centery-oy)^2 = R​
    the positive or negative value of R defines wich of the two possible centers we want and with the use of G2 or G3 defines in wich of the four posible arcs we want to move the drill.
    [​IMG]
    Examining PhlatMill.rb, I have seen that there is two functions:
    def arcmove(xo, yo=@cy, radius=0, g3=false, zo=@cz, so=@speed_curr, cmd=@cmd_arc)​
    and
    def arcmoveij(xo, yo, centerx,centery, g3=false, zo=@cz, so=@speed_curr, cmd=@cmd_arc)​

    But I have'nt found where ScetchUcam can be configured to use the second sintaxis, so I think it is an "in progress" feature. ¿Any help? thanks in advance.
     
  2. swarfer

    swarfer Moderator Staff Member

    Offline
    Messages:
    808
    Trophy Points:
    28
    Location:
    Grahamstown, South Africa
    the ij format works fine for complete circles, or arcs that have not been split, ie a circle or arc drawn, and then split anywhere except an arc segment intersection.
    if they have been split then Sketchup reports incorrect endpoints , the endpoints are not on the circle but rather on the chord that Sketchup *displays*. using IJ for this situation then generates errors in the Gcode controller because the end points do not match the radius, this is a fatal error and processing stops.
    therefore, IJ cannot be used for arc output at this time.

    what you want to do is to
    a) create arcs with more segments, say 48 to a circle (should be a multiple of 4). the bigger the circle the more segments you use so that
    the chords do not get so big you can see them in the cuts
    b) explode all arcs before creating cut lines
    the result is that all the gcode output is straight segments and you will not have any arc strangeness

    again I say it 'Marlin needs to be fixed, not SketchUcam' (-:
     
  3. blamarpa

    blamarpa New Member

    Offline
    Messages:
    6
    Trophy Points:
    1
    Location:
    Valladolid - SPAIN
    Ok, it seens more easy than i tougth. Learming some ruby, viewing a pair of Google videos talking about Sketchup programing and reading the ArcCurve page in the Sketchup api reference website (http://www.sketchup.com/intl/en/developer/docs/ourdoc/arccurve), , I have found that it is as easy as get phlatcut.center.x phlatcut.center.y and also, of course, phlatcut.center.z when phlatcut is an arc.
    I will play wit it to see if incorrects endpoints can be fixed in any way.
    Thanks for your help. My little "cnc" can only work in 25x25 cm and exploding and using more segmented arcs can be a valid trick doing small arcs that don't need precission. I have read that Marlin internaly divides arcs in little segments too.
     
  4. swarfer

    swarfer Moderator Staff Member

    Offline
    Messages:
    808
    Trophy Points:
    28
    Location:
    Grahamstown, South Africa
    the only way to draw a circle on an XY machine is by using the 'split it into segments' method, same as on your screen, the Windows graphics library just splits it into pixel sized segments so they look smooth. on a thing like Marlin I would split into 'machine resolution' segments, made a lot more complicated if X and Y have different resolutions. then I'd use the larger one.

    I have already modified the code to be able to use the IJ format function call, and reinstated the storage of the center point which you need for that. it even gets transformed already! (but commented in one place)

    so, in gcodeutil.rb all you need to do is replace calls to arcmove() with calls to arcmoveij() with the correct parameters. around line 1548 you will need to uncomment the tcenter stuff so you have the transformed center point available.

    having done that you will need to make sure that in your drawings any circle/arc that you draw and then split on an intersection with a chord gets redrawn, ie delete it and redraw it as one arc from endpoint to endpoint. This will ensure that the first and last segments have the correct co-ordinates that are actually on the radius. OR you can do the math to recalculate them. which may not be necessary at all since I doubt Marlin does any real checking of the arc parameters and will not moan if they do not match up, happily drawing the wrong arc instead (-:

    I ditched the IJ routine because the Planet-cnc usb controller and LinuxCNC both (and others I am sure) detect errors in the arc parameters and I believe using R format which 'works everywhere' is a better solution than trying to get everyone to draw arcs correctly as described above so that the code works. and recalculating the end points is more math than I want to do, I am a programmer not a mathematician (-:
    (yes I know giving the incorrect end point to the R format arc will result in the 'wrong arc' beign drawn, ie the controller calculates the center point and it will not match the actual drawings center point. However, I challenge you to produce a drawing where this effect can be easily seen in the cut output :) The goal here is Gcode that 'works everywhere' not super duper accurate Gcode for making spaceship components.)

    hmmm, strikes me this will only work this 'simply' if you do not use ramping, since the ramping code would also have to be changed to use IJ format. that is another layer or three of complexity. good luck with that (-:

    as to how many segments to use..... do not go too small since your Marlin buffer is limited and will run out and pause the cut while it waits for new lines to be sent. http://forums.reprap.org/read.php?1,169532
    pausing the cut will leave a mark. I have tested using 3 segments per arc, so in Sketchup it looks like 3 big flat sides, but the gcode has 3 arc commands which cut a perfect circle and reduces code size a lot.

    have fun
     
  5. blamarpa

    blamarpa New Member

    Offline
    Messages:
    6
    Trophy Points:
    1
    Location:
    Valladolid - SPAIN
    Yes because that intersection is in the segment of the arc and not in the real arc in 99.9%.
    I have take a picture if anibody reading this wants to understand why this happens.
    Correct me if I am wrong, please.
    It is something like this: you can see were the intersection is but really it is in a poligon and not in the arc because Sketchup divides each arc in x segments and cheat our eyes but not your cnc. An arc centered at one point drawed from origin will not always find this final point because it is not in the arc so a :banghead:complete circle will be drawed and then (at least my Marlin) goes to final point in :nailbiting: lineal movement.
    Real final point is not that but is in the line from center to bad_final_pint at a distance of R from center.:dance: (I'm not dancing,
    I'm wiping the sweat from myforehead).
    I have to do a few calculus to test if it is so.

    [​IMG]
     

Share This Page