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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  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_OUT ENABLED(DEBUG_TOOL_CHANGE)
  31. #include "../core/debug_out.h"
  32. #if HAS_MULTI_EXTRUDER
  33. toolchange_settings_t toolchange_settings; // Initialized by settings.load()
  34. #endif
  35. #if ENABLED(TOOLCHANGE_MIGRATION_FEATURE)
  36. migration_settings_t migration = migration_defaults;
  37. bool enable_first_prime;
  38. #endif
  39. #if ENABLED(TOOLCHANGE_FS_INIT_BEFORE_SWAP)
  40. bool toolchange_extruder_ready[EXTRUDERS];
  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 ((EXTRUDERS & 1) && e < EXTRUDERS - 1) {
  95. MOVE_SERVO(_SERVO_NR(e), 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. MOVE_SERVO(sns_index[e], sns_angles[angle_index]);
  107. safe_delay(500);
  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. MOVE_SERVO(SWITCHING_NOZZLE_SERVO_NR, servo_angles[SWITCHING_NOZZLE_SERVO_NR][angle_index]);
  115. safe_delay(500);
  116. }
  117. #endif
  118. #endif // SWITCHING_NOZZLE
  119. void _line_to_current(const AxisEnum fr_axis, const float fscale=1) {
  120. line_to_current_position(planner.settings.max_feedrate_mm_s[fr_axis] * fscale);
  121. }
  122. void slow_line_to_current(const AxisEnum fr_axis) { _line_to_current(fr_axis, 0.2f); }
  123. void fast_line_to_current(const AxisEnum fr_axis) { _line_to_current(fr_axis, 0.5f); }
  124. #if ENABLED(MAGNETIC_PARKING_EXTRUDER)
  125. float parkingposx[2], // M951 R L
  126. parkinggrabdistance, // M951 I
  127. parkingslowspeed, // M951 J
  128. parkinghighspeed, // M951 H
  129. parkingtraveldistance, // M951 D
  130. compensationmultiplier;
  131. inline void magnetic_parking_extruder_tool_change(const uint8_t new_tool) {
  132. const float oldx = current_position.x,
  133. grabpos = mpe_settings.parking_xpos[new_tool] + (new_tool ? mpe_settings.grab_distance : -mpe_settings.grab_distance),
  134. offsetcompensation = TERN0(HAS_HOTEND_OFFSET, hotend_offset[active_extruder].x * mpe_settings.compensation_factor);
  135. if (homing_needed_error(_BV(X_AXIS))) return;
  136. /**
  137. * Z Lift and Nozzle Offset shift ar defined in caller method to work equal with any Multi Hotend realization
  138. *
  139. * Steps:
  140. * 1. Move high speed to park position of new extruder
  141. * 2. Move to couple position of new extruder (this also discouple the old extruder)
  142. * 3. Move to park position of new extruder
  143. * 4. Move high speed to approach park position of old extruder
  144. * 5. Move to park position of old extruder
  145. * 6. Move to starting position
  146. */
  147. // STEP 1
  148. current_position.x = mpe_settings.parking_xpos[new_tool] + offsetcompensation;
  149. DEBUG_ECHOPGM("(1) Move extruder ", new_tool);
  150. DEBUG_POS(" to new extruder ParkPos", current_position);
  151. planner.buffer_line(current_position, mpe_settings.fast_feedrate, new_tool);
  152. planner.synchronize();
  153. // STEP 2
  154. current_position.x = grabpos + offsetcompensation;
  155. DEBUG_ECHOPGM("(2) Couple extruder ", new_tool);
  156. DEBUG_POS(" to new extruder GrabPos", current_position);
  157. planner.buffer_line(current_position, mpe_settings.slow_feedrate, new_tool);
  158. planner.synchronize();
  159. // Delay before moving tool, to allow magnetic coupling
  160. gcode.dwell(150);
  161. // STEP 3
  162. current_position.x = mpe_settings.parking_xpos[new_tool] + offsetcompensation;
  163. DEBUG_ECHOPGM("(3) Move extruder ", new_tool);
  164. DEBUG_POS(" back to new extruder ParkPos", current_position);
  165. planner.buffer_line(current_position, mpe_settings.slow_feedrate, new_tool);
  166. planner.synchronize();
  167. // STEP 4
  168. current_position.x = mpe_settings.parking_xpos[active_extruder] + (active_extruder == 0 ? MPE_TRAVEL_DISTANCE : -MPE_TRAVEL_DISTANCE) + offsetcompensation;
  169. DEBUG_ECHOPGM("(4) Move extruder ", new_tool);
  170. DEBUG_POS(" close to old extruder ParkPos", current_position);
  171. planner.buffer_line(current_position, mpe_settings.fast_feedrate, new_tool);
  172. planner.synchronize();
  173. // STEP 5
  174. current_position.x = mpe_settings.parking_xpos[active_extruder] + offsetcompensation;
  175. DEBUG_ECHOPGM("(5) Park extruder ", new_tool);
  176. DEBUG_POS(" at old extruder ParkPos", current_position);
  177. planner.buffer_line(current_position, mpe_settings.slow_feedrate, new_tool);
  178. planner.synchronize();
  179. // STEP 6
  180. current_position.x = oldx;
  181. DEBUG_ECHOPGM("(6) Move extruder ", new_tool);
  182. DEBUG_POS(" to starting position", current_position);
  183. planner.buffer_line(current_position, mpe_settings.fast_feedrate, new_tool);
  184. planner.synchronize();
  185. DEBUG_ECHOLNPGM("Autopark done.");
  186. }
  187. #elif ENABLED(PARKING_EXTRUDER)
  188. void pe_solenoid_init() {
  189. LOOP_LE_N(n, 1) pe_solenoid_set_pin_state(n, !PARKING_EXTRUDER_SOLENOIDS_PINS_ACTIVE);
  190. }
  191. void pe_solenoid_set_pin_state(const uint8_t extruder_num, const uint8_t state) {
  192. switch (extruder_num) {
  193. case 1: OUT_WRITE(SOL1_PIN, state); break;
  194. default: OUT_WRITE(SOL0_PIN, state); break;
  195. }
  196. #if PARKING_EXTRUDER_SOLENOIDS_DELAY > 0
  197. gcode.dwell(PARKING_EXTRUDER_SOLENOIDS_DELAY);
  198. #endif
  199. }
  200. bool extruder_parked = true, do_solenoid_activation = true;
  201. // Modifies tool_change() behavior based on homing side
  202. bool parking_extruder_unpark_after_homing(const uint8_t final_tool, bool homed_towards_final_tool) {
  203. do_solenoid_activation = false; // Tell parking_extruder_tool_change to skip solenoid activation
  204. if (!extruder_parked) return false; // nothing to do
  205. if (homed_towards_final_tool) {
  206. pe_solenoid_magnet_off(1 - final_tool);
  207. DEBUG_ECHOLNPGM("Disengage magnet", 1 - final_tool);
  208. pe_solenoid_magnet_on(final_tool);
  209. DEBUG_ECHOLNPGM("Engage magnet", final_tool);
  210. parking_extruder_set_parked(false);
  211. return false;
  212. }
  213. return true;
  214. }
  215. inline void parking_extruder_tool_change(const uint8_t new_tool, bool no_move) {
  216. if (!no_move) {
  217. constexpr float parkingposx[] = PARKING_EXTRUDER_PARKING_X;
  218. #if HAS_HOTEND_OFFSET
  219. const float x_offset = hotend_offset[active_extruder].x;
  220. #else
  221. constexpr float x_offset = 0;
  222. #endif
  223. const float midpos = (parkingposx[0] + parkingposx[1]) * 0.5f + x_offset,
  224. grabpos = parkingposx[new_tool] + (new_tool ? PARKING_EXTRUDER_GRAB_DISTANCE : -(PARKING_EXTRUDER_GRAB_DISTANCE)) + x_offset;
  225. /**
  226. * 1. Move to park position of old extruder
  227. * 2. Disengage magnetic field, wait for delay
  228. * 3. Move near new extruder
  229. * 4. Engage magnetic field for new extruder
  230. * 5. Move to parking incl. offset of new extruder
  231. * 6. Lower Z-Axis
  232. */
  233. // STEP 1
  234. DEBUG_POS("Start PE Tool-Change", current_position);
  235. // Don't park the active_extruder unless unparked
  236. if (!extruder_parked) {
  237. current_position.x = parkingposx[active_extruder] + x_offset;
  238. DEBUG_ECHOLNPGM("(1) Park extruder ", active_extruder);
  239. DEBUG_POS("Moving ParkPos", current_position);
  240. fast_line_to_current(X_AXIS);
  241. // STEP 2
  242. planner.synchronize();
  243. DEBUG_ECHOLNPGM("(2) Disengage magnet");
  244. pe_solenoid_magnet_off(active_extruder);
  245. // STEP 3
  246. current_position.x += active_extruder ? -10 : 10; // move 10mm away from parked extruder
  247. DEBUG_ECHOLNPGM("(3) Move near new extruder");
  248. DEBUG_POS("Move away from parked extruder", current_position);
  249. fast_line_to_current(X_AXIS);
  250. }
  251. // STEP 4
  252. planner.synchronize();
  253. DEBUG_ECHOLNPGM("(4) Engage magnetic field");
  254. // Just save power for inverted magnets
  255. TERN_(PARKING_EXTRUDER_SOLENOIDS_INVERT, pe_solenoid_magnet_on(active_extruder));
  256. pe_solenoid_magnet_on(new_tool);
  257. // STEP 5
  258. current_position.x = grabpos + (new_tool ? -10 : 10);
  259. fast_line_to_current(X_AXIS);
  260. current_position.x = grabpos;
  261. DEBUG_SYNCHRONIZE();
  262. DEBUG_POS("(5) Unpark extruder", current_position);
  263. slow_line_to_current(X_AXIS);
  264. // STEP 6
  265. current_position.x = DIFF_TERN(HAS_HOTEND_OFFSET, midpos, hotend_offset[new_tool].x);
  266. DEBUG_SYNCHRONIZE();
  267. DEBUG_POS("(6) Move midway between hotends", current_position);
  268. fast_line_to_current(X_AXIS);
  269. planner.synchronize(); // Always sync the final move
  270. DEBUG_POS("PE Tool-Change done.", current_position);
  271. parking_extruder_set_parked(false);
  272. }
  273. else if (do_solenoid_activation) {
  274. // Deactivate current extruder solenoid
  275. pe_solenoid_set_pin_state(active_extruder, !PARKING_EXTRUDER_SOLENOIDS_PINS_ACTIVE);
  276. // Engage new extruder magnetic field
  277. pe_solenoid_set_pin_state(new_tool, PARKING_EXTRUDER_SOLENOIDS_PINS_ACTIVE);
  278. }
  279. do_solenoid_activation = true; // Activate solenoid for subsequent tool_change()
  280. }
  281. #endif // PARKING_EXTRUDER
  282. #if ENABLED(TOOL_SENSOR)
  283. bool tool_sensor_disabled; // = false
  284. // Return a bitmask of tool sensor states
  285. inline uint8_t poll_tool_sensor_pins() {
  286. return (0
  287. #if PIN_EXISTS(TOOL_SENSOR1)
  288. | (READ(TOOL_SENSOR1_PIN) << 0)
  289. #endif
  290. #if PIN_EXISTS(TOOL_SENSOR2)
  291. | (READ(TOOL_SENSOR2_PIN) << 1)
  292. #endif
  293. #if PIN_EXISTS(TOOL_SENSOR3)
  294. | (READ(TOOL_SENSOR3_PIN) << 2)
  295. #endif
  296. #if PIN_EXISTS(TOOL_SENSOR4)
  297. | (READ(TOOL_SENSOR4_PIN) << 3)
  298. #endif
  299. #if PIN_EXISTS(TOOL_SENSOR5)
  300. | (READ(TOOL_SENSOR5_PIN) << 4)
  301. #endif
  302. #if PIN_EXISTS(TOOL_SENSOR6)
  303. | (READ(TOOL_SENSOR6_PIN) << 5)
  304. #endif
  305. #if PIN_EXISTS(TOOL_SENSOR7)
  306. | (READ(TOOL_SENSOR7_PIN) << 6)
  307. #endif
  308. #if PIN_EXISTS(TOOL_SENSOR8)
  309. | (READ(TOOL_SENSOR8_PIN) << 7)
  310. #endif
  311. );
  312. }
  313. uint8_t check_tool_sensor_stats(const uint8_t tool_index, const bool kill_on_error/*=false*/, const bool disable/*=false*/) {
  314. static uint8_t sensor_tries; // = 0
  315. for (;;) {
  316. if (poll_tool_sensor_pins() == _BV(tool_index)) {
  317. sensor_tries = 0;
  318. return tool_index;
  319. }
  320. else if (kill_on_error && (!tool_sensor_disabled || disable)) {
  321. sensor_tries++;
  322. if (sensor_tries > 10) kill(F("Tool Sensor error"));
  323. safe_delay(5);
  324. }
  325. else {
  326. sensor_tries++;
  327. if (sensor_tries > 10) return -1;
  328. safe_delay(5);
  329. }
  330. }
  331. }
  332. inline void switching_toolhead_lock(const bool locked) {
  333. #ifdef SWITCHING_TOOLHEAD_SERVO_ANGLES
  334. const uint16_t swt_angles[2] = SWITCHING_TOOLHEAD_SERVO_ANGLES;
  335. MOVE_SERVO(SWITCHING_TOOLHEAD_SERVO_NR, swt_angles[locked ? 0 : 1]);
  336. #elif PIN_EXISTS(SWT_SOLENOID)
  337. OUT_WRITE(SWT_SOLENOID_PIN, locked);
  338. gcode.dwell(10);
  339. #else
  340. #error "No toolhead locking mechanism configured."
  341. #endif
  342. }
  343. #include <bitset>
  344. void swt_init() {
  345. switching_toolhead_lock(true);
  346. #if ENABLED(TOOL_SENSOR)
  347. // Init tool sensors
  348. #if PIN_EXISTS(TOOL_SENSOR1)
  349. SET_INPUT_PULLUP(TOOL_SENSOR1_PIN);
  350. #endif
  351. #if PIN_EXISTS(TOOL_SENSOR2)
  352. SET_INPUT_PULLUP(TOOL_SENSOR2_PIN);
  353. #endif
  354. #if PIN_EXISTS(TOOL_SENSOR3)
  355. SET_INPUT_PULLUP(TOOL_SENSOR3_PIN);
  356. #endif
  357. #if PIN_EXISTS(TOOL_SENSOR4)
  358. SET_INPUT_PULLUP(TOOL_SENSOR4_PIN);
  359. #endif
  360. #if PIN_EXISTS(TOOL_SENSOR5)
  361. SET_INPUT_PULLUP(TOOL_SENSOR5_PIN);
  362. #endif
  363. #if PIN_EXISTS(TOOL_SENSOR6)
  364. SET_INPUT_PULLUP(TOOL_SENSOR6_PIN);
  365. #endif
  366. #if PIN_EXISTS(TOOL_SENSOR7)
  367. SET_INPUT_PULLUP(TOOL_SENSOR7_PIN);
  368. #endif
  369. #if PIN_EXISTS(TOOL_SENSOR8)
  370. SET_INPUT_PULLUP(TOOL_SENSOR8_PIN);
  371. #endif
  372. if (check_tool_sensor_stats(0)) {
  373. LCD_MESSAGE_F("TC error");
  374. switching_toolhead_lock(false);
  375. while (check_tool_sensor_stats(0)) { /* nada */ }
  376. switching_toolhead_lock(true);
  377. }
  378. LCD_MESSAGE_F("TC Success");
  379. #endif // TOOL_SENSOR
  380. }
  381. #endif // TOOL_SENSOR
  382. #if ENABLED(SWITCHING_TOOLHEAD)
  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. void tool_change_prime() {
  670. if (toolchange_settings.extra_prime > 0
  671. && TERN(PREVENT_COLD_EXTRUSION, !thermalManager.targetTooColdToExtrude(active_extruder), 1)
  672. ) {
  673. destination = current_position; // Remember the old position
  674. const bool ok = TERN1(TOOLCHANGE_PARK, all_axes_homed() && toolchange_settings.enable_park);
  675. #if HAS_FAN && TOOLCHANGE_FS_FAN >= 0
  676. // Store and stop fan. Restored on any exit.
  677. REMEMBER(fan, thermalManager.fan_speed[TOOLCHANGE_FS_FAN], 0);
  678. #endif
  679. // Z raise
  680. if (ok) {
  681. // Do a small lift to avoid the workpiece in the move back (below)
  682. current_position.z += toolchange_settings.z_raise;
  683. TERN_(HAS_SOFTWARE_ENDSTOPS, NOMORE(current_position.z, soft_endstop.max.z));
  684. fast_line_to_current(Z_AXIS);
  685. planner.synchronize();
  686. }
  687. // Park
  688. #if ENABLED(TOOLCHANGE_PARK)
  689. if (ok) {
  690. IF_DISABLED(TOOLCHANGE_PARK_Y_ONLY, current_position.x = toolchange_settings.change_point.x);
  691. IF_DISABLED(TOOLCHANGE_PARK_X_ONLY, current_position.y = toolchange_settings.change_point.y);
  692. #if NONE(TOOLCHANGE_PARK_X_ONLY, TOOLCHANGE_PARK_Y_ONLY)
  693. SECONDARY_AXIS_CODE(
  694. current_position.i = toolchange_settings.change_point.i,
  695. current_position.j = toolchange_settings.change_point.j,
  696. current_position.k = toolchange_settings.change_point.k,
  697. current_position.u = toolchange_settings.change_point.u,
  698. current_position.v = toolchange_settings.change_point.v,
  699. current_position.w = toolchange_settings.change_point.w,
  700. );
  701. #endif
  702. planner.buffer_line(current_position, MMM_TO_MMS(TOOLCHANGE_PARK_XY_FEEDRATE), active_extruder);
  703. planner.synchronize();
  704. }
  705. #endif
  706. // Prime (All distances are added and slowed down to ensure secure priming in all circumstances)
  707. unscaled_e_move(toolchange_settings.swap_length + toolchange_settings.extra_prime, MMM_TO_MMS(toolchange_settings.prime_speed));
  708. // Cutting retraction
  709. #if TOOLCHANGE_FS_WIPE_RETRACT
  710. unscaled_e_move(-(TOOLCHANGE_FS_WIPE_RETRACT), MMM_TO_MMS(toolchange_settings.retract_speed));
  711. #endif
  712. // Cool down with fan
  713. #if HAS_FAN && TOOLCHANGE_FS_FAN >= 0
  714. thermalManager.fan_speed[TOOLCHANGE_FS_FAN] = toolchange_settings.fan_speed;
  715. gcode.dwell(SEC_TO_MS(toolchange_settings.fan_time));
  716. thermalManager.fan_speed[TOOLCHANGE_FS_FAN] = 0;
  717. #endif
  718. // Move back
  719. #if ENABLED(TOOLCHANGE_PARK)
  720. if (ok) {
  721. #if ENABLED(TOOLCHANGE_NO_RETURN)
  722. const float temp = destination.z;
  723. destination = current_position;
  724. destination.z = temp;
  725. #endif
  726. prepare_internal_move_to_destination(TERN(TOOLCHANGE_NO_RETURN, planner.settings.max_feedrate_mm_s[Z_AXIS], MMM_TO_MMS(TOOLCHANGE_PARK_XY_FEEDRATE)));
  727. }
  728. #endif
  729. // Cutting recover
  730. unscaled_e_move(toolchange_settings.extra_resume + TOOLCHANGE_FS_WIPE_RETRACT, MMM_TO_MMS(toolchange_settings.unretract_speed));
  731. // Resume at the old E position
  732. current_position.e = destination.e;
  733. sync_plan_position_e();
  734. }
  735. }
  736. #endif // TOOLCHANGE_FILAMENT_SWAP
  737. /**
  738. * Perform a tool-change, which may result in moving the
  739. * previous tool out of the way and the new tool into place.
  740. */
  741. void tool_change(const uint8_t new_tool, bool no_move/*=false*/) {
  742. if (TERN0(MAGNETIC_SWITCHING_TOOLHEAD, new_tool == active_extruder))
  743. return;
  744. #if ENABLED(MIXING_EXTRUDER)
  745. UNUSED(no_move);
  746. if (new_tool >= MIXING_VIRTUAL_TOOLS)
  747. return invalid_extruder_error(new_tool);
  748. #if MIXING_VIRTUAL_TOOLS > 1
  749. // T0-Tnnn: Switch virtual tool by changing the index to the mix
  750. mixer.T(new_tool);
  751. #endif
  752. #elif HAS_PRUSA_MMU2
  753. UNUSED(no_move);
  754. mmu2.tool_change(new_tool);
  755. #elif EXTRUDERS == 0
  756. // Nothing to do
  757. UNUSED(new_tool); UNUSED(no_move);
  758. #elif EXTRUDERS < 2
  759. UNUSED(no_move);
  760. if (new_tool) invalid_extruder_error(new_tool);
  761. return;
  762. #elif HAS_MULTI_EXTRUDER
  763. planner.synchronize();
  764. #if ENABLED(DUAL_X_CARRIAGE) // Only T0 allowed if the Printer is in DXC_DUPLICATION_MODE or DXC_MIRRORED_MODE
  765. if (new_tool != 0 && idex_is_duplicating())
  766. return invalid_extruder_error(new_tool);
  767. #endif
  768. if (new_tool >= EXTRUDERS)
  769. return invalid_extruder_error(new_tool);
  770. if (!no_move && homing_needed()) {
  771. no_move = true;
  772. DEBUG_ECHOLNPGM("No move (not homed)");
  773. }
  774. TERN_(HAS_MARLINUI_MENU, if (!no_move) ui.update());
  775. #if ENABLED(DUAL_X_CARRIAGE)
  776. const bool idex_full_control = dual_x_carriage_mode == DXC_FULL_CONTROL_MODE;
  777. #else
  778. constexpr bool idex_full_control = false;
  779. #endif
  780. const uint8_t old_tool = active_extruder;
  781. const bool can_move_away = !no_move && !idex_full_control;
  782. #if HAS_LEVELING
  783. // Set current position to the physical position
  784. TEMPORARY_BED_LEVELING_STATE(false);
  785. #endif
  786. // First tool priming. To prime again, reboot the machine.
  787. #if ENABLED(TOOLCHANGE_FS_PRIME_FIRST_USED)
  788. static bool first_tool_is_primed = false;
  789. if (new_tool == old_tool && !first_tool_is_primed && enable_first_prime) {
  790. tool_change_prime();
  791. first_tool_is_primed = true;
  792. TERN_(TOOLCHANGE_FS_INIT_BEFORE_SWAP, toolchange_extruder_ready[old_tool] = true); // Primed and initialized
  793. }
  794. #endif
  795. if (new_tool != old_tool || TERN0(PARKING_EXTRUDER, extruder_parked)) { // PARKING_EXTRUDER may need to attach old_tool when homing
  796. destination = current_position;
  797. #if BOTH(TOOLCHANGE_FILAMENT_SWAP, HAS_FAN) && TOOLCHANGE_FS_FAN >= 0
  798. // Store and stop fan. Restored on any exit.
  799. REMEMBER(fan, thermalManager.fan_speed[TOOLCHANGE_FS_FAN], 0);
  800. #endif
  801. // Z raise before retraction
  802. #if ENABLED(TOOLCHANGE_ZRAISE_BEFORE_RETRACT) && DISABLED(SWITCHING_NOZZLE)
  803. if (can_move_away && TERN1(TOOLCHANGE_PARK, toolchange_settings.enable_park)) {
  804. // Do a small lift to avoid the workpiece in the move back (below)
  805. current_position.z += toolchange_settings.z_raise;
  806. TERN_(HAS_SOFTWARE_ENDSTOPS, NOMORE(current_position.z, soft_endstop.max.z));
  807. fast_line_to_current(Z_AXIS);
  808. planner.synchronize();
  809. }
  810. #endif
  811. // Unload / Retract
  812. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  813. const bool should_swap = can_move_away && toolchange_settings.swap_length,
  814. too_cold = TERN0(PREVENT_COLD_EXTRUSION,
  815. !DEBUGGING(DRYRUN) && (thermalManager.targetTooColdToExtrude(old_tool) || thermalManager.targetTooColdToExtrude(new_tool))
  816. );
  817. if (should_swap) {
  818. if (too_cold) {
  819. SERIAL_ECHO_MSG(STR_ERR_HOTEND_TOO_COLD);
  820. if (ENABLED(SINGLENOZZLE)) { active_extruder = new_tool; return; }
  821. }
  822. else {
  823. // For first new tool, change without unloading the old. 'Just prime/init the new'
  824. if (TERN1(TOOLCHANGE_FS_PRIME_FIRST_USED, first_tool_is_primed))
  825. unscaled_e_move(-toolchange_settings.swap_length, MMM_TO_MMS(toolchange_settings.retract_speed));
  826. TERN_(TOOLCHANGE_FS_PRIME_FIRST_USED, first_tool_is_primed = true); // The first new tool will be primed by toolchanging
  827. }
  828. }
  829. #endif
  830. TERN_(SWITCHING_NOZZLE_TWO_SERVOS, raise_nozzle(old_tool));
  831. REMEMBER(fr, feedrate_mm_s, XY_PROBE_FEEDRATE_MM_S);
  832. #if HAS_SOFTWARE_ENDSTOPS
  833. #if HAS_HOTEND_OFFSET
  834. #define _EXT_ARGS , old_tool, new_tool
  835. #else
  836. #define _EXT_ARGS
  837. #endif
  838. update_software_endstops(X_AXIS _EXT_ARGS);
  839. #if DISABLED(DUAL_X_CARRIAGE)
  840. update_software_endstops(Y_AXIS _EXT_ARGS);
  841. update_software_endstops(Z_AXIS _EXT_ARGS);
  842. #endif
  843. #endif
  844. #if DISABLED(TOOLCHANGE_ZRAISE_BEFORE_RETRACT) && DISABLED(SWITCHING_NOZZLE)
  845. if (can_move_away && TERN1(TOOLCHANGE_PARK, toolchange_settings.enable_park)) {
  846. // Do a small lift to avoid the workpiece in the move back (below)
  847. current_position.z += toolchange_settings.z_raise;
  848. TERN_(HAS_SOFTWARE_ENDSTOPS, NOMORE(current_position.z, soft_endstop.max.z));
  849. fast_line_to_current(Z_AXIS);
  850. }
  851. #endif
  852. // Toolchange park
  853. #if ENABLED(TOOLCHANGE_PARK) && DISABLED(SWITCHING_NOZZLE)
  854. if (can_move_away && toolchange_settings.enable_park) {
  855. IF_DISABLED(TOOLCHANGE_PARK_Y_ONLY, current_position.x = toolchange_settings.change_point.x);
  856. IF_DISABLED(TOOLCHANGE_PARK_X_ONLY, current_position.y = toolchange_settings.change_point.y);
  857. #if NONE(TOOLCHANGE_PARK_X_ONLY, TOOLCHANGE_PARK_Y_ONLY)
  858. SECONDARY_AXIS_CODE(
  859. current_position.i = toolchange_settings.change_point.i,
  860. current_position.j = toolchange_settings.change_point.j,
  861. current_position.k = toolchange_settings.change_point.k,
  862. current_position.u = toolchange_settings.change_point.u,
  863. current_position.v = toolchange_settings.change_point.v,
  864. current_position.w = toolchange_settings.change_point.w,
  865. );
  866. #endif
  867. planner.buffer_line(current_position, MMM_TO_MMS(TOOLCHANGE_PARK_XY_FEEDRATE), old_tool);
  868. planner.synchronize();
  869. }
  870. #endif
  871. #if HAS_HOTEND_OFFSET
  872. xyz_pos_t diff = hotend_offset[new_tool] - hotend_offset[old_tool];
  873. TERN_(DUAL_X_CARRIAGE, diff.x = 0);
  874. #else
  875. constexpr xyz_pos_t diff{0};
  876. #endif
  877. #if ENABLED(DUAL_X_CARRIAGE)
  878. dualx_tool_change(new_tool, no_move);
  879. #elif ENABLED(PARKING_EXTRUDER) // Dual Parking extruder
  880. parking_extruder_tool_change(new_tool, no_move);
  881. #elif ENABLED(MAGNETIC_PARKING_EXTRUDER) // Magnetic Parking extruder
  882. magnetic_parking_extruder_tool_change(new_tool);
  883. #elif ENABLED(SWITCHING_TOOLHEAD) // Switching Toolhead
  884. switching_toolhead_tool_change(new_tool, no_move);
  885. #elif ENABLED(MAGNETIC_SWITCHING_TOOLHEAD) // Magnetic Switching Toolhead
  886. magnetic_switching_toolhead_tool_change(new_tool, no_move);
  887. #elif ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD) // Magnetic Switching ToolChanger
  888. em_switching_toolhead_tool_change(new_tool, no_move);
  889. #elif ENABLED(SWITCHING_NOZZLE) && !SWITCHING_NOZZLE_TWO_SERVOS // Switching Nozzle (single servo)
  890. // Raise by a configured distance to avoid workpiece, except with
  891. // SWITCHING_NOZZLE_TWO_SERVOS, as both nozzles will lift instead.
  892. if (!no_move) {
  893. const float newz = current_position.z + _MAX(-diff.z, 0.0);
  894. // Check if Z has space to compensate at least z_offset, and if not, just abort now
  895. const float maxz = _MIN(TERN(HAS_SOFTWARE_ENDSTOPS, soft_endstop.max.z, Z_MAX_POS), Z_MAX_POS);
  896. if (newz > maxz) return;
  897. current_position.z = _MIN(newz + toolchange_settings.z_raise, maxz);
  898. fast_line_to_current(Z_AXIS);
  899. }
  900. move_nozzle_servo(new_tool);
  901. #endif
  902. IF_DISABLED(DUAL_X_CARRIAGE, active_extruder = new_tool); // Set the new active extruder
  903. TERN_(TOOL_SENSOR, tool_sensor_disabled = false);
  904. (void)check_tool_sensor_stats(active_extruder, true);
  905. // The newly-selected extruder XYZ is actually at...
  906. DEBUG_ECHOLNPGM("Offset Tool XYZ by { ", diff.x, ", ", diff.y, ", ", diff.z, " }");
  907. current_position += diff;
  908. // Tell the planner the new "current position"
  909. sync_plan_position();
  910. #if ENABLED(DELTA)
  911. //LOOP_NUM_AXES(i) update_software_endstops(i); // or modify the constrain function
  912. const bool safe_to_move = current_position.z < delta_clip_start_height - 1;
  913. #else
  914. constexpr bool safe_to_move = true;
  915. #endif
  916. // Return to position and lower again
  917. const bool should_move = safe_to_move && !no_move && IsRunning();
  918. if (should_move) {
  919. #if EITHER(SINGLENOZZLE_STANDBY_TEMP, SINGLENOZZLE_STANDBY_FAN)
  920. thermalManager.singlenozzle_change(old_tool, new_tool);
  921. #endif
  922. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  923. if (should_swap && !too_cold) {
  924. float fr = toolchange_settings.unretract_speed;
  925. #if ENABLED(TOOLCHANGE_FS_INIT_BEFORE_SWAP)
  926. if (!toolchange_extruder_ready[new_tool]) {
  927. toolchange_extruder_ready[new_tool] = true;
  928. fr = toolchange_settings.prime_speed; // Next move is a prime
  929. unscaled_e_move(0, MMM_TO_MMS(fr)); // Init planner with 0 length move
  930. }
  931. #endif
  932. // Unretract (or Prime)
  933. unscaled_e_move(toolchange_settings.swap_length, MMM_TO_MMS(fr));
  934. // Extra Prime
  935. unscaled_e_move(toolchange_settings.extra_prime, MMM_TO_MMS(toolchange_settings.prime_speed));
  936. // Cutting retraction
  937. #if TOOLCHANGE_FS_WIPE_RETRACT
  938. unscaled_e_move(-(TOOLCHANGE_FS_WIPE_RETRACT), MMM_TO_MMS(toolchange_settings.retract_speed));
  939. #endif
  940. // Cool down with fan
  941. #if HAS_FAN && TOOLCHANGE_FS_FAN >= 0
  942. thermalManager.fan_speed[TOOLCHANGE_FS_FAN] = toolchange_settings.fan_speed;
  943. gcode.dwell(SEC_TO_MS(toolchange_settings.fan_time));
  944. thermalManager.fan_speed[TOOLCHANGE_FS_FAN] = 0;
  945. #endif
  946. }
  947. #endif
  948. // Prevent a move outside physical bounds
  949. #if ENABLED(MAGNETIC_SWITCHING_TOOLHEAD)
  950. // If the original position is within tool store area, go to X origin at once
  951. if (destination.y < SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_CLEAR) {
  952. current_position.x = X_MIN_POS;
  953. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[X_AXIS], new_tool);
  954. planner.synchronize();
  955. }
  956. #else
  957. apply_motion_limits(destination);
  958. #endif
  959. // Should the nozzle move back to the old position?
  960. if (can_move_away) {
  961. #if ENABLED(TOOLCHANGE_NO_RETURN)
  962. // Just move back down
  963. DEBUG_ECHOLNPGM("Move back Z only");
  964. if (TERN1(TOOLCHANGE_PARK, toolchange_settings.enable_park))
  965. do_blocking_move_to_z(destination.z, planner.settings.max_feedrate_mm_s[Z_AXIS]);
  966. #else
  967. // Move back to the original (or adjusted) position
  968. DEBUG_POS("Move back", destination);
  969. #if ENABLED(TOOLCHANGE_PARK)
  970. if (toolchange_settings.enable_park) do_blocking_move_to_xy_z(destination, destination.z, MMM_TO_MMS(TOOLCHANGE_PARK_XY_FEEDRATE));
  971. #else
  972. do_blocking_move_to_xy(destination, planner.settings.max_feedrate_mm_s[X_AXIS]);
  973. do_blocking_move_to_z(destination.z, planner.settings.max_feedrate_mm_s[Z_AXIS]);
  974. #endif
  975. #endif
  976. }
  977. else DEBUG_ECHOLNPGM("Move back skipped");
  978. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  979. if (should_swap && !too_cold) {
  980. // Cutting recover
  981. unscaled_e_move(toolchange_settings.extra_resume + TOOLCHANGE_FS_WIPE_RETRACT, MMM_TO_MMS(toolchange_settings.unretract_speed));
  982. current_position.e = 0;
  983. sync_plan_position_e(); // New extruder primed and set to 0
  984. // Restart Fan
  985. #if HAS_FAN && TOOLCHANGE_FS_FAN >= 0
  986. RESTORE(fan);
  987. #endif
  988. }
  989. #endif
  990. TERN_(DUAL_X_CARRIAGE, idex_set_parked(false));
  991. }
  992. #if ENABLED(SWITCHING_NOZZLE)
  993. // Move back down. (Including when the new tool is higher.)
  994. if (!should_move)
  995. do_blocking_move_to_z(destination.z, planner.settings.max_feedrate_mm_s[Z_AXIS]);
  996. #endif
  997. TERN_(SWITCHING_NOZZLE_TWO_SERVOS, lower_nozzle(new_tool));
  998. } // (new_tool != old_tool)
  999. planner.synchronize();
  1000. #if ENABLED(EXT_SOLENOID) && DISABLED(PARKING_EXTRUDER)
  1001. disable_all_solenoids();
  1002. enable_solenoid(active_extruder);
  1003. #endif
  1004. #if HAS_PRUSA_MMU1
  1005. if (new_tool >= E_STEPPERS) return invalid_extruder_error(new_tool);
  1006. select_multiplexed_stepper(new_tool);
  1007. #endif
  1008. #if DO_SWITCH_EXTRUDER
  1009. planner.synchronize();
  1010. move_extruder_servo(active_extruder);
  1011. #endif
  1012. TERN_(HAS_FANMUX, fanmux_switch(active_extruder));
  1013. if (ENABLED(EVENT_GCODE_TOOLCHANGE_ALWAYS_RUN) || !no_move) {
  1014. #ifdef EVENT_GCODE_TOOLCHANGE_T0
  1015. if (new_tool == 0)
  1016. gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T0));
  1017. #endif
  1018. #ifdef EVENT_GCODE_TOOLCHANGE_T1
  1019. if (new_tool == 1)
  1020. gcode.process_subcommands_now(F(EVENT_GCODE_TOOLCHANGE_T1));
  1021. #endif
  1022. #ifdef EVENT_GCODE_AFTER_TOOLCHANGE
  1023. if (TERN1(DUAL_X_CARRIAGE, dual_x_carriage_mode == DXC_AUTO_PARK_MODE))
  1024. gcode.process_subcommands_now(F(EVENT_GCODE_AFTER_TOOLCHANGE));
  1025. #endif
  1026. }
  1027. SERIAL_ECHO_MSG(STR_ACTIVE_EXTRUDER, active_extruder);
  1028. #endif // HAS_MULTI_EXTRUDER
  1029. }
  1030. #if ENABLED(TOOLCHANGE_MIGRATION_FEATURE)
  1031. #define DEBUG_OUT ENABLED(DEBUG_TOOLCHANGE_MIGRATION_FEATURE)
  1032. #include "../core/debug_out.h"
  1033. bool extruder_migration() {
  1034. #if ENABLED(PREVENT_COLD_EXTRUSION)
  1035. if (thermalManager.targetTooColdToExtrude(active_extruder)) {
  1036. DEBUG_ECHOLNPGM("Migration Source Too Cold");
  1037. return false;
  1038. }
  1039. #endif
  1040. // No auto-migration or specified target?
  1041. if (!migration.target && active_extruder >= migration.last) {
  1042. DEBUG_ECHO_MSG("No Migration Target");
  1043. DEBUG_ECHO_MSG("Target: ", migration.target, " Last: ", migration.last, " Active: ", active_extruder);
  1044. migration.automode = false;
  1045. return false;
  1046. }
  1047. // Migrate to a target or the next extruder
  1048. uint8_t migration_extruder = active_extruder;
  1049. if (migration.target) {
  1050. DEBUG_ECHOLNPGM("Migration using fixed target");
  1051. // Specified target ok?
  1052. const int16_t t = migration.target - 1;
  1053. if (t != active_extruder) migration_extruder = t;
  1054. }
  1055. else if (migration.automode && migration_extruder < migration.last && migration_extruder < EXTRUDERS - 1)
  1056. migration_extruder++;
  1057. if (migration_extruder == active_extruder) {
  1058. DEBUG_ECHOLNPGM("Migration source matches active");
  1059. return false;
  1060. }
  1061. // Migration begins
  1062. DEBUG_ECHOLNPGM("Beginning migration");
  1063. migration.in_progress = true; // Prevent runout script
  1064. planner.synchronize();
  1065. // Remember position before migration
  1066. const float resume_current_e = current_position.e;
  1067. // Migrate the flow
  1068. planner.set_flow(migration_extruder, planner.flow_percentage[active_extruder]);
  1069. // Migrate the retracted state
  1070. #if ENABLED(FWRETRACT)
  1071. fwretract.retracted[migration_extruder] = fwretract.retracted[active_extruder];
  1072. #endif
  1073. // Migrate the temperature to the new hotend
  1074. #if HAS_MULTI_HOTEND
  1075. thermalManager.setTargetHotend(thermalManager.degTargetHotend(active_extruder), migration_extruder);
  1076. TERN_(AUTOTEMP, planner.autotemp_update());
  1077. thermalManager.set_heating_message(0);
  1078. thermalManager.wait_for_hotend(active_extruder);
  1079. #endif
  1080. // Migrate Linear Advance K factor to the new extruder
  1081. TERN_(LIN_ADVANCE, planner.extruder_advance_K[active_extruder] = planner.extruder_advance_K[migration_extruder]);
  1082. // Perform the tool change
  1083. tool_change(migration_extruder);
  1084. // Retract if previously retracted
  1085. #if ENABLED(FWRETRACT)
  1086. if (fwretract.retracted[active_extruder])
  1087. unscaled_e_move(-fwretract.settings.retract_length, fwretract.settings.retract_feedrate_mm_s);
  1088. #endif
  1089. // If no available extruder
  1090. if (EXTRUDERS < 2 || active_extruder >= EXTRUDERS - 2 || active_extruder == migration.last)
  1091. migration.automode = false;
  1092. migration.in_progress = false;
  1093. current_position.e = resume_current_e;
  1094. planner.synchronize();
  1095. planner.set_e_position_mm(current_position.e); // New extruder primed and ready
  1096. DEBUG_ECHOLNPGM("Migration Complete");
  1097. return true;
  1098. }
  1099. #endif // TOOLCHANGE_MIGRATION_FEATURE