My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

motion.cpp 43KB

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