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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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 'get_destination_from_command' 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. planner.get_axis_position_mm(A_AXIS),
  166. planner.get_axis_position_mm(B_AXIS),
  167. planner.get_axis_position_mm(C_AXIS)
  168. );
  169. #else
  170. #if IS_SCARA
  171. forward_kinematics_SCARA(
  172. planner.get_axis_position_degrees(A_AXIS),
  173. planner.get_axis_position_degrees(B_AXIS)
  174. );
  175. #else
  176. cartes[X_AXIS] = planner.get_axis_position_mm(X_AXIS);
  177. cartes[Y_AXIS] = planner.get_axis_position_mm(Y_AXIS);
  178. #endif
  179. cartes[Z_AXIS] = planner.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. planner.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 = ABS(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, 1U);
  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. if (!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. break;
  547. /*
  548. SERIAL_ECHO(segments);
  549. SERIAL_ECHOPAIR(": X=", raw[X_AXIS]); SERIAL_ECHOPAIR(" Y=", raw[Y_AXIS]);
  550. SERIAL_ECHOPAIR(" A=", delta[A_AXIS]); SERIAL_ECHOPAIR(" B=", delta[B_AXIS]);
  551. SERIAL_ECHOLNPAIR(" F", HYPOT(delta[A_AXIS] - oldA, delta[B_AXIS] - oldB) * inverse_secs * 60);
  552. safe_delay(5);
  553. //*/
  554. oldA = delta[A_AXIS]; oldB = delta[B_AXIS];
  555. #else
  556. if (!planner.buffer_line(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], raw[E_AXIS], _feedrate_mm_s, active_extruder, cartesian_segment_mm))
  557. break;
  558. #endif
  559. }
  560. // Ensure last segment arrives at target location.
  561. #if ENABLED(SCARA_FEEDRATE_SCALING)
  562. inverse_kinematics(rtarget);
  563. ADJUST_DELTA(rtarget);
  564. const float diff2 = HYPOT2(delta[A_AXIS] - oldA, delta[B_AXIS] - oldB);
  565. if (diff2) {
  566. planner.buffer_segment(delta[A_AXIS], delta[B_AXIS], rtarget[Z_AXIS], rtarget[E_AXIS], SQRT(diff2) * inverse_secs, active_extruder);
  567. /*
  568. SERIAL_ECHOPAIR("final: A=", delta[A_AXIS]); SERIAL_ECHOPAIR(" B=", delta[B_AXIS]);
  569. SERIAL_ECHOPAIR(" adiff=", delta[A_AXIS] - oldA); SERIAL_ECHOPAIR(" bdiff=", delta[B_AXIS] - oldB);
  570. SERIAL_ECHOLNPAIR(" F", (SQRT(diff2) * inverse_secs) * 60);
  571. SERIAL_EOL();
  572. safe_delay(5);
  573. //*/
  574. }
  575. #else
  576. planner.buffer_line_kinematic(rtarget, _feedrate_mm_s, active_extruder, cartesian_segment_mm);
  577. #endif
  578. return false; // caller will update current_position
  579. }
  580. #else // !IS_KINEMATIC
  581. #if ENABLED(SEGMENT_LEVELED_MOVES)
  582. /**
  583. * Prepare a segmented move on a CARTESIAN setup.
  584. *
  585. * This calls planner.buffer_line several times, adding
  586. * small incremental moves. This allows the planner to
  587. * apply more detailed bed leveling to the full move.
  588. */
  589. inline void segmented_line_to_destination(const float &fr_mm_s, const float segment_size=LEVELED_SEGMENT_LENGTH) {
  590. const float xdiff = destination[X_AXIS] - current_position[X_AXIS],
  591. ydiff = destination[Y_AXIS] - current_position[Y_AXIS];
  592. // If the move is only in Z/E don't split up the move
  593. if (!xdiff && !ydiff) {
  594. planner.buffer_line_kinematic(destination, fr_mm_s, active_extruder);
  595. return;
  596. }
  597. // Remaining cartesian distances
  598. const float zdiff = destination[Z_AXIS] - current_position[Z_AXIS],
  599. ediff = destination[E_AXIS] - current_position[E_AXIS];
  600. // Get the linear distance in XYZ
  601. // If the move is very short, check the E move distance
  602. // No E move either? Game over.
  603. float cartesian_mm = SQRT(sq(xdiff) + sq(ydiff) + sq(zdiff));
  604. if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = ABS(ediff);
  605. if (UNEAR_ZERO(cartesian_mm)) return;
  606. // The length divided by the segment size
  607. // At least one segment is required
  608. uint16_t segments = cartesian_mm / segment_size;
  609. NOLESS(segments, 1U);
  610. // The approximate length of each segment
  611. const float inv_segments = 1.0 / float(segments),
  612. cartesian_segment_mm = cartesian_mm * inv_segments,
  613. segment_distance[XYZE] = {
  614. xdiff * inv_segments,
  615. ydiff * inv_segments,
  616. zdiff * inv_segments,
  617. ediff * inv_segments
  618. };
  619. // SERIAL_ECHOPAIR("mm=", cartesian_mm);
  620. // SERIAL_ECHOLNPAIR(" segments=", segments);
  621. // SERIAL_ECHOLNPAIR(" segment_mm=", cartesian_segment_mm);
  622. // Get the raw current position as starting point
  623. float raw[XYZE];
  624. COPY(raw, current_position);
  625. // Calculate and execute the segments
  626. while (--segments) {
  627. static millis_t next_idle_ms = millis() + 200UL;
  628. thermalManager.manage_heater(); // This returns immediately if not really needed.
  629. if (ELAPSED(millis(), next_idle_ms)) {
  630. next_idle_ms = millis() + 200UL;
  631. idle();
  632. }
  633. LOOP_XYZE(i) raw[i] += segment_distance[i];
  634. if (!planner.buffer_line_kinematic(raw, fr_mm_s, active_extruder, cartesian_segment_mm))
  635. break;
  636. }
  637. // Since segment_distance is only approximate,
  638. // the final move must be to the exact destination.
  639. planner.buffer_line_kinematic(destination, fr_mm_s, active_extruder, cartesian_segment_mm);
  640. }
  641. #endif // SEGMENT_LEVELED_MOVES
  642. /**
  643. * Prepare a linear move in a Cartesian setup.
  644. *
  645. * When a mesh-based leveling system is active, moves are segmented
  646. * according to the configuration of the leveling system.
  647. *
  648. * Returns true if current_position[] was set to destination[]
  649. */
  650. inline bool prepare_move_to_destination_cartesian() {
  651. #if HAS_MESH
  652. if (planner.leveling_active && planner.leveling_active_at_z(destination[Z_AXIS])) {
  653. #if ENABLED(AUTO_BED_LEVELING_UBL)
  654. ubl.line_to_destination_cartesian(MMS_SCALED(feedrate_mm_s), active_extruder); // UBL's motion routine needs to know about
  655. return true; // all moves, including Z-only moves.
  656. #elif ENABLED(SEGMENT_LEVELED_MOVES)
  657. segmented_line_to_destination(MMS_SCALED(feedrate_mm_s));
  658. return false; // caller will update current_position
  659. #else
  660. /**
  661. * For MBL and ABL-BILINEAR only segment moves when X or Y are involved.
  662. * Otherwise fall through to do a direct single move.
  663. */
  664. if (current_position[X_AXIS] != destination[X_AXIS] || current_position[Y_AXIS] != destination[Y_AXIS]) {
  665. #if ENABLED(MESH_BED_LEVELING)
  666. mbl.line_to_destination(MMS_SCALED(feedrate_mm_s));
  667. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  668. bilinear_line_to_destination(MMS_SCALED(feedrate_mm_s));
  669. #endif
  670. return true;
  671. }
  672. #endif
  673. }
  674. #endif // HAS_MESH
  675. buffer_line_to_destination(MMS_SCALED(feedrate_mm_s));
  676. return false; // caller will update current_position
  677. }
  678. #endif // !IS_KINEMATIC
  679. #endif // !UBL_SEGMENTED
  680. #if ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE)
  681. bool extruder_duplication_enabled = false; // Used in Dual X mode 2
  682. #endif
  683. #if ENABLED(DUAL_X_CARRIAGE)
  684. DualXMode dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  685. float inactive_extruder_x_pos = X2_MAX_POS, // used in mode 0 & 1
  686. raised_parked_position[XYZE], // used in mode 1
  687. duplicate_extruder_x_offset = DEFAULT_DUPLICATION_X_OFFSET; // used in mode 2
  688. bool active_extruder_parked = false; // used in mode 1 & 2
  689. millis_t delayed_move_time = 0; // used in mode 1
  690. int16_t duplicate_extruder_temp_offset = 0; // used in mode 2
  691. float x_home_pos(const int extruder) {
  692. if (extruder == 0)
  693. return base_home_pos(X_AXIS);
  694. else
  695. /**
  696. * In dual carriage mode the extruder offset provides an override of the
  697. * second X-carriage position when homed - otherwise X2_HOME_POS is used.
  698. * This allows soft recalibration of the second extruder home position
  699. * without firmware reflash (through the M218 command).
  700. */
  701. return hotend_offset[X_AXIS][1] > 0 ? hotend_offset[X_AXIS][1] : X2_HOME_POS;
  702. }
  703. /**
  704. * Prepare a linear move in a dual X axis setup
  705. *
  706. * Return true if current_position[] was set to destination[]
  707. */
  708. inline bool dual_x_carriage_unpark() {
  709. if (active_extruder_parked) {
  710. switch (dual_x_carriage_mode) {
  711. case DXC_FULL_CONTROL_MODE:
  712. break;
  713. case DXC_AUTO_PARK_MODE:
  714. if (current_position[E_AXIS] == destination[E_AXIS]) {
  715. // This is a travel move (with no extrusion)
  716. // Skip it, but keep track of the current position
  717. // (so it can be used as the start of the next non-travel move)
  718. if (delayed_move_time != 0xFFFFFFFFUL) {
  719. set_current_from_destination();
  720. NOLESS(raised_parked_position[Z_AXIS], destination[Z_AXIS]);
  721. delayed_move_time = millis();
  722. return true;
  723. }
  724. }
  725. // unpark extruder: 1) raise, 2) move into starting XY position, 3) lower
  726. for (uint8_t i = 0; i < 3; i++)
  727. if (!planner.buffer_line(
  728. i == 0 ? raised_parked_position[X_AXIS] : current_position[X_AXIS],
  729. i == 0 ? raised_parked_position[Y_AXIS] : current_position[Y_AXIS],
  730. i == 2 ? current_position[Z_AXIS] : raised_parked_position[Z_AXIS],
  731. current_position[E_AXIS],
  732. i == 1 ? PLANNER_XY_FEEDRATE() : planner.max_feedrate_mm_s[Z_AXIS],
  733. active_extruder)
  734. ) break;
  735. delayed_move_time = 0;
  736. active_extruder_parked = false;
  737. #if ENABLED(DEBUG_LEVELING_FEATURE)
  738. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Clear active_extruder_parked");
  739. #endif
  740. break;
  741. case DXC_DUPLICATION_MODE:
  742. if (active_extruder == 0) {
  743. #if ENABLED(DEBUG_LEVELING_FEATURE)
  744. if (DEBUGGING(LEVELING)) {
  745. SERIAL_ECHOPAIR("Set planner X", inactive_extruder_x_pos);
  746. SERIAL_ECHOLNPAIR(" ... Line to X", current_position[X_AXIS] + duplicate_extruder_x_offset);
  747. }
  748. #endif
  749. // move duplicate extruder into correct duplication position.
  750. planner.set_position_mm(inactive_extruder_x_pos, current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  751. if (!planner.buffer_line(
  752. current_position[X_AXIS] + duplicate_extruder_x_offset,
  753. current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS],
  754. planner.max_feedrate_mm_s[X_AXIS], 1)
  755. ) break;
  756. planner.synchronize();
  757. SYNC_PLAN_POSITION_KINEMATIC();
  758. extruder_duplication_enabled = true;
  759. active_extruder_parked = false;
  760. #if ENABLED(DEBUG_LEVELING_FEATURE)
  761. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Set extruder_duplication_enabled\nClear active_extruder_parked");
  762. #endif
  763. }
  764. else {
  765. #if ENABLED(DEBUG_LEVELING_FEATURE)
  766. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Active extruder not 0");
  767. #endif
  768. }
  769. break;
  770. }
  771. }
  772. return false;
  773. }
  774. #endif // DUAL_X_CARRIAGE
  775. /**
  776. * Prepare a single move and get ready for the next one
  777. *
  778. * This may result in several calls to planner.buffer_line to
  779. * do smaller moves for DELTA, SCARA, mesh moves, etc.
  780. *
  781. * Make sure current_position[E] and destination[E] are good
  782. * before calling or cold/lengthy extrusion may get missed.
  783. */
  784. void prepare_move_to_destination() {
  785. clamp_to_software_endstops(destination);
  786. #if ENABLED(PREVENT_COLD_EXTRUSION) || ENABLED(PREVENT_LENGTHY_EXTRUDE)
  787. if (!DEBUGGING(DRYRUN)) {
  788. if (destination[E_AXIS] != current_position[E_AXIS]) {
  789. #if ENABLED(PREVENT_COLD_EXTRUSION)
  790. if (thermalManager.tooColdToExtrude(active_extruder)) {
  791. current_position[E_AXIS] = destination[E_AXIS]; // Behave as if the move really took place, but ignore E part
  792. SERIAL_ECHO_START();
  793. SERIAL_ECHOLNPGM(MSG_ERR_COLD_EXTRUDE_STOP);
  794. }
  795. #endif // PREVENT_COLD_EXTRUSION
  796. #if ENABLED(PREVENT_LENGTHY_EXTRUDE)
  797. if (ABS(destination[E_AXIS] - current_position[E_AXIS]) * planner.e_factor[active_extruder] > (EXTRUDE_MAXLENGTH)) {
  798. current_position[E_AXIS] = destination[E_AXIS]; // Behave as if the move really took place, but ignore E part
  799. SERIAL_ECHO_START();
  800. SERIAL_ECHOLNPGM(MSG_ERR_LONG_EXTRUDE_STOP);
  801. }
  802. #endif // PREVENT_LENGTHY_EXTRUDE
  803. }
  804. }
  805. #endif // PREVENT_COLD_EXTRUSION || PREVENT_LENGTHY_EXTRUDE
  806. #if ENABLED(DUAL_X_CARRIAGE)
  807. if (dual_x_carriage_unpark()) return;
  808. #endif
  809. if (
  810. #if UBL_SEGMENTED
  811. ubl.prepare_segmented_line_to(destination, MMS_SCALED(feedrate_mm_s))
  812. #elif IS_KINEMATIC
  813. prepare_kinematic_move_to(destination)
  814. #else
  815. prepare_move_to_destination_cartesian()
  816. #endif
  817. ) return;
  818. set_current_from_destination();
  819. }
  820. #if HAS_AXIS_UNHOMED_ERR
  821. bool axis_unhomed_error(const bool x/*=true*/, const bool y/*=true*/, const bool z/*=true*/) {
  822. #if ENABLED(HOME_AFTER_DEACTIVATE)
  823. const bool xx = x && !axis_known_position[X_AXIS],
  824. yy = y && !axis_known_position[Y_AXIS],
  825. zz = z && !axis_known_position[Z_AXIS];
  826. #else
  827. const bool xx = x && !axis_homed[X_AXIS],
  828. yy = y && !axis_homed[Y_AXIS],
  829. zz = z && !axis_homed[Z_AXIS];
  830. #endif
  831. if (xx || yy || zz) {
  832. SERIAL_ECHO_START();
  833. SERIAL_ECHOPGM(MSG_HOME " ");
  834. if (xx) SERIAL_ECHOPGM(MSG_X);
  835. if (yy) SERIAL_ECHOPGM(MSG_Y);
  836. if (zz) SERIAL_ECHOPGM(MSG_Z);
  837. SERIAL_ECHOLNPGM(" " MSG_FIRST);
  838. #if ENABLED(ULTRA_LCD)
  839. lcd_status_printf_P(0, PSTR(MSG_HOME " %s%s%s " MSG_FIRST), xx ? MSG_X : "", yy ? MSG_Y : "", zz ? MSG_Z : "");
  840. #endif
  841. return true;
  842. }
  843. return false;
  844. }
  845. #endif // HAS_AXIS_UNHOMED_ERR
  846. /**
  847. * Homing bump feedrate (mm/s)
  848. */
  849. inline float get_homing_bump_feedrate(const AxisEnum axis) {
  850. #if HOMING_Z_WITH_PROBE
  851. if (axis == Z_AXIS) return MMM_TO_MMS(Z_PROBE_SPEED_SLOW);
  852. #endif
  853. static const uint8_t homing_bump_divisor[] PROGMEM = HOMING_BUMP_DIVISOR;
  854. uint8_t hbd = pgm_read_byte(&homing_bump_divisor[axis]);
  855. if (hbd < 1) {
  856. hbd = 10;
  857. SERIAL_ECHO_START();
  858. SERIAL_ECHOLNPGM("Warning: Homing Bump Divisor < 1");
  859. }
  860. return homing_feedrate(axis) / hbd;
  861. }
  862. #if ENABLED(SENSORLESS_HOMING)
  863. /**
  864. * Set sensorless homing if the axis has it, accounting for Core Kinematics.
  865. */
  866. void sensorless_homing_per_axis(const AxisEnum axis, const bool enable/*=true*/) {
  867. switch (axis) {
  868. default: break;
  869. #if X_SENSORLESS
  870. case X_AXIS:
  871. tmc_sensorless_homing(stepperX, enable);
  872. #if CORE_IS_XY && Y_SENSORLESS
  873. tmc_sensorless_homing(stepperY, enable);
  874. #elif CORE_IS_XZ && Z_SENSORLESS
  875. tmc_sensorless_homing(stepperZ, enable);
  876. #endif
  877. break;
  878. #endif
  879. #if Y_SENSORLESS
  880. case Y_AXIS:
  881. tmc_sensorless_homing(stepperY, enable);
  882. #if CORE_IS_XY && X_SENSORLESS
  883. tmc_sensorless_homing(stepperX, enable);
  884. #elif CORE_IS_YZ && Z_SENSORLESS
  885. tmc_sensorless_homing(stepperZ, enable);
  886. #endif
  887. break;
  888. #endif
  889. #if Z_SENSORLESS
  890. case Z_AXIS:
  891. tmc_sensorless_homing(stepperZ, enable);
  892. #if CORE_IS_XZ && X_SENSORLESS
  893. tmc_sensorless_homing(stepperX, enable);
  894. #elif CORE_IS_YZ && Y_SENSORLESS
  895. tmc_sensorless_homing(stepperY, enable);
  896. #endif
  897. break;
  898. #endif
  899. }
  900. }
  901. #endif // SENSORLESS_HOMING
  902. /**
  903. * Home an individual linear axis
  904. */
  905. static void do_homing_move(const AxisEnum axis, const float distance, const float fr_mm_s=0.0) {
  906. #if ENABLED(DEBUG_LEVELING_FEATURE)
  907. if (DEBUGGING(LEVELING)) {
  908. SERIAL_ECHOPAIR(">>> do_homing_move(", axis_codes[axis]);
  909. SERIAL_ECHOPAIR(", ", distance);
  910. SERIAL_ECHOPGM(", ");
  911. if (fr_mm_s)
  912. SERIAL_ECHO(fr_mm_s);
  913. else {
  914. SERIAL_ECHOPAIR("[", homing_feedrate(axis));
  915. SERIAL_CHAR(']');
  916. }
  917. SERIAL_ECHOLNPGM(")");
  918. }
  919. #endif
  920. #if HOMING_Z_WITH_PROBE && HAS_HEATED_BED && ENABLED(WAIT_FOR_BED_HEATER)
  921. // Wait for bed to heat back up between probing points
  922. if (axis == Z_AXIS && distance < 0 && thermalManager.isHeatingBed()) {
  923. serialprintPGM(msg_wait_for_bed_heating);
  924. LCD_MESSAGEPGM(MSG_BED_HEATING);
  925. while (thermalManager.isHeatingBed()) safe_delay(200);
  926. lcd_reset_status();
  927. }
  928. #endif
  929. // Only do some things when moving towards an endstop
  930. const int8_t axis_home_dir =
  931. #if ENABLED(DUAL_X_CARRIAGE)
  932. (axis == X_AXIS) ? x_home_dir(active_extruder) :
  933. #endif
  934. home_dir(axis);
  935. const bool is_home_dir = (axis_home_dir > 0) == (distance > 0);
  936. if (is_home_dir) {
  937. if (axis == Z_AXIS) {
  938. #if HOMING_Z_WITH_PROBE
  939. #if ENABLED(BLTOUCH)
  940. set_bltouch_deployed(true);
  941. #endif
  942. #if QUIET_PROBING
  943. probing_pause(true);
  944. #endif
  945. #endif
  946. }
  947. // Disable stealthChop if used. Enable diag1 pin on driver.
  948. #if ENABLED(SENSORLESS_HOMING)
  949. sensorless_homing_per_axis(axis);
  950. #endif
  951. }
  952. // Tell the planner the axis is at 0
  953. current_position[axis] = 0;
  954. #if IS_SCARA
  955. SYNC_PLAN_POSITION_KINEMATIC();
  956. current_position[axis] = distance;
  957. inverse_kinematics(current_position);
  958. 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);
  959. #else
  960. sync_plan_position();
  961. current_position[axis] = distance; // Set delta/cartesian axes directly
  962. 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);
  963. #endif
  964. planner.synchronize();
  965. if (is_home_dir) {
  966. if (axis == Z_AXIS) {
  967. #if HOMING_Z_WITH_PROBE
  968. #if QUIET_PROBING
  969. probing_pause(false);
  970. #endif
  971. #if ENABLED(BLTOUCH)
  972. set_bltouch_deployed(false);
  973. #endif
  974. #endif
  975. }
  976. endstops.hit_on_purpose();
  977. // Re-enable stealthChop if used. Disable diag1 pin on driver.
  978. #if ENABLED(SENSORLESS_HOMING)
  979. sensorless_homing_per_axis(axis, false);
  980. #endif
  981. }
  982. #if ENABLED(DEBUG_LEVELING_FEATURE)
  983. if (DEBUGGING(LEVELING)) {
  984. SERIAL_ECHOPAIR("<<< do_homing_move(", axis_codes[axis]);
  985. SERIAL_CHAR(')');
  986. SERIAL_EOL();
  987. }
  988. #endif
  989. }
  990. /**
  991. * Set an axis' current position to its home position (after homing).
  992. *
  993. * For Core and Cartesian robots this applies one-to-one when an
  994. * individual axis has been homed.
  995. *
  996. * DELTA should wait until all homing is done before setting the XYZ
  997. * current_position to home, because homing is a single operation.
  998. * In the case where the axis positions are already known and previously
  999. * homed, DELTA could home to X or Y individually by moving either one
  1000. * to the center. However, homing Z always homes XY and Z.
  1001. *
  1002. * SCARA should wait until all XY homing is done before setting the XY
  1003. * current_position to home, because neither X nor Y is at home until
  1004. * both are at home. Z can however be homed individually.
  1005. *
  1006. * Callers must sync the planner position after calling this!
  1007. */
  1008. void set_axis_is_at_home(const AxisEnum axis) {
  1009. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1010. if (DEBUGGING(LEVELING)) {
  1011. SERIAL_ECHOPAIR(">>> set_axis_is_at_home(", axis_codes[axis]);
  1012. SERIAL_CHAR(')');
  1013. SERIAL_EOL();
  1014. }
  1015. #endif
  1016. axis_known_position[axis] = axis_homed[axis] = true;
  1017. #if HAS_POSITION_SHIFT
  1018. position_shift[axis] = 0;
  1019. update_software_endstops(axis);
  1020. #endif
  1021. #if ENABLED(DUAL_X_CARRIAGE)
  1022. if (axis == X_AXIS && (active_extruder == 1 || dual_x_carriage_mode == DXC_DUPLICATION_MODE)) {
  1023. current_position[X_AXIS] = x_home_pos(active_extruder);
  1024. return;
  1025. }
  1026. #endif
  1027. #if ENABLED(MORGAN_SCARA)
  1028. scara_set_axis_is_at_home(axis);
  1029. #elif ENABLED(DELTA)
  1030. current_position[axis] = (axis == Z_AXIS ? delta_height : base_home_pos(axis));
  1031. #else
  1032. current_position[axis] = base_home_pos(axis);
  1033. #endif
  1034. /**
  1035. * Z Probe Z Homing? Account for the probe's Z offset.
  1036. */
  1037. #if HAS_BED_PROBE && Z_HOME_DIR < 0
  1038. if (axis == Z_AXIS) {
  1039. #if HOMING_Z_WITH_PROBE
  1040. current_position[Z_AXIS] -= zprobe_zoffset;
  1041. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1042. if (DEBUGGING(LEVELING)) {
  1043. SERIAL_ECHOLNPGM("*** Z HOMED WITH PROBE (Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) ***");
  1044. SERIAL_ECHOLNPAIR("> zprobe_zoffset = ", zprobe_zoffset);
  1045. }
  1046. #endif
  1047. #elif ENABLED(DEBUG_LEVELING_FEATURE)
  1048. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("*** Z HOMED TO ENDSTOP (Z_MIN_PROBE_ENDSTOP) ***");
  1049. #endif
  1050. }
  1051. #endif
  1052. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1053. if (DEBUGGING(LEVELING)) {
  1054. #if HAS_HOME_OFFSET
  1055. SERIAL_ECHOPAIR("> home_offset[", axis_codes[axis]);
  1056. SERIAL_ECHOLNPAIR("] = ", home_offset[axis]);
  1057. #endif
  1058. DEBUG_POS("", current_position);
  1059. SERIAL_ECHOPAIR("<<< set_axis_is_at_home(", axis_codes[axis]);
  1060. SERIAL_CHAR(')');
  1061. SERIAL_EOL();
  1062. }
  1063. #endif
  1064. #if ENABLED(I2C_POSITION_ENCODERS)
  1065. I2CPEM.homed(axis);
  1066. #endif
  1067. }
  1068. /**
  1069. * Home an individual "raw axis" to its endstop.
  1070. * This applies to XYZ on Cartesian and Core robots, and
  1071. * to the individual ABC steppers on DELTA and SCARA.
  1072. *
  1073. * At the end of the procedure the axis is marked as
  1074. * homed and the current position of that axis is updated.
  1075. * Kinematic robots should wait till all axes are homed
  1076. * before updating the current position.
  1077. */
  1078. void homeaxis(const AxisEnum axis) {
  1079. #if IS_SCARA
  1080. // Only Z homing (with probe) is permitted
  1081. if (axis != Z_AXIS) { BUZZ(100, 880); return; }
  1082. #else
  1083. #define CAN_HOME(A) \
  1084. (axis == _AXIS(A) && ((A##_MIN_PIN > -1 && A##_HOME_DIR < 0) || (A##_MAX_PIN > -1 && A##_HOME_DIR > 0)))
  1085. if (!CAN_HOME(X) && !CAN_HOME(Y) && !CAN_HOME(Z)) return;
  1086. #endif
  1087. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1088. if (DEBUGGING(LEVELING)) {
  1089. SERIAL_ECHOPAIR(">>> homeaxis(", axis_codes[axis]);
  1090. SERIAL_CHAR(')');
  1091. SERIAL_EOL();
  1092. }
  1093. #endif
  1094. const int axis_home_dir = (
  1095. #if ENABLED(DUAL_X_CARRIAGE)
  1096. axis == X_AXIS ? x_home_dir(active_extruder) :
  1097. #endif
  1098. home_dir(axis)
  1099. );
  1100. // Homing Z towards the bed? Deploy the Z probe or endstop.
  1101. #if HOMING_Z_WITH_PROBE
  1102. if (axis == Z_AXIS && DEPLOY_PROBE()) return;
  1103. #endif
  1104. // Set flags for X, Y, Z motor locking
  1105. #if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
  1106. switch (axis) {
  1107. #if ENABLED(X_DUAL_ENDSTOPS)
  1108. case X_AXIS:
  1109. #endif
  1110. #if ENABLED(Y_DUAL_ENDSTOPS)
  1111. case Y_AXIS:
  1112. #endif
  1113. #if ENABLED(Z_DUAL_ENDSTOPS)
  1114. case Z_AXIS:
  1115. #endif
  1116. stepper.set_homing_dual_axis(true);
  1117. default: break;
  1118. }
  1119. #endif
  1120. // Fast move towards endstop until triggered
  1121. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1122. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Home 1 Fast:");
  1123. #endif
  1124. do_homing_move(axis, 1.5 * max_length(axis) * axis_home_dir);
  1125. // When homing Z with probe respect probe clearance
  1126. const float bump = axis_home_dir * (
  1127. #if HOMING_Z_WITH_PROBE
  1128. (axis == Z_AXIS && (Z_HOME_BUMP_MM)) ? MAX(Z_CLEARANCE_BETWEEN_PROBES, Z_HOME_BUMP_MM) :
  1129. #endif
  1130. home_bump_mm(axis)
  1131. );
  1132. // If a second homing move is configured...
  1133. if (bump) {
  1134. // Move away from the endstop by the axis HOME_BUMP_MM
  1135. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1136. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Move Away:");
  1137. #endif
  1138. do_homing_move(axis, -bump
  1139. #if HOMING_Z_WITH_PROBE
  1140. , axis == Z_AXIS ? MMM_TO_MMS(Z_PROBE_SPEED_FAST) : 0.0
  1141. #endif
  1142. );
  1143. // Slow move towards endstop until triggered
  1144. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1145. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Home 2 Slow:");
  1146. #endif
  1147. do_homing_move(axis, 2 * bump, get_homing_bump_feedrate(axis));
  1148. }
  1149. #if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
  1150. const bool pos_dir = axis_home_dir > 0;
  1151. #if ENABLED(X_DUAL_ENDSTOPS)
  1152. if (axis == X_AXIS) {
  1153. const float adj = ABS(endstops.x_endstop_adj);
  1154. if (pos_dir ? (endstops.x_endstop_adj > 0) : (endstops.x_endstop_adj < 0)) stepper.set_x_lock(true); else stepper.set_x2_lock(true);
  1155. do_homing_move(axis, pos_dir ? -adj : adj);
  1156. stepper.set_x_lock(false);
  1157. stepper.set_x2_lock(false);
  1158. }
  1159. #endif
  1160. #if ENABLED(Y_DUAL_ENDSTOPS)
  1161. if (axis == Y_AXIS) {
  1162. const float adj = ABS(endstops.y_endstop_adj);
  1163. if (pos_dir ? (endstops.y_endstop_adj > 0) : (endstops.y_endstop_adj < 0)) stepper.set_y_lock(true); else stepper.set_y2_lock(true);
  1164. do_homing_move(axis, pos_dir ? -adj : adj);
  1165. stepper.set_y_lock(false);
  1166. stepper.set_y2_lock(false);
  1167. }
  1168. #endif
  1169. #if ENABLED(Z_DUAL_ENDSTOPS)
  1170. if (axis == Z_AXIS) {
  1171. const float adj = ABS(endstops.z_endstop_adj);
  1172. if (pos_dir ? (endstops.z_endstop_adj > 0) : (endstops.z_endstop_adj < 0)) stepper.set_z_lock(true); else stepper.set_z2_lock(true);
  1173. do_homing_move(axis, pos_dir ? -adj : adj);
  1174. stepper.set_z_lock(false);
  1175. stepper.set_z2_lock(false);
  1176. }
  1177. #endif
  1178. stepper.set_homing_dual_axis(false);
  1179. #endif
  1180. #if IS_SCARA
  1181. set_axis_is_at_home(axis);
  1182. SYNC_PLAN_POSITION_KINEMATIC();
  1183. #elif ENABLED(DELTA)
  1184. // Delta has already moved all three towers up in G28
  1185. // so here it re-homes each tower in turn.
  1186. // Delta homing treats the axes as normal linear axes.
  1187. // retrace by the amount specified in delta_endstop_adj + additional dist in order to have minimum steps
  1188. if (delta_endstop_adj[axis] * Z_HOME_DIR <= 0) {
  1189. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1190. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("delta_endstop_adj:");
  1191. #endif
  1192. do_homing_move(axis, delta_endstop_adj[axis] - (MIN_STEPS_PER_SEGMENT + 1) * planner.steps_to_mm[axis] * Z_HOME_DIR);
  1193. }
  1194. #else
  1195. // For cartesian/core machines,
  1196. // set the axis to its home position
  1197. set_axis_is_at_home(axis);
  1198. sync_plan_position();
  1199. destination[axis] = current_position[axis];
  1200. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1201. if (DEBUGGING(LEVELING)) DEBUG_POS("> AFTER set_axis_is_at_home", current_position);
  1202. #endif
  1203. #endif
  1204. // Put away the Z probe
  1205. #if HOMING_Z_WITH_PROBE
  1206. if (axis == Z_AXIS && STOW_PROBE()) return;
  1207. #endif
  1208. // Clear retracted status if homing the Z axis
  1209. #if ENABLED(FWRETRACT)
  1210. if (axis == Z_AXIS) fwretract.hop_amount = 0.0;
  1211. #endif
  1212. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1213. if (DEBUGGING(LEVELING)) {
  1214. SERIAL_ECHOPAIR("<<< homeaxis(", axis_codes[axis]);
  1215. SERIAL_CHAR(')');
  1216. SERIAL_EOL();
  1217. }
  1218. #endif
  1219. } // homeaxis()
  1220. #if HAS_WORKSPACE_OFFSET || ENABLED(DUAL_X_CARRIAGE) || ENABLED(DELTA)
  1221. /**
  1222. * Software endstops can be used to monitor the open end of
  1223. * an axis that has a hardware endstop on the other end. Or
  1224. * they can prevent axes from moving past endstops and grinding.
  1225. *
  1226. * To keep doing their job as the coordinate system changes,
  1227. * the software endstop positions must be refreshed to remain
  1228. * at the same positions relative to the machine.
  1229. */
  1230. void update_software_endstops(const AxisEnum axis) {
  1231. #if HAS_HOME_OFFSET && HAS_POSITION_SHIFT
  1232. workspace_offset[axis] = home_offset[axis] + position_shift[axis];
  1233. #endif
  1234. #if ENABLED(DUAL_X_CARRIAGE)
  1235. if (axis == X_AXIS) {
  1236. // In Dual X mode hotend_offset[X] is T1's home position
  1237. float dual_max_x = MAX(hotend_offset[X_AXIS][1], X2_MAX_POS);
  1238. if (active_extruder != 0) {
  1239. // T1 can move from X2_MIN_POS to X2_MAX_POS or X2 home position (whichever is larger)
  1240. soft_endstop_min[X_AXIS] = X2_MIN_POS;
  1241. soft_endstop_max[X_AXIS] = dual_max_x;
  1242. }
  1243. else if (dual_x_carriage_mode == DXC_DUPLICATION_MODE) {
  1244. // In Duplication Mode, T0 can move as far left as X_MIN_POS
  1245. // but not so far to the right that T1 would move past the end
  1246. soft_endstop_min[X_AXIS] = base_min_pos(X_AXIS);
  1247. soft_endstop_max[X_AXIS] = MIN(base_max_pos(X_AXIS), dual_max_x - duplicate_extruder_x_offset);
  1248. }
  1249. else {
  1250. // In other modes, T0 can move from X_MIN_POS to X_MAX_POS
  1251. soft_endstop_min[axis] = base_min_pos(axis);
  1252. soft_endstop_max[axis] = base_max_pos(axis);
  1253. }
  1254. }
  1255. #elif ENABLED(DELTA)
  1256. soft_endstop_min[axis] = base_min_pos(axis);
  1257. soft_endstop_max[axis] = (axis == Z_AXIS ? delta_height : base_max_pos(axis));
  1258. #else
  1259. soft_endstop_min[axis] = base_min_pos(axis);
  1260. soft_endstop_max[axis] = base_max_pos(axis);
  1261. #endif
  1262. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1263. if (DEBUGGING(LEVELING)) {
  1264. SERIAL_ECHOPAIR("For ", axis_codes[axis]);
  1265. #if HAS_HOME_OFFSET
  1266. SERIAL_ECHOPAIR(" axis:\n home_offset = ", home_offset[axis]);
  1267. #endif
  1268. #if HAS_POSITION_SHIFT
  1269. SERIAL_ECHOPAIR("\n position_shift = ", position_shift[axis]);
  1270. #endif
  1271. SERIAL_ECHOPAIR("\n soft_endstop_min = ", soft_endstop_min[axis]);
  1272. SERIAL_ECHOLNPAIR("\n soft_endstop_max = ", soft_endstop_max[axis]);
  1273. }
  1274. #endif
  1275. #if ENABLED(DELTA)
  1276. switch (axis) {
  1277. #if HAS_SOFTWARE_ENDSTOPS
  1278. case X_AXIS:
  1279. case Y_AXIS:
  1280. // Get a minimum radius for clamping
  1281. soft_endstop_radius = MIN3(ABS(MAX(soft_endstop_min[X_AXIS], soft_endstop_min[Y_AXIS])), soft_endstop_max[X_AXIS], soft_endstop_max[Y_AXIS]);
  1282. soft_endstop_radius_2 = sq(soft_endstop_radius);
  1283. break;
  1284. #endif
  1285. case Z_AXIS:
  1286. delta_clip_start_height = soft_endstop_max[axis] - delta_safe_distance_from_top();
  1287. default: break;
  1288. }
  1289. #endif
  1290. }
  1291. #endif // HAS_WORKSPACE_OFFSET || DUAL_X_CARRIAGE || DELTA
  1292. #if HAS_M206_COMMAND
  1293. /**
  1294. * Change the home offset for an axis.
  1295. * Also refreshes the workspace offset.
  1296. */
  1297. void set_home_offset(const AxisEnum axis, const float v) {
  1298. home_offset[axis] = v;
  1299. update_software_endstops(axis);
  1300. }
  1301. #endif // HAS_M206_COMMAND