motion_control.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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, uint8_t 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 extruder_travel_total = target[E_AXIS] - position[E_AXIS];
  31. float rt_x = target[X_AXIS] - center_axis_x;
  32. float rt_y = target[Y_AXIS] - center_axis_y;
  33. // 20200419 - Add a variable that will be used to hold the arc segment length
  34. float mm_per_arc_segment = cs.mm_per_arc_segment;
  35. // CCW angle between position and target from circle center. Only one atan2() trig computation required.
  36. 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);
  37. if (angular_travel_total < 0) { angular_travel_total += 2 * M_PI; }
  38. bool check_mm_per_arc_segment_max = false;
  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. check_mm_per_arc_segment_max = true;
  45. }
  46. if (cs.arc_segments_per_sec > 0)
  47. {
  48. // 20200417 - FormerLurker - Implement MIN_ARC_SEGMENTS if it is defined - from Marlin 2.0 implementation
  49. float mm_per_arc_segment_sec = (feed_rate / 60.0f) * (1.0f / cs.arc_segments_per_sec);
  50. if (mm_per_arc_segment_sec < mm_per_arc_segment)
  51. mm_per_arc_segment = mm_per_arc_segment_sec;
  52. check_mm_per_arc_segment_max = true;
  53. }
  54. if (cs.min_mm_per_arc_segment > 0)
  55. {
  56. check_mm_per_arc_segment_max = true;
  57. // 20200417 - FormerLurker - Implement MIN_MM_PER_ARC_SEGMENT if it is defined
  58. // This prevents a very high number of segments from being generated for curves of a short radius
  59. if (mm_per_arc_segment < cs.min_mm_per_arc_segment) mm_per_arc_segment = cs.min_mm_per_arc_segment;
  60. }
  61. if (check_mm_per_arc_segment_max && mm_per_arc_segment > cs.mm_per_arc_segment) mm_per_arc_segment = cs.mm_per_arc_segment;
  62. // Adjust the angular travel if the direction is clockwise
  63. if (isclockwise) { angular_travel_total -= 2 * M_PI; }
  64. //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
  65. //to compensate when start pos = target pos && angle is zero -> angle = 2Pi
  66. if (position[X_AXIS] == target[X_AXIS] && position[Y_AXIS] == target[Y_AXIS] && angular_travel_total == 0)
  67. {
  68. angular_travel_total += 2 * M_PI;
  69. }
  70. //end fix G03
  71. // 20200417 - FormerLurker - rename millimeters_of_travel to millimeters_of_travel_arc to better describe what we are
  72. // calculating here
  73. float millimeters_of_travel_arc = hypot(angular_travel_total * radius, fabs(travel_z));
  74. if (millimeters_of_travel_arc < 0.001) { return; }
  75. // Calculate the total travel per segment
  76. // Calculate the number of arc segments
  77. uint16_t segments = static_cast<uint16_t>(ceil(millimeters_of_travel_arc / mm_per_arc_segment));
  78. // Calculate theta per segments and linear (z) travel per segment
  79. float theta_per_segment = angular_travel_total / segments;
  80. float linear_per_segment = travel_z / (segments);
  81. // Calculate the extrusion amount per segment
  82. float segment_extruder_travel = extruder_travel_total / (segments);
  83. /* Vector rotation by transformation matrix: r is the original vector, r_T is the rotated vector,
  84. and phi is the angle of rotation. Based on the solution approach by Jens Geisler.
  85. r_T = [cos(phi) -sin(phi);
  86. sin(phi) cos(phi] * r ;
  87. For arc generation, the center of the circle is the axis of rotation and the radius vector is
  88. defined from the circle center to the initial position. Each line segment is formed by successive
  89. vector rotations. This requires only two cos() and sin() computations to form the rotation
  90. matrix for the duration of the entire arc. Error may accumulate from numerical round-off, since
  91. all double numbers are single precision on the Arduino. (True double precision will not have
  92. round off issues for CNC applications.) Single precision error can accumulate to be greater than
  93. tool precision in some cases. Therefore, arc path correction is implemented.
  94. The small angle approximation was removed because of excessive errors for small circles (perhaps unique to
  95. 3d printing applications, causing significant path deviation and extrusion issues).
  96. Now there will be no corrections applied, but an accurate initial sin and cos will be calculated.
  97. This seems to work with a very high degree of accuracy and results in much simpler code.
  98. Finding a faster way to approximate sin, knowing that there can be substantial deviations from the true
  99. arc when using the previous approximation, would be beneficial.
  100. */
  101. // Don't bother calculating cot_T or sin_T if there is only 1 segment.
  102. if (segments > 1)
  103. {
  104. // Initialize the extruder axis
  105. float cos_T = cos(theta_per_segment);
  106. float sin_T = sin(theta_per_segment);
  107. float r_axisi;
  108. uint16_t i;
  109. for (i = 1; i < segments; i++) { // Increment (segments-1)
  110. r_axisi = r_axis_x * sin_T + r_axis_y * cos_T;
  111. r_axis_x = r_axis_x * cos_T - r_axis_y * sin_T;
  112. r_axis_y = r_axisi;
  113. // Update arc_target location
  114. position[X_AXIS] = center_axis_x + r_axis_x;
  115. position[Y_AXIS] = center_axis_y + r_axis_y;
  116. position[Z_AXIS] += linear_per_segment;
  117. position[E_AXIS] += segment_extruder_travel;
  118. // We can't clamp to the target because we are interpolating! We would need to update a position, clamp to it
  119. // after updating from calculated values.
  120. clamp_to_software_endstops(position);
  121. plan_buffer_line(position[X_AXIS], position[Y_AXIS], position[Z_AXIS], position[E_AXIS], feed_rate, extruder);
  122. }
  123. }
  124. // Ensure last segment arrives at target location.
  125. // Here we could clamp, but why bother. We would need to update our current position, clamp to it
  126. clamp_to_software_endstops(target);
  127. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feed_rate, extruder);
  128. }