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

motion.cpp 47KB

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