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.

delta.cpp 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * delta.cpp
  24. */
  25. #include "../inc/MarlinConfig.h"
  26. #if ENABLED(DELTA)
  27. #include "delta.h"
  28. #include "motion.h"
  29. // For homing:
  30. #include "planner.h"
  31. #include "endstops.h"
  32. #include "../lcd/ultralcd.h"
  33. #include "../Marlin.h"
  34. #if HAS_BED_PROBE
  35. #include "probe.h"
  36. #endif
  37. #if ENABLED(SENSORLESS_HOMING)
  38. #include "../feature/tmc_util.h"
  39. #include "stepper_indirection.h"
  40. #endif
  41. #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
  42. #include "../core/debug_out.h"
  43. // Initialized by settings.load()
  44. float delta_height,
  45. delta_endstop_adj[ABC] = { 0 },
  46. delta_radius,
  47. delta_diagonal_rod,
  48. delta_segments_per_second,
  49. delta_calibration_radius,
  50. delta_tower_angle_trim[ABC];
  51. float delta_tower[ABC][2],
  52. delta_diagonal_rod_2_tower[ABC],
  53. delta_clip_start_height = Z_MAX_POS;
  54. float delta_safe_distance_from_top();
  55. /**
  56. * Recalculate factors used for delta kinematics whenever
  57. * settings have been changed (e.g., by M665).
  58. */
  59. void recalc_delta_settings() {
  60. const float trt[ABC] = DELTA_RADIUS_TRIM_TOWER,
  61. drt[ABC] = DELTA_DIAGONAL_ROD_TRIM_TOWER;
  62. delta_tower[A_AXIS][X_AXIS] = cos(RADIANS(210 + delta_tower_angle_trim[A_AXIS])) * (delta_radius + trt[A_AXIS]); // front left tower
  63. delta_tower[A_AXIS][Y_AXIS] = sin(RADIANS(210 + delta_tower_angle_trim[A_AXIS])) * (delta_radius + trt[A_AXIS]);
  64. delta_tower[B_AXIS][X_AXIS] = cos(RADIANS(330 + delta_tower_angle_trim[B_AXIS])) * (delta_radius + trt[B_AXIS]); // front right tower
  65. delta_tower[B_AXIS][Y_AXIS] = sin(RADIANS(330 + delta_tower_angle_trim[B_AXIS])) * (delta_radius + trt[B_AXIS]);
  66. delta_tower[C_AXIS][X_AXIS] = cos(RADIANS( 90 + delta_tower_angle_trim[C_AXIS])) * (delta_radius + trt[C_AXIS]); // back middle tower
  67. delta_tower[C_AXIS][Y_AXIS] = sin(RADIANS( 90 + delta_tower_angle_trim[C_AXIS])) * (delta_radius + trt[C_AXIS]);
  68. delta_diagonal_rod_2_tower[A_AXIS] = sq(delta_diagonal_rod + drt[A_AXIS]);
  69. delta_diagonal_rod_2_tower[B_AXIS] = sq(delta_diagonal_rod + drt[B_AXIS]);
  70. delta_diagonal_rod_2_tower[C_AXIS] = sq(delta_diagonal_rod + drt[C_AXIS]);
  71. update_software_endstops(Z_AXIS);
  72. set_all_unhomed();
  73. }
  74. /**
  75. * Delta Inverse Kinematics
  76. *
  77. * Calculate the tower positions for a given machine
  78. * position, storing the result in the delta[] array.
  79. *
  80. * This is an expensive calculation, requiring 3 square
  81. * roots per segmented linear move, and strains the limits
  82. * of a Mega2560 with a Graphical Display.
  83. *
  84. * Suggested optimizations include:
  85. *
  86. * - Disable the home_offset (M206) and/or position_shift (G92)
  87. * features to remove up to 12 float additions.
  88. */
  89. #define DELTA_DEBUG(VAR) do { \
  90. SERIAL_ECHOPAIR("cartesian X:", VAR[X_AXIS]); \
  91. SERIAL_ECHOPAIR(" Y:", VAR[Y_AXIS]); \
  92. SERIAL_ECHOLNPAIR(" Z:", VAR[Z_AXIS]); \
  93. SERIAL_ECHOPAIR("delta A:", delta[A_AXIS]); \
  94. SERIAL_ECHOPAIR(" B:", delta[B_AXIS]); \
  95. SERIAL_ECHOLNPAIR(" C:", delta[C_AXIS]); \
  96. }while(0)
  97. void inverse_kinematics(const float (&raw)[XYZ]) {
  98. #if HAS_HOTEND_OFFSET
  99. // Delta hotend offsets must be applied in Cartesian space with no "spoofing"
  100. const float pos[XYZ] = {
  101. raw[X_AXIS] - hotend_offset[X_AXIS][active_extruder],
  102. raw[Y_AXIS] - hotend_offset[Y_AXIS][active_extruder],
  103. raw[Z_AXIS]
  104. };
  105. DELTA_IK(pos);
  106. //DELTA_DEBUG(pos);
  107. #else
  108. DELTA_IK(raw);
  109. //DELTA_DEBUG(raw);
  110. #endif
  111. }
  112. /**
  113. * Calculate the highest Z position where the
  114. * effector has the full range of XY motion.
  115. */
  116. float delta_safe_distance_from_top() {
  117. float cartesian[XYZ] = { 0, 0, 0 };
  118. inverse_kinematics(cartesian);
  119. float centered_extent = delta[A_AXIS];
  120. cartesian[Y_AXIS] = DELTA_PRINTABLE_RADIUS;
  121. inverse_kinematics(cartesian);
  122. return ABS(centered_extent - delta[A_AXIS]);
  123. }
  124. /**
  125. * Delta Forward Kinematics
  126. *
  127. * See the Wikipedia article "Trilateration"
  128. * https://en.wikipedia.org/wiki/Trilateration
  129. *
  130. * Establish a new coordinate system in the plane of the
  131. * three carriage points. This system has its origin at
  132. * tower1, with tower2 on the X axis. Tower3 is in the X-Y
  133. * plane with a Z component of zero.
  134. * We will define unit vectors in this coordinate system
  135. * in our original coordinate system. Then when we calculate
  136. * the Xnew, Ynew and Znew values, we can translate back into
  137. * the original system by moving along those unit vectors
  138. * by the corresponding values.
  139. *
  140. * Variable names matched to Marlin, c-version, and avoid the
  141. * use of any vector library.
  142. *
  143. * by Andreas Hardtung 2016-06-07
  144. * based on a Java function from "Delta Robot Kinematics V3"
  145. * by Steve Graves
  146. *
  147. * The result is stored in the cartes[] array.
  148. */
  149. void forward_kinematics_DELTA(const float &z1, const float &z2, const float &z3) {
  150. // Create a vector in old coordinates along x axis of new coordinate
  151. const float p12[3] = { delta_tower[B_AXIS][X_AXIS] - delta_tower[A_AXIS][X_AXIS], delta_tower[B_AXIS][Y_AXIS] - delta_tower[A_AXIS][Y_AXIS], z2 - z1 },
  152. // Get the reciprocal of Magnitude of vector.
  153. d2 = sq(p12[0]) + sq(p12[1]) + sq(p12[2]), inv_d = RSQRT(d2),
  154. // Create unit vector by multiplying by the inverse of the magnitude.
  155. ex[3] = { p12[0] * inv_d, p12[1] * inv_d, p12[2] * inv_d },
  156. // Get the vector from the origin of the new system to the third point.
  157. p13[3] = { delta_tower[C_AXIS][X_AXIS] - delta_tower[A_AXIS][X_AXIS], delta_tower[C_AXIS][Y_AXIS] - delta_tower[A_AXIS][Y_AXIS], z3 - z1 },
  158. // Use the dot product to find the component of this vector on the X axis.
  159. i = ex[0] * p13[0] + ex[1] * p13[1] + ex[2] * p13[2],
  160. // Create a vector along the x axis that represents the x component of p13.
  161. iex[3] = { ex[0] * i, ex[1] * i, ex[2] * i };
  162. // Subtract the X component from the original vector leaving only Y. We use the
  163. // variable that will be the unit vector after we scale it.
  164. float ey[3] = { p13[0] - iex[0], p13[1] - iex[1], p13[2] - iex[2] };
  165. // The magnitude and the inverse of the magnitude of Y component
  166. const float j2 = sq(ey[0]) + sq(ey[1]) + sq(ey[2]), inv_j = RSQRT(j2);
  167. // Convert to a unit vector
  168. ey[0] *= inv_j; ey[1] *= inv_j; ey[2] *= inv_j;
  169. // The cross product of the unit x and y is the unit z
  170. // float[] ez = vectorCrossProd(ex, ey);
  171. const float ez[3] = {
  172. ex[1] * ey[2] - ex[2] * ey[1],
  173. ex[2] * ey[0] - ex[0] * ey[2],
  174. ex[0] * ey[1] - ex[1] * ey[0]
  175. },
  176. // We now have the d, i and j values defined in Wikipedia.
  177. // Plug them into the equations defined in Wikipedia for Xnew, Ynew and Znew
  178. Xnew = (delta_diagonal_rod_2_tower[A_AXIS] - delta_diagonal_rod_2_tower[B_AXIS] + d2) * inv_d * 0.5,
  179. Ynew = ((delta_diagonal_rod_2_tower[A_AXIS] - delta_diagonal_rod_2_tower[C_AXIS] + sq(i) + j2) * 0.5 - i * Xnew) * inv_j,
  180. Znew = SQRT(delta_diagonal_rod_2_tower[A_AXIS] - HYPOT2(Xnew, Ynew));
  181. // Start from the origin of the old coordinates and add vectors in the
  182. // old coords that represent the Xnew, Ynew and Znew to find the point
  183. // in the old system.
  184. cartes[X_AXIS] = delta_tower[A_AXIS][X_AXIS] + ex[0] * Xnew + ey[0] * Ynew - ez[0] * Znew;
  185. cartes[Y_AXIS] = delta_tower[A_AXIS][Y_AXIS] + ex[1] * Xnew + ey[1] * Ynew - ez[1] * Znew;
  186. cartes[Z_AXIS] = z1 + ex[2] * Xnew + ey[2] * Ynew - ez[2] * Znew;
  187. }
  188. /**
  189. * A delta can only safely home all axes at the same time
  190. * This is like quick_home_xy() but for 3 towers.
  191. */
  192. void home_delta() {
  193. if (DEBUGGING(LEVELING)) DEBUG_POS(">>> home_delta", current_position);
  194. // Init the current position of all carriages to 0,0,0
  195. ZERO(current_position);
  196. ZERO(destination);
  197. sync_plan_position();
  198. // Disable stealthChop if used. Enable diag1 pin on driver.
  199. #if ENABLED(SENSORLESS_HOMING)
  200. sensorless_t stealth_states {
  201. tmc_enable_stallguard(stepperX),
  202. tmc_enable_stallguard(stepperY),
  203. tmc_enable_stallguard(stepperZ)
  204. };
  205. #endif
  206. // Move all carriages together linearly until an endstop is hit.
  207. destination[Z_AXIS] = (delta_height
  208. #if HAS_BED_PROBE
  209. - zprobe_zoffset
  210. #endif
  211. + 10);
  212. buffer_line_to_destination(homing_feedrate(X_AXIS));
  213. planner.synchronize();
  214. // Re-enable stealthChop if used. Disable diag1 pin on driver.
  215. #if ENABLED(SENSORLESS_HOMING)
  216. tmc_disable_stallguard(stepperX, stealth_states.x);
  217. tmc_disable_stallguard(stepperY, stealth_states.y);
  218. tmc_disable_stallguard(stepperZ, stealth_states.z);
  219. #endif
  220. endstops.validate_homing_move();
  221. // At least one carriage has reached the top.
  222. // Now re-home each carriage separately.
  223. homeaxis(A_AXIS);
  224. homeaxis(B_AXIS);
  225. homeaxis(C_AXIS);
  226. // Set all carriages to their home positions
  227. // Do this here all at once for Delta, because
  228. // XYZ isn't ABC. Applying this per-tower would
  229. // give the impression that they are the same.
  230. LOOP_XYZ(i) set_axis_is_at_home((AxisEnum)i);
  231. sync_plan_position();
  232. if (DEBUGGING(LEVELING)) DEBUG_POS("<<< home_delta", current_position);
  233. }
  234. #endif // DELTA