Marlin.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. #ifndef MARLIN_H
  23. #define MARLIN_H
  24. #include <math.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <inttypes.h>
  29. #include <util/delay.h>
  30. #include <avr/eeprom.h>
  31. #include <avr/interrupt.h>
  32. #include "MarlinConfig.h"
  33. #ifdef DEBUG_GCODE_PARSER
  34. #include "parser.h"
  35. #endif
  36. #include "enum.h"
  37. #include "types.h"
  38. #include "fastio.h"
  39. #include "utility.h"
  40. #include "serial.h"
  41. void idle(
  42. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  43. bool no_stepper_sleep = false // pass true to keep steppers from disabling on timeout
  44. #endif
  45. );
  46. void manage_inactivity(const bool ignore_stepper_queue=false);
  47. extern const char axis_codes[XYZE];
  48. #if ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE)
  49. extern bool extruder_duplication_enabled;
  50. #endif
  51. #if HAS_X2_ENABLE
  52. #define enable_X() do{ X_ENABLE_WRITE( X_ENABLE_ON); X2_ENABLE_WRITE( X_ENABLE_ON); }while(0)
  53. #define disable_X() do{ X_ENABLE_WRITE(!X_ENABLE_ON); X2_ENABLE_WRITE(!X_ENABLE_ON); CBI(axis_known_position, X_AXIS); }while(0)
  54. #elif HAS_X_ENABLE
  55. #define enable_X() X_ENABLE_WRITE( X_ENABLE_ON)
  56. #define disable_X() do{ X_ENABLE_WRITE(!X_ENABLE_ON); CBI(axis_known_position, X_AXIS); }while(0)
  57. #else
  58. #define enable_X() NOOP
  59. #define disable_X() NOOP
  60. #endif
  61. #if HAS_Y2_ENABLE
  62. #define enable_Y() do{ Y_ENABLE_WRITE( Y_ENABLE_ON); Y2_ENABLE_WRITE(Y_ENABLE_ON); }while(0)
  63. #define disable_Y() do{ Y_ENABLE_WRITE(!Y_ENABLE_ON); Y2_ENABLE_WRITE(!Y_ENABLE_ON); CBI(axis_known_position, Y_AXIS); }while(0)
  64. #elif HAS_Y_ENABLE
  65. #define enable_Y() Y_ENABLE_WRITE( Y_ENABLE_ON)
  66. #define disable_Y() do{ Y_ENABLE_WRITE(!Y_ENABLE_ON); CBI(axis_known_position, Y_AXIS); }while(0)
  67. #else
  68. #define enable_Y() NOOP
  69. #define disable_Y() NOOP
  70. #endif
  71. #if HAS_Z2_ENABLE
  72. #define enable_Z() do{ Z_ENABLE_WRITE( Z_ENABLE_ON); Z2_ENABLE_WRITE(Z_ENABLE_ON); }while(0)
  73. #define disable_Z() do{ Z_ENABLE_WRITE(!Z_ENABLE_ON); Z2_ENABLE_WRITE(!Z_ENABLE_ON); CBI(axis_known_position, Z_AXIS); }while(0)
  74. #elif HAS_Z_ENABLE
  75. #define enable_Z() Z_ENABLE_WRITE( Z_ENABLE_ON)
  76. #define disable_Z() do{ Z_ENABLE_WRITE(!Z_ENABLE_ON); CBI(axis_known_position, Z_AXIS); }while(0)
  77. #else
  78. #define enable_Z() NOOP
  79. #define disable_Z() NOOP
  80. #endif
  81. #if ENABLED(MIXING_EXTRUDER)
  82. /**
  83. * Mixing steppers synchronize their enable (and direction) together
  84. */
  85. #if MIXING_STEPPERS > 4
  86. #define enable_E0() { E0_ENABLE_WRITE( E_ENABLE_ON); E1_ENABLE_WRITE( E_ENABLE_ON); E2_ENABLE_WRITE( E_ENABLE_ON); E3_ENABLE_WRITE( E_ENABLE_ON); E4_ENABLE_WRITE( E_ENABLE_ON); }
  87. #define disable_E0() { E0_ENABLE_WRITE(!E_ENABLE_ON); E1_ENABLE_WRITE(!E_ENABLE_ON); E2_ENABLE_WRITE(!E_ENABLE_ON); E3_ENABLE_WRITE(!E_ENABLE_ON); E4_ENABLE_WRITE(!E_ENABLE_ON); }
  88. #elif MIXING_STEPPERS > 3
  89. #define enable_E0() { E0_ENABLE_WRITE( E_ENABLE_ON); E1_ENABLE_WRITE( E_ENABLE_ON); E2_ENABLE_WRITE( E_ENABLE_ON); E3_ENABLE_WRITE( E_ENABLE_ON); }
  90. #define disable_E0() { E0_ENABLE_WRITE(!E_ENABLE_ON); E1_ENABLE_WRITE(!E_ENABLE_ON); E2_ENABLE_WRITE(!E_ENABLE_ON); E3_ENABLE_WRITE(!E_ENABLE_ON); }
  91. #elif MIXING_STEPPERS > 2
  92. #define enable_E0() { E0_ENABLE_WRITE( E_ENABLE_ON); E1_ENABLE_WRITE( E_ENABLE_ON); E2_ENABLE_WRITE( E_ENABLE_ON); }
  93. #define disable_E0() { E0_ENABLE_WRITE(!E_ENABLE_ON); E1_ENABLE_WRITE(!E_ENABLE_ON); E2_ENABLE_WRITE(!E_ENABLE_ON); }
  94. #else
  95. #define enable_E0() { E0_ENABLE_WRITE( E_ENABLE_ON); E1_ENABLE_WRITE( E_ENABLE_ON); }
  96. #define disable_E0() { E0_ENABLE_WRITE(!E_ENABLE_ON); E1_ENABLE_WRITE(!E_ENABLE_ON); }
  97. #endif
  98. #define enable_E1() NOOP
  99. #define disable_E1() NOOP
  100. #define enable_E2() NOOP
  101. #define disable_E2() NOOP
  102. #define enable_E3() NOOP
  103. #define disable_E3() NOOP
  104. #define enable_E4() NOOP
  105. #define disable_E4() NOOP
  106. #else // !MIXING_EXTRUDER
  107. #if HAS_E0_ENABLE
  108. #define enable_E0() E0_ENABLE_WRITE( E_ENABLE_ON)
  109. #define disable_E0() E0_ENABLE_WRITE(!E_ENABLE_ON)
  110. #else
  111. #define enable_E0() NOOP
  112. #define disable_E0() NOOP
  113. #endif
  114. #if E_STEPPERS > 1 && HAS_E1_ENABLE
  115. #define enable_E1() E1_ENABLE_WRITE( E_ENABLE_ON)
  116. #define disable_E1() E1_ENABLE_WRITE(!E_ENABLE_ON)
  117. #else
  118. #define enable_E1() NOOP
  119. #define disable_E1() NOOP
  120. #endif
  121. #if E_STEPPERS > 2 && HAS_E2_ENABLE
  122. #define enable_E2() E2_ENABLE_WRITE( E_ENABLE_ON)
  123. #define disable_E2() E2_ENABLE_WRITE(!E_ENABLE_ON)
  124. #else
  125. #define enable_E2() NOOP
  126. #define disable_E2() NOOP
  127. #endif
  128. #if E_STEPPERS > 3 && HAS_E3_ENABLE
  129. #define enable_E3() E3_ENABLE_WRITE( E_ENABLE_ON)
  130. #define disable_E3() E3_ENABLE_WRITE(!E_ENABLE_ON)
  131. #else
  132. #define enable_E3() NOOP
  133. #define disable_E3() NOOP
  134. #endif
  135. #if E_STEPPERS > 4 && HAS_E4_ENABLE
  136. #define enable_E4() E4_ENABLE_WRITE( E_ENABLE_ON)
  137. #define disable_E4() E4_ENABLE_WRITE(!E_ENABLE_ON)
  138. #else
  139. #define enable_E4() NOOP
  140. #define disable_E4() NOOP
  141. #endif
  142. #endif // !MIXING_EXTRUDER
  143. #if ENABLED(HANGPRINTER)
  144. #define enable_A() enable_X()
  145. #define enable_B() enable_Y()
  146. #define enable_C() enable_Z()
  147. #define __D_ENABLE(p) E##p##_ENABLE_WRITE(E_ENABLE_ON)
  148. #define _D_ENABLE(p) __D_ENABLE(p)
  149. #define enable_D() _D_ENABLE(EXTRUDERS)
  150. // Don't allow any axes to be disabled
  151. #undef disable_X
  152. #undef disable_Y
  153. #undef disable_Z
  154. #define disable_X() NOOP
  155. #define disable_Y() NOOP
  156. #define disable_Z() NOOP
  157. #if EXTRUDERS >= 1
  158. #undef disable_E1
  159. #define disable_E1() NOOP
  160. #if EXTRUDERS >= 2
  161. #undef disable_E2
  162. #define disable_E2() NOOP
  163. #if EXTRUDERS >= 3
  164. #undef disable_E3
  165. #define disable_E3() NOOP
  166. #if EXTRUDERS >= 4
  167. #undef disable_E4
  168. #define disable_E4() NOOP
  169. #endif // EXTRUDERS >= 4
  170. #endif // EXTRUDERS >= 3
  171. #endif // EXTRUDERS >= 2
  172. #endif // EXTRUDERS >= 1
  173. #endif // HANGPRINTER
  174. #if ENABLED(G38_PROBE_TARGET)
  175. extern bool G38_move, // flag to tell the interrupt handler that a G38 command is being run
  176. G38_endstop_hit; // flag from the interrupt handler to indicate if the endstop went active
  177. #endif
  178. void enable_all_steppers();
  179. void disable_e_stepper(const uint8_t e);
  180. void disable_e_steppers();
  181. void disable_all_steppers();
  182. void sync_plan_position();
  183. void sync_plan_position_e();
  184. #if IS_KINEMATIC
  185. void sync_plan_position_kinematic();
  186. #define SYNC_PLAN_POSITION_KINEMATIC() sync_plan_position_kinematic()
  187. #else
  188. #define SYNC_PLAN_POSITION_KINEMATIC() sync_plan_position()
  189. #endif
  190. void flush_and_request_resend();
  191. void ok_to_send();
  192. void kill(const char*);
  193. void quickstop_stepper();
  194. extern uint8_t marlin_debug_flags;
  195. #define DEBUGGING(F) (marlin_debug_flags & (DEBUG_## F))
  196. extern bool Running;
  197. inline bool IsRunning() { return Running; }
  198. inline bool IsStopped() { return !Running; }
  199. bool enqueue_and_echo_command(const char* cmd); // Add a single command to the end of the buffer. Return false on failure.
  200. void enqueue_and_echo_commands_P(const char * const cmd); // Set one or more commands to be prioritized over the next Serial/SD command.
  201. void clear_command_queue();
  202. #if ENABLED(M100_FREE_MEMORY_WATCHER) || ENABLED(POWER_LOSS_RECOVERY)
  203. extern char command_queue[BUFSIZE][MAX_CMD_SIZE];
  204. #endif
  205. #define HAS_LCD_QUEUE_NOW (ENABLED(MALYAN_LCD) || (ENABLED(ULTIPANEL) && (ENABLED(AUTO_BED_LEVELING_UBL) || ENABLED(PID_AUTOTUNE_MENU) || ENABLED(ADVANCED_PAUSE_FEATURE))))
  206. #define HAS_QUEUE_NOW (ENABLED(SDSUPPORT) || HAS_LCD_QUEUE_NOW)
  207. #if HAS_QUEUE_NOW
  208. // Return only when commands are actually enqueued
  209. void enqueue_and_echo_command_now(const char* cmd);
  210. #if HAS_LCD_QUEUE_NOW
  211. void enqueue_and_echo_commands_now_P(const char * const cmd);
  212. #endif
  213. #endif
  214. extern millis_t previous_move_ms;
  215. inline void reset_stepper_timeout() { previous_move_ms = millis(); }
  216. /**
  217. * Feedrate scaling and conversion
  218. */
  219. extern float feedrate_mm_s;
  220. extern int16_t feedrate_percentage;
  221. #define MMS_SCALED(MM_S) ((MM_S)*feedrate_percentage*0.01f)
  222. extern bool axis_relative_modes[XYZE];
  223. extern uint8_t axis_homed, axis_known_position;
  224. constexpr uint8_t xyz_bits = _BV(X_AXIS) | _BV(Y_AXIS) | _BV(Z_AXIS);
  225. FORCE_INLINE bool all_axes_homed() { return (axis_homed & xyz_bits) == xyz_bits; }
  226. FORCE_INLINE bool all_axes_known() { return (axis_known_position & xyz_bits) == xyz_bits; }
  227. extern volatile bool wait_for_heatup;
  228. #if HAS_RESUME_CONTINUE
  229. extern volatile bool wait_for_user;
  230. #endif
  231. #if HAS_AUTO_REPORTING || ENABLED(HOST_KEEPALIVE_FEATURE)
  232. extern bool suspend_auto_report;
  233. #endif
  234. extern float current_position[XYZE], destination[XYZE];
  235. /**
  236. * Workspace offsets
  237. */
  238. #if HAS_WORKSPACE_OFFSET
  239. #if HAS_HOME_OFFSET
  240. extern float home_offset[XYZ];
  241. #endif
  242. #if HAS_POSITION_SHIFT
  243. extern float position_shift[XYZ];
  244. #endif
  245. #if HAS_HOME_OFFSET && HAS_POSITION_SHIFT
  246. extern float workspace_offset[XYZ];
  247. #define WORKSPACE_OFFSET(AXIS) workspace_offset[AXIS]
  248. #elif HAS_HOME_OFFSET
  249. #define WORKSPACE_OFFSET(AXIS) home_offset[AXIS]
  250. #elif HAS_POSITION_SHIFT
  251. #define WORKSPACE_OFFSET(AXIS) position_shift[AXIS]
  252. #endif
  253. #define NATIVE_TO_LOGICAL(POS, AXIS) ((POS) + WORKSPACE_OFFSET(AXIS))
  254. #define LOGICAL_TO_NATIVE(POS, AXIS) ((POS) - WORKSPACE_OFFSET(AXIS))
  255. #else
  256. #define NATIVE_TO_LOGICAL(POS, AXIS) (POS)
  257. #define LOGICAL_TO_NATIVE(POS, AXIS) (POS)
  258. #endif
  259. #define LOGICAL_X_POSITION(POS) NATIVE_TO_LOGICAL(POS, X_AXIS)
  260. #define LOGICAL_Y_POSITION(POS) NATIVE_TO_LOGICAL(POS, Y_AXIS)
  261. #define LOGICAL_Z_POSITION(POS) NATIVE_TO_LOGICAL(POS, Z_AXIS)
  262. #define RAW_X_POSITION(POS) LOGICAL_TO_NATIVE(POS, X_AXIS)
  263. #define RAW_Y_POSITION(POS) LOGICAL_TO_NATIVE(POS, Y_AXIS)
  264. #define RAW_Z_POSITION(POS) LOGICAL_TO_NATIVE(POS, Z_AXIS)
  265. // Hotend Offsets
  266. #if HOTENDS > 1
  267. extern float hotend_offset[XYZ][HOTENDS];
  268. #endif
  269. // Software Endstops
  270. extern float soft_endstop_min[XYZ], soft_endstop_max[XYZ];
  271. #if HAS_SOFTWARE_ENDSTOPS
  272. extern bool soft_endstops_enabled;
  273. void clamp_to_software_endstops(float target[XYZ]);
  274. #else
  275. #define soft_endstops_enabled false
  276. #define clamp_to_software_endstops(x) NOOP
  277. #endif
  278. #if HAS_WORKSPACE_OFFSET || ENABLED(DUAL_X_CARRIAGE)
  279. void update_software_endstops(const AxisEnum axis);
  280. #endif
  281. #define MAX_COORDINATE_SYSTEMS 9
  282. #if ENABLED(CNC_COORDINATE_SYSTEMS)
  283. extern float coordinate_system[MAX_COORDINATE_SYSTEMS][XYZ];
  284. bool select_coordinate_system(const int8_t _new);
  285. #endif
  286. void tool_change(const uint8_t tmp_extruder, const float fr_mm_s=0.0, bool no_move=false);
  287. void home_all_axes();
  288. void report_current_position();
  289. #if IS_KINEMATIC
  290. #if ENABLED(HANGPRINTER)
  291. extern float line_lengths[ABCD];
  292. #else
  293. extern float delta[ABC];
  294. #endif
  295. void inverse_kinematics(const float raw[XYZ]);
  296. #endif
  297. #if ENABLED(DELTA)
  298. extern float delta_height,
  299. delta_endstop_adj[ABC],
  300. delta_radius,
  301. delta_tower_angle_trim[ABC],
  302. delta_tower[ABC][2],
  303. delta_diagonal_rod,
  304. delta_calibration_radius,
  305. delta_diagonal_rod_2_tower[ABC],
  306. delta_segments_per_second,
  307. delta_clip_start_height;
  308. void recalc_delta_settings();
  309. float delta_safe_distance_from_top();
  310. // Macro to obtain the Z position of an individual tower
  311. #define DELTA_Z(V,T) V[Z_AXIS] + SQRT( \
  312. delta_diagonal_rod_2_tower[T] - HYPOT2( \
  313. delta_tower[T][X_AXIS] - V[X_AXIS], \
  314. delta_tower[T][Y_AXIS] - V[Y_AXIS] \
  315. ) \
  316. )
  317. #define DELTA_IK(V) do { \
  318. delta[A_AXIS] = DELTA_Z(V, A_AXIS); \
  319. delta[B_AXIS] = DELTA_Z(V, B_AXIS); \
  320. delta[C_AXIS] = DELTA_Z(V, C_AXIS); \
  321. }while(0)
  322. #elif ENABLED(HANGPRINTER)
  323. // Don't collect anchor positions in array because there are no A_x, D_x or D_y
  324. extern float anchor_A_y,
  325. anchor_A_z,
  326. anchor_B_x,
  327. anchor_B_y,
  328. anchor_B_z,
  329. anchor_C_x,
  330. anchor_C_y,
  331. anchor_C_z,
  332. anchor_D_z,
  333. delta_segments_per_second,
  334. line_lengths_origin[ABCD];
  335. void recalc_hangprinter_settings();
  336. #define HANGPRINTER_IK(V) do { \
  337. line_lengths[A_AXIS] = SQRT(sq(anchor_A_z - V[Z_AXIS]) \
  338. + sq(anchor_A_y - V[Y_AXIS]) \
  339. + sq( V[X_AXIS])); \
  340. line_lengths[B_AXIS] = SQRT(sq(anchor_B_z - V[Z_AXIS]) \
  341. + sq(anchor_B_y - V[Y_AXIS]) \
  342. + sq(anchor_B_x - V[X_AXIS])); \
  343. line_lengths[C_AXIS] = SQRT(sq(anchor_C_z - V[Z_AXIS]) \
  344. + sq(anchor_C_y - V[Y_AXIS]) \
  345. + sq(anchor_C_x - V[X_AXIS])); \
  346. line_lengths[D_AXIS] = SQRT(sq( V[X_AXIS]) \
  347. + sq( V[Y_AXIS]) \
  348. + sq(anchor_D_z - V[Z_AXIS])); \
  349. }while(0)
  350. // Inverse kinematics at origin
  351. #define HANGPRINTER_IK_ORIGIN(LL) do { \
  352. LL[A_AXIS] = SQRT(sq(anchor_A_z) \
  353. + sq(anchor_A_y)); \
  354. LL[B_AXIS] = SQRT(sq(anchor_B_z) \
  355. + sq(anchor_B_y) \
  356. + sq(anchor_B_x)); \
  357. LL[C_AXIS] = SQRT(sq(anchor_C_z) \
  358. + sq(anchor_C_y) \
  359. + sq(anchor_C_x)); \
  360. LL[D_AXIS] = anchor_D_z; \
  361. }while(0)
  362. #elif IS_SCARA
  363. void forward_kinematics_SCARA(const float &a, const float &b);
  364. #endif
  365. #if ENABLED(G26_MESH_VALIDATION)
  366. extern bool g26_debug_flag;
  367. #elif ENABLED(AUTO_BED_LEVELING_UBL)
  368. constexpr bool g26_debug_flag = false;
  369. #endif
  370. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  371. #define _GET_MESH_X(I) (bilinear_start[X_AXIS] + (I) * bilinear_grid_spacing[X_AXIS])
  372. #define _GET_MESH_Y(J) (bilinear_start[Y_AXIS] + (J) * bilinear_grid_spacing[Y_AXIS])
  373. #elif ENABLED(AUTO_BED_LEVELING_UBL)
  374. #define _GET_MESH_X(I) ubl.mesh_index_to_xpos(I)
  375. #define _GET_MESH_Y(J) ubl.mesh_index_to_ypos(J)
  376. #elif ENABLED(MESH_BED_LEVELING)
  377. #define _GET_MESH_X(I) mbl.index_to_xpos[I]
  378. #define _GET_MESH_Y(J) mbl.index_to_ypos[J]
  379. #endif
  380. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  381. extern int bilinear_grid_spacing[2], bilinear_start[2];
  382. extern float bilinear_grid_factor[2],
  383. z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y];
  384. float bilinear_z_offset(const float raw[XYZ]);
  385. #endif
  386. #if ENABLED(AUTO_BED_LEVELING_BILINEAR) || ENABLED(MESH_BED_LEVELING)
  387. typedef float (*element_2d_fn)(const uint8_t, const uint8_t);
  388. void print_2d_array(const uint8_t sx, const uint8_t sy, const uint8_t precision, const element_2d_fn fn);
  389. #endif
  390. #if HAS_LEVELING
  391. bool leveling_is_valid();
  392. void set_bed_leveling_enabled(const bool enable=true);
  393. void reset_bed_level();
  394. #endif
  395. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  396. void set_z_fade_height(const float zfh, const bool do_report=true);
  397. #endif
  398. #if HAS_BED_PROBE
  399. extern float zprobe_zoffset;
  400. bool set_probe_deployed(const bool deploy);
  401. #ifdef Z_AFTER_PROBING
  402. void move_z_after_probing();
  403. #endif
  404. enum ProbePtRaise : unsigned char {
  405. PROBE_PT_NONE, // No raise or stow after run_z_probe
  406. PROBE_PT_STOW, // Do a complete stow after run_z_probe
  407. PROBE_PT_RAISE, // Raise to "between" clearance after run_z_probe
  408. PROBE_PT_BIG_RAISE // Raise to big clearance after run_z_probe
  409. };
  410. float probe_pt(const float &rx, const float &ry, const ProbePtRaise raise_after=PROBE_PT_NONE, const uint8_t verbose_level=0, const bool probe_relative=true);
  411. #define DEPLOY_PROBE() set_probe_deployed(true)
  412. #define STOW_PROBE() set_probe_deployed(false)
  413. #else
  414. #define DEPLOY_PROBE()
  415. #define STOW_PROBE()
  416. #endif
  417. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  418. extern MarlinBusyState busy_state;
  419. #define KEEPALIVE_STATE(n) do{ busy_state = n; }while(0)
  420. #else
  421. #define KEEPALIVE_STATE(n) NOOP
  422. #endif
  423. #if FAN_COUNT > 0
  424. extern int16_t fanSpeeds[FAN_COUNT];
  425. #if ENABLED(EXTRA_FAN_SPEED)
  426. extern int16_t old_fanSpeeds[FAN_COUNT],
  427. new_fanSpeeds[FAN_COUNT];
  428. #endif
  429. #if ENABLED(PROBING_FANS_OFF)
  430. extern bool fans_paused;
  431. extern int16_t paused_fanSpeeds[FAN_COUNT];
  432. #endif
  433. #endif
  434. #if ENABLED(USE_CONTROLLER_FAN)
  435. extern int controllerFanSpeed;
  436. #endif
  437. #if ENABLED(BARICUDA)
  438. extern uint8_t baricuda_valve_pressure, baricuda_e_to_p_pressure;
  439. #endif
  440. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  441. extern bool filament_sensor; // Flag that filament sensor readings should control extrusion
  442. extern float filament_width_nominal, // Theoretical filament diameter i.e., 3.00 or 1.75
  443. filament_width_meas; // Measured filament diameter
  444. extern uint8_t meas_delay_cm; // Delay distance
  445. extern int8_t measurement_delay[MAX_MEASUREMENT_DELAY + 1], // Ring buffer to delay measurement
  446. filwidth_delay_index[2]; // Ring buffer indexes. Used by planner, temperature, and main code
  447. #endif
  448. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  449. extern int8_t did_pause_print;
  450. extern AdvancedPauseMenuResponse advanced_pause_menu_response;
  451. extern float filament_change_unload_length[EXTRUDERS],
  452. filament_change_load_length[EXTRUDERS];
  453. #endif
  454. #if HAS_POWER_SWITCH
  455. extern bool powersupply_on;
  456. #define PSU_PIN_ON() do{ OUT_WRITE(PS_ON_PIN, PS_ON_AWAKE); powersupply_on = true; }while(0)
  457. #define PSU_PIN_OFF() do{ OUT_WRITE(PS_ON_PIN, PS_ON_ASLEEP); powersupply_on = false; }while(0)
  458. #endif
  459. // Handling multiple extruders pins
  460. extern uint8_t active_extruder;
  461. #if ENABLED(MIXING_EXTRUDER)
  462. extern float mixing_factor[MIXING_STEPPERS];
  463. #endif
  464. inline void set_current_from_destination() { COPY(current_position, destination); }
  465. inline void set_destination_from_current() { COPY(destination, current_position); }
  466. void prepare_move_to_destination();
  467. /**
  468. * Blocking movement and shorthand functions
  469. */
  470. void do_blocking_move_to(const float rx, const float ry, const float rz, const float &fr_mm_s=0);
  471. void do_blocking_move_to_x(const float &rx, const float &fr_mm_s=0);
  472. void do_blocking_move_to_z(const float &rz, const float &fr_mm_s=0);
  473. void do_blocking_move_to_xy(const float &rx, const float &ry, const float &fr_mm_s=0);
  474. #if ENABLED(ARC_SUPPORT)
  475. void plan_arc(const float(&cart)[XYZE], const float(&offset)[2], const bool clockwise);
  476. #endif
  477. #define HAS_AXIS_UNHOMED_ERR ( \
  478. ENABLED(Z_PROBE_ALLEN_KEY) \
  479. || ENABLED(Z_PROBE_SLED) \
  480. || HAS_PROBING_PROCEDURE \
  481. || HOTENDS > 1 \
  482. || ENABLED(NOZZLE_CLEAN_FEATURE) \
  483. || ENABLED(NOZZLE_PARK_FEATURE) \
  484. || (ENABLED(ADVANCED_PAUSE_FEATURE) && ENABLED(HOME_BEFORE_FILAMENT_CHANGE)) \
  485. || HAS_M206_COMMAND \
  486. ) || ENABLED(NO_MOTION_BEFORE_HOMING)
  487. #if HAS_AXIS_UNHOMED_ERR
  488. bool axis_unhomed_error(const bool x=true, const bool y=true, const bool z=true);
  489. #endif
  490. /**
  491. * position_is_reachable family of functions
  492. */
  493. #if IS_KINEMATIC // (DELTA or SCARA)
  494. #if IS_SCARA
  495. extern const float L1, L2;
  496. #endif
  497. // Return true if the given point is within the printable area
  498. inline bool position_is_reachable(const float &rx, const float &ry, const float inset=0) {
  499. #if ENABLED(DELTA)
  500. return HYPOT2(rx, ry) <= sq(DELTA_PRINTABLE_RADIUS - inset);
  501. #elif ENABLED(HANGPRINTER)
  502. // TODO: This is over simplified. Hangprinter's build volume is _not_ cylindrical.
  503. return HYPOT2(rx, ry) <= sq(HANGPRINTER_PRINTABLE_RADIUS - inset);
  504. #elif IS_SCARA
  505. const float R2 = HYPOT2(rx - SCARA_OFFSET_X, ry - SCARA_OFFSET_Y);
  506. return (
  507. R2 <= sq(L1 + L2) - inset
  508. #if MIDDLE_DEAD_ZONE_R > 0
  509. && R2 >= sq(float(MIDDLE_DEAD_ZONE_R))
  510. #endif
  511. );
  512. #endif
  513. }
  514. #if HAS_BED_PROBE
  515. // Return true if the both nozzle and the probe can reach the given point.
  516. // Note: This won't work on SCARA since the probe offset rotates with the arm.
  517. inline bool position_is_reachable_by_probe(const float &rx, const float &ry) {
  518. return position_is_reachable(rx - (X_PROBE_OFFSET_FROM_EXTRUDER), ry - (Y_PROBE_OFFSET_FROM_EXTRUDER))
  519. && position_is_reachable(rx, ry, ABS(MIN_PROBE_EDGE));
  520. }
  521. #endif
  522. #else // CARTESIAN
  523. // Return true if the given position is within the machine bounds.
  524. inline bool position_is_reachable(const float &rx, const float &ry) {
  525. // Add 0.001 margin to deal with float imprecision
  526. return WITHIN(rx, X_MIN_POS - 0.001f, X_MAX_POS + 0.001f)
  527. && WITHIN(ry, Y_MIN_POS - 0.001f, Y_MAX_POS + 0.001f);
  528. }
  529. #if HAS_BED_PROBE
  530. /**
  531. * Return whether the given position is within the bed, and whether the nozzle
  532. * can reach the position required to put the probe at the given position.
  533. *
  534. * Example: For a probe offset of -10,+10, then for the probe to reach 0,0 the
  535. * nozzle must be be able to reach +10,-10.
  536. */
  537. inline bool position_is_reachable_by_probe(const float &rx, const float &ry) {
  538. return position_is_reachable(rx - (X_PROBE_OFFSET_FROM_EXTRUDER), ry - (Y_PROBE_OFFSET_FROM_EXTRUDER))
  539. && WITHIN(rx, MIN_PROBE_X - 0.001f, MAX_PROBE_X + 0.001f)
  540. && WITHIN(ry, MIN_PROBE_Y - 0.001f, MAX_PROBE_Y + 0.001f);
  541. }
  542. #endif
  543. #endif // CARTESIAN
  544. #if !HAS_BED_PROBE
  545. FORCE_INLINE bool position_is_reachable_by_probe(const float &rx, const float &ry) { return position_is_reachable(rx, ry); }
  546. #endif
  547. #endif // MARLIN_H