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

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