Browse Source

Merge pull request #4840 from thinkyhead/rc_G2_G3_arc_r_method

Add support for G2/G3 with R parameter
Scott Lahteine 8 years ago
parent
commit
2b7cc27c37
2 changed files with 51 additions and 9 deletions
  1. 50
    9
      Marlin/Marlin_main.cpp
  2. 1
    0
      Marlin/language.h

+ 50
- 9
Marlin/Marlin_main.cpp View File

@@ -2590,6 +2590,25 @@ inline void gcode_G0_G1(
2590 2590
 /**
2591 2591
  * G2: Clockwise Arc
2592 2592
  * G3: Counterclockwise Arc
2593
+ *
2594
+ * This command has two forms: IJ-form and R-form.
2595
+ *
2596
+ *  - I specifies an X offset. J specifies a Y offset.
2597
+ *    At least one of the IJ parameters is required.
2598
+ *    X and Y can be omitted to do a complete circle.
2599
+ *    The given XY is not error-checked. The arc ends
2600
+ *     based on the angle of the destination.
2601
+ *    Mixing I or J with R will throw an error.
2602
+ *
2603
+ *  - R specifies the radius. X or Y is required.
2604
+ *    Omitting both X and Y will throw an error.
2605
+ *    X or Y must differ from the current XY.
2606
+ *    Mixing R with I or J will throw an error.
2607
+ *
2608
+ *  Examples:
2609
+ *
2610
+ *    G2 I10           ; CW circle centered at X+10
2611
+ *    G3 X20 Y12 R14   ; CCW circle with r=14 ending at X20 Y12
2593 2612
  */
2594 2613
 #if ENABLED(ARC_SUPPORT)
2595 2614
   inline void gcode_G2_G3(bool clockwise) {
@@ -2606,16 +2625,38 @@ inline void gcode_G0_G1(
2606 2625
         relative_mode = relative_mode_backup;
2607 2626
       #endif
2608 2627
 
2609
-      // Center of arc as offset from current_position
2610
-      float arc_offset[2] = {
2611
-        code_seen('I') ? code_value_axis_units(X_AXIS) : 0,
2612
-        code_seen('J') ? code_value_axis_units(Y_AXIS) : 0
2613
-      };
2614
-
2615
-      // Send an arc to the planner
2616
-      plan_arc(destination, arc_offset, clockwise);
2628
+      float arc_offset[2] = { 0.0, 0.0 };
2629
+      if (code_seen('R')) {
2630
+        const float r = code_value_axis_units(X_AXIS),
2631
+                    x1 = current_position[X_AXIS], y1 = current_position[Y_AXIS],
2632
+                    x2 = destination[X_AXIS], y2 = destination[Y_AXIS];
2633
+        if (r && (x2 != x1 || y2 != y1)) {
2634
+          const float e = clockwise ? -1 : 1,                     // clockwise -1, counterclockwise 1
2635
+                      dx = x2 - x1, dy = y2 - y1,                 // X and Y differences
2636
+                      d = HYPOT(dx, dy),                          // Linear distance between the points
2637
+                      h = sqrt(sq(r) - sq(d * 0.5)),              // Distance to the arc pivot-point
2638
+                      mx = (x1 + x2) * 0.5, my = (y1 + y2) * 0.5, // Point between the two points
2639
+                      sx = -dy / d, sy = dx / d,                  // Slope of the perpendicular bisector
2640
+                      cx = mx + e * h * sx, cy = my + e * h * sy; // Pivot-point of the arc
2641
+          arc_offset[X_AXIS] = cx - x1;
2642
+          arc_offset[Y_AXIS] = cy - y1;
2643
+        }
2644
+      }
2645
+      else {
2646
+        if (code_seen('I')) arc_offset[X_AXIS] = code_value_axis_units(X_AXIS);
2647
+        if (code_seen('J')) arc_offset[Y_AXIS] = code_value_axis_units(Y_AXIS);
2648
+      }
2617 2649
 
2618
-      refresh_cmd_timeout();
2650
+      if (arc_offset[0] || arc_offset[1]) {
2651
+        // Send an arc to the planner
2652
+        plan_arc(destination, arc_offset, clockwise);
2653
+        refresh_cmd_timeout();
2654
+      }
2655
+      else {
2656
+        // Bad arguments
2657
+        SERIAL_ERROR_START;
2658
+        SERIAL_ERRORLNPGM(MSG_ERR_ARC_ARGS);
2659
+      }
2619 2660
     }
2620 2661
   }
2621 2662
 #endif

+ 1
- 0
Marlin/language.h View File

@@ -150,6 +150,7 @@
150 150
 #define MSG_ERR_MATERIAL_INDEX              "M145 S<index> out of range (0-1)"
151 151
 #define MSG_ERR_M421_PARAMETERS             "M421 requires XYZ or IJZ parameters"
152 152
 #define MSG_ERR_MESH_XY                     "Mesh XY or IJ cannot be resolved"
153
+#define MSG_ERR_ARC_ARGS                    "G2/G3 bad parameters"
153 154
 #define MSG_ERR_M428_TOO_FAR                "Too far from reference point"
154 155
 #define MSG_ERR_M303_DISABLED               "PIDTEMP disabled"
155 156
 #define MSG_M119_REPORT                     "Reporting endstop status"

Loading…
Cancel
Save