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

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