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.

motion.cpp 43KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  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. * motion.cpp
  24. */
  25. #include "motion.h"
  26. #include "endstops.h"
  27. #include "stepper.h"
  28. #include "planner.h"
  29. #include "temperature.h"
  30. #include "../gcode/gcode.h"
  31. #include "../inc/MarlinConfig.h"
  32. #if IS_SCARA
  33. #include "../libs/buzzer.h"
  34. #include "../lcd/ultralcd.h"
  35. #endif
  36. #if HAS_BED_PROBE
  37. #include "probe.h"
  38. #endif
  39. #if HAS_LEVELING
  40. #include "../feature/bedlevel/bedlevel.h"
  41. #endif
  42. #if HAS_AXIS_UNHOMED_ERR && ENABLED(ULTRA_LCD)
  43. #include "../lcd/ultralcd.h"
  44. #endif
  45. #if ENABLED(SENSORLESS_HOMING)
  46. #include "../feature/tmc2130.h"
  47. #endif
  48. #define XYZ_CONSTS(type, array, CONFIG) const PROGMEM type array##_P[XYZ] = { X_##CONFIG, Y_##CONFIG, Z_##CONFIG }
  49. XYZ_CONSTS(float, base_min_pos, MIN_POS);
  50. XYZ_CONSTS(float, base_max_pos, MAX_POS);
  51. XYZ_CONSTS(float, base_home_pos, HOME_POS);
  52. XYZ_CONSTS(float, max_length, MAX_LENGTH);
  53. XYZ_CONSTS(float, home_bump_mm, HOME_BUMP_MM);
  54. XYZ_CONSTS(signed char, home_dir, HOME_DIR);
  55. // Relative Mode. Enable with G91, disable with G90.
  56. bool relative_mode = false;
  57. /**
  58. * Cartesian Current Position
  59. * Used to track the native machine position as moves are queued.
  60. * Used by 'line_to_current_position' to do a move after changing it.
  61. * Used by 'SYNC_PLAN_POSITION_KINEMATIC' to update 'planner.position'.
  62. */
  63. float current_position[XYZE] = { 0.0 };
  64. /**
  65. * Cartesian Destination
  66. * A temporary position, usually applied to 'current_position'.
  67. * Set with 'get_destination_from_command' or 'set_destination_from_current'.
  68. * 'line_to_destination' sets 'current_position' to 'destination'.
  69. */
  70. float destination[XYZE] = { 0.0 };
  71. // The active extruder (tool). Set with T<extruder> command.
  72. uint8_t active_extruder = 0;
  73. // Extruder offsets
  74. #if HOTENDS > 1
  75. float hotend_offset[XYZ][HOTENDS]; // Initialized by settings.load()
  76. #endif
  77. // The feedrate for the current move, often used as the default if
  78. // no other feedrate is specified. Overridden for special moves.
  79. // Set by the last G0 through G5 command's "F" parameter.
  80. // Functions that override this for custom moves *must always* restore it!
  81. float feedrate_mm_s = MMM_TO_MMS(1500.0);
  82. int16_t feedrate_percentage = 100;
  83. // Homing feedrate is const progmem - compare to constexpr in the header
  84. const float homing_feedrate_mm_s[4] PROGMEM = {
  85. #if ENABLED(DELTA)
  86. MMM_TO_MMS(HOMING_FEEDRATE_Z), MMM_TO_MMS(HOMING_FEEDRATE_Z),
  87. #else
  88. MMM_TO_MMS(HOMING_FEEDRATE_XY), MMM_TO_MMS(HOMING_FEEDRATE_XY),
  89. #endif
  90. MMM_TO_MMS(HOMING_FEEDRATE_Z), 0
  91. };
  92. // Cartesian conversion result goes here:
  93. float cartes[XYZ];
  94. // Until kinematics.cpp is created, create this here
  95. #if IS_KINEMATIC
  96. float delta[ABC];
  97. #endif
  98. /**
  99. * The workspace can be offset by some commands, or
  100. * these offsets may be omitted to save on computation.
  101. */
  102. #if HAS_WORKSPACE_OFFSET
  103. #if HAS_POSITION_SHIFT
  104. // The distance that XYZ has been offset by G92. Reset by G28.
  105. float position_shift[XYZ] = { 0 };
  106. #endif
  107. #if HAS_HOME_OFFSET
  108. // This offset is added to the configured home position.
  109. // Set by M206, M428, or menu item. Saved to EEPROM.
  110. float home_offset[XYZ] = { 0 };
  111. #endif
  112. #if HAS_HOME_OFFSET && HAS_POSITION_SHIFT
  113. // The above two are combined to save on computes
  114. float workspace_offset[XYZ] = { 0 };
  115. #endif
  116. #endif
  117. #if OLDSCHOOL_ABL
  118. float xy_probe_feedrate_mm_s = MMM_TO_MMS(XY_PROBE_SPEED);
  119. #endif
  120. /**
  121. * Output the current position to serial
  122. */
  123. void report_current_position() {
  124. SERIAL_PROTOCOLPGM("X:");
  125. SERIAL_PROTOCOL(current_position[X_AXIS]);
  126. SERIAL_PROTOCOLPGM(" Y:");
  127. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  128. SERIAL_PROTOCOLPGM(" Z:");
  129. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  130. SERIAL_PROTOCOLPGM(" E:");
  131. SERIAL_PROTOCOL(current_position[E_AXIS]);
  132. stepper.report_positions();
  133. #if IS_SCARA
  134. scara_report_positions();
  135. #endif
  136. }
  137. /**
  138. * sync_plan_position
  139. *
  140. * Set the planner/stepper positions directly from current_position with
  141. * no kinematic translation. Used for homing axes and cartesian/core syncing.
  142. */
  143. void sync_plan_position() {
  144. #if ENABLED(DEBUG_LEVELING_FEATURE)
  145. if (DEBUGGING(LEVELING)) DEBUG_POS("sync_plan_position", current_position);
  146. #endif
  147. planner.set_position_mm(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  148. }
  149. void sync_plan_position_e() { planner.set_e_position_mm(current_position[E_AXIS]); }
  150. /**
  151. * Get the stepper positions in the cartes[] array.
  152. * Forward kinematics are applied for DELTA and SCARA.
  153. *
  154. * The result is in the current coordinate space with
  155. * leveling applied. The coordinates need to be run through
  156. * unapply_leveling to obtain the "ideal" coordinates
  157. * suitable for current_position, etc.
  158. */
  159. void get_cartesian_from_steppers() {
  160. #if ENABLED(DELTA)
  161. forward_kinematics_DELTA(
  162. stepper.get_axis_position_mm(A_AXIS),
  163. stepper.get_axis_position_mm(B_AXIS),
  164. stepper.get_axis_position_mm(C_AXIS)
  165. );
  166. #else
  167. #if IS_SCARA
  168. forward_kinematics_SCARA(
  169. stepper.get_axis_position_degrees(A_AXIS),
  170. stepper.get_axis_position_degrees(B_AXIS)
  171. );
  172. #else
  173. cartes[X_AXIS] = stepper.get_axis_position_mm(X_AXIS);
  174. cartes[Y_AXIS] = stepper.get_axis_position_mm(Y_AXIS);
  175. #endif
  176. cartes[Z_AXIS] = stepper.get_axis_position_mm(Z_AXIS);
  177. #endif
  178. }
  179. /**
  180. * Set the current_position for an axis based on
  181. * the stepper positions, removing any leveling that
  182. * may have been applied.
  183. */
  184. void set_current_from_steppers_for_axis(const AxisEnum axis) {
  185. get_cartesian_from_steppers();
  186. #if PLANNER_LEVELING
  187. planner.unapply_leveling(cartes);
  188. #endif
  189. if (axis == ALL_AXES)
  190. COPY(current_position, cartes);
  191. else
  192. current_position[axis] = cartes[axis];
  193. }
  194. /**
  195. * Move the planner to the current position from wherever it last moved
  196. * (or from wherever it has been told it is located).
  197. */
  198. void line_to_current_position() {
  199. planner.buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate_mm_s, active_extruder);
  200. }
  201. /**
  202. * Move the planner to the position stored in the destination array, which is
  203. * used by G0/G1/G2/G3/G5 and many other functions to set a destination.
  204. */
  205. void line_to_destination(const float fr_mm_s) {
  206. planner.buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], fr_mm_s, active_extruder);
  207. }
  208. #if IS_KINEMATIC
  209. void sync_plan_position_kinematic() {
  210. #if ENABLED(DEBUG_LEVELING_FEATURE)
  211. if (DEBUGGING(LEVELING)) DEBUG_POS("sync_plan_position_kinematic", current_position);
  212. #endif
  213. planner.set_position_mm_kinematic(current_position);
  214. }
  215. /**
  216. * Calculate delta, start a line, and set current_position to destination
  217. */
  218. void prepare_uninterpolated_move_to_destination(const float fr_mm_s/*=0.0*/) {
  219. #if ENABLED(DEBUG_LEVELING_FEATURE)
  220. if (DEBUGGING(LEVELING)) DEBUG_POS("prepare_uninterpolated_move_to_destination", destination);
  221. #endif
  222. gcode.refresh_cmd_timeout();
  223. #if UBL_DELTA
  224. // ubl segmented line will do z-only moves in single segment
  225. ubl.prepare_segmented_line_to(destination, MMS_SCALED(fr_mm_s ? fr_mm_s : feedrate_mm_s));
  226. #else
  227. if ( current_position[X_AXIS] == destination[X_AXIS]
  228. && current_position[Y_AXIS] == destination[Y_AXIS]
  229. && current_position[Z_AXIS] == destination[Z_AXIS]
  230. && current_position[E_AXIS] == destination[E_AXIS]
  231. ) return;
  232. planner.buffer_line_kinematic(destination, MMS_SCALED(fr_mm_s ? fr_mm_s : feedrate_mm_s), active_extruder);
  233. #endif
  234. set_current_from_destination();
  235. }
  236. #endif // IS_KINEMATIC
  237. /**
  238. * Plan a move to (X, Y, Z) and set the current_position
  239. * The final current_position may not be the one that was requested
  240. */
  241. void do_blocking_move_to(const float &rx, const float &ry, const float &rz, const float &fr_mm_s/*=0.0*/) {
  242. const float old_feedrate_mm_s = feedrate_mm_s;
  243. #if ENABLED(DEBUG_LEVELING_FEATURE)
  244. if (DEBUGGING(LEVELING)) print_xyz(PSTR(">>> do_blocking_move_to"), NULL, rx, ry, rz);
  245. #endif
  246. #if ENABLED(DELTA)
  247. if (!position_is_reachable(rx, ry)) return;
  248. feedrate_mm_s = fr_mm_s ? fr_mm_s : XY_PROBE_FEEDRATE_MM_S;
  249. set_destination_from_current(); // sync destination at the start
  250. #if ENABLED(DEBUG_LEVELING_FEATURE)
  251. if (DEBUGGING(LEVELING)) DEBUG_POS("set_destination_from_current", destination);
  252. #endif
  253. // when in the danger zone
  254. if (current_position[Z_AXIS] > delta_clip_start_height) {
  255. if (rz > delta_clip_start_height) { // staying in the danger zone
  256. destination[X_AXIS] = rx; // move directly (uninterpolated)
  257. destination[Y_AXIS] = ry;
  258. destination[Z_AXIS] = rz;
  259. prepare_uninterpolated_move_to_destination(); // set_current_from_destination()
  260. #if ENABLED(DEBUG_LEVELING_FEATURE)
  261. if (DEBUGGING(LEVELING)) DEBUG_POS("danger zone move", current_position);
  262. #endif
  263. return;
  264. }
  265. else {
  266. destination[Z_AXIS] = delta_clip_start_height;
  267. prepare_uninterpolated_move_to_destination(); // set_current_from_destination()
  268. #if ENABLED(DEBUG_LEVELING_FEATURE)
  269. if (DEBUGGING(LEVELING)) DEBUG_POS("zone border move", current_position);
  270. #endif
  271. }
  272. }
  273. if (rz > current_position[Z_AXIS]) { // raising?
  274. destination[Z_AXIS] = rz;
  275. prepare_uninterpolated_move_to_destination(); // set_current_from_destination()
  276. #if ENABLED(DEBUG_LEVELING_FEATURE)
  277. if (DEBUGGING(LEVELING)) DEBUG_POS("z raise move", current_position);
  278. #endif
  279. }
  280. destination[X_AXIS] = rx;
  281. destination[Y_AXIS] = ry;
  282. prepare_move_to_destination(); // set_current_from_destination()
  283. #if ENABLED(DEBUG_LEVELING_FEATURE)
  284. if (DEBUGGING(LEVELING)) DEBUG_POS("xy move", current_position);
  285. #endif
  286. if (rz < current_position[Z_AXIS]) { // lowering?
  287. destination[Z_AXIS] = rz;
  288. prepare_uninterpolated_move_to_destination(); // set_current_from_destination()
  289. #if ENABLED(DEBUG_LEVELING_FEATURE)
  290. if (DEBUGGING(LEVELING)) DEBUG_POS("z lower move", current_position);
  291. #endif
  292. }
  293. #elif IS_SCARA
  294. if (!position_is_reachable(rx, ry)) return;
  295. set_destination_from_current();
  296. // If Z needs to raise, do it before moving XY
  297. if (destination[Z_AXIS] < rz) {
  298. destination[Z_AXIS] = rz;
  299. prepare_uninterpolated_move_to_destination(fr_mm_s ? fr_mm_s : homing_feedrate(Z_AXIS));
  300. }
  301. destination[X_AXIS] = rx;
  302. destination[Y_AXIS] = ry;
  303. prepare_uninterpolated_move_to_destination(fr_mm_s ? fr_mm_s : XY_PROBE_FEEDRATE_MM_S);
  304. // If Z needs to lower, do it after moving XY
  305. if (destination[Z_AXIS] > rz) {
  306. destination[Z_AXIS] = rz;
  307. prepare_uninterpolated_move_to_destination(fr_mm_s ? fr_mm_s : homing_feedrate(Z_AXIS));
  308. }
  309. #else
  310. // If Z needs to raise, do it before moving XY
  311. if (current_position[Z_AXIS] < rz) {
  312. feedrate_mm_s = fr_mm_s ? fr_mm_s : homing_feedrate(Z_AXIS);
  313. current_position[Z_AXIS] = rz;
  314. line_to_current_position();
  315. }
  316. feedrate_mm_s = fr_mm_s ? fr_mm_s : XY_PROBE_FEEDRATE_MM_S;
  317. current_position[X_AXIS] = rx;
  318. current_position[Y_AXIS] = ry;
  319. line_to_current_position();
  320. // If Z needs to lower, do it after moving XY
  321. if (current_position[Z_AXIS] > rz) {
  322. feedrate_mm_s = fr_mm_s ? fr_mm_s : homing_feedrate(Z_AXIS);
  323. current_position[Z_AXIS] = rz;
  324. line_to_current_position();
  325. }
  326. #endif
  327. stepper.synchronize();
  328. feedrate_mm_s = old_feedrate_mm_s;
  329. #if ENABLED(DEBUG_LEVELING_FEATURE)
  330. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("<<< do_blocking_move_to");
  331. #endif
  332. }
  333. void do_blocking_move_to_x(const float &rx, const float &fr_mm_s/*=0.0*/) {
  334. do_blocking_move_to(rx, current_position[Y_AXIS], current_position[Z_AXIS], fr_mm_s);
  335. }
  336. void do_blocking_move_to_z(const float &rz, const float &fr_mm_s/*=0.0*/) {
  337. do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], rz, fr_mm_s);
  338. }
  339. void do_blocking_move_to_xy(const float &rx, const float &ry, const float &fr_mm_s/*=0.0*/) {
  340. do_blocking_move_to(rx, ry, current_position[Z_AXIS], fr_mm_s);
  341. }
  342. //
  343. // Prepare to do endstop or probe moves
  344. // with custom feedrates.
  345. //
  346. // - Save current feedrates
  347. // - Reset the rate multiplier
  348. // - Reset the command timeout
  349. // - Enable the endstops (for endstop moves)
  350. //
  351. void bracket_probe_move(const bool before) {
  352. static float saved_feedrate_mm_s;
  353. static int16_t saved_feedrate_percentage;
  354. #if ENABLED(DEBUG_LEVELING_FEATURE)
  355. if (DEBUGGING(LEVELING)) DEBUG_POS("bracket_probe_move", current_position);
  356. #endif
  357. if (before) {
  358. saved_feedrate_mm_s = feedrate_mm_s;
  359. saved_feedrate_percentage = feedrate_percentage;
  360. feedrate_percentage = 100;
  361. gcode.refresh_cmd_timeout();
  362. }
  363. else {
  364. feedrate_mm_s = saved_feedrate_mm_s;
  365. feedrate_percentage = saved_feedrate_percentage;
  366. gcode.refresh_cmd_timeout();
  367. }
  368. }
  369. void setup_for_endstop_or_probe_move() { bracket_probe_move(true); }
  370. void clean_up_after_endstop_or_probe_move() { bracket_probe_move(false); }
  371. // Software Endstops are based on the configured limits.
  372. float soft_endstop_min[XYZ] = { X_MIN_BED, Y_MIN_BED, Z_MIN_POS },
  373. soft_endstop_max[XYZ] = { X_MAX_BED, Y_MAX_BED, Z_MAX_POS };
  374. #if HAS_SOFTWARE_ENDSTOPS
  375. // Software Endstops are based on the configured limits.
  376. bool soft_endstops_enabled = true;
  377. #if IS_KINEMATIC
  378. float soft_endstop_radius, soft_endstop_radius_2;
  379. #endif
  380. /**
  381. * Constrain the given coordinates to the software endstops.
  382. *
  383. * For DELTA/SCARA the XY constraint is based on the smallest
  384. * radius within the set software endstops.
  385. */
  386. void clamp_to_software_endstops(float target[XYZ]) {
  387. if (!soft_endstops_enabled) return;
  388. #if IS_KINEMATIC
  389. const float dist_2 = HYPOT2(target[X_AXIS], target[Y_AXIS]);
  390. if (dist_2 > soft_endstop_radius_2) {
  391. const float ratio = soft_endstop_radius / SQRT(dist_2); // 200 / 300 = 0.66
  392. target[X_AXIS] *= ratio;
  393. target[Y_AXIS] *= ratio;
  394. }
  395. #else
  396. #if ENABLED(MIN_SOFTWARE_ENDSTOP_X)
  397. NOLESS(target[X_AXIS], soft_endstop_min[X_AXIS]);
  398. #endif
  399. #if ENABLED(MIN_SOFTWARE_ENDSTOP_Y)
  400. NOLESS(target[Y_AXIS], soft_endstop_min[Y_AXIS]);
  401. #endif
  402. #if ENABLED(MAX_SOFTWARE_ENDSTOP_X)
  403. NOMORE(target[X_AXIS], soft_endstop_max[X_AXIS]);
  404. #endif
  405. #if ENABLED(MAX_SOFTWARE_ENDSTOP_Y)
  406. NOMORE(target[Y_AXIS], soft_endstop_max[Y_AXIS]);
  407. #endif
  408. #endif
  409. #if ENABLED(MIN_SOFTWARE_ENDSTOP_Z)
  410. NOLESS(target[Z_AXIS], soft_endstop_min[Z_AXIS]);
  411. #endif
  412. #if ENABLED(MAX_SOFTWARE_ENDSTOP_Z)
  413. NOMORE(target[Z_AXIS], soft_endstop_max[Z_AXIS]);
  414. #endif
  415. }
  416. #endif
  417. #if IS_KINEMATIC && !UBL_DELTA
  418. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  419. #if ENABLED(DELTA)
  420. #define ADJUST_DELTA(V) \
  421. if (planner.leveling_active) { \
  422. const float zadj = bilinear_z_offset(V); \
  423. delta[A_AXIS] += zadj; \
  424. delta[B_AXIS] += zadj; \
  425. delta[C_AXIS] += zadj; \
  426. }
  427. #else
  428. #define ADJUST_DELTA(V) if (planner.leveling_active) { delta[Z_AXIS] += bilinear_z_offset(V); }
  429. #endif
  430. #else
  431. #define ADJUST_DELTA(V) NOOP
  432. #endif
  433. /**
  434. * Prepare a linear move in a DELTA or SCARA setup.
  435. *
  436. * This calls planner.buffer_line several times, adding
  437. * small incremental moves for DELTA or SCARA.
  438. */
  439. inline bool prepare_kinematic_move_to(float rtarget[XYZE]) {
  440. // Get the top feedrate of the move in the XY plane
  441. const float _feedrate_mm_s = MMS_SCALED(feedrate_mm_s);
  442. // If the move is only in Z/E don't split up the move
  443. if (rtarget[X_AXIS] == current_position[X_AXIS] && rtarget[Y_AXIS] == current_position[Y_AXIS]) {
  444. planner.buffer_line_kinematic(rtarget, _feedrate_mm_s, active_extruder);
  445. return false;
  446. }
  447. // Fail if attempting move outside printable radius
  448. if (!position_is_reachable(rtarget[X_AXIS], rtarget[Y_AXIS])) return true;
  449. // Get the cartesian distances moved in XYZE
  450. const float difference[XYZE] = {
  451. rtarget[X_AXIS] - current_position[X_AXIS],
  452. rtarget[Y_AXIS] - current_position[Y_AXIS],
  453. rtarget[Z_AXIS] - current_position[Z_AXIS],
  454. rtarget[E_AXIS] - current_position[E_AXIS]
  455. };
  456. // Get the linear distance in XYZ
  457. float cartesian_mm = SQRT(sq(difference[X_AXIS]) + sq(difference[Y_AXIS]) + sq(difference[Z_AXIS]));
  458. // If the move is very short, check the E move distance
  459. if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = FABS(difference[E_AXIS]);
  460. // No E move either? Game over.
  461. if (UNEAR_ZERO(cartesian_mm)) return true;
  462. // Minimum number of seconds to move the given distance
  463. const float seconds = cartesian_mm / _feedrate_mm_s;
  464. // The number of segments-per-second times the duration
  465. // gives the number of segments
  466. uint16_t segments = delta_segments_per_second * seconds;
  467. // For SCARA minimum segment size is 0.25mm
  468. #if IS_SCARA
  469. NOMORE(segments, cartesian_mm * 4);
  470. #endif
  471. // At least one segment is required
  472. NOLESS(segments, 1);
  473. // The approximate length of each segment
  474. const float inv_segments = 1.0 / float(segments),
  475. segment_distance[XYZE] = {
  476. difference[X_AXIS] * inv_segments,
  477. difference[Y_AXIS] * inv_segments,
  478. difference[Z_AXIS] * inv_segments,
  479. difference[E_AXIS] * inv_segments
  480. };
  481. // SERIAL_ECHOPAIR("mm=", cartesian_mm);
  482. // SERIAL_ECHOPAIR(" seconds=", seconds);
  483. // SERIAL_ECHOLNPAIR(" segments=", segments);
  484. #if IS_SCARA && ENABLED(SCARA_FEEDRATE_SCALING)
  485. // SCARA needs to scale the feed rate from mm/s to degrees/s
  486. const float inv_segment_length = min(10.0, float(segments) / cartesian_mm), // 1/mm/segs
  487. feed_factor = inv_segment_length * _feedrate_mm_s;
  488. float oldA = stepper.get_axis_position_degrees(A_AXIS),
  489. oldB = stepper.get_axis_position_degrees(B_AXIS);
  490. #endif
  491. // Get the current position as starting point
  492. float raw[XYZE];
  493. COPY(raw, current_position);
  494. // Drop one segment so the last move is to the exact target.
  495. // If there's only 1 segment, loops will be skipped entirely.
  496. --segments;
  497. // Calculate and execute the segments
  498. for (uint16_t s = segments + 1; --s;) {
  499. LOOP_XYZE(i) raw[i] += segment_distance[i];
  500. #if ENABLED(DELTA)
  501. DELTA_RAW_IK(); // Delta can inline its kinematics
  502. #else
  503. inverse_kinematics(raw);
  504. #endif
  505. ADJUST_DELTA(raw); // Adjust Z if bed leveling is enabled
  506. #if IS_SCARA && ENABLED(SCARA_FEEDRATE_SCALING)
  507. // For SCARA scale the feed rate from mm/s to degrees/s
  508. // Use ratio between the length of the move and the larger angle change
  509. const float adiff = abs(delta[A_AXIS] - oldA),
  510. bdiff = abs(delta[B_AXIS] - oldB);
  511. planner.buffer_line(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], raw[E_AXIS], max(adiff, bdiff) * feed_factor, active_extruder);
  512. oldA = delta[A_AXIS];
  513. oldB = delta[B_AXIS];
  514. #else
  515. planner.buffer_line(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], raw[E_AXIS], _feedrate_mm_s, active_extruder);
  516. #endif
  517. }
  518. // Since segment_distance is only approximate,
  519. // the final move must be to the exact destination.
  520. #if IS_SCARA && ENABLED(SCARA_FEEDRATE_SCALING)
  521. // For SCARA scale the feed rate from mm/s to degrees/s
  522. // With segments > 1 length is 1 segment, otherwise total length
  523. inverse_kinematics(rtarget);
  524. ADJUST_DELTA(rtarget);
  525. const float adiff = abs(delta[A_AXIS] - oldA),
  526. bdiff = abs(delta[B_AXIS] - oldB);
  527. planner.buffer_line(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], raw[E_AXIS], max(adiff, bdiff) * feed_factor, active_extruder);
  528. #else
  529. planner.buffer_line_kinematic(rtarget, _feedrate_mm_s, active_extruder);
  530. #endif
  531. return false;
  532. }
  533. #else // !IS_KINEMATIC || UBL_DELTA
  534. /**
  535. * Prepare a linear move in a Cartesian setup.
  536. * Bed Leveling will be applied to the move if enabled.
  537. *
  538. * Returns true if current_position[] was set to destination[]
  539. */
  540. inline bool prepare_move_to_destination_cartesian() {
  541. const float fr_scaled = MMS_SCALED(feedrate_mm_s);
  542. #if HAS_MESH
  543. if (!planner.leveling_active) {
  544. line_to_destination(fr_scaled);
  545. return false;
  546. }
  547. #if ENABLED(AUTO_BED_LEVELING_UBL)
  548. ubl.line_to_destination_cartesian(fr_scaled, active_extruder); // UBL's motion routine needs to know about all moves,
  549. return true; // even purely Z-Axis moves
  550. #else
  551. if (current_position[X_AXIS] != destination[X_AXIS] || current_position[Y_AXIS] != destination[Y_AXIS]) {
  552. #if ENABLED(MESH_BED_LEVELING)
  553. mesh_line_to_destination(fr_scaled);
  554. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  555. bilinear_line_to_destination(fr_scaled);
  556. #endif
  557. return true;
  558. }
  559. else {
  560. line_to_destination();
  561. return false;
  562. }
  563. #endif
  564. #else
  565. line_to_destination();
  566. #endif // HAS_MESH
  567. return false;
  568. }
  569. #endif // !IS_KINEMATIC || UBL_DELTA
  570. #if ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE)
  571. bool extruder_duplication_enabled = false; // Used in Dual X mode 2
  572. #endif
  573. #if ENABLED(DUAL_X_CARRIAGE)
  574. DualXMode dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  575. float inactive_extruder_x_pos = X2_MAX_POS, // used in mode 0 & 1
  576. raised_parked_position[XYZE], // used in mode 1
  577. duplicate_extruder_x_offset = DEFAULT_DUPLICATION_X_OFFSET; // used in mode 2
  578. bool active_extruder_parked = false; // used in mode 1 & 2
  579. millis_t delayed_move_time = 0; // used in mode 1
  580. int16_t duplicate_extruder_temp_offset = 0; // used in mode 2
  581. float x_home_pos(const int extruder) {
  582. if (extruder == 0)
  583. return base_home_pos(X_AXIS);
  584. else
  585. /**
  586. * In dual carriage mode the extruder offset provides an override of the
  587. * second X-carriage position when homed - otherwise X2_HOME_POS is used.
  588. * This allows soft recalibration of the second extruder home position
  589. * without firmware reflash (through the M218 command).
  590. */
  591. return hotend_offset[X_AXIS][1] > 0 ? hotend_offset[X_AXIS][1] : X2_HOME_POS;
  592. }
  593. /**
  594. * Prepare a linear move in a dual X axis setup
  595. *
  596. * Return true if current_position[] was set to destination[]
  597. */
  598. inline bool prepare_move_to_destination_dualx() {
  599. if (active_extruder_parked) {
  600. switch (dual_x_carriage_mode) {
  601. case DXC_FULL_CONTROL_MODE:
  602. break;
  603. case DXC_AUTO_PARK_MODE:
  604. if (current_position[E_AXIS] == destination[E_AXIS]) {
  605. // This is a travel move (with no extrusion)
  606. // Skip it, but keep track of the current position
  607. // (so it can be used as the start of the next non-travel move)
  608. if (delayed_move_time != 0xFFFFFFFFUL) {
  609. set_current_from_destination();
  610. NOLESS(raised_parked_position[Z_AXIS], destination[Z_AXIS]);
  611. delayed_move_time = millis();
  612. return true;
  613. }
  614. }
  615. // unpark extruder: 1) raise, 2) move into starting XY position, 3) lower
  616. for (uint8_t i = 0; i < 3; i++)
  617. planner.buffer_line(
  618. i == 0 ? raised_parked_position[X_AXIS] : current_position[X_AXIS],
  619. i == 0 ? raised_parked_position[Y_AXIS] : current_position[Y_AXIS],
  620. i == 2 ? current_position[Z_AXIS] : raised_parked_position[Z_AXIS],
  621. current_position[E_AXIS],
  622. i == 1 ? PLANNER_XY_FEEDRATE() : planner.max_feedrate_mm_s[Z_AXIS],
  623. active_extruder
  624. );
  625. delayed_move_time = 0;
  626. active_extruder_parked = false;
  627. #if ENABLED(DEBUG_LEVELING_FEATURE)
  628. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Clear active_extruder_parked");
  629. #endif
  630. break;
  631. case DXC_DUPLICATION_MODE:
  632. if (active_extruder == 0) {
  633. #if ENABLED(DEBUG_LEVELING_FEATURE)
  634. if (DEBUGGING(LEVELING)) {
  635. SERIAL_ECHOPAIR("Set planner X", inactive_extruder_x_pos);
  636. SERIAL_ECHOLNPAIR(" ... Line to X", current_position[X_AXIS] + duplicate_extruder_x_offset);
  637. }
  638. #endif
  639. // move duplicate extruder into correct duplication position.
  640. planner.set_position_mm(
  641. inactive_extruder_x_pos,
  642. current_position[Y_AXIS],
  643. current_position[Z_AXIS],
  644. current_position[E_AXIS]
  645. );
  646. planner.buffer_line(
  647. current_position[X_AXIS] + duplicate_extruder_x_offset,
  648. current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS],
  649. planner.max_feedrate_mm_s[X_AXIS], 1
  650. );
  651. SYNC_PLAN_POSITION_KINEMATIC();
  652. stepper.synchronize();
  653. extruder_duplication_enabled = true;
  654. active_extruder_parked = false;
  655. #if ENABLED(DEBUG_LEVELING_FEATURE)
  656. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Set extruder_duplication_enabled\nClear active_extruder_parked");
  657. #endif
  658. }
  659. else {
  660. #if ENABLED(DEBUG_LEVELING_FEATURE)
  661. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Active extruder not 0");
  662. #endif
  663. }
  664. break;
  665. }
  666. }
  667. return prepare_move_to_destination_cartesian();
  668. }
  669. #endif // DUAL_X_CARRIAGE
  670. /**
  671. * Prepare a single move and get ready for the next one
  672. *
  673. * This may result in several calls to planner.buffer_line to
  674. * do smaller moves for DELTA, SCARA, mesh moves, etc.
  675. */
  676. void prepare_move_to_destination() {
  677. clamp_to_software_endstops(destination);
  678. gcode.refresh_cmd_timeout();
  679. #if ENABLED(PREVENT_COLD_EXTRUSION)
  680. if (!DEBUGGING(DRYRUN)) {
  681. if (destination[E_AXIS] != current_position[E_AXIS]) {
  682. if (thermalManager.tooColdToExtrude(active_extruder)) {
  683. current_position[E_AXIS] = destination[E_AXIS]; // Behave as if the move really took place, but ignore E part
  684. SERIAL_ECHO_START();
  685. SERIAL_ECHOLNPGM(MSG_ERR_COLD_EXTRUDE_STOP);
  686. }
  687. #if ENABLED(PREVENT_LENGTHY_EXTRUDE)
  688. if (destination[E_AXIS] - current_position[E_AXIS] > EXTRUDE_MAXLENGTH) {
  689. current_position[E_AXIS] = destination[E_AXIS]; // Behave as if the move really took place, but ignore E part
  690. SERIAL_ECHO_START();
  691. SERIAL_ECHOLNPGM(MSG_ERR_LONG_EXTRUDE_STOP);
  692. }
  693. #endif
  694. }
  695. }
  696. #endif
  697. if (
  698. #if UBL_DELTA // Also works for CARTESIAN (smaller segments follow mesh more closely)
  699. ubl.prepare_segmented_line_to(destination, MMS_SCALED(feedrate_mm_s))
  700. #elif IS_KINEMATIC
  701. prepare_kinematic_move_to(destination)
  702. #elif ENABLED(DUAL_X_CARRIAGE)
  703. prepare_move_to_destination_dualx()
  704. #else
  705. prepare_move_to_destination_cartesian()
  706. #endif
  707. ) return;
  708. set_current_from_destination();
  709. }
  710. #if HAS_AXIS_UNHOMED_ERR
  711. bool axis_unhomed_error(const bool x/*=true*/, const bool y/*=true*/, const bool z/*=true*/) {
  712. #if ENABLED(HOME_AFTER_DEACTIVATE)
  713. const bool xx = x && !axis_known_position[X_AXIS],
  714. yy = y && !axis_known_position[Y_AXIS],
  715. zz = z && !axis_known_position[Z_AXIS];
  716. #else
  717. const bool xx = x && !axis_homed[X_AXIS],
  718. yy = y && !axis_homed[Y_AXIS],
  719. zz = z && !axis_homed[Z_AXIS];
  720. #endif
  721. if (xx || yy || zz) {
  722. SERIAL_ECHO_START();
  723. SERIAL_ECHOPGM(MSG_HOME " ");
  724. if (xx) SERIAL_ECHOPGM(MSG_X);
  725. if (yy) SERIAL_ECHOPGM(MSG_Y);
  726. if (zz) SERIAL_ECHOPGM(MSG_Z);
  727. SERIAL_ECHOLNPGM(" " MSG_FIRST);
  728. #if ENABLED(ULTRA_LCD)
  729. lcd_status_printf_P(0, PSTR(MSG_HOME " %s%s%s " MSG_FIRST), xx ? MSG_X : "", yy ? MSG_Y : "", zz ? MSG_Z : "");
  730. #endif
  731. return true;
  732. }
  733. return false;
  734. }
  735. #endif // HAS_AXIS_UNHOMED_ERR
  736. /**
  737. * The homing feedrate may vary
  738. */
  739. inline float get_homing_bump_feedrate(const AxisEnum axis) {
  740. static const uint8_t homing_bump_divisor[] PROGMEM = HOMING_BUMP_DIVISOR;
  741. uint8_t hbd = pgm_read_byte(&homing_bump_divisor[axis]);
  742. if (hbd < 1) {
  743. hbd = 10;
  744. SERIAL_ECHO_START();
  745. SERIAL_ECHOLNPGM("Warning: Homing Bump Divisor < 1");
  746. }
  747. return homing_feedrate(axis) / hbd;
  748. }
  749. /**
  750. * Home an individual linear axis
  751. */
  752. static void do_homing_move(const AxisEnum axis, const float distance, const float fr_mm_s=0.0) {
  753. #if ENABLED(DEBUG_LEVELING_FEATURE)
  754. if (DEBUGGING(LEVELING)) {
  755. SERIAL_ECHOPAIR(">>> do_homing_move(", axis_codes[axis]);
  756. SERIAL_ECHOPAIR(", ", distance);
  757. SERIAL_ECHOPAIR(", ", fr_mm_s);
  758. SERIAL_CHAR(')');
  759. SERIAL_EOL();
  760. }
  761. #endif
  762. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH)
  763. const bool deploy_bltouch = (axis == Z_AXIS && distance < 0);
  764. if (deploy_bltouch) set_bltouch_deployed(true);
  765. #endif
  766. #if QUIET_PROBING
  767. if (axis == Z_AXIS) probing_pause(true);
  768. #endif
  769. // Tell the planner we're at Z=0
  770. current_position[axis] = 0;
  771. #if IS_SCARA
  772. SYNC_PLAN_POSITION_KINEMATIC();
  773. current_position[axis] = distance;
  774. inverse_kinematics(current_position);
  775. planner.buffer_line(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], current_position[E_AXIS], fr_mm_s ? fr_mm_s : homing_feedrate(axis), active_extruder);
  776. #else
  777. sync_plan_position();
  778. current_position[axis] = distance;
  779. planner.buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], fr_mm_s ? fr_mm_s : homing_feedrate(axis), active_extruder);
  780. #endif
  781. stepper.synchronize();
  782. #if QUIET_PROBING
  783. if (axis == Z_AXIS) probing_pause(false);
  784. #endif
  785. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH)
  786. if (deploy_bltouch) set_bltouch_deployed(false);
  787. #endif
  788. endstops.hit_on_purpose();
  789. #if ENABLED(DEBUG_LEVELING_FEATURE)
  790. if (DEBUGGING(LEVELING)) {
  791. SERIAL_ECHOPAIR("<<< do_homing_move(", axis_codes[axis]);
  792. SERIAL_CHAR(')');
  793. SERIAL_EOL();
  794. }
  795. #endif
  796. }
  797. /**
  798. * Set an axis' current position to its home position (after homing).
  799. *
  800. * For Core and Cartesian robots this applies one-to-one when an
  801. * individual axis has been homed.
  802. *
  803. * DELTA should wait until all homing is done before setting the XYZ
  804. * current_position to home, because homing is a single operation.
  805. * In the case where the axis positions are already known and previously
  806. * homed, DELTA could home to X or Y individually by moving either one
  807. * to the center. However, homing Z always homes XY and Z.
  808. *
  809. * SCARA should wait until all XY homing is done before setting the XY
  810. * current_position to home, because neither X nor Y is at home until
  811. * both are at home. Z can however be homed individually.
  812. *
  813. * Callers must sync the planner position after calling this!
  814. */
  815. void set_axis_is_at_home(const AxisEnum axis) {
  816. #if ENABLED(DEBUG_LEVELING_FEATURE)
  817. if (DEBUGGING(LEVELING)) {
  818. SERIAL_ECHOPAIR(">>> set_axis_is_at_home(", axis_codes[axis]);
  819. SERIAL_CHAR(')');
  820. SERIAL_EOL();
  821. }
  822. #endif
  823. axis_known_position[axis] = axis_homed[axis] = true;
  824. #if HAS_POSITION_SHIFT
  825. position_shift[axis] = 0;
  826. update_software_endstops(axis);
  827. #endif
  828. #if ENABLED(DUAL_X_CARRIAGE)
  829. if (axis == X_AXIS && (active_extruder == 1 || dual_x_carriage_mode == DXC_DUPLICATION_MODE)) {
  830. current_position[X_AXIS] = x_home_pos(active_extruder);
  831. return;
  832. }
  833. #endif
  834. #if ENABLED(MORGAN_SCARA)
  835. scara_set_axis_is_at_home(axis);
  836. #else
  837. current_position[axis] = base_home_pos(axis);
  838. #endif
  839. /**
  840. * Z Probe Z Homing? Account for the probe's Z offset.
  841. */
  842. #if HAS_BED_PROBE && Z_HOME_DIR < 0
  843. if (axis == Z_AXIS) {
  844. #if HOMING_Z_WITH_PROBE
  845. current_position[Z_AXIS] -= zprobe_zoffset;
  846. #if ENABLED(DEBUG_LEVELING_FEATURE)
  847. if (DEBUGGING(LEVELING)) {
  848. SERIAL_ECHOLNPGM("*** Z HOMED WITH PROBE (Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) ***");
  849. SERIAL_ECHOLNPAIR("> zprobe_zoffset = ", zprobe_zoffset);
  850. }
  851. #endif
  852. #elif ENABLED(DEBUG_LEVELING_FEATURE)
  853. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("*** Z HOMED TO ENDSTOP (Z_MIN_PROBE_ENDSTOP) ***");
  854. #endif
  855. }
  856. #endif
  857. #if ENABLED(DEBUG_LEVELING_FEATURE)
  858. if (DEBUGGING(LEVELING)) {
  859. #if HAS_HOME_OFFSET
  860. SERIAL_ECHOPAIR("> home_offset[", axis_codes[axis]);
  861. SERIAL_ECHOLNPAIR("] = ", home_offset[axis]);
  862. #endif
  863. DEBUG_POS("", current_position);
  864. SERIAL_ECHOPAIR("<<< set_axis_is_at_home(", axis_codes[axis]);
  865. SERIAL_CHAR(')');
  866. SERIAL_EOL();
  867. }
  868. #endif
  869. #if ENABLED(I2C_POSITION_ENCODERS)
  870. I2CPEM.homed(axis);
  871. #endif
  872. }
  873. /**
  874. * Home an individual "raw axis" to its endstop.
  875. * This applies to XYZ on Cartesian and Core robots, and
  876. * to the individual ABC steppers on DELTA and SCARA.
  877. *
  878. * At the end of the procedure the axis is marked as
  879. * homed and the current position of that axis is updated.
  880. * Kinematic robots should wait till all axes are homed
  881. * before updating the current position.
  882. */
  883. void homeaxis(const AxisEnum axis) {
  884. #if IS_SCARA
  885. // Only Z homing (with probe) is permitted
  886. if (axis != Z_AXIS) { BUZZ(100, 880); return; }
  887. #else
  888. #define CAN_HOME(A) \
  889. (axis == A##_AXIS && ((A##_MIN_PIN > -1 && A##_HOME_DIR < 0) || (A##_MAX_PIN > -1 && A##_HOME_DIR > 0)))
  890. if (!CAN_HOME(X) && !CAN_HOME(Y) && !CAN_HOME(Z)) return;
  891. #endif
  892. #if ENABLED(DEBUG_LEVELING_FEATURE)
  893. if (DEBUGGING(LEVELING)) {
  894. SERIAL_ECHOPAIR(">>> homeaxis(", axis_codes[axis]);
  895. SERIAL_CHAR(')');
  896. SERIAL_EOL();
  897. }
  898. #endif
  899. const int axis_home_dir =
  900. #if ENABLED(DUAL_X_CARRIAGE)
  901. (axis == X_AXIS) ? x_home_dir(active_extruder) :
  902. #endif
  903. home_dir(axis);
  904. // Homing Z towards the bed? Deploy the Z probe or endstop.
  905. #if HOMING_Z_WITH_PROBE
  906. if (axis == Z_AXIS && DEPLOY_PROBE()) return;
  907. #endif
  908. // Set flags for X, Y, Z motor locking
  909. #if ENABLED(X_DUAL_ENDSTOPS)
  910. if (axis == X_AXIS) stepper.set_homing_flag_x(true);
  911. #endif
  912. #if ENABLED(Y_DUAL_ENDSTOPS)
  913. if (axis == Y_AXIS) stepper.set_homing_flag_y(true);
  914. #endif
  915. #if ENABLED(Z_DUAL_ENDSTOPS)
  916. if (axis == Z_AXIS) stepper.set_homing_flag_z(true);
  917. #endif
  918. // Disable stealthChop if used. Enable diag1 pin on driver.
  919. #if ENABLED(SENSORLESS_HOMING)
  920. #if ENABLED(X_IS_TMC2130)
  921. if (axis == X_AXIS) tmc2130_sensorless_homing(stepperX);
  922. #endif
  923. #if ENABLED(Y_IS_TMC2130)
  924. if (axis == Y_AXIS) tmc2130_sensorless_homing(stepperY);
  925. #endif
  926. #endif
  927. // Fast move towards endstop until triggered
  928. #if ENABLED(DEBUG_LEVELING_FEATURE)
  929. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Home 1 Fast:");
  930. #endif
  931. do_homing_move(axis, 1.5 * max_length(axis) * axis_home_dir);
  932. // When homing Z with probe respect probe clearance
  933. const float bump = axis_home_dir * (
  934. #if HOMING_Z_WITH_PROBE
  935. (axis == Z_AXIS) ? max(Z_CLEARANCE_BETWEEN_PROBES, home_bump_mm(Z_AXIS)) :
  936. #endif
  937. home_bump_mm(axis)
  938. );
  939. // If a second homing move is configured...
  940. if (bump) {
  941. // Move away from the endstop by the axis HOME_BUMP_MM
  942. #if ENABLED(DEBUG_LEVELING_FEATURE)
  943. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Move Away:");
  944. #endif
  945. do_homing_move(axis, -bump);
  946. // Slow move towards endstop until triggered
  947. #if ENABLED(DEBUG_LEVELING_FEATURE)
  948. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Home 2 Slow:");
  949. #endif
  950. do_homing_move(axis, 2 * bump, get_homing_bump_feedrate(axis));
  951. }
  952. #if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
  953. const bool pos_dir = axis_home_dir > 0;
  954. #if ENABLED(X_DUAL_ENDSTOPS)
  955. if (axis == X_AXIS) {
  956. const bool lock_x1 = pos_dir ? (endstops.x_endstop_adj > 0) : (endstops.x_endstop_adj < 0);
  957. float adj = FABS(endstops.x_endstop_adj);
  958. if (pos_dir) adj = -adj;
  959. if (lock_x1) stepper.set_x_lock(true); else stepper.set_x2_lock(true);
  960. do_homing_move(axis, adj);
  961. if (lock_x1) stepper.set_x_lock(false); else stepper.set_x2_lock(false);
  962. stepper.set_homing_flag_x(false);
  963. }
  964. #endif
  965. #if ENABLED(Y_DUAL_ENDSTOPS)
  966. if (axis == Y_AXIS) {
  967. const bool lock_y1 = pos_dir ? (endstops.y_endstop_adj > 0) : (endstops.y_endstop_adj < 0);
  968. float adj = FABS(endstops.y_endstop_adj);
  969. if (pos_dir) adj = -adj;
  970. if (lock_y1) stepper.set_y_lock(true); else stepper.set_y2_lock(true);
  971. do_homing_move(axis, adj);
  972. if (lock_y1) stepper.set_y_lock(false); else stepper.set_y2_lock(false);
  973. stepper.set_homing_flag_y(false);
  974. }
  975. #endif
  976. #if ENABLED(Z_DUAL_ENDSTOPS)
  977. if (axis == Z_AXIS) {
  978. const bool lock_z1 = pos_dir ? (endstops.z_endstop_adj > 0) : (endstops.z_endstop_adj < 0);
  979. float adj = FABS(endstops.z_endstop_adj);
  980. if (pos_dir) adj = -adj;
  981. if (lock_z1) stepper.set_z_lock(true); else stepper.set_z2_lock(true);
  982. do_homing_move(axis, adj);
  983. if (lock_z1) stepper.set_z_lock(false); else stepper.set_z2_lock(false);
  984. stepper.set_homing_flag_z(false);
  985. }
  986. #endif
  987. #endif
  988. #if IS_SCARA
  989. set_axis_is_at_home(axis);
  990. SYNC_PLAN_POSITION_KINEMATIC();
  991. #elif ENABLED(DELTA)
  992. // Delta has already moved all three towers up in G28
  993. // so here it re-homes each tower in turn.
  994. // Delta homing treats the axes as normal linear axes.
  995. // retrace by the amount specified in delta_endstop_adj + additional 0.1mm in order to have minimum steps
  996. if (delta_endstop_adj[axis] * Z_HOME_DIR <= 0) {
  997. #if ENABLED(DEBUG_LEVELING_FEATURE)
  998. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("delta_endstop_adj:");
  999. #endif
  1000. do_homing_move(axis, delta_endstop_adj[axis] - 0.1 * Z_HOME_DIR);
  1001. }
  1002. #else
  1003. // For cartesian/core machines,
  1004. // set the axis to its home position
  1005. set_axis_is_at_home(axis);
  1006. sync_plan_position();
  1007. destination[axis] = current_position[axis];
  1008. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1009. if (DEBUGGING(LEVELING)) DEBUG_POS("> AFTER set_axis_is_at_home", current_position);
  1010. #endif
  1011. #endif
  1012. // Re-enable stealthChop if used. Disable diag1 pin on driver.
  1013. #if ENABLED(SENSORLESS_HOMING)
  1014. #if ENABLED(X_IS_TMC2130)
  1015. if (axis == X_AXIS) tmc2130_sensorless_homing(stepperX, false);
  1016. #endif
  1017. #if ENABLED(Y_IS_TMC2130)
  1018. if (axis == Y_AXIS) tmc2130_sensorless_homing(stepperY, false);
  1019. #endif
  1020. #endif
  1021. // Put away the Z probe
  1022. #if HOMING_Z_WITH_PROBE
  1023. if (axis == Z_AXIS && STOW_PROBE()) return;
  1024. #endif
  1025. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1026. if (DEBUGGING(LEVELING)) {
  1027. SERIAL_ECHOPAIR("<<< homeaxis(", axis_codes[axis]);
  1028. SERIAL_CHAR(')');
  1029. SERIAL_EOL();
  1030. }
  1031. #endif
  1032. } // homeaxis()
  1033. #if HAS_WORKSPACE_OFFSET || ENABLED(DUAL_X_CARRIAGE)
  1034. /**
  1035. * Software endstops can be used to monitor the open end of
  1036. * an axis that has a hardware endstop on the other end. Or
  1037. * they can prevent axes from moving past endstops and grinding.
  1038. *
  1039. * To keep doing their job as the coordinate system changes,
  1040. * the software endstop positions must be refreshed to remain
  1041. * at the same positions relative to the machine.
  1042. */
  1043. void update_software_endstops(const AxisEnum axis) {
  1044. const float offs = 0.0
  1045. #if HAS_HOME_OFFSET
  1046. + home_offset[axis]
  1047. #endif
  1048. #if HAS_POSITION_SHIFT
  1049. + position_shift[axis]
  1050. #endif
  1051. ;
  1052. #if HAS_HOME_OFFSET && HAS_POSITION_SHIFT
  1053. workspace_offset[axis] = offs;
  1054. #endif
  1055. #if ENABLED(DUAL_X_CARRIAGE)
  1056. if (axis == X_AXIS) {
  1057. // In Dual X mode hotend_offset[X] is T1's home position
  1058. float dual_max_x = max(hotend_offset[X_AXIS][1], X2_MAX_POS);
  1059. if (active_extruder != 0) {
  1060. // T1 can move from X2_MIN_POS to X2_MAX_POS or X2 home position (whichever is larger)
  1061. soft_endstop_min[X_AXIS] = X2_MIN_POS + offs;
  1062. soft_endstop_max[X_AXIS] = dual_max_x + offs;
  1063. }
  1064. else if (dual_x_carriage_mode == DXC_DUPLICATION_MODE) {
  1065. // In Duplication Mode, T0 can move as far left as X_MIN_POS
  1066. // but not so far to the right that T1 would move past the end
  1067. soft_endstop_min[X_AXIS] = base_min_pos(X_AXIS) + offs;
  1068. soft_endstop_max[X_AXIS] = min(base_max_pos(X_AXIS), dual_max_x - duplicate_extruder_x_offset) + offs;
  1069. }
  1070. else {
  1071. // In other modes, T0 can move from X_MIN_POS to X_MAX_POS
  1072. soft_endstop_min[axis] = base_min_pos(axis) + offs;
  1073. soft_endstop_max[axis] = base_max_pos(axis) + offs;
  1074. }
  1075. }
  1076. #elif ENABLED(DELTA)
  1077. soft_endstop_min[axis] = base_min_pos(axis) + (axis == Z_AXIS ? 0 : offs);
  1078. soft_endstop_max[axis] = base_max_pos(axis) + offs;
  1079. #else
  1080. soft_endstop_min[axis] = base_min_pos(axis) + offs;
  1081. soft_endstop_max[axis] = base_max_pos(axis) + offs;
  1082. #endif
  1083. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1084. if (DEBUGGING(LEVELING)) {
  1085. SERIAL_ECHOPAIR("For ", axis_codes[axis]);
  1086. #if HAS_HOME_OFFSET
  1087. SERIAL_ECHOPAIR(" axis:\n home_offset = ", home_offset[axis]);
  1088. #endif
  1089. #if HAS_POSITION_SHIFT
  1090. SERIAL_ECHOPAIR("\n position_shift = ", position_shift[axis]);
  1091. #endif
  1092. SERIAL_ECHOPAIR("\n soft_endstop_min = ", soft_endstop_min[axis]);
  1093. SERIAL_ECHOLNPAIR("\n soft_endstop_max = ", soft_endstop_max[axis]);
  1094. }
  1095. #endif
  1096. #if ENABLED(DELTA)
  1097. switch(axis) {
  1098. case X_AXIS:
  1099. case Y_AXIS:
  1100. // Get a minimum radius for clamping
  1101. soft_endstop_radius = MIN3(FABS(max(soft_endstop_min[X_AXIS], soft_endstop_min[Y_AXIS])), soft_endstop_max[X_AXIS], soft_endstop_max[Y_AXIS]);
  1102. soft_endstop_radius_2 = sq(soft_endstop_radius);
  1103. break;
  1104. case Z_AXIS:
  1105. delta_clip_start_height = soft_endstop_max[axis] - delta_safe_distance_from_top();
  1106. default: break;
  1107. }
  1108. #endif
  1109. }
  1110. #endif // HAS_WORKSPACE_OFFSET || DUAL_X_CARRIAGE
  1111. #if HAS_M206_COMMAND
  1112. /**
  1113. * Change the home offset for an axis, update the current
  1114. * position and the software endstops to retain the same
  1115. * relative distance to the new home.
  1116. *
  1117. * Since this changes the current_position, code should
  1118. * call sync_plan_position soon after this.
  1119. */
  1120. void set_home_offset(const AxisEnum axis, const float v) {
  1121. current_position[axis] += v - home_offset[axis];
  1122. home_offset[axis] = v;
  1123. update_software_endstops(axis);
  1124. }
  1125. #endif // HAS_M206_COMMAND