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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 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. #pragma once
  30. #include "../inc/MarlinConfig.h"
  31. #if IS_SCARA
  32. #include "scara.h"
  33. #endif
  34. // Axis homed and known-position states
  35. extern uint8_t axis_homed, axis_known_position;
  36. constexpr uint8_t xyz_bits = _BV(X_AXIS) | _BV(Y_AXIS) | _BV(Z_AXIS);
  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 !(
  43. #if ENABLED(HOME_AFTER_DEACTIVATE)
  44. all_axes_known()
  45. #else
  46. all_axes_homed()
  47. #endif
  48. );
  49. }
  50. // Error margin to work around float imprecision
  51. constexpr float slop = 0.0001;
  52. extern bool relative_mode;
  53. extern float current_position[XYZE], // High-level current tool position
  54. destination[XYZE]; // Destination for a move
  55. // Scratch space for a cartesian result
  56. extern float cartes[XYZ];
  57. // Until kinematics.cpp is created, declare this here
  58. #if IS_KINEMATIC
  59. extern float delta[ABC];
  60. #endif
  61. #if HAS_ABL_NOT_UBL
  62. extern float xy_probe_feedrate_mm_s;
  63. #define XY_PROBE_FEEDRATE_MM_S xy_probe_feedrate_mm_s
  64. #elif defined(XY_PROBE_SPEED)
  65. #define XY_PROBE_FEEDRATE_MM_S MMM_TO_MMS(XY_PROBE_SPEED)
  66. #else
  67. #define XY_PROBE_FEEDRATE_MM_S PLANNER_XY_FEEDRATE()
  68. #endif
  69. /**
  70. * Feed rates are often configured with mm/m
  71. * but the planner and stepper like mm/s units.
  72. */
  73. extern const float homing_feedrate_mm_s[XYZ];
  74. FORCE_INLINE float homing_feedrate(const AxisEnum a) { return pgm_read_float(&homing_feedrate_mm_s[a]); }
  75. float get_homing_bump_feedrate(const AxisEnum axis);
  76. extern float feedrate_mm_s;
  77. /**
  78. * Feedrate scaling and conversion
  79. */
  80. extern int16_t feedrate_percentage;
  81. #define MMS_SCALED(MM_S) ((MM_S)*feedrate_percentage*0.01f)
  82. // The active extruder (tool). Set with T<extruder> command.
  83. #if EXTRUDERS > 1
  84. extern uint8_t active_extruder;
  85. #else
  86. constexpr uint8_t active_extruder = 0;
  87. #endif
  88. FORCE_INLINE float pgm_read_any(const float *p) { return pgm_read_float(p); }
  89. FORCE_INLINE signed char pgm_read_any(const signed char *p) { return pgm_read_byte(p); }
  90. #define XYZ_DEFS(type, array, CONFIG) \
  91. extern const type array##_P[XYZ]; \
  92. FORCE_INLINE type array(AxisEnum axis) { return pgm_read_any(&array##_P[axis]); } \
  93. typedef void __void_##CONFIG##__
  94. XYZ_DEFS(float, base_min_pos, MIN_POS);
  95. XYZ_DEFS(float, base_max_pos, MAX_POS);
  96. XYZ_DEFS(float, base_home_pos, HOME_POS);
  97. XYZ_DEFS(float, max_length, MAX_LENGTH);
  98. XYZ_DEFS(float, home_bump_mm, HOME_BUMP_MM);
  99. XYZ_DEFS(signed char, home_dir, HOME_DIR);
  100. #if HAS_WORKSPACE_OFFSET
  101. void update_workspace_offset(const AxisEnum axis);
  102. #else
  103. #define update_workspace_offset(x) NOOP
  104. #endif
  105. #if HAS_HOTEND_OFFSET
  106. extern float hotend_offset[XYZ][HOTENDS];
  107. void reset_hotend_offsets();
  108. #else
  109. constexpr float hotend_offset[XYZ][HOTENDS] = { { 0 }, { 0 }, { 0 } };
  110. #endif
  111. typedef struct { float min, max; } axis_limits_t;
  112. #if HAS_SOFTWARE_ENDSTOPS
  113. extern bool soft_endstops_enabled;
  114. extern axis_limits_t soft_endstop[XYZ];
  115. void apply_motion_limits(float target[XYZ]);
  116. void update_software_endstops(const AxisEnum axis
  117. #if HAS_HOTEND_OFFSET
  118. , const uint8_t old_tool_index=0, const uint8_t new_tool_index=0
  119. #endif
  120. );
  121. #else
  122. constexpr bool soft_endstops_enabled = false;
  123. //constexpr axis_limits_t soft_endstop[XYZ] = { { X_MIN_POS, X_MAX_POS }, { Y_MIN_POS, Y_MAX_POS }, { Z_MIN_POS, Z_MAX_POS } };
  124. #define apply_motion_limits(V) NOOP
  125. #define update_software_endstops(...) NOOP
  126. #endif
  127. void report_current_position();
  128. inline void set_current_from_destination() { COPY(current_position, destination); }
  129. inline void set_destination_from_current() { COPY(destination, current_position); }
  130. void get_cartesian_from_steppers();
  131. void set_current_from_steppers_for_axis(const AxisEnum axis);
  132. /**
  133. * sync_plan_position
  134. *
  135. * Set the planner/stepper positions directly from current_position with
  136. * no kinematic translation. Used for homing axes and cartesian/core syncing.
  137. */
  138. void sync_plan_position();
  139. void sync_plan_position_e();
  140. /**
  141. * Move the planner to the current position from wherever it last moved
  142. * (or from wherever it has been told it is located).
  143. */
  144. void line_to_current_position(const float &fr_mm_s=feedrate_mm_s);
  145. /**
  146. * Move the planner to the position stored in the destination array, which is
  147. * used by G0/G1/G2/G3/G5 and many other functions to set a destination.
  148. */
  149. void buffer_line_to_destination(const float fr_mm_s);
  150. #if IS_KINEMATIC
  151. void prepare_uninterpolated_move_to_destination(const float &fr_mm_s=0);
  152. #endif
  153. void prepare_move_to_destination();
  154. /**
  155. * Blocking movement and shorthand functions
  156. */
  157. void do_blocking_move_to(const float rx, const float ry, const float rz, const float &fr_mm_s=0);
  158. void do_blocking_move_to_x(const float &rx, const float &fr_mm_s=0);
  159. void do_blocking_move_to_z(const float &rz, const float &fr_mm_s=0);
  160. void do_blocking_move_to_xy(const float &rx, const float &ry, const float &fr_mm_s=0);
  161. FORCE_INLINE void do_blocking_move_to(const float (&raw)[XYZ], const float &fr_mm_s=0) {
  162. do_blocking_move_to(raw[X_AXIS], raw[Y_AXIS], raw[Z_AXIS], fr_mm_s);
  163. }
  164. FORCE_INLINE void do_blocking_move_to(const float (&raw)[XYZE], const float &fr_mm_s=0) {
  165. do_blocking_move_to(raw[X_AXIS], raw[Y_AXIS], raw[Z_AXIS], fr_mm_s);
  166. }
  167. void setup_for_endstop_or_probe_move();
  168. void clean_up_after_endstop_or_probe_move();
  169. //
  170. // Homing
  171. //
  172. bool axis_unhomed_error(const bool x=true, const bool y=true, const bool z=true);
  173. #if ENABLED(NO_MOTION_BEFORE_HOMING)
  174. #define MOTION_CONDITIONS (IsRunning() && !axis_unhomed_error())
  175. #else
  176. #define MOTION_CONDITIONS IsRunning()
  177. #endif
  178. void set_axis_is_at_home(const AxisEnum axis);
  179. void set_axis_is_not_at_home(const AxisEnum axis);
  180. void homeaxis(const AxisEnum axis);
  181. /**
  182. * Workspace offsets
  183. */
  184. #if HAS_HOME_OFFSET || HAS_POSITION_SHIFT
  185. #if HAS_HOME_OFFSET
  186. extern float home_offset[XYZ];
  187. #endif
  188. #if HAS_POSITION_SHIFT
  189. extern float position_shift[XYZ];
  190. #endif
  191. #if HAS_HOME_OFFSET && HAS_POSITION_SHIFT
  192. extern float workspace_offset[XYZ];
  193. #define WORKSPACE_OFFSET(AXIS) workspace_offset[AXIS]
  194. #elif HAS_HOME_OFFSET
  195. #define WORKSPACE_OFFSET(AXIS) home_offset[AXIS]
  196. #else
  197. #define WORKSPACE_OFFSET(AXIS) position_shift[AXIS]
  198. #endif
  199. #define NATIVE_TO_LOGICAL(POS, AXIS) ((POS) + WORKSPACE_OFFSET(AXIS))
  200. #define LOGICAL_TO_NATIVE(POS, AXIS) ((POS) - WORKSPACE_OFFSET(AXIS))
  201. #else
  202. #define NATIVE_TO_LOGICAL(POS, AXIS) (POS)
  203. #define LOGICAL_TO_NATIVE(POS, AXIS) (POS)
  204. #endif
  205. #define LOGICAL_X_POSITION(POS) NATIVE_TO_LOGICAL(POS, X_AXIS)
  206. #define LOGICAL_Y_POSITION(POS) NATIVE_TO_LOGICAL(POS, Y_AXIS)
  207. #define LOGICAL_Z_POSITION(POS) NATIVE_TO_LOGICAL(POS, Z_AXIS)
  208. #define RAW_X_POSITION(POS) LOGICAL_TO_NATIVE(POS, X_AXIS)
  209. #define RAW_Y_POSITION(POS) LOGICAL_TO_NATIVE(POS, Y_AXIS)
  210. #define RAW_Z_POSITION(POS) LOGICAL_TO_NATIVE(POS, Z_AXIS)
  211. /**
  212. * position_is_reachable family of functions
  213. */
  214. #if IS_KINEMATIC // (DELTA or SCARA)
  215. #if IS_SCARA
  216. extern const float L1, L2;
  217. #endif
  218. #if HAS_SCARA_OFFSET
  219. extern float scara_home_offset[ABC]; // A and B angular offsets, Z mm offset
  220. #endif
  221. // Return true if the given point is within the printable area
  222. inline bool position_is_reachable(const float &rx, const float &ry, const float inset=0) {
  223. #if ENABLED(DELTA)
  224. return HYPOT2(rx, ry) <= sq(DELTA_PRINTABLE_RADIUS - inset);
  225. #elif IS_SCARA
  226. const float R2 = HYPOT2(rx - SCARA_OFFSET_X, ry - SCARA_OFFSET_Y);
  227. return (
  228. R2 <= sq(L1 + L2) - inset
  229. #if MIDDLE_DEAD_ZONE_R > 0
  230. && R2 >= sq(float(MIDDLE_DEAD_ZONE_R))
  231. #endif
  232. );
  233. #endif
  234. }
  235. #if HAS_BED_PROBE
  236. // Return true if the both nozzle and the probe can reach the given point.
  237. // Note: This won't work on SCARA since the probe offset rotates with the arm.
  238. inline bool position_is_reachable_by_probe(const float &rx, const float &ry) {
  239. return position_is_reachable(rx - (X_PROBE_OFFSET_FROM_EXTRUDER), ry - (Y_PROBE_OFFSET_FROM_EXTRUDER))
  240. && position_is_reachable(rx, ry, ABS(MIN_PROBE_EDGE));
  241. }
  242. #endif
  243. #else // CARTESIAN
  244. // Return true if the given position is within the machine bounds.
  245. inline bool position_is_reachable(const float &rx, const float &ry) {
  246. if (!WITHIN(ry, Y_MIN_POS - slop, Y_MAX_POS + slop)) return false;
  247. #if ENABLED(DUAL_X_CARRIAGE)
  248. if (active_extruder)
  249. return WITHIN(rx, X2_MIN_POS - slop, X2_MAX_POS + slop);
  250. else
  251. return WITHIN(rx, X1_MIN_POS - slop, X1_MAX_POS + slop);
  252. #else
  253. return WITHIN(rx, X_MIN_POS - slop, X_MAX_POS + slop);
  254. #endif
  255. }
  256. #if HAS_BED_PROBE
  257. /**
  258. * Return whether the given position is within the bed, and whether the nozzle
  259. * can reach the position required to put the probe at the given position.
  260. *
  261. * Example: For a probe offset of -10,+10, then for the probe to reach 0,0 the
  262. * nozzle must be be able to reach +10,-10.
  263. */
  264. inline bool position_is_reachable_by_probe(const float &rx, const float &ry) {
  265. return position_is_reachable(rx - (X_PROBE_OFFSET_FROM_EXTRUDER), ry - (Y_PROBE_OFFSET_FROM_EXTRUDER))
  266. && WITHIN(rx, MIN_PROBE_X - slop, MAX_PROBE_X + slop)
  267. && WITHIN(ry, MIN_PROBE_Y - slop, MAX_PROBE_Y + slop);
  268. }
  269. #endif
  270. #endif // CARTESIAN
  271. #if !HAS_BED_PROBE
  272. FORCE_INLINE bool position_is_reachable_by_probe(const float &rx, const float &ry) { return position_is_reachable(rx, ry); }
  273. #endif
  274. /**
  275. * Duplication mode
  276. */
  277. #if HAS_DUPLICATION_MODE
  278. extern bool extruder_duplication_enabled, // Used in Dual X mode 2
  279. mirrored_duplication_mode; // Used in Dual X mode 3
  280. #if ENABLED(MULTI_NOZZLE_DUPLICATION)
  281. extern uint8_t duplication_e_mask;
  282. #endif
  283. #endif
  284. /**
  285. * Dual X Carriage
  286. */
  287. #if ENABLED(DUAL_X_CARRIAGE)
  288. enum DualXMode : char {
  289. DXC_FULL_CONTROL_MODE,
  290. DXC_AUTO_PARK_MODE,
  291. DXC_DUPLICATION_MODE,
  292. DXC_MIRRORED_MODE
  293. };
  294. extern DualXMode dual_x_carriage_mode;
  295. extern float inactive_extruder_x_pos, // Used in mode 0 & 1
  296. raised_parked_position[XYZE], // Used in mode 1
  297. duplicate_extruder_x_offset; // Used in mode 2 & 3
  298. extern bool active_extruder_parked; // Used in mode 1, 2 & 3
  299. extern millis_t delayed_move_time; // Used in mode 1
  300. extern int16_t duplicate_extruder_temp_offset; // Used in mode 2 & 3
  301. FORCE_INLINE bool dxc_is_duplicating() { return dual_x_carriage_mode >= DXC_DUPLICATION_MODE; }
  302. float x_home_pos(const int extruder);
  303. FORCE_INLINE int x_home_dir(const uint8_t extruder) { return extruder ? X2_HOME_DIR : X_HOME_DIR; }
  304. #elif ENABLED(MULTI_NOZZLE_DUPLICATION)
  305. enum DualXMode : char {
  306. DXC_DUPLICATION_MODE = 2
  307. };
  308. #endif
  309. #if HAS_M206_COMMAND
  310. void set_home_offset(const AxisEnum axis, const float v);
  311. #endif