My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

motion.h 14KB

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