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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  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 NEED_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_to_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_to_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_to_current(); // sync destination at the start
  257. #if ENABLED(DEBUG_LEVELING_FEATURE)
  258. if (DEBUGGING(LEVELING)) DEBUG_POS("set_destination_to_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_to_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_to_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_to_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_to_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_to_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_to_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. /**
  385. * Constrain the given coordinates to the software endstops.
  386. */
  387. // NOTE: This makes no sense for delta beds other than Z-axis.
  388. // For delta the X/Y would need to be clamped at
  389. // DELTA_PRINTABLE_RADIUS from center of bed, but delta
  390. // now enforces is_position_reachable for X/Y regardless
  391. // of HAS_SOFTWARE_ENDSTOPS, so that enforcement would be
  392. // redundant here.
  393. void clamp_to_software_endstops(float target[XYZ]) {
  394. if (!soft_endstops_enabled) return;
  395. #if ENABLED(MIN_SOFTWARE_ENDSTOPS)
  396. #if DISABLED(DELTA)
  397. NOLESS(target[X_AXIS], soft_endstop_min[X_AXIS]);
  398. NOLESS(target[Y_AXIS], soft_endstop_min[Y_AXIS]);
  399. #endif
  400. NOLESS(target[Z_AXIS], soft_endstop_min[Z_AXIS]);
  401. #endif
  402. #if ENABLED(MAX_SOFTWARE_ENDSTOPS)
  403. #if DISABLED(DELTA)
  404. NOMORE(target[X_AXIS], soft_endstop_max[X_AXIS]);
  405. NOMORE(target[Y_AXIS], soft_endstop_max[Y_AXIS]);
  406. #endif
  407. NOMORE(target[Z_AXIS], soft_endstop_max[Z_AXIS]);
  408. #endif
  409. }
  410. #endif
  411. #if IS_KINEMATIC && !UBL_DELTA
  412. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  413. #if ENABLED(DELTA)
  414. #define ADJUST_DELTA(V) \
  415. if (planner.abl_enabled) { \
  416. const float zadj = bilinear_z_offset(V); \
  417. delta[A_AXIS] += zadj; \
  418. delta[B_AXIS] += zadj; \
  419. delta[C_AXIS] += zadj; \
  420. }
  421. #else
  422. #define ADJUST_DELTA(V) if (planner.abl_enabled) { delta[Z_AXIS] += bilinear_z_offset(V); }
  423. #endif
  424. #else
  425. #define ADJUST_DELTA(V) NOOP
  426. #endif
  427. /**
  428. * Prepare a linear move in a DELTA or SCARA setup.
  429. *
  430. * This calls planner.buffer_line several times, adding
  431. * small incremental moves for DELTA or SCARA.
  432. */
  433. inline bool prepare_kinematic_move_to(float ltarget[XYZE]) {
  434. // Get the top feedrate of the move in the XY plane
  435. const float _feedrate_mm_s = MMS_SCALED(feedrate_mm_s);
  436. // If the move is only in Z/E don't split up the move
  437. if (ltarget[X_AXIS] == current_position[X_AXIS] && ltarget[Y_AXIS] == current_position[Y_AXIS]) {
  438. planner.buffer_line_kinematic(ltarget, _feedrate_mm_s, active_extruder);
  439. return false;
  440. }
  441. // Fail if attempting move outside printable radius
  442. if (!position_is_reachable_xy(ltarget[X_AXIS], ltarget[Y_AXIS])) return true;
  443. // Get the cartesian distances moved in XYZE
  444. const float difference[XYZE] = {
  445. ltarget[X_AXIS] - current_position[X_AXIS],
  446. ltarget[Y_AXIS] - current_position[Y_AXIS],
  447. ltarget[Z_AXIS] - current_position[Z_AXIS],
  448. ltarget[E_AXIS] - current_position[E_AXIS]
  449. };
  450. // Get the linear distance in XYZ
  451. float cartesian_mm = SQRT(sq(difference[X_AXIS]) + sq(difference[Y_AXIS]) + sq(difference[Z_AXIS]));
  452. // If the move is very short, check the E move distance
  453. if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = FABS(difference[E_AXIS]);
  454. // No E move either? Game over.
  455. if (UNEAR_ZERO(cartesian_mm)) return true;
  456. // Minimum number of seconds to move the given distance
  457. const float seconds = cartesian_mm / _feedrate_mm_s;
  458. // The number of segments-per-second times the duration
  459. // gives the number of segments
  460. uint16_t segments = delta_segments_per_second * seconds;
  461. // For SCARA minimum segment size is 0.25mm
  462. #if IS_SCARA
  463. NOMORE(segments, cartesian_mm * 4);
  464. #endif
  465. // At least one segment is required
  466. NOLESS(segments, 1);
  467. // The approximate length of each segment
  468. const float inv_segments = 1.0 / float(segments),
  469. segment_distance[XYZE] = {
  470. difference[X_AXIS] * inv_segments,
  471. difference[Y_AXIS] * inv_segments,
  472. difference[Z_AXIS] * inv_segments,
  473. difference[E_AXIS] * inv_segments
  474. };
  475. // SERIAL_ECHOPAIR("mm=", cartesian_mm);
  476. // SERIAL_ECHOPAIR(" seconds=", seconds);
  477. // SERIAL_ECHOLNPAIR(" segments=", segments);
  478. #if IS_SCARA && ENABLED(SCARA_FEEDRATE_SCALING)
  479. // SCARA needs to scale the feed rate from mm/s to degrees/s
  480. const float inv_segment_length = min(10.0, float(segments) / cartesian_mm), // 1/mm/segs
  481. feed_factor = inv_segment_length * _feedrate_mm_s;
  482. float oldA = stepper.get_axis_position_degrees(A_AXIS),
  483. oldB = stepper.get_axis_position_degrees(B_AXIS);
  484. #endif
  485. // Get the logical current position as starting point
  486. float logical[XYZE];
  487. COPY(logical, current_position);
  488. // Drop one segment so the last move is to the exact target.
  489. // If there's only 1 segment, loops will be skipped entirely.
  490. --segments;
  491. // Calculate and execute the segments
  492. for (uint16_t s = segments + 1; --s;) {
  493. LOOP_XYZE(i) logical[i] += segment_distance[i];
  494. #if ENABLED(DELTA)
  495. DELTA_LOGICAL_IK(); // Delta can inline its kinematics
  496. #else
  497. inverse_kinematics(logical);
  498. #endif
  499. ADJUST_DELTA(logical); // Adjust Z if bed leveling is enabled
  500. #if IS_SCARA && ENABLED(SCARA_FEEDRATE_SCALING)
  501. // For SCARA scale the feed rate from mm/s to degrees/s
  502. // Use ratio between the length of the move and the larger angle change
  503. const float adiff = abs(delta[A_AXIS] - oldA),
  504. bdiff = abs(delta[B_AXIS] - oldB);
  505. planner.buffer_line(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], logical[E_AXIS], max(adiff, bdiff) * feed_factor, active_extruder);
  506. oldA = delta[A_AXIS];
  507. oldB = delta[B_AXIS];
  508. #else
  509. planner.buffer_line(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], logical[E_AXIS], _feedrate_mm_s, active_extruder);
  510. #endif
  511. }
  512. // Since segment_distance is only approximate,
  513. // the final move must be to the exact destination.
  514. #if IS_SCARA && ENABLED(SCARA_FEEDRATE_SCALING)
  515. // For SCARA scale the feed rate from mm/s to degrees/s
  516. // With segments > 1 length is 1 segment, otherwise total length
  517. inverse_kinematics(ltarget);
  518. ADJUST_DELTA(ltarget);
  519. const float adiff = abs(delta[A_AXIS] - oldA),
  520. bdiff = abs(delta[B_AXIS] - oldB);
  521. planner.buffer_line(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], logical[E_AXIS], max(adiff, bdiff) * feed_factor, active_extruder);
  522. #else
  523. planner.buffer_line_kinematic(ltarget, _feedrate_mm_s, active_extruder);
  524. #endif
  525. return false;
  526. }
  527. #else // !IS_KINEMATIC || UBL_DELTA
  528. /**
  529. * Prepare a linear move in a Cartesian setup.
  530. * If Mesh Bed Leveling is enabled, perform a mesh move.
  531. *
  532. * Returns true if the caller didn't update current_position.
  533. */
  534. inline bool prepare_move_to_destination_cartesian() {
  535. #if ENABLED(AUTO_BED_LEVELING_UBL)
  536. const float fr_scaled = MMS_SCALED(feedrate_mm_s);
  537. if (ubl.state.active) { // direct use of ubl.state.active for speed
  538. ubl.line_to_destination_cartesian(fr_scaled, active_extruder);
  539. return true;
  540. }
  541. else
  542. line_to_destination(fr_scaled);
  543. #else
  544. // Do not use feedrate_percentage for E or Z only moves
  545. if (current_position[X_AXIS] == destination[X_AXIS] && current_position[Y_AXIS] == destination[Y_AXIS])
  546. line_to_destination();
  547. else {
  548. const float fr_scaled = MMS_SCALED(feedrate_mm_s);
  549. #if ENABLED(MESH_BED_LEVELING)
  550. if (mbl.active()) { // direct used of mbl.active() for speed
  551. mesh_line_to_destination(fr_scaled);
  552. return true;
  553. }
  554. else
  555. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  556. if (planner.abl_enabled) { // direct use of abl_enabled for speed
  557. bilinear_line_to_destination(fr_scaled);
  558. return true;
  559. }
  560. else
  561. #endif
  562. line_to_destination(fr_scaled);
  563. }
  564. #endif
  565. return false;
  566. }
  567. #endif // !IS_KINEMATIC || UBL_DELTA
  568. #if ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE)
  569. bool extruder_duplication_enabled = false; // Used in Dual X mode 2
  570. #endif
  571. #if ENABLED(DUAL_X_CARRIAGE)
  572. DualXMode dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  573. float inactive_extruder_x_pos = X2_MAX_POS, // used in mode 0 & 1
  574. raised_parked_position[XYZE], // used in mode 1
  575. duplicate_extruder_x_offset = DEFAULT_DUPLICATION_X_OFFSET; // used in mode 2
  576. bool active_extruder_parked = false; // used in mode 1 & 2
  577. millis_t delayed_move_time = 0; // used in mode 1
  578. int16_t duplicate_extruder_temp_offset = 0; // used in mode 2
  579. float x_home_pos(const int extruder) {
  580. if (extruder == 0)
  581. return LOGICAL_X_POSITION(base_home_pos(X_AXIS));
  582. else
  583. /**
  584. * In dual carriage mode the extruder offset provides an override of the
  585. * second X-carriage position when homed - otherwise X2_HOME_POS is used.
  586. * This allows soft recalibration of the second extruder home position
  587. * without firmware reflash (through the M218 command).
  588. */
  589. return LOGICAL_X_POSITION(hotend_offset[X_AXIS][1] > 0 ? hotend_offset[X_AXIS][1] : X2_HOME_POS);
  590. }
  591. /**
  592. * Prepare a linear move in a dual X axis setup
  593. */
  594. inline bool prepare_move_to_destination_dualx() {
  595. if (active_extruder_parked) {
  596. switch (dual_x_carriage_mode) {
  597. case DXC_FULL_CONTROL_MODE:
  598. break;
  599. case DXC_AUTO_PARK_MODE:
  600. if (current_position[E_AXIS] == destination[E_AXIS]) {
  601. // This is a travel move (with no extrusion)
  602. // Skip it, but keep track of the current position
  603. // (so it can be used as the start of the next non-travel move)
  604. if (delayed_move_time != 0xFFFFFFFFUL) {
  605. set_current_to_destination();
  606. NOLESS(raised_parked_position[Z_AXIS], destination[Z_AXIS]);
  607. delayed_move_time = millis();
  608. return true;
  609. }
  610. }
  611. // unpark extruder: 1) raise, 2) move into starting XY position, 3) lower
  612. for (uint8_t i = 0; i < 3; i++)
  613. planner.buffer_line(
  614. i == 0 ? raised_parked_position[X_AXIS] : current_position[X_AXIS],
  615. i == 0 ? raised_parked_position[Y_AXIS] : current_position[Y_AXIS],
  616. i == 2 ? current_position[Z_AXIS] : raised_parked_position[Z_AXIS],
  617. current_position[E_AXIS],
  618. i == 1 ? PLANNER_XY_FEEDRATE() : planner.max_feedrate_mm_s[Z_AXIS],
  619. active_extruder
  620. );
  621. delayed_move_time = 0;
  622. active_extruder_parked = false;
  623. #if ENABLED(DEBUG_LEVELING_FEATURE)
  624. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Clear active_extruder_parked");
  625. #endif
  626. break;
  627. case DXC_DUPLICATION_MODE:
  628. if (active_extruder == 0) {
  629. #if ENABLED(DEBUG_LEVELING_FEATURE)
  630. if (DEBUGGING(LEVELING)) {
  631. SERIAL_ECHOPAIR("Set planner X", LOGICAL_X_POSITION(inactive_extruder_x_pos));
  632. SERIAL_ECHOLNPAIR(" ... Line to X", current_position[X_AXIS] + duplicate_extruder_x_offset);
  633. }
  634. #endif
  635. // move duplicate extruder into correct duplication position.
  636. planner.set_position_mm(
  637. LOGICAL_X_POSITION(inactive_extruder_x_pos),
  638. current_position[Y_AXIS],
  639. current_position[Z_AXIS],
  640. current_position[E_AXIS]
  641. );
  642. planner.buffer_line(
  643. current_position[X_AXIS] + duplicate_extruder_x_offset,
  644. current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS],
  645. planner.max_feedrate_mm_s[X_AXIS], 1
  646. );
  647. SYNC_PLAN_POSITION_KINEMATIC();
  648. stepper.synchronize();
  649. extruder_duplication_enabled = true;
  650. active_extruder_parked = false;
  651. #if ENABLED(DEBUG_LEVELING_FEATURE)
  652. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Set extruder_duplication_enabled\nClear active_extruder_parked");
  653. #endif
  654. }
  655. else {
  656. #if ENABLED(DEBUG_LEVELING_FEATURE)
  657. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Active extruder not 0");
  658. #endif
  659. }
  660. break;
  661. }
  662. }
  663. return false;
  664. }
  665. #endif // DUAL_X_CARRIAGE
  666. /**
  667. * Prepare a single move and get ready for the next one
  668. *
  669. * This may result in several calls to planner.buffer_line to
  670. * do smaller moves for DELTA, SCARA, mesh moves, etc.
  671. */
  672. void prepare_move_to_destination() {
  673. clamp_to_software_endstops(destination);
  674. gcode.refresh_cmd_timeout();
  675. #if ENABLED(PREVENT_COLD_EXTRUSION)
  676. if (!DEBUGGING(DRYRUN)) {
  677. if (destination[E_AXIS] != current_position[E_AXIS]) {
  678. if (thermalManager.tooColdToExtrude(active_extruder)) {
  679. current_position[E_AXIS] = destination[E_AXIS]; // Behave as if the move really took place, but ignore E part
  680. SERIAL_ECHO_START();
  681. SERIAL_ECHOLNPGM(MSG_ERR_COLD_EXTRUDE_STOP);
  682. }
  683. #if ENABLED(PREVENT_LENGTHY_EXTRUDE)
  684. if (destination[E_AXIS] - current_position[E_AXIS] > EXTRUDE_MAXLENGTH) {
  685. current_position[E_AXIS] = destination[E_AXIS]; // Behave as if the move really took place, but ignore E part
  686. SERIAL_ECHO_START();
  687. SERIAL_ECHOLNPGM(MSG_ERR_LONG_EXTRUDE_STOP);
  688. }
  689. #endif
  690. }
  691. }
  692. #endif
  693. if (
  694. #if UBL_DELTA // Also works for CARTESIAN (smaller segments follow mesh more closely)
  695. ubl.prepare_segmented_line_to(destination, feedrate_mm_s)
  696. #elif IS_KINEMATIC
  697. prepare_kinematic_move_to(destination)
  698. #elif ENABLED(DUAL_X_CARRIAGE)
  699. prepare_move_to_destination_dualx() || prepare_move_to_destination_cartesian()
  700. #else
  701. prepare_move_to_destination_cartesian()
  702. #endif
  703. ) return;
  704. set_current_to_destination();
  705. }
  706. #if NEED_UNHOMED_ERR
  707. bool axis_unhomed_error(const bool x/*=true*/, const bool y/*=true*/, const bool z/*=true*/) {
  708. #if ENABLED(HOME_AFTER_DEACTIVATE)
  709. const bool xx = x && !axis_known_position[X_AXIS],
  710. yy = y && !axis_known_position[Y_AXIS],
  711. zz = z && !axis_known_position[Z_AXIS];
  712. #else
  713. const bool xx = x && !axis_homed[X_AXIS],
  714. yy = y && !axis_homed[Y_AXIS],
  715. zz = z && !axis_homed[Z_AXIS];
  716. #endif
  717. if (xx || yy || zz) {
  718. SERIAL_ECHO_START();
  719. SERIAL_ECHOPGM(MSG_HOME " ");
  720. if (xx) SERIAL_ECHOPGM(MSG_X);
  721. if (yy) SERIAL_ECHOPGM(MSG_Y);
  722. if (zz) SERIAL_ECHOPGM(MSG_Z);
  723. SERIAL_ECHOLNPGM(" " MSG_FIRST);
  724. #if ENABLED(ULTRA_LCD)
  725. lcd_status_printf_P(0, PSTR(MSG_HOME " %s%s%s " MSG_FIRST), xx ? MSG_X : "", yy ? MSG_Y : "", zz ? MSG_Z : "");
  726. #endif
  727. return true;
  728. }
  729. return false;
  730. }
  731. #endif
  732. /**
  733. * The homing feedrate may vary
  734. */
  735. inline float get_homing_bump_feedrate(const AxisEnum axis) {
  736. static const uint8_t homing_bump_divisor[] PROGMEM = HOMING_BUMP_DIVISOR;
  737. uint8_t hbd = pgm_read_byte(&homing_bump_divisor[axis]);
  738. if (hbd < 1) {
  739. hbd = 10;
  740. SERIAL_ECHO_START();
  741. SERIAL_ECHOLNPGM("Warning: Homing Bump Divisor < 1");
  742. }
  743. return homing_feedrate(axis) / hbd;
  744. }
  745. /**
  746. * Home an individual linear axis
  747. */
  748. static void do_homing_move(const AxisEnum axis, const float distance, const float fr_mm_s=0.0) {
  749. #if ENABLED(DEBUG_LEVELING_FEATURE)
  750. if (DEBUGGING(LEVELING)) {
  751. SERIAL_ECHOPAIR(">>> do_homing_move(", axis_codes[axis]);
  752. SERIAL_ECHOPAIR(", ", distance);
  753. SERIAL_ECHOPAIR(", ", fr_mm_s);
  754. SERIAL_CHAR(')');
  755. SERIAL_EOL();
  756. }
  757. #endif
  758. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH)
  759. const bool deploy_bltouch = (axis == Z_AXIS && distance < 0);
  760. if (deploy_bltouch) set_bltouch_deployed(true);
  761. #endif
  762. #if QUIET_PROBING
  763. if (axis == Z_AXIS) probing_pause(true);
  764. #endif
  765. // Tell the planner we're at Z=0
  766. current_position[axis] = 0;
  767. #if IS_SCARA
  768. SYNC_PLAN_POSITION_KINEMATIC();
  769. current_position[axis] = distance;
  770. inverse_kinematics(current_position);
  771. 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);
  772. #else
  773. sync_plan_position();
  774. current_position[axis] = distance;
  775. 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);
  776. #endif
  777. stepper.synchronize();
  778. #if QUIET_PROBING
  779. if (axis == Z_AXIS) probing_pause(false);
  780. #endif
  781. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH)
  782. if (deploy_bltouch) set_bltouch_deployed(false);
  783. #endif
  784. endstops.hit_on_purpose();
  785. #if ENABLED(DEBUG_LEVELING_FEATURE)
  786. if (DEBUGGING(LEVELING)) {
  787. SERIAL_ECHOPAIR("<<< do_homing_move(", axis_codes[axis]);
  788. SERIAL_CHAR(')');
  789. SERIAL_EOL();
  790. }
  791. #endif
  792. }
  793. /**
  794. * Set an axis' current position to its home position (after homing).
  795. *
  796. * For Core and Cartesian robots this applies one-to-one when an
  797. * individual axis has been homed.
  798. *
  799. * DELTA should wait until all homing is done before setting the XYZ
  800. * current_position to home, because homing is a single operation.
  801. * In the case where the axis positions are already known and previously
  802. * homed, DELTA could home to X or Y individually by moving either one
  803. * to the center. However, homing Z always homes XY and Z.
  804. *
  805. * SCARA should wait until all XY homing is done before setting the XY
  806. * current_position to home, because neither X nor Y is at home until
  807. * both are at home. Z can however be homed individually.
  808. *
  809. * Callers must sync the planner position after calling this!
  810. */
  811. void set_axis_is_at_home(const AxisEnum axis) {
  812. #if ENABLED(DEBUG_LEVELING_FEATURE)
  813. if (DEBUGGING(LEVELING)) {
  814. SERIAL_ECHOPAIR(">>> set_axis_is_at_home(", axis_codes[axis]);
  815. SERIAL_CHAR(')');
  816. SERIAL_EOL();
  817. }
  818. #endif
  819. axis_known_position[axis] = axis_homed[axis] = true;
  820. #if HAS_POSITION_SHIFT
  821. position_shift[axis] = 0;
  822. update_software_endstops(axis);
  823. #endif
  824. #if ENABLED(DUAL_X_CARRIAGE)
  825. if (axis == X_AXIS && (active_extruder == 1 || dual_x_carriage_mode == DXC_DUPLICATION_MODE)) {
  826. current_position[X_AXIS] = x_home_pos(active_extruder);
  827. return;
  828. }
  829. #endif
  830. #if ENABLED(MORGAN_SCARA)
  831. scara_set_axis_is_at_home(axis);
  832. #else
  833. current_position[axis] = LOGICAL_POSITION(base_home_pos(axis), axis);
  834. #endif
  835. /**
  836. * Z Probe Z Homing? Account for the probe's Z offset.
  837. */
  838. #if HAS_BED_PROBE && Z_HOME_DIR < 0
  839. if (axis == Z_AXIS) {
  840. #if HOMING_Z_WITH_PROBE
  841. current_position[Z_AXIS] -= zprobe_zoffset;
  842. #if ENABLED(DEBUG_LEVELING_FEATURE)
  843. if (DEBUGGING(LEVELING)) {
  844. SERIAL_ECHOLNPGM("*** Z HOMED WITH PROBE (Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) ***");
  845. SERIAL_ECHOLNPAIR("> zprobe_zoffset = ", zprobe_zoffset);
  846. }
  847. #endif
  848. #elif ENABLED(DEBUG_LEVELING_FEATURE)
  849. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("*** Z HOMED TO ENDSTOP (Z_MIN_PROBE_ENDSTOP) ***");
  850. #endif
  851. }
  852. #endif
  853. #if ENABLED(DEBUG_LEVELING_FEATURE)
  854. if (DEBUGGING(LEVELING)) {
  855. #if HAS_HOME_OFFSET
  856. SERIAL_ECHOPAIR("> home_offset[", axis_codes[axis]);
  857. SERIAL_ECHOLNPAIR("] = ", home_offset[axis]);
  858. #endif
  859. DEBUG_POS("", current_position);
  860. SERIAL_ECHOPAIR("<<< set_axis_is_at_home(", axis_codes[axis]);
  861. SERIAL_CHAR(')');
  862. SERIAL_EOL();
  863. }
  864. #endif
  865. #if ENABLED(I2C_POSITION_ENCODERS)
  866. I2CPEM.homed(axis);
  867. #endif
  868. }
  869. /**
  870. * Home an individual "raw axis" to its endstop.
  871. * This applies to XYZ on Cartesian and Core robots, and
  872. * to the individual ABC steppers on DELTA and SCARA.
  873. *
  874. * At the end of the procedure the axis is marked as
  875. * homed and the current position of that axis is updated.
  876. * Kinematic robots should wait till all axes are homed
  877. * before updating the current position.
  878. */
  879. void homeaxis(const AxisEnum axis) {
  880. #if IS_SCARA
  881. // Only Z homing (with probe) is permitted
  882. if (axis != Z_AXIS) { BUZZ(100, 880); return; }
  883. #else
  884. #define CAN_HOME(A) \
  885. (axis == A##_AXIS && ((A##_MIN_PIN > -1 && A##_HOME_DIR < 0) || (A##_MAX_PIN > -1 && A##_HOME_DIR > 0)))
  886. if (!CAN_HOME(X) && !CAN_HOME(Y) && !CAN_HOME(Z)) return;
  887. #endif
  888. #if ENABLED(DEBUG_LEVELING_FEATURE)
  889. if (DEBUGGING(LEVELING)) {
  890. SERIAL_ECHOPAIR(">>> homeaxis(", axis_codes[axis]);
  891. SERIAL_CHAR(')');
  892. SERIAL_EOL();
  893. }
  894. #endif
  895. const int axis_home_dir =
  896. #if ENABLED(DUAL_X_CARRIAGE)
  897. (axis == X_AXIS) ? x_home_dir(active_extruder) :
  898. #endif
  899. home_dir(axis);
  900. // Homing Z towards the bed? Deploy the Z probe or endstop.
  901. #if HOMING_Z_WITH_PROBE
  902. if (axis == Z_AXIS && DEPLOY_PROBE()) return;
  903. #endif
  904. // Set a flag for Z motor locking
  905. #if ENABLED(Z_DUAL_ENDSTOPS)
  906. if (axis == Z_AXIS) stepper.set_homing_flag(true);
  907. #endif
  908. // Disable stealthChop if used. Enable diag1 pin on driver.
  909. #if ENABLED(SENSORLESS_HOMING)
  910. #if ENABLED(X_IS_TMC2130)
  911. if (axis == X_AXIS) tmc2130_sensorless_homing(stepperX);
  912. #endif
  913. #if ENABLED(Y_IS_TMC2130)
  914. if (axis == Y_AXIS) tmc2130_sensorless_homing(stepperY);
  915. #endif
  916. #endif
  917. // Fast move towards endstop until triggered
  918. #if ENABLED(DEBUG_LEVELING_FEATURE)
  919. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Home 1 Fast:");
  920. #endif
  921. do_homing_move(axis, 1.5 * max_length(axis) * axis_home_dir);
  922. // When homing Z with probe respect probe clearance
  923. const float bump = axis_home_dir * (
  924. #if HOMING_Z_WITH_PROBE
  925. (axis == Z_AXIS) ? max(Z_CLEARANCE_BETWEEN_PROBES, home_bump_mm(Z_AXIS)) :
  926. #endif
  927. home_bump_mm(axis)
  928. );
  929. // If a second homing move is configured...
  930. if (bump) {
  931. // Move away from the endstop by the axis HOME_BUMP_MM
  932. #if ENABLED(DEBUG_LEVELING_FEATURE)
  933. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Move Away:");
  934. #endif
  935. do_homing_move(axis, -bump);
  936. // Slow move towards endstop until triggered
  937. #if ENABLED(DEBUG_LEVELING_FEATURE)
  938. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Home 2 Slow:");
  939. #endif
  940. do_homing_move(axis, 2 * bump, get_homing_bump_feedrate(axis));
  941. }
  942. #if ENABLED(Z_DUAL_ENDSTOPS)
  943. if (axis == Z_AXIS) {
  944. float adj = FABS(endstops.z_endstop_adj);
  945. bool lockZ1;
  946. if (axis_home_dir > 0) {
  947. adj = -adj;
  948. lockZ1 = (endstops.z_endstop_adj > 0);
  949. }
  950. else
  951. lockZ1 = (endstops.z_endstop_adj < 0);
  952. if (lockZ1) stepper.set_z_lock(true); else stepper.set_z2_lock(true);
  953. // Move to the adjusted endstop height
  954. do_homing_move(axis, adj);
  955. if (lockZ1) stepper.set_z_lock(false); else stepper.set_z2_lock(false);
  956. stepper.set_homing_flag(false);
  957. } // Z_AXIS
  958. #endif
  959. #if IS_SCARA
  960. set_axis_is_at_home(axis);
  961. SYNC_PLAN_POSITION_KINEMATIC();
  962. #elif ENABLED(DELTA)
  963. // Delta has already moved all three towers up in G28
  964. // so here it re-homes each tower in turn.
  965. // Delta homing treats the axes as normal linear axes.
  966. // retrace by the amount specified in delta_endstop_adj + additional 0.1mm in order to have minimum steps
  967. if (delta_endstop_adj[axis] * Z_HOME_DIR <= 0) {
  968. #if ENABLED(DEBUG_LEVELING_FEATURE)
  969. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("delta_endstop_adj:");
  970. #endif
  971. do_homing_move(axis, delta_endstop_adj[axis] - 0.1 * Z_HOME_DIR);
  972. }
  973. #else
  974. // For cartesian/core machines,
  975. // set the axis to its home position
  976. set_axis_is_at_home(axis);
  977. sync_plan_position();
  978. destination[axis] = current_position[axis];
  979. #if ENABLED(DEBUG_LEVELING_FEATURE)
  980. if (DEBUGGING(LEVELING)) DEBUG_POS("> AFTER set_axis_is_at_home", current_position);
  981. #endif
  982. #endif
  983. // Re-enable stealthChop if used. Disable diag1 pin on driver.
  984. #if ENABLED(SENSORLESS_HOMING)
  985. #if ENABLED(X_IS_TMC2130)
  986. if (axis == X_AXIS) tmc2130_sensorless_homing(stepperX, false);
  987. #endif
  988. #if ENABLED(Y_IS_TMC2130)
  989. if (axis == Y_AXIS) tmc2130_sensorless_homing(stepperY, false);
  990. #endif
  991. #endif
  992. // Put away the Z probe
  993. #if HOMING_Z_WITH_PROBE
  994. if (axis == Z_AXIS && STOW_PROBE()) return;
  995. #endif
  996. #if ENABLED(DEBUG_LEVELING_FEATURE)
  997. if (DEBUGGING(LEVELING)) {
  998. SERIAL_ECHOPAIR("<<< homeaxis(", axis_codes[axis]);
  999. SERIAL_CHAR(')');
  1000. SERIAL_EOL();
  1001. }
  1002. #endif
  1003. } // homeaxis()
  1004. #if HAS_WORKSPACE_OFFSET || ENABLED(DUAL_X_CARRIAGE)
  1005. /**
  1006. * Software endstops can be used to monitor the open end of
  1007. * an axis that has a hardware endstop on the other end. Or
  1008. * they can prevent axes from moving past endstops and grinding.
  1009. *
  1010. * To keep doing their job as the coordinate system changes,
  1011. * the software endstop positions must be refreshed to remain
  1012. * at the same positions relative to the machine.
  1013. */
  1014. void update_software_endstops(const AxisEnum axis) {
  1015. const float offs = 0.0
  1016. #if HAS_HOME_OFFSET
  1017. + home_offset[axis]
  1018. #endif
  1019. #if HAS_POSITION_SHIFT
  1020. + position_shift[axis]
  1021. #endif
  1022. ;
  1023. #if HAS_HOME_OFFSET && HAS_POSITION_SHIFT
  1024. workspace_offset[axis] = offs;
  1025. #endif
  1026. #if ENABLED(DUAL_X_CARRIAGE)
  1027. if (axis == X_AXIS) {
  1028. // In Dual X mode hotend_offset[X] is T1's home position
  1029. float dual_max_x = max(hotend_offset[X_AXIS][1], X2_MAX_POS);
  1030. if (active_extruder != 0) {
  1031. // T1 can move from X2_MIN_POS to X2_MAX_POS or X2 home position (whichever is larger)
  1032. soft_endstop_min[X_AXIS] = X2_MIN_POS + offs;
  1033. soft_endstop_max[X_AXIS] = dual_max_x + offs;
  1034. }
  1035. else if (dual_x_carriage_mode == DXC_DUPLICATION_MODE) {
  1036. // In Duplication Mode, T0 can move as far left as X_MIN_POS
  1037. // but not so far to the right that T1 would move past the end
  1038. soft_endstop_min[X_AXIS] = base_min_pos(X_AXIS) + offs;
  1039. soft_endstop_max[X_AXIS] = min(base_max_pos(X_AXIS), dual_max_x - duplicate_extruder_x_offset) + offs;
  1040. }
  1041. else {
  1042. // In other modes, T0 can move from X_MIN_POS to X_MAX_POS
  1043. soft_endstop_min[axis] = base_min_pos(axis) + offs;
  1044. soft_endstop_max[axis] = base_max_pos(axis) + offs;
  1045. }
  1046. }
  1047. #elif ENABLED(DELTA)
  1048. soft_endstop_min[axis] = base_min_pos(axis) + (axis == Z_AXIS ? 0 : offs);
  1049. soft_endstop_max[axis] = base_max_pos(axis) + offs;
  1050. #else
  1051. soft_endstop_min[axis] = base_min_pos(axis) + offs;
  1052. soft_endstop_max[axis] = base_max_pos(axis) + offs;
  1053. #endif
  1054. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1055. if (DEBUGGING(LEVELING)) {
  1056. SERIAL_ECHOPAIR("For ", axis_codes[axis]);
  1057. #if HAS_HOME_OFFSET
  1058. SERIAL_ECHOPAIR(" axis:\n home_offset = ", home_offset[axis]);
  1059. #endif
  1060. #if HAS_POSITION_SHIFT
  1061. SERIAL_ECHOPAIR("\n position_shift = ", position_shift[axis]);
  1062. #endif
  1063. SERIAL_ECHOPAIR("\n soft_endstop_min = ", soft_endstop_min[axis]);
  1064. SERIAL_ECHOLNPAIR("\n soft_endstop_max = ", soft_endstop_max[axis]);
  1065. }
  1066. #endif
  1067. #if ENABLED(DELTA)
  1068. if (axis == Z_AXIS)
  1069. delta_clip_start_height = soft_endstop_max[axis] - delta_safe_distance_from_top();
  1070. #endif
  1071. }
  1072. #endif // HAS_WORKSPACE_OFFSET || DUAL_X_CARRIAGE
  1073. #if HAS_M206_COMMAND
  1074. /**
  1075. * Change the home offset for an axis, update the current
  1076. * position and the software endstops to retain the same
  1077. * relative distance to the new home.
  1078. *
  1079. * Since this changes the current_position, code should
  1080. * call sync_plan_position soon after this.
  1081. */
  1082. void set_home_offset(const AxisEnum axis, const float v) {
  1083. current_position[axis] += v - home_offset[axis];
  1084. home_offset[axis] = v;
  1085. update_software_endstops(axis);
  1086. }
  1087. #endif // HAS_M206_COMMAND