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

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