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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. #define FORCE_INLINE __attribute__((always_inline)) inline
  25. /**
  26. * Compiler warning on unused variable.
  27. */
  28. #define UNUSED(x) (void) (x)
  29. #include <math.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <inttypes.h>
  34. #include <util/delay.h>
  35. #include <avr/pgmspace.h>
  36. #include <avr/eeprom.h>
  37. #include <avr/interrupt.h>
  38. #include "fastio.h"
  39. #include "Configuration.h"
  40. #ifndef SANITYCHECK_H
  41. #error "Your Configuration.h and Configuration_adv.h files are outdated!"
  42. #endif
  43. #include "enum.h"
  44. #include "utility.h"
  45. typedef unsigned long millis_t;
  46. #ifdef USBCON
  47. #include "HardwareSerial.h"
  48. #if ENABLED(BLUETOOTH)
  49. #define MYSERIAL bluetoothSerial
  50. #else
  51. #define MYSERIAL Serial
  52. #endif // BLUETOOTH
  53. #else
  54. #include "MarlinSerial.h"
  55. #define MYSERIAL customizedSerial
  56. #endif
  57. #include "WString.h"
  58. #if ENABLED(PRINTCOUNTER)
  59. #include "printcounter.h"
  60. #else
  61. #include "stopwatch.h"
  62. #endif
  63. #define SERIAL_CHAR(x) MYSERIAL.write(x)
  64. #define SERIAL_EOL SERIAL_CHAR('\n')
  65. #define SERIAL_PROTOCOLCHAR(x) SERIAL_CHAR(x)
  66. #define SERIAL_PROTOCOL(x) MYSERIAL.print(x)
  67. #define SERIAL_PROTOCOL_F(x,y) MYSERIAL.print(x,y)
  68. #define SERIAL_PROTOCOLPGM(x) serialprintPGM(PSTR(x))
  69. #define SERIAL_PROTOCOLLN(x) do{ MYSERIAL.print(x); SERIAL_EOL; }while(0)
  70. #define SERIAL_PROTOCOLLNPGM(x) do{ serialprintPGM(PSTR(x "\n")); }while(0)
  71. #define SERIAL_PROTOCOLPAIR(name, value) SERIAL_ECHOPAIR(name, value)
  72. extern const char errormagic[] PROGMEM;
  73. extern const char echomagic[] PROGMEM;
  74. #define SERIAL_ERROR_START serialprintPGM(errormagic)
  75. #define SERIAL_ERROR(x) SERIAL_PROTOCOL(x)
  76. #define SERIAL_ERRORPGM(x) SERIAL_PROTOCOLPGM(x)
  77. #define SERIAL_ERRORLN(x) SERIAL_PROTOCOLLN(x)
  78. #define SERIAL_ERRORLNPGM(x) SERIAL_PROTOCOLLNPGM(x)
  79. #define SERIAL_ECHO_START serialprintPGM(echomagic)
  80. #define SERIAL_ECHO(x) SERIAL_PROTOCOL(x)
  81. #define SERIAL_ECHOPGM(x) SERIAL_PROTOCOLPGM(x)
  82. #define SERIAL_ECHOLN(x) SERIAL_PROTOCOLLN(x)
  83. #define SERIAL_ECHOLNPGM(x) SERIAL_PROTOCOLLNPGM(x)
  84. #define SERIAL_ECHOPAIR(name,value) (serial_echopair_P(PSTR(name),(value)))
  85. void serial_echopair_P(const char* s_P, char v);
  86. void serial_echopair_P(const char* s_P, int v);
  87. void serial_echopair_P(const char* s_P, long v);
  88. void serial_echopair_P(const char* s_P, float v);
  89. void serial_echopair_P(const char* s_P, double v);
  90. void serial_echopair_P(const char* s_P, unsigned long v);
  91. FORCE_INLINE void serial_echopair_P(const char* s_P, uint16_t v) { serial_echopair_P(s_P, (int)v); }
  92. FORCE_INLINE void serial_echopair_P(const char* s_P, bool v) { serial_echopair_P(s_P, (int)v); }
  93. FORCE_INLINE void serial_echopair_P(const char* s_P, void *v) { serial_echopair_P(s_P, (unsigned long)v); }
  94. // Things to write to serial from Program memory. Saves 400 to 2k of RAM.
  95. FORCE_INLINE void serialprintPGM(const char* str) {
  96. char ch;
  97. while ((ch = pgm_read_byte(str))) {
  98. MYSERIAL.write(ch);
  99. str++;
  100. }
  101. }
  102. void idle(
  103. #if ENABLED(FILAMENT_CHANGE_FEATURE)
  104. bool no_stepper_sleep = false // pass true to keep steppers from disabling on timeout
  105. #endif
  106. );
  107. void manage_inactivity(bool ignore_stepper_queue = false);
  108. #if ENABLED(DUAL_X_CARRIAGE) || ENABLED(DUAL_NOZZLE_DUPLICATION_MODE)
  109. extern bool extruder_duplication_enabled;
  110. #endif
  111. #if HAS_X2_ENABLE
  112. #define enable_x() do{ X_ENABLE_WRITE( X_ENABLE_ON); X2_ENABLE_WRITE( X_ENABLE_ON); }while(0)
  113. #define disable_x() do{ X_ENABLE_WRITE(!X_ENABLE_ON); X2_ENABLE_WRITE(!X_ENABLE_ON); axis_known_position[X_AXIS] = false; }while(0)
  114. #elif HAS_X_ENABLE
  115. #define enable_x() X_ENABLE_WRITE( X_ENABLE_ON)
  116. #define disable_x() do{ X_ENABLE_WRITE(!X_ENABLE_ON); axis_known_position[X_AXIS] = false; }while(0)
  117. #else
  118. #define enable_x() NOOP
  119. #define disable_x() NOOP
  120. #endif
  121. #if HAS_Y2_ENABLE
  122. #define enable_y() do{ Y_ENABLE_WRITE( Y_ENABLE_ON); Y2_ENABLE_WRITE(Y_ENABLE_ON); }while(0)
  123. #define disable_y() do{ Y_ENABLE_WRITE(!Y_ENABLE_ON); Y2_ENABLE_WRITE(!Y_ENABLE_ON); axis_known_position[Y_AXIS] = false; }while(0)
  124. #elif HAS_Y_ENABLE
  125. #define enable_y() Y_ENABLE_WRITE( Y_ENABLE_ON)
  126. #define disable_y() do{ Y_ENABLE_WRITE(!Y_ENABLE_ON); axis_known_position[Y_AXIS] = false; }while(0)
  127. #else
  128. #define enable_y() NOOP
  129. #define disable_y() NOOP
  130. #endif
  131. #if HAS_Z2_ENABLE
  132. #define enable_z() do{ Z_ENABLE_WRITE( Z_ENABLE_ON); Z2_ENABLE_WRITE(Z_ENABLE_ON); }while(0)
  133. #define disable_z() do{ Z_ENABLE_WRITE(!Z_ENABLE_ON); Z2_ENABLE_WRITE(!Z_ENABLE_ON); axis_known_position[Z_AXIS] = false; }while(0)
  134. #elif HAS_Z_ENABLE
  135. #define enable_z() Z_ENABLE_WRITE( Z_ENABLE_ON)
  136. #define disable_z() do{ Z_ENABLE_WRITE(!Z_ENABLE_ON); axis_known_position[Z_AXIS] = false; }while(0)
  137. #else
  138. #define enable_z() NOOP
  139. #define disable_z() NOOP
  140. #endif
  141. #if ENABLED(MIXING_EXTRUDER)
  142. /**
  143. * Mixing steppers synchronize their enable (and direction) together
  144. */
  145. #if MIXING_STEPPERS > 3
  146. #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); }
  147. #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); }
  148. #elif MIXING_STEPPERS > 2
  149. #define enable_e0() { E0_ENABLE_WRITE( E_ENABLE_ON); E1_ENABLE_WRITE( E_ENABLE_ON); E2_ENABLE_WRITE( E_ENABLE_ON); }
  150. #define disable_e0() { E0_ENABLE_WRITE(!E_ENABLE_ON); E1_ENABLE_WRITE(!E_ENABLE_ON); E2_ENABLE_WRITE(!E_ENABLE_ON); }
  151. #else
  152. #define enable_e0() { E0_ENABLE_WRITE( E_ENABLE_ON); E1_ENABLE_WRITE( E_ENABLE_ON); }
  153. #define disable_e0() { E0_ENABLE_WRITE(!E_ENABLE_ON); E1_ENABLE_WRITE(!E_ENABLE_ON); }
  154. #endif
  155. #define enable_e1() NOOP
  156. #define disable_e1() NOOP
  157. #define enable_e2() NOOP
  158. #define disable_e2() NOOP
  159. #define enable_e3() NOOP
  160. #define disable_e3() NOOP
  161. #else // !MIXING_EXTRUDER
  162. #if HAS_E0_ENABLE
  163. #define enable_e0() E0_ENABLE_WRITE( E_ENABLE_ON)
  164. #define disable_e0() E0_ENABLE_WRITE(!E_ENABLE_ON)
  165. #else
  166. #define enable_e0() NOOP
  167. #define disable_e0() NOOP
  168. #endif
  169. #if E_STEPPERS > 1 && HAS_E1_ENABLE
  170. #define enable_e1() E1_ENABLE_WRITE( E_ENABLE_ON)
  171. #define disable_e1() E1_ENABLE_WRITE(!E_ENABLE_ON)
  172. #else
  173. #define enable_e1() NOOP
  174. #define disable_e1() NOOP
  175. #endif
  176. #if E_STEPPERS > 2 && HAS_E2_ENABLE
  177. #define enable_e2() E2_ENABLE_WRITE( E_ENABLE_ON)
  178. #define disable_e2() E2_ENABLE_WRITE(!E_ENABLE_ON)
  179. #else
  180. #define enable_e2() NOOP
  181. #define disable_e2() NOOP
  182. #endif
  183. #if E_STEPPERS > 3 && HAS_E3_ENABLE
  184. #define enable_e3() E3_ENABLE_WRITE( E_ENABLE_ON)
  185. #define disable_e3() E3_ENABLE_WRITE(!E_ENABLE_ON)
  186. #else
  187. #define enable_e3() NOOP
  188. #define disable_e3() NOOP
  189. #endif
  190. #endif // !MIXING_EXTRUDER
  191. /**
  192. * The axis order in all axis related arrays is X, Y, Z, E
  193. */
  194. #define NUM_AXIS 4
  195. #define _AXIS(AXIS) AXIS ##_AXIS
  196. void enable_all_steppers();
  197. void disable_all_steppers();
  198. void FlushSerialRequestResend();
  199. void ok_to_send();
  200. void reset_bed_level();
  201. void kill(const char*);
  202. void quickstop_stepper();
  203. #if ENABLED(FILAMENT_RUNOUT_SENSOR)
  204. void handle_filament_runout();
  205. #endif
  206. extern uint8_t marlin_debug_flags;
  207. #define DEBUGGING(F) (marlin_debug_flags & (DEBUG_## F))
  208. extern bool Running;
  209. inline bool IsRunning() { return Running; }
  210. inline bool IsStopped() { return !Running; }
  211. bool enqueue_and_echo_command(const char* cmd, bool say_ok=false); //put a single ASCII command at the end of the current buffer or return false when it is full
  212. void enqueue_and_echo_command_now(const char* cmd); // enqueue now, only return when the command has been enqueued
  213. void enqueue_and_echo_commands_P(const char* cmd); //put one or many ASCII commands at the end of the current buffer, read from flash
  214. void clear_command_queue();
  215. void clamp_to_software_endstops(float target[3]);
  216. extern millis_t previous_cmd_ms;
  217. inline void refresh_cmd_timeout() { previous_cmd_ms = millis(); }
  218. #if ENABLED(FAST_PWM_FAN)
  219. void setPwmFrequency(uint8_t pin, int val);
  220. #endif
  221. #ifndef CRITICAL_SECTION_START
  222. #define CRITICAL_SECTION_START unsigned char _sreg = SREG; cli();
  223. #define CRITICAL_SECTION_END SREG = _sreg;
  224. #endif
  225. /**
  226. * Feedrate scaling and conversion
  227. */
  228. extern int feedrate_percentage;
  229. #define MMM_TO_MMS(MM_M) ((MM_M)/60.0)
  230. #define MMS_TO_MMM(MM_S) ((MM_S)*60.0)
  231. #define MMM_SCALED(MM_M) ((MM_M)*feedrate_percentage/100.0)
  232. #define MMS_SCALED(MM_S) MMM_SCALED(MM_S)
  233. #define MMM_TO_MMS_SCALED(MM_M) (MMS_SCALED(MMM_TO_MMS(MM_M)))
  234. extern bool axis_relative_modes[];
  235. extern bool volumetric_enabled;
  236. extern int extruder_multiplier[EXTRUDERS]; // sets extrude multiply factor (in percent) for each extruder individually
  237. 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.
  238. extern float volumetric_multiplier[EXTRUDERS]; // reciprocal of cross-sectional area of filament (in square millimeters), stored this way to reduce computational burden in planner
  239. extern bool axis_known_position[3]; // axis[n].is_known
  240. extern bool axis_homed[3]; // axis[n].is_homed
  241. extern volatile bool wait_for_heatup;
  242. extern float current_position[NUM_AXIS];
  243. extern float position_shift[3];
  244. extern float home_offset[3];
  245. extern float sw_endstop_min[3];
  246. extern float sw_endstop_max[3];
  247. #define LOGICAL_POSITION(POS, AXIS) (POS + home_offset[AXIS] + position_shift[AXIS])
  248. #define RAW_POSITION(POS, AXIS) (POS - home_offset[AXIS] - position_shift[AXIS])
  249. #define LOGICAL_X_POSITION(POS) LOGICAL_POSITION(POS, X_AXIS)
  250. #define LOGICAL_Y_POSITION(POS) LOGICAL_POSITION(POS, Y_AXIS)
  251. #define LOGICAL_Z_POSITION(POS) LOGICAL_POSITION(POS, Z_AXIS)
  252. #define RAW_X_POSITION(POS) RAW_POSITION(POS, X_AXIS)
  253. #define RAW_Y_POSITION(POS) RAW_POSITION(POS, Y_AXIS)
  254. #define RAW_Z_POSITION(POS) RAW_POSITION(POS, Z_AXIS)
  255. #define RAW_CURRENT_POSITION(AXIS) RAW_POSITION(current_position[AXIS], AXIS)
  256. // GCode support for external objects
  257. bool code_seen(char);
  258. int code_value_int();
  259. float code_value_temp_abs();
  260. float code_value_temp_diff();
  261. #if ENABLED(DELTA)
  262. extern float delta[3];
  263. extern float endstop_adj[3]; // axis[n].endstop_adj
  264. extern float delta_radius;
  265. extern float delta_diagonal_rod;
  266. extern float delta_segments_per_second;
  267. extern float delta_diagonal_rod_trim_tower_1;
  268. extern float delta_diagonal_rod_trim_tower_2;
  269. extern float delta_diagonal_rod_trim_tower_3;
  270. void inverse_kinematics(const float cartesian[3]);
  271. void recalc_delta_settings(float radius, float diagonal_rod);
  272. #if ENABLED(AUTO_BED_LEVELING_FEATURE)
  273. extern int delta_grid_spacing[2];
  274. void adjust_delta(float cartesian[3]);
  275. #endif
  276. #elif ENABLED(SCARA)
  277. extern float axis_scaling[3]; // Build size scaling
  278. void inverse_kinematics(const float cartesian[3]);
  279. void forward_kinematics_SCARA(float f_scara[3]);
  280. #endif
  281. #if ENABLED(Z_DUAL_ENDSTOPS)
  282. extern float z_endstop_adj;
  283. #endif
  284. #if HAS_BED_PROBE
  285. extern float zprobe_zoffset;
  286. #endif
  287. #if ENABLED(HOST_KEEPALIVE_FEATURE)
  288. extern uint8_t host_keepalive_interval;
  289. #endif
  290. #if FAN_COUNT > 0
  291. extern int fanSpeeds[FAN_COUNT];
  292. #endif
  293. #if ENABLED(BARICUDA)
  294. extern int baricuda_valve_pressure;
  295. extern int baricuda_e_to_p_pressure;
  296. #endif
  297. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  298. extern float filament_width_nominal; //holds the theoretical filament diameter i.e., 3.00 or 1.75
  299. extern bool filament_sensor; //indicates that filament sensor readings should control extrusion
  300. extern float filament_width_meas; //holds the filament diameter as accurately measured
  301. extern int8_t measurement_delay[]; //ring buffer to delay measurement
  302. extern int filwidth_delay_index1, filwidth_delay_index2; //ring buffer index. used by planner, temperature, and main code
  303. extern int meas_delay_cm; //delay distance
  304. #endif
  305. #if ENABLED(FILAMENT_CHANGE_FEATURE)
  306. extern FilamentChangeMenuResponse filament_change_menu_response;
  307. #endif
  308. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  309. extern int lpq_len;
  310. #endif
  311. #if ENABLED(FWRETRACT)
  312. extern bool autoretract_enabled;
  313. extern bool retracted[EXTRUDERS]; // extruder[n].retracted
  314. extern float retract_length, retract_length_swap, retract_feedrate_mm_s, retract_zlift;
  315. extern float retract_recover_length, retract_recover_length_swap, retract_recover_feedrate_mm_s;
  316. #endif
  317. // Print job timer
  318. #if ENABLED(PRINTCOUNTER)
  319. extern PrintCounter print_job_timer;
  320. #else
  321. extern Stopwatch print_job_timer;
  322. #endif
  323. // Handling multiple extruders pins
  324. extern uint8_t active_extruder;
  325. #if HAS_TEMP_HOTEND || HAS_TEMP_BED
  326. void print_heaterstates();
  327. #endif
  328. #if ENABLED(MIXING_EXTRUDER)
  329. extern float mixing_factor[MIXING_STEPPERS];
  330. #endif
  331. void calculate_volumetric_multipliers();
  332. // Buzzer
  333. #if HAS_BUZZER
  334. #if ENABLED(SPEAKER)
  335. #include "speaker.h"
  336. extern Speaker buzzer;
  337. #else
  338. #include "buzzer.h"
  339. extern Buzzer buzzer;
  340. #endif
  341. #endif
  342. /**
  343. * Blocking movement and shorthand functions
  344. */
  345. inline void do_blocking_move_to(float x, float y, float z, float fr_mm_m=0.0);
  346. inline void do_blocking_move_to_x(float x, float fr_mm_m=0.0);
  347. inline void do_blocking_move_to_z(float z, float fr_mm_m=0.0);
  348. inline void do_blocking_move_to_xy(float x, float y, float fr_mm_m=0.0);
  349. #endif //MARLIN_H