My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

G2_G3.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../../inc/MarlinConfig.h"
  23. #if ENABLED(ARC_SUPPORT)
  24. #include "../gcode.h"
  25. #include "../../module/motion.h"
  26. #include "../../module/planner.h"
  27. #include "../../module/temperature.h"
  28. #if N_ARC_CORRECTION < 1
  29. #undef N_ARC_CORRECTION
  30. #define N_ARC_CORRECTION 1
  31. #endif
  32. /**
  33. * Plan an arc in 2 dimensions
  34. *
  35. * The arc is approximated by generating many small linear segments.
  36. * The length of each segment is configured in MM_PER_ARC_SEGMENT (Default 1mm)
  37. * Arcs should only be made relatively large (over 5mm), as larger arcs with
  38. * larger segments will tend to be more efficient. Your slicer should have
  39. * options for G2/G3 arc generation. In future these options may be GCode tunable.
  40. */
  41. void plan_arc(
  42. float logical[XYZE], // Destination position
  43. float *offset, // Center of rotation relative to current_position
  44. uint8_t clockwise // Clockwise?
  45. ) {
  46. #if ENABLED(CNC_WORKSPACE_PLANES)
  47. AxisEnum p_axis, q_axis, l_axis;
  48. switch (workspace_plane) {
  49. case PLANE_XY: p_axis = X_AXIS; q_axis = Y_AXIS; l_axis = Z_AXIS; break;
  50. case PLANE_ZX: p_axis = Z_AXIS; q_axis = X_AXIS; l_axis = Y_AXIS; break;
  51. case PLANE_YZ: p_axis = Y_AXIS; q_axis = Z_AXIS; l_axis = X_AXIS; break;
  52. }
  53. #else
  54. constexpr AxisEnum p_axis = X_AXIS, q_axis = Y_AXIS, l_axis = Z_AXIS;
  55. #endif
  56. // Radius vector from center to current location
  57. float r_P = -offset[0], r_Q = -offset[1];
  58. const float radius = HYPOT(r_P, r_Q),
  59. center_P = current_position[p_axis] - r_P,
  60. center_Q = current_position[q_axis] - r_Q,
  61. rt_X = logical[p_axis] - center_P,
  62. rt_Y = logical[q_axis] - center_Q,
  63. linear_travel = logical[l_axis] - current_position[l_axis],
  64. extruder_travel = logical[E_AXIS] - current_position[E_AXIS];
  65. // CCW angle of rotation between position and target from the circle center. Only one atan2() trig computation required.
  66. float angular_travel = ATAN2(r_P * rt_Y - r_Q * rt_X, r_P * rt_X + r_Q * rt_Y);
  67. if (angular_travel < 0) angular_travel += RADIANS(360);
  68. if (clockwise) angular_travel -= RADIANS(360);
  69. // Make a circle if the angular rotation is 0 and the target is current position
  70. if (angular_travel == 0 && current_position[p_axis] == logical[p_axis] && current_position[q_axis] == logical[q_axis])
  71. angular_travel = RADIANS(360);
  72. const float mm_of_travel = HYPOT(angular_travel * radius, FABS(linear_travel));
  73. if (mm_of_travel < 0.001) return;
  74. uint16_t segments = FLOOR(mm_of_travel / (MM_PER_ARC_SEGMENT));
  75. if (segments == 0) segments = 1;
  76. /**
  77. * Vector rotation by transformation matrix: r is the original vector, r_T is the rotated vector,
  78. * and phi is the angle of rotation. Based on the solution approach by Jens Geisler.
  79. * r_T = [cos(phi) -sin(phi);
  80. * sin(phi) cos(phi)] * r ;
  81. *
  82. * For arc generation, the center of the circle is the axis of rotation and the radius vector is
  83. * defined from the circle center to the initial position. Each line segment is formed by successive
  84. * vector rotations. This requires only two cos() and sin() computations to form the rotation
  85. * matrix for the duration of the entire arc. Error may accumulate from numerical round-off, since
  86. * all double numbers are single precision on the Arduino. (True double precision will not have
  87. * round off issues for CNC applications.) Single precision error can accumulate to be greater than
  88. * tool precision in some cases. Therefore, arc path correction is implemented.
  89. *
  90. * Small angle approximation may be used to reduce computation overhead further. This approximation
  91. * holds for everything, but very small circles and large MM_PER_ARC_SEGMENT values. In other words,
  92. * theta_per_segment would need to be greater than 0.1 rad and N_ARC_CORRECTION would need to be large
  93. * to cause an appreciable drift error. N_ARC_CORRECTION~=25 is more than small enough to correct for
  94. * numerical drift error. N_ARC_CORRECTION may be on the order a hundred(s) before error becomes an
  95. * issue for CNC machines with the single precision Arduino calculations.
  96. *
  97. * This approximation also allows plan_arc to immediately insert a line segment into the planner
  98. * without the initial overhead of computing cos() or sin(). By the time the arc needs to be applied
  99. * a correction, the planner should have caught up to the lag caused by the initial plan_arc overhead.
  100. * This is important when there are successive arc motions.
  101. */
  102. // Vector rotation matrix values
  103. float arc_target[XYZE];
  104. const float theta_per_segment = angular_travel / segments,
  105. linear_per_segment = linear_travel / segments,
  106. extruder_per_segment = extruder_travel / segments,
  107. sin_T = theta_per_segment,
  108. cos_T = 1 - 0.5 * sq(theta_per_segment); // Small angle approximation
  109. // Initialize the linear axis
  110. arc_target[l_axis] = current_position[l_axis];
  111. // Initialize the extruder axis
  112. arc_target[E_AXIS] = current_position[E_AXIS];
  113. const float fr_mm_s = MMS_SCALED(feedrate_mm_s);
  114. millis_t next_idle_ms = millis() + 200UL;
  115. #if N_ARC_CORRECTION > 1
  116. int8_t arc_recalc_count = N_ARC_CORRECTION;
  117. #endif
  118. for (uint16_t i = 1; i < segments; i++) { // Iterate (segments-1) times
  119. thermalManager.manage_heater();
  120. if (ELAPSED(millis(), next_idle_ms)) {
  121. next_idle_ms = millis() + 200UL;
  122. idle();
  123. }
  124. #if N_ARC_CORRECTION > 1
  125. if (--arc_recalc_count) {
  126. // Apply vector rotation matrix to previous r_P / 1
  127. const float r_new_Y = r_P * sin_T + r_Q * cos_T;
  128. r_P = r_P * cos_T - r_Q * sin_T;
  129. r_Q = r_new_Y;
  130. }
  131. else
  132. #endif
  133. {
  134. #if N_ARC_CORRECTION > 1
  135. arc_recalc_count = N_ARC_CORRECTION;
  136. #endif
  137. // Arc correction to radius vector. Computed only every N_ARC_CORRECTION increments.
  138. // Compute exact location by applying transformation matrix from initial radius vector(=-offset).
  139. // To reduce stuttering, the sin and cos could be computed at different times.
  140. // For now, compute both at the same time.
  141. const float cos_Ti = cos(i * theta_per_segment), sin_Ti = sin(i * theta_per_segment);
  142. r_P = -offset[0] * cos_Ti + offset[1] * sin_Ti;
  143. r_Q = -offset[0] * sin_Ti - offset[1] * cos_Ti;
  144. }
  145. // Update arc_target location
  146. arc_target[p_axis] = center_P + r_P;
  147. arc_target[q_axis] = center_Q + r_Q;
  148. arc_target[l_axis] += linear_per_segment;
  149. arc_target[E_AXIS] += extruder_per_segment;
  150. clamp_to_software_endstops(arc_target);
  151. planner.buffer_line_kinematic(arc_target, fr_mm_s, active_extruder);
  152. }
  153. // Ensure last segment arrives at target location.
  154. planner.buffer_line_kinematic(logical, fr_mm_s, active_extruder);
  155. // As far as the parser is concerned, the position is now == target. In reality the
  156. // motion control system might still be processing the action and the real tool position
  157. // in any intermediate location.
  158. set_current_to_destination();
  159. } // plan_arc
  160. /**
  161. * G2: Clockwise Arc
  162. * G3: Counterclockwise Arc
  163. *
  164. * This command has two forms: IJ-form and R-form.
  165. *
  166. * - I specifies an X offset. J specifies a Y offset.
  167. * At least one of the IJ parameters is required.
  168. * X and Y can be omitted to do a complete circle.
  169. * The given XY is not error-checked. The arc ends
  170. * based on the angle of the destination.
  171. * Mixing I or J with R will throw an error.
  172. *
  173. * - R specifies the radius. X or Y is required.
  174. * Omitting both X and Y will throw an error.
  175. * X or Y must differ from the current XY.
  176. * Mixing R with I or J will throw an error.
  177. *
  178. * - P specifies the number of full circles to do
  179. * before the specified arc move.
  180. *
  181. * Examples:
  182. *
  183. * G2 I10 ; CW circle centered at X+10
  184. * G3 X20 Y12 R14 ; CCW circle with r=14 ending at X20 Y12
  185. */
  186. void GcodeSuite::G2_G3(const bool clockwise) {
  187. if (IsRunning()) {
  188. #if ENABLED(SF_ARC_FIX)
  189. const bool relative_mode_backup = relative_mode;
  190. relative_mode = true;
  191. #endif
  192. get_destination_from_command();
  193. #if ENABLED(SF_ARC_FIX)
  194. relative_mode = relative_mode_backup;
  195. #endif
  196. float arc_offset[2] = { 0.0, 0.0 };
  197. if (parser.seenval('R')) {
  198. const float r = parser.value_linear_units(),
  199. p1 = current_position[X_AXIS], q1 = current_position[Y_AXIS],
  200. p2 = destination[X_AXIS], q2 = destination[Y_AXIS];
  201. if (r && (p2 != p1 || q2 != q1)) {
  202. const float e = clockwise ^ (r < 0) ? -1 : 1, // clockwise -1/1, counterclockwise 1/-1
  203. dx = p2 - p1, dy = q2 - q1, // X and Y differences
  204. d = HYPOT(dx, dy), // Linear distance between the points
  205. h = SQRT(sq(r) - sq(d * 0.5)), // Distance to the arc pivot-point
  206. mx = (p1 + p2) * 0.5, my = (q1 + q2) * 0.5, // Point between the two points
  207. sx = -dy / d, sy = dx / d, // Slope of the perpendicular bisector
  208. cx = mx + e * h * sx, cy = my + e * h * sy; // Pivot-point of the arc
  209. arc_offset[0] = cx - p1;
  210. arc_offset[1] = cy - q1;
  211. }
  212. }
  213. else {
  214. if (parser.seenval('I')) arc_offset[0] = parser.value_linear_units();
  215. if (parser.seenval('J')) arc_offset[1] = parser.value_linear_units();
  216. }
  217. if (arc_offset[0] || arc_offset[1]) {
  218. #if ENABLED(ARC_P_CIRCLES)
  219. // P indicates number of circles to do
  220. int8_t circles_to_do = parser.byteval('P');
  221. if (!WITHIN(circles_to_do, 0, 100)) {
  222. SERIAL_ERROR_START();
  223. SERIAL_ERRORLNPGM(MSG_ERR_ARC_ARGS);
  224. }
  225. while (circles_to_do--)
  226. plan_arc(current_position, arc_offset, clockwise);
  227. #endif
  228. // Send the arc to the planner
  229. plan_arc(destination, arc_offset, clockwise);
  230. refresh_cmd_timeout();
  231. }
  232. else {
  233. // Bad arguments
  234. SERIAL_ERROR_START();
  235. SERIAL_ERRORLNPGM(MSG_ERR_ARC_ARGS);
  236. }
  237. }
  238. }
  239. #endif // ARC_SUPPORT