motion_control.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. Grbl is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Grbl is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Grbl. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "Marlin.h"
  18. #include "stepper.h"
  19. #include "planner.h"
  20. // The arc is approximated by generating a huge number of tiny, linear segments. The length of each
  21. // segment is configured in settings.mm_per_arc_segment.
  22. void mc_arc(float *position, float *target, float *offset, uint8_t axis_0, uint8_t axis_1,
  23. uint8_t axis_linear, float feed_rate, float radius, uint8_t isclockwise, uint8_t extruder)
  24. {
  25. // int acceleration_manager_was_enabled = plan_is_acceleration_manager_enabled();
  26. // plan_set_acceleration_manager_enabled(false); // disable acceleration management for the duration of the arc
  27. float center_axis0 = position[axis_0] + offset[axis_0];
  28. float center_axis1 = position[axis_1] + offset[axis_1];
  29. float linear_travel = target[axis_linear] - position[axis_linear];
  30. float extruder_travel = target[E_AXIS] - position[E_AXIS];
  31. float r_axis0 = -offset[axis_0]; // Radius vector from center to current location
  32. float r_axis1 = -offset[axis_1];
  33. float rt_axis0 = target[axis_0] - center_axis0;
  34. float rt_axis1 = target[axis_1] - center_axis1;
  35. // CCW angle between position and target from circle center. Only one atan2() trig computation required.
  36. float angular_travel = atan2(r_axis0*rt_axis1-r_axis1*rt_axis0, r_axis0*rt_axis0+r_axis1*rt_axis1);
  37. if (angular_travel < 0) { angular_travel += 2*M_PI; }
  38. if (isclockwise) { angular_travel -= 2*M_PI; }
  39. //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
  40. //to compensate when start pos = target pos && angle is zero -> angle = 2Pi
  41. if (position[axis_0] == target[axis_0] && position[axis_1] == target[axis_1] && angular_travel == 0)
  42. {
  43. angular_travel += 2*M_PI;
  44. }
  45. //end fix G03
  46. float millimeters_of_travel = hypot(angular_travel*radius, fabs(linear_travel));
  47. if (millimeters_of_travel < 0.001) { return; }
  48. uint16_t segments = floor(millimeters_of_travel/MM_PER_ARC_SEGMENT);
  49. if(segments == 0) segments = 1;
  50. /*
  51. // Multiply inverse feed_rate to compensate for the fact that this movement is approximated
  52. // by a number of discrete segments. The inverse feed_rate should be correct for the sum of
  53. // all segments.
  54. if (invert_feed_rate) { feed_rate *= segments; }
  55. */
  56. float theta_per_segment = angular_travel/segments;
  57. float linear_per_segment = linear_travel/segments;
  58. float extruder_per_segment = extruder_travel/segments;
  59. /* Vector rotation by transformation matrix: r is the original vector, r_T is the rotated vector,
  60. and phi is the angle of rotation. Based on the solution approach by Jens Geisler.
  61. r_T = [cos(phi) -sin(phi);
  62. sin(phi) cos(phi] * r ;
  63. For arc generation, the center of the circle is the axis of rotation and the radius vector is
  64. defined from the circle center to the initial position. Each line segment is formed by successive
  65. vector rotations. This requires only two cos() and sin() computations to form the rotation
  66. matrix for the duration of the entire arc. Error may accumulate from numerical round-off, since
  67. all double numbers are single precision on the Arduino. (True double precision will not have
  68. round off issues for CNC applications.) Single precision error can accumulate to be greater than
  69. tool precision in some cases. Therefore, arc path correction is implemented.
  70. Small angle approximation may be used to reduce computation overhead further. This approximation
  71. holds for everything, but very small circles and large mm_per_arc_segment values. In other words,
  72. theta_per_segment would need to be greater than 0.1 rad and N_ARC_CORRECTION would need to be large
  73. to cause an appreciable drift error. N_ARC_CORRECTION~=25 is more than small enough to correct for
  74. numerical drift error. N_ARC_CORRECTION may be on the order a hundred(s) before error becomes an
  75. issue for CNC machines with the single precision Arduino calculations.
  76. This approximation also allows mc_arc to immediately insert a line segment into the planner
  77. without the initial overhead of computing cos() or sin(). By the time the arc needs to be applied
  78. a correction, the planner should have caught up to the lag caused by the initial mc_arc overhead.
  79. This is important when there are successive arc motions.
  80. */
  81. // Vector rotation matrix values
  82. float cos_T = 1-0.5*theta_per_segment*theta_per_segment; // Small angle approximation
  83. float sin_T = theta_per_segment;
  84. float arc_target[4];
  85. float sin_Ti;
  86. float cos_Ti;
  87. float r_axisi;
  88. uint16_t i;
  89. int8_t count = 0;
  90. // Initialize the linear axis
  91. arc_target[axis_linear] = position[axis_linear];
  92. // Initialize the extruder axis
  93. arc_target[E_AXIS] = position[E_AXIS];
  94. for (i = 1; i<segments; i++) { // Increment (segments-1)
  95. if (count < N_ARC_CORRECTION) {
  96. // Apply vector rotation matrix
  97. r_axisi = r_axis0*sin_T + r_axis1*cos_T;
  98. r_axis0 = r_axis0*cos_T - r_axis1*sin_T;
  99. r_axis1 = r_axisi;
  100. count++;
  101. } else {
  102. // Arc correction to radius vector. Computed only every N_ARC_CORRECTION increments.
  103. // Compute exact location by applying transformation matrix from initial radius vector(=-offset).
  104. cos_Ti = cos(i*theta_per_segment);
  105. sin_Ti = sin(i*theta_per_segment);
  106. r_axis0 = -offset[axis_0]*cos_Ti + offset[axis_1]*sin_Ti;
  107. r_axis1 = -offset[axis_0]*sin_Ti - offset[axis_1]*cos_Ti;
  108. count = 0;
  109. }
  110. // Update arc_target location
  111. arc_target[axis_0] = center_axis0 + r_axis0;
  112. arc_target[axis_1] = center_axis1 + r_axis1;
  113. arc_target[axis_linear] += linear_per_segment;
  114. arc_target[E_AXIS] += extruder_per_segment;
  115. clamp_to_software_endstops(arc_target);
  116. plan_buffer_line(arc_target[X_AXIS], arc_target[Y_AXIS], arc_target[Z_AXIS], arc_target[E_AXIS], feed_rate, extruder);
  117. }
  118. // Ensure last segment arrives at target location.
  119. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feed_rate, extruder);
  120. // plan_set_acceleration_manager_enabled(acceleration_manager_was_enabled);
  121. }