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

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