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

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