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

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