My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Marlin.h 14KB

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