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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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. #if UBL_SEGMENTED
  232. // ubl segmented line will do z-only moves in single segment
  233. ubl.prepare_segmented_line_to(destination, MMS_SCALED(fr_mm_s ? fr_mm_s : feedrate_mm_s));
  234. #else
  235. if ( current_position[X_AXIS] == destination[X_AXIS]
  236. && current_position[Y_AXIS] == destination[Y_AXIS]
  237. && current_position[Z_AXIS] == destination[Z_AXIS]
  238. && current_position[E_AXIS] == destination[E_AXIS]
  239. ) return;
  240. planner.buffer_line_kinematic(destination, MMS_SCALED(fr_mm_s ? fr_mm_s : feedrate_mm_s), active_extruder);
  241. #endif
  242. set_current_from_destination();
  243. }
  244. #endif // IS_KINEMATIC
  245. /**
  246. * Plan a move to (X, Y, Z) and set the current_position
  247. * The final current_position may not be the one that was requested
  248. */
  249. void do_blocking_move_to(const float rx, const float ry, const float rz, const float &fr_mm_s/*=0.0*/) {
  250. const float old_feedrate_mm_s = feedrate_mm_s;
  251. #if ENABLED(DEBUG_LEVELING_FEATURE)
  252. if (DEBUGGING(LEVELING)) print_xyz(PSTR(">>> do_blocking_move_to"), NULL, rx, ry, rz);
  253. #endif
  254. const float z_feedrate = fr_mm_s ? fr_mm_s : homing_feedrate(Z_AXIS);
  255. #if ENABLED(DELTA)
  256. if (!position_is_reachable(rx, ry)) return;
  257. feedrate_mm_s = fr_mm_s ? fr_mm_s : XY_PROBE_FEEDRATE_MM_S;
  258. set_destination_from_current(); // sync destination at the start
  259. #if ENABLED(DEBUG_LEVELING_FEATURE)
  260. if (DEBUGGING(LEVELING)) DEBUG_POS("set_destination_from_current", destination);
  261. #endif
  262. // when in the danger zone
  263. if (current_position[Z_AXIS] > delta_clip_start_height) {
  264. if (rz > delta_clip_start_height) { // staying in the danger zone
  265. destination[X_AXIS] = rx; // move directly (uninterpolated)
  266. destination[Y_AXIS] = ry;
  267. destination[Z_AXIS] = rz;
  268. prepare_uninterpolated_move_to_destination(); // set_current_from_destination()
  269. #if ENABLED(DEBUG_LEVELING_FEATURE)
  270. if (DEBUGGING(LEVELING)) DEBUG_POS("danger zone move", current_position);
  271. #endif
  272. return;
  273. }
  274. destination[Z_AXIS] = delta_clip_start_height;
  275. prepare_uninterpolated_move_to_destination(); // set_current_from_destination()
  276. #if ENABLED(DEBUG_LEVELING_FEATURE)
  277. if (DEBUGGING(LEVELING)) DEBUG_POS("zone border move", current_position);
  278. #endif
  279. }
  280. if (rz > current_position[Z_AXIS]) { // raising?
  281. destination[Z_AXIS] = rz;
  282. prepare_uninterpolated_move_to_destination(z_feedrate); // set_current_from_destination()
  283. #if ENABLED(DEBUG_LEVELING_FEATURE)
  284. if (DEBUGGING(LEVELING)) DEBUG_POS("z raise move", current_position);
  285. #endif
  286. }
  287. destination[X_AXIS] = rx;
  288. destination[Y_AXIS] = ry;
  289. prepare_move_to_destination(); // set_current_from_destination()
  290. #if ENABLED(DEBUG_LEVELING_FEATURE)
  291. if (DEBUGGING(LEVELING)) DEBUG_POS("xy move", current_position);
  292. #endif
  293. if (rz < current_position[Z_AXIS]) { // lowering?
  294. destination[Z_AXIS] = rz;
  295. prepare_uninterpolated_move_to_destination(z_feedrate); // set_current_from_destination()
  296. #if ENABLED(DEBUG_LEVELING_FEATURE)
  297. if (DEBUGGING(LEVELING)) DEBUG_POS("z lower move", current_position);
  298. #endif
  299. }
  300. #elif IS_SCARA
  301. if (!position_is_reachable(rx, ry)) return;
  302. set_destination_from_current();
  303. // If Z needs to raise, do it before moving XY
  304. if (destination[Z_AXIS] < rz) {
  305. destination[Z_AXIS] = rz;
  306. prepare_uninterpolated_move_to_destination(z_feedrate);
  307. }
  308. destination[X_AXIS] = rx;
  309. destination[Y_AXIS] = ry;
  310. prepare_uninterpolated_move_to_destination(fr_mm_s ? fr_mm_s : XY_PROBE_FEEDRATE_MM_S);
  311. // If Z needs to lower, do it after moving XY
  312. if (destination[Z_AXIS] > rz) {
  313. destination[Z_AXIS] = rz;
  314. prepare_uninterpolated_move_to_destination(z_feedrate);
  315. }
  316. #else
  317. // If Z needs to raise, do it before moving XY
  318. if (current_position[Z_AXIS] < rz) {
  319. feedrate_mm_s = z_feedrate;
  320. current_position[Z_AXIS] = rz;
  321. line_to_current_position();
  322. }
  323. feedrate_mm_s = fr_mm_s ? fr_mm_s : XY_PROBE_FEEDRATE_MM_S;
  324. current_position[X_AXIS] = rx;
  325. current_position[Y_AXIS] = ry;
  326. line_to_current_position();
  327. // If Z needs to lower, do it after moving XY
  328. if (current_position[Z_AXIS] > rz) {
  329. feedrate_mm_s = z_feedrate;
  330. current_position[Z_AXIS] = rz;
  331. line_to_current_position();
  332. }
  333. #endif
  334. feedrate_mm_s = old_feedrate_mm_s;
  335. #if ENABLED(DEBUG_LEVELING_FEATURE)
  336. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("<<< do_blocking_move_to");
  337. #endif
  338. stepper.synchronize();
  339. }
  340. void do_blocking_move_to_x(const float &rx, const float &fr_mm_s/*=0.0*/) {
  341. do_blocking_move_to(rx, current_position[Y_AXIS], current_position[Z_AXIS], fr_mm_s);
  342. }
  343. void do_blocking_move_to_z(const float &rz, const float &fr_mm_s/*=0.0*/) {
  344. do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], rz, fr_mm_s);
  345. }
  346. void do_blocking_move_to_xy(const float &rx, const float &ry, const float &fr_mm_s/*=0.0*/) {
  347. do_blocking_move_to(rx, ry, current_position[Z_AXIS], fr_mm_s);
  348. }
  349. //
  350. // Prepare to do endstop or probe moves
  351. // with custom feedrates.
  352. //
  353. // - Save current feedrates
  354. // - Reset the rate multiplier
  355. // - Reset the command timeout
  356. // - Enable the endstops (for endstop moves)
  357. //
  358. void bracket_probe_move(const bool before) {
  359. static float saved_feedrate_mm_s;
  360. static int16_t saved_feedrate_percentage;
  361. #if ENABLED(DEBUG_LEVELING_FEATURE)
  362. if (DEBUGGING(LEVELING)) DEBUG_POS("bracket_probe_move", current_position);
  363. #endif
  364. if (before) {
  365. saved_feedrate_mm_s = feedrate_mm_s;
  366. saved_feedrate_percentage = feedrate_percentage;
  367. feedrate_percentage = 100;
  368. }
  369. else {
  370. feedrate_mm_s = saved_feedrate_mm_s;
  371. feedrate_percentage = saved_feedrate_percentage;
  372. }
  373. }
  374. void setup_for_endstop_or_probe_move() { bracket_probe_move(true); }
  375. void clean_up_after_endstop_or_probe_move() { bracket_probe_move(false); }
  376. // Software Endstops are based on the configured limits.
  377. float soft_endstop_min[XYZ] = { X_MIN_BED, Y_MIN_BED, Z_MIN_POS },
  378. soft_endstop_max[XYZ] = { X_MAX_BED, Y_MAX_BED, Z_MAX_POS };
  379. #if HAS_SOFTWARE_ENDSTOPS
  380. // Software Endstops are based on the configured limits.
  381. bool soft_endstops_enabled = true;
  382. #if IS_KINEMATIC
  383. float soft_endstop_radius, soft_endstop_radius_2;
  384. #endif
  385. /**
  386. * Constrain the given coordinates to the software endstops.
  387. *
  388. * For DELTA/SCARA the XY constraint is based on the smallest
  389. * radius within the set software endstops.
  390. */
  391. void clamp_to_software_endstops(float target[XYZ]) {
  392. if (!soft_endstops_enabled) return;
  393. #if IS_KINEMATIC
  394. const float dist_2 = HYPOT2(target[X_AXIS], target[Y_AXIS]);
  395. if (dist_2 > soft_endstop_radius_2) {
  396. const float ratio = soft_endstop_radius / SQRT(dist_2); // 200 / 300 = 0.66
  397. target[X_AXIS] *= ratio;
  398. target[Y_AXIS] *= ratio;
  399. }
  400. #else
  401. #if ENABLED(MIN_SOFTWARE_ENDSTOP_X)
  402. NOLESS(target[X_AXIS], soft_endstop_min[X_AXIS]);
  403. #endif
  404. #if ENABLED(MIN_SOFTWARE_ENDSTOP_Y)
  405. NOLESS(target[Y_AXIS], soft_endstop_min[Y_AXIS]);
  406. #endif
  407. #if ENABLED(MAX_SOFTWARE_ENDSTOP_X)
  408. NOMORE(target[X_AXIS], soft_endstop_max[X_AXIS]);
  409. #endif
  410. #if ENABLED(MAX_SOFTWARE_ENDSTOP_Y)
  411. NOMORE(target[Y_AXIS], soft_endstop_max[Y_AXIS]);
  412. #endif
  413. #endif
  414. #if ENABLED(MIN_SOFTWARE_ENDSTOP_Z)
  415. NOLESS(target[Z_AXIS], soft_endstop_min[Z_AXIS]);
  416. #endif
  417. #if ENABLED(MAX_SOFTWARE_ENDSTOP_Z)
  418. NOMORE(target[Z_AXIS], soft_endstop_max[Z_AXIS]);
  419. #endif
  420. }
  421. #endif
  422. #if !UBL_SEGMENTED
  423. #if IS_KINEMATIC
  424. #if IS_SCARA
  425. /**
  426. * Before raising this value, use M665 S[seg_per_sec] to decrease
  427. * the number of segments-per-second. Default is 200. Some deltas
  428. * do better with 160 or lower. It would be good to know how many
  429. * segments-per-second are actually possible for SCARA on AVR.
  430. *
  431. * Longer segments result in less kinematic overhead
  432. * but may produce jagged lines. Try 0.5mm, 1.0mm, and 2.0mm
  433. * and compare the difference.
  434. */
  435. #define SCARA_MIN_SEGMENT_LENGTH 0.5
  436. #endif
  437. /**
  438. * Prepare a linear move in a DELTA or SCARA setup.
  439. *
  440. * Called from prepare_move_to_destination as the
  441. * default Delta/SCARA segmenter.
  442. *
  443. * This calls planner.buffer_line several times, adding
  444. * small incremental moves for DELTA or SCARA.
  445. *
  446. * For Unified Bed Leveling (Delta or Segmented Cartesian)
  447. * the ubl.prepare_segmented_line_to method replaces this.
  448. *
  449. * For Auto Bed Leveling (Bilinear) with SEGMENT_LEVELED_MOVES
  450. * this is replaced by segmented_line_to_destination below.
  451. */
  452. inline bool prepare_kinematic_move_to(const float (&rtarget)[XYZE]) {
  453. // Get the top feedrate of the move in the XY plane
  454. const float _feedrate_mm_s = MMS_SCALED(feedrate_mm_s);
  455. const float xdiff = rtarget[X_AXIS] - current_position[X_AXIS],
  456. ydiff = rtarget[Y_AXIS] - current_position[Y_AXIS];
  457. // If the move is only in Z/E don't split up the move
  458. if (!xdiff && !ydiff) {
  459. planner.buffer_line_kinematic(rtarget, _feedrate_mm_s, active_extruder);
  460. return false; // caller will update current_position
  461. }
  462. // Fail if attempting move outside printable radius
  463. if (!position_is_reachable(rtarget[X_AXIS], rtarget[Y_AXIS])) return true;
  464. // Remaining cartesian distances
  465. const float zdiff = rtarget[Z_AXIS] - current_position[Z_AXIS],
  466. ediff = rtarget[E_AXIS] - current_position[E_AXIS];
  467. // Get the linear distance in XYZ
  468. float cartesian_mm = SQRT(sq(xdiff) + sq(ydiff) + sq(zdiff));
  469. // If the move is very short, check the E move distance
  470. if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = FABS(ediff);
  471. // No E move either? Game over.
  472. if (UNEAR_ZERO(cartesian_mm)) return true;
  473. // Minimum number of seconds to move the given distance
  474. const float seconds = cartesian_mm / _feedrate_mm_s;
  475. // The number of segments-per-second times the duration
  476. // gives the number of segments
  477. uint16_t segments = delta_segments_per_second * seconds;
  478. // For SCARA enforce a minimum segment size
  479. #if IS_SCARA
  480. NOMORE(segments, cartesian_mm * (1.0 / SCARA_MIN_SEGMENT_LENGTH));
  481. #endif
  482. // At least one segment is required
  483. NOLESS(segments, 1);
  484. // The approximate length of each segment
  485. const float inv_segments = 1.0 / float(segments),
  486. segment_distance[XYZE] = {
  487. xdiff * inv_segments,
  488. ydiff * inv_segments,
  489. zdiff * inv_segments,
  490. ediff * inv_segments
  491. };
  492. #if DISABLED(SCARA_FEEDRATE_SCALING)
  493. const float cartesian_segment_mm = cartesian_mm * inv_segments;
  494. #endif
  495. /*
  496. SERIAL_ECHOPAIR("mm=", cartesian_mm);
  497. SERIAL_ECHOPAIR(" seconds=", seconds);
  498. SERIAL_ECHOPAIR(" segments=", segments);
  499. #if DISABLED(SCARA_FEEDRATE_SCALING)
  500. SERIAL_ECHOLNPAIR(" segment_mm=", cartesian_segment_mm);
  501. #else
  502. SERIAL_EOL();
  503. #endif
  504. //*/
  505. #if ENABLED(SCARA_FEEDRATE_SCALING)
  506. // SCARA needs to scale the feed rate from mm/s to degrees/s
  507. // i.e., Complete the angular vector in the given time.
  508. const float segment_length = cartesian_mm * inv_segments,
  509. inv_segment_length = 1.0 / segment_length, // 1/mm/segs
  510. inverse_secs = inv_segment_length * _feedrate_mm_s;
  511. float oldA = planner.position_float[A_AXIS],
  512. oldB = planner.position_float[B_AXIS];
  513. /*
  514. SERIAL_ECHOPGM("Scaled kinematic move: ");
  515. SERIAL_ECHOPAIR(" segment_length (inv)=", segment_length);
  516. SERIAL_ECHOPAIR(" (", inv_segment_length);
  517. SERIAL_ECHOPAIR(") _feedrate_mm_s=", _feedrate_mm_s);
  518. SERIAL_ECHOPAIR(" inverse_secs=", inverse_secs);
  519. SERIAL_ECHOPAIR(" oldA=", oldA);
  520. SERIAL_ECHOLNPAIR(" oldB=", oldB);
  521. safe_delay(5);
  522. //*/
  523. #endif
  524. // Get the current position as starting point
  525. float raw[XYZE];
  526. COPY(raw, current_position);
  527. // Calculate and execute the segments
  528. while (--segments) {
  529. static millis_t next_idle_ms = millis() + 200UL;
  530. thermalManager.manage_heater(); // This returns immediately if not really needed.
  531. if (ELAPSED(millis(), next_idle_ms)) {
  532. next_idle_ms = millis() + 200UL;
  533. idle();
  534. }
  535. LOOP_XYZE(i) raw[i] += segment_distance[i];
  536. #if ENABLED(DELTA) && HOTENDS < 2
  537. DELTA_IK(raw); // Delta can inline its kinematics
  538. #else
  539. inverse_kinematics(raw);
  540. #endif
  541. ADJUST_DELTA(raw); // Adjust Z if bed leveling is enabled
  542. #if ENABLED(SCARA_FEEDRATE_SCALING)
  543. // For SCARA scale the feed rate from mm/s to degrees/s
  544. // i.e., Complete the angular vector in the given time.
  545. planner.buffer_segment(delta[A_AXIS], delta[B_AXIS], raw[Z_AXIS], raw[E_AXIS], HYPOT(delta[A_AXIS] - oldA, delta[B_AXIS] - oldB) * inverse_secs, active_extruder);
  546. /*
  547. SERIAL_ECHO(segments);
  548. SERIAL_ECHOPAIR(": X=", raw[X_AXIS]); SERIAL_ECHOPAIR(" Y=", raw[Y_AXIS]);
  549. SERIAL_ECHOPAIR(" A=", delta[A_AXIS]); SERIAL_ECHOPAIR(" B=", delta[B_AXIS]);
  550. SERIAL_ECHOLNPAIR(" F", HYPOT(delta[A_AXIS] - oldA, delta[B_AXIS] - oldB) * inverse_secs * 60);
  551. safe_delay(5);
  552. //*/
  553. oldA = delta[A_AXIS]; oldB = delta[B_AXIS];
  554. #else
  555. planner.buffer_line(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], raw[E_AXIS], _feedrate_mm_s, active_extruder, cartesian_segment_mm);
  556. #endif
  557. }
  558. // Ensure last segment arrives at target location.
  559. #if ENABLED(SCARA_FEEDRATE_SCALING)
  560. inverse_kinematics(rtarget);
  561. ADJUST_DELTA(rtarget);
  562. const float diff2 = HYPOT2(delta[A_AXIS] - oldA, delta[B_AXIS] - oldB);
  563. if (diff2) {
  564. planner.buffer_segment(delta[A_AXIS], delta[B_AXIS], rtarget[Z_AXIS], rtarget[E_AXIS], SQRT(diff2) * inverse_secs, active_extruder);
  565. /*
  566. SERIAL_ECHOPAIR("final: A=", delta[A_AXIS]); SERIAL_ECHOPAIR(" B=", delta[B_AXIS]);
  567. SERIAL_ECHOPAIR(" adiff=", delta[A_AXIS] - oldA); SERIAL_ECHOPAIR(" bdiff=", delta[B_AXIS] - oldB);
  568. SERIAL_ECHOLNPAIR(" F", (SQRT(diff2) * inverse_secs) * 60);
  569. SERIAL_EOL();
  570. safe_delay(5);
  571. //*/
  572. }
  573. #else
  574. planner.buffer_line_kinematic(rtarget, _feedrate_mm_s, active_extruder, cartesian_segment_mm);
  575. #endif
  576. return false; // caller will update current_position
  577. }
  578. #else // !IS_KINEMATIC
  579. #if ENABLED(SEGMENT_LEVELED_MOVES)
  580. /**
  581. * Prepare a segmented move on a CARTESIAN setup.
  582. *
  583. * This calls planner.buffer_line several times, adding
  584. * small incremental moves. This allows the planner to
  585. * apply more detailed bed leveling to the full move.
  586. */
  587. inline void segmented_line_to_destination(const float &fr_mm_s, const float segment_size=LEVELED_SEGMENT_LENGTH) {
  588. const float xdiff = destination[X_AXIS] - current_position[X_AXIS],
  589. ydiff = destination[Y_AXIS] - current_position[Y_AXIS];
  590. // If the move is only in Z/E don't split up the move
  591. if (!xdiff && !ydiff) {
  592. planner.buffer_line_kinematic(destination, fr_mm_s, active_extruder);
  593. return;
  594. }
  595. // Remaining cartesian distances
  596. const float zdiff = destination[Z_AXIS] - current_position[Z_AXIS],
  597. ediff = destination[E_AXIS] - current_position[E_AXIS];
  598. // Get the linear distance in XYZ
  599. // If the move is very short, check the E move distance
  600. // No E move either? Game over.
  601. float cartesian_mm = SQRT(sq(xdiff) + sq(ydiff) + sq(zdiff));
  602. if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = FABS(ediff);
  603. if (UNEAR_ZERO(cartesian_mm)) return;
  604. // The length divided by the segment size
  605. // At least one segment is required
  606. uint16_t segments = cartesian_mm / segment_size;
  607. NOLESS(segments, 1);
  608. // The approximate length of each segment
  609. const float inv_segments = 1.0 / float(segments),
  610. cartesian_segment_mm = cartesian_mm * inv_segments,
  611. segment_distance[XYZE] = {
  612. xdiff * inv_segments,
  613. ydiff * inv_segments,
  614. zdiff * inv_segments,
  615. ediff * inv_segments
  616. };
  617. // SERIAL_ECHOPAIR("mm=", cartesian_mm);
  618. // SERIAL_ECHOLNPAIR(" segments=", segments);
  619. // SERIAL_ECHOLNPAIR(" segment_mm=", cartesian_segment_mm);
  620. // Get the raw current position as starting point
  621. float raw[XYZE];
  622. COPY(raw, current_position);
  623. // Calculate and execute the segments
  624. while (--segments) {
  625. static millis_t next_idle_ms = millis() + 200UL;
  626. thermalManager.manage_heater(); // This returns immediately if not really needed.
  627. if (ELAPSED(millis(), next_idle_ms)) {
  628. next_idle_ms = millis() + 200UL;
  629. idle();
  630. }
  631. LOOP_XYZE(i) raw[i] += segment_distance[i];
  632. planner.buffer_line_kinematic(raw, fr_mm_s, active_extruder, cartesian_segment_mm);
  633. }
  634. // Since segment_distance is only approximate,
  635. // the final move must be to the exact destination.
  636. planner.buffer_line_kinematic(destination, fr_mm_s, active_extruder, cartesian_segment_mm);
  637. }
  638. #endif // SEGMENT_LEVELED_MOVES
  639. /**
  640. * Prepare a linear move in a Cartesian setup.
  641. *
  642. * When a mesh-based leveling system is active, moves are segmented
  643. * according to the configuration of the leveling system.
  644. *
  645. * Returns true if current_position[] was set to destination[]
  646. */
  647. inline bool prepare_move_to_destination_cartesian() {
  648. #if HAS_MESH
  649. if (planner.leveling_active && planner.leveling_active_at_z(destination[Z_AXIS])) {
  650. #if ENABLED(AUTO_BED_LEVELING_UBL)
  651. ubl.line_to_destination_cartesian(MMS_SCALED(feedrate_mm_s), active_extruder); // UBL's motion routine needs to know about
  652. return true; // all moves, including Z-only moves.
  653. #elif ENABLED(SEGMENT_LEVELED_MOVES)
  654. segmented_line_to_destination(MMS_SCALED(feedrate_mm_s));
  655. return false; // caller will update current_position
  656. #else
  657. /**
  658. * For MBL and ABL-BILINEAR only segment moves when X or Y are involved.
  659. * Otherwise fall through to do a direct single move.
  660. */
  661. if (current_position[X_AXIS] != destination[X_AXIS] || current_position[Y_AXIS] != destination[Y_AXIS]) {
  662. #if ENABLED(MESH_BED_LEVELING)
  663. mesh_line_to_destination(MMS_SCALED(feedrate_mm_s));
  664. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  665. bilinear_line_to_destination(MMS_SCALED(feedrate_mm_s));
  666. #endif
  667. return true;
  668. }
  669. #endif
  670. }
  671. #endif // HAS_MESH
  672. buffer_line_to_destination(MMS_SCALED(feedrate_mm_s));
  673. return false; // caller will update current_position
  674. }
  675. #endif // !IS_KINEMATIC
  676. #endif // !UBL_SEGMENTED
  677. #if ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE)
  678. bool extruder_duplication_enabled = false; // Used in Dual X mode 2
  679. #endif
  680. #if ENABLED(DUAL_X_CARRIAGE)
  681. DualXMode dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  682. float inactive_extruder_x_pos = X2_MAX_POS, // used in mode 0 & 1
  683. raised_parked_position[XYZE], // used in mode 1
  684. duplicate_extruder_x_offset = DEFAULT_DUPLICATION_X_OFFSET; // used in mode 2
  685. bool active_extruder_parked = false; // used in mode 1 & 2
  686. millis_t delayed_move_time = 0; // used in mode 1
  687. int16_t duplicate_extruder_temp_offset = 0; // used in mode 2
  688. float x_home_pos(const int extruder) {
  689. if (extruder == 0)
  690. return base_home_pos(X_AXIS);
  691. else
  692. /**
  693. * In dual carriage mode the extruder offset provides an override of the
  694. * second X-carriage position when homed - otherwise X2_HOME_POS is used.
  695. * This allows soft recalibration of the second extruder home position
  696. * without firmware reflash (through the M218 command).
  697. */
  698. return hotend_offset[X_AXIS][1] > 0 ? hotend_offset[X_AXIS][1] : X2_HOME_POS;
  699. }
  700. /**
  701. * Prepare a linear move in a dual X axis setup
  702. *
  703. * Return true if current_position[] was set to destination[]
  704. */
  705. inline bool dual_x_carriage_unpark() {
  706. if (active_extruder_parked) {
  707. switch (dual_x_carriage_mode) {
  708. case DXC_FULL_CONTROL_MODE:
  709. break;
  710. case DXC_AUTO_PARK_MODE:
  711. if (current_position[E_AXIS] == destination[E_AXIS]) {
  712. // This is a travel move (with no extrusion)
  713. // Skip it, but keep track of the current position
  714. // (so it can be used as the start of the next non-travel move)
  715. if (delayed_move_time != 0xFFFFFFFFUL) {
  716. set_current_from_destination();
  717. NOLESS(raised_parked_position[Z_AXIS], destination[Z_AXIS]);
  718. delayed_move_time = millis();
  719. return true;
  720. }
  721. }
  722. // unpark extruder: 1) raise, 2) move into starting XY position, 3) lower
  723. for (uint8_t i = 0; i < 3; i++)
  724. planner.buffer_line(
  725. i == 0 ? raised_parked_position[X_AXIS] : current_position[X_AXIS],
  726. i == 0 ? raised_parked_position[Y_AXIS] : current_position[Y_AXIS],
  727. i == 2 ? current_position[Z_AXIS] : raised_parked_position[Z_AXIS],
  728. current_position[E_AXIS],
  729. i == 1 ? PLANNER_XY_FEEDRATE() : planner.max_feedrate_mm_s[Z_AXIS],
  730. active_extruder
  731. );
  732. delayed_move_time = 0;
  733. active_extruder_parked = false;
  734. #if ENABLED(DEBUG_LEVELING_FEATURE)
  735. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Clear active_extruder_parked");
  736. #endif
  737. break;
  738. case DXC_DUPLICATION_MODE:
  739. if (active_extruder == 0) {
  740. #if ENABLED(DEBUG_LEVELING_FEATURE)
  741. if (DEBUGGING(LEVELING)) {
  742. SERIAL_ECHOPAIR("Set planner X", inactive_extruder_x_pos);
  743. SERIAL_ECHOLNPAIR(" ... Line to X", current_position[X_AXIS] + duplicate_extruder_x_offset);
  744. }
  745. #endif
  746. // move duplicate extruder into correct duplication position.
  747. planner.set_position_mm(
  748. inactive_extruder_x_pos,
  749. current_position[Y_AXIS],
  750. current_position[Z_AXIS],
  751. current_position[E_AXIS]
  752. );
  753. planner.buffer_line(
  754. current_position[X_AXIS] + duplicate_extruder_x_offset,
  755. current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS],
  756. planner.max_feedrate_mm_s[X_AXIS], 1
  757. );
  758. stepper.synchronize();
  759. SYNC_PLAN_POSITION_KINEMATIC();
  760. extruder_duplication_enabled = true;
  761. active_extruder_parked = false;
  762. #if ENABLED(DEBUG_LEVELING_FEATURE)
  763. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Set extruder_duplication_enabled\nClear active_extruder_parked");
  764. #endif
  765. }
  766. else {
  767. #if ENABLED(DEBUG_LEVELING_FEATURE)
  768. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Active extruder not 0");
  769. #endif
  770. }
  771. break;
  772. }
  773. }
  774. return false;
  775. }
  776. #endif // DUAL_X_CARRIAGE
  777. /**
  778. * Prepare a single move and get ready for the next one
  779. *
  780. * This may result in several calls to planner.buffer_line to
  781. * do smaller moves for DELTA, SCARA, mesh moves, etc.
  782. *
  783. * Make sure current_position[E] and destination[E] are good
  784. * before calling or cold/lengthy extrusion may get missed.
  785. */
  786. void prepare_move_to_destination() {
  787. clamp_to_software_endstops(destination);
  788. #if ENABLED(PREVENT_COLD_EXTRUSION) || ENABLED(PREVENT_LENGTHY_EXTRUDE)
  789. if (!DEBUGGING(DRYRUN)) {
  790. if (destination[E_AXIS] != current_position[E_AXIS]) {
  791. #if ENABLED(PREVENT_COLD_EXTRUSION)
  792. if (thermalManager.tooColdToExtrude(active_extruder)) {
  793. current_position[E_AXIS] = destination[E_AXIS]; // Behave as if the move really took place, but ignore E part
  794. SERIAL_ECHO_START();
  795. SERIAL_ECHOLNPGM(MSG_ERR_COLD_EXTRUDE_STOP);
  796. }
  797. #endif // PREVENT_COLD_EXTRUSION
  798. #if ENABLED(PREVENT_LENGTHY_EXTRUDE)
  799. if (FABS(destination[E_AXIS] - current_position[E_AXIS]) * planner.e_factor[active_extruder] > (EXTRUDE_MAXLENGTH)) {
  800. current_position[E_AXIS] = destination[E_AXIS]; // Behave as if the move really took place, but ignore E part
  801. SERIAL_ECHO_START();
  802. SERIAL_ECHOLNPGM(MSG_ERR_LONG_EXTRUDE_STOP);
  803. }
  804. #endif // PREVENT_LENGTHY_EXTRUDE
  805. }
  806. }
  807. #endif // PREVENT_COLD_EXTRUSION || PREVENT_LENGTHY_EXTRUDE
  808. #if ENABLED(DUAL_X_CARRIAGE)
  809. if (dual_x_carriage_unpark()) return;
  810. #endif
  811. if (
  812. #if UBL_SEGMENTED
  813. ubl.prepare_segmented_line_to(destination, MMS_SCALED(feedrate_mm_s))
  814. #elif IS_KINEMATIC
  815. prepare_kinematic_move_to(destination)
  816. #else
  817. prepare_move_to_destination_cartesian()
  818. #endif
  819. ) return;
  820. set_current_from_destination();
  821. }
  822. #if HAS_AXIS_UNHOMED_ERR
  823. bool axis_unhomed_error(const bool x/*=true*/, const bool y/*=true*/, const bool z/*=true*/) {
  824. #if ENABLED(HOME_AFTER_DEACTIVATE)
  825. const bool xx = x && !axis_known_position[X_AXIS],
  826. yy = y && !axis_known_position[Y_AXIS],
  827. zz = z && !axis_known_position[Z_AXIS];
  828. #else
  829. const bool xx = x && !axis_homed[X_AXIS],
  830. yy = y && !axis_homed[Y_AXIS],
  831. zz = z && !axis_homed[Z_AXIS];
  832. #endif
  833. if (xx || yy || zz) {
  834. SERIAL_ECHO_START();
  835. SERIAL_ECHOPGM(MSG_HOME " ");
  836. if (xx) SERIAL_ECHOPGM(MSG_X);
  837. if (yy) SERIAL_ECHOPGM(MSG_Y);
  838. if (zz) SERIAL_ECHOPGM(MSG_Z);
  839. SERIAL_ECHOLNPGM(" " MSG_FIRST);
  840. #if ENABLED(ULTRA_LCD)
  841. lcd_status_printf_P(0, PSTR(MSG_HOME " %s%s%s " MSG_FIRST), xx ? MSG_X : "", yy ? MSG_Y : "", zz ? MSG_Z : "");
  842. #endif
  843. return true;
  844. }
  845. return false;
  846. }
  847. #endif // HAS_AXIS_UNHOMED_ERR
  848. /**
  849. * Homing bump feedrate (mm/s)
  850. */
  851. inline float get_homing_bump_feedrate(const AxisEnum axis) {
  852. #if HOMING_Z_WITH_PROBE
  853. if (axis == Z_AXIS) return MMM_TO_MMS(Z_PROBE_SPEED_SLOW);
  854. #endif
  855. static const uint8_t homing_bump_divisor[] PROGMEM = HOMING_BUMP_DIVISOR;
  856. uint8_t hbd = pgm_read_byte(&homing_bump_divisor[axis]);
  857. if (hbd < 1) {
  858. hbd = 10;
  859. SERIAL_ECHO_START();
  860. SERIAL_ECHOLNPGM("Warning: Homing Bump Divisor < 1");
  861. }
  862. return homing_feedrate(axis) / hbd;
  863. }
  864. #if ENABLED(SENSORLESS_HOMING)
  865. /**
  866. * Set sensorless homing if the axis has it, accounting for Core Kinematics.
  867. */
  868. void sensorless_homing_per_axis(const AxisEnum axis, const bool enable/*=true*/) {
  869. switch (axis) {
  870. default: break;
  871. #if X_SENSORLESS
  872. case X_AXIS:
  873. tmc_sensorless_homing(stepperX, enable);
  874. #if CORE_IS_XY && Y_SENSORLESS
  875. tmc_sensorless_homing(stepperY, enable);
  876. #elif CORE_IS_XZ && Z_SENSORLESS
  877. tmc_sensorless_homing(stepperZ, enable);
  878. #endif
  879. break;
  880. #endif
  881. #if Y_SENSORLESS
  882. case Y_AXIS:
  883. tmc_sensorless_homing(stepperY, enable);
  884. #if CORE_IS_XY && X_SENSORLESS
  885. tmc_sensorless_homing(stepperX, enable);
  886. #elif CORE_IS_YZ && Z_SENSORLESS
  887. tmc_sensorless_homing(stepperZ, enable);
  888. #endif
  889. break;
  890. #endif
  891. #if Z_SENSORLESS
  892. case Z_AXIS:
  893. tmc_sensorless_homing(stepperZ, enable);
  894. #if CORE_IS_XZ && X_SENSORLESS
  895. tmc_sensorless_homing(stepperX, enable);
  896. #elif CORE_IS_YZ && Y_SENSORLESS
  897. tmc_sensorless_homing(stepperY, enable);
  898. #endif
  899. break;
  900. #endif
  901. }
  902. }
  903. #endif // SENSORLESS_HOMING
  904. /**
  905. * Home an individual linear axis
  906. */
  907. static void do_homing_move(const AxisEnum axis, const float distance, const float fr_mm_s=0.0) {
  908. #if ENABLED(DEBUG_LEVELING_FEATURE)
  909. if (DEBUGGING(LEVELING)) {
  910. SERIAL_ECHOPAIR(">>> do_homing_move(", axis_codes[axis]);
  911. SERIAL_ECHOPAIR(", ", distance);
  912. SERIAL_ECHOPAIR(", ", fr_mm_s);
  913. SERIAL_ECHOPAIR(" [", fr_mm_s ? fr_mm_s : homing_feedrate(axis));
  914. SERIAL_ECHOLNPGM("])");
  915. }
  916. #endif
  917. #if HOMING_Z_WITH_PROBE && HAS_HEATED_BED && ENABLED(WAIT_FOR_BED_HEATER)
  918. // Wait for bed to heat back up between probing points
  919. if (axis == Z_AXIS && distance < 0 && thermalManager.isHeatingBed()) {
  920. serialprintPGM(msg_wait_for_bed_heating);
  921. LCD_MESSAGEPGM(MSG_BED_HEATING);
  922. while (thermalManager.isHeatingBed()) safe_delay(200);
  923. lcd_reset_status();
  924. }
  925. #endif
  926. // Only do some things when moving towards an endstop
  927. const int8_t axis_home_dir =
  928. #if ENABLED(DUAL_X_CARRIAGE)
  929. (axis == X_AXIS) ? x_home_dir(active_extruder) :
  930. #endif
  931. home_dir(axis);
  932. const bool is_home_dir = (axis_home_dir > 0) == (distance > 0);
  933. if (is_home_dir) {
  934. if (axis == Z_AXIS) {
  935. #if HOMING_Z_WITH_PROBE
  936. #if ENABLED(BLTOUCH)
  937. set_bltouch_deployed(true);
  938. #endif
  939. #if QUIET_PROBING
  940. probing_pause(true);
  941. #endif
  942. #endif
  943. }
  944. // Disable stealthChop if used. Enable diag1 pin on driver.
  945. #if ENABLED(SENSORLESS_HOMING)
  946. sensorless_homing_per_axis(axis);
  947. #endif
  948. }
  949. // Tell the planner the axis is at 0
  950. current_position[axis] = 0;
  951. #if IS_SCARA
  952. SYNC_PLAN_POSITION_KINEMATIC();
  953. current_position[axis] = distance;
  954. inverse_kinematics(current_position);
  955. 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);
  956. #else
  957. sync_plan_position();
  958. current_position[axis] = distance; // Set delta/cartesian axes directly
  959. 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);
  960. #endif
  961. stepper.synchronize();
  962. if (is_home_dir) {
  963. if (axis == Z_AXIS) {
  964. #if HOMING_Z_WITH_PROBE
  965. #if QUIET_PROBING
  966. probing_pause(false);
  967. #endif
  968. #if ENABLED(BLTOUCH)
  969. set_bltouch_deployed(false);
  970. #endif
  971. #endif
  972. }
  973. endstops.hit_on_purpose();
  974. // Re-enable stealthChop if used. Disable diag1 pin on driver.
  975. #if ENABLED(SENSORLESS_HOMING)
  976. sensorless_homing_per_axis(axis, false);
  977. #endif
  978. }
  979. #if ENABLED(DEBUG_LEVELING_FEATURE)
  980. if (DEBUGGING(LEVELING)) {
  981. SERIAL_ECHOPAIR("<<< do_homing_move(", axis_codes[axis]);
  982. SERIAL_CHAR(')');
  983. SERIAL_EOL();
  984. }
  985. #endif
  986. }
  987. /**
  988. * Set an axis' current position to its home position (after homing).
  989. *
  990. * For Core and Cartesian robots this applies one-to-one when an
  991. * individual axis has been homed.
  992. *
  993. * DELTA should wait until all homing is done before setting the XYZ
  994. * current_position to home, because homing is a single operation.
  995. * In the case where the axis positions are already known and previously
  996. * homed, DELTA could home to X or Y individually by moving either one
  997. * to the center. However, homing Z always homes XY and Z.
  998. *
  999. * SCARA should wait until all XY homing is done before setting the XY
  1000. * current_position to home, because neither X nor Y is at home until
  1001. * both are at home. Z can however be homed individually.
  1002. *
  1003. * Callers must sync the planner position after calling this!
  1004. */
  1005. void set_axis_is_at_home(const AxisEnum axis) {
  1006. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1007. if (DEBUGGING(LEVELING)) {
  1008. SERIAL_ECHOPAIR(">>> set_axis_is_at_home(", axis_codes[axis]);
  1009. SERIAL_CHAR(')');
  1010. SERIAL_EOL();
  1011. }
  1012. #endif
  1013. axis_known_position[axis] = axis_homed[axis] = true;
  1014. #if HAS_POSITION_SHIFT
  1015. position_shift[axis] = 0;
  1016. update_software_endstops(axis);
  1017. #endif
  1018. #if ENABLED(DUAL_X_CARRIAGE)
  1019. if (axis == X_AXIS && (active_extruder == 1 || dual_x_carriage_mode == DXC_DUPLICATION_MODE)) {
  1020. current_position[X_AXIS] = x_home_pos(active_extruder);
  1021. return;
  1022. }
  1023. #endif
  1024. #if ENABLED(MORGAN_SCARA)
  1025. scara_set_axis_is_at_home(axis);
  1026. #elif ENABLED(DELTA)
  1027. current_position[axis] = (axis == Z_AXIS ? delta_height : base_home_pos(axis));
  1028. #else
  1029. current_position[axis] = base_home_pos(axis);
  1030. #endif
  1031. /**
  1032. * Z Probe Z Homing? Account for the probe's Z offset.
  1033. */
  1034. #if HAS_BED_PROBE && Z_HOME_DIR < 0
  1035. if (axis == Z_AXIS) {
  1036. #if HOMING_Z_WITH_PROBE
  1037. current_position[Z_AXIS] -= zprobe_zoffset;
  1038. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1039. if (DEBUGGING(LEVELING)) {
  1040. SERIAL_ECHOLNPGM("*** Z HOMED WITH PROBE (Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) ***");
  1041. SERIAL_ECHOLNPAIR("> zprobe_zoffset = ", zprobe_zoffset);
  1042. }
  1043. #endif
  1044. #elif ENABLED(DEBUG_LEVELING_FEATURE)
  1045. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("*** Z HOMED TO ENDSTOP (Z_MIN_PROBE_ENDSTOP) ***");
  1046. #endif
  1047. }
  1048. #endif
  1049. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1050. if (DEBUGGING(LEVELING)) {
  1051. #if HAS_HOME_OFFSET
  1052. SERIAL_ECHOPAIR("> home_offset[", axis_codes[axis]);
  1053. SERIAL_ECHOLNPAIR("] = ", home_offset[axis]);
  1054. #endif
  1055. DEBUG_POS("", current_position);
  1056. SERIAL_ECHOPAIR("<<< set_axis_is_at_home(", axis_codes[axis]);
  1057. SERIAL_CHAR(')');
  1058. SERIAL_EOL();
  1059. }
  1060. #endif
  1061. #if ENABLED(I2C_POSITION_ENCODERS)
  1062. I2CPEM.homed(axis);
  1063. #endif
  1064. }
  1065. /**
  1066. * Home an individual "raw axis" to its endstop.
  1067. * This applies to XYZ on Cartesian and Core robots, and
  1068. * to the individual ABC steppers on DELTA and SCARA.
  1069. *
  1070. * At the end of the procedure the axis is marked as
  1071. * homed and the current position of that axis is updated.
  1072. * Kinematic robots should wait till all axes are homed
  1073. * before updating the current position.
  1074. */
  1075. void homeaxis(const AxisEnum axis) {
  1076. #if IS_SCARA
  1077. // Only Z homing (with probe) is permitted
  1078. if (axis != Z_AXIS) { BUZZ(100, 880); return; }
  1079. #else
  1080. #define CAN_HOME(A) \
  1081. (axis == A##_AXIS && ((A##_MIN_PIN > -1 && A##_HOME_DIR < 0) || (A##_MAX_PIN > -1 && A##_HOME_DIR > 0)))
  1082. if (!CAN_HOME(X) && !CAN_HOME(Y) && !CAN_HOME(Z)) return;
  1083. #endif
  1084. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1085. if (DEBUGGING(LEVELING)) {
  1086. SERIAL_ECHOPAIR(">>> homeaxis(", axis_codes[axis]);
  1087. SERIAL_CHAR(')');
  1088. SERIAL_EOL();
  1089. }
  1090. #endif
  1091. const int axis_home_dir =
  1092. #if ENABLED(DUAL_X_CARRIAGE)
  1093. (axis == X_AXIS) ? x_home_dir(active_extruder) :
  1094. #endif
  1095. home_dir(axis);
  1096. // Homing Z towards the bed? Deploy the Z probe or endstop.
  1097. #if HOMING_Z_WITH_PROBE
  1098. if (axis == Z_AXIS && DEPLOY_PROBE()) return;
  1099. #endif
  1100. // Set flags for X, Y, Z motor locking
  1101. #if ENABLED(X_DUAL_ENDSTOPS)
  1102. if (axis == X_AXIS) stepper.set_homing_flag_x(true);
  1103. #endif
  1104. #if ENABLED(Y_DUAL_ENDSTOPS)
  1105. if (axis == Y_AXIS) stepper.set_homing_flag_y(true);
  1106. #endif
  1107. #if ENABLED(Z_DUAL_ENDSTOPS)
  1108. if (axis == Z_AXIS) stepper.set_homing_flag_z(true);
  1109. #endif
  1110. // Fast move towards endstop until triggered
  1111. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1112. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Home 1 Fast:");
  1113. #endif
  1114. do_homing_move(axis, 1.5 * max_length(axis) * axis_home_dir);
  1115. // When homing Z with probe respect probe clearance
  1116. const float bump = axis_home_dir * (
  1117. #if HOMING_Z_WITH_PROBE
  1118. (axis == Z_AXIS && (Z_HOME_BUMP_MM)) ? max(Z_CLEARANCE_BETWEEN_PROBES, Z_HOME_BUMP_MM) :
  1119. #endif
  1120. home_bump_mm(axis)
  1121. );
  1122. // If a second homing move is configured...
  1123. if (bump) {
  1124. // Move away from the endstop by the axis HOME_BUMP_MM
  1125. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1126. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Move Away:");
  1127. #endif
  1128. do_homing_move(axis, -bump
  1129. #if HOMING_Z_WITH_PROBE
  1130. , axis == Z_AXIS ? MMM_TO_MMS(Z_PROBE_SPEED_FAST) : 0.0
  1131. #endif
  1132. );
  1133. // Slow move towards endstop until triggered
  1134. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1135. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Home 2 Slow:");
  1136. #endif
  1137. do_homing_move(axis, 2 * bump, get_homing_bump_feedrate(axis));
  1138. }
  1139. #if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
  1140. const bool pos_dir = axis_home_dir > 0;
  1141. #if ENABLED(X_DUAL_ENDSTOPS)
  1142. if (axis == X_AXIS) {
  1143. const bool lock_x1 = pos_dir ? (endstops.x_endstop_adj > 0) : (endstops.x_endstop_adj < 0);
  1144. float adj = FABS(endstops.x_endstop_adj);
  1145. if (pos_dir) adj = -adj;
  1146. if (lock_x1) stepper.set_x_lock(true); else stepper.set_x2_lock(true);
  1147. do_homing_move(axis, adj);
  1148. if (lock_x1) stepper.set_x_lock(false); else stepper.set_x2_lock(false);
  1149. stepper.set_homing_flag_x(false);
  1150. }
  1151. #endif
  1152. #if ENABLED(Y_DUAL_ENDSTOPS)
  1153. if (axis == Y_AXIS) {
  1154. const bool lock_y1 = pos_dir ? (endstops.y_endstop_adj > 0) : (endstops.y_endstop_adj < 0);
  1155. float adj = FABS(endstops.y_endstop_adj);
  1156. if (pos_dir) adj = -adj;
  1157. if (lock_y1) stepper.set_y_lock(true); else stepper.set_y2_lock(true);
  1158. do_homing_move(axis, adj);
  1159. if (lock_y1) stepper.set_y_lock(false); else stepper.set_y2_lock(false);
  1160. stepper.set_homing_flag_y(false);
  1161. }
  1162. #endif
  1163. #if ENABLED(Z_DUAL_ENDSTOPS)
  1164. if (axis == Z_AXIS) {
  1165. const bool lock_z1 = pos_dir ? (endstops.z_endstop_adj > 0) : (endstops.z_endstop_adj < 0);
  1166. float adj = FABS(endstops.z_endstop_adj);
  1167. if (pos_dir) adj = -adj;
  1168. if (lock_z1) stepper.set_z_lock(true); else stepper.set_z2_lock(true);
  1169. do_homing_move(axis, adj);
  1170. if (lock_z1) stepper.set_z_lock(false); else stepper.set_z2_lock(false);
  1171. stepper.set_homing_flag_z(false);
  1172. }
  1173. #endif
  1174. #endif
  1175. #if IS_SCARA
  1176. set_axis_is_at_home(axis);
  1177. SYNC_PLAN_POSITION_KINEMATIC();
  1178. #elif ENABLED(DELTA)
  1179. // Delta has already moved all three towers up in G28
  1180. // so here it re-homes each tower in turn.
  1181. // Delta homing treats the axes as normal linear axes.
  1182. // retrace by the amount specified in delta_endstop_adj + additional dist in order to have minimum steps
  1183. if (delta_endstop_adj[axis] * Z_HOME_DIR <= 0) {
  1184. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1185. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("delta_endstop_adj:");
  1186. #endif
  1187. do_homing_move(axis, delta_endstop_adj[axis] - (MIN_STEPS_PER_SEGMENT + 1) * planner.steps_to_mm[axis] * Z_HOME_DIR);
  1188. }
  1189. #else
  1190. // For cartesian/core machines,
  1191. // set the axis to its home position
  1192. set_axis_is_at_home(axis);
  1193. sync_plan_position();
  1194. destination[axis] = current_position[axis];
  1195. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1196. if (DEBUGGING(LEVELING)) DEBUG_POS("> AFTER set_axis_is_at_home", current_position);
  1197. #endif
  1198. #endif
  1199. // Put away the Z probe
  1200. #if HOMING_Z_WITH_PROBE
  1201. if (axis == Z_AXIS && STOW_PROBE()) return;
  1202. #endif
  1203. // Clear z_lift if homing the Z axis
  1204. #if ENABLED(FWRETRACT)
  1205. if (axis == Z_AXIS)
  1206. fwretract.hop_amount = 0.0;
  1207. #endif
  1208. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1209. if (DEBUGGING(LEVELING)) {
  1210. SERIAL_ECHOPAIR("<<< homeaxis(", axis_codes[axis]);
  1211. SERIAL_CHAR(')');
  1212. SERIAL_EOL();
  1213. }
  1214. #endif
  1215. } // homeaxis()
  1216. #if HAS_WORKSPACE_OFFSET || ENABLED(DUAL_X_CARRIAGE) || ENABLED(DELTA)
  1217. /**
  1218. * Software endstops can be used to monitor the open end of
  1219. * an axis that has a hardware endstop on the other end. Or
  1220. * they can prevent axes from moving past endstops and grinding.
  1221. *
  1222. * To keep doing their job as the coordinate system changes,
  1223. * the software endstop positions must be refreshed to remain
  1224. * at the same positions relative to the machine.
  1225. */
  1226. void update_software_endstops(const AxisEnum axis) {
  1227. #if HAS_HOME_OFFSET && HAS_POSITION_SHIFT
  1228. workspace_offset[axis] = home_offset[axis] + position_shift[axis];
  1229. #endif
  1230. #if ENABLED(DUAL_X_CARRIAGE)
  1231. if (axis == X_AXIS) {
  1232. // In Dual X mode hotend_offset[X] is T1's home position
  1233. float dual_max_x = max(hotend_offset[X_AXIS][1], X2_MAX_POS);
  1234. if (active_extruder != 0) {
  1235. // T1 can move from X2_MIN_POS to X2_MAX_POS or X2 home position (whichever is larger)
  1236. soft_endstop_min[X_AXIS] = X2_MIN_POS;
  1237. soft_endstop_max[X_AXIS] = dual_max_x;
  1238. }
  1239. else if (dual_x_carriage_mode == DXC_DUPLICATION_MODE) {
  1240. // In Duplication Mode, T0 can move as far left as X_MIN_POS
  1241. // but not so far to the right that T1 would move past the end
  1242. soft_endstop_min[X_AXIS] = base_min_pos(X_AXIS);
  1243. soft_endstop_max[X_AXIS] = min(base_max_pos(X_AXIS), dual_max_x - duplicate_extruder_x_offset);
  1244. }
  1245. else {
  1246. // In other modes, T0 can move from X_MIN_POS to X_MAX_POS
  1247. soft_endstop_min[axis] = base_min_pos(axis);
  1248. soft_endstop_max[axis] = base_max_pos(axis);
  1249. }
  1250. }
  1251. #elif ENABLED(DELTA)
  1252. soft_endstop_min[axis] = base_min_pos(axis);
  1253. soft_endstop_max[axis] = (axis == Z_AXIS ? delta_height : base_max_pos(axis));
  1254. #else
  1255. soft_endstop_min[axis] = base_min_pos(axis);
  1256. soft_endstop_max[axis] = base_max_pos(axis);
  1257. #endif
  1258. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1259. if (DEBUGGING(LEVELING)) {
  1260. SERIAL_ECHOPAIR("For ", axis_codes[axis]);
  1261. #if HAS_HOME_OFFSET
  1262. SERIAL_ECHOPAIR(" axis:\n home_offset = ", home_offset[axis]);
  1263. #endif
  1264. #if HAS_POSITION_SHIFT
  1265. SERIAL_ECHOPAIR("\n position_shift = ", position_shift[axis]);
  1266. #endif
  1267. SERIAL_ECHOPAIR("\n soft_endstop_min = ", soft_endstop_min[axis]);
  1268. SERIAL_ECHOLNPAIR("\n soft_endstop_max = ", soft_endstop_max[axis]);
  1269. }
  1270. #endif
  1271. #if ENABLED(DELTA)
  1272. switch(axis) {
  1273. #if HAS_SOFTWARE_ENDSTOPS
  1274. case X_AXIS:
  1275. case Y_AXIS:
  1276. // Get a minimum radius for clamping
  1277. 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]);
  1278. soft_endstop_radius_2 = sq(soft_endstop_radius);
  1279. break;
  1280. #endif
  1281. case Z_AXIS:
  1282. delta_clip_start_height = soft_endstop_max[axis] - delta_safe_distance_from_top();
  1283. default: break;
  1284. }
  1285. #endif
  1286. }
  1287. #endif // HAS_WORKSPACE_OFFSET || DUAL_X_CARRIAGE || DELTA
  1288. #if HAS_M206_COMMAND
  1289. /**
  1290. * Change the home offset for an axis.
  1291. * Also refreshes the workspace offset.
  1292. */
  1293. void set_home_offset(const AxisEnum axis, const float v) {
  1294. home_offset[axis] = v;
  1295. update_software_endstops(axis);
  1296. }
  1297. #endif // HAS_M206_COMMAND