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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. /**
  24. * motion.h
  25. *
  26. * High-level motion commands to feed the planner
  27. * Some of these methods may migrate to the planner class.
  28. */
  29. #include "../inc/MarlinConfig.h"
  30. #if IS_SCARA
  31. #include "scara.h"
  32. #endif
  33. // Error margin to work around float imprecision
  34. constexpr float fslop = 0.0001;
  35. extern bool relative_mode;
  36. extern xyze_pos_t current_position, // High-level current tool position
  37. destination; // Destination for a move
  38. // G60/G61 Position Save and Return
  39. #if SAVED_POSITIONS
  40. extern uint8_t saved_slots[(SAVED_POSITIONS + 7) >> 3];
  41. extern xyz_pos_t stored_position[SAVED_POSITIONS];
  42. #endif
  43. // Scratch space for a cartesian result
  44. extern xyz_pos_t cartes;
  45. // Until kinematics.cpp is created, declare this here
  46. #if IS_KINEMATIC
  47. extern abc_pos_t delta;
  48. #endif
  49. #if HAS_ABL_NOT_UBL
  50. extern feedRate_t xy_probe_feedrate_mm_s;
  51. #define XY_PROBE_FEEDRATE_MM_S xy_probe_feedrate_mm_s
  52. #elif defined(XY_PROBE_SPEED)
  53. #define XY_PROBE_FEEDRATE_MM_S MMM_TO_MMS(XY_PROBE_SPEED)
  54. #else
  55. #define XY_PROBE_FEEDRATE_MM_S PLANNER_XY_FEEDRATE()
  56. #endif
  57. constexpr feedRate_t z_probe_fast_mm_s = MMM_TO_MMS(Z_PROBE_SPEED_FAST);
  58. /**
  59. * Feed rates are often configured with mm/m
  60. * but the planner and stepper like mm/s units.
  61. */
  62. constexpr xyz_feedrate_t homing_feedrate_mm_m = HOMING_FEEDRATE_MM_M;
  63. FORCE_INLINE feedRate_t homing_feedrate(const AxisEnum a) {
  64. float v;
  65. #if ENABLED(DELTA)
  66. v = homing_feedrate_mm_m.z;
  67. #else
  68. switch (a) {
  69. case X_AXIS: v = homing_feedrate_mm_m.x; break;
  70. case Y_AXIS: v = homing_feedrate_mm_m.y; break;
  71. case Z_AXIS:
  72. default: v = homing_feedrate_mm_m.z;
  73. }
  74. #endif
  75. return MMM_TO_MMS(v);
  76. }
  77. feedRate_t get_homing_bump_feedrate(const AxisEnum axis);
  78. /**
  79. * The default feedrate for many moves, set by the most recent move
  80. */
  81. extern feedRate_t feedrate_mm_s;
  82. /**
  83. * Feedrate scaling is applied to all G0/G1, G2/G3, and G5 moves
  84. */
  85. extern int16_t feedrate_percentage;
  86. #define MMS_SCALED(V) ((V) * 0.01f * feedrate_percentage)
  87. // The active extruder (tool). Set with T<extruder> command.
  88. #if HAS_MULTI_EXTRUDER
  89. extern uint8_t active_extruder;
  90. #else
  91. constexpr uint8_t active_extruder = 0;
  92. #endif
  93. #if ENABLED(LCD_SHOW_E_TOTAL)
  94. extern float e_move_accumulator;
  95. #endif
  96. #ifdef __IMXRT1062__
  97. #define DEFS_PROGMEM
  98. #else
  99. #define DEFS_PROGMEM PROGMEM
  100. #endif
  101. inline float pgm_read_any(const float *p) { return TERN(__IMXRT1062__, *p, pgm_read_float(p)); }
  102. inline int8_t pgm_read_any(const int8_t *p) { return TERN(__IMXRT1062__, *p, pgm_read_byte(p)); }
  103. #define XYZ_DEFS(T, NAME, OPT) \
  104. inline T NAME(const AxisEnum axis) { \
  105. static const XYZval<T> NAME##_P DEFS_PROGMEM = { X_##OPT, Y_##OPT, Z_##OPT }; \
  106. return pgm_read_any(&NAME##_P[axis]); \
  107. }
  108. XYZ_DEFS(float, base_min_pos, MIN_POS);
  109. XYZ_DEFS(float, base_max_pos, MAX_POS);
  110. XYZ_DEFS(float, base_home_pos, HOME_POS);
  111. XYZ_DEFS(float, max_length, MAX_LENGTH);
  112. XYZ_DEFS(int8_t, home_dir, HOME_DIR);
  113. inline float home_bump_mm(const AxisEnum axis) {
  114. static const xyz_pos_t home_bump_mm_P DEFS_PROGMEM = HOMING_BUMP_MM;
  115. return pgm_read_any(&home_bump_mm_P[axis]);
  116. }
  117. #if HAS_WORKSPACE_OFFSET
  118. void update_workspace_offset(const AxisEnum axis);
  119. #else
  120. inline void update_workspace_offset(const AxisEnum) {}
  121. #endif
  122. #if HAS_HOTEND_OFFSET
  123. extern xyz_pos_t hotend_offset[HOTENDS];
  124. void reset_hotend_offsets();
  125. #elif HOTENDS
  126. constexpr xyz_pos_t hotend_offset[HOTENDS] = { { 0 } };
  127. #else
  128. constexpr xyz_pos_t hotend_offset[1] = { { 0 } };
  129. #endif
  130. #if HAS_SOFTWARE_ENDSTOPS
  131. typedef struct {
  132. bool _enabled, _loose;
  133. bool enabled() { return _enabled && !_loose; }
  134. xyz_pos_t min, max;
  135. void get_manual_axis_limits(const AxisEnum axis, float &amin, float &amax) {
  136. amin = -100000; amax = 100000; // "No limits"
  137. #if HAS_SOFTWARE_ENDSTOPS
  138. if (enabled()) switch (axis) {
  139. case X_AXIS:
  140. TERN_(MIN_SOFTWARE_ENDSTOP_X, amin = min.x);
  141. TERN_(MAX_SOFTWARE_ENDSTOP_X, amax = max.x);
  142. break;
  143. case Y_AXIS:
  144. TERN_(MIN_SOFTWARE_ENDSTOP_Y, amin = min.y);
  145. TERN_(MAX_SOFTWARE_ENDSTOP_Y, amax = max.y);
  146. break;
  147. case Z_AXIS:
  148. TERN_(MIN_SOFTWARE_ENDSTOP_Z, amin = min.z);
  149. TERN_(MAX_SOFTWARE_ENDSTOP_Z, amax = max.z);
  150. default: break;
  151. }
  152. #endif
  153. }
  154. } soft_endstops_t;
  155. extern soft_endstops_t soft_endstop;
  156. void apply_motion_limits(xyz_pos_t &target);
  157. void update_software_endstops(const AxisEnum axis
  158. #if HAS_HOTEND_OFFSET
  159. , const uint8_t old_tool_index=0, const uint8_t new_tool_index=0
  160. #endif
  161. );
  162. #define SET_SOFT_ENDSTOP_LOOSE(loose) (soft_endstop._loose = loose)
  163. #else // !HAS_SOFTWARE_ENDSTOPS
  164. typedef struct {
  165. bool enabled() { return false; }
  166. void get_manual_axis_limits(const AxisEnum axis, float &amin, float &amax) {
  167. // No limits
  168. amin = current_position[axis] - 1000;
  169. amax = current_position[axis] + 1000;
  170. }
  171. } soft_endstops_t;
  172. extern soft_endstops_t soft_endstop;
  173. #define apply_motion_limits(V) NOOP
  174. #define update_software_endstops(...) NOOP
  175. #define SET_SOFT_ENDSTOP_LOOSE(V) NOOP
  176. #endif // !HAS_SOFTWARE_ENDSTOPS
  177. void report_real_position();
  178. void report_current_position();
  179. void report_current_position_projected();
  180. void get_cartesian_from_steppers();
  181. void set_current_from_steppers_for_axis(const AxisEnum axis);
  182. void quickstop_stepper();
  183. /**
  184. * sync_plan_position
  185. *
  186. * Set the planner/stepper positions directly from current_position with
  187. * no kinematic translation. Used for homing axes and cartesian/core syncing.
  188. */
  189. void sync_plan_position();
  190. void sync_plan_position_e();
  191. /**
  192. * Move the planner to the current position from wherever it last moved
  193. * (or from wherever it has been told it is located).
  194. */
  195. void line_to_current_position(const feedRate_t &fr_mm_s=feedrate_mm_s);
  196. #if EXTRUDERS
  197. void unscaled_e_move(const float &length, const feedRate_t &fr_mm_s);
  198. #endif
  199. void prepare_line_to_destination();
  200. void _internal_move_to_destination(const feedRate_t &fr_mm_s=0.0f
  201. #if IS_KINEMATIC
  202. , const bool is_fast=false
  203. #endif
  204. );
  205. inline void prepare_internal_move_to_destination(const feedRate_t &fr_mm_s=0.0f) {
  206. _internal_move_to_destination(fr_mm_s);
  207. }
  208. #if IS_KINEMATIC
  209. void prepare_fast_move_to_destination(const feedRate_t &scaled_fr_mm_s=MMS_SCALED(feedrate_mm_s));
  210. inline void prepare_internal_fast_move_to_destination(const feedRate_t &fr_mm_s=0.0f) {
  211. _internal_move_to_destination(fr_mm_s, true);
  212. }
  213. #endif
  214. /**
  215. * Blocking movement and shorthand functions
  216. */
  217. void do_blocking_move_to(const float rx, const float ry, const float rz, const feedRate_t &fr_mm_s=0.0f);
  218. void do_blocking_move_to(const xy_pos_t &raw, const feedRate_t &fr_mm_s=0.0f);
  219. void do_blocking_move_to(const xyz_pos_t &raw, const feedRate_t &fr_mm_s=0.0f);
  220. void do_blocking_move_to(const xyze_pos_t &raw, const feedRate_t &fr_mm_s=0.0f);
  221. void do_blocking_move_to_x(const float &rx, const feedRate_t &fr_mm_s=0.0f);
  222. void do_blocking_move_to_y(const float &ry, const feedRate_t &fr_mm_s=0.0f);
  223. void do_blocking_move_to_z(const float &rz, const feedRate_t &fr_mm_s=0.0f);
  224. void do_blocking_move_to_xy(const float &rx, const float &ry, const feedRate_t &fr_mm_s=0.0f);
  225. void do_blocking_move_to_xy(const xy_pos_t &raw, const feedRate_t &fr_mm_s=0.0f);
  226. FORCE_INLINE void do_blocking_move_to_xy(const xyz_pos_t &raw, const feedRate_t &fr_mm_s=0.0f) { do_blocking_move_to_xy(xy_pos_t(raw), fr_mm_s); }
  227. FORCE_INLINE void do_blocking_move_to_xy(const xyze_pos_t &raw, const feedRate_t &fr_mm_s=0.0f) { do_blocking_move_to_xy(xy_pos_t(raw), fr_mm_s); }
  228. void do_blocking_move_to_xy_z(const xy_pos_t &raw, const float &z, const feedRate_t &fr_mm_s=0.0f);
  229. FORCE_INLINE void do_blocking_move_to_xy_z(const xyz_pos_t &raw, const float &z, const feedRate_t &fr_mm_s=0.0f) { do_blocking_move_to_xy_z(xy_pos_t(raw), z, fr_mm_s); }
  230. FORCE_INLINE void do_blocking_move_to_xy_z(const xyze_pos_t &raw, const float &z, const feedRate_t &fr_mm_s=0.0f) { do_blocking_move_to_xy_z(xy_pos_t(raw), z, fr_mm_s); }
  231. void remember_feedrate_and_scaling();
  232. void remember_feedrate_scaling_off();
  233. void restore_feedrate_and_scaling();
  234. void do_z_clearance(const float &zclear, const bool z_trusted=true, const bool raise_on_untrusted=true, const bool lower_allowed=false);
  235. /**
  236. * Homing and Trusted Axes
  237. */
  238. constexpr uint8_t xyz_bits = _BV(X_AXIS) | _BV(Y_AXIS) | _BV(Z_AXIS);
  239. extern uint8_t axis_homed, axis_trusted;
  240. void homeaxis(const AxisEnum axis);
  241. void set_axis_is_at_home(const AxisEnum axis);
  242. void set_axis_never_homed(const AxisEnum axis);
  243. uint8_t axes_should_home(uint8_t axis_bits=0x07);
  244. bool homing_needed_error(uint8_t axis_bits=0x07);
  245. FORCE_INLINE bool axis_was_homed(const AxisEnum axis) { return TEST(axis_homed, axis); }
  246. FORCE_INLINE bool axis_is_trusted(const AxisEnum axis) { return TEST(axis_trusted, axis); }
  247. FORCE_INLINE bool axis_should_home(const AxisEnum axis) { return (axes_should_home() & _BV(axis)) != 0; }
  248. FORCE_INLINE bool no_axes_homed() { return !axis_homed; }
  249. FORCE_INLINE bool all_axes_homed() { return xyz_bits == (axis_homed & xyz_bits); }
  250. FORCE_INLINE bool homing_needed() { return !all_axes_homed(); }
  251. FORCE_INLINE bool all_axes_trusted() { return xyz_bits == (axis_trusted & xyz_bits); }
  252. FORCE_INLINE void set_axis_homed(const AxisEnum axis) { SBI(axis_homed, axis); }
  253. FORCE_INLINE void set_axis_unhomed(const AxisEnum axis) { CBI(axis_homed, axis); }
  254. FORCE_INLINE void set_axis_trusted(const AxisEnum axis) { SBI(axis_trusted, axis); }
  255. FORCE_INLINE void set_axis_untrusted(const AxisEnum axis) { CBI(axis_trusted, axis); }
  256. FORCE_INLINE void set_all_homed() { axis_homed = axis_trusted = xyz_bits; }
  257. FORCE_INLINE void set_all_unhomed() { axis_homed = axis_trusted = 0; }
  258. #if ENABLED(NO_MOTION_BEFORE_HOMING)
  259. #define MOTION_CONDITIONS (IsRunning() && !homing_needed_error())
  260. #else
  261. #define MOTION_CONDITIONS IsRunning()
  262. #endif
  263. #define BABYSTEP_ALLOWED() ((ENABLED(BABYSTEP_WITHOUT_HOMING) || all_axes_trusted()) && (ENABLED(BABYSTEP_ALWAYS_AVAILABLE) || printer_busy()))
  264. /**
  265. * Workspace offsets
  266. */
  267. #if HAS_HOME_OFFSET || HAS_POSITION_SHIFT
  268. #if HAS_HOME_OFFSET
  269. extern xyz_pos_t home_offset;
  270. #endif
  271. #if HAS_POSITION_SHIFT
  272. extern xyz_pos_t position_shift;
  273. #endif
  274. #if HAS_HOME_OFFSET && HAS_POSITION_SHIFT
  275. extern xyz_pos_t workspace_offset;
  276. #define _WS workspace_offset
  277. #elif HAS_HOME_OFFSET
  278. #define _WS home_offset
  279. #else
  280. #define _WS position_shift
  281. #endif
  282. #define NATIVE_TO_LOGICAL(POS, AXIS) ((POS) + _WS[AXIS])
  283. #define LOGICAL_TO_NATIVE(POS, AXIS) ((POS) - _WS[AXIS])
  284. FORCE_INLINE void toLogical(xy_pos_t &raw) { raw += _WS; }
  285. FORCE_INLINE void toLogical(xyz_pos_t &raw) { raw += _WS; }
  286. FORCE_INLINE void toLogical(xyze_pos_t &raw) { raw += _WS; }
  287. FORCE_INLINE void toNative(xy_pos_t &raw) { raw -= _WS; }
  288. FORCE_INLINE void toNative(xyz_pos_t &raw) { raw -= _WS; }
  289. FORCE_INLINE void toNative(xyze_pos_t &raw) { raw -= _WS; }
  290. #else
  291. #define NATIVE_TO_LOGICAL(POS, AXIS) (POS)
  292. #define LOGICAL_TO_NATIVE(POS, AXIS) (POS)
  293. FORCE_INLINE void toLogical(xy_pos_t&) {}
  294. FORCE_INLINE void toLogical(xyz_pos_t&) {}
  295. FORCE_INLINE void toLogical(xyze_pos_t&) {}
  296. FORCE_INLINE void toNative(xy_pos_t&) {}
  297. FORCE_INLINE void toNative(xyz_pos_t&) {}
  298. FORCE_INLINE void toNative(xyze_pos_t&) {}
  299. #endif
  300. #define LOGICAL_X_POSITION(POS) NATIVE_TO_LOGICAL(POS, X_AXIS)
  301. #define LOGICAL_Y_POSITION(POS) NATIVE_TO_LOGICAL(POS, Y_AXIS)
  302. #define LOGICAL_Z_POSITION(POS) NATIVE_TO_LOGICAL(POS, Z_AXIS)
  303. #define RAW_X_POSITION(POS) LOGICAL_TO_NATIVE(POS, X_AXIS)
  304. #define RAW_Y_POSITION(POS) LOGICAL_TO_NATIVE(POS, Y_AXIS)
  305. #define RAW_Z_POSITION(POS) LOGICAL_TO_NATIVE(POS, Z_AXIS)
  306. /**
  307. * position_is_reachable family of functions
  308. */
  309. #if IS_KINEMATIC // (DELTA or SCARA)
  310. #if HAS_SCARA_OFFSET
  311. extern abc_pos_t scara_home_offset; // A and B angular offsets, Z mm offset
  312. #endif
  313. // Return true if the given point is within the printable area
  314. inline bool position_is_reachable(const float &rx, const float &ry, const float inset=0) {
  315. #if ENABLED(DELTA)
  316. return HYPOT2(rx, ry) <= sq(DELTA_PRINTABLE_RADIUS - inset + fslop);
  317. #elif IS_SCARA
  318. const float R2 = HYPOT2(rx - SCARA_OFFSET_X, ry - SCARA_OFFSET_Y);
  319. return (
  320. R2 <= sq(L1 + L2) - inset
  321. #if MIDDLE_DEAD_ZONE_R > 0
  322. && R2 >= sq(float(MIDDLE_DEAD_ZONE_R))
  323. #endif
  324. );
  325. #endif
  326. }
  327. inline bool position_is_reachable(const xy_pos_t &pos, const float inset=0) {
  328. return position_is_reachable(pos.x, pos.y, inset);
  329. }
  330. #else // CARTESIAN
  331. // Return true if the given position is within the machine bounds.
  332. inline bool position_is_reachable(const float &rx, const float &ry) {
  333. if (!WITHIN(ry, Y_MIN_POS - fslop, Y_MAX_POS + fslop)) return false;
  334. #if ENABLED(DUAL_X_CARRIAGE)
  335. if (active_extruder)
  336. return WITHIN(rx, X2_MIN_POS - fslop, X2_MAX_POS + fslop);
  337. else
  338. return WITHIN(rx, X1_MIN_POS - fslop, X1_MAX_POS + fslop);
  339. #else
  340. return WITHIN(rx, X_MIN_POS - fslop, X_MAX_POS + fslop);
  341. #endif
  342. }
  343. inline bool position_is_reachable(const xy_pos_t &pos) { return position_is_reachable(pos.x, pos.y); }
  344. #endif // CARTESIAN
  345. /**
  346. * Duplication mode
  347. */
  348. #if HAS_DUPLICATION_MODE
  349. extern bool extruder_duplication_enabled; // Used in Dual X mode 2
  350. #endif
  351. /**
  352. * Dual X Carriage
  353. */
  354. #if ENABLED(DUAL_X_CARRIAGE)
  355. enum DualXMode : char {
  356. DXC_FULL_CONTROL_MODE,
  357. DXC_AUTO_PARK_MODE,
  358. DXC_DUPLICATION_MODE,
  359. DXC_MIRRORED_MODE
  360. };
  361. extern DualXMode dual_x_carriage_mode;
  362. extern float inactive_extruder_x, // Used in mode 0 & 1
  363. duplicate_extruder_x_offset; // Used in mode 2 & 3
  364. extern xyz_pos_t raised_parked_position; // Used in mode 1
  365. extern bool active_extruder_parked; // Used in mode 1, 2 & 3
  366. extern millis_t delayed_move_time; // Used in mode 1
  367. extern int16_t duplicate_extruder_temp_offset; // Used in mode 2 & 3
  368. extern bool idex_mirrored_mode; // Used in mode 3
  369. FORCE_INLINE bool idex_is_duplicating() { return dual_x_carriage_mode >= DXC_DUPLICATION_MODE; }
  370. float x_home_pos(const uint8_t extruder);
  371. FORCE_INLINE int x_home_dir(const uint8_t extruder) { return extruder ? X2_HOME_DIR : X_HOME_DIR; }
  372. void set_duplication_enabled(const bool dupe, const int8_t tool_index=-1);
  373. void idex_set_mirrored_mode(const bool mirr);
  374. void idex_set_parked(const bool park=true);
  375. #else
  376. #if ENABLED(MULTI_NOZZLE_DUPLICATION)
  377. extern uint8_t duplication_e_mask;
  378. enum DualXMode : char { DXC_DUPLICATION_MODE = 2 };
  379. FORCE_INLINE void set_duplication_enabled(const bool dupe) { extruder_duplication_enabled = dupe; }
  380. #endif
  381. FORCE_INLINE int x_home_dir(const uint8_t) { return home_dir(X_AXIS); }
  382. #endif
  383. #if HAS_M206_COMMAND
  384. void set_home_offset(const AxisEnum axis, const float v);
  385. #endif
  386. #if USE_SENSORLESS
  387. struct sensorless_t;
  388. sensorless_t start_sensorless_homing_per_axis(const AxisEnum axis);
  389. void end_sensorless_homing_per_axis(const AxisEnum axis, sensorless_t enable_stealth);
  390. #endif