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.

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