My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

motion.cpp 54KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673
  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) || ENABLED(EXTENSIBLE_UI))
  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[XYZ] 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)
  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) || ENABLED(EXTENSIBLE_UI)
  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, false, 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 AXIS_HAS_STALLGUARD(X2)
  907. stealth_states.x2 = tmc_enable_stallguard(stepperX2);
  908. #endif
  909. #if CORE_IS_XY && Y_SENSORLESS
  910. stealth_states.y = tmc_enable_stallguard(stepperY);
  911. #elif CORE_IS_XZ && Z_SENSORLESS
  912. stealth_states.z = tmc_enable_stallguard(stepperZ);
  913. #endif
  914. break;
  915. #endif
  916. #if Y_SENSORLESS
  917. case Y_AXIS:
  918. stealth_states.y = tmc_enable_stallguard(stepperY);
  919. #if AXIS_HAS_STALLGUARD(Y2)
  920. stealth_states.y2 = tmc_enable_stallguard(stepperY2);
  921. #endif
  922. #if CORE_IS_XY && X_SENSORLESS
  923. stealth_states.x = tmc_enable_stallguard(stepperX);
  924. #elif CORE_IS_YZ && Z_SENSORLESS
  925. stealth_states.z = tmc_enable_stallguard(stepperZ);
  926. #endif
  927. break;
  928. #endif
  929. #if Z_SENSORLESS
  930. case Z_AXIS:
  931. stealth_states.z = tmc_enable_stallguard(stepperZ);
  932. #if AXIS_HAS_STALLGUARD(Z2)
  933. stealth_states.z2 = tmc_enable_stallguard(stepperZ2);
  934. #endif
  935. #if AXIS_HAS_STALLGUARD(Z3)
  936. stealth_states.z3 = tmc_enable_stallguard(stepperZ3);
  937. #endif
  938. #if CORE_IS_XZ && X_SENSORLESS
  939. stealth_states.x = tmc_enable_stallguard(stepperX);
  940. #elif CORE_IS_YZ && Y_SENSORLESS
  941. stealth_states.y = tmc_enable_stallguard(stepperY);
  942. #endif
  943. break;
  944. #endif
  945. }
  946. return stealth_states;
  947. }
  948. void end_sensorless_homing_per_axis(const AxisEnum axis, sensorless_t enable_stealth) {
  949. switch (axis) {
  950. default: break;
  951. #if X_SENSORLESS
  952. case X_AXIS:
  953. tmc_disable_stallguard(stepperX, enable_stealth.x);
  954. #if AXIS_HAS_STALLGUARD(X2)
  955. tmc_disable_stallguard(stepperX2, enable_stealth.x2);
  956. #endif
  957. #if CORE_IS_XY && Y_SENSORLESS
  958. tmc_disable_stallguard(stepperY, enable_stealth.y);
  959. #elif CORE_IS_XZ && Z_SENSORLESS
  960. tmc_disable_stallguard(stepperZ, enable_stealth.z);
  961. #endif
  962. break;
  963. #endif
  964. #if Y_SENSORLESS
  965. case Y_AXIS:
  966. tmc_disable_stallguard(stepperY, enable_stealth.y);
  967. #if AXIS_HAS_STALLGUARD(Y2)
  968. tmc_disable_stallguard(stepperY2, enable_stealth.y2);
  969. #endif
  970. #if CORE_IS_XY && X_SENSORLESS
  971. tmc_disable_stallguard(stepperX, enable_stealth.x);
  972. #elif CORE_IS_YZ && Z_SENSORLESS
  973. tmc_disable_stallguard(stepperZ, enable_stealth.z);
  974. #endif
  975. break;
  976. #endif
  977. #if Z_SENSORLESS
  978. case Z_AXIS:
  979. tmc_disable_stallguard(stepperZ, enable_stealth.z);
  980. #if AXIS_HAS_STALLGUARD(Z2)
  981. tmc_disable_stallguard(stepperZ2, enable_stealth.z2);
  982. #endif
  983. #if AXIS_HAS_STALLGUARD(Z3)
  984. tmc_disable_stallguard(stepperZ3, enable_stealth.z3);
  985. #endif
  986. #if CORE_IS_XZ && X_SENSORLESS
  987. tmc_disable_stallguard(stepperX, enable_stealth.x);
  988. #elif CORE_IS_YZ && Y_SENSORLESS
  989. tmc_disable_stallguard(stepperY, enable_stealth.y);
  990. #endif
  991. break;
  992. #endif
  993. }
  994. }
  995. #endif // SENSORLESS_HOMING
  996. /**
  997. * Home an individual linear axis
  998. */
  999. void do_homing_move(const AxisEnum axis, const float distance, const float fr_mm_s=0.0) {
  1000. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1001. if (DEBUGGING(LEVELING)) {
  1002. SERIAL_ECHOPAIR(">>> do_homing_move(", axis_codes[axis]);
  1003. SERIAL_ECHOPAIR(", ", distance);
  1004. SERIAL_ECHOPGM(", ");
  1005. if (fr_mm_s)
  1006. SERIAL_ECHO(fr_mm_s);
  1007. else {
  1008. SERIAL_ECHOPAIR("[", homing_feedrate(axis));
  1009. SERIAL_CHAR(']');
  1010. }
  1011. SERIAL_ECHOLNPGM(")");
  1012. }
  1013. #endif
  1014. #if HOMING_Z_WITH_PROBE && HAS_HEATED_BED && ENABLED(WAIT_FOR_BED_HEATER)
  1015. // Wait for bed to heat back up between probing points
  1016. if (axis == Z_AXIS && distance < 0 && thermalManager.isHeatingBed()) {
  1017. serialprintPGM(msg_wait_for_bed_heating);
  1018. LCD_MESSAGEPGM(MSG_BED_HEATING);
  1019. thermalManager.wait_for_bed();
  1020. ui.reset_status();
  1021. }
  1022. #endif
  1023. // Only do some things when moving towards an endstop
  1024. const int8_t axis_home_dir =
  1025. #if ENABLED(DUAL_X_CARRIAGE)
  1026. (axis == X_AXIS) ? x_home_dir(active_extruder) :
  1027. #endif
  1028. home_dir(axis);
  1029. const bool is_home_dir = (axis_home_dir > 0) == (distance > 0);
  1030. #if ENABLED(SENSORLESS_HOMING)
  1031. sensorless_t stealth_states;
  1032. #endif
  1033. if (is_home_dir) {
  1034. #if HOMING_Z_WITH_PROBE && QUIET_PROBING
  1035. if (axis == Z_AXIS) probing_pause(true);
  1036. #endif
  1037. // Disable stealthChop if used. Enable diag1 pin on driver.
  1038. #if ENABLED(SENSORLESS_HOMING)
  1039. stealth_states = start_sensorless_homing_per_axis(axis);
  1040. #endif
  1041. }
  1042. #if IS_SCARA
  1043. // Tell the planner the axis is at 0
  1044. current_position[axis] = 0;
  1045. sync_plan_position();
  1046. current_position[axis] = distance;
  1047. planner.buffer_line(current_position, fr_mm_s ? fr_mm_s : homing_feedrate(axis), active_extruder);
  1048. #else
  1049. 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) };
  1050. target[axis] = 0;
  1051. planner.set_machine_position_mm(target);
  1052. target[axis] = distance;
  1053. #if IS_KINEMATIC && ENABLED(JUNCTION_DEVIATION)
  1054. const float delta_mm_cart[XYZE] = {0, 0, 0, 0};
  1055. #endif
  1056. // Set delta/cartesian axes directly
  1057. planner.buffer_segment(target
  1058. #if IS_KINEMATIC && ENABLED(JUNCTION_DEVIATION)
  1059. , delta_mm_cart
  1060. #endif
  1061. , fr_mm_s ? fr_mm_s : homing_feedrate(axis), active_extruder
  1062. );
  1063. #endif
  1064. planner.synchronize();
  1065. if (is_home_dir) {
  1066. #if HOMING_Z_WITH_PROBE && QUIET_PROBING
  1067. if (axis == Z_AXIS) probing_pause(false);
  1068. #endif
  1069. endstops.validate_homing_move();
  1070. // Re-enable stealthChop if used. Disable diag1 pin on driver.
  1071. #if ENABLED(SENSORLESS_HOMING)
  1072. end_sensorless_homing_per_axis(axis, stealth_states);
  1073. #endif
  1074. }
  1075. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1076. if (DEBUGGING(LEVELING)) {
  1077. SERIAL_ECHOPAIR("<<< do_homing_move(", axis_codes[axis]);
  1078. SERIAL_CHAR(')');
  1079. SERIAL_EOL();
  1080. }
  1081. #endif
  1082. }
  1083. /**
  1084. * Set an axis' current position to its home position (after homing).
  1085. *
  1086. * For Core and Cartesian robots this applies one-to-one when an
  1087. * individual axis has been homed.
  1088. *
  1089. * DELTA should wait until all homing is done before setting the XYZ
  1090. * current_position to home, because homing is a single operation.
  1091. * In the case where the axis positions are already known and previously
  1092. * homed, DELTA could home to X or Y individually by moving either one
  1093. * to the center. However, homing Z always homes XY and Z.
  1094. *
  1095. * SCARA should wait until all XY homing is done before setting the XY
  1096. * current_position to home, because neither X nor Y is at home until
  1097. * both are at home. Z can however be homed individually.
  1098. *
  1099. * Callers must sync the planner position after calling this!
  1100. */
  1101. void set_axis_is_at_home(const AxisEnum axis) {
  1102. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1103. if (DEBUGGING(LEVELING)) {
  1104. SERIAL_ECHOPAIR(">>> set_axis_is_at_home(", axis_codes[axis]);
  1105. SERIAL_CHAR(')');
  1106. SERIAL_EOL();
  1107. }
  1108. #endif
  1109. SBI(axis_known_position, axis);
  1110. SBI(axis_homed, axis);
  1111. #if HAS_POSITION_SHIFT
  1112. position_shift[axis] = 0;
  1113. update_workspace_offset(axis);
  1114. #endif
  1115. #if ENABLED(DUAL_X_CARRIAGE)
  1116. if (axis == X_AXIS && (active_extruder == 1 || dual_x_carriage_mode == DXC_DUPLICATION_MODE)) {
  1117. current_position[X_AXIS] = x_home_pos(active_extruder);
  1118. return;
  1119. }
  1120. #endif
  1121. #if ENABLED(MORGAN_SCARA)
  1122. scara_set_axis_is_at_home(axis);
  1123. #elif ENABLED(DELTA)
  1124. current_position[axis] = (axis == Z_AXIS ? delta_height
  1125. #if HAS_BED_PROBE
  1126. - zprobe_zoffset
  1127. #endif
  1128. : base_home_pos(axis));
  1129. #else
  1130. current_position[axis] = base_home_pos(axis);
  1131. #endif
  1132. /**
  1133. * Z Probe Z Homing? Account for the probe's Z offset.
  1134. */
  1135. #if HAS_BED_PROBE && Z_HOME_DIR < 0
  1136. if (axis == Z_AXIS) {
  1137. #if HOMING_Z_WITH_PROBE
  1138. current_position[Z_AXIS] -= zprobe_zoffset;
  1139. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1140. if (DEBUGGING(LEVELING)) {
  1141. SERIAL_ECHOLNPGM("*** Z HOMED WITH PROBE (Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) ***");
  1142. SERIAL_ECHOLNPAIR("> zprobe_zoffset = ", zprobe_zoffset);
  1143. }
  1144. #endif
  1145. #elif ENABLED(DEBUG_LEVELING_FEATURE)
  1146. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("*** Z HOMED TO ENDSTOP (Z_MIN_PROBE_ENDSTOP) ***");
  1147. #endif
  1148. }
  1149. #endif
  1150. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1151. if (DEBUGGING(LEVELING)) {
  1152. #if HAS_HOME_OFFSET
  1153. SERIAL_ECHOPAIR("> home_offset[", axis_codes[axis]);
  1154. SERIAL_ECHOLNPAIR("] = ", home_offset[axis]);
  1155. #endif
  1156. DEBUG_POS("", current_position);
  1157. SERIAL_ECHOPAIR("<<< set_axis_is_at_home(", axis_codes[axis]);
  1158. SERIAL_CHAR(')');
  1159. SERIAL_EOL();
  1160. }
  1161. #endif
  1162. #if ENABLED(I2C_POSITION_ENCODERS)
  1163. I2CPEM.homed(axis);
  1164. #endif
  1165. }
  1166. /**
  1167. * Set an axis' to be unhomed.
  1168. */
  1169. void set_axis_is_not_at_home(const AxisEnum axis) {
  1170. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1171. if (DEBUGGING(LEVELING)) {
  1172. SERIAL_ECHOPAIR(">>> set_axis_is_not_at_home(", axis_codes[axis]);
  1173. SERIAL_CHAR(')');
  1174. SERIAL_EOL();
  1175. }
  1176. #endif
  1177. CBI(axis_known_position, axis);
  1178. CBI(axis_homed, axis);
  1179. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1180. if (DEBUGGING(LEVELING)) {
  1181. SERIAL_ECHOPAIR("<<< set_axis_is_not_at_home(", axis_codes[axis]);
  1182. SERIAL_CHAR(')');
  1183. SERIAL_EOL();
  1184. }
  1185. #endif
  1186. #if ENABLED(I2C_POSITION_ENCODERS)
  1187. I2CPEM.unhomed(axis);
  1188. #endif
  1189. }
  1190. /**
  1191. * Home an individual "raw axis" to its endstop.
  1192. * This applies to XYZ on Cartesian and Core robots, and
  1193. * to the individual ABC steppers on DELTA and SCARA.
  1194. *
  1195. * At the end of the procedure the axis is marked as
  1196. * homed and the current position of that axis is updated.
  1197. * Kinematic robots should wait till all axes are homed
  1198. * before updating the current position.
  1199. */
  1200. void homeaxis(const AxisEnum axis) {
  1201. #if IS_SCARA
  1202. // Only Z homing (with probe) is permitted
  1203. if (axis != Z_AXIS) { BUZZ(100, 880); return; }
  1204. #else
  1205. #define CAN_HOME(A) \
  1206. (axis == _AXIS(A) && ((A##_MIN_PIN > -1 && A##_HOME_DIR < 0) || (A##_MAX_PIN > -1 && A##_HOME_DIR > 0)))
  1207. if (!CAN_HOME(X) && !CAN_HOME(Y) && !CAN_HOME(Z)) return;
  1208. #endif
  1209. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1210. if (DEBUGGING(LEVELING)) {
  1211. SERIAL_ECHOPAIR(">>> homeaxis(", axis_codes[axis]);
  1212. SERIAL_CHAR(')');
  1213. SERIAL_EOL();
  1214. }
  1215. #endif
  1216. const int axis_home_dir = (
  1217. #if ENABLED(DUAL_X_CARRIAGE)
  1218. axis == X_AXIS ? x_home_dir(active_extruder) :
  1219. #endif
  1220. home_dir(axis)
  1221. );
  1222. // Homing Z towards the bed? Deploy the Z probe or endstop.
  1223. #if HOMING_Z_WITH_PROBE
  1224. if (axis == Z_AXIS && DEPLOY_PROBE()) return;
  1225. #endif
  1226. // Set flags for X, Y, Z motor locking
  1227. #if HAS_EXTRA_ENDSTOPS
  1228. switch (axis) {
  1229. #if ENABLED(X_DUAL_ENDSTOPS)
  1230. case X_AXIS:
  1231. #endif
  1232. #if ENABLED(Y_DUAL_ENDSTOPS)
  1233. case Y_AXIS:
  1234. #endif
  1235. #if Z_MULTI_ENDSTOPS
  1236. case Z_AXIS:
  1237. #endif
  1238. stepper.set_separate_multi_axis(true);
  1239. default: break;
  1240. }
  1241. #endif
  1242. // Fast move towards endstop until triggered
  1243. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1244. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Home 1 Fast:");
  1245. #endif
  1246. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH)
  1247. // BLTOUCH needs to be deployed every time
  1248. if (axis == Z_AXIS && set_bltouch_deployed(true)) return;
  1249. #endif
  1250. do_homing_move(axis, 1.5f * max_length(
  1251. #if ENABLED(DELTA)
  1252. Z_AXIS
  1253. #else
  1254. axis
  1255. #endif
  1256. ) * axis_home_dir
  1257. );
  1258. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH)
  1259. // BLTOUCH needs to be stowed after trigger to rearm itself
  1260. if (axis == Z_AXIS) set_bltouch_deployed(false);
  1261. #endif
  1262. // When homing Z with probe respect probe clearance
  1263. const float bump = axis_home_dir * (
  1264. #if HOMING_Z_WITH_PROBE
  1265. (axis == Z_AXIS && (Z_HOME_BUMP_MM)) ? MAX(Z_CLEARANCE_BETWEEN_PROBES, Z_HOME_BUMP_MM) :
  1266. #endif
  1267. home_bump_mm(axis)
  1268. );
  1269. // If a second homing move is configured...
  1270. if (bump) {
  1271. // Move away from the endstop by the axis HOME_BUMP_MM
  1272. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1273. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Move Away:");
  1274. #endif
  1275. do_homing_move(axis, -bump
  1276. #if HOMING_Z_WITH_PROBE
  1277. , axis == Z_AXIS ? MMM_TO_MMS(Z_PROBE_SPEED_FAST) : 0.0
  1278. #endif
  1279. );
  1280. // Slow move towards endstop until triggered
  1281. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1282. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Home 2 Slow:");
  1283. #endif
  1284. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH)
  1285. // BLTOUCH needs to be deployed every time
  1286. if (axis == Z_AXIS && set_bltouch_deployed(true)) return;
  1287. #endif
  1288. do_homing_move(axis, 2 * bump, get_homing_bump_feedrate(axis));
  1289. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH)
  1290. // BLTOUCH needs to be stowed after trigger to rearm itself
  1291. if (axis == Z_AXIS) set_bltouch_deployed(false);
  1292. #endif
  1293. }
  1294. #if HAS_EXTRA_ENDSTOPS
  1295. const bool pos_dir = axis_home_dir > 0;
  1296. #if ENABLED(X_DUAL_ENDSTOPS)
  1297. if (axis == X_AXIS) {
  1298. const float adj = ABS(endstops.x2_endstop_adj);
  1299. if (adj) {
  1300. if (pos_dir ? (endstops.x2_endstop_adj > 0) : (endstops.x2_endstop_adj < 0)) stepper.set_x_lock(true); else stepper.set_x2_lock(true);
  1301. do_homing_move(axis, pos_dir ? -adj : adj);
  1302. stepper.set_x_lock(false);
  1303. stepper.set_x2_lock(false);
  1304. }
  1305. }
  1306. #endif
  1307. #if ENABLED(Y_DUAL_ENDSTOPS)
  1308. if (axis == Y_AXIS) {
  1309. const float adj = ABS(endstops.y2_endstop_adj);
  1310. if (adj) {
  1311. if (pos_dir ? (endstops.y2_endstop_adj > 0) : (endstops.y2_endstop_adj < 0)) stepper.set_y_lock(true); else stepper.set_y2_lock(true);
  1312. do_homing_move(axis, pos_dir ? -adj : adj);
  1313. stepper.set_y_lock(false);
  1314. stepper.set_y2_lock(false);
  1315. }
  1316. }
  1317. #endif
  1318. #if ENABLED(Z_DUAL_ENDSTOPS)
  1319. if (axis == Z_AXIS) {
  1320. const float adj = ABS(endstops.z2_endstop_adj);
  1321. if (adj) {
  1322. if (pos_dir ? (endstops.z2_endstop_adj > 0) : (endstops.z2_endstop_adj < 0)) stepper.set_z_lock(true); else stepper.set_z2_lock(true);
  1323. do_homing_move(axis, pos_dir ? -adj : adj);
  1324. stepper.set_z_lock(false);
  1325. stepper.set_z2_lock(false);
  1326. }
  1327. }
  1328. #endif
  1329. #if ENABLED(Z_TRIPLE_ENDSTOPS)
  1330. if (axis == Z_AXIS) {
  1331. // we push the function pointers for the stepper lock function into an array
  1332. void (*lock[3]) (bool)= {&stepper.set_z_lock, &stepper.set_z2_lock, &stepper.set_z3_lock};
  1333. float adj[3] = {0, endstops.z2_endstop_adj, endstops.z3_endstop_adj};
  1334. void (*tempLock) (bool);
  1335. float tempAdj;
  1336. // manual bubble sort by adjust value
  1337. if (adj[1] < adj[0]) {
  1338. tempLock = lock[0], tempAdj = adj[0];
  1339. lock[0] = lock[1], adj[0] = adj[1];
  1340. lock[1] = tempLock, adj[1] = tempAdj;
  1341. }
  1342. if (adj[2] < adj[1]) {
  1343. tempLock = lock[1], tempAdj = adj[1];
  1344. lock[1] = lock[2], adj[1] = adj[2];
  1345. lock[2] = tempLock, adj[2] = tempAdj;
  1346. }
  1347. if (adj[1] < adj[0]) {
  1348. tempLock = lock[0], tempAdj = adj[0];
  1349. lock[0] = lock[1], adj[0] = adj[1];
  1350. lock[1] = tempLock, adj[1] = tempAdj;
  1351. }
  1352. if (pos_dir) {
  1353. // normalize adj to smallest value and do the first move
  1354. (*lock[0])(true);
  1355. do_homing_move(axis, adj[1] - adj[0]);
  1356. // lock the second stepper for the final correction
  1357. (*lock[1])(true);
  1358. do_homing_move(axis, adj[2] - adj[1]);
  1359. }
  1360. else {
  1361. (*lock[2])(true);
  1362. do_homing_move(axis, adj[1] - adj[2]);
  1363. (*lock[1])(true);
  1364. do_homing_move(axis, adj[0] - adj[1]);
  1365. }
  1366. stepper.set_z_lock(false);
  1367. stepper.set_z2_lock(false);
  1368. stepper.set_z3_lock(false);
  1369. }
  1370. #endif
  1371. // Reset flags for X, Y, Z motor locking
  1372. switch (axis) {
  1373. #if ENABLED(X_DUAL_ENDSTOPS)
  1374. case X_AXIS:
  1375. #endif
  1376. #if ENABLED(Y_DUAL_ENDSTOPS)
  1377. case Y_AXIS:
  1378. #endif
  1379. #if Z_MULTI_ENDSTOPS
  1380. case Z_AXIS:
  1381. #endif
  1382. stepper.set_separate_multi_axis(false);
  1383. default: break;
  1384. }
  1385. #endif
  1386. #if IS_SCARA
  1387. set_axis_is_at_home(axis);
  1388. sync_plan_position();
  1389. #elif ENABLED(DELTA)
  1390. // Delta has already moved all three towers up in G28
  1391. // so here it re-homes each tower in turn.
  1392. // Delta homing treats the axes as normal linear axes.
  1393. // retrace by the amount specified in delta_endstop_adj + additional dist in order to have minimum steps
  1394. if (delta_endstop_adj[axis] * Z_HOME_DIR <= 0) {
  1395. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1396. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("delta_endstop_adj:");
  1397. #endif
  1398. do_homing_move(axis, delta_endstop_adj[axis] - (MIN_STEPS_PER_SEGMENT + 1) * planner.steps_to_mm[axis] * Z_HOME_DIR);
  1399. }
  1400. #else // CARTESIAN / CORE
  1401. set_axis_is_at_home(axis);
  1402. sync_plan_position();
  1403. destination[axis] = current_position[axis];
  1404. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1405. if (DEBUGGING(LEVELING)) DEBUG_POS("> AFTER set_axis_is_at_home", current_position);
  1406. #endif
  1407. #endif
  1408. // Put away the Z probe
  1409. #if HOMING_Z_WITH_PROBE
  1410. if (axis == Z_AXIS && STOW_PROBE()) return;
  1411. #endif
  1412. // Clear retracted status if homing the Z axis
  1413. #if ENABLED(FWRETRACT)
  1414. if (axis == Z_AXIS) fwretract.current_hop = 0.0;
  1415. #endif
  1416. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1417. if (DEBUGGING(LEVELING)) {
  1418. SERIAL_ECHOPAIR("<<< homeaxis(", axis_codes[axis]);
  1419. SERIAL_CHAR(')');
  1420. SERIAL_EOL();
  1421. }
  1422. #endif
  1423. } // homeaxis()
  1424. #if HAS_WORKSPACE_OFFSET
  1425. void update_workspace_offset(const AxisEnum axis) {
  1426. workspace_offset[axis] = home_offset[axis] + position_shift[axis];
  1427. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1428. if (DEBUGGING(LEVELING)) {
  1429. SERIAL_ECHOPAIR("For ", axis_codes[axis]);
  1430. SERIAL_ECHOPAIR(" axis:\n home_offset = ", home_offset[axis]);
  1431. SERIAL_ECHOLNPAIR("\n position_shift = ", position_shift[axis]);
  1432. }
  1433. #endif
  1434. }
  1435. #endif
  1436. #if HAS_M206_COMMAND
  1437. /**
  1438. * Change the home offset for an axis.
  1439. * Also refreshes the workspace offset.
  1440. */
  1441. void set_home_offset(const AxisEnum axis, const float v) {
  1442. home_offset[axis] = v;
  1443. update_workspace_offset(axis);
  1444. }
  1445. #endif // HAS_M206_COMMAND