My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

delta.cpp 9.6KB

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