PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Marlin/src/module/delta.cpp

https://bitbucket.org/cpdevelops/anet-marlin
C++ | 300 lines | 142 code | 40 blank | 118 comment | 4 complexity | 993adfe1c1f75ec79ba510fb5d8771a7 MD5 | raw file
Possible License(s): GPL-3.0
  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 "stepper.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. #endif
  37. // Initialized by settings.load()
  38. float delta_height,
  39. delta_endstop_adj[ABC] = { 0 },
  40. delta_radius,
  41. delta_diagonal_rod,
  42. delta_segments_per_second,
  43. delta_calibration_radius,
  44. delta_tower_angle_trim[ABC];
  45. float delta_tower[ABC][2],
  46. delta_diagonal_rod_2_tower[ABC],
  47. delta_clip_start_height = Z_MAX_POS;
  48. float delta_safe_distance_from_top();
  49. /**
  50. * Recalculate factors used for delta kinematics whenever
  51. * settings have been changed (e.g., by M665).
  52. */
  53. void recalc_delta_settings() {
  54. const float trt[ABC] = DELTA_RADIUS_TRIM_TOWER,
  55. drt[ABC] = DELTA_DIAGONAL_ROD_TRIM_TOWER;
  56. delta_tower[A_AXIS][X_AXIS] = cos(RADIANS(210 + delta_tower_angle_trim[A_AXIS])) * (delta_radius + trt[A_AXIS]); // front left tower
  57. delta_tower[A_AXIS][Y_AXIS] = sin(RADIANS(210 + delta_tower_angle_trim[A_AXIS])) * (delta_radius + trt[A_AXIS]);
  58. delta_tower[B_AXIS][X_AXIS] = cos(RADIANS(330 + delta_tower_angle_trim[B_AXIS])) * (delta_radius + trt[B_AXIS]); // front right tower
  59. delta_tower[B_AXIS][Y_AXIS] = sin(RADIANS(330 + delta_tower_angle_trim[B_AXIS])) * (delta_radius + trt[B_AXIS]);
  60. delta_tower[C_AXIS][X_AXIS] = cos(RADIANS( 90 + delta_tower_angle_trim[C_AXIS])) * (delta_radius + trt[C_AXIS]); // back middle tower
  61. delta_tower[C_AXIS][Y_AXIS] = sin(RADIANS( 90 + delta_tower_angle_trim[C_AXIS])) * (delta_radius + trt[C_AXIS]);
  62. delta_diagonal_rod_2_tower[A_AXIS] = sq(delta_diagonal_rod + drt[A_AXIS]);
  63. delta_diagonal_rod_2_tower[B_AXIS] = sq(delta_diagonal_rod + drt[B_AXIS]);
  64. delta_diagonal_rod_2_tower[C_AXIS] = sq(delta_diagonal_rod + drt[C_AXIS]);
  65. update_software_endstops(Z_AXIS);
  66. axis_homed[X_AXIS] = axis_homed[Y_AXIS] = axis_homed[Z_AXIS] = false;
  67. }
  68. /**
  69. * Delta Inverse Kinematics
  70. *
  71. * Calculate the tower positions for a given machine
  72. * position, storing the result in the delta[] array.
  73. *
  74. * This is an expensive calculation, requiring 3 square
  75. * roots per segmented linear move, and strains the limits
  76. * of a Mega2560 with a Graphical Display.
  77. *
  78. * Suggested optimizations include:
  79. *
  80. * - Disable the home_offset (M206) and/or position_shift (G92)
  81. * features to remove up to 12 float additions.
  82. *
  83. * - Use a fast-inverse-sqrt function and add the reciprocal.
  84. * (see above)
  85. */
  86. #if ENABLED(DELTA_FAST_SQRT) && defined(__AVR__)
  87. /**
  88. * Fast inverse sqrt from Quake III Arena
  89. * See: https://en.wikipedia.org/wiki/Fast_inverse_square_root
  90. */
  91. float Q_rsqrt(float number) {
  92. long i;
  93. float x2, y;
  94. const float threehalfs = 1.5f;
  95. x2 = number * 0.5f;
  96. y = number;
  97. i = * ( long * ) &y; // evil floating point bit level hacking
  98. i = 0x5F3759DF - ( i >> 1 ); // what the f***?
  99. y = * ( float * ) &i;
  100. y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
  101. // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
  102. return y;
  103. }
  104. #endif
  105. #define DELTA_DEBUG(VAR) do { \
  106. SERIAL_ECHOPAIR("cartesian X:", VAR[X_AXIS]); \
  107. SERIAL_ECHOPAIR(" Y:", VAR[Y_AXIS]); \
  108. SERIAL_ECHOLNPAIR(" Z:", VAR[Z_AXIS]); \
  109. SERIAL_ECHOPAIR("delta A:", delta[A_AXIS]); \
  110. SERIAL_ECHOPAIR(" B:", delta[B_AXIS]); \
  111. SERIAL_ECHOLNPAIR(" C:", delta[C_AXIS]); \
  112. }while(0)
  113. void inverse_kinematics(const float raw[XYZ]) {
  114. #if HOTENDS > 1
  115. // Delta hotend offsets must be applied in Cartesian space with no "spoofing"
  116. const float pos[XYZ] = {
  117. raw[X_AXIS] - hotend_offset[X_AXIS][active_extruder],
  118. raw[Y_AXIS] - hotend_offset[Y_AXIS][active_extruder],
  119. raw[Z_AXIS]
  120. };
  121. DELTA_IK(pos);
  122. //DELTA_DEBUG(pos);
  123. #else
  124. DELTA_IK(raw);
  125. //DELTA_DEBUG(raw);
  126. #endif
  127. }
  128. /**
  129. * Calculate the highest Z position where the
  130. * effector has the full range of XY motion.
  131. */
  132. float delta_safe_distance_from_top() {
  133. float cartesian[XYZ] = { 0, 0, 0 };
  134. inverse_kinematics(cartesian);
  135. float centered_extent = delta[A_AXIS];
  136. cartesian[Y_AXIS] = DELTA_PRINTABLE_RADIUS;
  137. inverse_kinematics(cartesian);
  138. return FABS(centered_extent - delta[A_AXIS]);
  139. }
  140. /**
  141. * Delta Forward Kinematics
  142. *
  143. * See the Wikipedia article "Trilateration"
  144. * https://en.wikipedia.org/wiki/Trilateration
  145. *
  146. * Establish a new coordinate system in the plane of the
  147. * three carriage points. This system has its origin at
  148. * tower1, with tower2 on the X axis. Tower3 is in the X-Y
  149. * plane with a Z component of zero.
  150. * We will define unit vectors in this coordinate system
  151. * in our original coordinate system. Then when we calculate
  152. * the Xnew, Ynew and Znew values, we can translate back into
  153. * the original system by moving along those unit vectors
  154. * by the corresponding values.
  155. *
  156. * Variable names matched to Marlin, c-version, and avoid the
  157. * use of any vector library.
  158. *
  159. * by Andreas Hardtung 2016-06-07
  160. * based on a Java function from "Delta Robot Kinematics V3"
  161. * by Steve Graves
  162. *
  163. * The result is stored in the cartes[] array.
  164. */
  165. void forward_kinematics_DELTA(float z1, float z2, float z3) {
  166. // Create a vector in old coordinates along x axis of new coordinate
  167. 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 };
  168. // Get the Magnitude of vector.
  169. float d = SQRT( sq(p12[0]) + sq(p12[1]) + sq(p12[2]) );
  170. // Create unit vector by dividing by magnitude.
  171. float ex[3] = { p12[0] / d, p12[1] / d, p12[2] / d };
  172. // Get the vector from the origin of the new system to the third point.
  173. float 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 };
  174. // Use the dot product to find the component of this vector on the X axis.
  175. float i = ex[0] * p13[0] + ex[1] * p13[1] + ex[2] * p13[2];
  176. // Create a vector along the x axis that represents the x component of p13.
  177. float iex[3] = { ex[0] * i, ex[1] * i, ex[2] * i };
  178. // Subtract the X component from the original vector leaving only Y. We use the
  179. // variable that will be the unit vector after we scale it.
  180. float ey[3] = { p13[0] - iex[0], p13[1] - iex[1], p13[2] - iex[2] };
  181. // The magnitude of Y component
  182. float j = SQRT( sq(ey[0]) + sq(ey[1]) + sq(ey[2]) );
  183. // Convert to a unit vector
  184. ey[0] /= j; ey[1] /= j; ey[2] /= j;
  185. // The cross product of the unit x and y is the unit z
  186. // float[] ez = vectorCrossProd(ex, ey);
  187. float ez[3] = {
  188. ex[1] * ey[2] - ex[2] * ey[1],
  189. ex[2] * ey[0] - ex[0] * ey[2],
  190. ex[0] * ey[1] - ex[1] * ey[0]
  191. };
  192. // We now have the d, i and j values defined in Wikipedia.
  193. // Plug them into the equations defined in Wikipedia for Xnew, Ynew and Znew
  194. float Xnew = (delta_diagonal_rod_2_tower[A_AXIS] - delta_diagonal_rod_2_tower[B_AXIS] + sq(d)) / (d * 2),
  195. Ynew = ((delta_diagonal_rod_2_tower[A_AXIS] - delta_diagonal_rod_2_tower[C_AXIS] + HYPOT2(i, j)) / 2 - i * Xnew) / j,
  196. Znew = SQRT(delta_diagonal_rod_2_tower[A_AXIS] - HYPOT2(Xnew, Ynew));
  197. // Start from the origin of the old coordinates and add vectors in the
  198. // old coords that represent the Xnew, Ynew and Znew to find the point
  199. // in the old system.
  200. cartes[X_AXIS] = delta_tower[A_AXIS][X_AXIS] + ex[0] * Xnew + ey[0] * Ynew - ez[0] * Znew;
  201. cartes[Y_AXIS] = delta_tower[A_AXIS][Y_AXIS] + ex[1] * Xnew + ey[1] * Ynew - ez[1] * Znew;
  202. cartes[Z_AXIS] = z1 + ex[2] * Xnew + ey[2] * Ynew - ez[2] * Znew;
  203. }
  204. #if ENABLED(SENSORLESS_HOMING)
  205. inline void delta_sensorless_homing(const bool on=true) {
  206. sensorless_homing_per_axis(A_AXIS, on);
  207. sensorless_homing_per_axis(B_AXIS, on);
  208. sensorless_homing_per_axis(C_AXIS, on);
  209. }
  210. #endif
  211. /**
  212. * A delta can only safely home all axes at the same time
  213. * This is like quick_home_xy() but for 3 towers.
  214. */
  215. bool home_delta() {
  216. #if ENABLED(DEBUG_LEVELING_FEATURE)
  217. if (DEBUGGING(LEVELING)) DEBUG_POS(">>> home_delta", current_position);
  218. #endif
  219. // Init the current position of all carriages to 0,0,0
  220. ZERO(current_position);
  221. sync_plan_position();
  222. // Disable stealthChop if used. Enable diag1 pin on driver.
  223. #if ENABLED(SENSORLESS_HOMING)
  224. delta_sensorless_homing();
  225. #endif
  226. // Move all carriages together linearly until an endstop is hit.
  227. current_position[X_AXIS] = current_position[Y_AXIS] = current_position[Z_AXIS] = (delta_height + 10);
  228. feedrate_mm_s = homing_feedrate(X_AXIS);
  229. line_to_current_position();
  230. stepper.synchronize();
  231. // Re-enable stealthChop if used. Disable diag1 pin on driver.
  232. #if ENABLED(SENSORLESS_HOMING)
  233. delta_sensorless_homing(false);
  234. #endif
  235. // If an endstop was not hit, then damage can occur if homing is continued.
  236. // This can occur if the delta height not set correctly.
  237. if (!(Endstops::endstop_hit_bits & (_BV(X_MAX) | _BV(Y_MAX) | _BV(Z_MAX)))) {
  238. LCD_MESSAGEPGM(MSG_ERR_HOMING_FAILED);
  239. SERIAL_ERROR_START();
  240. SERIAL_ERRORLNPGM(MSG_ERR_HOMING_FAILED);
  241. return false;
  242. }
  243. endstops.hit_on_purpose(); // clear endstop hit flags
  244. // At least one carriage has reached the top.
  245. // Now re-home each carriage separately.
  246. HOMEAXIS(A);
  247. HOMEAXIS(B);
  248. HOMEAXIS(C);
  249. // Set all carriages to their home positions
  250. // Do this here all at once for Delta, because
  251. // XYZ isn't ABC. Applying this per-tower would
  252. // give the impression that they are the same.
  253. LOOP_XYZ(i) set_axis_is_at_home((AxisEnum)i);
  254. SYNC_PLAN_POSITION_KINEMATIC();
  255. #if ENABLED(DEBUG_LEVELING_FEATURE)
  256. if (DEBUGGING(LEVELING)) DEBUG_POS("<<< home_delta", current_position);
  257. #endif
  258. return true;
  259. }
  260. #endif // DELTA