motion_control.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. motion_control.c - high level interface for issuing motion commands
  3. Part of Grbl
  4. Copyright (c) 2009-2011 Simen Svale Skogsrud
  5. Copyright (c) 2011 Sungeun K. Jeon
  6. Copyright (c) 2020 Brad Hochgesang
  7. Grbl is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11. Grbl is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with Grbl. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "Marlin.h"
  19. #include "stepper.h"
  20. #include "planner.h"
  21. // The arc is approximated by generating a huge number of tiny, linear segments. The length of each
  22. // segment is configured in settings.mm_per_arc_segment.
  23. void mc_arc(float* position, float* target, float* offset, float feed_rate, float radius, bool isclockwise, uint8_t extruder)
  24. {
  25. float r_axis_x = -offset[X_AXIS]; // Radius vector from center to current location
  26. float r_axis_y = -offset[Y_AXIS];
  27. float center_axis_x = position[X_AXIS] - r_axis_x;
  28. float center_axis_y = position[Y_AXIS] - r_axis_y;
  29. float travel_z = target[Z_AXIS] - position[Z_AXIS];
  30. float rt_x = target[X_AXIS] - center_axis_x;
  31. float rt_y = target[Y_AXIS] - center_axis_y;
  32. // 20200419 - Add a variable that will be used to hold the arc segment length
  33. float mm_per_arc_segment = cs.mm_per_arc_segment;
  34. // 20210109 - Add a variable to hold the n_arc_correction value
  35. unsigned char n_arc_correction = cs.n_arc_correction;
  36. // CCW angle between position and target from circle center. Only one atan2() trig computation required.
  37. float angular_travel_total = atan2(r_axis_x * rt_y - r_axis_y * rt_x, r_axis_x * rt_x + r_axis_y * rt_y);
  38. if (angular_travel_total < 0) { angular_travel_total += 2 * M_PI; }
  39. if (cs.min_arc_segments > 0)
  40. {
  41. // 20200417 - FormerLurker - Implement MIN_ARC_SEGMENTS if it is defined - from Marlin 2.0 implementation
  42. // Do this before converting the angular travel for clockwise rotation
  43. mm_per_arc_segment = radius * ((2.0f * M_PI) / cs.min_arc_segments);
  44. }
  45. if (cs.arc_segments_per_sec > 0)
  46. {
  47. // 20200417 - FormerLurker - Implement MIN_ARC_SEGMENTS if it is defined - from Marlin 2.0 implementation
  48. float mm_per_arc_segment_sec = (feed_rate / 60.0f) * (1.0f / cs.arc_segments_per_sec);
  49. if (mm_per_arc_segment_sec < mm_per_arc_segment)
  50. mm_per_arc_segment = mm_per_arc_segment_sec;
  51. }
  52. // Note: no need to check to see if min_mm_per_arc_segment is enabled or not (i.e. = 0), since mm_per_arc_segment can never be below 0.
  53. if (mm_per_arc_segment < cs.min_mm_per_arc_segment)
  54. {
  55. // 20200417 - FormerLurker - Implement MIN_MM_PER_ARC_SEGMENT if it is defined
  56. // This prevents a very high number of segments from being generated for curves of a short radius
  57. mm_per_arc_segment = cs.min_mm_per_arc_segment;
  58. }
  59. else if (mm_per_arc_segment > cs.mm_per_arc_segment) {
  60. // 20210113 - This can be implemented in an else if since we can't be below the min AND above the max at the same time.
  61. // 20200417 - FormerLurker - Implement MIN_MM_PER_ARC_SEGMENT if it is defined
  62. mm_per_arc_segment = cs.mm_per_arc_segment;
  63. }
  64. // Adjust the angular travel if the direction is clockwise
  65. if (isclockwise) { angular_travel_total -= 2 * M_PI; }
  66. //20141002:full circle for G03 did not work, e.g. G03 X80 Y80 I20 J0 F2000 is giving an Angle of zero so head is not moving
  67. //to compensate when start pos = target pos && angle is zero -> angle = 2Pi
  68. if (position[X_AXIS] == target[X_AXIS] && position[Y_AXIS] == target[Y_AXIS] && angular_travel_total == 0)
  69. {
  70. angular_travel_total += 2 * M_PI;
  71. }
  72. //end fix G03
  73. // 20200417 - FormerLurker - rename millimeters_of_travel to millimeters_of_travel_arc to better describe what we are
  74. // calculating here
  75. const float millimeters_of_travel_arc = hypot(angular_travel_total * radius, fabs(travel_z));
  76. if (millimeters_of_travel_arc < 0.001) { return; }
  77. // Calculate the number of arc segments
  78. unsigned short segments = static_cast<unsigned short>(ceil(millimeters_of_travel_arc / mm_per_arc_segment));
  79. /* Vector rotation by transformation matrix: r is the original vector, r_T is the rotated vector,
  80. and phi is the angle of rotation. Based on the solution approach by Jens Geisler.
  81. r_T = [cos(phi) -sin(phi);
  82. sin(phi) cos(phi] * r ;
  83. For arc generation, the center of the circle is the axis of rotation and the radius vector is
  84. defined from the circle center to the initial position. Each line segment is formed by successive
  85. vector rotations. This requires only two cos() and sin() computations to form the rotation
  86. matrix for the duration of the entire arc. Error may accumulate from numerical round-off, since
  87. all double numbers are single precision on the Arduino. (True double precision will not have
  88. round off issues for CNC applications.) Single precision error can accumulate to be greater than
  89. tool precision in some cases. Therefore, arc path correction is implemented.
  90. The small angle approximation was removed because of excessive errors for small circles (perhaps unique to
  91. 3d printing applications, causing significant path deviation and extrusion issues).
  92. Now there will be no corrections applied, but an accurate initial sin and cos will be calculated.
  93. This seems to work with a very high degree of accuracy and results in much simpler code.
  94. Finding a faster way to approximate sin, knowing that there can be substantial deviations from the true
  95. arc when using the previous approximation, would be beneficial.
  96. */
  97. // If there is only one segment, no need to do a bunch of work since this is a straight line!
  98. if (segments > 1)
  99. {
  100. // Calculate theta per segments, and linear (z) travel per segment, e travel per segment
  101. // as well as the small angle approximation for sin and cos.
  102. const float theta_per_segment = angular_travel_total / segments,
  103. linear_per_segment = travel_z / (segments),
  104. segment_extruder_travel = (target[E_AXIS] - position[E_AXIS]) / (segments),
  105. sq_theta_per_segment = theta_per_segment * theta_per_segment,
  106. sin_T = theta_per_segment - sq_theta_per_segment * theta_per_segment / 6,
  107. cos_T = 1 - 0.5f * sq_theta_per_segment;
  108. // Loop through all but one of the segments. The last one can be done simply
  109. // by moving to the target.
  110. for (uint16_t i = 1; i < segments; i++) {
  111. if (n_arc_correction-- == 0) {
  112. // Calculate the actual position for r_axis_x and r_axis_y
  113. const float cos_Ti = cos(i * theta_per_segment), sin_Ti = sin(i * theta_per_segment);
  114. r_axis_x = -offset[X_AXIS] * cos_Ti + offset[Y_AXIS] * sin_Ti;
  115. r_axis_y = -offset[X_AXIS] * sin_Ti - offset[Y_AXIS] * cos_Ti;
  116. // reset n_arc_correction
  117. n_arc_correction = cs.n_arc_correction;
  118. }
  119. else {
  120. // Calculate X and Y using the small angle approximation
  121. const float r_axisi = r_axis_x * sin_T + r_axis_y * cos_T;
  122. r_axis_x = r_axis_x * cos_T - r_axis_y * sin_T;
  123. r_axis_y = r_axisi;
  124. }
  125. // Update Position
  126. position[X_AXIS] = center_axis_x + r_axis_x;
  127. position[Y_AXIS] = center_axis_y + r_axis_y;
  128. position[Z_AXIS] += linear_per_segment;
  129. position[E_AXIS] += segment_extruder_travel;
  130. // Clamp to the calculated position.
  131. clamp_to_software_endstops(position);
  132. // Insert the segment into the buffer
  133. plan_buffer_line(position[X_AXIS], position[Y_AXIS], position[Z_AXIS], position[E_AXIS], feed_rate, extruder, position);
  134. // Handle the situation where the planner is aborted hard.
  135. if (planner_aborted)
  136. return;
  137. }
  138. }
  139. // Clamp to the target position.
  140. clamp_to_software_endstops(target);
  141. // Ensure last segment arrives at target location.
  142. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feed_rate, extruder, target);
  143. }