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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. /**
  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/marlinui.h"
  33. #include "../MarlinCore.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. abc_float_t delta_endstop_adj{0};
  46. float delta_radius,
  47. delta_diagonal_rod,
  48. segments_per_second;
  49. abc_float_t delta_tower_angle_trim;
  50. xy_float_t delta_tower[ABC];
  51. abc_float_t delta_diagonal_rod_2_tower;
  52. float delta_clip_start_height = Z_MAX_POS;
  53. abc_float_t delta_diagonal_rod_trim;
  54. float delta_safe_distance_from_top();
  55. void refresh_delta_clip_start_height() {
  56. delta_clip_start_height = TERN(HAS_SOFTWARE_ENDSTOPS,
  57. soft_endstop.max.z,
  58. DIFF_TERN(HAS_BED_PROBE, delta_height, probe.offset.z)
  59. ) - delta_safe_distance_from_top();
  60. }
  61. /**
  62. * Recalculate factors used for delta kinematics whenever
  63. * settings have been changed (e.g., by M665).
  64. */
  65. void recalc_delta_settings() {
  66. constexpr abc_float_t trt = DELTA_RADIUS_TRIM_TOWER;
  67. delta_tower[A_AXIS].set(cos(RADIANS(210 + delta_tower_angle_trim.a)) * (delta_radius + trt.a), // front left tower
  68. sin(RADIANS(210 + delta_tower_angle_trim.a)) * (delta_radius + trt.a));
  69. delta_tower[B_AXIS].set(cos(RADIANS(330 + delta_tower_angle_trim.b)) * (delta_radius + trt.b), // front right tower
  70. sin(RADIANS(330 + delta_tower_angle_trim.b)) * (delta_radius + trt.b));
  71. delta_tower[C_AXIS].set(cos(RADIANS( 90 + delta_tower_angle_trim.c)) * (delta_radius + trt.c), // back middle tower
  72. sin(RADIANS( 90 + delta_tower_angle_trim.c)) * (delta_radius + trt.c));
  73. delta_diagonal_rod_2_tower.set(sq(delta_diagonal_rod + delta_diagonal_rod_trim.a),
  74. sq(delta_diagonal_rod + delta_diagonal_rod_trim.b),
  75. sq(delta_diagonal_rod + delta_diagonal_rod_trim.c));
  76. update_software_endstops(Z_AXIS);
  77. set_all_unhomed();
  78. }
  79. /**
  80. * Delta Inverse Kinematics
  81. *
  82. * Calculate the tower positions for a given machine
  83. * position, storing the result in the delta[] array.
  84. *
  85. * This is an expensive calculation, requiring 3 square
  86. * roots per segmented linear move, and strains the limits
  87. * of a Mega2560 with a Graphical Display.
  88. *
  89. * Suggested optimizations include:
  90. *
  91. * - Disable the home_offset (M206) and/or position_shift (G92)
  92. * features to remove up to 12 float additions.
  93. */
  94. #define DELTA_DEBUG(VAR) do { \
  95. SERIAL_ECHOLNPGM_P(PSTR("Cartesian X"), VAR.x, SP_Y_STR, VAR.y, SP_Z_STR, VAR.z); \
  96. SERIAL_ECHOLNPGM_P(PSTR("Delta A"), delta.a, SP_B_STR, delta.b, SP_C_STR, delta.c); \
  97. }while(0)
  98. void inverse_kinematics(const xyz_pos_t &raw) {
  99. #if HAS_HOTEND_OFFSET
  100. // Delta hotend offsets must be applied in Cartesian space with no "spoofing"
  101. xyz_pos_t pos = { raw.x - hotend_offset[active_extruder].x,
  102. raw.y - hotend_offset[active_extruder].y,
  103. raw.z };
  104. DELTA_IK(pos);
  105. //DELTA_DEBUG(pos);
  106. #else
  107. DELTA_IK(raw);
  108. //DELTA_DEBUG(raw);
  109. #endif
  110. }
  111. /**
  112. * Calculate the highest Z position where the
  113. * effector has the full range of XY motion.
  114. */
  115. float delta_safe_distance_from_top() {
  116. xyz_pos_t cartesian{0};
  117. inverse_kinematics(cartesian);
  118. const float centered_extent = delta.a;
  119. cartesian.y = DELTA_PRINTABLE_RADIUS;
  120. inverse_kinematics(cartesian);
  121. return ABS(centered_extent - delta.a);
  122. }
  123. /**
  124. * Delta Forward Kinematics
  125. *
  126. * See the Wikipedia article "Trilateration"
  127. * https://en.wikipedia.org/wiki/Trilateration
  128. *
  129. * Establish a new coordinate system in the plane of the
  130. * three carriage points. This system has its origin at
  131. * tower1, with tower2 on the X axis. Tower3 is in the X-Y
  132. * plane with a Z component of zero.
  133. * We will define unit vectors in this coordinate system
  134. * in our original coordinate system. Then when we calculate
  135. * the Xnew, Ynew and Znew values, we can translate back into
  136. * the original system by moving along those unit vectors
  137. * by the corresponding values.
  138. *
  139. * Variable names matched to Marlin, c-version, and avoid the
  140. * use of any vector library.
  141. *
  142. * by Andreas Hardtung 2016-06-07
  143. * based on a Java function from "Delta Robot Kinematics V3"
  144. * by Steve Graves
  145. *
  146. * The result is stored in the cartes[] array.
  147. */
  148. void forward_kinematics(const_float_t z1, const_float_t z2, const_float_t z3) {
  149. // Create a vector in old coordinates along x axis of new coordinate
  150. const float p12[3] = { delta_tower[B_AXIS].x - delta_tower[A_AXIS].x, delta_tower[B_AXIS].y - delta_tower[A_AXIS].y, z2 - z1 },
  151. // Get the reciprocal of Magnitude of vector.
  152. d2 = sq(p12[0]) + sq(p12[1]) + sq(p12[2]), inv_d = RSQRT(d2),
  153. // Create unit vector by multiplying by the inverse of the magnitude.
  154. ex[3] = { p12[0] * inv_d, p12[1] * inv_d, p12[2] * inv_d },
  155. // Get the vector from the origin of the new system to the third point.
  156. p13[3] = { delta_tower[C_AXIS].x - delta_tower[A_AXIS].x, delta_tower[C_AXIS].y - delta_tower[A_AXIS].y, z3 - z1 },
  157. // Use the dot product to find the component of this vector on the X axis.
  158. i = ex[0] * p13[0] + ex[1] * p13[1] + ex[2] * p13[2],
  159. // Create a vector along the x axis that represents the x component of p13.
  160. iex[3] = { ex[0] * i, ex[1] * i, ex[2] * i };
  161. // Subtract the X component from the original vector leaving only Y. We use the
  162. // variable that will be the unit vector after we scale it.
  163. float ey[3] = { p13[0] - iex[0], p13[1] - iex[1], p13[2] - iex[2] };
  164. // The magnitude and the inverse of the magnitude of Y component
  165. const float j2 = sq(ey[0]) + sq(ey[1]) + sq(ey[2]), inv_j = RSQRT(j2);
  166. // Convert to a unit vector
  167. ey[0] *= inv_j; ey[1] *= inv_j; ey[2] *= inv_j;
  168. // The cross product of the unit x and y is the unit z
  169. // float[] ez = vectorCrossProd(ex, ey);
  170. const float ez[3] = {
  171. ex[1] * ey[2] - ex[2] * ey[1],
  172. ex[2] * ey[0] - ex[0] * ey[2],
  173. ex[0] * ey[1] - ex[1] * ey[0]
  174. },
  175. // We now have the d, i and j values defined in Wikipedia.
  176. // Plug them into the equations defined in Wikipedia for Xnew, Ynew and Znew
  177. Xnew = (delta_diagonal_rod_2_tower.a - delta_diagonal_rod_2_tower.b + d2) * inv_d * 0.5,
  178. Ynew = ((delta_diagonal_rod_2_tower.a - delta_diagonal_rod_2_tower.c + sq(i) + j2) * 0.5 - i * Xnew) * inv_j,
  179. Znew = SQRT(delta_diagonal_rod_2_tower.a - HYPOT2(Xnew, Ynew));
  180. // Start from the origin of the old coordinates and add vectors in the
  181. // old coords that represent the Xnew, Ynew and Znew to find the point
  182. // in the old system.
  183. cartes.set(delta_tower[A_AXIS].x + ex[0] * Xnew + ey[0] * Ynew - ez[0] * Znew,
  184. delta_tower[A_AXIS].y + ex[1] * Xnew + ey[1] * Ynew - ez[1] * Znew,
  185. z1 + ex[2] * Xnew + ey[2] * Ynew - ez[2] * Znew);
  186. }
  187. /**
  188. * A delta can only safely home all axes at the same time
  189. * This is like quick_home_xy() but for 3 towers.
  190. */
  191. void home_delta() {
  192. DEBUG_SECTION(log_home_delta, "home_delta", DEBUGGING(LEVELING));
  193. // Init the current position of all carriages to 0,0,0
  194. current_position.reset();
  195. destination.reset();
  196. sync_plan_position();
  197. // Disable stealthChop if used. Enable diag1 pin on driver.
  198. #if ENABLED(SENSORLESS_HOMING)
  199. TERN_(X_SENSORLESS, sensorless_t stealth_states_x = start_sensorless_homing_per_axis(X_AXIS));
  200. TERN_(Y_SENSORLESS, sensorless_t stealth_states_y = start_sensorless_homing_per_axis(Y_AXIS));
  201. TERN_(Z_SENSORLESS, sensorless_t stealth_states_z = start_sensorless_homing_per_axis(Z_AXIS));
  202. TERN_(I_SENSORLESS, sensorless_t stealth_states_i = start_sensorless_homing_per_axis(I_AXIS));
  203. TERN_(J_SENSORLESS, sensorless_t stealth_states_j = start_sensorless_homing_per_axis(J_AXIS));
  204. TERN_(K_SENSORLESS, sensorless_t stealth_states_k = start_sensorless_homing_per_axis(K_AXIS));
  205. TERN_(U_SENSORLESS, sensorless_t stealth_states_u = start_sensorless_homing_per_axis(U_AXIS));
  206. TERN_(V_SENSORLESS, sensorless_t stealth_states_v = start_sensorless_homing_per_axis(V_AXIS));
  207. TERN_(W_SENSORLESS, sensorless_t stealth_states_w = start_sensorless_homing_per_axis(W_AXIS));
  208. #if SENSORLESS_STALLGUARD_DELAY
  209. safe_delay(SENSORLESS_STALLGUARD_DELAY); // Short delay needed to settle
  210. #endif
  211. #endif
  212. // Move all carriages together linearly until an endstop is hit.
  213. current_position.z = DIFF_TERN(HAS_BED_PROBE, delta_height + 10, probe.offset.z);
  214. line_to_current_position(homing_feedrate(Z_AXIS));
  215. planner.synchronize();
  216. TERN_(HAS_DELTA_SENSORLESS_PROBING, endstops.report_states());
  217. // Re-enable stealthChop if used. Disable diag1 pin on driver.
  218. #if ENABLED(SENSORLESS_HOMING) && DISABLED(ENDSTOPS_ALWAYS_ON_DEFAULT)
  219. TERN_(X_SENSORLESS, end_sensorless_homing_per_axis(X_AXIS, stealth_states_x));
  220. TERN_(Y_SENSORLESS, end_sensorless_homing_per_axis(Y_AXIS, stealth_states_y));
  221. TERN_(Z_SENSORLESS, end_sensorless_homing_per_axis(Z_AXIS, stealth_states_z));
  222. TERN_(I_SENSORLESS, end_sensorless_homing_per_axis(I_AXIS, stealth_states_i));
  223. TERN_(J_SENSORLESS, end_sensorless_homing_per_axis(J_AXIS, stealth_states_j));
  224. TERN_(K_SENSORLESS, end_sensorless_homing_per_axis(K_AXIS, stealth_states_k));
  225. TERN_(U_SENSORLESS, end_sensorless_homing_per_axis(U_AXIS, stealth_states_u));
  226. TERN_(V_SENSORLESS, end_sensorless_homing_per_axis(V_AXIS, stealth_states_v));
  227. TERN_(W_SENSORLESS, end_sensorless_homing_per_axis(W_AXIS, stealth_states_w));
  228. #if SENSORLESS_STALLGUARD_DELAY
  229. safe_delay(SENSORLESS_STALLGUARD_DELAY); // Short delay needed to settle
  230. #endif
  231. #endif
  232. endstops.validate_homing_move();
  233. // At least one carriage has reached the top.
  234. // Now re-home each carriage separately.
  235. homeaxis(A_AXIS);
  236. homeaxis(B_AXIS);
  237. homeaxis(C_AXIS);
  238. // Set all carriages to their home positions
  239. // Do this here all at once for Delta, because
  240. // XYZ isn't ABC. Applying this per-tower would
  241. // give the impression that they are the same.
  242. LOOP_ABC(i) set_axis_is_at_home((AxisEnum)i);
  243. sync_plan_position();
  244. #if DISABLED(DELTA_HOME_TO_SAFE_ZONE) && defined(HOMING_BACKOFF_POST_MM)
  245. constexpr xyz_float_t endstop_backoff = HOMING_BACKOFF_POST_MM;
  246. if (endstop_backoff.z) {
  247. current_position.z -= ABS(endstop_backoff.z) * Z_HOME_DIR;
  248. line_to_current_position(homing_feedrate(Z_AXIS));
  249. }
  250. #endif
  251. }
  252. #endif // DELTA