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.

tool_change.cpp 51KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "../inc/MarlinConfigPre.h"
  23. #include "tool_change.h"
  24. #include "probe.h"
  25. #include "motion.h"
  26. #include "planner.h"
  27. #include "temperature.h"
  28. #include "../MarlinCore.h"
  29. //#define DEBUG_TOOL_CHANGE
  30. //#define DEBUG_TOOLCHANGE_FILAMENT_SWAP
  31. #define DEBUG_OUT ENABLED(DEBUG_TOOL_CHANGE)
  32. #include "../core/debug_out.h"
  33. #if HAS_MULTI_EXTRUDER
  34. toolchange_settings_t toolchange_settings; // Initialized by settings.load()
  35. #endif
  36. #if ENABLED(TOOLCHANGE_MIGRATION_FEATURE)
  37. migration_settings_t migration = migration_defaults;
  38. #endif
  39. #if ENABLED(TOOLCHANGE_FS_INIT_BEFORE_SWAP)
  40. Flags<EXTRUDERS> toolchange_extruder_ready;
  41. #endif
  42. #if EITHER(MAGNETIC_PARKING_EXTRUDER, TOOL_SENSOR) \
  43. || defined(EVENT_GCODE_TOOLCHANGE_T0) || defined(EVENT_GCODE_TOOLCHANGE_T1) || defined(EVENT_GCODE_AFTER_TOOLCHANGE) \
  44. || (ENABLED(PARKING_EXTRUDER) && PARKING_EXTRUDER_SOLENOIDS_DELAY > 0)
  45. #include "../gcode/gcode.h"
  46. #endif
  47. #if ENABLED(TOOL_SENSOR)
  48. #include "../lcd/marlinui.h"
  49. #endif
  50. #if ENABLED(DUAL_X_CARRIAGE)
  51. #include "stepper.h"
  52. #endif
  53. #if ANY(SWITCHING_EXTRUDER, SWITCHING_NOZZLE, SWITCHING_TOOLHEAD)
  54. #include "servo.h"
  55. #endif
  56. #if ENABLED(EXT_SOLENOID) && DISABLED(PARKING_EXTRUDER)
  57. #include "../feature/solenoid.h"
  58. #endif
  59. #if ENABLED(MIXING_EXTRUDER)
  60. #include "../feature/mixing.h"
  61. #endif
  62. #if HAS_LEVELING
  63. #include "../feature/bedlevel/bedlevel.h"
  64. #endif
  65. #if HAS_FANMUX
  66. #include "../feature/fanmux.h"
  67. #endif
  68. #if HAS_PRUSA_MMU1
  69. #include "../feature/mmu/mmu.h"
  70. #elif HAS_PRUSA_MMU2
  71. #include "../feature/mmu/mmu2.h"
  72. #endif
  73. #if HAS_MARLINUI_MENU
  74. #include "../lcd/marlinui.h"
  75. #endif
  76. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  77. #include "../feature/pause.h"
  78. #endif
  79. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  80. #include "../gcode/gcode.h"
  81. #if TOOLCHANGE_FS_WIPE_RETRACT <= 0
  82. #undef TOOLCHANGE_FS_WIPE_RETRACT
  83. #define TOOLCHANGE_FS_WIPE_RETRACT 0
  84. #endif
  85. #endif
  86. #if DO_SWITCH_EXTRUDER
  87. #if EXTRUDERS > 3
  88. #define _SERVO_NR(E) ((E) < 2 ? SWITCHING_EXTRUDER_SERVO_NR : SWITCHING_EXTRUDER_E23_SERVO_NR)
  89. #else
  90. #define _SERVO_NR(E) SWITCHING_EXTRUDER_SERVO_NR
  91. #endif
  92. void move_extruder_servo(const uint8_t e) {
  93. planner.synchronize();
  94. if (e < EXTRUDERS) {
  95. servo[_SERVO_NR(e)].move(servo_angles[_SERVO_NR(e)][e & 1]);
  96. safe_delay(500);
  97. }
  98. }
  99. #endif // DO_SWITCH_EXTRUDER
  100. #if ENABLED(SWITCHING_NOZZLE)
  101. #if SWITCHING_NOZZLE_TWO_SERVOS
  102. inline void _move_nozzle_servo(const uint8_t e, const uint8_t angle_index) {
  103. constexpr int8_t sns_index[2] = { SWITCHING_NOZZLE_SERVO_NR, SWITCHING_NOZZLE_E1_SERVO_NR };
  104. constexpr int16_t sns_angles[2] = SWITCHING_NOZZLE_SERVO_ANGLES;
  105. planner.synchronize();
  106. servo[sns_index[e]].move(sns_angles[angle_index]);
  107. safe_delay(SWITCHING_NOZZLE_SERVO_DWELL);
  108. }
  109. void lower_nozzle(const uint8_t e) { _move_nozzle_servo(e, 0); }
  110. void raise_nozzle(const uint8_t e) { _move_nozzle_servo(e, 1); }
  111. #else
  112. void move_nozzle_servo(const uint8_t angle_index) {
  113. planner.synchronize();
  114. servo[SWITCHING_NOZZLE_SERVO_NR].move(servo_angles[SWITCHING_NOZZLE_SERVO_NR][angle_index]);
  115. safe_delay(SWITCHING_NOZZLE_SERVO_DWELL);
  116. }
  117. #endif
  118. #endif // SWITCHING_NOZZLE
  119. // Move to position routines
  120. void _line_to_current(const AxisEnum fr_axis, const float fscale=1) {
  121. line_to_current_position(planner.settings.max_feedrate_mm_s[fr_axis] * fscale);
  122. }
  123. void slow_line_to_current(const AxisEnum fr_axis) { _line_to_current(fr_axis, 0.2f); }
  124. void fast_line_to_current(const AxisEnum fr_axis) { _line_to_current(fr_axis, 0.5f); }
  125. #if ENABLED(MAGNETIC_PARKING_EXTRUDER)
  126. float parkingposx[2], // M951 R L
  127. parkinggrabdistance, // M951 I
  128. parkingslowspeed, // M951 J
  129. parkinghighspeed, // M951 H
  130. parkingtraveldistance, // M951 D
  131. compensationmultiplier;
  132. inline void magnetic_parking_extruder_tool_change(const uint8_t new_tool) {
  133. const float oldx = current_position.x,
  134. grabpos = mpe_settings.parking_xpos[new_tool] + (new_tool ? mpe_settings.grab_distance : -mpe_settings.grab_distance),
  135. offsetcompensation = TERN0(HAS_HOTEND_OFFSET, hotend_offset[active_extruder].x * mpe_settings.compensation_factor);
  136. if (homing_needed_error(_BV(X_AXIS))) return;
  137. /**
  138. * Z Lift and Nozzle Offset shift ar defined in caller method to work equal with any Multi Hotend realization
  139. *
  140. * Steps:
  141. * 1. Move high speed to park position of new extruder
  142. * 2. Move to couple position of new extruder (this also discouple the old extruder)
  143. * 3. Move to park position of new extruder
  144. * 4. Move high speed to approach park position of old extruder
  145. * 5. Move to park position of old extruder
  146. * 6. Move to starting position
  147. */
  148. // STEP 1
  149. current_position.x = mpe_settings.parking_xpos[new_tool] + offsetcompensation;
  150. DEBUG_ECHOPGM("(1) Move extruder ", new_tool);
  151. DEBUG_POS(" to new extruder ParkPos", current_position);
  152. planner.buffer_line(current_position, mpe_settings.fast_feedrate, new_tool);
  153. planner.synchronize();
  154. // STEP 2
  155. current_position.x = grabpos + offsetcompensation;
  156. DEBUG_ECHOPGM("(2) Couple extruder ", new_tool);
  157. DEBUG_POS(" to new extruder GrabPos", current_position);
  158. planner.buffer_line(current_position, mpe_settings.slow_feedrate, new_tool);
  159. planner.synchronize();
  160. // Delay before moving tool, to allow magnetic coupling
  161. gcode.dwell(150);
  162. // STEP 3
  163. current_position.x = mpe_settings.parking_xpos[new_tool] + offsetcompensation;
  164. DEBUG_ECHOPGM("(3) Move extruder ", new_tool);
  165. DEBUG_POS(" back to new extruder ParkPos", current_position);
  166. planner.buffer_line(current_position, mpe_settings.slow_feedrate, new_tool);
  167. planner.synchronize();
  168. // STEP 4
  169. current_position.x = mpe_settings.parking_xpos[active_extruder] + (active_extruder == 0 ? MPE_TRAVEL_DISTANCE : -MPE_TRAVEL_DISTANCE) + offsetcompensation;
  170. DEBUG_ECHOPGM("(4) Move extruder ", new_tool);
  171. DEBUG_POS(" close to old extruder ParkPos", current_position);
  172. planner.buffer_line(current_position, mpe_settings.fast_feedrate, new_tool);
  173. planner.synchronize();
  174. // STEP 5
  175. current_position.x = mpe_settings.parking_xpos[active_extruder] + offsetcompensation;
  176. DEBUG_ECHOPGM("(5) Park extruder ", new_tool);
  177. DEBUG_POS(" at old extruder ParkPos", current_position);
  178. planner.buffer_line(current_position, mpe_settings.slow_feedrate, new_tool);
  179. planner.synchronize();
  180. // STEP 6
  181. current_position.x = oldx;
  182. DEBUG_ECHOPGM("(6) Move extruder ", new_tool);
  183. DEBUG_POS(" to starting position", current_position);
  184. planner.buffer_line(current_position, mpe_settings.fast_feedrate, new_tool);
  185. planner.synchronize();
  186. DEBUG_ECHOLNPGM("Autopark done.");
  187. }
  188. #elif ENABLED(PARKING_EXTRUDER)
  189. void pe_solenoid_init() {
  190. LOOP_LE_N(n, 1) pe_solenoid_set_pin_state(n, !PARKING_EXTRUDER_SOLENOIDS_PINS_ACTIVE);
  191. }
  192. void pe_solenoid_set_pin_state(const uint8_t extruder_num, const uint8_t state) {
  193. switch (extruder_num) {
  194. case 1: OUT_WRITE(SOL1_PIN, state); break;
  195. default: OUT_WRITE(SOL0_PIN, state); break;
  196. }
  197. #if PARKING_EXTRUDER_SOLENOIDS_DELAY > 0
  198. gcode.dwell(PARKING_EXTRUDER_SOLENOIDS_DELAY);
  199. #endif
  200. }
  201. bool extruder_parked = true, do_solenoid_activation = true;
  202. // Modifies tool_change() behavior based on homing side
  203. bool parking_extruder_unpark_after_homing(const uint8_t final_tool, bool homed_towards_final_tool) {
  204. do_solenoid_activation = false; // Tell parking_extruder_tool_change to skip solenoid activation
  205. if (!extruder_parked) return false; // nothing to do
  206. if (homed_towards_final_tool) {
  207. pe_solenoid_magnet_off(1 - final_tool);
  208. DEBUG_ECHOLNPGM("Disengage magnet", 1 - final_tool);
  209. pe_solenoid_magnet_on(final_tool);
  210. DEBUG_ECHOLNPGM("Engage magnet", final_tool);
  211. parking_extruder_set_parked(false);
  212. return false;
  213. }
  214. return true;
  215. }
  216. inline void parking_extruder_tool_change(const uint8_t new_tool, bool no_move) {
  217. if (!no_move) {
  218. constexpr float parkingposx[] = PARKING_EXTRUDER_PARKING_X;
  219. #if HAS_HOTEND_OFFSET
  220. const float x_offset = hotend_offset[active_extruder].x;
  221. #else
  222. constexpr float x_offset = 0;
  223. #endif
  224. const float midpos = (parkingposx[0] + parkingposx[1]) * 0.5f + x_offset,
  225. grabpos = parkingposx[new_tool] + (new_tool ? PARKING_EXTRUDER_GRAB_DISTANCE : -(PARKING_EXTRUDER_GRAB_DISTANCE)) + x_offset;
  226. /**
  227. * 1. Move to park position of old extruder
  228. * 2. Disengage magnetic field, wait for delay
  229. * 3. Move near new extruder
  230. * 4. Engage magnetic field for new extruder
  231. * 5. Move to parking incl. offset of new extruder
  232. * 6. Lower Z-Axis
  233. */
  234. // STEP 1
  235. DEBUG_POS("Start PE Tool-Change", current_position);
  236. // Don't park the active_extruder unless unparked
  237. if (!extruder_parked) {
  238. current_position.x = parkingposx[active_extruder] + x_offset;
  239. DEBUG_ECHOLNPGM("(1) Park extruder ", active_extruder);
  240. DEBUG_POS("Moving ParkPos", current_position);
  241. fast_line_to_current(X_AXIS);
  242. // STEP 2
  243. planner.synchronize();
  244. DEBUG_ECHOLNPGM("(2) Disengage magnet");
  245. pe_solenoid_magnet_off(active_extruder);
  246. // STEP 3
  247. current_position.x += active_extruder ? -10 : 10; // move 10mm away from parked extruder
  248. DEBUG_ECHOLNPGM("(3) Move near new extruder");
  249. DEBUG_POS("Move away from parked extruder", current_position);
  250. fast_line_to_current(X_AXIS);
  251. }
  252. // STEP 4
  253. planner.synchronize();
  254. DEBUG_ECHOLNPGM("(4) Engage magnetic field");
  255. // Just save power for inverted magnets
  256. TERN_(PARKING_EXTRUDER_SOLENOIDS_INVERT, pe_solenoid_magnet_on(active_extruder));
  257. pe_solenoid_magnet_on(new_tool);
  258. // STEP 5
  259. current_position.x = grabpos + (new_tool ? -10 : 10);
  260. fast_line_to_current(X_AXIS);
  261. current_position.x = grabpos;
  262. DEBUG_SYNCHRONIZE();
  263. DEBUG_POS("(5) Unpark extruder", current_position);
  264. slow_line_to_current(X_AXIS);
  265. // STEP 6
  266. current_position.x = DIFF_TERN(HAS_HOTEND_OFFSET, midpos, hotend_offset[new_tool].x);
  267. DEBUG_SYNCHRONIZE();
  268. DEBUG_POS("(6) Move midway between hotends", current_position);
  269. fast_line_to_current(X_AXIS);
  270. planner.synchronize(); // Always sync the final move
  271. DEBUG_POS("PE Tool-Change done.", current_position);
  272. parking_extruder_set_parked(false);
  273. }
  274. else if (do_solenoid_activation) {
  275. // Deactivate current extruder solenoid
  276. pe_solenoid_set_pin_state(active_extruder, !PARKING_EXTRUDER_SOLENOIDS_PINS_ACTIVE);
  277. // Engage new extruder magnetic field
  278. pe_solenoid_set_pin_state(new_tool, PARKING_EXTRUDER_SOLENOIDS_PINS_ACTIVE);
  279. }
  280. do_solenoid_activation = true; // Activate solenoid for subsequent tool_change()
  281. }
  282. #endif // PARKING_EXTRUDER
  283. #if ENABLED(TOOL_SENSOR)
  284. bool tool_sensor_disabled; // = false
  285. // Return a bitmask of tool sensor states
  286. inline uint8_t poll_tool_sensor_pins() {
  287. return (0
  288. #if PIN_EXISTS(TOOL_SENSOR1)
  289. | (READ(TOOL_SENSOR1_PIN) << 0)
  290. #endif
  291. #if PIN_EXISTS(TOOL_SENSOR2)
  292. | (READ(TOOL_SENSOR2_PIN) << 1)
  293. #endif
  294. #if PIN_EXISTS(TOOL_SENSOR3)
  295. | (READ(TOOL_SENSOR3_PIN) << 2)
  296. #endif
  297. #if PIN_EXISTS(TOOL_SENSOR4)
  298. | (READ(TOOL_SENSOR4_PIN) << 3)
  299. #endif
  300. #if PIN_EXISTS(TOOL_SENSOR5)
  301. | (READ(TOOL_SENSOR5_PIN) << 4)
  302. #endif
  303. #if PIN_EXISTS(TOOL_SENSOR6)
  304. | (READ(TOOL_SENSOR6_PIN) << 5)
  305. #endif
  306. #if PIN_EXISTS(TOOL_SENSOR7)
  307. | (READ(TOOL_SENSOR7_PIN) << 6)
  308. #endif
  309. #if PIN_EXISTS(TOOL_SENSOR8)
  310. | (READ(TOOL_SENSOR8_PIN) << 7)
  311. #endif
  312. );
  313. }
  314. uint8_t check_tool_sensor_stats(const uint8_t tool_index, const bool kill_on_error/*=false*/, const bool disable/*=false*/) {
  315. static uint8_t sensor_tries; // = 0
  316. for (;;) {
  317. if (poll_tool_sensor_pins() == _BV(tool_index)) {
  318. sensor_tries = 0;
  319. return tool_index;
  320. }
  321. else if (kill_on_error && (!tool_sensor_disabled || disable)) {
  322. sensor_tries++;
  323. if (sensor_tries > 10) kill(F("Tool Sensor error"));
  324. safe_delay(5);
  325. }
  326. else {
  327. sensor_tries++;
  328. if (sensor_tries > 10) return -1;
  329. safe_delay(5);
  330. }
  331. }
  332. }
  333. #endif // TOOL_SENSOR
  334. #if ENABLED(SWITCHING_TOOLHEAD)
  335. inline void switching_toolhead_lock(const bool locked) {
  336. #ifdef SWITCHING_TOOLHEAD_SERVO_ANGLES
  337. const uint16_t swt_angles[2] = SWITCHING_TOOLHEAD_SERVO_ANGLES;
  338. servo[SWITCHING_TOOLHEAD_SERVO_NR].move(swt_angles[locked ? 0 : 1]);
  339. #elif PIN_EXISTS(SWT_SOLENOID)
  340. OUT_WRITE(SWT_SOLENOID_PIN, locked);
  341. gcode.dwell(10);
  342. #else
  343. #error "No toolhead locking mechanism configured."
  344. #endif
  345. }
  346. void swt_init() {
  347. switching_toolhead_lock(true);
  348. #if ENABLED(TOOL_SENSOR)
  349. // Init tool sensors
  350. #if PIN_EXISTS(TOOL_SENSOR1)
  351. SET_INPUT_PULLUP(TOOL_SENSOR1_PIN);
  352. #endif
  353. #if PIN_EXISTS(TOOL_SENSOR2)
  354. SET_INPUT_PULLUP(TOOL_SENSOR2_PIN);
  355. #endif
  356. #if PIN_EXISTS(TOOL_SENSOR3)
  357. SET_INPUT_PULLUP(TOOL_SENSOR3_PIN);
  358. #endif
  359. #if PIN_EXISTS(TOOL_SENSOR4)
  360. SET_INPUT_PULLUP(TOOL_SENSOR4_PIN);
  361. #endif
  362. #if PIN_EXISTS(TOOL_SENSOR5)
  363. SET_INPUT_PULLUP(TOOL_SENSOR5_PIN);
  364. #endif
  365. #if PIN_EXISTS(TOOL_SENSOR6)
  366. SET_INPUT_PULLUP(TOOL_SENSOR6_PIN);
  367. #endif
  368. #if PIN_EXISTS(TOOL_SENSOR7)
  369. SET_INPUT_PULLUP(TOOL_SENSOR7_PIN);
  370. #endif
  371. #if PIN_EXISTS(TOOL_SENSOR8)
  372. SET_INPUT_PULLUP(TOOL_SENSOR8_PIN);
  373. #endif
  374. if (check_tool_sensor_stats(0)) {
  375. LCD_MESSAGE_F("TC error");
  376. switching_toolhead_lock(false);
  377. while (check_tool_sensor_stats(0)) { /* nada */ }
  378. switching_toolhead_lock(true);
  379. }
  380. LCD_MESSAGE_F("TC Success");
  381. #endif // TOOL_SENSOR
  382. }
  383. inline void switching_toolhead_tool_change(const uint8_t new_tool, bool no_move/*=false*/) {
  384. if (no_move) return;
  385. constexpr float toolheadposx[] = SWITCHING_TOOLHEAD_X_POS;
  386. const float placexpos = toolheadposx[active_extruder],
  387. grabxpos = toolheadposx[new_tool];
  388. (void)check_tool_sensor_stats(active_extruder, true);
  389. /**
  390. * 1. Move to switch position of current toolhead
  391. * 2. Unlock tool and drop it in the dock
  392. * 3. Move to the new toolhead
  393. * 4. Grab and lock the new toolhead
  394. */
  395. // 1. Move to switch position of current toolhead
  396. DEBUG_POS("Start ST Tool-Change", current_position);
  397. current_position.x = placexpos;
  398. DEBUG_ECHOLNPGM("(1) Place old tool ", active_extruder);
  399. DEBUG_POS("Move X SwitchPos", current_position);
  400. fast_line_to_current(X_AXIS);
  401. current_position.y = SWITCHING_TOOLHEAD_Y_POS - (SWITCHING_TOOLHEAD_Y_SECURITY);
  402. DEBUG_SYNCHRONIZE();
  403. DEBUG_POS("Move Y SwitchPos + Security", current_position);
  404. slow_line_to_current(Y_AXIS);
  405. // 2. Unlock tool and drop it in the dock
  406. TERN_(TOOL_SENSOR, tool_sensor_disabled = true);
  407. planner.synchronize();
  408. DEBUG_ECHOLNPGM("(2) Unlock and Place Toolhead");
  409. switching_toolhead_lock(false);
  410. safe_delay(500);
  411. current_position.y = SWITCHING_TOOLHEAD_Y_POS;
  412. DEBUG_POS("Move Y SwitchPos", current_position);
  413. slow_line_to_current(Y_AXIS);
  414. // Wait for move to complete, then another 0.2s
  415. planner.synchronize();
  416. safe_delay(200);
  417. current_position.y -= SWITCHING_TOOLHEAD_Y_CLEAR;
  418. DEBUG_POS("Move back Y clear", current_position);
  419. slow_line_to_current(Y_AXIS); // move away from docked toolhead
  420. (void)check_tool_sensor_stats(active_extruder);
  421. // 3. Move to the new toolhead
  422. current_position.x = grabxpos;
  423. DEBUG_SYNCHRONIZE();
  424. DEBUG_ECHOLNPGM("(3) Move to new toolhead position");
  425. DEBUG_POS("Move to new toolhead X", current_position);
  426. fast_line_to_current(X_AXIS);
  427. current_position.y = SWITCHING_TOOLHEAD_Y_POS - (SWITCHING_TOOLHEAD_Y_SECURITY);
  428. DEBUG_SYNCHRONIZE();
  429. DEBUG_POS("Move Y SwitchPos + Security", current_position);
  430. slow_line_to_current(Y_AXIS);
  431. // 4. Grab and lock the new toolhead
  432. current_position.y = SWITCHING_TOOLHEAD_Y_POS;
  433. DEBUG_SYNCHRONIZE();
  434. DEBUG_ECHOLNPGM("(4) Grab and lock new toolhead");
  435. DEBUG_POS("Move Y SwitchPos", current_position);
  436. slow_line_to_current(Y_AXIS);
  437. // Wait for move to finish, pause 0.2s, move servo, pause 0.5s
  438. planner.synchronize();
  439. safe_delay(200);
  440. (void)check_tool_sensor_stats(new_tool, true, true);
  441. switching_toolhead_lock(true);
  442. safe_delay(500);
  443. current_position.y -= SWITCHING_TOOLHEAD_Y_CLEAR;
  444. DEBUG_POS("Move back Y clear", current_position);
  445. slow_line_to_current(Y_AXIS); // Move away from docked toolhead
  446. planner.synchronize(); // Always sync the final move
  447. (void)check_tool_sensor_stats(new_tool, true, true);
  448. DEBUG_POS("ST Tool-Change done.", current_position);
  449. }
  450. #elif ENABLED(MAGNETIC_SWITCHING_TOOLHEAD)
  451. inline void magnetic_switching_toolhead_tool_change(const uint8_t new_tool, bool no_move/*=false*/) {
  452. if (no_move) return;
  453. constexpr float toolheadposx[] = SWITCHING_TOOLHEAD_X_POS,
  454. toolheadclearx[] = SWITCHING_TOOLHEAD_X_SECURITY;
  455. const float placexpos = toolheadposx[active_extruder],
  456. placexclear = toolheadclearx[active_extruder],
  457. grabxpos = toolheadposx[new_tool],
  458. grabxclear = toolheadclearx[new_tool];
  459. /**
  460. * 1. Move to switch position of current toolhead
  461. * 2. Release and place toolhead in the dock
  462. * 3. Move to the new toolhead
  463. * 4. Grab the new toolhead and move to security position
  464. */
  465. DEBUG_POS("Start MST Tool-Change", current_position);
  466. // 1. Move to switch position current toolhead
  467. current_position.y = SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_CLEAR;
  468. SERIAL_ECHOLNPGM("(1) Place old tool ", active_extruder);
  469. DEBUG_POS("Move Y SwitchPos + Security", current_position);
  470. fast_line_to_current(Y_AXIS);
  471. current_position.x = placexclear;
  472. DEBUG_SYNCHRONIZE();
  473. DEBUG_POS("Move X SwitchPos + Security", current_position);
  474. fast_line_to_current(X_AXIS);
  475. current_position.y = SWITCHING_TOOLHEAD_Y_POS;
  476. DEBUG_SYNCHRONIZE();
  477. DEBUG_POS("Move Y SwitchPos", current_position);
  478. fast_line_to_current(Y_AXIS);
  479. current_position.x = placexpos;
  480. DEBUG_SYNCHRONIZE();
  481. DEBUG_POS("Move X SwitchPos", current_position);
  482. line_to_current_position(planner.settings.max_feedrate_mm_s[X_AXIS] * 0.25f);
  483. // 2. Release and place toolhead in the dock
  484. DEBUG_SYNCHRONIZE();
  485. DEBUG_ECHOLNPGM("(2) Release and Place Toolhead");
  486. current_position.y = SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_RELEASE;
  487. DEBUG_POS("Move Y SwitchPos + Release", current_position);
  488. line_to_current_position(planner.settings.max_feedrate_mm_s[Y_AXIS] * 0.1f);
  489. current_position.y = SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_SECURITY;
  490. DEBUG_SYNCHRONIZE();
  491. DEBUG_POS("Move Y SwitchPos + Security", current_position);
  492. line_to_current_position(planner.settings.max_feedrate_mm_s[Y_AXIS]);
  493. // 3. Move to new toolhead position
  494. DEBUG_SYNCHRONIZE();
  495. DEBUG_ECHOLNPGM("(3) Move to new toolhead position");
  496. current_position.x = grabxpos;
  497. DEBUG_POS("Move to new toolhead X", current_position);
  498. fast_line_to_current(X_AXIS);
  499. // 4. Grab the new toolhead and move to security position
  500. DEBUG_SYNCHRONIZE();
  501. DEBUG_ECHOLNPGM("(4) Grab new toolhead, move to security position");
  502. current_position.y = SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_RELEASE;
  503. DEBUG_POS("Move Y SwitchPos + Release", current_position);
  504. line_to_current_position(planner.settings.max_feedrate_mm_s[Y_AXIS]);
  505. current_position.y = SWITCHING_TOOLHEAD_Y_POS;
  506. DEBUG_SYNCHRONIZE();
  507. DEBUG_POS("Move Y SwitchPos", current_position);
  508. _line_to_current(Y_AXIS, 0.2f);
  509. #if ENABLED(PRIME_BEFORE_REMOVE) && (SWITCHING_TOOLHEAD_PRIME_MM || SWITCHING_TOOLHEAD_RETRACT_MM)
  510. #if SWITCHING_TOOLHEAD_PRIME_MM
  511. current_position.e += SWITCHING_TOOLHEAD_PRIME_MM;
  512. planner.buffer_line(current_position, MMM_TO_MMS(SWITCHING_TOOLHEAD_PRIME_FEEDRATE), new_tool);
  513. #endif
  514. #if SWITCHING_TOOLHEAD_RETRACT_MM
  515. current_position.e -= SWITCHING_TOOLHEAD_RETRACT_MM;
  516. planner.buffer_line(current_position, MMM_TO_MMS(SWITCHING_TOOLHEAD_RETRACT_FEEDRATE), new_tool);
  517. #endif
  518. #else
  519. planner.synchronize();
  520. safe_delay(100); // Give switch time to settle
  521. #endif
  522. current_position.x = grabxclear;
  523. DEBUG_POS("Move to new toolhead X + Security", current_position);
  524. _line_to_current(X_AXIS, 0.1f);
  525. planner.synchronize();
  526. safe_delay(100); // Give switch time to settle
  527. current_position.y += SWITCHING_TOOLHEAD_Y_CLEAR;
  528. DEBUG_POS("Move back Y clear", current_position);
  529. fast_line_to_current(Y_AXIS); // move away from docked toolhead
  530. planner.synchronize(); // Always sync last tool-change move
  531. DEBUG_POS("MST Tool-Change done.", current_position);
  532. }
  533. #elif ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  534. inline void est_activate_solenoid() { OUT_WRITE(SOL0_PIN, HIGH); }
  535. inline void est_deactivate_solenoid() { OUT_WRITE(SOL0_PIN, LOW); }
  536. void est_init() { est_activate_solenoid(); }
  537. inline void em_switching_toolhead_tool_change(const uint8_t new_tool, bool no_move) {
  538. if (no_move) return;
  539. constexpr float toolheadposx[] = SWITCHING_TOOLHEAD_X_POS;
  540. const float placexpos = toolheadposx[active_extruder],
  541. grabxpos = toolheadposx[new_tool];
  542. const xyz_pos_t &hoffs = hotend_offset[active_extruder];
  543. /**
  544. * 1. Raise Z-Axis to give enough clearance
  545. * 2. Move to position near active extruder parking
  546. * 3. Move gently to park position of active extruder
  547. * 4. Disengage magnetic field, wait for delay
  548. * 5. Leave extruder and move to position near new extruder parking
  549. * 6. Move gently to park position of new extruder
  550. * 7. Engage magnetic field for new extruder parking
  551. * 8. Unpark extruder
  552. * 9. Apply Z hotend offset to current position
  553. */
  554. DEBUG_POS("Start EMST Tool-Change", current_position);
  555. // 1. Raise Z-Axis to give enough clearance
  556. current_position.z += SWITCHING_TOOLHEAD_Z_HOP;
  557. DEBUG_POS("(1) Raise Z-Axis ", current_position);
  558. fast_line_to_current(Z_AXIS);
  559. // 2. Move to position near active extruder parking
  560. DEBUG_SYNCHRONIZE();
  561. DEBUG_ECHOLNPGM("(2) Move near active extruder parking", active_extruder);
  562. DEBUG_POS("Moving ParkPos", current_position);
  563. current_position.set(hoffs.x + placexpos,
  564. hoffs.y + SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_CLEAR);
  565. fast_line_to_current(X_AXIS);
  566. // 3. Move gently to park position of active extruder
  567. DEBUG_SYNCHRONIZE();
  568. SERIAL_ECHOLNPGM("(3) Move gently to park position of active extruder", active_extruder);
  569. DEBUG_POS("Moving ParkPos", current_position);
  570. current_position.y -= SWITCHING_TOOLHEAD_Y_CLEAR;
  571. slow_line_to_current(Y_AXIS);
  572. // 4. Disengage magnetic field, wait for delay
  573. planner.synchronize();
  574. DEBUG_ECHOLNPGM("(4) Disengage magnet");
  575. est_deactivate_solenoid();
  576. // 5. Leave extruder and move to position near new extruder parking
  577. DEBUG_ECHOLNPGM("(5) Move near new extruder parking");
  578. DEBUG_POS("Moving ParkPos", current_position);
  579. current_position.y += SWITCHING_TOOLHEAD_Y_CLEAR;
  580. slow_line_to_current(Y_AXIS);
  581. current_position.set(hoffs.x + grabxpos,
  582. hoffs.y + SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_CLEAR);
  583. fast_line_to_current(X_AXIS);
  584. // 6. Move gently to park position of new extruder
  585. current_position.y -= SWITCHING_TOOLHEAD_Y_CLEAR;
  586. if (DEBUGGING(LEVELING)) {
  587. planner.synchronize();
  588. DEBUG_ECHOLNPGM("(6) Move near new extruder");
  589. }
  590. slow_line_to_current(Y_AXIS);
  591. // 7. Engage magnetic field for new extruder parking
  592. DEBUG_SYNCHRONIZE();
  593. DEBUG_ECHOLNPGM("(7) Engage magnetic field");
  594. est_activate_solenoid();
  595. // 8. Unpark extruder
  596. current_position.y += SWITCHING_TOOLHEAD_Y_CLEAR;
  597. DEBUG_ECHOLNPGM("(8) Unpark extruder");
  598. slow_line_to_current(X_AXIS);
  599. planner.synchronize(); // Always sync the final move
  600. // 9. Apply Z hotend offset to current position
  601. DEBUG_POS("(9) Applying Z-offset", current_position);
  602. current_position.z += hoffs.z - hotend_offset[new_tool].z;
  603. DEBUG_POS("EMST Tool-Change done.", current_position);
  604. }
  605. #endif // ELECTROMAGNETIC_SWITCHING_TOOLHEAD
  606. #if HAS_EXTRUDERS
  607. inline void invalid_extruder_error(const uint8_t e) {
  608. SERIAL_ECHO_START();
  609. SERIAL_CHAR('T'); SERIAL_ECHO(e);
  610. SERIAL_CHAR(' '); SERIAL_ECHOLNPGM(STR_INVALID_EXTRUDER);
  611. }
  612. #endif
  613. #if ENABLED(DUAL_X_CARRIAGE)
  614. /**
  615. * @brief Dual X Tool Change
  616. * @details Change tools, with extra behavior based on current mode
  617. *
  618. * @param new_tool Tool index to activate
  619. * @param no_move Flag indicating no moves should take place
  620. */
  621. inline void dualx_tool_change(const uint8_t new_tool, bool &no_move) {
  622. DEBUG_ECHOPGM("Dual X Carriage Mode ");
  623. switch (dual_x_carriage_mode) {
  624. case DXC_FULL_CONTROL_MODE: DEBUG_ECHOLNPGM("FULL_CONTROL"); break;
  625. case DXC_AUTO_PARK_MODE: DEBUG_ECHOLNPGM("AUTO_PARK"); break;
  626. case DXC_DUPLICATION_MODE: DEBUG_ECHOLNPGM("DUPLICATION"); break;
  627. case DXC_MIRRORED_MODE: DEBUG_ECHOLNPGM("MIRRORED"); break;
  628. }
  629. // Get the home position of the currently-active tool
  630. const float xhome = x_home_pos(active_extruder);
  631. if (dual_x_carriage_mode == DXC_AUTO_PARK_MODE // If Auto-Park mode is enabled
  632. && IsRunning() && !no_move // ...and movement is permitted
  633. && (delayed_move_time || current_position.x != xhome) // ...and delayed_move_time is set OR not "already parked"...
  634. ) {
  635. DEBUG_ECHOLNPGM("MoveX to ", xhome);
  636. current_position.x = xhome;
  637. line_to_current_position(planner.settings.max_feedrate_mm_s[X_AXIS]); // Park the current head
  638. planner.synchronize();
  639. }
  640. // Activate the new extruder ahead of calling set_axis_is_at_home!
  641. active_extruder = new_tool;
  642. // This function resets the max/min values - the current position may be overwritten below.
  643. set_axis_is_at_home(X_AXIS);
  644. DEBUG_POS("New Extruder", current_position);
  645. switch (dual_x_carriage_mode) {
  646. case DXC_FULL_CONTROL_MODE:
  647. // New current position is the position of the activated extruder
  648. current_position.x = inactive_extruder_x;
  649. // Save the inactive extruder's position (from the old current_position)
  650. inactive_extruder_x = destination.x;
  651. DEBUG_ECHOLNPGM("DXC Full Control curr.x=", current_position.x, " dest.x=", destination.x);
  652. break;
  653. case DXC_AUTO_PARK_MODE:
  654. idex_set_parked();
  655. break;
  656. default:
  657. break;
  658. }
  659. // Ensure X axis DIR pertains to the correct carriage
  660. stepper.set_directions();
  661. DEBUG_ECHOLNPGM("Active extruder parked: ", active_extruder_parked ? "yes" : "no");
  662. DEBUG_POS("New extruder (parked)", current_position);
  663. }
  664. #endif // DUAL_X_CARRIAGE
  665. /**
  666. * Prime active tool using TOOLCHANGE_FILAMENT_SWAP settings
  667. */
  668. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  669. #ifdef DEBUG_TOOLCHANGE_FILAMENT_SWAP
  670. #define FS_DEBUG(V...) SERIAL_ECHOLNPGM("DEBUG: " V)
  671. #else
  672. #define FS_DEBUG(...) NOOP
  673. #endif
  674. // Define any variables required
  675. static Flags<EXTRUDERS> extruder_was_primed; // Extruders primed status
  676. #if ENABLED(TOOLCHANGE_FS_PRIME_FIRST_USED)
  677. bool enable_first_prime; // As set by M217 V
  678. #endif
  679. // Cool down with fan
  680. inline void filament_swap_cooling() {
  681. #if HAS_FAN && TOOLCHANGE_FS_FAN >= 0
  682. thermalManager.fan_speed[TOOLCHANGE_FS_FAN] = toolchange_settings.fan_speed;
  683. gcode.dwell(SEC_TO_MS(toolchange_settings.fan_time));
  684. thermalManager.fan_speed[TOOLCHANGE_FS_FAN] = FAN_OFF_PWM;
  685. #endif
  686. }
  687. /**
  688. * Check if too cold to move the specified tool
  689. *
  690. * Returns TRUE if too cold to move (also echos message: STR_ERR_HOTEND_TOO_COLD)
  691. * Returns FALSE if able to move.
  692. */
  693. bool too_cold(uint8_t toolID){
  694. if (TERN0(PREVENT_COLD_EXTRUSION, !DEBUGGING(DRYRUN) && thermalManager.targetTooColdToExtrude(toolID))) {
  695. SERIAL_ECHO_MSG(STR_ERR_HOTEND_TOO_COLD);
  696. return true;
  697. }
  698. return false;
  699. }
  700. /**
  701. * Cutting recovery -- Recover from cutting retraction that occurs at the end of nozzle priming
  702. *
  703. * If the active_extruder is up to temp (!too_cold):
  704. * Extrude filament distance = toolchange_settings.extra_resume + toolchange_settings.wipe_retract
  705. * current_position.e = e;
  706. * sync_plan_position_e();
  707. */
  708. void extruder_cutting_recover(const_float_t e) {
  709. if (!too_cold(active_extruder)) {
  710. const float dist = toolchange_settings.extra_resume + toolchange_settings.wipe_retract;
  711. FS_DEBUG("Performing Cutting Recover | Distance: ", dist, " | Speed: ", MMM_TO_MMS(toolchange_settings.unretract_speed), "mm/s");
  712. unscaled_e_move(dist, MMM_TO_MMS(toolchange_settings.unretract_speed));
  713. planner.synchronize();
  714. FS_DEBUG("Set position to: ", e);
  715. current_position.e = e;
  716. sync_plan_position_e(); // Resume new E Position
  717. }
  718. }
  719. /**
  720. * Prime the currently selected extruder (Filament loading only)
  721. *
  722. * If too_cold(toolID) returns TRUE -> returns without moving extruder.
  723. * Extruders filament = swap_length + extra prime, then performs cutting retraction if enabled.
  724. * If cooling fan is enabled, calls filament_swap_cooling();
  725. */
  726. void extruder_prime() {
  727. if (too_cold(active_extruder)) {
  728. FS_DEBUG("Priming Aborted - Nozzle Too Cold!");
  729. return; // Extruder too cold to prime
  730. }
  731. float fr = toolchange_settings.unretract_speed; // Set default speed for unretract
  732. #if ENABLED(TOOLCHANGE_FS_SLOW_FIRST_PRIME)
  733. /**
  734. * Perform first unretract movement at the slower Prime_Speed to avoid breakage on first prime
  735. */
  736. static Flags<EXTRUDERS> extruder_did_first_prime; // Extruders first priming status
  737. if (!extruder_did_first_prime[active_extruder]) {
  738. extruder_did_first_prime.set(active_extruder); // Log first prime complete
  739. // new nozzle - prime at user-specified speed.
  740. FS_DEBUG("First time priming T", active_extruder, ", reducing speed from ", MMM_TO_MMS(fr), " to ", MMM_TO_MMS(toolchange_settings.prime_speed), "mm/s");
  741. fr = toolchange_settings.prime_speed;
  742. unscaled_e_move(0, MMM_TO_MMS(fr)); // Init planner with 0 length move
  743. }
  744. #endif
  745. //Calculate and perform the priming distance
  746. if (toolchange_settings.extra_prime >= 0) {
  747. // Positive extra_prime value
  748. // - Return filament at speed (fr) then extra_prime at prime speed
  749. FS_DEBUG("Loading Filament for T", active_extruder, " | Distance: ", toolchange_settings.swap_length, " | Speed: ", MMM_TO_MMS(fr), "mm/s");
  750. unscaled_e_move(toolchange_settings.swap_length, MMM_TO_MMS(fr)); // Prime (Unretract) filament by extruding equal to Swap Length (Unretract)
  751. if (toolchange_settings.extra_prime > 0) {
  752. FS_DEBUG("Performing Extra Priming for T", active_extruder, " | Distance: ", toolchange_settings.extra_prime, " | Speed: ", MMM_TO_MMS(toolchange_settings.prime_speed), "mm/s");
  753. unscaled_e_move(toolchange_settings.extra_prime, MMM_TO_MMS(toolchange_settings.prime_speed)); // Extra Prime Distance
  754. }
  755. }
  756. else {
  757. // Negative extra_prime value
  758. // - Unretract distance (swap length) is reduced by the value of extra_prime
  759. const float eswap = toolchange_settings.swap_length + toolchange_settings.extra_prime;
  760. FS_DEBUG("Negative ExtraPrime value - Swap Return Length has been reduced from ", toolchange_settings.swap_length, " to ", eswap);
  761. FS_DEBUG("Loading Filament for T", active_extruder, " | Distance: ", eswap, " | Speed: ", MMM_TO_MMS(fr), "mm/s");
  762. unscaled_e_move(eswap, MMM_TO_MMS(fr));
  763. }
  764. extruder_was_primed.set(active_extruder); // Log that this extruder has been primed
  765. // Cutting retraction
  766. #if TOOLCHANGE_FS_WIPE_RETRACT
  767. FS_DEBUG("Performing Cutting Retraction | Distance: ", -toolchange_settings.wipe_retract, " | Speed: ", MMM_TO_MMS(toolchange_settings.retract_speed), "mm/s");
  768. unscaled_e_move(-toolchange_settings.wipe_retract, MMM_TO_MMS(toolchange_settings.retract_speed));
  769. #endif
  770. // Cool down with fan
  771. filament_swap_cooling();
  772. }
  773. /**
  774. * Sequence to Prime the currently selected extruder
  775. * Raise Z, move the ToolChange_Park if enabled, prime the extruder, move back.
  776. */
  777. void tool_change_prime() {
  778. FS_DEBUG(">>> tool_change_prime()");
  779. if (!too_cold(active_extruder)) {
  780. destination = current_position; // Remember the old position
  781. const bool ok = TERN1(TOOLCHANGE_PARK, all_axes_homed() && toolchange_settings.enable_park);
  782. #if HAS_FAN && TOOLCHANGE_FS_FAN >= 0
  783. // Store and stop fan. Restored on any exit.
  784. REMEMBER(fan, thermalManager.fan_speed[TOOLCHANGE_FS_FAN], 0);
  785. #endif
  786. // Z raise
  787. if (ok) {
  788. // Do a small lift to avoid the workpiece in the move back (below)
  789. current_position.z += toolchange_settings.z_raise;
  790. TERN_(HAS_SOFTWARE_ENDSTOPS, NOMORE(current_position.z, soft_endstop.max.z));
  791. fast_line_to_current(Z_AXIS);
  792. planner.synchronize();
  793. }
  794. // Park
  795. #if ENABLED(TOOLCHANGE_PARK)
  796. if (ok) {
  797. IF_DISABLED(TOOLCHANGE_PARK_Y_ONLY, current_position.x = toolchange_settings.change_point.x);
  798. IF_DISABLED(TOOLCHANGE_PARK_X_ONLY, current_position.y = toolchange_settings.change_point.y);
  799. #if NONE(TOOLCHANGE_PARK_X_ONLY, TOOLCHANGE_PARK_Y_ONLY)
  800. SECONDARY_AXIS_CODE(
  801. current_position.i = toolchange_settings.change_point.i,
  802. current_position.j = toolchange_settings.change_point.j,
  803. current_position.k = toolchange_settings.change_point.k,
  804. current_position.u = toolchange_settings.change_point.u,
  805. current_position.v = toolchange_settings.change_point.v,
  806. current_position.w = toolchange_settings.change_point.w
  807. );
  808. #endif
  809. planner.buffer_line(current_position, MMM_TO_MMS(TOOLCHANGE_PARK_XY_FEEDRATE), active_extruder);
  810. planner.synchronize();
  811. }
  812. #endif
  813. extruder_prime();
  814. // Move back
  815. #if ENABLED(TOOLCHANGE_PARK)
  816. if (ok) {
  817. #if ENABLED(TOOLCHANGE_NO_RETURN)
  818. const float temp = destination.z;
  819. destination = current_position;
  820. destination.z = temp;
  821. #endif
  822. prepare_internal_move_to_destination(TERN(TOOLCHANGE_NO_RETURN, planner.settings.max_feedrate_mm_s[Z_AXIS], MMM_TO_MMS(TOOLCHANGE_PARK_XY_FEEDRATE)));
  823. }
  824. #endif
  825. extruder_cutting_recover(destination.e); // Cutting recover
  826. }
  827. FS_DEBUG("<<< tool_change_prime");
  828. }
  829. #endif // TOOLCHANGE_FILAMENT_SWAP
  830. /**
  831. * Perform a tool-change, which may result in moving the
  832. * previous tool out of the way and the new tool into place.
  833. */
  834. void tool_change(const uint8_t new_tool, bool no_move/*=false*/) {
  835. if (TERN0(MAGNETIC_SWITCHING_TOOLHEAD, new_tool == active_extruder))
  836. return;
  837. #if ENABLED(MIXING_EXTRUDER)
  838. UNUSED(no_move);
  839. if (new_tool >= MIXING_VIRTUAL_TOOLS)
  840. return invalid_extruder_error(new_tool);
  841. #if MIXING_VIRTUAL_TOOLS > 1
  842. // T0-Tnnn: Switch virtual tool by changing the index to the mix
  843. mixer.T(new_tool);
  844. #endif
  845. #elif HAS_PRUSA_MMU2
  846. UNUSED(no_move);
  847. mmu2.tool_change(new_tool);
  848. #elif EXTRUDERS == 0
  849. // Nothing to do
  850. UNUSED(new_tool); UNUSED(no_move);
  851. #elif EXTRUDERS < 2
  852. UNUSED(no_move);
  853. if (new_tool) invalid_extruder_error(new_tool);
  854. return;
  855. #elif HAS_MULTI_EXTRUDER
  856. planner.synchronize();
  857. #if ENABLED(DUAL_X_CARRIAGE) // Only T0 allowed if the Printer is in DXC_DUPLICATION_MODE or DXC_MIRRORED_MODE
  858. if (new_tool != 0 && idex_is_duplicating())
  859. return invalid_extruder_error(new_tool);
  860. #endif
  861. if (new_tool >= EXTRUDERS)
  862. return invalid_extruder_error(new_tool);
  863. if (!no_move && homing_needed()) {
  864. no_move = true;
  865. DEBUG_ECHOLNPGM("No move (not homed)");
  866. }
  867. TERN_(HAS_MARLINUI_MENU, if (!no_move) ui.update());
  868. #if ENABLED(DUAL_X_CARRIAGE)
  869. const bool idex_full_control = dual_x_carriage_mode == DXC_FULL_CONTROL_MODE;
  870. #else
  871. constexpr bool idex_full_control = false;
  872. #endif
  873. const uint8_t old_tool = active_extruder;
  874. const bool can_move_away = !no_move && !idex_full_control;
  875. #if HAS_LEVELING
  876. // Set current position to the physical position
  877. TEMPORARY_BED_LEVELING_STATE(false);
  878. #endif
  879. // First tool priming. To prime again, reboot the machine. -- Should only occur for first T0 after powerup!
  880. #if ENABLED(TOOLCHANGE_FS_PRIME_FIRST_USED)
  881. if (enable_first_prime && old_tool == 0 && new_tool == 0 && !extruder_was_primed[0]) {
  882. tool_change_prime();
  883. TERN_(TOOLCHANGE_FS_INIT_BEFORE_SWAP, toolchange_extruder_ready.set(old_tool)); // Primed and initialized
  884. }
  885. #endif
  886. if (new_tool != old_tool || TERN0(PARKING_EXTRUDER, extruder_parked)) { // PARKING_EXTRUDER may need to attach old_tool when homing
  887. destination = current_position;
  888. #if BOTH(TOOLCHANGE_FILAMENT_SWAP, HAS_FAN) && TOOLCHANGE_FS_FAN >= 0
  889. // Store and stop fan. Restored on any exit.
  890. REMEMBER(fan, thermalManager.fan_speed[TOOLCHANGE_FS_FAN], 0);
  891. #endif
  892. // Z raise before retraction
  893. #if ENABLED(TOOLCHANGE_ZRAISE_BEFORE_RETRACT) && DISABLED(SWITCHING_NOZZLE)
  894. if (can_move_away && TERN1(TOOLCHANGE_PARK, toolchange_settings.enable_park)) {
  895. // Do a small lift to avoid the workpiece in the move back (below)
  896. current_position.z += toolchange_settings.z_raise;
  897. TERN_(HAS_SOFTWARE_ENDSTOPS, NOMORE(current_position.z, soft_endstop.max.z));
  898. fast_line_to_current(Z_AXIS);
  899. planner.synchronize();
  900. }
  901. #endif
  902. // Unload / Retract
  903. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  904. const bool should_swap = can_move_away && toolchange_settings.swap_length;
  905. if (should_swap) {
  906. if (too_cold(old_tool)) {
  907. // If SingleNozzle setup is too cold, unable to perform tool_change.
  908. if (ENABLED(SINGLENOZZLE)) { active_extruder = new_tool; return; }
  909. }
  910. else if (extruder_was_primed[old_tool]) {
  911. // Retract the old extruder if it was previously primed
  912. // To-Do: Should SingleNozzle always retract?
  913. FS_DEBUG("Retracting Filament for T", old_tool, ". | Distance: ", toolchange_settings.swap_length, " | Speed: ", MMM_TO_MMS(toolchange_settings.retract_speed), "mm/s");
  914. unscaled_e_move(-toolchange_settings.swap_length, MMM_TO_MMS(toolchange_settings.retract_speed));
  915. }
  916. }
  917. #endif
  918. TERN_(SWITCHING_NOZZLE_TWO_SERVOS, raise_nozzle(old_tool));
  919. REMEMBER(fr, feedrate_mm_s, XY_PROBE_FEEDRATE_MM_S);
  920. #if HAS_SOFTWARE_ENDSTOPS
  921. #if HAS_HOTEND_OFFSET
  922. #define _EXT_ARGS , old_tool, new_tool
  923. #else
  924. #define _EXT_ARGS
  925. #endif
  926. update_software_endstops(X_AXIS _EXT_ARGS);
  927. #if DISABLED(DUAL_X_CARRIAGE)
  928. update_software_endstops(Y_AXIS _EXT_ARGS);
  929. update_software_endstops(Z_AXIS _EXT_ARGS);
  930. #endif
  931. #endif
  932. #if DISABLED(TOOLCHANGE_ZRAISE_BEFORE_RETRACT) && DISABLED(SWITCHING_NOZZLE)
  933. if (can_move_away && TERN1(TOOLCHANGE_PARK, toolchange_settings.enable_park)) {
  934. // Do a small lift to avoid the workpiece in the move back (below)
  935. current_position.z += toolchange_settings.z_raise;
  936. TERN_(HAS_SOFTWARE_ENDSTOPS, NOMORE(current_position.z, soft_endstop.max.z));
  937. fast_line_to_current(Z_AXIS);
  938. }
  939. #endif
  940. // Toolchange park
  941. #if ENABLED(TOOLCHANGE_PARK) && DISABLED(SWITCHING_NOZZLE)
  942. if (can_move_away && toolchange_settings.enable_park) {
  943. IF_DISABLED(TOOLCHANGE_PARK_Y_ONLY, current_position.x = toolchange_settings.change_point.x);
  944. IF_DISABLED(TOOLCHANGE_PARK_X_ONLY, current_position.y = toolchange_settings.change_point.y);
  945. #if NONE(TOOLCHANGE_PARK_X_ONLY, TOOLCHANGE_PARK_Y_ONLY)
  946. SECONDARY_AXIS_CODE(
  947. current_position.i = toolchange_settings.change_point.i,
  948. current_position.j = toolchange_settings.change_point.j,
  949. current_position.k = toolchange_settings.change_point.k,
  950. current_position.u = toolchange_settings.change_point.u,
  951. current_position.v = toolchange_settings.change_point.v,
  952. current_position.w = toolchange_settings.change_point.w
  953. );
  954. #endif
  955. planner.buffer_line(current_position, MMM_TO_MMS(TOOLCHANGE_PARK_XY_FEEDRATE), old_tool);
  956. planner.synchronize();
  957. }
  958. #endif
  959. #if HAS_HOTEND_OFFSET
  960. xyz_pos_t diff = hotend_offset[new_tool] - hotend_offset[old_tool];
  961. TERN_(DUAL_X_CARRIAGE, diff.x = 0);
  962. #else
  963. constexpr xyz_pos_t diff{0};
  964. #endif
  965. #if ENABLED(DUAL_X_CARRIAGE)
  966. dualx_tool_change(new_tool, no_move);
  967. #elif ENABLED(PARKING_EXTRUDER) // Dual Parking extruder
  968. parking_extruder_tool_change(new_tool, no_move);
  969. #elif ENABLED(MAGNETIC_PARKING_EXTRUDER) // Magnetic Parking extruder
  970. magnetic_parking_extruder_tool_change(new_tool);
  971. #elif ENABLED(SWITCHING_TOOLHEAD) // Switching Toolhead
  972. switching_toolhead_tool_change(new_tool, no_move);
  973. #elif ENABLED(MAGNETIC_SWITCHING_TOOLHEAD) // Magnetic Switching Toolhead
  974. magnetic_switching_toolhead_tool_change(new_tool, no_move);
  975. #elif ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD) // Magnetic Switching ToolChanger
  976. em_switching_toolhead_tool_change(new_tool, no_move);
  977. #elif ENABLED(SWITCHING_NOZZLE) && !SWITCHING_NOZZLE_TWO_SERVOS // Switching Nozzle (single servo)
  978. // Raise by a configured distance to avoid workpiece, except with
  979. // SWITCHING_NOZZLE_TWO_SERVOS, as both nozzles will lift instead.
  980. if (!no_move) {
  981. const float newz = current_position.z + _MAX(-diff.z, 0.0);
  982. // Check if Z has space to compensate at least z_offset, and if not, just abort now
  983. const float maxz = _MIN(TERN(HAS_SOFTWARE_ENDSTOPS, soft_endstop.max.z, Z_MAX_POS), Z_MAX_POS);
  984. if (newz > maxz) return;
  985. current_position.z = _MIN(newz + toolchange_settings.z_raise, maxz);
  986. fast_line_to_current(Z_AXIS);
  987. }
  988. move_nozzle_servo(new_tool);
  989. #endif
  990. IF_DISABLED(DUAL_X_CARRIAGE, active_extruder = new_tool); // Set the new active extruder
  991. TERN_(TOOL_SENSOR, tool_sensor_disabled = false);
  992. (void)check_tool_sensor_stats(active_extruder, true);
  993. // The newly-selected extruder XYZ is actually at...
  994. DEBUG_ECHOLNPGM("Offset Tool XYZ by { ", diff.x, ", ", diff.y, ", ", diff.z, " }");
  995. current_position += diff;
  996. // Tell the planner the new "current position"
  997. sync_plan_position();
  998. #if ENABLED(DELTA)
  999. //LOOP_NUM_AXES(i) update_software_endstops(i); // or modify the constrain function
  1000. const bool safe_to_move = current_position.z < delta_clip_start_height - 1;
  1001. #else
  1002. constexpr bool safe_to_move = true;
  1003. #endif
  1004. // Return to position and lower again
  1005. const bool should_move = safe_to_move && !no_move && IsRunning();
  1006. if (should_move) {
  1007. #if EITHER(SINGLENOZZLE_STANDBY_TEMP, SINGLENOZZLE_STANDBY_FAN)
  1008. thermalManager.singlenozzle_change(old_tool, new_tool);
  1009. #endif
  1010. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  1011. if (should_swap && !too_cold(active_extruder))
  1012. extruder_prime(); // Prime selected Extruder
  1013. #endif
  1014. // Prevent a move outside physical bounds
  1015. #if ENABLED(MAGNETIC_SWITCHING_TOOLHEAD)
  1016. // If the original position is within tool store area, go to X origin at once
  1017. if (destination.y < SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_CLEAR) {
  1018. current_position.x = X_MIN_POS;
  1019. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[X_AXIS], new_tool);
  1020. planner.synchronize();
  1021. }
  1022. #else
  1023. apply_motion_limits(destination);
  1024. #endif
  1025. // Should the nozzle move back to the old position?
  1026. if (can_move_away) {
  1027. #if ENABLED(TOOLCHANGE_NO_RETURN)
  1028. // Just move back down
  1029. DEBUG_ECHOLNPGM("Move back Z only");
  1030. if (TERN1(TOOLCHANGE_PARK, toolchange_settings.enable_park))
  1031. do_blocking_move_to_z(destination.z, planner.settings.max_feedrate_mm_s[Z_AXIS]);
  1032. #else
  1033. // Move back to the original (or adjusted) position
  1034. DEBUG_POS("Move back", destination);
  1035. #if ENABLED(TOOLCHANGE_PARK)
  1036. if (toolchange_settings.enable_park) do_blocking_move_to_xy_z(destination, destination.z, MMM_TO_MMS(TOOLCHANGE_PARK_XY_FEEDRATE));
  1037. #else
  1038. do_blocking_move_to_xy(destination, planner.settings.max_feedrate_mm_s[X_AXIS]);
  1039. do_blocking_move_to_z(destination.z, planner.settings.max_feedrate_mm_s[Z_AXIS]);
  1040. #endif
  1041. #endif
  1042. }
  1043. else DEBUG_ECHOLNPGM("Move back skipped");
  1044. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  1045. if (should_swap && !too_cold(active_extruder)) {
  1046. extruder_cutting_recover(0); // New extruder primed and set to 0
  1047. // Restart Fan
  1048. #if HAS_FAN && TOOLCHANGE_FS_FAN >= 0
  1049. RESTORE(fan);
  1050. #endif
  1051. }
  1052. #endif
  1053. TERN_(DUAL_X_CARRIAGE, idex_set_parked(false));
  1054. }
  1055. #if ENABLED(SWITCHING_NOZZLE)
  1056. // Move back down. (Including when the new tool is higher.)
  1057. if (!should_move)
  1058. do_blocking_move_to_z(destination.z, planner.settings.max_feedrate_mm_s[Z_AXIS]);
  1059. #endif
  1060. TERN_(SWITCHING_NOZZLE_TWO_SERVOS, lower_nozzle(new_tool));
  1061. } // (new_tool != old_tool)
  1062. planner.synchronize();
  1063. #if ENABLED(EXT_SOLENOID) && DISABLED(PARKING_EXTRUDER)
  1064. disable_all_solenoids();
  1065. enable_solenoid(active_extruder);
  1066. #endif
  1067. #if HAS_PRUSA_MMU1
  1068. if (new_tool >= E_STEPPERS) return invalid_extruder_error(new_tool);
  1069. select_multiplexed_stepper(new_tool);
  1070. #endif
  1071. #if DO_SWITCH_EXTRUDER
  1072. planner.synchronize();
  1073. move_extruder_servo(active_extruder);
  1074. #endif
  1075. TERN_(HAS_FANMUX, fanmux_switch(active_extruder));
  1076. if (ENABLED(EVENT_GCODE_TOOLCHANGE_ALWAYS_RUN) || !no_move) {
  1077. #ifdef EVENT_GCODE_TOOLCHANGE_T0
  1078. if (new_tool == 0)
  1079. gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T0));
  1080. #endif
  1081. #ifdef EVENT_GCODE_TOOLCHANGE_T1
  1082. if (new_tool == 1)
  1083. gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T1));
  1084. #endif
  1085. #ifdef EVENT_GCODE_AFTER_TOOLCHANGE
  1086. if (TERN1(DUAL_X_CARRIAGE, dual_x_carriage_mode == DXC_AUTO_PARK_MODE))
  1087. gcode.process_subcommands_now(F(EVENT_GCODE_AFTER_TOOLCHANGE));
  1088. #endif
  1089. }
  1090. SERIAL_ECHOLNPGM(STR_ACTIVE_EXTRUDER, active_extruder);
  1091. #endif // HAS_MULTI_EXTRUDER
  1092. }
  1093. #if ENABLED(TOOLCHANGE_MIGRATION_FEATURE)
  1094. #define DEBUG_OUT ENABLED(DEBUG_TOOLCHANGE_MIGRATION_FEATURE)
  1095. #include "../core/debug_out.h"
  1096. bool extruder_migration() {
  1097. #if ENABLED(PREVENT_COLD_EXTRUSION)
  1098. if (thermalManager.targetTooColdToExtrude(active_extruder)) {
  1099. DEBUG_ECHOLNPGM("Migration Source Too Cold");
  1100. return false;
  1101. }
  1102. #endif
  1103. // No auto-migration or specified target?
  1104. if (!migration.target && active_extruder >= migration.last) {
  1105. DEBUG_ECHO_MSG("No Migration Target");
  1106. DEBUG_ECHO_MSG("Target: ", migration.target, " Last: ", migration.last, " Active: ", active_extruder);
  1107. migration.automode = false;
  1108. return false;
  1109. }
  1110. // Migrate to a target or the next extruder
  1111. uint8_t migration_extruder = active_extruder;
  1112. if (migration.target) {
  1113. DEBUG_ECHOLNPGM("Migration using fixed target");
  1114. // Specified target ok?
  1115. const int16_t t = migration.target - 1;
  1116. if (t != active_extruder) migration_extruder = t;
  1117. }
  1118. else if (migration.automode && migration_extruder < migration.last && migration_extruder < EXTRUDERS - 1)
  1119. migration_extruder++;
  1120. if (migration_extruder == active_extruder) {
  1121. DEBUG_ECHOLNPGM("Migration source matches active");
  1122. return false;
  1123. }
  1124. // Migration begins
  1125. DEBUG_ECHOLNPGM("Beginning migration");
  1126. migration.in_progress = true; // Prevent runout script
  1127. planner.synchronize();
  1128. // Remember position before migration
  1129. const float resume_current_e = current_position.e;
  1130. // Migrate the flow
  1131. planner.set_flow(migration_extruder, planner.flow_percentage[active_extruder]);
  1132. // Migrate the retracted state
  1133. #if ENABLED(FWRETRACT)
  1134. fwretract.retracted.set(migration_extruder, fwretract.retracted[active_extruder]);
  1135. #endif
  1136. // Migrate the temperature to the new hotend
  1137. #if HAS_MULTI_HOTEND
  1138. thermalManager.setTargetHotend(thermalManager.degTargetHotend(active_extruder), migration_extruder);
  1139. TERN_(AUTOTEMP, planner.autotemp_update());
  1140. thermalManager.set_heating_message(0);
  1141. thermalManager.wait_for_hotend(active_extruder);
  1142. #endif
  1143. // Migrate Linear Advance K factor to the new extruder
  1144. TERN_(LIN_ADVANCE, planner.extruder_advance_K[active_extruder] = planner.extruder_advance_K[migration_extruder]);
  1145. // Perform the tool change
  1146. tool_change(migration_extruder);
  1147. // Retract if previously retracted
  1148. #if ENABLED(FWRETRACT)
  1149. if (fwretract.retracted[active_extruder])
  1150. unscaled_e_move(-fwretract.settings.retract_length, fwretract.settings.retract_feedrate_mm_s);
  1151. #endif
  1152. // If no available extruder
  1153. if (EXTRUDERS < 2 || active_extruder >= EXTRUDERS - 2 || active_extruder == migration.last)
  1154. migration.automode = false;
  1155. migration.in_progress = false;
  1156. current_position.e = resume_current_e;
  1157. planner.synchronize();
  1158. planner.set_e_position_mm(current_position.e); // New extruder primed and ready
  1159. DEBUG_ECHOLNPGM("Migration Complete");
  1160. return true;
  1161. }
  1162. #endif // TOOLCHANGE_MIGRATION_FEATURE