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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 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 ENABLED(BLTOUCH)
  43. #include "../feature/bltouch.h"
  44. #endif
  45. #if HAS_DISPLAY
  46. #include "../lcd/ultralcd.h"
  47. #endif
  48. #if ENABLED(SENSORLESS_HOMING)
  49. #include "../feature/tmc_util.h"
  50. #endif
  51. #if ENABLED(FWRETRACT)
  52. #include "../feature/fwretract.h"
  53. #endif
  54. #if ENABLED(BABYSTEP_DISPLAY_TOTAL)
  55. #include "../feature/babystep.h"
  56. #endif
  57. #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
  58. #include "../core/debug_out.h"
  59. #define XYZ_CONSTS(T, NAME, OPT) const PROGMEM XYZval<T> NAME##_P = { X_##OPT, Y_##OPT, Z_##OPT }
  60. XYZ_CONSTS(float, base_min_pos, MIN_POS);
  61. XYZ_CONSTS(float, base_max_pos, MAX_POS);
  62. XYZ_CONSTS(float, base_home_pos, HOME_POS);
  63. XYZ_CONSTS(float, max_length, MAX_LENGTH);
  64. XYZ_CONSTS(float, home_bump_mm, HOME_BUMP_MM);
  65. XYZ_CONSTS(signed char, home_dir, HOME_DIR);
  66. /**
  67. * axis_homed
  68. * Flags that each linear axis was homed.
  69. * XYZ on cartesian, ABC on delta, ABZ on SCARA.
  70. *
  71. * axis_known_position
  72. * Flags that the position is known in each linear axis. Set when homed.
  73. * Cleared whenever a stepper powers off, potentially losing its position.
  74. */
  75. uint8_t axis_homed, axis_known_position; // = 0
  76. // Relative Mode. Enable with G91, disable with G90.
  77. bool relative_mode; // = false;
  78. /**
  79. * Cartesian Current Position
  80. * Used to track the native machine position as moves are queued.
  81. * Used by 'line_to_current_position' to do a move after changing it.
  82. * Used by 'sync_plan_position' to update 'planner.position'.
  83. */
  84. xyze_pos_t current_position = { X_HOME_POS, Y_HOME_POS, Z_HOME_POS };
  85. /**
  86. * Cartesian Destination
  87. * The destination for a move, filled in by G-code movement commands,
  88. * and expected by functions like 'prepare_move_to_destination'.
  89. * G-codes can set destination using 'get_destination_from_command'
  90. */
  91. xyze_pos_t destination; // {0}
  92. // The active extruder (tool). Set with T<extruder> command.
  93. #if EXTRUDERS > 1
  94. uint8_t active_extruder; // = 0
  95. #endif
  96. #if ENABLED(LCD_SHOW_E_TOTAL)
  97. float e_move_accumulator; // = 0
  98. #endif
  99. // Extruder offsets
  100. #if HAS_HOTEND_OFFSET
  101. xyz_pos_t hotend_offset[HOTENDS]; // Initialized by settings.load()
  102. void reset_hotend_offsets() {
  103. constexpr float tmp[XYZ][HOTENDS] = { HOTEND_OFFSET_X, HOTEND_OFFSET_Y, HOTEND_OFFSET_Z };
  104. static_assert(
  105. !tmp[X_AXIS][0] && !tmp[Y_AXIS][0] && !tmp[Z_AXIS][0],
  106. "Offsets for the first hotend must be 0.0."
  107. );
  108. // Transpose from [XYZ][HOTENDS] to [HOTENDS][XYZ]
  109. HOTEND_LOOP() LOOP_XYZ(a) hotend_offset[e][a] = tmp[a][e];
  110. #if ENABLED(DUAL_X_CARRIAGE)
  111. hotend_offset[1].x = _MAX(X2_HOME_POS, X2_MAX_POS);
  112. #endif
  113. }
  114. #endif
  115. // The feedrate for the current move, often used as the default if
  116. // no other feedrate is specified. Overridden for special moves.
  117. // Set by the last G0 through G5 command's "F" parameter.
  118. // Functions that override this for custom moves *must always* restore it!
  119. feedRate_t feedrate_mm_s = MMM_TO_MMS(1500);
  120. int16_t feedrate_percentage = 100;
  121. // Homing feedrate is const progmem - compare to constexpr in the header
  122. const feedRate_t homing_feedrate_mm_s[XYZ] PROGMEM = {
  123. #if ENABLED(DELTA)
  124. MMM_TO_MMS(HOMING_FEEDRATE_Z), MMM_TO_MMS(HOMING_FEEDRATE_Z),
  125. #else
  126. MMM_TO_MMS(HOMING_FEEDRATE_XY), MMM_TO_MMS(HOMING_FEEDRATE_XY),
  127. #endif
  128. MMM_TO_MMS(HOMING_FEEDRATE_Z)
  129. };
  130. // Cartesian conversion result goes here:
  131. xyz_pos_t cartes;
  132. #if IS_KINEMATIC
  133. abc_pos_t delta;
  134. #if HAS_SCARA_OFFSET
  135. abc_pos_t scara_home_offset;
  136. #endif
  137. #if HAS_SOFTWARE_ENDSTOPS
  138. float delta_max_radius, delta_max_radius_2;
  139. #elif IS_SCARA
  140. constexpr float delta_max_radius = SCARA_PRINTABLE_RADIUS,
  141. delta_max_radius_2 = sq(SCARA_PRINTABLE_RADIUS);
  142. #else // DELTA
  143. constexpr float delta_max_radius = DELTA_PRINTABLE_RADIUS,
  144. delta_max_radius_2 = sq(DELTA_PRINTABLE_RADIUS);
  145. #endif
  146. #endif
  147. /**
  148. * The workspace can be offset by some commands, or
  149. * these offsets may be omitted to save on computation.
  150. */
  151. #if HAS_POSITION_SHIFT
  152. // The distance that XYZ has been offset by G92. Reset by G28.
  153. xyz_pos_t position_shift{0};
  154. #endif
  155. #if HAS_HOME_OFFSET
  156. // This offset is added to the configured home position.
  157. // Set by M206, M428, or menu item. Saved to EEPROM.
  158. xyz_pos_t home_offset{0};
  159. #endif
  160. #if HAS_HOME_OFFSET && HAS_POSITION_SHIFT
  161. // The above two are combined to save on computes
  162. xyz_pos_t workspace_offset{0};
  163. #endif
  164. #if HAS_ABL_NOT_UBL
  165. float xy_probe_feedrate_mm_s = MMM_TO_MMS(XY_PROBE_SPEED);
  166. #endif
  167. /**
  168. * Output the current position to serial
  169. */
  170. void report_current_position() {
  171. const xyz_pos_t lpos = current_position.asLogical();
  172. SERIAL_ECHOPAIR("X:", lpos.x, " Y:", lpos.y, " Z:", lpos.z, " E:", current_position.e);
  173. stepper.report_positions();
  174. #if IS_SCARA
  175. scara_report_positions();
  176. #endif
  177. }
  178. /**
  179. * sync_plan_position
  180. *
  181. * Set the planner/stepper positions directly from current_position with
  182. * no kinematic translation. Used for homing axes and cartesian/core syncing.
  183. */
  184. void sync_plan_position() {
  185. if (DEBUGGING(LEVELING)) DEBUG_POS("sync_plan_position", current_position);
  186. planner.set_position_mm(current_position);
  187. }
  188. void sync_plan_position_e() { planner.set_e_position_mm(current_position.e); }
  189. /**
  190. * Get the stepper positions in the cartes[] array.
  191. * Forward kinematics are applied for DELTA and SCARA.
  192. *
  193. * The result is in the current coordinate space with
  194. * leveling applied. The coordinates need to be run through
  195. * unapply_leveling to obtain the "ideal" coordinates
  196. * suitable for current_position, etc.
  197. */
  198. void get_cartesian_from_steppers() {
  199. #if ENABLED(DELTA)
  200. forward_kinematics_DELTA(
  201. planner.get_axis_position_mm(A_AXIS),
  202. planner.get_axis_position_mm(B_AXIS),
  203. planner.get_axis_position_mm(C_AXIS)
  204. );
  205. #else
  206. #if IS_SCARA
  207. forward_kinematics_SCARA(
  208. planner.get_axis_position_degrees(A_AXIS),
  209. planner.get_axis_position_degrees(B_AXIS)
  210. );
  211. #else
  212. cartes.set(planner.get_axis_position_mm(X_AXIS), planner.get_axis_position_mm(Y_AXIS));
  213. #endif
  214. cartes.z = planner.get_axis_position_mm(Z_AXIS);
  215. #endif
  216. }
  217. /**
  218. * Set the current_position for an axis based on
  219. * the stepper positions, removing any leveling that
  220. * may have been applied.
  221. *
  222. * To prevent small shifts in axis position always call
  223. * sync_plan_position after updating axes with this.
  224. *
  225. * To keep hosts in sync, always call report_current_position
  226. * after updating the current_position.
  227. */
  228. void set_current_from_steppers_for_axis(const AxisEnum axis) {
  229. get_cartesian_from_steppers();
  230. #if HAS_POSITION_MODIFIERS
  231. xyze_pos_t pos = { cartes.x, cartes.y, cartes.z, current_position.e };
  232. planner.unapply_modifiers(pos
  233. #if HAS_LEVELING
  234. , true
  235. #endif
  236. );
  237. xyze_pos_t &cartes = pos;
  238. #endif
  239. if (axis == ALL_AXES)
  240. current_position = cartes;
  241. else
  242. current_position[axis] = cartes[axis];
  243. }
  244. /**
  245. * Move the planner to the current position from wherever it last moved
  246. * (or from wherever it has been told it is located).
  247. */
  248. void line_to_current_position(const feedRate_t &fr_mm_s/*=feedrate_mm_s*/) {
  249. planner.buffer_line(current_position, fr_mm_s, active_extruder);
  250. }
  251. #if IS_KINEMATIC
  252. /**
  253. * Buffer a fast move without interpolation. Set current_position to destination
  254. */
  255. void prepare_fast_move_to_destination(const feedRate_t &scaled_fr_mm_s/*=MMS_SCALED(feedrate_mm_s)*/) {
  256. if (DEBUGGING(LEVELING)) DEBUG_POS("prepare_fast_move_to_destination", destination);
  257. #if UBL_SEGMENTED
  258. // UBL segmented line will do Z-only moves in single segment
  259. ubl.line_to_destination_segmented(scaled_fr_mm_s);
  260. #else
  261. if (current_position == destination) return;
  262. planner.buffer_line(destination, scaled_fr_mm_s, active_extruder);
  263. #endif
  264. current_position = destination;
  265. }
  266. #endif // IS_KINEMATIC
  267. void _internal_move_to_destination(const feedRate_t &fr_mm_s/*=0.0f*/
  268. #if IS_KINEMATIC
  269. , const bool is_fast/*=false*/
  270. #endif
  271. ) {
  272. const feedRate_t old_feedrate = feedrate_mm_s;
  273. if (fr_mm_s) feedrate_mm_s = fr_mm_s;
  274. const uint16_t old_pct = feedrate_percentage;
  275. feedrate_percentage = 100;
  276. #if EXTRUDERS
  277. const float old_fac = planner.e_factor[active_extruder];
  278. planner.e_factor[active_extruder] = 1.0f;
  279. #endif
  280. #if IS_KINEMATIC
  281. if (is_fast)
  282. prepare_fast_move_to_destination();
  283. else
  284. #endif
  285. prepare_move_to_destination();
  286. feedrate_mm_s = old_feedrate;
  287. feedrate_percentage = old_pct;
  288. #if EXTRUDERS
  289. planner.e_factor[active_extruder] = old_fac;
  290. #endif
  291. }
  292. /**
  293. * Plan a move to (X, Y, Z) and set the current_position
  294. */
  295. void do_blocking_move_to(const float rx, const float ry, const float rz, const feedRate_t &fr_mm_s/*=0.0*/) {
  296. if (DEBUGGING(LEVELING)) DEBUG_XYZ(">>> do_blocking_move_to", rx, ry, rz);
  297. const feedRate_t z_feedrate = fr_mm_s ?: homing_feedrate(Z_AXIS),
  298. xy_feedrate = fr_mm_s ?: feedRate_t(XY_PROBE_FEEDRATE_MM_S);
  299. #if ENABLED(DELTA)
  300. if (!position_is_reachable(rx, ry)) return;
  301. REMEMBER(fr, feedrate_mm_s, xy_feedrate);
  302. destination = current_position; // sync destination at the start
  303. if (DEBUGGING(LEVELING)) DEBUG_POS("destination = current_position", destination);
  304. // when in the danger zone
  305. if (current_position.z > delta_clip_start_height) {
  306. if (rz > delta_clip_start_height) { // staying in the danger zone
  307. destination.set(rx, ry, rz); // move directly (uninterpolated)
  308. prepare_internal_fast_move_to_destination(); // set current_position from destination
  309. if (DEBUGGING(LEVELING)) DEBUG_POS("danger zone move", current_position);
  310. return;
  311. }
  312. destination.z = delta_clip_start_height;
  313. prepare_internal_fast_move_to_destination(); // set current_position from destination
  314. if (DEBUGGING(LEVELING)) DEBUG_POS("zone border move", current_position);
  315. }
  316. if (rz > current_position.z) { // raising?
  317. destination.z = rz;
  318. prepare_internal_fast_move_to_destination(z_feedrate); // set current_position from destination
  319. if (DEBUGGING(LEVELING)) DEBUG_POS("z raise move", current_position);
  320. }
  321. destination.set(rx, ry);
  322. prepare_internal_move_to_destination(); // set current_position from destination
  323. if (DEBUGGING(LEVELING)) DEBUG_POS("xy move", current_position);
  324. if (rz < current_position.z) { // lowering?
  325. destination.z = rz;
  326. prepare_internal_fast_move_to_destination(z_feedrate); // set current_position from destination
  327. if (DEBUGGING(LEVELING)) DEBUG_POS("z lower move", current_position);
  328. }
  329. #elif IS_SCARA
  330. if (!position_is_reachable(rx, ry)) return;
  331. destination = current_position;
  332. // If Z needs to raise, do it before moving XY
  333. if (destination.z < rz) {
  334. destination.z = rz;
  335. prepare_internal_fast_move_to_destination(z_feedrate);
  336. }
  337. destination.set(rx, ry);
  338. prepare_internal_fast_move_to_destination(xy_feedrate);
  339. // If Z needs to lower, do it after moving XY
  340. if (destination.z > rz) {
  341. destination.z = rz;
  342. prepare_internal_fast_move_to_destination(z_feedrate);
  343. }
  344. #else
  345. // If Z needs to raise, do it before moving XY
  346. if (current_position.z < rz) {
  347. current_position.z = rz;
  348. line_to_current_position(z_feedrate);
  349. }
  350. current_position.set(rx, ry);
  351. line_to_current_position(xy_feedrate);
  352. // If Z needs to lower, do it after moving XY
  353. if (current_position.z > rz) {
  354. current_position.z = rz;
  355. line_to_current_position(z_feedrate);
  356. }
  357. #endif
  358. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("<<< do_blocking_move_to");
  359. planner.synchronize();
  360. }
  361. void do_blocking_move_to(const xy_pos_t &raw, const feedRate_t &fr_mm_s/*=0.0f*/) {
  362. do_blocking_move_to(raw.x, raw.y, current_position.z, fr_mm_s);
  363. }
  364. void do_blocking_move_to(const xyz_pos_t &raw, const feedRate_t &fr_mm_s/*=0.0f*/) {
  365. do_blocking_move_to(raw.x, raw.y, raw.z, fr_mm_s);
  366. }
  367. void do_blocking_move_to(const xyze_pos_t &raw, const feedRate_t &fr_mm_s/*=0.0f*/) {
  368. do_blocking_move_to(raw.x, raw.y, raw.z, fr_mm_s);
  369. }
  370. void do_blocking_move_to_x(const float &rx, const feedRate_t &fr_mm_s/*=0.0*/) {
  371. do_blocking_move_to(rx, current_position.y, current_position.z, fr_mm_s);
  372. }
  373. void do_blocking_move_to_y(const float &ry, const feedRate_t &fr_mm_s/*=0.0*/) {
  374. do_blocking_move_to(current_position.x, ry, current_position.z, fr_mm_s);
  375. }
  376. void do_blocking_move_to_z(const float &rz, const feedRate_t &fr_mm_s/*=0.0*/) {
  377. do_blocking_move_to_xy_z(current_position, rz, fr_mm_s);
  378. }
  379. void do_blocking_move_to_xy(const float &rx, const float &ry, const feedRate_t &fr_mm_s/*=0.0*/) {
  380. do_blocking_move_to(rx, ry, current_position.z, fr_mm_s);
  381. }
  382. void do_blocking_move_to_xy(const xy_pos_t &raw, const feedRate_t &fr_mm_s/*=0.0f*/) {
  383. do_blocking_move_to_xy(raw.x, raw.y, fr_mm_s);
  384. }
  385. void do_blocking_move_to_xy_z(const xy_pos_t &raw, const float &z, const feedRate_t &fr_mm_s/*=0.0f*/) {
  386. do_blocking_move_to(raw.x, raw.y, z, fr_mm_s);
  387. }
  388. //
  389. // Prepare to do endstop or probe moves with custom feedrates.
  390. // - Save / restore current feedrate and multiplier
  391. //
  392. static float saved_feedrate_mm_s;
  393. static int16_t saved_feedrate_percentage;
  394. void remember_feedrate_and_scaling() {
  395. saved_feedrate_mm_s = feedrate_mm_s;
  396. saved_feedrate_percentage = feedrate_percentage;
  397. }
  398. void remember_feedrate_scaling_off() {
  399. remember_feedrate_and_scaling();
  400. feedrate_percentage = 100;
  401. }
  402. void restore_feedrate_and_scaling() {
  403. feedrate_mm_s = saved_feedrate_mm_s;
  404. feedrate_percentage = saved_feedrate_percentage;
  405. }
  406. #if HAS_SOFTWARE_ENDSTOPS
  407. bool soft_endstops_enabled = true;
  408. // Software Endstops are based on the configured limits.
  409. axis_limits_t soft_endstop = {
  410. { X_MIN_POS, Y_MIN_POS, Z_MIN_POS },
  411. { X_MAX_POS, Y_MAX_POS, Z_MAX_POS }
  412. };
  413. /**
  414. * Software endstops can be used to monitor the open end of
  415. * an axis that has a hardware endstop on the other end. Or
  416. * they can prevent axes from moving past endstops and grinding.
  417. *
  418. * To keep doing their job as the coordinate system changes,
  419. * the software endstop positions must be refreshed to remain
  420. * at the same positions relative to the machine.
  421. */
  422. void update_software_endstops(const AxisEnum axis
  423. #if HAS_HOTEND_OFFSET
  424. , const uint8_t old_tool_index/*=0*/, const uint8_t new_tool_index/*=0*/
  425. #endif
  426. ) {
  427. #if ENABLED(DUAL_X_CARRIAGE)
  428. if (axis == X_AXIS) {
  429. // In Dual X mode hotend_offset[X] is T1's home position
  430. const float dual_max_x = _MAX(hotend_offset[1].x, X2_MAX_POS);
  431. if (new_tool_index != 0) {
  432. // T1 can move from X2_MIN_POS to X2_MAX_POS or X2 home position (whichever is larger)
  433. soft_endstop.min.x = X2_MIN_POS;
  434. soft_endstop.max.x = dual_max_x;
  435. }
  436. else if (dxc_is_duplicating()) {
  437. // In Duplication Mode, T0 can move as far left as X1_MIN_POS
  438. // but not so far to the right that T1 would move past the end
  439. soft_endstop.min.x = X1_MIN_POS;
  440. soft_endstop.max.x = _MIN(X1_MAX_POS, dual_max_x - duplicate_extruder_x_offset);
  441. }
  442. else {
  443. // In other modes, T0 can move from X1_MIN_POS to X1_MAX_POS
  444. soft_endstop.min.x = X1_MIN_POS;
  445. soft_endstop.max.x = X1_MAX_POS;
  446. }
  447. }
  448. #elif ENABLED(DELTA)
  449. soft_endstop.min[axis] = base_min_pos(axis);
  450. soft_endstop.max[axis] = (axis == Z_AXIS ? delta_height
  451. #if HAS_BED_PROBE
  452. - probe_offset.z
  453. #endif
  454. : base_max_pos(axis));
  455. switch (axis) {
  456. case X_AXIS:
  457. case Y_AXIS:
  458. // Get a minimum radius for clamping
  459. delta_max_radius = _MIN(ABS(_MAX(soft_endstop.min.x, soft_endstop.min.y)), soft_endstop.max.x, soft_endstop.max.y);
  460. delta_max_radius_2 = sq(delta_max_radius);
  461. break;
  462. case Z_AXIS:
  463. delta_clip_start_height = soft_endstop.max[axis] - delta_safe_distance_from_top();
  464. default: break;
  465. }
  466. #elif HAS_HOTEND_OFFSET
  467. // Software endstops are relative to the tool 0 workspace, so
  468. // the movement limits must be shifted by the tool offset to
  469. // retain the same physical limit when other tools are selected.
  470. if (old_tool_index != new_tool_index) {
  471. const float offs = hotend_offset[new_tool_index][axis] - hotend_offset[old_tool_index][axis];
  472. soft_endstop.min[axis] += offs;
  473. soft_endstop.max[axis] += offs;
  474. }
  475. else {
  476. const float offs = hotend_offset[active_extruder][axis];
  477. soft_endstop.min[axis] = base_min_pos(axis) + offs;
  478. soft_endstop.max[axis] = base_max_pos(axis) + offs;
  479. }
  480. #else
  481. soft_endstop.min[axis] = base_min_pos(axis);
  482. soft_endstop.max[axis] = base_max_pos(axis);
  483. #endif
  484. if (DEBUGGING(LEVELING))
  485. SERIAL_ECHOLNPAIR("Axis ", axis_codes[axis], " min:", soft_endstop.min[axis], " max:", soft_endstop.max[axis]);
  486. }
  487. /**
  488. * Constrain the given coordinates to the software endstops.
  489. *
  490. * For DELTA/SCARA the XY constraint is based on the smallest
  491. * radius within the set software endstops.
  492. */
  493. void apply_motion_limits(xyz_pos_t &target) {
  494. if (!soft_endstops_enabled) return;
  495. #if IS_KINEMATIC
  496. #if ENABLED(DELTA)
  497. if (!all_axes_homed()) return;
  498. #endif
  499. #if HAS_HOTEND_OFFSET && ENABLED(DELTA)
  500. // The effector center position will be the target minus the hotend offset.
  501. const xy_pos_t offs = hotend_offset[active_extruder];
  502. #else
  503. // SCARA needs to consider the angle of the arm through the entire move, so for now use no tool offset.
  504. constexpr xy_pos_t offs{0};
  505. #endif
  506. if (true
  507. #if IS_SCARA
  508. && TEST(axis_homed, X_AXIS) && TEST(axis_homed, Y_AXIS)
  509. #endif
  510. ) {
  511. const float dist_2 = HYPOT2(target.x - offs.x, target.y - offs.y);
  512. if (dist_2 > delta_max_radius_2)
  513. target *= delta_max_radius / SQRT(dist_2); // 200 / 300 = 0.66
  514. }
  515. #else
  516. if (TEST(axis_homed, X_AXIS)) {
  517. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MIN_SOFTWARE_ENDSTOP_X)
  518. NOLESS(target.x, soft_endstop.min.x);
  519. #endif
  520. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MAX_SOFTWARE_ENDSTOP_X)
  521. NOMORE(target.x, soft_endstop.max.x);
  522. #endif
  523. }
  524. if (TEST(axis_homed, Y_AXIS)) {
  525. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MIN_SOFTWARE_ENDSTOP_Y)
  526. NOLESS(target.y, soft_endstop.min.y);
  527. #endif
  528. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MAX_SOFTWARE_ENDSTOP_Y)
  529. NOMORE(target.y, soft_endstop.max.y);
  530. #endif
  531. }
  532. #endif
  533. if (TEST(axis_homed, Z_AXIS)) {
  534. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MIN_SOFTWARE_ENDSTOP_Z)
  535. NOLESS(target.z, soft_endstop.min.z);
  536. #endif
  537. #if !HAS_SOFTWARE_ENDSTOPS || ENABLED(MAX_SOFTWARE_ENDSTOP_Z)
  538. NOMORE(target.z, soft_endstop.max.z);
  539. #endif
  540. }
  541. }
  542. #endif // HAS_SOFTWARE_ENDSTOPS
  543. #if !UBL_SEGMENTED
  544. #if IS_KINEMATIC
  545. #if IS_SCARA
  546. /**
  547. * Before raising this value, use M665 S[seg_per_sec] to decrease
  548. * the number of segments-per-second. Default is 200. Some deltas
  549. * do better with 160 or lower. It would be good to know how many
  550. * segments-per-second are actually possible for SCARA on AVR.
  551. *
  552. * Longer segments result in less kinematic overhead
  553. * but may produce jagged lines. Try 0.5mm, 1.0mm, and 2.0mm
  554. * and compare the difference.
  555. */
  556. #define SCARA_MIN_SEGMENT_LENGTH 0.5f
  557. #endif
  558. /**
  559. * Prepare a linear move in a DELTA or SCARA setup.
  560. *
  561. * Called from prepare_move_to_destination as the
  562. * default Delta/SCARA segmenter.
  563. *
  564. * This calls planner.buffer_line several times, adding
  565. * small incremental moves for DELTA or SCARA.
  566. *
  567. * For Unified Bed Leveling (Delta or Segmented Cartesian)
  568. * the ubl.line_to_destination_segmented method replaces this.
  569. *
  570. * For Auto Bed Leveling (Bilinear) with SEGMENT_LEVELED_MOVES
  571. * this is replaced by segmented_line_to_destination below.
  572. */
  573. inline bool line_to_destination_kinematic() {
  574. // Get the top feedrate of the move in the XY plane
  575. const float scaled_fr_mm_s = MMS_SCALED(feedrate_mm_s);
  576. const xyze_float_t diff = destination - current_position;
  577. // If the move is only in Z/E don't split up the move
  578. if (!diff.x && !diff.y) {
  579. planner.buffer_line(destination, scaled_fr_mm_s, active_extruder);
  580. return false; // caller will update current_position
  581. }
  582. // Fail if attempting move outside printable radius
  583. if (!position_is_reachable(destination)) return true;
  584. // Get the linear distance in XYZ
  585. float cartesian_mm = diff.magnitude();
  586. // If the move is very short, check the E move distance
  587. if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = ABS(diff.e);
  588. // No E move either? Game over.
  589. if (UNEAR_ZERO(cartesian_mm)) return true;
  590. // Minimum number of seconds to move the given distance
  591. const float seconds = cartesian_mm / scaled_fr_mm_s;
  592. // The number of segments-per-second times the duration
  593. // gives the number of segments
  594. uint16_t segments = delta_segments_per_second * seconds;
  595. // For SCARA enforce a minimum segment size
  596. #if IS_SCARA
  597. NOMORE(segments, cartesian_mm * RECIPROCAL(SCARA_MIN_SEGMENT_LENGTH));
  598. #endif
  599. // At least one segment is required
  600. NOLESS(segments, 1U);
  601. // The approximate length of each segment
  602. const float inv_segments = 1.0f / float(segments),
  603. cartesian_segment_mm = cartesian_mm * inv_segments;
  604. const xyze_float_t segment_distance = diff * inv_segments;
  605. #if ENABLED(SCARA_FEEDRATE_SCALING)
  606. const float inv_duration = scaled_fr_mm_s / cartesian_segment_mm;
  607. #endif
  608. /*
  609. SERIAL_ECHOPAIR("mm=", cartesian_mm);
  610. SERIAL_ECHOPAIR(" seconds=", seconds);
  611. SERIAL_ECHOPAIR(" segments=", segments);
  612. SERIAL_ECHOPAIR(" segment_mm=", cartesian_segment_mm);
  613. SERIAL_EOL();
  614. //*/
  615. // Get the current position as starting point
  616. xyze_pos_t raw = current_position;
  617. // Calculate and execute the segments
  618. while (--segments) {
  619. static millis_t next_idle_ms = millis() + 200UL;
  620. thermalManager.manage_heater(); // This returns immediately if not really needed.
  621. if (ELAPSED(millis(), next_idle_ms)) {
  622. next_idle_ms = millis() + 200UL;
  623. idle();
  624. }
  625. raw += segment_distance;
  626. if (!planner.buffer_line(raw, scaled_fr_mm_s, active_extruder, cartesian_segment_mm
  627. #if ENABLED(SCARA_FEEDRATE_SCALING)
  628. , inv_duration
  629. #endif
  630. ))
  631. break;
  632. }
  633. // Ensure last segment arrives at target location.
  634. planner.buffer_line(destination, scaled_fr_mm_s, active_extruder, cartesian_segment_mm
  635. #if ENABLED(SCARA_FEEDRATE_SCALING)
  636. , inv_duration
  637. #endif
  638. );
  639. return false; // caller will update current_position
  640. }
  641. #else // !IS_KINEMATIC
  642. #if ENABLED(SEGMENT_LEVELED_MOVES)
  643. /**
  644. * Prepare a segmented move on a CARTESIAN setup.
  645. *
  646. * This calls planner.buffer_line several times, adding
  647. * small incremental moves. This allows the planner to
  648. * apply more detailed bed leveling to the full move.
  649. */
  650. inline void segmented_line_to_destination(const feedRate_t &fr_mm_s, const float segment_size=LEVELED_SEGMENT_LENGTH) {
  651. const xyze_float_t diff = destination - current_position;
  652. // If the move is only in Z/E don't split up the move
  653. if (!diff.x && !diff.y) {
  654. planner.buffer_line(destination, fr_mm_s, active_extruder);
  655. return;
  656. }
  657. // Get the linear distance in XYZ
  658. // If the move is very short, check the E move distance
  659. // No E move either? Game over.
  660. float cartesian_mm = diff.magnitude();
  661. if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = ABS(diff.e);
  662. if (UNEAR_ZERO(cartesian_mm)) return;
  663. // The length divided by the segment size
  664. // At least one segment is required
  665. uint16_t segments = cartesian_mm / segment_size;
  666. NOLESS(segments, 1U);
  667. // The approximate length of each segment
  668. const float inv_segments = 1.0f / float(segments),
  669. cartesian_segment_mm = cartesian_mm * inv_segments;
  670. const xyze_float_t segment_distance = diff * inv_segments;
  671. #if ENABLED(SCARA_FEEDRATE_SCALING)
  672. const float inv_duration = scaled_fr_mm_s / cartesian_segment_mm;
  673. #endif
  674. // SERIAL_ECHOPAIR("mm=", cartesian_mm);
  675. // SERIAL_ECHOLNPAIR(" segments=", segments);
  676. // SERIAL_ECHOLNPAIR(" segment_mm=", cartesian_segment_mm);
  677. // Get the raw current position as starting point
  678. xyze_pos_t raw = current_position;
  679. // Calculate and execute the segments
  680. while (--segments) {
  681. static millis_t next_idle_ms = millis() + 200UL;
  682. thermalManager.manage_heater(); // This returns immediately if not really needed.
  683. if (ELAPSED(millis(), next_idle_ms)) {
  684. next_idle_ms = millis() + 200UL;
  685. idle();
  686. }
  687. raw += segment_distance;
  688. if (!planner.buffer_line(raw, fr_mm_s, active_extruder, cartesian_segment_mm
  689. #if ENABLED(SCARA_FEEDRATE_SCALING)
  690. , inv_duration
  691. #endif
  692. ))
  693. break;
  694. }
  695. // Since segment_distance is only approximate,
  696. // the final move must be to the exact destination.
  697. planner.buffer_line(destination, fr_mm_s, active_extruder, cartesian_segment_mm
  698. #if ENABLED(SCARA_FEEDRATE_SCALING)
  699. , inv_duration
  700. #endif
  701. );
  702. }
  703. #endif // SEGMENT_LEVELED_MOVES
  704. /**
  705. * Prepare a linear move in a Cartesian setup.
  706. *
  707. * When a mesh-based leveling system is active, moves are segmented
  708. * according to the configuration of the leveling system.
  709. *
  710. * Return true if 'current_position' was set to 'destination'
  711. */
  712. inline bool prepare_move_to_destination_cartesian() {
  713. const float scaled_fr_mm_s = MMS_SCALED(feedrate_mm_s);
  714. #if HAS_MESH
  715. if (planner.leveling_active && planner.leveling_active_at_z(destination.z)) {
  716. #if ENABLED(AUTO_BED_LEVELING_UBL)
  717. ubl.line_to_destination_cartesian(scaled_fr_mm_s, active_extruder); // UBL's motion routine needs to know about
  718. return true; // all moves, including Z-only moves.
  719. #elif ENABLED(SEGMENT_LEVELED_MOVES)
  720. segmented_line_to_destination(scaled_fr_mm_s);
  721. return false; // caller will update current_position
  722. #else
  723. /**
  724. * For MBL and ABL-BILINEAR only segment moves when X or Y are involved.
  725. * Otherwise fall through to do a direct single move.
  726. */
  727. if (xy_pos_t(current_position) != xy_pos_t(destination)) {
  728. #if ENABLED(MESH_BED_LEVELING)
  729. mbl.line_to_destination(scaled_fr_mm_s);
  730. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  731. bilinear_line_to_destination(scaled_fr_mm_s);
  732. #endif
  733. return true;
  734. }
  735. #endif
  736. }
  737. #endif // HAS_MESH
  738. planner.buffer_line(destination, scaled_fr_mm_s, active_extruder);
  739. return false; // caller will update current_position
  740. }
  741. #endif // !IS_KINEMATIC
  742. #endif // !UBL_SEGMENTED
  743. #if HAS_DUPLICATION_MODE
  744. bool extruder_duplication_enabled,
  745. mirrored_duplication_mode;
  746. #if ENABLED(MULTI_NOZZLE_DUPLICATION)
  747. uint8_t duplication_e_mask; // = 0
  748. #endif
  749. #endif
  750. #if ENABLED(DUAL_X_CARRIAGE)
  751. DualXMode dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  752. float inactive_extruder_x_pos = X2_MAX_POS, // used in mode 0 & 1
  753. duplicate_extruder_x_offset = DEFAULT_DUPLICATION_X_OFFSET; // used in mode 2
  754. xyz_pos_t raised_parked_position; // used in mode 1
  755. bool active_extruder_parked = false; // used in mode 1 & 2
  756. millis_t delayed_move_time = 0; // used in mode 1
  757. int16_t duplicate_extruder_temp_offset = 0; // used in mode 2
  758. float x_home_pos(const int extruder) {
  759. if (extruder == 0)
  760. return base_home_pos(X_AXIS);
  761. else
  762. /**
  763. * In dual carriage mode the extruder offset provides an override of the
  764. * second X-carriage position when homed - otherwise X2_HOME_POS is used.
  765. * This allows soft recalibration of the second extruder home position
  766. * without firmware reflash (through the M218 command).
  767. */
  768. return hotend_offset[1].x > 0 ? hotend_offset[1].x : X2_HOME_POS;
  769. }
  770. /**
  771. * Prepare a linear move in a dual X axis setup
  772. *
  773. * Return true if current_position[] was set to destination[]
  774. */
  775. inline bool dual_x_carriage_unpark() {
  776. if (active_extruder_parked) {
  777. switch (dual_x_carriage_mode) {
  778. case DXC_FULL_CONTROL_MODE:
  779. break;
  780. case DXC_AUTO_PARK_MODE:
  781. if (current_position.e == destination.e) {
  782. // This is a travel move (with no extrusion)
  783. // Skip it, but keep track of the current position
  784. // (so it can be used as the start of the next non-travel move)
  785. if (delayed_move_time != 0xFFFFFFFFUL) {
  786. current_position = destination;
  787. NOLESS(raised_parked_position.z, destination.z);
  788. delayed_move_time = millis();
  789. return true;
  790. }
  791. }
  792. // unpark extruder: 1) raise, 2) move into starting XY position, 3) lower
  793. #define CUR_X current_position.x
  794. #define CUR_Y current_position.y
  795. #define CUR_Z current_position.z
  796. #define CUR_E current_position.e
  797. #define RAISED_X raised_parked_position.x
  798. #define RAISED_Y raised_parked_position.y
  799. #define RAISED_Z raised_parked_position.z
  800. if ( planner.buffer_line(RAISED_X, RAISED_Y, RAISED_Z, CUR_E, planner.settings.max_feedrate_mm_s[Z_AXIS], active_extruder))
  801. if (planner.buffer_line( CUR_X, CUR_Y, RAISED_Z, CUR_E, PLANNER_XY_FEEDRATE(), active_extruder))
  802. line_to_current_position(planner.settings.max_feedrate_mm_s[Z_AXIS]);
  803. delayed_move_time = 0;
  804. active_extruder_parked = false;
  805. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Clear active_extruder_parked");
  806. break;
  807. case DXC_MIRRORED_MODE:
  808. case DXC_DUPLICATION_MODE:
  809. if (active_extruder == 0) {
  810. xyze_pos_t new_pos = current_position;
  811. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE)
  812. new_pos.x += duplicate_extruder_x_offset;
  813. else
  814. new_pos.x = inactive_extruder_x_pos;
  815. // move duplicate extruder into correct duplication position.
  816. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Set planner X", inactive_extruder_x_pos, " ... Line to X", new_pos.x);
  817. planner.set_position_mm(inactive_extruder_x_pos, current_position.y, current_position.z, current_position.e);
  818. if (!planner.buffer_line(new_pos, planner.settings.max_feedrate_mm_s[X_AXIS], 1)) break;
  819. planner.synchronize();
  820. sync_plan_position();
  821. extruder_duplication_enabled = true;
  822. active_extruder_parked = false;
  823. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Set extruder_duplication_enabled\nClear active_extruder_parked");
  824. }
  825. else if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Active extruder not 0");
  826. break;
  827. }
  828. }
  829. stepper.set_directions();
  830. return false;
  831. }
  832. #endif // DUAL_X_CARRIAGE
  833. /**
  834. * Prepare a single move and get ready for the next one
  835. *
  836. * This may result in several calls to planner.buffer_line to
  837. * do smaller moves for DELTA, SCARA, mesh moves, etc.
  838. *
  839. * Make sure current_position.e and destination.e are good
  840. * before calling or cold/lengthy extrusion may get missed.
  841. *
  842. * Before exit, current_position is set to destination.
  843. */
  844. void prepare_move_to_destination() {
  845. apply_motion_limits(destination);
  846. #if EITHER(PREVENT_COLD_EXTRUSION, PREVENT_LENGTHY_EXTRUDE)
  847. if (!DEBUGGING(DRYRUN) && destination.e != current_position.e) {
  848. bool ignore_e = false;
  849. #if ENABLED(PREVENT_COLD_EXTRUSION)
  850. ignore_e = thermalManager.tooColdToExtrude(active_extruder);
  851. if (ignore_e) SERIAL_ECHO_MSG(MSG_ERR_COLD_EXTRUDE_STOP);
  852. #endif
  853. #if ENABLED(PREVENT_LENGTHY_EXTRUDE)
  854. const float e_delta = ABS(destination.e - current_position.e) * planner.e_factor[active_extruder];
  855. if (e_delta > (EXTRUDE_MAXLENGTH)) {
  856. #if ENABLED(MIXING_EXTRUDER)
  857. float collector[MIXING_STEPPERS];
  858. mixer.refresh_collector(1.0, mixer.get_current_vtool(), collector);
  859. MIXER_STEPPER_LOOP(e) {
  860. if (e_delta * collector[e] > (EXTRUDE_MAXLENGTH)) {
  861. ignore_e = true;
  862. SERIAL_ECHO_MSG(MSG_ERR_LONG_EXTRUDE_STOP);
  863. break;
  864. }
  865. }
  866. #else
  867. ignore_e = true;
  868. SERIAL_ECHO_MSG(MSG_ERR_LONG_EXTRUDE_STOP);
  869. #endif
  870. }
  871. #endif
  872. if (ignore_e) {
  873. current_position.e = destination.e; // Behave as if the E move really took place
  874. planner.set_e_position_mm(destination.e); // Prevent the planner from complaining too
  875. }
  876. }
  877. #endif // PREVENT_COLD_EXTRUSION || PREVENT_LENGTHY_EXTRUDE
  878. #if ENABLED(DUAL_X_CARRIAGE)
  879. if (dual_x_carriage_unpark()) return;
  880. #endif
  881. if (
  882. #if UBL_SEGMENTED
  883. #if IS_KINEMATIC // UBL using Kinematic / Cartesian cases as a workaround for now.
  884. ubl.line_to_destination_segmented(MMS_SCALED(feedrate_mm_s))
  885. #else
  886. prepare_move_to_destination_cartesian()
  887. #endif
  888. #elif IS_KINEMATIC
  889. line_to_destination_kinematic()
  890. #else
  891. prepare_move_to_destination_cartesian()
  892. #endif
  893. ) return;
  894. current_position = destination;
  895. }
  896. uint8_t axes_need_homing(uint8_t axis_bits/*=0x07*/) {
  897. #if ENABLED(HOME_AFTER_DEACTIVATE)
  898. #define HOMED_FLAGS axis_known_position
  899. #else
  900. #define HOMED_FLAGS axis_homed
  901. #endif
  902. // Clear test bits that are homed
  903. if (TEST(axis_bits, X_AXIS) && TEST(HOMED_FLAGS, X_AXIS)) CBI(axis_bits, X_AXIS);
  904. if (TEST(axis_bits, Y_AXIS) && TEST(HOMED_FLAGS, Y_AXIS)) CBI(axis_bits, Y_AXIS);
  905. if (TEST(axis_bits, Z_AXIS) && TEST(HOMED_FLAGS, Z_AXIS)) CBI(axis_bits, Z_AXIS);
  906. return axis_bits;
  907. }
  908. bool axis_unhomed_error(uint8_t axis_bits/*=0x07*/) {
  909. if ((axis_bits = axes_need_homing(axis_bits))) {
  910. PGM_P home_first = GET_TEXT(MSG_HOME_FIRST);
  911. char msg[strlen_P(home_first)+1];
  912. sprintf_P(msg, home_first,
  913. TEST(axis_bits, X_AXIS) ? "X" : "",
  914. TEST(axis_bits, Y_AXIS) ? "Y" : "",
  915. TEST(axis_bits, Z_AXIS) ? "Z" : ""
  916. );
  917. SERIAL_ECHO_START();
  918. SERIAL_ECHOLN(msg);
  919. #if HAS_DISPLAY
  920. ui.set_status(msg);
  921. #endif
  922. return true;
  923. }
  924. return false;
  925. }
  926. /**
  927. * Homing bump feedrate (mm/s)
  928. */
  929. feedRate_t get_homing_bump_feedrate(const AxisEnum axis) {
  930. #if HOMING_Z_WITH_PROBE
  931. if (axis == Z_AXIS) return MMM_TO_MMS(Z_PROBE_SPEED_SLOW);
  932. #endif
  933. static const uint8_t homing_bump_divisor[] PROGMEM = HOMING_BUMP_DIVISOR;
  934. uint8_t hbd = pgm_read_byte(&homing_bump_divisor[axis]);
  935. if (hbd < 1) {
  936. hbd = 10;
  937. SERIAL_ECHO_MSG("Warning: Homing Bump Divisor < 1");
  938. }
  939. return homing_feedrate(axis) / float(hbd);
  940. }
  941. #if ENABLED(SENSORLESS_HOMING)
  942. /**
  943. * Set sensorless homing if the axis has it, accounting for Core Kinematics.
  944. */
  945. sensorless_t start_sensorless_homing_per_axis(const AxisEnum axis) {
  946. sensorless_t stealth_states { false };
  947. switch (axis) {
  948. default: break;
  949. #if X_SENSORLESS
  950. case X_AXIS:
  951. stealth_states.x = tmc_enable_stallguard(stepperX);
  952. #if AXIS_HAS_STALLGUARD(X2)
  953. stealth_states.x2 = tmc_enable_stallguard(stepperX2);
  954. #endif
  955. #if CORE_IS_XY && Y_SENSORLESS
  956. stealth_states.y = tmc_enable_stallguard(stepperY);
  957. #elif CORE_IS_XZ && Z_SENSORLESS
  958. stealth_states.z = tmc_enable_stallguard(stepperZ);
  959. #endif
  960. break;
  961. #endif
  962. #if Y_SENSORLESS
  963. case Y_AXIS:
  964. stealth_states.y = tmc_enable_stallguard(stepperY);
  965. #if AXIS_HAS_STALLGUARD(Y2)
  966. stealth_states.y2 = tmc_enable_stallguard(stepperY2);
  967. #endif
  968. #if CORE_IS_XY && X_SENSORLESS
  969. stealth_states.x = tmc_enable_stallguard(stepperX);
  970. #elif CORE_IS_YZ && Z_SENSORLESS
  971. stealth_states.z = tmc_enable_stallguard(stepperZ);
  972. #endif
  973. break;
  974. #endif
  975. #if Z_SENSORLESS
  976. case Z_AXIS:
  977. stealth_states.z = tmc_enable_stallguard(stepperZ);
  978. #if AXIS_HAS_STALLGUARD(Z2)
  979. stealth_states.z2 = tmc_enable_stallguard(stepperZ2);
  980. #endif
  981. #if AXIS_HAS_STALLGUARD(Z3)
  982. stealth_states.z3 = tmc_enable_stallguard(stepperZ3);
  983. #endif
  984. #if AXIS_HAS_STALLGUARD(Z4)
  985. stealth_states.z4 = tmc_enable_stallguard(stepperZ4);
  986. #endif
  987. #if CORE_IS_XZ && X_SENSORLESS
  988. stealth_states.x = tmc_enable_stallguard(stepperX);
  989. #elif CORE_IS_YZ && Y_SENSORLESS
  990. stealth_states.y = tmc_enable_stallguard(stepperY);
  991. #endif
  992. break;
  993. #endif
  994. }
  995. #if ENABLED(SPI_ENDSTOPS)
  996. switch (axis) {
  997. #if X_SPI_SENSORLESS
  998. case X_AXIS: endstops.tmc_spi_homing.x = true; break;
  999. #endif
  1000. #if Y_SPI_SENSORLESS
  1001. case Y_AXIS: endstops.tmc_spi_homing.y = true; break;
  1002. #endif
  1003. #if Z_SPI_SENSORLESS
  1004. case Z_AXIS: endstops.tmc_spi_homing.z = true; break;
  1005. #endif
  1006. default: break;
  1007. }
  1008. #endif
  1009. #if ENABLED(IMPROVE_HOMING_RELIABILITY)
  1010. sg_guard_period = millis() + default_sg_guard_duration;
  1011. #endif
  1012. return stealth_states;
  1013. }
  1014. void end_sensorless_homing_per_axis(const AxisEnum axis, sensorless_t enable_stealth) {
  1015. switch (axis) {
  1016. default: break;
  1017. #if X_SENSORLESS
  1018. case X_AXIS:
  1019. tmc_disable_stallguard(stepperX, enable_stealth.x);
  1020. #if AXIS_HAS_STALLGUARD(X2)
  1021. tmc_disable_stallguard(stepperX2, enable_stealth.x2);
  1022. #endif
  1023. #if CORE_IS_XY && Y_SENSORLESS
  1024. tmc_disable_stallguard(stepperY, enable_stealth.y);
  1025. #elif CORE_IS_XZ && Z_SENSORLESS
  1026. tmc_disable_stallguard(stepperZ, enable_stealth.z);
  1027. #endif
  1028. break;
  1029. #endif
  1030. #if Y_SENSORLESS
  1031. case Y_AXIS:
  1032. tmc_disable_stallguard(stepperY, enable_stealth.y);
  1033. #if AXIS_HAS_STALLGUARD(Y2)
  1034. tmc_disable_stallguard(stepperY2, enable_stealth.y2);
  1035. #endif
  1036. #if CORE_IS_XY && X_SENSORLESS
  1037. tmc_disable_stallguard(stepperX, enable_stealth.x);
  1038. #elif CORE_IS_YZ && Z_SENSORLESS
  1039. tmc_disable_stallguard(stepperZ, enable_stealth.z);
  1040. #endif
  1041. break;
  1042. #endif
  1043. #if Z_SENSORLESS
  1044. case Z_AXIS:
  1045. tmc_disable_stallguard(stepperZ, enable_stealth.z);
  1046. #if AXIS_HAS_STALLGUARD(Z2)
  1047. tmc_disable_stallguard(stepperZ2, enable_stealth.z2);
  1048. #endif
  1049. #if AXIS_HAS_STALLGUARD(Z3)
  1050. tmc_disable_stallguard(stepperZ3, enable_stealth.z3);
  1051. #endif
  1052. #if AXIS_HAS_STALLGUARD(Z4)
  1053. tmc_disable_stallguard(stepperZ4, enable_stealth.z4);
  1054. #endif
  1055. #if CORE_IS_XZ && X_SENSORLESS
  1056. tmc_disable_stallguard(stepperX, enable_stealth.x);
  1057. #elif CORE_IS_YZ && Y_SENSORLESS
  1058. tmc_disable_stallguard(stepperY, enable_stealth.y);
  1059. #endif
  1060. break;
  1061. #endif
  1062. }
  1063. #if ENABLED(SPI_ENDSTOPS)
  1064. switch (axis) {
  1065. #if X_SPI_SENSORLESS
  1066. case X_AXIS: endstops.tmc_spi_homing.x = false; break;
  1067. #endif
  1068. #if Y_SPI_SENSORLESS
  1069. case Y_AXIS: endstops.tmc_spi_homing.y = false; break;
  1070. #endif
  1071. #if Z_SPI_SENSORLESS
  1072. case Z_AXIS: endstops.tmc_spi_homing.z = false; break;
  1073. #endif
  1074. default: break;
  1075. }
  1076. #endif
  1077. }
  1078. #endif // SENSORLESS_HOMING
  1079. /**
  1080. * Home an individual linear axis
  1081. */
  1082. void do_homing_move(const AxisEnum axis, const float distance, const feedRate_t fr_mm_s=0.0) {
  1083. if (DEBUGGING(LEVELING)) {
  1084. DEBUG_ECHOPAIR(">>> do_homing_move(", axis_codes[axis], ", ", distance, ", ");
  1085. if (fr_mm_s)
  1086. DEBUG_ECHO(fr_mm_s);
  1087. else
  1088. DEBUG_ECHOPAIR("[", homing_feedrate(axis), "]");
  1089. DEBUG_ECHOLNPGM(")");
  1090. }
  1091. #if HOMING_Z_WITH_PROBE && HAS_HEATED_BED && ENABLED(WAIT_FOR_BED_HEATER)
  1092. // Wait for bed to heat back up between probing points
  1093. if (axis == Z_AXIS && distance < 0 && thermalManager.isHeatingBed()) {
  1094. serialprintPGM(msg_wait_for_bed_heating);
  1095. #if HAS_DISPLAY
  1096. LCD_MESSAGEPGM(MSG_BED_HEATING);
  1097. #endif
  1098. thermalManager.wait_for_bed();
  1099. #if HAS_DISPLAY
  1100. ui.reset_status();
  1101. #endif
  1102. }
  1103. #endif
  1104. // Only do some things when moving towards an endstop
  1105. const int8_t axis_home_dir =
  1106. #if ENABLED(DUAL_X_CARRIAGE)
  1107. (axis == X_AXIS) ? x_home_dir(active_extruder) :
  1108. #endif
  1109. home_dir(axis);
  1110. const bool is_home_dir = (axis_home_dir > 0) == (distance > 0);
  1111. #if ENABLED(SENSORLESS_HOMING)
  1112. sensorless_t stealth_states;
  1113. #endif
  1114. if (is_home_dir) {
  1115. #if HOMING_Z_WITH_PROBE && QUIET_PROBING
  1116. if (axis == Z_AXIS) probing_pause(true);
  1117. #endif
  1118. // Disable stealthChop if used. Enable diag1 pin on driver.
  1119. #if ENABLED(SENSORLESS_HOMING)
  1120. stealth_states = start_sensorless_homing_per_axis(axis);
  1121. #endif
  1122. }
  1123. const feedRate_t real_fr_mm_s = fr_mm_s ?: homing_feedrate(axis);
  1124. #if IS_SCARA
  1125. // Tell the planner the axis is at 0
  1126. current_position[axis] = 0;
  1127. sync_plan_position();
  1128. current_position[axis] = distance;
  1129. line_to_current_position(real_fr_mm_s);
  1130. #else
  1131. abce_pos_t target = { planner.get_axis_position_mm(A_AXIS), planner.get_axis_position_mm(B_AXIS), planner.get_axis_position_mm(C_AXIS), planner.get_axis_position_mm(E_AXIS) };
  1132. target[axis] = 0;
  1133. planner.set_machine_position_mm(target);
  1134. target[axis] = distance;
  1135. #if IS_KINEMATIC && DISABLED(CLASSIC_JERK)
  1136. const xyze_float_t delta_mm_cart{0};
  1137. #endif
  1138. // Set delta/cartesian axes directly
  1139. planner.buffer_segment(target
  1140. #if IS_KINEMATIC && DISABLED(CLASSIC_JERK)
  1141. , delta_mm_cart
  1142. #endif
  1143. , real_fr_mm_s, active_extruder
  1144. );
  1145. #endif
  1146. planner.synchronize();
  1147. if (is_home_dir) {
  1148. #if HOMING_Z_WITH_PROBE && QUIET_PROBING
  1149. if (axis == Z_AXIS) probing_pause(false);
  1150. #endif
  1151. endstops.validate_homing_move();
  1152. // Re-enable stealthChop if used. Disable diag1 pin on driver.
  1153. #if ENABLED(SENSORLESS_HOMING)
  1154. end_sensorless_homing_per_axis(axis, stealth_states);
  1155. #endif
  1156. }
  1157. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("<<< do_homing_move(", axis_codes[axis], ")");
  1158. }
  1159. /**
  1160. * Set an axis' current position to its home position (after homing).
  1161. *
  1162. * For Core and Cartesian robots this applies one-to-one when an
  1163. * individual axis has been homed.
  1164. *
  1165. * DELTA should wait until all homing is done before setting the XYZ
  1166. * current_position to home, because homing is a single operation.
  1167. * In the case where the axis positions are already known and previously
  1168. * homed, DELTA could home to X or Y individually by moving either one
  1169. * to the center. However, homing Z always homes XY and Z.
  1170. *
  1171. * SCARA should wait until all XY homing is done before setting the XY
  1172. * current_position to home, because neither X nor Y is at home until
  1173. * both are at home. Z can however be homed individually.
  1174. *
  1175. * Callers must sync the planner position after calling this!
  1176. */
  1177. void set_axis_is_at_home(const AxisEnum axis) {
  1178. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> set_axis_is_at_home(", axis_codes[axis], ")");
  1179. SBI(axis_known_position, axis);
  1180. SBI(axis_homed, axis);
  1181. #if ENABLED(DUAL_X_CARRIAGE)
  1182. if (axis == X_AXIS && (active_extruder == 1 || dual_x_carriage_mode == DXC_DUPLICATION_MODE)) {
  1183. current_position.x = x_home_pos(active_extruder);
  1184. return;
  1185. }
  1186. #endif
  1187. #if ENABLED(MORGAN_SCARA)
  1188. scara_set_axis_is_at_home(axis);
  1189. #elif ENABLED(DELTA)
  1190. current_position[axis] = (axis == Z_AXIS ? delta_height
  1191. #if HAS_BED_PROBE
  1192. - probe_offset.z
  1193. #endif
  1194. : base_home_pos(axis));
  1195. #else
  1196. current_position[axis] = base_home_pos(axis);
  1197. #endif
  1198. /**
  1199. * Z Probe Z Homing? Account for the probe's Z offset.
  1200. */
  1201. #if HAS_BED_PROBE && Z_HOME_DIR < 0
  1202. if (axis == Z_AXIS) {
  1203. #if HOMING_Z_WITH_PROBE
  1204. current_position.z -= probe_offset.z;
  1205. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("*** Z HOMED WITH PROBE (Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) ***\n> probe_offset.z = ", probe_offset.z);
  1206. #else
  1207. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("*** Z HOMED TO ENDSTOP ***");
  1208. #endif
  1209. }
  1210. #endif
  1211. #if ENABLED(I2C_POSITION_ENCODERS)
  1212. I2CPEM.homed(axis);
  1213. #endif
  1214. #if ENABLED(BABYSTEP_DISPLAY_TOTAL)
  1215. babystep.reset_total(axis);
  1216. #endif
  1217. #if HAS_POSITION_SHIFT
  1218. position_shift[axis] = 0;
  1219. update_workspace_offset(axis);
  1220. #endif
  1221. if (DEBUGGING(LEVELING)) {
  1222. #if HAS_HOME_OFFSET
  1223. DEBUG_ECHOLNPAIR("> home_offset[", axis_codes[axis], "] = ", home_offset[axis]);
  1224. #endif
  1225. DEBUG_POS("", current_position);
  1226. DEBUG_ECHOLNPAIR("<<< set_axis_is_at_home(", axis_codes[axis], ")");
  1227. }
  1228. }
  1229. /**
  1230. * Set an axis' to be unhomed.
  1231. */
  1232. void set_axis_is_not_at_home(const AxisEnum axis) {
  1233. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> set_axis_is_not_at_home(", axis_codes[axis], ")");
  1234. CBI(axis_known_position, axis);
  1235. CBI(axis_homed, axis);
  1236. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("<<< set_axis_is_not_at_home(", axis_codes[axis], ")");
  1237. #if ENABLED(I2C_POSITION_ENCODERS)
  1238. I2CPEM.unhomed(axis);
  1239. #endif
  1240. }
  1241. /**
  1242. * Home an individual "raw axis" to its endstop.
  1243. * This applies to XYZ on Cartesian and Core robots, and
  1244. * to the individual ABC steppers on DELTA and SCARA.
  1245. *
  1246. * At the end of the procedure the axis is marked as
  1247. * homed and the current position of that axis is updated.
  1248. * Kinematic robots should wait till all axes are homed
  1249. * before updating the current position.
  1250. */
  1251. void homeaxis(const AxisEnum axis) {
  1252. #if IS_SCARA
  1253. // Only Z homing (with probe) is permitted
  1254. if (axis != Z_AXIS) { BUZZ(100, 880); return; }
  1255. #else
  1256. #define _CAN_HOME(A) \
  1257. (axis == _AXIS(A) && ((A##_MIN_PIN > -1 && A##_HOME_DIR < 0) || (A##_MAX_PIN > -1 && A##_HOME_DIR > 0)))
  1258. #if X_SPI_SENSORLESS
  1259. #define CAN_HOME_X true
  1260. #else
  1261. #define CAN_HOME_X _CAN_HOME(X)
  1262. #endif
  1263. #if Y_SPI_SENSORLESS
  1264. #define CAN_HOME_Y true
  1265. #else
  1266. #define CAN_HOME_Y _CAN_HOME(Y)
  1267. #endif
  1268. #if Z_SPI_SENSORLESS
  1269. #define CAN_HOME_Z true
  1270. #else
  1271. #define CAN_HOME_Z _CAN_HOME(Z)
  1272. #endif
  1273. if (!CAN_HOME_X && !CAN_HOME_Y && !CAN_HOME_Z) return;
  1274. #endif
  1275. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR(">>> homeaxis(", axis_codes[axis], ")");
  1276. const int axis_home_dir = (
  1277. #if ENABLED(DUAL_X_CARRIAGE)
  1278. axis == X_AXIS ? x_home_dir(active_extruder) :
  1279. #endif
  1280. home_dir(axis)
  1281. );
  1282. // Homing Z towards the bed? Deploy the Z probe or endstop.
  1283. #if HOMING_Z_WITH_PROBE
  1284. if (axis == Z_AXIS && DEPLOY_PROBE()) return;
  1285. #endif
  1286. // Set flags for X, Y, Z motor locking
  1287. #if HAS_EXTRA_ENDSTOPS
  1288. switch (axis) {
  1289. #if ENABLED(X_DUAL_ENDSTOPS)
  1290. case X_AXIS:
  1291. #endif
  1292. #if ENABLED(Y_DUAL_ENDSTOPS)
  1293. case Y_AXIS:
  1294. #endif
  1295. #if ENABLED(Z_MULTI_ENDSTOPS)
  1296. case Z_AXIS:
  1297. #endif
  1298. stepper.set_separate_multi_axis(true);
  1299. default: break;
  1300. }
  1301. #endif
  1302. // Fast move towards endstop until triggered
  1303. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Home 1 Fast:");
  1304. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH)
  1305. if (axis == Z_AXIS && bltouch.deploy()) return; // The initial DEPLOY
  1306. #endif
  1307. do_homing_move(axis, 1.5f * max_length(
  1308. #if ENABLED(DELTA)
  1309. Z_AXIS
  1310. #else
  1311. axis
  1312. #endif
  1313. ) * axis_home_dir
  1314. );
  1315. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH) && DISABLED(BLTOUCH_HS_MODE)
  1316. if (axis == Z_AXIS) bltouch.stow(); // Intermediate STOW (in LOW SPEED MODE)
  1317. #endif
  1318. // When homing Z with probe respect probe clearance
  1319. const float bump = axis_home_dir * (
  1320. #if HOMING_Z_WITH_PROBE
  1321. (axis == Z_AXIS && (Z_HOME_BUMP_MM)) ? _MAX(Z_CLEARANCE_BETWEEN_PROBES, Z_HOME_BUMP_MM) :
  1322. #endif
  1323. home_bump_mm(axis)
  1324. );
  1325. // If a second homing move is configured...
  1326. if (bump) {
  1327. // Move away from the endstop by the axis HOME_BUMP_MM
  1328. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Move Away:");
  1329. do_homing_move(axis, -bump
  1330. #if HOMING_Z_WITH_PROBE
  1331. , MMM_TO_MMS(axis == Z_AXIS ? Z_PROBE_SPEED_FAST : 0)
  1332. #endif
  1333. );
  1334. // Slow move towards endstop until triggered
  1335. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Home 2 Slow:");
  1336. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH) && DISABLED(BLTOUCH_HS_MODE)
  1337. if (axis == Z_AXIS && bltouch.deploy()) return; // Intermediate DEPLOY (in LOW SPEED MODE)
  1338. #endif
  1339. do_homing_move(axis, 2 * bump, get_homing_bump_feedrate(axis));
  1340. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH)
  1341. if (axis == Z_AXIS) bltouch.stow(); // The final STOW
  1342. #endif
  1343. }
  1344. #if HAS_EXTRA_ENDSTOPS
  1345. const bool pos_dir = axis_home_dir > 0;
  1346. #if ENABLED(X_DUAL_ENDSTOPS)
  1347. if (axis == X_AXIS) {
  1348. const float adj = ABS(endstops.x2_endstop_adj);
  1349. if (adj) {
  1350. if (pos_dir ? (endstops.x2_endstop_adj > 0) : (endstops.x2_endstop_adj < 0)) stepper.set_x_lock(true); else stepper.set_x2_lock(true);
  1351. do_homing_move(axis, pos_dir ? -adj : adj);
  1352. stepper.set_x_lock(false);
  1353. stepper.set_x2_lock(false);
  1354. }
  1355. }
  1356. #endif
  1357. #if ENABLED(Y_DUAL_ENDSTOPS)
  1358. if (axis == Y_AXIS) {
  1359. const float adj = ABS(endstops.y2_endstop_adj);
  1360. if (adj) {
  1361. if (pos_dir ? (endstops.y2_endstop_adj > 0) : (endstops.y2_endstop_adj < 0)) stepper.set_y_lock(true); else stepper.set_y2_lock(true);
  1362. do_homing_move(axis, pos_dir ? -adj : adj);
  1363. stepper.set_y_lock(false);
  1364. stepper.set_y2_lock(false);
  1365. }
  1366. }
  1367. #endif
  1368. #if ENABLED(Z_MULTI_ENDSTOPS)
  1369. if (axis == Z_AXIS) {
  1370. #if NUM_Z_STEPPER_DRIVERS == 2
  1371. const float adj = ABS(endstops.z2_endstop_adj);
  1372. if (adj) {
  1373. if (pos_dir ? (endstops.z2_endstop_adj > 0) : (endstops.z2_endstop_adj < 0)) stepper.set_z_lock(true); else stepper.set_z2_lock(true);
  1374. do_homing_move(axis, pos_dir ? -adj : adj);
  1375. stepper.set_z_lock(false);
  1376. stepper.set_z2_lock(false);
  1377. }
  1378. #else
  1379. // Handy arrays of stepper lock function pointers
  1380. typedef void (*adjustFunc_t)(const bool);
  1381. adjustFunc_t lock[] = {
  1382. stepper.set_z_lock, stepper.set_z2_lock, stepper.set_z3_lock
  1383. #if NUM_Z_STEPPER_DRIVERS >= 4
  1384. , stepper.set_z4_lock
  1385. #endif
  1386. };
  1387. float adj[] = {
  1388. 0, endstops.z2_endstop_adj, endstops.z3_endstop_adj
  1389. #if NUM_Z_STEPPER_DRIVERS >= 4
  1390. , endstops.z4_endstop_adj
  1391. #endif
  1392. };
  1393. adjustFunc_t tempLock;
  1394. float tempAdj;
  1395. // Manual bubble sort by adjust value
  1396. if (adj[1] < adj[0]) {
  1397. tempLock = lock[0], tempAdj = adj[0];
  1398. lock[0] = lock[1], adj[0] = adj[1];
  1399. lock[1] = tempLock, adj[1] = tempAdj;
  1400. }
  1401. if (adj[2] < adj[1]) {
  1402. tempLock = lock[1], tempAdj = adj[1];
  1403. lock[1] = lock[2], adj[1] = adj[2];
  1404. lock[2] = tempLock, adj[2] = tempAdj;
  1405. }
  1406. #if NUM_Z_STEPPER_DRIVERS >= 4
  1407. if (adj[3] < adj[2]) {
  1408. tempLock = lock[2], tempAdj = adj[2];
  1409. lock[2] = lock[3], adj[2] = adj[3];
  1410. lock[3] = tempLock, adj[3] = tempAdj;
  1411. }
  1412. if (adj[2] < adj[1]) {
  1413. tempLock = lock[1], tempAdj = adj[1];
  1414. lock[1] = lock[2], adj[1] = adj[2];
  1415. lock[2] = tempLock, adj[2] = tempAdj;
  1416. }
  1417. #endif
  1418. if (adj[1] < adj[0]) {
  1419. tempLock = lock[0], tempAdj = adj[0];
  1420. lock[0] = lock[1], adj[0] = adj[1];
  1421. lock[1] = tempLock, adj[1] = tempAdj;
  1422. }
  1423. if (pos_dir) {
  1424. // normalize adj to smallest value and do the first move
  1425. (*lock[0])(true);
  1426. do_homing_move(axis, adj[1] - adj[0]);
  1427. // lock the second stepper for the final correction
  1428. (*lock[1])(true);
  1429. do_homing_move(axis, adj[2] - adj[1]);
  1430. #if NUM_Z_STEPPER_DRIVERS >= 4
  1431. // lock the third stepper for the final correction
  1432. (*lock[2])(true);
  1433. do_homing_move(axis, adj[3] - adj[2]);
  1434. #endif
  1435. }
  1436. else {
  1437. #if NUM_Z_STEPPER_DRIVERS >= 4
  1438. (*lock[3])(true);
  1439. do_homing_move(axis, adj[2] - adj[3]);
  1440. #endif
  1441. (*lock[2])(true);
  1442. do_homing_move(axis, adj[1] - adj[2]);
  1443. (*lock[1])(true);
  1444. do_homing_move(axis, adj[0] - adj[1]);
  1445. }
  1446. stepper.set_z_lock(false);
  1447. stepper.set_z2_lock(false);
  1448. stepper.set_z3_lock(false);
  1449. #if NUM_Z_STEPPER_DRIVERS >= 4
  1450. stepper.set_z4_lock(false);
  1451. #endif
  1452. #endif
  1453. }
  1454. #endif
  1455. // Reset flags for X, Y, Z motor locking
  1456. switch (axis) {
  1457. default: break;
  1458. #if ENABLED(X_DUAL_ENDSTOPS)
  1459. case X_AXIS:
  1460. #endif
  1461. #if ENABLED(Y_DUAL_ENDSTOPS)
  1462. case Y_AXIS:
  1463. #endif
  1464. #if ENABLED(Z_MULTI_ENDSTOPS)
  1465. case Z_AXIS:
  1466. #endif
  1467. stepper.set_separate_multi_axis(false);
  1468. }
  1469. #endif
  1470. #if IS_SCARA
  1471. set_axis_is_at_home(axis);
  1472. sync_plan_position();
  1473. #elif ENABLED(DELTA)
  1474. // Delta has already moved all three towers up in G28
  1475. // so here it re-homes each tower in turn.
  1476. // Delta homing treats the axes as normal linear axes.
  1477. // retrace by the amount specified in delta_endstop_adj + additional dist in order to have minimum steps
  1478. if (delta_endstop_adj[axis] * Z_HOME_DIR <= 0) {
  1479. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("delta_endstop_adj:");
  1480. do_homing_move(axis, delta_endstop_adj[axis] - (MIN_STEPS_PER_SEGMENT + 1) * planner.steps_to_mm[axis] * Z_HOME_DIR);
  1481. }
  1482. #else // CARTESIAN / CORE
  1483. set_axis_is_at_home(axis);
  1484. sync_plan_position();
  1485. destination[axis] = current_position[axis];
  1486. if (DEBUGGING(LEVELING)) DEBUG_POS("> AFTER set_axis_is_at_home", current_position);
  1487. #endif
  1488. // Put away the Z probe
  1489. #if HOMING_Z_WITH_PROBE
  1490. if (axis == Z_AXIS && STOW_PROBE()) return;
  1491. #endif
  1492. #ifdef HOMING_BACKOFF_MM
  1493. constexpr xyz_float_t endstop_backoff = HOMING_BACKOFF_MM;
  1494. const float backoff_mm = endstop_backoff[
  1495. #if ENABLED(DELTA)
  1496. Z_AXIS
  1497. #else
  1498. axis
  1499. #endif
  1500. ];
  1501. if (backoff_mm) {
  1502. current_position[axis] -= ABS(backoff_mm) * axis_home_dir;
  1503. line_to_current_position(
  1504. #if HOMING_Z_WITH_PROBE
  1505. (axis == Z_AXIS) ? MMM_TO_MMS(Z_PROBE_SPEED_FAST) :
  1506. #endif
  1507. homing_feedrate(axis)
  1508. );
  1509. }
  1510. #endif
  1511. // Clear retracted status if homing the Z axis
  1512. #if ENABLED(FWRETRACT)
  1513. if (axis == Z_AXIS) fwretract.current_hop = 0.0;
  1514. #endif
  1515. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("<<< homeaxis(", axis_codes[axis], ")");
  1516. } // homeaxis()
  1517. #if HAS_WORKSPACE_OFFSET
  1518. void update_workspace_offset(const AxisEnum axis) {
  1519. workspace_offset[axis] = home_offset[axis] + position_shift[axis];
  1520. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Axis ", axis_codes[axis], " home_offset = ", home_offset[axis], " position_shift = ", position_shift[axis]);
  1521. }
  1522. #endif
  1523. #if HAS_M206_COMMAND
  1524. /**
  1525. * Change the home offset for an axis.
  1526. * Also refreshes the workspace offset.
  1527. */
  1528. void set_home_offset(const AxisEnum axis, const float v) {
  1529. home_offset[axis] = v;
  1530. update_workspace_offset(axis);
  1531. }
  1532. #endif // HAS_M206_COMMAND