My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

delta.cpp 9.7KB

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