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.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. // Axis homed and known-position states
  34. extern uint8_t axis_homed, axis_known_position;
  35. constexpr uint8_t xyz_bits = _BV(X_AXIS) | _BV(Y_AXIS) | _BV(Z_AXIS);
  36. FORCE_INLINE bool no_axes_homed() { return !axis_homed; }
  37. FORCE_INLINE bool all_axes_homed() { return (axis_homed & xyz_bits) == xyz_bits; }
  38. FORCE_INLINE bool all_axes_known() { return (axis_known_position & xyz_bits) == xyz_bits; }
  39. FORCE_INLINE void set_all_unhomed() { axis_homed = 0; }
  40. FORCE_INLINE void set_all_unknown() { axis_known_position = 0; }
  41. FORCE_INLINE bool homing_needed() {
  42. return !TERN(HOME_AFTER_DEACTIVATE, all_axes_known, all_axes_homed)();
  43. }
  44. // Error margin to work around float imprecision
  45. constexpr float fslop = 0.0001;
  46. extern bool relative_mode;
  47. extern xyze_pos_t current_position, // High-level current tool position
  48. destination; // Destination for a move
  49. // G60/G61 Position Save and Return
  50. #if SAVED_POSITIONS
  51. extern uint8_t saved_slots[(SAVED_POSITIONS + 7) >> 3];
  52. extern xyz_pos_t stored_position[SAVED_POSITIONS];
  53. #endif
  54. // Scratch space for a cartesian result
  55. extern xyz_pos_t cartes;
  56. // Until kinematics.cpp is created, declare this here
  57. #if IS_KINEMATIC
  58. extern abc_pos_t delta;
  59. #endif
  60. #if HAS_ABL_NOT_UBL
  61. extern float xy_probe_feedrate_mm_s;
  62. #define XY_PROBE_FEEDRATE_MM_S xy_probe_feedrate_mm_s
  63. #elif defined(XY_PROBE_SPEED)
  64. #define XY_PROBE_FEEDRATE_MM_S MMM_TO_MMS(XY_PROBE_SPEED)
  65. #else
  66. #define XY_PROBE_FEEDRATE_MM_S PLANNER_XY_FEEDRATE()
  67. #endif
  68. #if ENABLED(Z_SAFE_HOMING)
  69. constexpr xy_float_t safe_homing_xy = { Z_SAFE_HOMING_X_POINT, Z_SAFE_HOMING_Y_POINT };
  70. #endif
  71. /**
  72. * Feed rates are often configured with mm/m
  73. * but the planner and stepper like mm/s units.
  74. */
  75. extern const feedRate_t homing_feedrate_mm_s[XYZ];
  76. FORCE_INLINE feedRate_t homing_feedrate(const AxisEnum a) { return pgm_read_float(&homing_feedrate_mm_s[a]); }
  77. feedRate_t get_homing_bump_feedrate(const AxisEnum axis);
  78. extern feedRate_t feedrate_mm_s;
  79. /**
  80. * Feedrate scaling
  81. */
  82. extern int16_t feedrate_percentage;
  83. // The active extruder (tool). Set with T<extruder> command.
  84. #if EXTRUDERS > 1
  85. extern uint8_t active_extruder;
  86. #else
  87. constexpr uint8_t active_extruder = 0;
  88. #endif
  89. #if ENABLED(LCD_SHOW_E_TOTAL)
  90. extern float e_move_accumulator;
  91. #endif
  92. inline float pgm_read_any(const float *p) { return pgm_read_float(p); }
  93. inline signed char pgm_read_any(const signed char *p) { return pgm_read_byte(p); }
  94. #define XYZ_DEFS(T, NAME, OPT) \
  95. inline T NAME(const AxisEnum axis) { \
  96. static const XYZval<T> NAME##_P PROGMEM = { X_##OPT, Y_##OPT, Z_##OPT }; \
  97. return pgm_read_any(&NAME##_P[axis]); \
  98. }
  99. XYZ_DEFS(float, base_min_pos, MIN_POS);
  100. XYZ_DEFS(float, base_max_pos, MAX_POS);
  101. XYZ_DEFS(float, base_home_pos, HOME_POS);
  102. XYZ_DEFS(float, max_length, MAX_LENGTH);
  103. XYZ_DEFS(signed char, home_dir, HOME_DIR);
  104. inline float home_bump_mm(const AxisEnum axis) {
  105. static const xyz_pos_t home_bump_mm_P PROGMEM = HOMING_BUMP_MM;
  106. return pgm_read_any(&home_bump_mm_P[axis]);
  107. }
  108. #if HAS_WORKSPACE_OFFSET
  109. void update_workspace_offset(const AxisEnum axis);
  110. #else
  111. inline void update_workspace_offset(const AxisEnum) {}
  112. #endif
  113. #if HAS_HOTEND_OFFSET
  114. extern xyz_pos_t hotend_offset[HOTENDS];
  115. void reset_hotend_offsets();
  116. #elif HOTENDS
  117. constexpr xyz_pos_t hotend_offset[HOTENDS] = { { 0 } };
  118. #else
  119. constexpr xyz_pos_t hotend_offset[1] = { { 0 } };
  120. #endif
  121. typedef struct { xyz_pos_t min, max; } axis_limits_t;
  122. #if HAS_SOFTWARE_ENDSTOPS
  123. extern bool soft_endstops_enabled;
  124. extern axis_limits_t soft_endstop;
  125. void apply_motion_limits(xyz_pos_t &target);
  126. void update_software_endstops(const AxisEnum axis
  127. #if HAS_HOTEND_OFFSET
  128. , const uint8_t old_tool_index=0, const uint8_t new_tool_index=0
  129. #endif
  130. );
  131. #define TEMPORARY_SOFT_ENDSTOP_STATE(enable) REMEMBER(tes, soft_endstops_enabled, enable);
  132. #else
  133. constexpr bool soft_endstops_enabled = false;
  134. //constexpr axis_limits_t soft_endstop = {
  135. // { X_MIN_POS, Y_MIN_POS, Z_MIN_POS },
  136. // { X_MAX_POS, Y_MAX_POS, Z_MAX_POS } };
  137. #define apply_motion_limits(V) NOOP
  138. #define update_software_endstops(...) NOOP
  139. #define TEMPORARY_SOFT_ENDSTOP_STATE(...) NOOP
  140. #endif
  141. void report_real_position();
  142. void report_current_position();
  143. void report_current_position_projected();
  144. void get_cartesian_from_steppers();
  145. void set_current_from_steppers_for_axis(const AxisEnum axis);
  146. /**
  147. * sync_plan_position
  148. *
  149. * Set the planner/stepper positions directly from current_position with
  150. * no kinematic translation. Used for homing axes and cartesian/core syncing.
  151. */
  152. void sync_plan_position();
  153. void sync_plan_position_e();
  154. /**
  155. * Move the planner to the current position from wherever it last moved
  156. * (or from wherever it has been told it is located).
  157. */
  158. void line_to_current_position(const feedRate_t &fr_mm_s=feedrate_mm_s);
  159. #if EXTRUDERS
  160. void unscaled_e_move(const float &length, const feedRate_t &fr_mm_s);
  161. #endif
  162. void prepare_line_to_destination();
  163. void _internal_move_to_destination(const feedRate_t &fr_mm_s=0.0f
  164. #if IS_KINEMATIC
  165. , const bool is_fast=false
  166. #endif
  167. );
  168. inline void prepare_internal_move_to_destination(const feedRate_t &fr_mm_s=0.0f) {
  169. _internal_move_to_destination(fr_mm_s);
  170. }
  171. #if IS_KINEMATIC
  172. void prepare_fast_move_to_destination(const feedRate_t &scaled_fr_mm_s=MMS_SCALED(feedrate_mm_s));
  173. inline void prepare_internal_fast_move_to_destination(const feedRate_t &fr_mm_s=0.0f) {
  174. _internal_move_to_destination(fr_mm_s, true);
  175. }
  176. #endif
  177. /**
  178. * Blocking movement and shorthand functions
  179. */
  180. void do_blocking_move_to(const float rx, const float ry, const float rz, const feedRate_t &fr_mm_s=0.0f);
  181. void do_blocking_move_to(const xy_pos_t &raw, const feedRate_t &fr_mm_s=0.0f);
  182. void do_blocking_move_to(const xyz_pos_t &raw, const feedRate_t &fr_mm_s=0.0f);
  183. void do_blocking_move_to(const xyze_pos_t &raw, const feedRate_t &fr_mm_s=0.0f);
  184. void do_blocking_move_to_x(const float &rx, const feedRate_t &fr_mm_s=0.0f);
  185. void do_blocking_move_to_y(const float &ry, const feedRate_t &fr_mm_s=0.0f);
  186. void do_blocking_move_to_z(const float &rz, const feedRate_t &fr_mm_s=0.0f);
  187. void do_blocking_move_to_xy(const float &rx, const float &ry, const feedRate_t &fr_mm_s=0.0f);
  188. void do_blocking_move_to_xy(const xy_pos_t &raw, const feedRate_t &fr_mm_s=0.0f);
  189. 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); }
  190. 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); }
  191. void do_blocking_move_to_xy_z(const xy_pos_t &raw, const float &z, const feedRate_t &fr_mm_s=0.0f);
  192. 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); }
  193. 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); }
  194. void remember_feedrate_and_scaling();
  195. void remember_feedrate_scaling_off();
  196. void restore_feedrate_and_scaling();
  197. void do_z_clearance(const float &zclear, const bool z_known=true, const bool raise_on_unknown=true, const bool lower_allowed=false);
  198. //
  199. // Homing
  200. //
  201. uint8_t axes_need_homing(uint8_t axis_bits=0x07);
  202. bool axis_unhomed_error(uint8_t axis_bits=0x07);
  203. #if ENABLED(NO_MOTION_BEFORE_HOMING)
  204. #define MOTION_CONDITIONS (IsRunning() && !axis_unhomed_error())
  205. #else
  206. #define MOTION_CONDITIONS IsRunning()
  207. #endif
  208. void set_axis_is_at_home(const AxisEnum axis);
  209. void set_axis_not_trusted(const AxisEnum axis);
  210. void homeaxis(const AxisEnum axis);
  211. /**
  212. * Workspace offsets
  213. */
  214. #if HAS_HOME_OFFSET || HAS_POSITION_SHIFT
  215. #if HAS_HOME_OFFSET
  216. extern xyz_pos_t home_offset;
  217. #endif
  218. #if HAS_POSITION_SHIFT
  219. extern xyz_pos_t position_shift;
  220. #endif
  221. #if HAS_HOME_OFFSET && HAS_POSITION_SHIFT
  222. extern xyz_pos_t workspace_offset;
  223. #define _WS workspace_offset
  224. #elif HAS_HOME_OFFSET
  225. #define _WS home_offset
  226. #else
  227. #define _WS position_shift
  228. #endif
  229. #define NATIVE_TO_LOGICAL(POS, AXIS) ((POS) + _WS[AXIS])
  230. #define LOGICAL_TO_NATIVE(POS, AXIS) ((POS) - _WS[AXIS])
  231. FORCE_INLINE void toLogical(xy_pos_t &raw) { raw += _WS; }
  232. FORCE_INLINE void toLogical(xyz_pos_t &raw) { raw += _WS; }
  233. FORCE_INLINE void toLogical(xyze_pos_t &raw) { raw += _WS; }
  234. FORCE_INLINE void toNative(xy_pos_t &raw) { raw -= _WS; }
  235. FORCE_INLINE void toNative(xyz_pos_t &raw) { raw -= _WS; }
  236. FORCE_INLINE void toNative(xyze_pos_t &raw) { raw -= _WS; }
  237. #else
  238. #define NATIVE_TO_LOGICAL(POS, AXIS) (POS)
  239. #define LOGICAL_TO_NATIVE(POS, AXIS) (POS)
  240. FORCE_INLINE void toLogical(xy_pos_t&) {}
  241. FORCE_INLINE void toLogical(xyz_pos_t&) {}
  242. FORCE_INLINE void toLogical(xyze_pos_t&) {}
  243. FORCE_INLINE void toNative(xy_pos_t&) {}
  244. FORCE_INLINE void toNative(xyz_pos_t&) {}
  245. FORCE_INLINE void toNative(xyze_pos_t&) {}
  246. #endif
  247. #define LOGICAL_X_POSITION(POS) NATIVE_TO_LOGICAL(POS, X_AXIS)
  248. #define LOGICAL_Y_POSITION(POS) NATIVE_TO_LOGICAL(POS, Y_AXIS)
  249. #define LOGICAL_Z_POSITION(POS) NATIVE_TO_LOGICAL(POS, Z_AXIS)
  250. #define RAW_X_POSITION(POS) LOGICAL_TO_NATIVE(POS, X_AXIS)
  251. #define RAW_Y_POSITION(POS) LOGICAL_TO_NATIVE(POS, Y_AXIS)
  252. #define RAW_Z_POSITION(POS) LOGICAL_TO_NATIVE(POS, Z_AXIS)
  253. /**
  254. * position_is_reachable family of functions
  255. */
  256. #if IS_KINEMATIC // (DELTA or SCARA)
  257. #if HAS_SCARA_OFFSET
  258. extern abc_pos_t scara_home_offset; // A and B angular offsets, Z mm offset
  259. #endif
  260. // Return true if the given point is within the printable area
  261. inline bool position_is_reachable(const float &rx, const float &ry, const float inset=0) {
  262. #if ENABLED(DELTA)
  263. return HYPOT2(rx, ry) <= sq(DELTA_PRINTABLE_RADIUS - inset + fslop);
  264. #elif IS_SCARA
  265. const float R2 = HYPOT2(rx - SCARA_OFFSET_X, ry - SCARA_OFFSET_Y);
  266. return (
  267. R2 <= sq(L1 + L2) - inset
  268. #if MIDDLE_DEAD_ZONE_R > 0
  269. && R2 >= sq(float(MIDDLE_DEAD_ZONE_R))
  270. #endif
  271. );
  272. #endif
  273. }
  274. inline bool position_is_reachable(const xy_pos_t &pos, const float inset=0) {
  275. return position_is_reachable(pos.x, pos.y, inset);
  276. }
  277. #else // CARTESIAN
  278. // Return true if the given position is within the machine bounds.
  279. inline bool position_is_reachable(const float &rx, const float &ry) {
  280. if (!WITHIN(ry, Y_MIN_POS - fslop, Y_MAX_POS + fslop)) return false;
  281. #if ENABLED(DUAL_X_CARRIAGE)
  282. if (active_extruder)
  283. return WITHIN(rx, X2_MIN_POS - fslop, X2_MAX_POS + fslop);
  284. else
  285. return WITHIN(rx, X1_MIN_POS - fslop, X1_MAX_POS + fslop);
  286. #else
  287. return WITHIN(rx, X_MIN_POS - fslop, X_MAX_POS + fslop);
  288. #endif
  289. }
  290. inline bool position_is_reachable(const xy_pos_t &pos) { return position_is_reachable(pos.x, pos.y); }
  291. #endif // CARTESIAN
  292. /**
  293. * Duplication mode
  294. */
  295. #if HAS_DUPLICATION_MODE
  296. extern bool extruder_duplication_enabled, // Used in Dual X mode 2
  297. mirrored_duplication_mode; // Used in Dual X mode 3
  298. #if ENABLED(MULTI_NOZZLE_DUPLICATION)
  299. extern uint8_t duplication_e_mask;
  300. #endif
  301. #endif
  302. /**
  303. * Dual X Carriage
  304. */
  305. #if ENABLED(DUAL_X_CARRIAGE)
  306. enum DualXMode : char {
  307. DXC_FULL_CONTROL_MODE,
  308. DXC_AUTO_PARK_MODE,
  309. DXC_DUPLICATION_MODE,
  310. DXC_MIRRORED_MODE
  311. };
  312. extern DualXMode dual_x_carriage_mode;
  313. extern float inactive_extruder_x_pos, // Used in mode 0 & 1
  314. duplicate_extruder_x_offset; // Used in mode 2 & 3
  315. extern xyz_pos_t raised_parked_position; // Used in mode 1
  316. extern bool active_extruder_parked; // Used in mode 1, 2 & 3
  317. extern millis_t delayed_move_time; // Used in mode 1
  318. extern int16_t duplicate_extruder_temp_offset; // Used in mode 2 & 3
  319. FORCE_INLINE bool dxc_is_duplicating() { return dual_x_carriage_mode >= DXC_DUPLICATION_MODE; }
  320. float x_home_pos(const uint8_t extruder);
  321. FORCE_INLINE int x_home_dir(const uint8_t extruder) { return extruder ? X2_HOME_DIR : X_HOME_DIR; }
  322. #else
  323. #if ENABLED(MULTI_NOZZLE_DUPLICATION)
  324. enum DualXMode : char { DXC_DUPLICATION_MODE = 2 };
  325. #endif
  326. FORCE_INLINE int x_home_dir(const uint8_t) { return home_dir(X_AXIS); }
  327. #endif
  328. #if HAS_M206_COMMAND
  329. void set_home_offset(const AxisEnum axis, const float v);
  330. #endif
  331. #if USE_SENSORLESS
  332. struct sensorless_t;
  333. sensorless_t start_sensorless_homing_per_axis(const AxisEnum axis);
  334. void end_sensorless_homing_per_axis(const AxisEnum axis, sensorless_t enable_stealth);
  335. #endif