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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. #ifndef MIN_CIRCLE_SEGMENTS
  38. #define MIN_CIRCLE_SEGMENTS 72 // 5° per segment
  39. #endif
  40. #if !defined(MAX_ARC_SEGMENT_MM) && defined(MIN_ARC_SEGMENT_MM)
  41. #define MAX_ARC_SEGMENT_MM MIN_ARC_SEGMENT_MM
  42. #elif !defined(MIN_ARC_SEGMENT_MM) && defined(MAX_ARC_SEGMENT_MM)
  43. #define MIN_ARC_SEGMENT_MM MAX_ARC_SEGMENT_MM
  44. #endif
  45. #define ARC_LIJKUVW_CODE(L,I,J,K,U,V,W) CODE_N(SUB2(NUM_AXES),L,I,J,K,U,V,W)
  46. #define ARC_LIJKUVWE_CODE(L,I,J,K,U,V,W,E) ARC_LIJKUVW_CODE(L,I,J,K,U,V,W); CODE_ITEM_E(E)
  47. /**
  48. * Plan an arc in 2 dimensions, with linear motion in the other axes.
  49. * The arc is traced with many small linear segments according to the configuration.
  50. */
  51. void plan_arc(
  52. const xyze_pos_t &cart, // Destination position
  53. const ab_float_t &offset, // Center of rotation relative to current_position
  54. const bool clockwise, // Clockwise?
  55. const uint8_t circles // Take the scenic route
  56. ) {
  57. #if ENABLED(CNC_WORKSPACE_PLANES)
  58. AxisEnum axis_p, axis_q, axis_l;
  59. switch (gcode.workspace_plane) {
  60. default:
  61. case GcodeSuite::PLANE_XY: axis_p = X_AXIS; axis_q = Y_AXIS; axis_l = Z_AXIS; break;
  62. case GcodeSuite::PLANE_YZ: axis_p = Y_AXIS; axis_q = Z_AXIS; axis_l = X_AXIS; break;
  63. case GcodeSuite::PLANE_ZX: axis_p = Z_AXIS; axis_q = X_AXIS; axis_l = Y_AXIS; break;
  64. }
  65. #else
  66. constexpr AxisEnum axis_p = X_AXIS, axis_q = Y_AXIS OPTARG(HAS_Z_AXIS, axis_l = Z_AXIS);
  67. #endif
  68. // Radius vector from center to current location
  69. ab_float_t rvec = -offset;
  70. const float radius = HYPOT(rvec.a, rvec.b),
  71. center_P = current_position[axis_p] - rvec.a,
  72. center_Q = current_position[axis_q] - rvec.b,
  73. rt_X = cart[axis_p] - center_P,
  74. rt_Y = cart[axis_q] - center_Q;
  75. ARC_LIJKUVW_CODE(
  76. const float start_L = current_position[axis_l],
  77. const float start_I = current_position.i,
  78. const float start_J = current_position.j,
  79. const float start_K = current_position.k,
  80. const float start_U = current_position.u,
  81. const float start_V = current_position.v,
  82. const float start_W = current_position.w
  83. );
  84. // Angle of rotation between position and target from the circle center.
  85. float angular_travel, abs_angular_travel;
  86. // Minimum number of segments in an arc move
  87. uint16_t min_segments = 1;
  88. // Do a full circle if starting and ending positions are "identical"
  89. if (NEAR(current_position[axis_p], cart[axis_p]) && NEAR(current_position[axis_q], cart[axis_q])) {
  90. // Preserve direction for circles
  91. angular_travel = clockwise ? -RADIANS(360) : RADIANS(360);
  92. abs_angular_travel = RADIANS(360);
  93. min_segments = MIN_CIRCLE_SEGMENTS;
  94. }
  95. else {
  96. // Calculate the angle
  97. angular_travel = ATAN2(rvec.a * rt_Y - rvec.b * rt_X, rvec.a * rt_X + rvec.b * rt_Y);
  98. // Angular travel too small to detect? Just return.
  99. if (!angular_travel) return;
  100. // Make sure angular travel over 180 degrees goes the other way around.
  101. switch (((angular_travel < 0) << 1) | clockwise) {
  102. case 1: angular_travel -= RADIANS(360); break; // Positive but CW? Reverse direction.
  103. case 2: angular_travel += RADIANS(360); break; // Negative but CCW? Reverse direction.
  104. }
  105. abs_angular_travel = ABS(angular_travel);
  106. // Apply minimum segments to the arc
  107. const float portion_of_circle = abs_angular_travel / RADIANS(360); // Portion of a complete circle (0 < N < 1)
  108. min_segments = CEIL((MIN_CIRCLE_SEGMENTS) * portion_of_circle); // Minimum segments for the arc
  109. }
  110. ARC_LIJKUVWE_CODE(
  111. float travel_L = cart[axis_l] - start_L,
  112. float travel_I = cart.i - start_I,
  113. float travel_J = cart.j - start_J,
  114. float travel_K = cart.k - start_K,
  115. float travel_U = cart.u - start_U,
  116. float travel_V = cart.v - start_V,
  117. float travel_W = cart.w - start_W,
  118. float travel_E = cart.e - current_position.e
  119. );
  120. // If "P" specified circles, call plan_arc recursively then continue with the rest of the arc
  121. if (TERN0(ARC_P_CIRCLES, circles)) {
  122. const float total_angular = abs_angular_travel + circles * RADIANS(360), // Total rotation with all circles and remainder
  123. part_per_circle = RADIANS(360) / total_angular; // Each circle's part of the total
  124. ARC_LIJKUVWE_CODE(
  125. const float per_circle_L = travel_L * part_per_circle, // L movement per circle
  126. const float per_circle_I = travel_I * part_per_circle,
  127. const float per_circle_J = travel_J * part_per_circle,
  128. const float per_circle_K = travel_K * part_per_circle,
  129. const float per_circle_U = travel_U * part_per_circle,
  130. const float per_circle_V = travel_V * part_per_circle,
  131. const float per_circle_W = travel_W * part_per_circle,
  132. const float per_circle_E = travel_E * part_per_circle // E movement per circle
  133. );
  134. xyze_pos_t temp_position = current_position;
  135. for (uint16_t n = circles; n--;) {
  136. ARC_LIJKUVWE_CODE( // Destination Linear Axes
  137. temp_position[axis_l] += per_circle_L,
  138. temp_position.i += per_circle_I,
  139. temp_position.j += per_circle_J,
  140. temp_position.k += per_circle_K,
  141. temp_position.u += per_circle_U,
  142. temp_position.v += per_circle_V,
  143. temp_position.w += per_circle_W,
  144. temp_position.e += per_circle_E // Destination E axis
  145. );
  146. plan_arc(temp_position, offset, clockwise, 0); // Plan a single whole circle
  147. }
  148. ARC_LIJKUVWE_CODE(
  149. travel_L = cart[axis_l] - current_position[axis_l],
  150. travel_I = cart.i - current_position.i,
  151. travel_J = cart.j - current_position.j,
  152. travel_K = cart.k - current_position.k,
  153. travel_U = cart.u - current_position.u,
  154. travel_V = cart.v - current_position.v,
  155. travel_W = cart.w - current_position.w,
  156. travel_E = cart.e - current_position.e
  157. );
  158. }
  159. // Millimeters in the arc, assuming it's flat
  160. const float flat_mm = radius * abs_angular_travel;
  161. // Return if the move is near zero
  162. if (flat_mm < 0.0001f
  163. GANG_N(SUB2(NUM_AXES),
  164. && travel_L < 0.0001f,
  165. && travel_I < 0.0001f,
  166. && travel_J < 0.0001f,
  167. && travel_K < 0.0001f,
  168. && travel_U < 0.0001f,
  169. && travel_V < 0.0001f,
  170. && travel_W < 0.0001f
  171. )
  172. ) return;
  173. // Feedrate for the move, scaled by the feedrate multiplier
  174. const feedRate_t scaled_fr_mm_s = MMS_SCALED(feedrate_mm_s);
  175. // Get the nominal segment length based on settings
  176. const float nominal_segment_mm = (
  177. #if ARC_SEGMENTS_PER_SEC // Length based on segments per second and feedrate
  178. constrain(scaled_fr_mm_s * RECIPROCAL(ARC_SEGMENTS_PER_SEC), MIN_ARC_SEGMENT_MM, MAX_ARC_SEGMENT_MM)
  179. #else
  180. MAX_ARC_SEGMENT_MM // Length using the maximum segment size
  181. #endif
  182. );
  183. // Number of whole segments based on the nominal segment length
  184. const float nominal_segments = _MAX(FLOOR(flat_mm / nominal_segment_mm), min_segments);
  185. // A new segment length based on the required minimum
  186. const float segment_mm = constrain(flat_mm / nominal_segments, MIN_ARC_SEGMENT_MM, MAX_ARC_SEGMENT_MM);
  187. // The number of whole segments in the arc, ignoring the remainder
  188. uint16_t segments = FLOOR(flat_mm / segment_mm);
  189. // Are the segments now too few to reach the destination?
  190. const float segmented_length = segment_mm * segments;
  191. const bool tooshort = segmented_length < flat_mm - 0.0001f;
  192. const float proportion = tooshort ? segmented_length / flat_mm : 1.0f;
  193. /**
  194. * Vector rotation by transformation matrix: r is the original vector, r_T is the rotated vector,
  195. * and phi is the angle of rotation. Based on the solution approach by Jens Geisler.
  196. * r_T = [cos(phi) -sin(phi);
  197. * sin(phi) cos(phi)] * r ;
  198. *
  199. * For arc generation, the center of the circle is the axis of rotation and the radius vector is
  200. * defined from the circle center to the initial position. Each line segment is formed by successive
  201. * vector rotations. This requires only two cos() and sin() computations to form the rotation
  202. * matrix for the duration of the entire arc. Error may accumulate from numerical round-off, since
  203. * all double numbers are single precision on the Arduino. (True double precision will not have
  204. * round off issues for CNC applications.) Single precision error can accumulate to be greater than
  205. * tool precision in some cases. Therefore, arc path correction is implemented.
  206. *
  207. * Small angle approximation may be used to reduce computation overhead further. This approximation
  208. * holds for everything, but very small circles and large MAX_ARC_SEGMENT_MM values. In other words,
  209. * theta_per_segment would need to be greater than 0.1 rad and N_ARC_CORRECTION would need to be large
  210. * to cause an appreciable drift error. N_ARC_CORRECTION~=25 is more than small enough to correct for
  211. * numerical drift error. N_ARC_CORRECTION may be on the order a hundred(s) before error becomes an
  212. * issue for CNC machines with the single precision Arduino calculations.
  213. *
  214. * This approximation also allows plan_arc to immediately insert a line segment into the planner
  215. * without the initial overhead of computing cos() or sin(). By the time the arc needs to be applied
  216. * a correction, the planner should have caught up to the lag caused by the initial plan_arc overhead.
  217. * This is important when there are successive arc motions.
  218. */
  219. // Vector rotation matrix values
  220. xyze_pos_t raw;
  221. const float theta_per_segment = proportion * angular_travel / segments,
  222. sq_theta_per_segment = sq(theta_per_segment),
  223. sin_T = theta_per_segment - sq_theta_per_segment * theta_per_segment / 6,
  224. cos_T = 1 - 0.5f * sq_theta_per_segment; // Small angle approximation
  225. #if DISABLED(AUTO_BED_LEVELING_UBL)
  226. ARC_LIJKUVW_CODE(
  227. const float per_segment_L = proportion * travel_L / segments,
  228. const float per_segment_I = proportion * travel_I / segments,
  229. const float per_segment_J = proportion * travel_J / segments,
  230. const float per_segment_K = proportion * travel_K / segments,
  231. const float per_segment_U = proportion * travel_U / segments,
  232. const float per_segment_V = proportion * travel_V / segments,
  233. const float per_segment_W = proportion * travel_W / segments
  234. );
  235. #endif
  236. CODE_ITEM_E(const float extruder_per_segment = proportion * travel_E / segments);
  237. // For shortened segments, run all but the remainder in the loop
  238. if (tooshort) segments++;
  239. // Initialize all linear axes and E
  240. ARC_LIJKUVWE_CODE(
  241. raw[axis_l] = current_position[axis_l],
  242. raw.i = current_position.i,
  243. raw.j = current_position.j,
  244. raw.k = current_position.k,
  245. raw.u = current_position.u,
  246. raw.v = current_position.v,
  247. raw.w = current_position.w,
  248. raw.e = current_position.e
  249. );
  250. #if ENABLED(SCARA_FEEDRATE_SCALING)
  251. const float inv_duration = scaled_fr_mm_s / segment_mm;
  252. #endif
  253. millis_t next_idle_ms = millis() + 200UL;
  254. #if N_ARC_CORRECTION > 1
  255. int8_t arc_recalc_count = N_ARC_CORRECTION;
  256. #endif
  257. for (uint16_t i = 1; i < segments; i++) { // Iterate (segments-1) times
  258. thermalManager.manage_heater();
  259. const millis_t ms = millis();
  260. if (ELAPSED(ms, next_idle_ms)) {
  261. next_idle_ms = ms + 200UL;
  262. idle();
  263. }
  264. #if N_ARC_CORRECTION > 1
  265. if (--arc_recalc_count) {
  266. // Apply vector rotation matrix to previous rvec.a / 1
  267. const float r_new_Y = rvec.a * sin_T + rvec.b * cos_T;
  268. rvec.a = rvec.a * cos_T - rvec.b * sin_T;
  269. rvec.b = r_new_Y;
  270. }
  271. else
  272. #endif
  273. {
  274. #if N_ARC_CORRECTION > 1
  275. arc_recalc_count = N_ARC_CORRECTION;
  276. #endif
  277. // Arc correction to radius vector. Computed only every N_ARC_CORRECTION increments.
  278. // Compute exact location by applying transformation matrix from initial radius vector(=-offset).
  279. // To reduce stuttering, the sin and cos could be computed at different times.
  280. // For now, compute both at the same time.
  281. const float cos_Ti = cos(i * theta_per_segment), sin_Ti = sin(i * theta_per_segment);
  282. rvec.a = -offset[0] * cos_Ti + offset[1] * sin_Ti;
  283. rvec.b = -offset[0] * sin_Ti - offset[1] * cos_Ti;
  284. }
  285. // Update raw location
  286. raw[axis_p] = center_P + rvec.a;
  287. raw[axis_q] = center_Q + rvec.b;
  288. ARC_LIJKUVWE_CODE(
  289. #if ENABLED(AUTO_BED_LEVELING_UBL)
  290. raw[axis_l] = start_L,
  291. raw.i = start_I, raw.j = start_J, raw.k = start_K,
  292. raw.u = start_U, raw.v = start_V, raw.w = start_V
  293. #else
  294. raw[axis_l] += per_segment_L,
  295. raw.i += per_segment_I, raw.j += per_segment_J, raw.k += per_segment_K,
  296. raw.u += per_segment_U, raw.v += per_segment_V, raw.w += per_segment_W
  297. #endif
  298. , raw.e += extruder_per_segment
  299. );
  300. apply_motion_limits(raw);
  301. #if HAS_LEVELING && !PLANNER_LEVELING
  302. planner.apply_leveling(raw);
  303. #endif
  304. if (!planner.buffer_line(raw, scaled_fr_mm_s, active_extruder, 0 OPTARG(SCARA_FEEDRATE_SCALING, inv_duration)))
  305. break;
  306. }
  307. // Ensure last segment arrives at target location.
  308. raw = cart;
  309. #if ENABLED(AUTO_BED_LEVELING_UBL)
  310. ARC_LIJKUVW_CODE(
  311. raw[axis_l] = start_L,
  312. raw.i = start_I, raw.j = start_J, raw.k = start_K,
  313. raw.u = start_U, raw.v = start_V, raw.w = start_W
  314. );
  315. #endif
  316. apply_motion_limits(raw);
  317. #if HAS_LEVELING && !PLANNER_LEVELING
  318. planner.apply_leveling(raw);
  319. #endif
  320. planner.buffer_line(raw, scaled_fr_mm_s, active_extruder, 0 OPTARG(SCARA_FEEDRATE_SCALING, inv_duration));
  321. #if ENABLED(AUTO_BED_LEVELING_UBL)
  322. ARC_LIJKUVW_CODE(
  323. raw[axis_l] = start_L,
  324. raw.i = start_I, raw.j = start_J, raw.k = start_K,
  325. raw.u = start_U, raw.v = start_V, raw.w = start_W
  326. );
  327. #endif
  328. current_position = raw;
  329. } // plan_arc
  330. /**
  331. * G2: Clockwise Arc
  332. * G3: Counterclockwise Arc
  333. *
  334. * This command has two forms: IJ-form (JK, KI) and R-form.
  335. *
  336. * - Depending on the current Workspace Plane orientation,
  337. * use parameters IJ/JK/KI to specify the XY/YZ/ZX offsets.
  338. * At least one of the IJ/JK/KI parameters is required.
  339. * XY/YZ/ZX can be omitted to do a complete circle.
  340. * The given XY/YZ/ZX is not error-checked. The arc ends
  341. * based on the angle of the destination.
  342. * Mixing IJ/JK/KI with R will throw an error.
  343. *
  344. * - R specifies the radius. X or Y (Y or Z / Z or X) is required.
  345. * Omitting both XY/YZ/ZX will throw an error.
  346. * XY/YZ/ZX must differ from the current XY/YZ/ZX.
  347. * Mixing R with IJ/JK/KI will throw an error.
  348. *
  349. * - P specifies the number of full circles to do
  350. * before the specified arc move.
  351. *
  352. * Examples:
  353. *
  354. * G2 I10 ; CW circle centered at X+10
  355. * G3 X20 Y12 R14 ; CCW circle with r=14 ending at X20 Y12
  356. */
  357. void GcodeSuite::G2_G3(const bool clockwise) {
  358. if (MOTION_CONDITIONS) {
  359. TERN_(FULL_REPORT_TO_HOST_FEATURE, set_and_report_grblstate(M_RUNNING));
  360. #if ENABLED(SF_ARC_FIX)
  361. const bool relative_mode_backup = relative_mode;
  362. relative_mode = true;
  363. #endif
  364. get_destination_from_command(); // Get X Y [Z[I[J[K...]]]] [E] F (and set cutter power)
  365. TERN_(SF_ARC_FIX, relative_mode = relative_mode_backup);
  366. ab_float_t arc_offset = { 0, 0 };
  367. if (parser.seenval('R')) {
  368. const float r = parser.value_linear_units();
  369. if (r) {
  370. const xy_pos_t p1 = current_position, p2 = destination;
  371. if (p1 != p2) {
  372. const xy_pos_t d2 = (p2 - p1) * 0.5f; // XY vector to midpoint of move from current
  373. const float e = clockwise ^ (r < 0) ? -1 : 1, // clockwise -1/1, counterclockwise 1/-1
  374. len = d2.magnitude(), // Distance to mid-point of move from current
  375. h2 = (r - len) * (r + len), // factored to reduce rounding error
  376. h = (h2 >= 0) ? SQRT(h2) : 0.0f; // Distance to the arc pivot-point from midpoint
  377. const xy_pos_t s = { -d2.y, d2.x }; // Perpendicular bisector. (Divide by len for unit vector.)
  378. arc_offset = d2 + s / len * e * h; // The calculated offset (mid-point if |r| <= len)
  379. }
  380. }
  381. }
  382. else {
  383. #if ENABLED(CNC_WORKSPACE_PLANES)
  384. char achar, bchar;
  385. switch (workspace_plane) {
  386. default:
  387. case GcodeSuite::PLANE_XY: achar = 'I'; bchar = 'J'; break;
  388. case GcodeSuite::PLANE_YZ: achar = 'J'; bchar = 'K'; break;
  389. case GcodeSuite::PLANE_ZX: achar = 'K'; bchar = 'I'; break;
  390. }
  391. #else
  392. constexpr char achar = 'I', bchar = 'J';
  393. #endif
  394. if (parser.seenval(achar)) arc_offset.a = parser.value_linear_units();
  395. if (parser.seenval(bchar)) arc_offset.b = parser.value_linear_units();
  396. }
  397. if (arc_offset) {
  398. #if ENABLED(ARC_P_CIRCLES)
  399. // P indicates number of circles to do
  400. const int8_t circles_to_do = parser.byteval('P');
  401. if (!WITHIN(circles_to_do, 0, 100))
  402. SERIAL_ERROR_MSG(STR_ERR_ARC_ARGS);
  403. #else
  404. constexpr uint8_t circles_to_do = 0;
  405. #endif
  406. // Send the arc to the planner
  407. plan_arc(destination, arc_offset, clockwise, circles_to_do);
  408. reset_stepper_timeout();
  409. }
  410. else
  411. SERIAL_ERROR_MSG(STR_ERR_ARC_ARGS);
  412. TERN_(FULL_REPORT_TO_HOST_FEATURE, set_and_report_grblstate(M_IDLE));
  413. }
  414. }
  415. #endif // ARC_SUPPORT