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 10KB

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