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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://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 ENABLED(DELTA)
  29. #include "../../module/delta.h"
  30. #elif ENABLED(SCARA)
  31. #include "../../module/scara.h"
  32. #endif
  33. #if N_ARC_CORRECTION < 1
  34. #undef N_ARC_CORRECTION
  35. #define N_ARC_CORRECTION 1
  36. #endif
  37. /**
  38. * Plan an arc in 2 dimensions, with optional linear motion in a 3rd dimension
  39. *
  40. * The arc is traced by generating many small linear segments, as configured by
  41. * MM_PER_ARC_SEGMENT (Default 1mm). In the future we hope more slicers will include
  42. * an option to generate G2/G3 arcs for curved surfaces, as this will allow faster
  43. * boards to produce much smoother curved surfaces.
  44. */
  45. void plan_arc(
  46. const xyze_pos_t &cart, // Destination position
  47. const ab_float_t &offset, // Center of rotation relative to current_position
  48. const bool clockwise, // Clockwise?
  49. const uint8_t circles // Take the scenic route
  50. ) {
  51. #if ENABLED(CNC_WORKSPACE_PLANES)
  52. AxisEnum p_axis, q_axis, l_axis;
  53. switch (gcode.workspace_plane) {
  54. default:
  55. case GcodeSuite::PLANE_XY: p_axis = X_AXIS; q_axis = Y_AXIS; l_axis = Z_AXIS; break;
  56. case GcodeSuite::PLANE_YZ: p_axis = Y_AXIS; q_axis = Z_AXIS; l_axis = X_AXIS; break;
  57. case GcodeSuite::PLANE_ZX: p_axis = Z_AXIS; q_axis = X_AXIS; l_axis = Y_AXIS; break;
  58. }
  59. #else
  60. constexpr AxisEnum p_axis = X_AXIS, q_axis = Y_AXIS, l_axis = Z_AXIS;
  61. #endif
  62. // Radius vector from center to current location
  63. ab_float_t rvec = -offset;
  64. const float radius = HYPOT(rvec.a, rvec.b),
  65. center_P = current_position[p_axis] - rvec.a,
  66. center_Q = current_position[q_axis] - rvec.b,
  67. rt_X = cart[p_axis] - center_P,
  68. rt_Y = cart[q_axis] - center_Q,
  69. start_L = current_position[l_axis];
  70. #ifdef MIN_ARC_SEGMENTS
  71. uint16_t min_segments = MIN_ARC_SEGMENTS;
  72. #else
  73. constexpr uint16_t min_segments = 1;
  74. #endif
  75. // Angle of rotation between position and target from the circle center.
  76. float angular_travel;
  77. // Do a full circle if starting and ending positions are "identical"
  78. if (NEAR(current_position[p_axis], cart[p_axis]) && NEAR(current_position[q_axis], cart[q_axis])) {
  79. // Preserve direction for circles
  80. angular_travel = clockwise ? -RADIANS(360) : RADIANS(360);
  81. }
  82. else {
  83. // Calculate the angle
  84. angular_travel = ATAN2(rvec.a * rt_Y - rvec.b * rt_X, rvec.a * rt_X + rvec.b * rt_Y);
  85. // Angular travel too small to detect? Just return.
  86. if (!angular_travel) return;
  87. // Make sure angular travel over 180 degrees goes the other way around.
  88. switch (((angular_travel < 0) << 1) | clockwise) {
  89. case 1: angular_travel -= RADIANS(360); break; // Positive but CW? Reverse direction.
  90. case 2: angular_travel += RADIANS(360); break; // Negative but CCW? Reverse direction.
  91. }
  92. #ifdef MIN_ARC_SEGMENTS
  93. min_segments = CEIL(min_segments * ABS(angular_travel) / RADIANS(360));
  94. NOLESS(min_segments, 1U);
  95. #endif
  96. }
  97. float linear_travel = cart[l_axis] - start_L,
  98. extruder_travel = cart.e - current_position.e;
  99. // If circling around...
  100. if (ENABLED(ARC_P_CIRCLES) && circles) {
  101. const float total_angular = angular_travel + circles * RADIANS(360), // Total rotation with all circles and remainder
  102. part_per_circle = RADIANS(360) / total_angular, // Each circle's part of the total
  103. l_per_circle = linear_travel * part_per_circle, // L movement per circle
  104. e_per_circle = extruder_travel * part_per_circle; // E movement per circle
  105. xyze_pos_t temp_position = current_position; // for plan_arc to compare to current_position
  106. for (uint16_t n = circles; n--;) {
  107. temp_position.e += e_per_circle; // Destination E axis
  108. temp_position[l_axis] += l_per_circle; // Destination L axis
  109. plan_arc(temp_position, offset, clockwise, 0); // Plan a single whole circle
  110. }
  111. linear_travel = cart[l_axis] - current_position[l_axis];
  112. extruder_travel = cart.e - current_position.e;
  113. }
  114. const float flat_mm = radius * angular_travel,
  115. mm_of_travel = linear_travel ? HYPOT(flat_mm, linear_travel) : ABS(flat_mm);
  116. if (mm_of_travel < 0.001f) return;
  117. const feedRate_t scaled_fr_mm_s = MMS_SCALED(feedrate_mm_s);
  118. // Start with a nominal segment length
  119. float seg_length = (
  120. #ifdef ARC_SEGMENTS_PER_R
  121. constrain(MM_PER_ARC_SEGMENT * radius, MM_PER_ARC_SEGMENT, ARC_SEGMENTS_PER_R)
  122. #elif ARC_SEGMENTS_PER_SEC
  123. _MAX(scaled_fr_mm_s * RECIPROCAL(ARC_SEGMENTS_PER_SEC), MM_PER_ARC_SEGMENT)
  124. #else
  125. MM_PER_ARC_SEGMENT
  126. #endif
  127. );
  128. // Divide total travel by nominal segment length
  129. uint16_t segments = FLOOR(mm_of_travel / seg_length);
  130. NOLESS(segments, min_segments); // At least some segments
  131. seg_length = mm_of_travel / segments;
  132. /**
  133. * Vector rotation by transformation matrix: r is the original vector, r_T is the rotated vector,
  134. * and phi is the angle of rotation. Based on the solution approach by Jens Geisler.
  135. * r_T = [cos(phi) -sin(phi);
  136. * sin(phi) cos(phi)] * r ;
  137. *
  138. * For arc generation, the center of the circle is the axis of rotation and the radius vector is
  139. * defined from the circle center to the initial position. Each line segment is formed by successive
  140. * vector rotations. This requires only two cos() and sin() computations to form the rotation
  141. * matrix for the duration of the entire arc. Error may accumulate from numerical round-off, since
  142. * all double numbers are single precision on the Arduino. (True double precision will not have
  143. * round off issues for CNC applications.) Single precision error can accumulate to be greater than
  144. * tool precision in some cases. Therefore, arc path correction is implemented.
  145. *
  146. * Small angle approximation may be used to reduce computation overhead further. This approximation
  147. * holds for everything, but very small circles and large MM_PER_ARC_SEGMENT values. In other words,
  148. * theta_per_segment would need to be greater than 0.1 rad and N_ARC_CORRECTION would need to be large
  149. * to cause an appreciable drift error. N_ARC_CORRECTION~=25 is more than small enough to correct for
  150. * numerical drift error. N_ARC_CORRECTION may be on the order a hundred(s) before error becomes an
  151. * issue for CNC machines with the single precision Arduino calculations.
  152. *
  153. * This approximation also allows plan_arc to immediately insert a line segment into the planner
  154. * without the initial overhead of computing cos() or sin(). By the time the arc needs to be applied
  155. * a correction, the planner should have caught up to the lag caused by the initial plan_arc overhead.
  156. * This is important when there are successive arc motions.
  157. */
  158. // Vector rotation matrix values
  159. xyze_pos_t raw;
  160. const float theta_per_segment = angular_travel / segments,
  161. linear_per_segment = linear_travel / segments,
  162. extruder_per_segment = extruder_travel / segments,
  163. sq_theta_per_segment = sq(theta_per_segment),
  164. sin_T = theta_per_segment - sq_theta_per_segment * theta_per_segment / 6,
  165. cos_T = 1 - 0.5f * sq_theta_per_segment; // Small angle approximation
  166. // Initialize the linear axis
  167. raw[l_axis] = current_position[l_axis];
  168. // Initialize the extruder axis
  169. raw.e = current_position.e;
  170. #if ENABLED(SCARA_FEEDRATE_SCALING)
  171. const float inv_duration = scaled_fr_mm_s / seg_length;
  172. #endif
  173. millis_t next_idle_ms = millis() + 200UL;
  174. #if N_ARC_CORRECTION > 1
  175. int8_t arc_recalc_count = N_ARC_CORRECTION;
  176. #endif
  177. for (uint16_t i = 1; i < segments; i++) { // Iterate (segments-1) times
  178. thermalManager.manage_heater();
  179. if (ELAPSED(millis(), next_idle_ms)) {
  180. next_idle_ms = millis() + 200UL;
  181. idle();
  182. }
  183. #if N_ARC_CORRECTION > 1
  184. if (--arc_recalc_count) {
  185. // Apply vector rotation matrix to previous rvec.a / 1
  186. const float r_new_Y = rvec.a * sin_T + rvec.b * cos_T;
  187. rvec.a = rvec.a * cos_T - rvec.b * sin_T;
  188. rvec.b = r_new_Y;
  189. }
  190. else
  191. #endif
  192. {
  193. #if N_ARC_CORRECTION > 1
  194. arc_recalc_count = N_ARC_CORRECTION;
  195. #endif
  196. // Arc correction to radius vector. Computed only every N_ARC_CORRECTION increments.
  197. // Compute exact location by applying transformation matrix from initial radius vector(=-offset).
  198. // To reduce stuttering, the sin and cos could be computed at different times.
  199. // For now, compute both at the same time.
  200. const float cos_Ti = cos(i * theta_per_segment), sin_Ti = sin(i * theta_per_segment);
  201. rvec.a = -offset[0] * cos_Ti + offset[1] * sin_Ti;
  202. rvec.b = -offset[0] * sin_Ti - offset[1] * cos_Ti;
  203. }
  204. // Update raw location
  205. raw[p_axis] = center_P + rvec.a;
  206. raw[q_axis] = center_Q + rvec.b;
  207. #if ENABLED(AUTO_BED_LEVELING_UBL)
  208. raw[l_axis] = start_L;
  209. UNUSED(linear_per_segment);
  210. #else
  211. raw[l_axis] += linear_per_segment;
  212. #endif
  213. raw.e += extruder_per_segment;
  214. apply_motion_limits(raw);
  215. #if HAS_LEVELING && !PLANNER_LEVELING
  216. planner.apply_leveling(raw);
  217. #endif
  218. if (!planner.buffer_line(raw, scaled_fr_mm_s, active_extruder, 0
  219. #if ENABLED(SCARA_FEEDRATE_SCALING)
  220. , inv_duration
  221. #endif
  222. )) break;
  223. }
  224. // Ensure last segment arrives at target location.
  225. raw = cart;
  226. TERN_(AUTO_BED_LEVELING_UBL, raw[l_axis] = start_L);
  227. apply_motion_limits(raw);
  228. #if HAS_LEVELING && !PLANNER_LEVELING
  229. planner.apply_leveling(raw);
  230. #endif
  231. planner.buffer_line(raw, scaled_fr_mm_s, active_extruder, 0
  232. #if ENABLED(SCARA_FEEDRATE_SCALING)
  233. , inv_duration
  234. #endif
  235. );
  236. TERN_(AUTO_BED_LEVELING_UBL, raw[l_axis] = start_L);
  237. current_position = raw;
  238. } // plan_arc
  239. /**
  240. * G2: Clockwise Arc
  241. * G3: Counterclockwise Arc
  242. *
  243. * This command has two forms: IJ-form (JK, KI) and R-form.
  244. *
  245. * - Depending on the current Workspace Plane orientation,
  246. * use parameters IJ/JK/KI to specify the XY/YZ/ZX offsets.
  247. * At least one of the IJ/JK/KI parameters is required.
  248. * XY/YZ/ZX can be omitted to do a complete circle.
  249. * The given XY/YZ/ZX is not error-checked. The arc ends
  250. * based on the angle of the destination.
  251. * Mixing IJ/JK/KI with R will throw an error.
  252. *
  253. * - R specifies the radius. X or Y (Y or Z / Z or X) is required.
  254. * Omitting both XY/YZ/ZX will throw an error.
  255. * XY/YZ/ZX must differ from the current XY/YZ/ZX.
  256. * Mixing R with IJ/JK/KI will throw an error.
  257. *
  258. * - P specifies the number of full circles to do
  259. * before the specified arc move.
  260. *
  261. * Examples:
  262. *
  263. * G2 I10 ; CW circle centered at X+10
  264. * G3 X20 Y12 R14 ; CCW circle with r=14 ending at X20 Y12
  265. */
  266. void GcodeSuite::G2_G3(const bool clockwise) {
  267. if (MOTION_CONDITIONS) {
  268. #if ENABLED(SF_ARC_FIX)
  269. const bool relative_mode_backup = relative_mode;
  270. relative_mode = true;
  271. #endif
  272. get_destination_from_command(); // Get X Y Z E F (and set cutter power)
  273. TERN_(SF_ARC_FIX, relative_mode = relative_mode_backup);
  274. ab_float_t arc_offset = { 0, 0 };
  275. if (parser.seenval('R')) {
  276. const float r = parser.value_linear_units();
  277. if (r) {
  278. const xy_pos_t p1 = current_position, p2 = destination;
  279. if (p1 != p2) {
  280. const xy_pos_t d2 = (p2 - p1) * 0.5f; // XY vector to midpoint of move from current
  281. const float e = clockwise ^ (r < 0) ? -1 : 1, // clockwise -1/1, counterclockwise 1/-1
  282. len = d2.magnitude(), // Distance to mid-point of move from current
  283. h2 = (r - len) * (r + len), // factored to reduce rounding error
  284. h = (h2 >= 0) ? SQRT(h2) : 0.0f; // Distance to the arc pivot-point from midpoint
  285. const xy_pos_t s = { -d2.y, d2.x }; // Perpendicular bisector. (Divide by len for unit vector.)
  286. arc_offset = d2 + s / len * e * h; // The calculated offset (mid-point if |r| <= len)
  287. }
  288. }
  289. }
  290. else {
  291. #if ENABLED(CNC_WORKSPACE_PLANES)
  292. char achar, bchar;
  293. switch (gcode.workspace_plane) {
  294. default:
  295. case GcodeSuite::PLANE_XY: achar = 'I'; bchar = 'J'; break;
  296. case GcodeSuite::PLANE_YZ: achar = 'J'; bchar = 'K'; break;
  297. case GcodeSuite::PLANE_ZX: achar = 'K'; bchar = 'I'; break;
  298. }
  299. #else
  300. constexpr char achar = 'I', bchar = 'J';
  301. #endif
  302. if (parser.seenval(achar)) arc_offset.a = parser.value_linear_units();
  303. if (parser.seenval(bchar)) arc_offset.b = parser.value_linear_units();
  304. }
  305. if (arc_offset) {
  306. #if ENABLED(ARC_P_CIRCLES)
  307. // P indicates number of circles to do
  308. const int8_t circles_to_do = parser.byteval('P');
  309. if (!WITHIN(circles_to_do, 0, 100))
  310. SERIAL_ERROR_MSG(STR_ERR_ARC_ARGS);
  311. #else
  312. constexpr uint8_t circles_to_do = 0;
  313. #endif
  314. // Send the arc to the planner
  315. plan_arc(destination, arc_offset, clockwise, circles_to_do);
  316. reset_stepper_timeout();
  317. }
  318. else
  319. SERIAL_ERROR_MSG(STR_ERR_ARC_ARGS);
  320. }
  321. }
  322. #endif // ARC_SUPPORT