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.

Marlin.h 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 "MarlinConfig.h"
  30. #ifdef DEBUG_GCODE_PARSER
  31. #include "gcode.h"
  32. #endif
  33. #include "src/HAL/HAL.h"
  34. #include "enum.h"
  35. #include "types.h"
  36. #include "utility.h"
  37. #include "serial.h"
  38. #if ENABLED(PRINTCOUNTER)
  39. #include "printcounter.h"
  40. #else
  41. #include "stopwatch.h"
  42. #endif
  43. void idle(
  44. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  45. bool no_stepper_sleep = false // pass true to keep steppers from disabling on timeout
  46. #endif
  47. );
  48. void manage_inactivity(bool ignore_stepper_queue = false);
  49. #if ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE)
  50. extern bool extruder_duplication_enabled;
  51. #endif
  52. #if HAS_X2_ENABLE
  53. #define enable_X() do{ X_ENABLE_WRITE( X_ENABLE_ON); X2_ENABLE_WRITE( X_ENABLE_ON); }while(0)
  54. #define disable_X() do{ X_ENABLE_WRITE(!X_ENABLE_ON); X2_ENABLE_WRITE(!X_ENABLE_ON); axis_known_position[X_AXIS] = false; }while(0)
  55. #elif HAS_X_ENABLE
  56. #define enable_X() X_ENABLE_WRITE( X_ENABLE_ON)
  57. #define disable_X() do{ X_ENABLE_WRITE(!X_ENABLE_ON); axis_known_position[X_AXIS] = false; }while(0)
  58. #else
  59. #define enable_X() NOOP
  60. #define disable_X() NOOP
  61. #endif
  62. #if HAS_Y2_ENABLE
  63. #define enable_Y() do{ Y_ENABLE_WRITE( Y_ENABLE_ON); Y2_ENABLE_WRITE(Y_ENABLE_ON); }while(0)
  64. #define disable_Y() do{ Y_ENABLE_WRITE(!Y_ENABLE_ON); Y2_ENABLE_WRITE(!Y_ENABLE_ON); axis_known_position[Y_AXIS] = false; }while(0)
  65. #elif HAS_Y_ENABLE
  66. #define enable_Y() Y_ENABLE_WRITE( Y_ENABLE_ON)
  67. #define disable_Y() do{ Y_ENABLE_WRITE(!Y_ENABLE_ON); axis_known_position[Y_AXIS] = false; }while(0)
  68. #else
  69. #define enable_Y() NOOP
  70. #define disable_Y() NOOP
  71. #endif
  72. #if HAS_Z2_ENABLE
  73. #define enable_Z() do{ Z_ENABLE_WRITE( Z_ENABLE_ON); Z2_ENABLE_WRITE(Z_ENABLE_ON); }while(0)
  74. #define disable_Z() do{ Z_ENABLE_WRITE(!Z_ENABLE_ON); Z2_ENABLE_WRITE(!Z_ENABLE_ON); axis_known_position[Z_AXIS] = false; }while(0)
  75. #elif HAS_Z_ENABLE
  76. #define enable_Z() Z_ENABLE_WRITE( Z_ENABLE_ON)
  77. #define disable_Z() do{ Z_ENABLE_WRITE(!Z_ENABLE_ON); axis_known_position[Z_AXIS] = false; }while(0)
  78. #else
  79. #define enable_Z() NOOP
  80. #define disable_Z() NOOP
  81. #endif
  82. #if ENABLED(MIXING_EXTRUDER)
  83. /**
  84. * Mixing steppers synchronize their enable (and direction) together
  85. */
  86. #if MIXING_STEPPERS > 3
  87. #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); }
  88. #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); }
  89. #elif MIXING_STEPPERS > 2
  90. #define enable_E0() { E0_ENABLE_WRITE( E_ENABLE_ON); E1_ENABLE_WRITE( E_ENABLE_ON); E2_ENABLE_WRITE( E_ENABLE_ON); }
  91. #define disable_E0() { E0_ENABLE_WRITE(!E_ENABLE_ON); E1_ENABLE_WRITE(!E_ENABLE_ON); E2_ENABLE_WRITE(!E_ENABLE_ON); }
  92. #else
  93. #define enable_E0() { E0_ENABLE_WRITE( E_ENABLE_ON); E1_ENABLE_WRITE( E_ENABLE_ON); }
  94. #define disable_E0() { E0_ENABLE_WRITE(!E_ENABLE_ON); E1_ENABLE_WRITE(!E_ENABLE_ON); }
  95. #endif
  96. #define enable_E1() NOOP
  97. #define disable_E1() NOOP
  98. #define enable_E2() NOOP
  99. #define disable_E2() NOOP
  100. #define enable_E3() NOOP
  101. #define disable_E3() NOOP
  102. #define enable_E4() NOOP
  103. #define disable_E4() NOOP
  104. #else // !MIXING_EXTRUDER
  105. #if HAS_E0_ENABLE
  106. #define enable_E0() E0_ENABLE_WRITE( E_ENABLE_ON)
  107. #define disable_E0() E0_ENABLE_WRITE(!E_ENABLE_ON)
  108. #else
  109. #define enable_E0() NOOP
  110. #define disable_E0() NOOP
  111. #endif
  112. #if E_STEPPERS > 1 && HAS_E1_ENABLE
  113. #define enable_E1() E1_ENABLE_WRITE( E_ENABLE_ON)
  114. #define disable_E1() E1_ENABLE_WRITE(!E_ENABLE_ON)
  115. #else
  116. #define enable_E1() NOOP
  117. #define disable_E1() NOOP
  118. #endif
  119. #if E_STEPPERS > 2 && HAS_E2_ENABLE
  120. #define enable_E2() E2_ENABLE_WRITE( E_ENABLE_ON)
  121. #define disable_E2() E2_ENABLE_WRITE(!E_ENABLE_ON)
  122. #else
  123. #define enable_E2() NOOP
  124. #define disable_E2() NOOP
  125. #endif
  126. #if E_STEPPERS > 3 && HAS_E3_ENABLE
  127. #define enable_E3() E3_ENABLE_WRITE( E_ENABLE_ON)
  128. #define disable_E3() E3_ENABLE_WRITE(!E_ENABLE_ON)
  129. #else
  130. #define enable_E3() NOOP
  131. #define disable_E3() NOOP
  132. #endif
  133. #if E_STEPPERS > 4 && HAS_E4_ENABLE
  134. #define enable_E4() E4_ENABLE_WRITE( E_ENABLE_ON)
  135. #define disable_E4() E4_ENABLE_WRITE(!E_ENABLE_ON)
  136. #else
  137. #define enable_E4() NOOP
  138. #define disable_E4() NOOP
  139. #endif
  140. #endif // !MIXING_EXTRUDER
  141. #if ENABLED(G38_PROBE_TARGET)
  142. extern bool G38_move, // flag to tell the interrupt handler that a G38 command is being run
  143. G38_endstop_hit; // flag from the interrupt handler to indicate if the endstop went active
  144. #endif
  145. /**
  146. * The axis order in all axis related arrays is X, Y, Z, E
  147. */
  148. #define _AXIS(AXIS) AXIS ##_AXIS
  149. void enable_all_steppers();
  150. void disable_e_steppers();
  151. void disable_all_steppers();
  152. void FlushSerialRequestResend();
  153. void ok_to_send();
  154. void kill(const char*);
  155. void quickstop_stepper();
  156. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  157. void handle_filament_runout();
  158. #endif
  159. extern uint8_t marlin_debug_flags;
  160. #define DEBUGGING(F) (marlin_debug_flags & (DEBUG_## F))
  161. extern bool Running;
  162. inline bool IsRunning() { return Running; }
  163. inline bool IsStopped() { return !Running; }
  164. bool enqueue_and_echo_command(const char* cmd, bool say_ok=false); // Add a single command to the end of the buffer. Return false on failure.
  165. void enqueue_and_echo_commands_P(const char * const cmd); // Set one or more commands to be prioritized over the next Serial/SD command.
  166. void clear_command_queue();
  167. extern millis_t previous_cmd_ms;
  168. inline void refresh_cmd_timeout() { previous_cmd_ms = millis(); }
  169. #if ENABLED(FAST_PWM_FAN)
  170. void setPwmFrequency(uint8_t pin, int val);
  171. #endif
  172. /**
  173. * Feedrate scaling and conversion
  174. */
  175. extern int16_t feedrate_percentage;
  176. #define MMM_TO_MMS(MM_M) ((MM_M)/60.0)
  177. #define MMS_TO_MMM(MM_S) ((MM_S)*60.0)
  178. #define MMS_SCALED(MM_S) ((MM_S)*feedrate_percentage*0.01)
  179. extern bool axis_relative_modes[];
  180. extern bool volumetric_enabled;
  181. extern int16_t flow_percentage[EXTRUDERS]; // Extrusion factor for each extruder
  182. extern float filament_size[EXTRUDERS]; // cross-sectional area of filament (in millimeters), typically around 1.75 or 2.85, 0 disables the volumetric calculations for the extruder.
  183. extern float volumetric_multiplier[EXTRUDERS]; // reciprocal of cross-sectional area of filament (in square millimeters), stored this way to reduce computational burden in planner
  184. extern bool axis_known_position[XYZ];
  185. extern bool axis_homed[XYZ];
  186. extern volatile bool wait_for_heatup;
  187. #if HAS_RESUME_CONTINUE
  188. extern volatile bool wait_for_user;
  189. #endif
  190. extern float current_position[NUM_AXIS];
  191. // Workspace offsets
  192. #if HAS_WORKSPACE_OFFSET
  193. #if HAS_HOME_OFFSET
  194. extern float home_offset[XYZ];
  195. #endif
  196. #if HAS_POSITION_SHIFT
  197. extern float position_shift[XYZ];
  198. #endif
  199. #endif
  200. #if HAS_HOME_OFFSET && HAS_POSITION_SHIFT
  201. extern float workspace_offset[XYZ];
  202. #define WORKSPACE_OFFSET(AXIS) workspace_offset[AXIS]
  203. #elif HAS_HOME_OFFSET
  204. #define WORKSPACE_OFFSET(AXIS) home_offset[AXIS]
  205. #elif HAS_POSITION_SHIFT
  206. #define WORKSPACE_OFFSET(AXIS) position_shift[AXIS]
  207. #else
  208. #define WORKSPACE_OFFSET(AXIS) 0
  209. #endif
  210. #define LOGICAL_POSITION(POS, AXIS) ((POS) + WORKSPACE_OFFSET(AXIS))
  211. #define RAW_POSITION(POS, AXIS) ((POS) - WORKSPACE_OFFSET(AXIS))
  212. #if HAS_POSITION_SHIFT || DISABLED(DELTA)
  213. #define LOGICAL_X_POSITION(POS) LOGICAL_POSITION(POS, X_AXIS)
  214. #define LOGICAL_Y_POSITION(POS) LOGICAL_POSITION(POS, Y_AXIS)
  215. #define RAW_X_POSITION(POS) RAW_POSITION(POS, X_AXIS)
  216. #define RAW_Y_POSITION(POS) RAW_POSITION(POS, Y_AXIS)
  217. #else
  218. #define LOGICAL_X_POSITION(POS) (POS)
  219. #define LOGICAL_Y_POSITION(POS) (POS)
  220. #define RAW_X_POSITION(POS) (POS)
  221. #define RAW_Y_POSITION(POS) (POS)
  222. #endif
  223. #define LOGICAL_Z_POSITION(POS) LOGICAL_POSITION(POS, Z_AXIS)
  224. #define RAW_Z_POSITION(POS) RAW_POSITION(POS, Z_AXIS)
  225. #define RAW_CURRENT_POSITION(A) RAW_##A##_POSITION(current_position[A##_AXIS])
  226. // Hotend Offsets
  227. #if HOTENDS > 1
  228. extern float hotend_offset[XYZ][HOTENDS];
  229. #endif
  230. // Software Endstops
  231. extern float soft_endstop_min[XYZ], soft_endstop_max[XYZ];
  232. #if HAS_SOFTWARE_ENDSTOPS
  233. extern bool soft_endstops_enabled;
  234. void clamp_to_software_endstops(float target[XYZ]);
  235. #else
  236. #define soft_endstops_enabled false
  237. #define clamp_to_software_endstops(x) NOOP
  238. #endif
  239. #if HAS_WORKSPACE_OFFSET || ENABLED(DUAL_X_CARRIAGE)
  240. void update_software_endstops(const AxisEnum axis);
  241. #endif
  242. #if IS_KINEMATIC
  243. extern float delta[ABC];
  244. void inverse_kinematics(const float logical[XYZ]);
  245. #endif
  246. #if ENABLED(DELTA)
  247. extern float endstop_adj[ABC],
  248. delta_radius,
  249. delta_diagonal_rod,
  250. delta_calibration_radius,
  251. delta_segments_per_second,
  252. delta_tower_angle_trim[2],
  253. delta_clip_start_height;
  254. void recalc_delta_settings(float radius, float diagonal_rod);
  255. #elif IS_SCARA
  256. void forward_kinematics_SCARA(const float &a, const float &b);
  257. #endif
  258. #if ENABLED(AUTO_BED_LEVELING_BILINEAR)
  259. extern int bilinear_grid_spacing[2], bilinear_start[2];
  260. extern float bilinear_grid_factor[2],
  261. z_values[GRID_MAX_POINTS_X][GRID_MAX_POINTS_Y];
  262. float bilinear_z_offset(const float logical[XYZ]);
  263. #endif
  264. #if ENABLED(AUTO_BED_LEVELING_UBL)
  265. typedef struct { double A, B, D; } linear_fit;
  266. linear_fit* lsf_linear_fit(double x[], double y[], double z[], const int);
  267. #endif
  268. #if HAS_LEVELING
  269. bool leveling_is_valid();
  270. bool leveling_is_active();
  271. void set_bed_leveling_enabled(const bool enable=true);
  272. void reset_bed_level();
  273. #endif
  274. #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
  275. void set_z_fade_height(const float zfh);
  276. #endif
  277. #if ENABLED(Z_DUAL_ENDSTOPS)
  278. extern float z_endstop_adj;
  279. #endif
  280. #if HAS_BED_PROBE
  281. extern float zprobe_zoffset;
  282. void refresh_zprobe_zoffset(const bool no_babystep=false);
  283. #define DEPLOY_PROBE() set_probe_deployed(true)
  284. #define STOW_PROBE() set_probe_deployed(false)
  285. #else
  286. #define DEPLOY_PROBE()
  287. #define STOW_PROBE()
  288. #endif
  289. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  290. extern MarlinBusyState busy_state;
  291. #define KEEPALIVE_STATE(n) do{ busy_state = n; }while(0)
  292. #else
  293. #define KEEPALIVE_STATE(n) NOOP
  294. #endif
  295. #if FAN_COUNT > 0
  296. extern int16_t fanSpeeds[FAN_COUNT];
  297. #if ENABLED(PROBING_FANS_OFF)
  298. extern bool fans_paused;
  299. extern int16_t paused_fanSpeeds[FAN_COUNT];
  300. #endif
  301. #endif
  302. #if ENABLED(BARICUDA)
  303. extern uint8_t baricuda_valve_pressure, baricuda_e_to_p_pressure;
  304. #endif
  305. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  306. extern bool filament_sensor; // Flag that filament sensor readings should control extrusion
  307. extern float filament_width_nominal, // Theoretical filament diameter i.e., 3.00 or 1.75
  308. filament_width_meas; // Measured filament diameter
  309. extern uint8_t meas_delay_cm, // Delay distance
  310. measurement_delay[]; // Ring buffer to delay measurement
  311. extern int8_t filwidth_delay_index[2]; // Ring buffer indexes. Used by planner, temperature, and main code
  312. #endif
  313. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  314. extern AdvancedPauseMenuResponse advanced_pause_menu_response;
  315. #endif
  316. #if ENABLED(PID_EXTRUSION_SCALING)
  317. extern int lpq_len;
  318. #endif
  319. #if ENABLED(FWRETRACT)
  320. extern bool autoretract_enabled; // M209 S - Autoretract switch
  321. extern float retract_length, // M207 S - G10 Retract length
  322. retract_feedrate_mm_s, // M207 F - G10 Retract feedrate
  323. retract_zlift, // M207 Z - G10 Retract hop size
  324. retract_recover_length, // M208 S - G11 Recover length
  325. retract_recover_feedrate_mm_s, // M208 F - G11 Recover feedrate
  326. swap_retract_length, // M207 W - G10 Swap Retract length
  327. swap_retract_recover_length, // M208 W - G11 Swap Recover length
  328. swap_retract_recover_feedrate_mm_s; // M208 R - G11 Swap Recover feedrate
  329. #endif
  330. // Print job timer
  331. #if ENABLED(PRINTCOUNTER)
  332. extern PrintCounter print_job_timer;
  333. #else
  334. extern Stopwatch print_job_timer;
  335. #endif
  336. // Handling multiple extruders pins
  337. extern uint8_t active_extruder;
  338. #if HAS_TEMP_HOTEND || HAS_TEMP_BED
  339. void print_heaterstates();
  340. #endif
  341. #if ENABLED(MIXING_EXTRUDER)
  342. extern float mixing_factor[MIXING_STEPPERS];
  343. #endif
  344. void calculate_volumetric_multipliers();
  345. /**
  346. * Blocking movement and shorthand functions
  347. */
  348. void do_blocking_move_to(const float &x, const float &y, const float &z, const float &fr_mm_s=0.0);
  349. void do_blocking_move_to_x(const float &x, const float &fr_mm_s=0.0);
  350. void do_blocking_move_to_z(const float &z, const float &fr_mm_s=0.0);
  351. void do_blocking_move_to_xy(const float &x, const float &y, const float &fr_mm_s=0.0);
  352. #if ENABLED(Z_PROBE_ALLEN_KEY) || ENABLED(Z_PROBE_SLED) || HAS_PROBING_PROCEDURE || HOTENDS > 1 || ENABLED(NOZZLE_CLEAN_FEATURE) || ENABLED(NOZZLE_PARK_FEATURE)
  353. bool axis_unhomed_error(const bool x=true, const bool y=true, const bool z=true);
  354. #endif
  355. /**
  356. * position_is_reachable family of functions
  357. */
  358. #if IS_KINEMATIC // (DELTA or SCARA)
  359. #if IS_SCARA
  360. extern const float L1, L2;
  361. #endif
  362. inline bool position_is_reachable_raw_xy(const float &rx, const float &ry) {
  363. #if ENABLED(DELTA)
  364. return HYPOT2(rx, ry) <= sq(DELTA_PRINTABLE_RADIUS);
  365. #elif IS_SCARA
  366. #if MIDDLE_DEAD_ZONE_R > 0
  367. const float R2 = HYPOT2(rx - SCARA_OFFSET_X, ry - SCARA_OFFSET_Y);
  368. return R2 >= sq(float(MIDDLE_DEAD_ZONE_R)) && R2 <= sq(L1 + L2);
  369. #else
  370. return HYPOT2(rx - SCARA_OFFSET_X, ry - SCARA_OFFSET_Y) <= sq(L1 + L2);
  371. #endif
  372. #else // CARTESIAN
  373. // To be migrated from MakerArm branch in future
  374. #endif
  375. }
  376. inline bool position_is_reachable_by_probe_raw_xy(const float &rx, const float &ry) {
  377. // Both the nozzle and the probe must be able to reach the point.
  378. // This won't work on SCARA since the probe offset rotates with the arm.
  379. return position_is_reachable_raw_xy(rx, ry)
  380. && position_is_reachable_raw_xy(rx - X_PROBE_OFFSET_FROM_EXTRUDER, ry - Y_PROBE_OFFSET_FROM_EXTRUDER);
  381. }
  382. #else // CARTESIAN
  383. inline bool position_is_reachable_raw_xy(const float &rx, const float &ry) {
  384. // Add 0.001 margin to deal with float imprecision
  385. return WITHIN(rx, X_MIN_POS - 0.001, X_MAX_POS + 0.001)
  386. && WITHIN(ry, Y_MIN_POS - 0.001, Y_MAX_POS + 0.001);
  387. }
  388. inline bool position_is_reachable_by_probe_raw_xy(const float &rx, const float &ry) {
  389. // Add 0.001 margin to deal with float imprecision
  390. return WITHIN(rx, MIN_PROBE_X - 0.001, MAX_PROBE_X + 0.001)
  391. && WITHIN(ry, MIN_PROBE_Y - 0.001, MAX_PROBE_Y + 0.001);
  392. }
  393. #endif // CARTESIAN
  394. FORCE_INLINE bool position_is_reachable_by_probe_xy(const float &lx, const float &ly) {
  395. return position_is_reachable_by_probe_raw_xy(RAW_X_POSITION(lx), RAW_Y_POSITION(ly));
  396. }
  397. FORCE_INLINE bool position_is_reachable_xy(const float &lx, const float &ly) {
  398. return position_is_reachable_raw_xy(RAW_X_POSITION(lx), RAW_Y_POSITION(ly));
  399. }
  400. #endif // MARLIN_H