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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  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. // Relative Mode. Enable with G91, disable with G90.
  59. bool relative_mode = false;
  60. /**
  61. * Cartesian Current Position
  62. * Used to track the native machine position as moves are queued.
  63. * Used by 'buffer_line_to_current_position' to do a move after changing it.
  64. * Used by 'SYNC_PLAN_POSITION_KINEMATIC' to update 'planner.position'.
  65. */
  66. float current_position[XYZE] = { 0.0 };
  67. /**
  68. * Cartesian Destination
  69. * The destination for a move, filled in by G-code movement commands,
  70. * and expected by functions like 'prepare_move_to_destination'.
  71. * Set with 'gcode_get_destination' or 'set_destination_from_current'.
  72. */
  73. float destination[XYZE] = { 0.0 };
  74. // The active extruder (tool). Set with T<extruder> command.
  75. uint8_t active_extruder = 0;
  76. // Extruder offsets
  77. #if HOTENDS > 1
  78. float hotend_offset[XYZ][HOTENDS]; // Initialized by settings.load()
  79. #endif
  80. // The feedrate for the current move, often used as the default if
  81. // no other feedrate is specified. Overridden for special moves.
  82. // Set by the last G0 through G5 command's "F" parameter.
  83. // Functions that override this for custom moves *must always* restore it!
  84. float feedrate_mm_s = MMM_TO_MMS(1500.0);
  85. int16_t feedrate_percentage = 100;
  86. // Homing feedrate is const progmem - compare to constexpr in the header
  87. const float homing_feedrate_mm_s[4] PROGMEM = {
  88. #if ENABLED(DELTA)
  89. MMM_TO_MMS(HOMING_FEEDRATE_Z), MMM_TO_MMS(HOMING_FEEDRATE_Z),
  90. #else
  91. MMM_TO_MMS(HOMING_FEEDRATE_XY), MMM_TO_MMS(HOMING_FEEDRATE_XY),
  92. #endif
  93. MMM_TO_MMS(HOMING_FEEDRATE_Z), 0
  94. };
  95. // Cartesian conversion result goes here:
  96. float cartes[XYZ];
  97. // Until kinematics.cpp is created, create this here
  98. #if IS_KINEMATIC
  99. float delta[ABC];
  100. #endif
  101. /**
  102. * The workspace can be offset by some commands, or
  103. * these offsets may be omitted to save on computation.
  104. */
  105. #if HAS_WORKSPACE_OFFSET
  106. #if HAS_POSITION_SHIFT
  107. // The distance that XYZ has been offset by G92. Reset by G28.
  108. float position_shift[XYZ] = { 0 };
  109. #endif
  110. #if HAS_HOME_OFFSET
  111. // This offset is added to the configured home position.
  112. // Set by M206, M428, or menu item. Saved to EEPROM.
  113. float home_offset[XYZ] = { 0 };
  114. #endif
  115. #if HAS_HOME_OFFSET && HAS_POSITION_SHIFT
  116. // The above two are combined to save on computes
  117. float workspace_offset[XYZ] = { 0 };
  118. #endif
  119. #endif
  120. #if OLDSCHOOL_ABL
  121. float xy_probe_feedrate_mm_s = MMM_TO_MMS(XY_PROBE_SPEED);
  122. #endif
  123. /**
  124. * Output the current position to serial
  125. */
  126. void report_current_position() {
  127. SERIAL_PROTOCOLPGM("X:");
  128. SERIAL_PROTOCOL(LOGICAL_X_POSITION(current_position[X_AXIS]));
  129. SERIAL_PROTOCOLPGM(" Y:");
  130. SERIAL_PROTOCOL(LOGICAL_Y_POSITION(current_position[Y_AXIS]));
  131. SERIAL_PROTOCOLPGM(" Z:");
  132. SERIAL_PROTOCOL(LOGICAL_Z_POSITION(current_position[Z_AXIS]));
  133. SERIAL_PROTOCOLPGM(" E:");
  134. SERIAL_PROTOCOL(current_position[E_AXIS]);
  135. stepper.report_positions();
  136. #if IS_SCARA
  137. scara_report_positions();
  138. #endif
  139. }
  140. /**
  141. * sync_plan_position
  142. *
  143. * Set the planner/stepper positions directly from current_position with
  144. * no kinematic translation. Used for homing axes and cartesian/core syncing.
  145. */
  146. void sync_plan_position() {
  147. #if ENABLED(DEBUG_LEVELING_FEATURE)
  148. if (DEBUGGING(LEVELING)) DEBUG_POS("sync_plan_position", current_position);
  149. #endif
  150. planner.set_position_mm(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  151. }
  152. void sync_plan_position_e() { planner.set_e_position_mm(current_position[E_AXIS]); }
  153. /**
  154. * Get the stepper positions in the cartes[] array.
  155. * Forward kinematics are applied for DELTA and SCARA.
  156. *
  157. * The result is in the current coordinate space with
  158. * leveling applied. The coordinates need to be run through
  159. * unapply_leveling to obtain the "ideal" coordinates
  160. * suitable for current_position, etc.
  161. */
  162. void get_cartesian_from_steppers() {
  163. #if ENABLED(DELTA)
  164. forward_kinematics_DELTA(
  165. stepper.get_axis_position_mm(A_AXIS),
  166. stepper.get_axis_position_mm(B_AXIS),
  167. stepper.get_axis_position_mm(C_AXIS)
  168. );
  169. #else
  170. #if IS_SCARA
  171. forward_kinematics_SCARA(
  172. stepper.get_axis_position_degrees(A_AXIS),
  173. stepper.get_axis_position_degrees(B_AXIS)
  174. );
  175. #else
  176. cartes[X_AXIS] = stepper.get_axis_position_mm(X_AXIS);
  177. cartes[Y_AXIS] = stepper.get_axis_position_mm(Y_AXIS);
  178. #endif
  179. cartes[Z_AXIS] = stepper.get_axis_position_mm(Z_AXIS);
  180. #endif
  181. }
  182. /**
  183. * Set the current_position for an axis based on
  184. * the stepper positions, removing any leveling that
  185. * may have been applied.
  186. *
  187. * To prevent small shifts in axis position always call
  188. * SYNC_PLAN_POSITION_KINEMATIC after updating axes with this.
  189. *
  190. * To keep hosts in sync, always call report_current_position
  191. * after updating the current_position.
  192. */
  193. void set_current_from_steppers_for_axis(const AxisEnum axis) {
  194. get_cartesian_from_steppers();
  195. #if PLANNER_LEVELING
  196. planner.unapply_leveling(cartes);
  197. #endif
  198. if (axis == ALL_AXES)
  199. COPY(current_position, cartes);
  200. else
  201. current_position[axis] = cartes[axis];
  202. }
  203. /**
  204. * Move the planner to the current position from wherever it last moved
  205. * (or from wherever it has been told it is located).
  206. */
  207. void line_to_current_position() {
  208. planner.buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate_mm_s, active_extruder);
  209. }
  210. /**
  211. * Move the planner to the position stored in the destination array, which is
  212. * used by G0/G1/G2/G3/G5 and many other functions to set a destination.
  213. */
  214. void buffer_line_to_destination(const float fr_mm_s) {
  215. planner.buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], fr_mm_s, active_extruder);
  216. }
  217. #if IS_KINEMATIC
  218. void sync_plan_position_kinematic() {
  219. #if ENABLED(DEBUG_LEVELING_FEATURE)
  220. if (DEBUGGING(LEVELING)) DEBUG_POS("sync_plan_position_kinematic", current_position);
  221. #endif
  222. planner.set_position_mm_kinematic(current_position);
  223. }
  224. /**
  225. * Calculate delta, start a line, and set current_position to destination
  226. */
  227. void prepare_uninterpolated_move_to_destination(const float fr_mm_s/*=0.0*/) {
  228. #if ENABLED(DEBUG_LEVELING_FEATURE)
  229. if (DEBUGGING(LEVELING)) DEBUG_POS("prepare_uninterpolated_move_to_destination", destination);
  230. #endif
  231. gcode.refresh_cmd_timeout();
  232. #if UBL_SEGMENTED
  233. // ubl segmented line will do z-only moves in single segment
  234. ubl.prepare_segmented_line_to(destination, MMS_SCALED(fr_mm_s ? fr_mm_s : feedrate_mm_s));
  235. #else
  236. if ( current_position[X_AXIS] == destination[X_AXIS]
  237. && current_position[Y_AXIS] == destination[Y_AXIS]
  238. && current_position[Z_AXIS] == destination[Z_AXIS]
  239. && current_position[E_AXIS] == destination[E_AXIS]
  240. ) return;
  241. planner.buffer_line_kinematic(destination, MMS_SCALED(fr_mm_s ? fr_mm_s : feedrate_mm_s), active_extruder);
  242. #endif
  243. set_current_from_destination();
  244. }
  245. #endif // IS_KINEMATIC
  246. /**
  247. * Plan a move to (X, Y, Z) and set the current_position
  248. * The final current_position may not be the one that was requested
  249. */
  250. void do_blocking_move_to(const float &rx, const float &ry, const float &rz, const float &fr_mm_s/*=0.0*/) {
  251. const float old_feedrate_mm_s = feedrate_mm_s;
  252. #if ENABLED(DEBUG_LEVELING_FEATURE)
  253. if (DEBUGGING(LEVELING)) print_xyz(PSTR(">>> do_blocking_move_to"), NULL, rx, ry, rz);
  254. #endif
  255. const float z_feedrate = fr_mm_s ? fr_mm_s : homing_feedrate(Z_AXIS);
  256. #if ENABLED(DELTA)
  257. if (!position_is_reachable(rx, ry)) return;
  258. feedrate_mm_s = fr_mm_s ? fr_mm_s : XY_PROBE_FEEDRATE_MM_S;
  259. set_destination_from_current(); // sync destination at the start
  260. #if ENABLED(DEBUG_LEVELING_FEATURE)
  261. if (DEBUGGING(LEVELING)) DEBUG_POS("set_destination_from_current", destination);
  262. #endif
  263. // when in the danger zone
  264. if (current_position[Z_AXIS] > delta_clip_start_height) {
  265. if (rz > delta_clip_start_height) { // staying in the danger zone
  266. destination[X_AXIS] = rx; // move directly (uninterpolated)
  267. destination[Y_AXIS] = ry;
  268. destination[Z_AXIS] = rz;
  269. prepare_uninterpolated_move_to_destination(); // set_current_from_destination()
  270. #if ENABLED(DEBUG_LEVELING_FEATURE)
  271. if (DEBUGGING(LEVELING)) DEBUG_POS("danger zone move", current_position);
  272. #endif
  273. return;
  274. }
  275. destination[Z_AXIS] = delta_clip_start_height;
  276. prepare_uninterpolated_move_to_destination(); // set_current_from_destination()
  277. #if ENABLED(DEBUG_LEVELING_FEATURE)
  278. if (DEBUGGING(LEVELING)) DEBUG_POS("zone border move", current_position);
  279. #endif
  280. }
  281. if (rz > current_position[Z_AXIS]) { // raising?
  282. destination[Z_AXIS] = rz;
  283. prepare_uninterpolated_move_to_destination(z_feedrate); // set_current_from_destination()
  284. #if ENABLED(DEBUG_LEVELING_FEATURE)
  285. if (DEBUGGING(LEVELING)) DEBUG_POS("z raise move", current_position);
  286. #endif
  287. }
  288. destination[X_AXIS] = rx;
  289. destination[Y_AXIS] = ry;
  290. prepare_move_to_destination(); // set_current_from_destination()
  291. #if ENABLED(DEBUG_LEVELING_FEATURE)
  292. if (DEBUGGING(LEVELING)) DEBUG_POS("xy move", current_position);
  293. #endif
  294. if (rz < current_position[Z_AXIS]) { // lowering?
  295. destination[Z_AXIS] = rz;
  296. prepare_uninterpolated_move_to_destination(z_feedrate); // set_current_from_destination()
  297. #if ENABLED(DEBUG_LEVELING_FEATURE)
  298. if (DEBUGGING(LEVELING)) DEBUG_POS("z lower move", current_position);
  299. #endif
  300. }
  301. #elif IS_SCARA
  302. if (!position_is_reachable(rx, ry)) return;
  303. set_destination_from_current();
  304. // If Z needs to raise, do it before moving XY
  305. if (destination[Z_AXIS] < rz) {
  306. destination[Z_AXIS] = rz;
  307. prepare_uninterpolated_move_to_destination(z_feedrate);
  308. }
  309. destination[X_AXIS] = rx;
  310. destination[Y_AXIS] = ry;
  311. prepare_uninterpolated_move_to_destination(fr_mm_s ? fr_mm_s : XY_PROBE_FEEDRATE_MM_S);
  312. // If Z needs to lower, do it after moving XY
  313. if (destination[Z_AXIS] > rz) {
  314. destination[Z_AXIS] = rz;
  315. prepare_uninterpolated_move_to_destination(z_feedrate);
  316. }
  317. #else
  318. // If Z needs to raise, do it before moving XY
  319. if (current_position[Z_AXIS] < rz) {
  320. feedrate_mm_s = z_feedrate;
  321. current_position[Z_AXIS] = rz;
  322. line_to_current_position();
  323. }
  324. feedrate_mm_s = fr_mm_s ? fr_mm_s : XY_PROBE_FEEDRATE_MM_S;
  325. current_position[X_AXIS] = rx;
  326. current_position[Y_AXIS] = ry;
  327. line_to_current_position();
  328. // If Z needs to lower, do it after moving XY
  329. if (current_position[Z_AXIS] > rz) {
  330. feedrate_mm_s = z_feedrate;
  331. current_position[Z_AXIS] = rz;
  332. line_to_current_position();
  333. }
  334. #endif
  335. stepper.synchronize();
  336. feedrate_mm_s = old_feedrate_mm_s;
  337. #if ENABLED(DEBUG_LEVELING_FEATURE)
  338. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("<<< do_blocking_move_to");
  339. #endif
  340. }
  341. void do_blocking_move_to_x(const float &rx, const float &fr_mm_s/*=0.0*/) {
  342. do_blocking_move_to(rx, current_position[Y_AXIS], current_position[Z_AXIS], fr_mm_s);
  343. }
  344. void do_blocking_move_to_z(const float &rz, const float &fr_mm_s/*=0.0*/) {
  345. do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], rz, fr_mm_s);
  346. }
  347. void do_blocking_move_to_xy(const float &rx, const float &ry, const float &fr_mm_s/*=0.0*/) {
  348. do_blocking_move_to(rx, ry, current_position[Z_AXIS], fr_mm_s);
  349. }
  350. //
  351. // Prepare to do endstop or probe moves
  352. // with custom feedrates.
  353. //
  354. // - Save current feedrates
  355. // - Reset the rate multiplier
  356. // - Reset the command timeout
  357. // - Enable the endstops (for endstop moves)
  358. //
  359. void bracket_probe_move(const bool before) {
  360. static float saved_feedrate_mm_s;
  361. static int16_t saved_feedrate_percentage;
  362. #if ENABLED(DEBUG_LEVELING_FEATURE)
  363. if (DEBUGGING(LEVELING)) DEBUG_POS("bracket_probe_move", current_position);
  364. #endif
  365. if (before) {
  366. saved_feedrate_mm_s = feedrate_mm_s;
  367. saved_feedrate_percentage = feedrate_percentage;
  368. feedrate_percentage = 100;
  369. gcode.refresh_cmd_timeout();
  370. }
  371. else {
  372. feedrate_mm_s = saved_feedrate_mm_s;
  373. feedrate_percentage = saved_feedrate_percentage;
  374. gcode.refresh_cmd_timeout();
  375. }
  376. }
  377. void setup_for_endstop_or_probe_move() { bracket_probe_move(true); }
  378. void clean_up_after_endstop_or_probe_move() { bracket_probe_move(false); }
  379. // Software Endstops are based on the configured limits.
  380. float soft_endstop_min[XYZ] = { X_MIN_BED, Y_MIN_BED, Z_MIN_POS },
  381. soft_endstop_max[XYZ] = { X_MAX_BED, Y_MAX_BED, Z_MAX_POS };
  382. #if HAS_SOFTWARE_ENDSTOPS
  383. // Software Endstops are based on the configured limits.
  384. bool soft_endstops_enabled = true;
  385. #if IS_KINEMATIC
  386. float soft_endstop_radius, soft_endstop_radius_2;
  387. #endif
  388. /**
  389. * Constrain the given coordinates to the software endstops.
  390. *
  391. * For DELTA/SCARA the XY constraint is based on the smallest
  392. * radius within the set software endstops.
  393. */
  394. void clamp_to_software_endstops(float target[XYZ]) {
  395. if (!soft_endstops_enabled) return;
  396. #if IS_KINEMATIC
  397. const float dist_2 = HYPOT2(target[X_AXIS], target[Y_AXIS]);
  398. if (dist_2 > soft_endstop_radius_2) {
  399. const float ratio = soft_endstop_radius / SQRT(dist_2); // 200 / 300 = 0.66
  400. target[X_AXIS] *= ratio;
  401. target[Y_AXIS] *= ratio;
  402. }
  403. #else
  404. #if ENABLED(MIN_SOFTWARE_ENDSTOP_X)
  405. NOLESS(target[X_AXIS], soft_endstop_min[X_AXIS]);
  406. #endif
  407. #if ENABLED(MIN_SOFTWARE_ENDSTOP_Y)
  408. NOLESS(target[Y_AXIS], soft_endstop_min[Y_AXIS]);
  409. #endif
  410. #if ENABLED(MAX_SOFTWARE_ENDSTOP_X)
  411. NOMORE(target[X_AXIS], soft_endstop_max[X_AXIS]);
  412. #endif
  413. #if ENABLED(MAX_SOFTWARE_ENDSTOP_Y)
  414. NOMORE(target[Y_AXIS], soft_endstop_max[Y_AXIS]);
  415. #endif
  416. #endif
  417. #if ENABLED(MIN_SOFTWARE_ENDSTOP_Z)
  418. NOLESS(target[Z_AXIS], soft_endstop_min[Z_AXIS]);
  419. #endif
  420. #if ENABLED(MAX_SOFTWARE_ENDSTOP_Z)
  421. NOMORE(target[Z_AXIS], soft_endstop_max[Z_AXIS]);
  422. #endif
  423. }
  424. #endif
  425. #if !UBL_SEGMENTED
  426. #if IS_KINEMATIC
  427. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  428. #if ENABLED(DELTA)
  429. #define ADJUST_DELTA(V) \
  430. if (planner.leveling_active) { \
  431. const float zadj = bilinear_z_offset(V); \
  432. delta[A_AXIS] += zadj; \
  433. delta[B_AXIS] += zadj; \
  434. delta[C_AXIS] += zadj; \
  435. }
  436. #else
  437. #define ADJUST_DELTA(V) if (planner.leveling_active) { delta[Z_AXIS] += bilinear_z_offset(V); }
  438. #endif
  439. #else
  440. #define ADJUST_DELTA(V) NOOP
  441. #endif
  442. /**
  443. * Prepare a linear move in a DELTA or SCARA setup.
  444. *
  445. * Called from prepare_move_to_destination as the
  446. * default Delta/SCARA segmenter.
  447. *
  448. * This calls planner.buffer_line several times, adding
  449. * small incremental moves for DELTA or SCARA.
  450. *
  451. * For Unified Bed Leveling (Delta or Segmented Cartesian)
  452. * the ubl.prepare_segmented_line_to method replaces this.
  453. *
  454. * For Auto Bed Leveling (Bilinear) with SEGMENT_LEVELED_MOVES
  455. * this is replaced by segmented_line_to_destination below.
  456. */
  457. inline bool prepare_kinematic_move_to(const float (&rtarget)[XYZE]) {
  458. // Get the top feedrate of the move in the XY plane
  459. const float _feedrate_mm_s = MMS_SCALED(feedrate_mm_s);
  460. const float xdiff = rtarget[X_AXIS] - current_position[X_AXIS],
  461. ydiff = rtarget[Y_AXIS] - current_position[Y_AXIS];
  462. // If the move is only in Z/E don't split up the move
  463. if (!xdiff && !ydiff) {
  464. planner.buffer_line_kinematic(rtarget, _feedrate_mm_s, active_extruder);
  465. return false; // caller will update current_position
  466. }
  467. // Fail if attempting move outside printable radius
  468. if (!position_is_reachable(rtarget[X_AXIS], rtarget[Y_AXIS])) return true;
  469. // Remaining cartesian distances
  470. const float zdiff = rtarget[Z_AXIS] - current_position[Z_AXIS],
  471. ediff = rtarget[E_AXIS] - current_position[E_AXIS];
  472. // Get the linear distance in XYZ
  473. float cartesian_mm = SQRT(sq(xdiff) + sq(ydiff) + sq(zdiff));
  474. // If the move is very short, check the E move distance
  475. if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = FABS(ediff);
  476. // No E move either? Game over.
  477. if (UNEAR_ZERO(cartesian_mm)) return true;
  478. // Minimum number of seconds to move the given distance
  479. const float seconds = cartesian_mm / _feedrate_mm_s;
  480. // The number of segments-per-second times the duration
  481. // gives the number of segments
  482. uint16_t segments = delta_segments_per_second * seconds;
  483. // For SCARA minimum segment size is 0.25mm
  484. #if IS_SCARA
  485. NOMORE(segments, cartesian_mm * 4);
  486. #endif
  487. // At least one segment is required
  488. NOLESS(segments, 1);
  489. // The approximate length of each segment
  490. const float inv_segments = 1.0 / float(segments),
  491. cartesian_segment_mm = cartesian_mm * inv_segments,
  492. segment_distance[XYZE] = {
  493. xdiff * inv_segments,
  494. ydiff * inv_segments,
  495. zdiff * inv_segments,
  496. ediff * inv_segments
  497. };
  498. // SERIAL_ECHOPAIR("mm=", cartesian_mm);
  499. // SERIAL_ECHOPAIR(" seconds=", seconds);
  500. // SERIAL_ECHOLNPAIR(" segments=", segments);
  501. // SERIAL_ECHOLNPAIR(" segment_mm=", cartesian_segment_mm);
  502. #if ENABLED(SCARA_FEEDRATE_SCALING)
  503. // SCARA needs to scale the feed rate from mm/s to degrees/s
  504. const float inv_segment_length = min(10.0, float(segments) / cartesian_mm), // 1/mm/segs
  505. inverse_secs = inv_segment_length * _feedrate_mm_s;
  506. float oldA = stepper.get_axis_position_degrees(A_AXIS),
  507. oldB = stepper.get_axis_position_degrees(B_AXIS);
  508. #endif
  509. // Get the current position as starting point
  510. float raw[XYZE];
  511. COPY(raw, current_position);
  512. // Calculate and execute the segments
  513. while (--segments) {
  514. static millis_t next_idle_ms = millis() + 200UL;
  515. thermalManager.manage_heater(); // This returns immediately if not really needed.
  516. if (ELAPSED(millis(), next_idle_ms)) {
  517. next_idle_ms = millis() + 200UL;
  518. idle();
  519. }
  520. LOOP_XYZE(i) raw[i] += segment_distance[i];
  521. #if ENABLED(DELTA)
  522. DELTA_IK(raw); // Delta can inline its kinematics
  523. #else
  524. inverse_kinematics(raw);
  525. #endif
  526. ADJUST_DELTA(raw); // Adjust Z if bed leveling is enabled
  527. #if ENABLED(SCARA_FEEDRATE_SCALING)
  528. // For SCARA scale the feed rate from mm/s to degrees/s
  529. // i.e., Complete the angular vector in the given time.
  530. planner.buffer_segment(delta[A_AXIS], delta[B_AXIS], raw[Z_AXIS], raw[E_AXIS], HYPOT(delta[A_AXIS] - oldA, delta[B_AXIS] - oldB) * inverse_secs, active_extruder, cartesian_segment_mm);
  531. oldA = delta[A_AXIS]; oldB = delta[B_AXIS];
  532. #else
  533. planner.buffer_line(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], raw[E_AXIS], _feedrate_mm_s, active_extruder, cartesian_segment_mm);
  534. #endif
  535. }
  536. // Ensure last segment arrives at target location.
  537. #if ENABLED(SCARA_FEEDRATE_SCALING)
  538. inverse_kinematics(rtarget);
  539. ADJUST_DELTA(rtarget);
  540. planner.buffer_segment(delta[A_AXIS], delta[B_AXIS], rtarget[Z_AXIS], rtarget[E_AXIS], HYPOT(delta[A_AXIS] - oldA, delta[B_AXIS] - oldB) * inverse_secs, active_extruder, cartesian_segment_mm);
  541. #else
  542. planner.buffer_line_kinematic(rtarget, _feedrate_mm_s, active_extruder, cartesian_segment_mm);
  543. #endif
  544. return false; // caller will update current_position
  545. }
  546. #else // !IS_KINEMATIC
  547. #if ENABLED(SEGMENT_LEVELED_MOVES)
  548. /**
  549. * Prepare a segmented move on a CARTESIAN setup.
  550. *
  551. * This calls planner.buffer_line several times, adding
  552. * small incremental moves. This allows the planner to
  553. * apply more detailed bed leveling to the full move.
  554. */
  555. inline void segmented_line_to_destination(const float &fr_mm_s, const float segment_size=LEVELED_SEGMENT_LENGTH) {
  556. const float xdiff = destination[X_AXIS] - current_position[X_AXIS],
  557. ydiff = destination[Y_AXIS] - current_position[Y_AXIS];
  558. // If the move is only in Z/E don't split up the move
  559. if (!xdiff && !ydiff) {
  560. planner.buffer_line_kinematic(destination, fr_mm_s, active_extruder);
  561. return;
  562. }
  563. // Remaining cartesian distances
  564. const float zdiff = destination[Z_AXIS] - current_position[Z_AXIS],
  565. ediff = destination[E_AXIS] - current_position[E_AXIS];
  566. // Get the linear distance in XYZ
  567. // If the move is very short, check the E move distance
  568. // No E move either? Game over.
  569. float cartesian_mm = SQRT(sq(xdiff) + sq(ydiff) + sq(zdiff));
  570. if (UNEAR_ZERO(cartesian_mm)) cartesian_mm = FABS(ediff);
  571. if (UNEAR_ZERO(cartesian_mm)) return;
  572. // The length divided by the segment size
  573. // At least one segment is required
  574. uint16_t segments = cartesian_mm / segment_size;
  575. NOLESS(segments, 1);
  576. // The approximate length of each segment
  577. const float inv_segments = 1.0 / float(segments),
  578. cartesian_segment_mm = cartesian_mm * inv_segments,
  579. segment_distance[XYZE] = {
  580. xdiff * inv_segments,
  581. ydiff * inv_segments,
  582. zdiff * inv_segments,
  583. ediff * inv_segments
  584. };
  585. // SERIAL_ECHOPAIR("mm=", cartesian_mm);
  586. // SERIAL_ECHOLNPAIR(" segments=", segments);
  587. // SERIAL_ECHOLNPAIR(" segment_mm=", cartesian_segment_mm);
  588. // Get the raw current position as starting point
  589. float raw[XYZE];
  590. COPY(raw, current_position);
  591. // Calculate and execute the segments
  592. while (--segments) {
  593. static millis_t next_idle_ms = millis() + 200UL;
  594. thermalManager.manage_heater(); // This returns immediately if not really needed.
  595. if (ELAPSED(millis(), next_idle_ms)) {
  596. next_idle_ms = millis() + 200UL;
  597. idle();
  598. }
  599. LOOP_XYZE(i) raw[i] += segment_distance[i];
  600. planner.buffer_line_kinematic(raw, fr_mm_s, active_extruder, cartesian_segment_mm);
  601. }
  602. // Since segment_distance is only approximate,
  603. // the final move must be to the exact destination.
  604. planner.buffer_line_kinematic(destination, fr_mm_s, active_extruder, cartesian_segment_mm);
  605. }
  606. #endif // SEGMENT_LEVELED_MOVES
  607. /**
  608. * Prepare a linear move in a Cartesian setup.
  609. *
  610. * When a mesh-based leveling system is active, moves are segmented
  611. * according to the configuration of the leveling system.
  612. *
  613. * Returns true if current_position[] was set to destination[]
  614. */
  615. inline bool prepare_move_to_destination_cartesian() {
  616. #if HAS_MESH
  617. if (planner.leveling_active && planner.leveling_active_at_z(destination[Z_AXIS])) {
  618. #if ENABLED(AUTO_BED_LEVELING_UBL)
  619. ubl.line_to_destination_cartesian(MMS_SCALED(feedrate_mm_s), active_extruder); // UBL's motion routine needs to know about
  620. return true; // all moves, including Z-only moves.
  621. #elif ENABLED(SEGMENT_LEVELED_MOVES)
  622. segmented_line_to_destination(MMS_SCALED(feedrate_mm_s));
  623. return false; // caller will update current_position
  624. #else
  625. /**
  626. * For MBL and ABL-BILINEAR only segment moves when X or Y are involved.
  627. * Otherwise fall through to do a direct single move.
  628. */
  629. if (current_position[X_AXIS] != destination[X_AXIS] || current_position[Y_AXIS] != destination[Y_AXIS]) {
  630. #if ENABLED(MESH_BED_LEVELING)
  631. mesh_line_to_destination(MMS_SCALED(feedrate_mm_s));
  632. #elif ENABLED(AUTO_BED_LEVELING_BILINEAR)
  633. bilinear_line_to_destination(MMS_SCALED(feedrate_mm_s));
  634. #endif
  635. return true;
  636. }
  637. #endif
  638. }
  639. #endif // HAS_MESH
  640. buffer_line_to_destination(MMS_SCALED(feedrate_mm_s));
  641. return false; // caller will update current_position
  642. }
  643. #endif // !IS_KINEMATIC
  644. #endif // !UBL_SEGMENTED
  645. #if ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE)
  646. bool extruder_duplication_enabled = false; // Used in Dual X mode 2
  647. #endif
  648. #if ENABLED(DUAL_X_CARRIAGE)
  649. DualXMode dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  650. float inactive_extruder_x_pos = X2_MAX_POS, // used in mode 0 & 1
  651. raised_parked_position[XYZE], // used in mode 1
  652. duplicate_extruder_x_offset = DEFAULT_DUPLICATION_X_OFFSET; // used in mode 2
  653. bool active_extruder_parked = false; // used in mode 1 & 2
  654. millis_t delayed_move_time = 0; // used in mode 1
  655. int16_t duplicate_extruder_temp_offset = 0; // used in mode 2
  656. float x_home_pos(const int extruder) {
  657. if (extruder == 0)
  658. return base_home_pos(X_AXIS);
  659. else
  660. /**
  661. * In dual carriage mode the extruder offset provides an override of the
  662. * second X-carriage position when homed - otherwise X2_HOME_POS is used.
  663. * This allows soft recalibration of the second extruder home position
  664. * without firmware reflash (through the M218 command).
  665. */
  666. return hotend_offset[X_AXIS][1] > 0 ? hotend_offset[X_AXIS][1] : X2_HOME_POS;
  667. }
  668. /**
  669. * Prepare a linear move in a dual X axis setup
  670. *
  671. * Return true if current_position[] was set to destination[]
  672. */
  673. inline bool dual_x_carriage_unpark() {
  674. if (active_extruder_parked) {
  675. switch (dual_x_carriage_mode) {
  676. case DXC_FULL_CONTROL_MODE:
  677. break;
  678. case DXC_AUTO_PARK_MODE:
  679. if (current_position[E_AXIS] == destination[E_AXIS]) {
  680. // This is a travel move (with no extrusion)
  681. // Skip it, but keep track of the current position
  682. // (so it can be used as the start of the next non-travel move)
  683. if (delayed_move_time != 0xFFFFFFFFUL) {
  684. set_current_from_destination();
  685. NOLESS(raised_parked_position[Z_AXIS], destination[Z_AXIS]);
  686. delayed_move_time = millis();
  687. return true;
  688. }
  689. }
  690. // unpark extruder: 1) raise, 2) move into starting XY position, 3) lower
  691. for (uint8_t i = 0; i < 3; i++)
  692. planner.buffer_line(
  693. i == 0 ? raised_parked_position[X_AXIS] : current_position[X_AXIS],
  694. i == 0 ? raised_parked_position[Y_AXIS] : current_position[Y_AXIS],
  695. i == 2 ? current_position[Z_AXIS] : raised_parked_position[Z_AXIS],
  696. current_position[E_AXIS],
  697. i == 1 ? PLANNER_XY_FEEDRATE() : planner.max_feedrate_mm_s[Z_AXIS],
  698. active_extruder
  699. );
  700. delayed_move_time = 0;
  701. active_extruder_parked = false;
  702. #if ENABLED(DEBUG_LEVELING_FEATURE)
  703. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Clear active_extruder_parked");
  704. #endif
  705. break;
  706. case DXC_DUPLICATION_MODE:
  707. if (active_extruder == 0) {
  708. #if ENABLED(DEBUG_LEVELING_FEATURE)
  709. if (DEBUGGING(LEVELING)) {
  710. SERIAL_ECHOPAIR("Set planner X", inactive_extruder_x_pos);
  711. SERIAL_ECHOLNPAIR(" ... Line to X", current_position[X_AXIS] + duplicate_extruder_x_offset);
  712. }
  713. #endif
  714. // move duplicate extruder into correct duplication position.
  715. planner.set_position_mm(
  716. inactive_extruder_x_pos,
  717. current_position[Y_AXIS],
  718. current_position[Z_AXIS],
  719. current_position[E_AXIS]
  720. );
  721. planner.buffer_line(
  722. current_position[X_AXIS] + duplicate_extruder_x_offset,
  723. current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS],
  724. planner.max_feedrate_mm_s[X_AXIS], 1
  725. );
  726. SYNC_PLAN_POSITION_KINEMATIC();
  727. stepper.synchronize();
  728. extruder_duplication_enabled = true;
  729. active_extruder_parked = false;
  730. #if ENABLED(DEBUG_LEVELING_FEATURE)
  731. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Set extruder_duplication_enabled\nClear active_extruder_parked");
  732. #endif
  733. }
  734. else {
  735. #if ENABLED(DEBUG_LEVELING_FEATURE)
  736. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Active extruder not 0");
  737. #endif
  738. }
  739. break;
  740. }
  741. }
  742. return false;
  743. }
  744. #endif // DUAL_X_CARRIAGE
  745. /**
  746. * Prepare a single move and get ready for the next one
  747. *
  748. * This may result in several calls to planner.buffer_line to
  749. * do smaller moves for DELTA, SCARA, mesh moves, etc.
  750. *
  751. * Make sure current_position[E] and destination[E] are good
  752. * before calling or cold/lengthy extrusion may get missed.
  753. */
  754. void prepare_move_to_destination() {
  755. clamp_to_software_endstops(destination);
  756. gcode.refresh_cmd_timeout();
  757. #if ENABLED(PREVENT_COLD_EXTRUSION) || ENABLED(PREVENT_LENGTHY_EXTRUDE)
  758. if (!DEBUGGING(DRYRUN)) {
  759. if (destination[E_AXIS] != current_position[E_AXIS]) {
  760. #if ENABLED(PREVENT_COLD_EXTRUSION)
  761. if (thermalManager.tooColdToExtrude(active_extruder)) {
  762. current_position[E_AXIS] = destination[E_AXIS]; // Behave as if the move really took place, but ignore E part
  763. SERIAL_ECHO_START();
  764. SERIAL_ECHOLNPGM(MSG_ERR_COLD_EXTRUDE_STOP);
  765. }
  766. #endif // PREVENT_COLD_EXTRUSION
  767. #if ENABLED(PREVENT_LENGTHY_EXTRUDE)
  768. if (FABS(destination[E_AXIS] - current_position[E_AXIS]) * planner.e_factor[active_extruder] > (EXTRUDE_MAXLENGTH)) {
  769. current_position[E_AXIS] = destination[E_AXIS]; // Behave as if the move really took place, but ignore E part
  770. SERIAL_ECHO_START();
  771. SERIAL_ECHOLNPGM(MSG_ERR_LONG_EXTRUDE_STOP);
  772. }
  773. #endif // PREVENT_LENGTHY_EXTRUDE
  774. }
  775. }
  776. #endif // PREVENT_COLD_EXTRUSION || PREVENT_LENGTHY_EXTRUDE
  777. #if ENABLED(DUAL_X_CARRIAGE)
  778. if (dual_x_carriage_unpark()) return;
  779. #endif
  780. if (
  781. #if UBL_SEGMENTED
  782. ubl.prepare_segmented_line_to(destination, MMS_SCALED(feedrate_mm_s))
  783. #elif IS_KINEMATIC
  784. prepare_kinematic_move_to(destination)
  785. #else
  786. prepare_move_to_destination_cartesian()
  787. #endif
  788. ) return;
  789. set_current_from_destination();
  790. }
  791. #if HAS_AXIS_UNHOMED_ERR
  792. bool axis_unhomed_error(const bool x/*=true*/, const bool y/*=true*/, const bool z/*=true*/) {
  793. #if ENABLED(HOME_AFTER_DEACTIVATE)
  794. const bool xx = x && !axis_known_position[X_AXIS],
  795. yy = y && !axis_known_position[Y_AXIS],
  796. zz = z && !axis_known_position[Z_AXIS];
  797. #else
  798. const bool xx = x && !axis_homed[X_AXIS],
  799. yy = y && !axis_homed[Y_AXIS],
  800. zz = z && !axis_homed[Z_AXIS];
  801. #endif
  802. if (xx || yy || zz) {
  803. SERIAL_ECHO_START();
  804. SERIAL_ECHOPGM(MSG_HOME " ");
  805. if (xx) SERIAL_ECHOPGM(MSG_X);
  806. if (yy) SERIAL_ECHOPGM(MSG_Y);
  807. if (zz) SERIAL_ECHOPGM(MSG_Z);
  808. SERIAL_ECHOLNPGM(" " MSG_FIRST);
  809. #if ENABLED(ULTRA_LCD)
  810. lcd_status_printf_P(0, PSTR(MSG_HOME " %s%s%s " MSG_FIRST), xx ? MSG_X : "", yy ? MSG_Y : "", zz ? MSG_Z : "");
  811. #endif
  812. return true;
  813. }
  814. return false;
  815. }
  816. #endif // HAS_AXIS_UNHOMED_ERR
  817. /**
  818. * The homing feedrate may vary
  819. */
  820. inline float get_homing_bump_feedrate(const AxisEnum axis) {
  821. static const uint8_t homing_bump_divisor[] PROGMEM = HOMING_BUMP_DIVISOR;
  822. uint8_t hbd = pgm_read_byte(&homing_bump_divisor[axis]);
  823. if (hbd < 1) {
  824. hbd = 10;
  825. SERIAL_ECHO_START();
  826. SERIAL_ECHOLNPGM("Warning: Homing Bump Divisor < 1");
  827. }
  828. return homing_feedrate(axis) / hbd;
  829. }
  830. /**
  831. * Home an individual linear axis
  832. */
  833. static void do_homing_move(const AxisEnum axis, const float distance, const float fr_mm_s=0.0) {
  834. #if ENABLED(DEBUG_LEVELING_FEATURE)
  835. if (DEBUGGING(LEVELING)) {
  836. SERIAL_ECHOPAIR(">>> do_homing_move(", axis_codes[axis]);
  837. SERIAL_ECHOPAIR(", ", distance);
  838. SERIAL_ECHOPAIR(", ", fr_mm_s);
  839. SERIAL_CHAR(')');
  840. SERIAL_EOL();
  841. }
  842. #endif
  843. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH)
  844. const bool deploy_bltouch = (axis == Z_AXIS && distance < 0);
  845. if (deploy_bltouch) set_bltouch_deployed(true);
  846. #endif
  847. #if QUIET_PROBING
  848. if (axis == Z_AXIS) probing_pause(true);
  849. #endif
  850. // Tell the planner the axis is at 0
  851. current_position[axis] = 0;
  852. #if IS_SCARA
  853. SYNC_PLAN_POSITION_KINEMATIC();
  854. current_position[axis] = distance;
  855. inverse_kinematics(current_position);
  856. planner.buffer_line(delta[A_AXIS], delta[B_AXIS], delta[C_AXIS], current_position[E_AXIS], fr_mm_s ? fr_mm_s : homing_feedrate(axis), active_extruder);
  857. #else
  858. sync_plan_position();
  859. current_position[axis] = distance;
  860. planner.buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], fr_mm_s ? fr_mm_s : homing_feedrate(axis), active_extruder);
  861. #endif
  862. stepper.synchronize();
  863. #if QUIET_PROBING
  864. if (axis == Z_AXIS) probing_pause(false);
  865. #endif
  866. #if HOMING_Z_WITH_PROBE && ENABLED(BLTOUCH)
  867. if (deploy_bltouch) set_bltouch_deployed(false);
  868. #endif
  869. endstops.hit_on_purpose();
  870. #if ENABLED(DEBUG_LEVELING_FEATURE)
  871. if (DEBUGGING(LEVELING)) {
  872. SERIAL_ECHOPAIR("<<< do_homing_move(", axis_codes[axis]);
  873. SERIAL_CHAR(')');
  874. SERIAL_EOL();
  875. }
  876. #endif
  877. }
  878. /**
  879. * Set an axis' current position to its home position (after homing).
  880. *
  881. * For Core and Cartesian robots this applies one-to-one when an
  882. * individual axis has been homed.
  883. *
  884. * DELTA should wait until all homing is done before setting the XYZ
  885. * current_position to home, because homing is a single operation.
  886. * In the case where the axis positions are already known and previously
  887. * homed, DELTA could home to X or Y individually by moving either one
  888. * to the center. However, homing Z always homes XY and Z.
  889. *
  890. * SCARA should wait until all XY homing is done before setting the XY
  891. * current_position to home, because neither X nor Y is at home until
  892. * both are at home. Z can however be homed individually.
  893. *
  894. * Callers must sync the planner position after calling this!
  895. */
  896. void set_axis_is_at_home(const AxisEnum axis) {
  897. #if ENABLED(DEBUG_LEVELING_FEATURE)
  898. if (DEBUGGING(LEVELING)) {
  899. SERIAL_ECHOPAIR(">>> set_axis_is_at_home(", axis_codes[axis]);
  900. SERIAL_CHAR(')');
  901. SERIAL_EOL();
  902. }
  903. #endif
  904. axis_known_position[axis] = axis_homed[axis] = true;
  905. #if HAS_POSITION_SHIFT
  906. position_shift[axis] = 0;
  907. update_software_endstops(axis);
  908. #endif
  909. #if ENABLED(DUAL_X_CARRIAGE)
  910. if (axis == X_AXIS && (active_extruder == 1 || dual_x_carriage_mode == DXC_DUPLICATION_MODE)) {
  911. current_position[X_AXIS] = x_home_pos(active_extruder);
  912. return;
  913. }
  914. #endif
  915. #if ENABLED(MORGAN_SCARA)
  916. scara_set_axis_is_at_home(axis);
  917. #elif ENABLED(DELTA)
  918. current_position[axis] = (axis == Z_AXIS ? delta_height : base_home_pos(axis));
  919. #else
  920. current_position[axis] = base_home_pos(axis);
  921. #endif
  922. /**
  923. * Z Probe Z Homing? Account for the probe's Z offset.
  924. */
  925. #if HAS_BED_PROBE && Z_HOME_DIR < 0
  926. if (axis == Z_AXIS) {
  927. #if HOMING_Z_WITH_PROBE
  928. current_position[Z_AXIS] -= zprobe_zoffset;
  929. #if ENABLED(DEBUG_LEVELING_FEATURE)
  930. if (DEBUGGING(LEVELING)) {
  931. SERIAL_ECHOLNPGM("*** Z HOMED WITH PROBE (Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN) ***");
  932. SERIAL_ECHOLNPAIR("> zprobe_zoffset = ", zprobe_zoffset);
  933. }
  934. #endif
  935. #elif ENABLED(DEBUG_LEVELING_FEATURE)
  936. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("*** Z HOMED TO ENDSTOP (Z_MIN_PROBE_ENDSTOP) ***");
  937. #endif
  938. }
  939. #endif
  940. #if ENABLED(DEBUG_LEVELING_FEATURE)
  941. if (DEBUGGING(LEVELING)) {
  942. #if HAS_HOME_OFFSET
  943. SERIAL_ECHOPAIR("> home_offset[", axis_codes[axis]);
  944. SERIAL_ECHOLNPAIR("] = ", home_offset[axis]);
  945. #endif
  946. DEBUG_POS("", current_position);
  947. SERIAL_ECHOPAIR("<<< set_axis_is_at_home(", axis_codes[axis]);
  948. SERIAL_CHAR(')');
  949. SERIAL_EOL();
  950. }
  951. #endif
  952. #if ENABLED(I2C_POSITION_ENCODERS)
  953. I2CPEM.homed(axis);
  954. #endif
  955. }
  956. /**
  957. * Home an individual "raw axis" to its endstop.
  958. * This applies to XYZ on Cartesian and Core robots, and
  959. * to the individual ABC steppers on DELTA and SCARA.
  960. *
  961. * At the end of the procedure the axis is marked as
  962. * homed and the current position of that axis is updated.
  963. * Kinematic robots should wait till all axes are homed
  964. * before updating the current position.
  965. */
  966. void homeaxis(const AxisEnum axis) {
  967. #if IS_SCARA
  968. // Only Z homing (with probe) is permitted
  969. if (axis != Z_AXIS) { BUZZ(100, 880); return; }
  970. #else
  971. #define CAN_HOME(A) \
  972. (axis == A##_AXIS && ((A##_MIN_PIN > -1 && A##_HOME_DIR < 0) || (A##_MAX_PIN > -1 && A##_HOME_DIR > 0)))
  973. if (!CAN_HOME(X) && !CAN_HOME(Y) && !CAN_HOME(Z)) return;
  974. #endif
  975. #if ENABLED(DEBUG_LEVELING_FEATURE)
  976. if (DEBUGGING(LEVELING)) {
  977. SERIAL_ECHOPAIR(">>> homeaxis(", axis_codes[axis]);
  978. SERIAL_CHAR(')');
  979. SERIAL_EOL();
  980. }
  981. #endif
  982. const int axis_home_dir =
  983. #if ENABLED(DUAL_X_CARRIAGE)
  984. (axis == X_AXIS) ? x_home_dir(active_extruder) :
  985. #endif
  986. home_dir(axis);
  987. // Homing Z towards the bed? Deploy the Z probe or endstop.
  988. #if HOMING_Z_WITH_PROBE
  989. if (axis == Z_AXIS && DEPLOY_PROBE()) return;
  990. #endif
  991. // Set flags for X, Y, Z motor locking
  992. #if ENABLED(X_DUAL_ENDSTOPS)
  993. if (axis == X_AXIS) stepper.set_homing_flag_x(true);
  994. #endif
  995. #if ENABLED(Y_DUAL_ENDSTOPS)
  996. if (axis == Y_AXIS) stepper.set_homing_flag_y(true);
  997. #endif
  998. #if ENABLED(Z_DUAL_ENDSTOPS)
  999. if (axis == Z_AXIS) stepper.set_homing_flag_z(true);
  1000. #endif
  1001. // Disable stealthChop if used. Enable diag1 pin on driver.
  1002. #if ENABLED(SENSORLESS_HOMING)
  1003. #if ENABLED(X_IS_TMC2130)
  1004. if (axis == X_AXIS) tmc_sensorless_homing(stepperX);
  1005. #endif
  1006. #if ENABLED(Y_IS_TMC2130)
  1007. if (axis == Y_AXIS) tmc_sensorless_homing(stepperY);
  1008. #endif
  1009. #endif
  1010. // Fast move towards endstop until triggered
  1011. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1012. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Home 1 Fast:");
  1013. #endif
  1014. do_homing_move(axis, 1.5 * max_length(axis) * axis_home_dir);
  1015. // When homing Z with probe respect probe clearance
  1016. const float bump = axis_home_dir * (
  1017. #if HOMING_Z_WITH_PROBE
  1018. (axis == Z_AXIS) ? max(Z_CLEARANCE_BETWEEN_PROBES, home_bump_mm(Z_AXIS)) :
  1019. #endif
  1020. home_bump_mm(axis)
  1021. );
  1022. // If a second homing move is configured...
  1023. if (bump) {
  1024. // Move away from the endstop by the axis HOME_BUMP_MM
  1025. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1026. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Move Away:");
  1027. #endif
  1028. do_homing_move(axis, -bump);
  1029. // Slow move towards endstop until triggered
  1030. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1031. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Home 2 Slow:");
  1032. #endif
  1033. do_homing_move(axis, 2 * bump, get_homing_bump_feedrate(axis));
  1034. }
  1035. #if ENABLED(X_DUAL_ENDSTOPS) || ENABLED(Y_DUAL_ENDSTOPS) || ENABLED(Z_DUAL_ENDSTOPS)
  1036. const bool pos_dir = axis_home_dir > 0;
  1037. #if ENABLED(X_DUAL_ENDSTOPS)
  1038. if (axis == X_AXIS) {
  1039. const bool lock_x1 = pos_dir ? (endstops.x_endstop_adj > 0) : (endstops.x_endstop_adj < 0);
  1040. float adj = FABS(endstops.x_endstop_adj);
  1041. if (pos_dir) adj = -adj;
  1042. if (lock_x1) stepper.set_x_lock(true); else stepper.set_x2_lock(true);
  1043. do_homing_move(axis, adj);
  1044. if (lock_x1) stepper.set_x_lock(false); else stepper.set_x2_lock(false);
  1045. stepper.set_homing_flag_x(false);
  1046. }
  1047. #endif
  1048. #if ENABLED(Y_DUAL_ENDSTOPS)
  1049. if (axis == Y_AXIS) {
  1050. const bool lock_y1 = pos_dir ? (endstops.y_endstop_adj > 0) : (endstops.y_endstop_adj < 0);
  1051. float adj = FABS(endstops.y_endstop_adj);
  1052. if (pos_dir) adj = -adj;
  1053. if (lock_y1) stepper.set_y_lock(true); else stepper.set_y2_lock(true);
  1054. do_homing_move(axis, adj);
  1055. if (lock_y1) stepper.set_y_lock(false); else stepper.set_y2_lock(false);
  1056. stepper.set_homing_flag_y(false);
  1057. }
  1058. #endif
  1059. #if ENABLED(Z_DUAL_ENDSTOPS)
  1060. if (axis == Z_AXIS) {
  1061. const bool lock_z1 = pos_dir ? (endstops.z_endstop_adj > 0) : (endstops.z_endstop_adj < 0);
  1062. float adj = FABS(endstops.z_endstop_adj);
  1063. if (pos_dir) adj = -adj;
  1064. if (lock_z1) stepper.set_z_lock(true); else stepper.set_z2_lock(true);
  1065. do_homing_move(axis, adj);
  1066. if (lock_z1) stepper.set_z_lock(false); else stepper.set_z2_lock(false);
  1067. stepper.set_homing_flag_z(false);
  1068. }
  1069. #endif
  1070. #endif
  1071. #if IS_SCARA
  1072. set_axis_is_at_home(axis);
  1073. SYNC_PLAN_POSITION_KINEMATIC();
  1074. #elif ENABLED(DELTA)
  1075. // Delta has already moved all three towers up in G28
  1076. // so here it re-homes each tower in turn.
  1077. // Delta homing treats the axes as normal linear axes.
  1078. // retrace by the amount specified in delta_endstop_adj + additional 0.1mm in order to have minimum steps
  1079. if (delta_endstop_adj[axis] * Z_HOME_DIR <= 0) {
  1080. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1081. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("delta_endstop_adj:");
  1082. #endif
  1083. do_homing_move(axis, delta_endstop_adj[axis] - 0.1 * Z_HOME_DIR);
  1084. }
  1085. #else
  1086. // For cartesian/core machines,
  1087. // set the axis to its home position
  1088. set_axis_is_at_home(axis);
  1089. sync_plan_position();
  1090. destination[axis] = current_position[axis];
  1091. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1092. if (DEBUGGING(LEVELING)) DEBUG_POS("> AFTER set_axis_is_at_home", current_position);
  1093. #endif
  1094. #endif
  1095. // Re-enable stealthChop if used. Disable diag1 pin on driver.
  1096. #if ENABLED(SENSORLESS_HOMING)
  1097. #if ENABLED(X_IS_TMC2130)
  1098. if (axis == X_AXIS) tmc_sensorless_homing(stepperX, false);
  1099. #endif
  1100. #if ENABLED(Y_IS_TMC2130)
  1101. if (axis == Y_AXIS) tmc_sensorless_homing(stepperY, false);
  1102. #endif
  1103. #endif
  1104. // Put away the Z probe
  1105. #if HOMING_Z_WITH_PROBE
  1106. if (axis == Z_AXIS && STOW_PROBE()) return;
  1107. #endif
  1108. // Clear retracted status if homing the Z axis
  1109. #if ENABLED(FWRETRACT)
  1110. if (axis == Z_AXIS)
  1111. for (uint8_t i = 0; i < EXTRUDERS; i++) fwretract.retracted[i] = false;
  1112. #endif
  1113. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1114. if (DEBUGGING(LEVELING)) {
  1115. SERIAL_ECHOPAIR("<<< homeaxis(", axis_codes[axis]);
  1116. SERIAL_CHAR(')');
  1117. SERIAL_EOL();
  1118. }
  1119. #endif
  1120. } // homeaxis()
  1121. #if HAS_WORKSPACE_OFFSET || ENABLED(DUAL_X_CARRIAGE) || ENABLED(DELTA)
  1122. /**
  1123. * Software endstops can be used to monitor the open end of
  1124. * an axis that has a hardware endstop on the other end. Or
  1125. * they can prevent axes from moving past endstops and grinding.
  1126. *
  1127. * To keep doing their job as the coordinate system changes,
  1128. * the software endstop positions must be refreshed to remain
  1129. * at the same positions relative to the machine.
  1130. */
  1131. void update_software_endstops(const AxisEnum axis) {
  1132. #if HAS_HOME_OFFSET && HAS_POSITION_SHIFT
  1133. workspace_offset[axis] = home_offset[axis] + position_shift[axis];
  1134. #endif
  1135. #if ENABLED(DUAL_X_CARRIAGE)
  1136. if (axis == X_AXIS) {
  1137. // In Dual X mode hotend_offset[X] is T1's home position
  1138. float dual_max_x = max(hotend_offset[X_AXIS][1], X2_MAX_POS);
  1139. if (active_extruder != 0) {
  1140. // T1 can move from X2_MIN_POS to X2_MAX_POS or X2 home position (whichever is larger)
  1141. soft_endstop_min[X_AXIS] = X2_MIN_POS;
  1142. soft_endstop_max[X_AXIS] = dual_max_x;
  1143. }
  1144. else if (dual_x_carriage_mode == DXC_DUPLICATION_MODE) {
  1145. // In Duplication Mode, T0 can move as far left as X_MIN_POS
  1146. // but not so far to the right that T1 would move past the end
  1147. soft_endstop_min[X_AXIS] = base_min_pos(X_AXIS);
  1148. soft_endstop_max[X_AXIS] = min(base_max_pos(X_AXIS), dual_max_x - duplicate_extruder_x_offset);
  1149. }
  1150. else {
  1151. // In other modes, T0 can move from X_MIN_POS to X_MAX_POS
  1152. soft_endstop_min[axis] = base_min_pos(axis);
  1153. soft_endstop_max[axis] = base_max_pos(axis);
  1154. }
  1155. }
  1156. #elif ENABLED(DELTA)
  1157. soft_endstop_min[axis] = base_min_pos(axis);
  1158. soft_endstop_max[axis] = (axis == Z_AXIS ? delta_height : base_max_pos(axis));
  1159. #else
  1160. soft_endstop_min[axis] = base_min_pos(axis);
  1161. soft_endstop_max[axis] = base_max_pos(axis);
  1162. #endif
  1163. #if ENABLED(DEBUG_LEVELING_FEATURE)
  1164. if (DEBUGGING(LEVELING)) {
  1165. SERIAL_ECHOPAIR("For ", axis_codes[axis]);
  1166. #if HAS_HOME_OFFSET
  1167. SERIAL_ECHOPAIR(" axis:\n home_offset = ", home_offset[axis]);
  1168. #endif
  1169. #if HAS_POSITION_SHIFT
  1170. SERIAL_ECHOPAIR("\n position_shift = ", position_shift[axis]);
  1171. #endif
  1172. SERIAL_ECHOPAIR("\n soft_endstop_min = ", soft_endstop_min[axis]);
  1173. SERIAL_ECHOLNPAIR("\n soft_endstop_max = ", soft_endstop_max[axis]);
  1174. }
  1175. #endif
  1176. #if ENABLED(DELTA)
  1177. switch(axis) {
  1178. #if HAS_SOFTWARE_ENDSTOPS
  1179. case X_AXIS:
  1180. case Y_AXIS:
  1181. // Get a minimum radius for clamping
  1182. soft_endstop_radius = MIN3(FABS(max(soft_endstop_min[X_AXIS], soft_endstop_min[Y_AXIS])), soft_endstop_max[X_AXIS], soft_endstop_max[Y_AXIS]);
  1183. soft_endstop_radius_2 = sq(soft_endstop_radius);
  1184. break;
  1185. #endif
  1186. case Z_AXIS:
  1187. delta_clip_start_height = soft_endstop_max[axis] - delta_safe_distance_from_top();
  1188. default: break;
  1189. }
  1190. #endif
  1191. }
  1192. #endif // HAS_WORKSPACE_OFFSET || DUAL_X_CARRIAGE || DELTA
  1193. #if HAS_M206_COMMAND
  1194. /**
  1195. * Change the home offset for an axis.
  1196. * Also refreshes the workspace offset.
  1197. */
  1198. void set_home_offset(const AxisEnum axis, const float v) {
  1199. home_offset[axis] = v;
  1200. update_software_endstops(axis);
  1201. }
  1202. #endif // HAS_M206_COMMAND