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.

scara.cpp 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. * scara.cpp
  24. */
  25. #include "../inc/MarlinConfig.h"
  26. #if IS_SCARA
  27. #include "scara.h"
  28. #include "motion.h"
  29. #include "planner.h"
  30. #if ENABLED(AXEL_TPARA)
  31. #include "endstops.h"
  32. #include "../MarlinCore.h"
  33. #endif
  34. float delta_segments_per_second = TERN(AXEL_TPARA, TPARA_SEGMENTS_PER_SECOND, SCARA_SEGMENTS_PER_SECOND);
  35. void scara_set_axis_is_at_home(const AxisEnum axis) {
  36. if (axis == Z_AXIS)
  37. current_position.z = Z_HOME_POS;
  38. else {
  39. #if ENABLED(MORGAN_SCARA)
  40. // MORGAN_SCARA uses arm angles for AB home position
  41. ab_float_t homeposition = { SCARA_OFFSET_THETA1, SCARA_OFFSET_THETA2 };
  42. //DEBUG_ECHOLNPAIR("homeposition A:", homeposition.a, " B:", homeposition.b);
  43. #elif ENABLED(MP_SCARA)
  44. // MP_SCARA uses a Cartesian XY home position
  45. xyz_pos_t homeposition = { X_HOME_POS, Y_HOME_POS, Z_HOME_POS };
  46. //DEBUG_ECHOPGM("homeposition");
  47. //DEBUG_ECHOLNPAIR_P(SP_X_LBL, homeposition.x, SP_Y_LBL, homeposition.y);
  48. #elif ENABLED(AXEL_TPARA)
  49. xyz_pos_t homeposition = { X_HOME_POS, Y_HOME_POS, Z_HOME_POS };
  50. //DEBUG_ECHOPGM("homeposition");
  51. //DEBUG_ECHOLNPAIR_P(SP_X_LBL, homeposition.x, SP_Y_LBL, homeposition.y, SP_Z_LBL, homeposition.z);
  52. #endif
  53. #if ENABLED(MORGAN_SCARA)
  54. delta = homeposition;
  55. #else
  56. inverse_kinematics(homeposition);
  57. #endif
  58. #if EITHER(MORGAN_SCARA, MP_SCARA)
  59. forward_kinematics(delta.a, delta.b);
  60. #elif ENABLED(AXEL_TPARA)
  61. forward_kinematics(delta.a, delta.b, delta.c);
  62. #endif
  63. current_position[axis] = cartes[axis];
  64. //DEBUG_ECHOPGM("Cartesian");
  65. //DEBUG_ECHOLNPAIR_P(SP_X_LBL, current_position.x, SP_Y_LBL, current_position.y);
  66. update_software_endstops(axis);
  67. }
  68. }
  69. #if EITHER(MORGAN_SCARA, MP_SCARA)
  70. static constexpr xy_pos_t scara_offset = { SCARA_OFFSET_X, SCARA_OFFSET_Y };
  71. /**
  72. * Morgan SCARA Forward Kinematics. Results in 'cartes'.
  73. * Maths and first version by QHARLEY.
  74. * Integrated into Marlin and slightly restructured by Joachim Cerny.
  75. */
  76. void forward_kinematics(const float &a, const float &b) {
  77. const float a_sin = sin(RADIANS(a)) * L1,
  78. a_cos = cos(RADIANS(a)) * L1,
  79. b_sin = sin(RADIANS(b + TERN0(MP_SCARA, a))) * L2,
  80. b_cos = cos(RADIANS(b + TERN0(MP_SCARA, a))) * L2;
  81. cartes.x = a_cos + b_cos + scara_offset.x; // theta
  82. cartes.y = a_sin + b_sin + scara_offset.y; // phi
  83. /*
  84. DEBUG_ECHOLNPAIR(
  85. "SCARA FK Angle a=", a,
  86. " b=", b,
  87. " a_sin=", a_sin,
  88. " a_cos=", a_cos,
  89. " b_sin=", b_sin,
  90. " b_cos=", b_cos
  91. );
  92. DEBUG_ECHOLNPAIR(" cartes (X,Y) = "(cartes.x, ", ", cartes.y, ")");
  93. //*/
  94. }
  95. /**
  96. * Morgan SCARA Inverse Kinematics. Results are stored in 'delta'.
  97. *
  98. * See https://reprap.org/forum/read.php?185,283327
  99. *
  100. * Maths and first version by QHARLEY.
  101. * Integrated into Marlin and slightly restructured by Joachim Cerny.
  102. */
  103. void inverse_kinematics(const xyz_pos_t &raw) {
  104. float C2, S2, SK1, SK2, THETA, PSI;
  105. // Translate SCARA to standard XY with scaling factor
  106. const xy_pos_t spos = raw - scara_offset;
  107. const float H2 = HYPOT2(spos.x, spos.y);
  108. if (L1 == L2)
  109. C2 = H2 / L1_2_2 - 1;
  110. else
  111. C2 = (H2 - (L1_2 + L2_2)) / (2.0f * L1 * L2);
  112. LIMIT(C2, -1, 1);
  113. S2 = SQRT(1.0f - sq(C2));
  114. // Unrotated Arm1 plus rotated Arm2 gives the distance from Center to End
  115. SK1 = L1 + L2 * C2;
  116. // Rotated Arm2 gives the distance from Arm1 to Arm2
  117. SK2 = L2 * S2;
  118. // Angle of Arm1 is the difference between Center-to-End angle and the Center-to-Elbow
  119. THETA = ATAN2(SK1, SK2) - ATAN2(spos.x, spos.y);
  120. // Angle of Arm2
  121. PSI = ATAN2(S2, C2);
  122. delta.set(DEGREES(THETA), DEGREES(PSI + TERN0(MORGAN_SCARA, THETA)), raw.z);
  123. /*
  124. DEBUG_POS("SCARA IK", raw);
  125. DEBUG_POS("SCARA IK", delta);
  126. DEBUG_ECHOLNPAIR(" SCARA (x,y) ", sx, ",", sy, " C2=", C2, " S2=", S2, " Theta=", THETA, " Psi=", PSI);
  127. //*/
  128. }
  129. #elif ENABLED(MP_SCARA)
  130. void inverse_kinematics(const xyz_pos_t &raw) {
  131. const float x = raw.x, y = raw.y, c = HYPOT(x, y),
  132. THETA3 = ATAN2(y, x),
  133. THETA1 = THETA3 + ACOS((sq(c) + sq(L1) - sq(L2)) / (2.0f * c * L1)),
  134. THETA2 = THETA3 - ACOS((sq(c) + sq(L2) - sq(L1)) / (2.0f * c * L2));
  135. delta.set(DEGREES(THETA1), DEGREES(THETA2), raw.z);
  136. /*
  137. DEBUG_POS("SCARA IK", raw);
  138. DEBUG_POS("SCARA IK", delta);
  139. SERIAL_ECHOLNPAIR(" SCARA (x,y) ", x, ",", y," Theta1=", THETA1, " Theta2=", THETA2);
  140. //*/
  141. }
  142. #elif ENABLED(AXEL_TPARA)
  143. static constexpr xyz_pos_t robot_offset = { TPARA_OFFSET_X, TPARA_OFFSET_Y, TPARA_OFFSET_Z };
  144. // Convert ABC inputs in degrees to XYZ outputs in mm
  145. void forward_kinematics(const float &a, const float &b, const float &c) {
  146. const float w = c - b,
  147. r = L1 * cos(RADIANS(b)) + L2 * sin(RADIANS(w - (90 - b))),
  148. x = r * cos(RADIANS(a)),
  149. y = r * sin(RADIANS(a)),
  150. rho2 = L1_2 + L2_2 - 2.0f * L1 * L2 * cos(RADIANS(w));
  151. cartes = robot_offset + xyz_pos_t({ x, y, SQRT(rho2 - x * x - y * y) });
  152. }
  153. // Home YZ together, then X (or all at once). Based on quick_home_xy & home_delta
  154. void home_TPARA() {
  155. // Init the current position of all carriages to 0,0,0
  156. current_position.reset();
  157. destination.reset();
  158. sync_plan_position();
  159. // Disable stealthChop if used. Enable diag1 pin on driver.
  160. #if ENABLED(SENSORLESS_HOMING)
  161. TERN_(X_SENSORLESS, sensorless_t stealth_states_x = start_sensorless_homing_per_axis(X_AXIS));
  162. TERN_(Y_SENSORLESS, sensorless_t stealth_states_y = start_sensorless_homing_per_axis(Y_AXIS));
  163. TERN_(Z_SENSORLESS, sensorless_t stealth_states_z = start_sensorless_homing_per_axis(Z_AXIS));
  164. #endif
  165. // const int x_axis_home_dir = x_home_dir(active_extruder);
  166. // const xy_pos_t pos { max_length(X_AXIS) , max_length(Y_AXIS) };
  167. // const float mlz = max_length(X_AXIS),
  168. // Move all carriages together linearly until an endstop is hit.
  169. //do_blocking_move_to_xy_z(pos, mlz, homing_feedrate(Z_AXIS));
  170. current_position.x = 0 ;
  171. current_position.y = 0 ;
  172. current_position.z = max_length(Z_AXIS) ;
  173. line_to_current_position(homing_feedrate(Z_AXIS));
  174. planner.synchronize();
  175. // Re-enable stealthChop if used. Disable diag1 pin on driver.
  176. #if ENABLED(SENSORLESS_HOMING)
  177. TERN_(X_SENSORLESS, end_sensorless_homing_per_axis(X_AXIS, stealth_states_x));
  178. TERN_(Y_SENSORLESS, end_sensorless_homing_per_axis(Y_AXIS, stealth_states_y));
  179. TERN_(Z_SENSORLESS, end_sensorless_homing_per_axis(Z_AXIS, stealth_states_z));
  180. #endif
  181. endstops.validate_homing_move();
  182. // At least one motor has reached its endstop.
  183. // Now re-home each motor separately.
  184. homeaxis(A_AXIS);
  185. homeaxis(C_AXIS);
  186. homeaxis(B_AXIS);
  187. // Set all carriages to their home positions
  188. // Do this here all at once for Delta, because
  189. // XYZ isn't ABC. Applying this per-tower would
  190. // give the impression that they are the same.
  191. LOOP_XYZ(i) set_axis_is_at_home((AxisEnum)i);
  192. sync_plan_position();
  193. }
  194. void inverse_kinematics(const xyz_pos_t &raw) {
  195. const xyz_pos_t spos = raw - robot_offset;
  196. const float RXY = SQRT(HYPOT2(spos.x, spos.y)),
  197. RHO2 = NORMSQ(spos.x, spos.y, spos.z),
  198. //RHO = SQRT(RHO2),
  199. LSS = L1_2 + L2_2,
  200. LM = 2.0f * L1 * L2,
  201. CG = (LSS - RHO2) / LM,
  202. SG = SQRT(1 - POW(CG, 2)), // Method 2
  203. K1 = L1 - L2 * CG,
  204. K2 = L2 * SG,
  205. // Angle of Body Joint
  206. THETA = ATAN2(spos.y, spos.x),
  207. // Angle of Elbow Joint
  208. //GAMMA = ACOS(CG),
  209. GAMMA = ATAN2(SG, CG), // Method 2
  210. // Angle of Shoulder Joint, elevation angle measured from horizontal (r+)
  211. //PHI = asin(spos.z/RHO) + asin(L2 * sin(GAMMA) / RHO),
  212. PHI = ATAN2(spos.z, RXY) + ATAN2(K2, K1), // Method 2
  213. // Elbow motor angle measured from horizontal, same frame as shoulder (r+)
  214. PSI = PHI + GAMMA;
  215. delta.set(DEGREES(THETA), DEGREES(PHI), DEGREES(PSI));
  216. //SERIAL_ECHOLNPAIR(" SCARA (x,y,z) ", spos.x , ",", spos.y, ",", spos.z, " Rho=", RHO, " Rho2=", RHO2, " Theta=", THETA, " Phi=", PHI, " Psi=", PSI, " Gamma=", GAMMA);
  217. }
  218. #endif
  219. void scara_report_positions() {
  220. SERIAL_ECHOLNPAIR("SCARA Theta:", planner.get_axis_position_degrees(A_AXIS)
  221. #if ENABLED(AXEL_TPARA)
  222. , " Phi:", planner.get_axis_position_degrees(B_AXIS)
  223. , " Psi:", planner.get_axis_position_degrees(C_AXIS)
  224. #else
  225. , " Psi" TERN_(MORGAN_SCARA, "+Theta") ":", planner.get_axis_position_degrees(B_AXIS)
  226. #endif
  227. );
  228. SERIAL_EOL();
  229. }
  230. #endif // IS_SCARA