My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

tool_change.cpp 43KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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 ENABLED(SINGLENOZZLE_STANDBY_TEMP)
  43. uint16_t singlenozzle_temp[EXTRUDERS];
  44. #endif
  45. #if BOTH(HAS_FAN, SINGLENOZZLE_STANDBY_FAN)
  46. uint8_t singlenozzle_fan_speed[EXTRUDERS];
  47. #endif
  48. #if ENABLED(MAGNETIC_PARKING_EXTRUDER) || defined(EVENT_GCODE_AFTER_TOOLCHANGE) || (ENABLED(PARKING_EXTRUDER) && PARKING_EXTRUDER_SOLENOIDS_DELAY > 0)
  49. #include "../gcode/gcode.h"
  50. #endif
  51. #if ANY(SWITCHING_EXTRUDER, SWITCHING_NOZZLE, SWITCHING_TOOLHEAD)
  52. #include "servo.h"
  53. #endif
  54. #if ENABLED(EXT_SOLENOID) && DISABLED(PARKING_EXTRUDER)
  55. #include "../feature/solenoid.h"
  56. #endif
  57. #if ENABLED(MK2_MULTIPLEXER)
  58. #include "../feature/snmm.h"
  59. #endif
  60. #if ENABLED(MIXING_EXTRUDER)
  61. #include "../feature/mixing.h"
  62. #endif
  63. #if HAS_LEVELING
  64. #include "../feature/bedlevel/bedlevel.h"
  65. #endif
  66. #if HAS_FANMUX
  67. #include "../feature/fanmux.h"
  68. #endif
  69. #if ENABLED(PRUSA_MMU2)
  70. #include "../feature/mmu2/mmu2.h"
  71. #endif
  72. #if HAS_LCD_MENU
  73. #include "../lcd/ultralcd.h"
  74. #endif
  75. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  76. #include "../feature/pause.h"
  77. #endif
  78. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  79. #include "../gcode/gcode.h"
  80. #if TOOLCHANGE_FS_WIPE_RETRACT <= 0
  81. #undef TOOLCHANGE_FS_WIPE_RETRACT
  82. #define TOOLCHANGE_FS_WIPE_RETRACT 0
  83. #endif
  84. #endif
  85. #if DO_SWITCH_EXTRUDER
  86. #if EXTRUDERS > 3
  87. #define _SERVO_NR(E) ((E) < 2 ? SWITCHING_EXTRUDER_SERVO_NR : SWITCHING_EXTRUDER_E23_SERVO_NR)
  88. #else
  89. #define _SERVO_NR(E) SWITCHING_EXTRUDER_SERVO_NR
  90. #endif
  91. void move_extruder_servo(const uint8_t e) {
  92. planner.synchronize();
  93. #if EXTRUDERS & 1
  94. if (e < EXTRUDERS - 1)
  95. #endif
  96. {
  97. MOVE_SERVO(_SERVO_NR(e), servo_angles[_SERVO_NR(e)][e]);
  98. safe_delay(500);
  99. }
  100. }
  101. #endif // DO_SWITCH_EXTRUDER
  102. #if ENABLED(SWITCHING_NOZZLE)
  103. #if SWITCHING_NOZZLE_TWO_SERVOS
  104. inline void _move_nozzle_servo(const uint8_t e, const uint8_t angle_index) {
  105. constexpr int8_t sns_index[2] = { SWITCHING_NOZZLE_SERVO_NR, SWITCHING_NOZZLE_E1_SERVO_NR };
  106. constexpr int16_t sns_angles[2] = SWITCHING_NOZZLE_SERVO_ANGLES;
  107. planner.synchronize();
  108. MOVE_SERVO(sns_index[e], sns_angles[angle_index]);
  109. safe_delay(500);
  110. }
  111. void lower_nozzle(const uint8_t e) { _move_nozzle_servo(e, 0); }
  112. void raise_nozzle(const uint8_t e) { _move_nozzle_servo(e, 1); }
  113. #else
  114. void move_nozzle_servo(const uint8_t angle_index) {
  115. planner.synchronize();
  116. MOVE_SERVO(SWITCHING_NOZZLE_SERVO_NR, servo_angles[SWITCHING_NOZZLE_SERVO_NR][angle_index]);
  117. safe_delay(500);
  118. }
  119. #endif
  120. #endif // SWITCHING_NOZZLE
  121. inline 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. inline void slow_line_to_current(const AxisEnum fr_axis) { _line_to_current(fr_axis, 0.5f); }
  125. inline void fast_line_to_current(const AxisEnum fr_axis) { _line_to_current(fr_axis); }
  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_ECHOPAIR("(1) Move extruder ", int(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_ECHOPAIR("(2) Couple extruder ", int(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_ECHOPAIR("(3) Move extruder ", int(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_ECHOPAIR("(4) Move extruder ", int(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_ECHOPAIR("(5) Park extruder ", int(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_ECHOPAIR("(6) Move extruder ", int(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)
  192. #if ENABLED(PARKING_EXTRUDER_SOLENOIDS_INVERT)
  193. pe_activate_solenoid(n);
  194. #else
  195. pe_deactivate_solenoid(n);
  196. #endif
  197. }
  198. void pe_set_solenoid(const uint8_t extruder_num, const uint8_t state) {
  199. switch (extruder_num) {
  200. case 1: OUT_WRITE(SOL1_PIN, state); break;
  201. default: OUT_WRITE(SOL0_PIN, state); break;
  202. }
  203. #if PARKING_EXTRUDER_SOLENOIDS_DELAY > 0
  204. gcode.dwell(PARKING_EXTRUDER_SOLENOIDS_DELAY);
  205. #endif
  206. }
  207. inline void parking_extruder_tool_change(const uint8_t new_tool, bool no_move) {
  208. if (!no_move) {
  209. constexpr float parkingposx[] = PARKING_EXTRUDER_PARKING_X;
  210. #if HAS_HOTEND_OFFSET
  211. const float x_offset = hotend_offset[active_extruder].x;
  212. #else
  213. constexpr float x_offset = 0;
  214. #endif
  215. const float midpos = (parkingposx[0] + parkingposx[1]) * 0.5f + x_offset,
  216. grabpos = parkingposx[new_tool] + (new_tool ? PARKING_EXTRUDER_GRAB_DISTANCE : -(PARKING_EXTRUDER_GRAB_DISTANCE)) + x_offset;
  217. /**
  218. * 1. Move to park position of old extruder
  219. * 2. Disengage magnetic field, wait for delay
  220. * 3. Move near new extruder
  221. * 4. Engage magnetic field for new extruder
  222. * 5. Move to parking incl. offset of new extruder
  223. * 6. Lower Z-Axis
  224. */
  225. // STEP 1
  226. DEBUG_POS("Start PE Tool-Change", current_position);
  227. current_position.x = parkingposx[active_extruder] + x_offset;
  228. DEBUG_ECHOLNPAIR("(1) Park extruder ", int(active_extruder));
  229. DEBUG_POS("Moving ParkPos", current_position);
  230. fast_line_to_current(X_AXIS);
  231. // STEP 2
  232. planner.synchronize();
  233. DEBUG_ECHOLNPGM("(2) Disengage magnet");
  234. pe_deactivate_solenoid(active_extruder);
  235. // STEP 3
  236. current_position.x += active_extruder ? -10 : 10; // move 10mm away from parked extruder
  237. DEBUG_ECHOLNPGM("(3) Move near new extruder");
  238. DEBUG_POS("Move away from parked extruder", current_position);
  239. fast_line_to_current(X_AXIS);
  240. // STEP 4
  241. planner.synchronize();
  242. DEBUG_ECHOLNPGM("(4) Engage magnetic field");
  243. // Just save power for inverted magnets
  244. TERN_(PARKING_EXTRUDER_SOLENOIDS_INVERT, pe_activate_solenoid(active_extruder));
  245. pe_activate_solenoid(new_tool);
  246. // STEP 5
  247. current_position.x = grabpos + (new_tool ? -10 : 10);
  248. fast_line_to_current(X_AXIS);
  249. current_position.x = grabpos;
  250. DEBUG_SYNCHRONIZE();
  251. DEBUG_POS("(5) Unpark extruder", current_position);
  252. slow_line_to_current(X_AXIS);
  253. // STEP 6
  254. current_position.x = midpos - TERN0(HAS_HOTEND_OFFSET, hotend_offset[new_tool].x);
  255. DEBUG_SYNCHRONIZE();
  256. DEBUG_POS("(6) Move midway between hotends", current_position);
  257. fast_line_to_current(X_AXIS);
  258. planner.synchronize(); // Always sync the final move
  259. DEBUG_POS("PE Tool-Change done.", current_position);
  260. }
  261. else { // nomove == true
  262. // Only engage magnetic field for new extruder
  263. pe_activate_solenoid(new_tool);
  264. // Just save power for inverted magnets
  265. TERN_(PARKING_EXTRUDER_SOLENOIDS_INVERT, pe_activate_solenoid(active_extruder));
  266. }
  267. }
  268. #endif // PARKING_EXTRUDER
  269. #if ENABLED(SWITCHING_TOOLHEAD)
  270. inline void swt_lock(const bool locked=true) {
  271. const uint16_t swt_angles[2] = SWITCHING_TOOLHEAD_SERVO_ANGLES;
  272. MOVE_SERVO(SWITCHING_TOOLHEAD_SERVO_NR, swt_angles[locked ? 0 : 1]);
  273. }
  274. void swt_init() { swt_lock(); }
  275. inline void switching_toolhead_tool_change(const uint8_t new_tool, bool no_move/*=false*/) {
  276. if (no_move) return;
  277. constexpr float toolheadposx[] = SWITCHING_TOOLHEAD_X_POS;
  278. const float placexpos = toolheadposx[active_extruder],
  279. grabxpos = toolheadposx[new_tool];
  280. /**
  281. * 1. Move to switch position of current toolhead
  282. * 2. Unlock tool and drop it in the dock
  283. * 3. Move to the new toolhead
  284. * 4. Grab and lock the new toolhead
  285. */
  286. // 1. Move to switch position of current toolhead
  287. DEBUG_POS("Start ST Tool-Change", current_position);
  288. current_position.x = placexpos;
  289. DEBUG_ECHOLNPAIR("(1) Place old tool ", int(active_extruder));
  290. DEBUG_POS("Move X SwitchPos", current_position);
  291. fast_line_to_current(X_AXIS);
  292. current_position.y = SWITCHING_TOOLHEAD_Y_POS - (SWITCHING_TOOLHEAD_Y_SECURITY);
  293. DEBUG_SYNCHRONIZE();
  294. DEBUG_POS("Move Y SwitchPos + Security", current_position);
  295. fast_line_to_current(Y_AXIS);
  296. // 2. Unlock tool and drop it in the dock
  297. planner.synchronize();
  298. DEBUG_ECHOLNPGM("(2) Unlock and Place Toolhead");
  299. swt_lock(false);
  300. safe_delay(500);
  301. current_position.y = SWITCHING_TOOLHEAD_Y_POS;
  302. DEBUG_POS("Move Y SwitchPos", current_position);
  303. slow_line_to_current(Y_AXIS);
  304. // Wait for move to complete, then another 0.2s
  305. planner.synchronize();
  306. safe_delay(200);
  307. current_position.y -= SWITCHING_TOOLHEAD_Y_CLEAR;
  308. DEBUG_POS("Move back Y clear", current_position);
  309. fast_line_to_current(Y_AXIS); // move away from docked toolhead
  310. // 3. Move to the new toolhead
  311. current_position.x = grabxpos;
  312. DEBUG_SYNCHRONIZE();
  313. DEBUG_ECHOLNPGM("(3) Move to new toolhead position");
  314. DEBUG_POS("Move to new toolhead X", current_position);
  315. fast_line_to_current(X_AXIS);
  316. current_position.y = SWITCHING_TOOLHEAD_Y_POS - (SWITCHING_TOOLHEAD_Y_SECURITY);
  317. DEBUG_SYNCHRONIZE();
  318. DEBUG_POS("Move Y SwitchPos + Security", current_position);
  319. fast_line_to_current(Y_AXIS);
  320. // 4. Grab and lock the new toolhead
  321. current_position.y = SWITCHING_TOOLHEAD_Y_POS;
  322. DEBUG_SYNCHRONIZE();
  323. DEBUG_ECHOLNPGM("(4) Grab and lock new toolhead");
  324. DEBUG_POS("Move Y SwitchPos", current_position);
  325. slow_line_to_current(Y_AXIS);
  326. // Wait for move to finish, pause 0.2s, move servo, pause 0.5s
  327. planner.synchronize();
  328. safe_delay(200);
  329. swt_lock();
  330. safe_delay(500);
  331. current_position.y -= SWITCHING_TOOLHEAD_Y_CLEAR;
  332. DEBUG_POS("Move back Y clear", current_position);
  333. fast_line_to_current(Y_AXIS); // Move away from docked toolhead
  334. planner.synchronize(); // Always sync the final move
  335. DEBUG_POS("ST Tool-Change done.", current_position);
  336. }
  337. #elif ENABLED(MAGNETIC_SWITCHING_TOOLHEAD)
  338. inline void magnetic_switching_toolhead_tool_change(const uint8_t new_tool, bool no_move/*=false*/) {
  339. if (no_move) return;
  340. constexpr float toolheadposx[] = SWITCHING_TOOLHEAD_X_POS,
  341. toolheadclearx[] = SWITCHING_TOOLHEAD_X_SECURITY;
  342. const float placexpos = toolheadposx[active_extruder],
  343. placexclear = toolheadclearx[active_extruder],
  344. grabxpos = toolheadposx[new_tool],
  345. grabxclear = toolheadclearx[new_tool];
  346. /**
  347. * 1. Move to switch position of current toolhead
  348. * 2. Release and place toolhead in the dock
  349. * 3. Move to the new toolhead
  350. * 4. Grab the new toolhead and move to security position
  351. */
  352. DEBUG_POS("Start MST Tool-Change", current_position);
  353. // 1. Move to switch position current toolhead
  354. current_position.y = SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_CLEAR;
  355. SERIAL_ECHOLNPAIR("(1) Place old tool ", int(active_extruder));
  356. DEBUG_POS("Move Y SwitchPos + Security", current_position);
  357. fast_line_to_current(Y_AXIS);
  358. current_position.x = placexclear;
  359. DEBUG_SYNCHRONIZE();
  360. DEBUG_POS("Move X SwitchPos + Security", current_position);
  361. fast_line_to_current(X_AXIS);
  362. current_position.y = SWITCHING_TOOLHEAD_Y_POS;
  363. DEBUG_SYNCHRONIZE();
  364. DEBUG_POS("Move Y SwitchPos", current_position);
  365. fast_line_to_current(Y_AXIS);
  366. current_position.x = placexpos;
  367. DEBUG_SYNCHRONIZE();
  368. DEBUG_POS("Move X SwitchPos", current_position);
  369. line_to_current_position(planner.settings.max_feedrate_mm_s[X_AXIS] * 0.25f);
  370. // 2. Release and place toolhead in the dock
  371. DEBUG_SYNCHRONIZE();
  372. DEBUG_ECHOLNPGM("(2) Release and Place Toolhead");
  373. current_position.y = SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_RELEASE;
  374. DEBUG_POS("Move Y SwitchPos + Release", current_position);
  375. line_to_current_position(planner.settings.max_feedrate_mm_s[Y_AXIS] * 0.1f);
  376. current_position.y = SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_SECURITY;
  377. DEBUG_SYNCHRONIZE();
  378. DEBUG_POS("Move Y SwitchPos + Security", current_position);
  379. line_to_current_position(planner.settings.max_feedrate_mm_s[Y_AXIS]);
  380. // 3. Move to new toolhead position
  381. DEBUG_SYNCHRONIZE();
  382. DEBUG_ECHOLNPGM("(3) Move to new toolhead position");
  383. current_position.x = grabxpos;
  384. DEBUG_POS("Move to new toolhead X", current_position);
  385. fast_line_to_current(X_AXIS);
  386. // 4. Grab the new toolhead and move to security position
  387. DEBUG_SYNCHRONIZE();
  388. DEBUG_ECHOLNPGM("(4) Grab new toolhead, move to security position");
  389. current_position.y = SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_RELEASE;
  390. DEBUG_POS("Move Y SwitchPos + Release", current_position);
  391. line_to_current_position(planner.settings.max_feedrate_mm_s[Y_AXIS]);
  392. current_position.y = SWITCHING_TOOLHEAD_Y_POS;
  393. DEBUG_SYNCHRONIZE();
  394. DEBUG_POS("Move Y SwitchPos", current_position);
  395. _line_to_current(Y_AXIS, 0.2f);
  396. #if ENABLED(PRIME_BEFORE_REMOVE) && (SWITCHING_TOOLHEAD_PRIME_MM || SWITCHING_TOOLHEAD_RETRACT_MM)
  397. #if SWITCHING_TOOLHEAD_PRIME_MM
  398. current_position.e += SWITCHING_TOOLHEAD_PRIME_MM;
  399. planner.buffer_line(current_position, MMM_TO_MMS(SWITCHING_TOOLHEAD_PRIME_FEEDRATE), new_tool);
  400. #endif
  401. #if SWITCHING_TOOLHEAD_RETRACT_MM
  402. current_position.e -= SWITCHING_TOOLHEAD_RETRACT_MM;
  403. planner.buffer_line(current_position, MMM_TO_MMS(SWITCHING_TOOLHEAD_RETRACT_FEEDRATE), new_tool);
  404. #endif
  405. #else
  406. planner.synchronize();
  407. safe_delay(100); // Give switch time to settle
  408. #endif
  409. current_position.x = grabxclear;
  410. DEBUG_POS("Move to new toolhead X + Security", current_position);
  411. _line_to_current(X_AXIS, 0.1f);
  412. planner.synchronize();
  413. safe_delay(100); // Give switch time to settle
  414. current_position.y += SWITCHING_TOOLHEAD_Y_CLEAR;
  415. DEBUG_POS("Move back Y clear", current_position);
  416. fast_line_to_current(Y_AXIS); // move away from docked toolhead
  417. planner.synchronize(); // Always sync last tool-change move
  418. DEBUG_POS("MST Tool-Change done.", current_position);
  419. }
  420. #elif ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  421. inline void est_activate_solenoid() { OUT_WRITE(SOL0_PIN, HIGH); }
  422. inline void est_deactivate_solenoid() { OUT_WRITE(SOL0_PIN, LOW); }
  423. void est_init() { est_activate_solenoid(); }
  424. inline void em_switching_toolhead_tool_change(const uint8_t new_tool, bool no_move) {
  425. if (no_move) return;
  426. constexpr float toolheadposx[] = SWITCHING_TOOLHEAD_X_POS;
  427. const float placexpos = toolheadposx[active_extruder],
  428. grabxpos = toolheadposx[new_tool];
  429. const xyz_pos_t &hoffs = hotend_offset[active_extruder];
  430. /**
  431. * 1. Raise Z-Axis to give enough clearance
  432. * 2. Move to position near active extruder parking
  433. * 3. Move gently to park position of active extruder
  434. * 4. Disengage magnetic field, wait for delay
  435. * 5. Leave extruder and move to position near new extruder parking
  436. * 6. Move gently to park position of new extruder
  437. * 7. Engage magnetic field for new extruder parking
  438. * 8. Unpark extruder
  439. * 9. Apply Z hotend offset to current position
  440. */
  441. DEBUG_POS("Start EMST Tool-Change", current_position);
  442. // 1. Raise Z-Axis to give enough clearance
  443. current_position.z += SWITCHING_TOOLHEAD_Z_HOP;
  444. DEBUG_POS("(1) Raise Z-Axis ", current_position);
  445. fast_line_to_current(Z_AXIS);
  446. // 2. Move to position near active extruder parking
  447. DEBUG_SYNCHRONIZE();
  448. DEBUG_ECHOLNPAIR("(2) Move near active extruder parking", active_extruder);
  449. DEBUG_POS("Moving ParkPos", current_position);
  450. current_position.set(hoffs.x + placexpos,
  451. hoffs.y + SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_CLEAR);
  452. fast_line_to_current(X_AXIS);
  453. // 3. Move gently to park position of active extruder
  454. DEBUG_SYNCHRONIZE();
  455. SERIAL_ECHOLNPAIR("(3) Move gently to park position of active extruder", active_extruder);
  456. DEBUG_POS("Moving ParkPos", current_position);
  457. current_position.y -= SWITCHING_TOOLHEAD_Y_CLEAR;
  458. slow_line_to_current(Y_AXIS);
  459. // 4. Disengage magnetic field, wait for delay
  460. planner.synchronize();
  461. DEBUG_ECHOLNPGM("(4) Disengage magnet");
  462. est_deactivate_solenoid();
  463. // 5. Leave extruder and move to position near new extruder parking
  464. DEBUG_ECHOLNPGM("(5) Move near new extruder parking");
  465. DEBUG_POS("Moving ParkPos", current_position);
  466. current_position.y += SWITCHING_TOOLHEAD_Y_CLEAR;
  467. slow_line_to_current(Y_AXIS);
  468. current_position.set(hoffs.x + grabxpos,
  469. hoffs.y + SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_CLEAR);
  470. fast_line_to_current(X_AXIS);
  471. // 6. Move gently to park position of new extruder
  472. current_position.y -= SWITCHING_TOOLHEAD_Y_CLEAR;
  473. if (DEBUGGING(LEVELING)) {
  474. planner.synchronize();
  475. DEBUG_ECHOLNPGM("(6) Move near new extruder");
  476. }
  477. slow_line_to_current(Y_AXIS);
  478. // 7. Engage magnetic field for new extruder parking
  479. DEBUG_SYNCHRONIZE();
  480. DEBUG_ECHOLNPGM("(7) Engage magnetic field");
  481. est_activate_solenoid();
  482. // 8. Unpark extruder
  483. current_position.y += SWITCHING_TOOLHEAD_Y_CLEAR;
  484. DEBUG_ECHOLNPGM("(8) Unpark extruder");
  485. slow_line_to_current(X_AXIS);
  486. planner.synchronize(); // Always sync the final move
  487. // 9. Apply Z hotend offset to current position
  488. DEBUG_POS("(9) Applying Z-offset", current_position);
  489. current_position.z += hoffs.z - hotend_offset[new_tool].z;
  490. DEBUG_POS("EMST Tool-Change done.", current_position);
  491. }
  492. #endif // ELECTROMAGNETIC_SWITCHING_TOOLHEAD
  493. #if EXTRUDERS
  494. inline void invalid_extruder_error(const uint8_t e) {
  495. SERIAL_ECHO_START();
  496. SERIAL_CHAR('T'); SERIAL_ECHO(int(e));
  497. SERIAL_CHAR(' '); SERIAL_ECHOLNPGM(STR_INVALID_EXTRUDER);
  498. }
  499. #endif
  500. #if ENABLED(DUAL_X_CARRIAGE)
  501. inline void dualx_tool_change(const uint8_t new_tool, bool &no_move) {
  502. DEBUG_ECHOPGM("Dual X Carriage Mode ");
  503. switch (dual_x_carriage_mode) {
  504. case DXC_FULL_CONTROL_MODE: DEBUG_ECHOLNPGM("FULL_CONTROL"); break;
  505. case DXC_AUTO_PARK_MODE: DEBUG_ECHOLNPGM("AUTO_PARK"); break;
  506. case DXC_DUPLICATION_MODE: DEBUG_ECHOLNPGM("DUPLICATION"); break;
  507. case DXC_MIRRORED_MODE: DEBUG_ECHOLNPGM("MIRRORED"); break;
  508. }
  509. const float xhome = x_home_pos(active_extruder);
  510. if (dual_x_carriage_mode == DXC_AUTO_PARK_MODE
  511. && IsRunning() && !no_move
  512. && (delayed_move_time || current_position.x != xhome)
  513. ) {
  514. DEBUG_ECHOLNPAIR("MoveX to ", xhome);
  515. // Park old head
  516. current_position.x = xhome;
  517. line_to_current_position(planner.settings.max_feedrate_mm_s[X_AXIS]);
  518. planner.synchronize();
  519. }
  520. // Activate the new extruder ahead of calling set_axis_is_at_home!
  521. active_extruder = new_tool;
  522. // This function resets the max/min values - the current position may be overwritten below.
  523. set_axis_is_at_home(X_AXIS);
  524. DEBUG_POS("New Extruder", current_position);
  525. switch (dual_x_carriage_mode) {
  526. case DXC_FULL_CONTROL_MODE:
  527. // New current position is the position of the activated extruder
  528. current_position.x = inactive_extruder_x_pos;
  529. // Save the inactive extruder's position (from the old current_position)
  530. inactive_extruder_x_pos = destination.x;
  531. break;
  532. case DXC_AUTO_PARK_MODE:
  533. // record current raised toolhead position for use by unpark
  534. raised_parked_position = current_position;
  535. active_extruder_parked = true;
  536. delayed_move_time = 0;
  537. break;
  538. default:
  539. break;
  540. }
  541. DEBUG_ECHOLNPAIR("Active extruder parked: ", active_extruder_parked ? "yes" : "no");
  542. DEBUG_POS("New extruder (parked)", current_position);
  543. }
  544. #endif // DUAL_X_CARRIAGE
  545. /**
  546. * Prime active tool using TOOLCHANGE_FILAMENT_SWAP settings
  547. */
  548. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  549. void tool_change_prime() {
  550. if (toolchange_settings.extra_prime > 0
  551. && TERN(PREVENT_COLD_EXTRUSION, !thermalManager.targetTooColdToExtrude(active_extruder), 1)
  552. ) {
  553. destination = current_position; // Remember the old position
  554. const bool ok = TERN1(TOOLCHANGE_PARK, all_axes_homed() && toolchange_settings.enable_park);
  555. #if HAS_FAN && TOOLCHANGE_FS_FAN >= 0
  556. // Store and stop fan. Restored on any exit.
  557. REMEMBER(fan, thermalManager.fan_speed[TOOLCHANGE_FS_FAN], 0);
  558. #endif
  559. // Z raise
  560. if (ok) {
  561. // Do a small lift to avoid the workpiece in the move back (below)
  562. current_position.z += toolchange_settings.z_raise;
  563. #if HAS_SOFTWARE_ENDSTOPS
  564. NOMORE(current_position.z, soft_endstop.max.z);
  565. #endif
  566. fast_line_to_current(Z_AXIS);
  567. planner.synchronize();
  568. }
  569. // Park
  570. #if ENABLED(TOOLCHANGE_PARK)
  571. if (ok) {
  572. TERN(TOOLCHANGE_PARK_Y_ONLY,,current_position.x = toolchange_settings.change_point.x);
  573. TERN(TOOLCHANGE_PARK_X_ONLY,,current_position.y = toolchange_settings.change_point.y);
  574. planner.buffer_line(current_position, MMM_TO_MMS(TOOLCHANGE_PARK_XY_FEEDRATE), active_extruder);
  575. planner.synchronize();
  576. }
  577. #endif
  578. // Prime (All distances are added and slowed down to ensure secure priming in all circumstances)
  579. unscaled_e_move(toolchange_settings.swap_length + toolchange_settings.extra_prime, MMM_TO_MMS(toolchange_settings.prime_speed));
  580. // Cutting retraction
  581. #if TOOLCHANGE_FS_WIPE_RETRACT
  582. unscaled_e_move(-(TOOLCHANGE_FS_WIPE_RETRACT), MMM_TO_MMS(toolchange_settings.retract_speed));
  583. #endif
  584. // Cool down with fan
  585. #if HAS_FAN && TOOLCHANGE_FS_FAN >= 0
  586. thermalManager.fan_speed[TOOLCHANGE_FS_FAN] = toolchange_settings.fan_speed;
  587. gcode.dwell(toolchange_settings.fan_time * 1000);
  588. thermalManager.fan_speed[TOOLCHANGE_FS_FAN] = 0;
  589. #endif
  590. // Move back
  591. #if ENABLED(TOOLCHANGE_PARK)
  592. if (ok) {
  593. #if ENABLED(TOOLCHANGE_NO_RETURN)
  594. do_blocking_move_to_z(destination.z, planner.settings.max_feedrate_mm_s[Z_AXIS]);
  595. #else
  596. do_blocking_move_to(destination, MMM_TO_MMS(TOOLCHANGE_PARK_XY_FEEDRATE));
  597. #endif
  598. }
  599. #endif
  600. // Cutting recover
  601. unscaled_e_move(toolchange_settings.extra_resume + TOOLCHANGE_FS_WIPE_RETRACT, MMM_TO_MMS(toolchange_settings.unretract_speed));
  602. planner.synchronize();
  603. current_position.e = destination.e;
  604. sync_plan_position_e(); // Resume at the old E position
  605. }
  606. }
  607. #endif
  608. /**
  609. * Perform a tool-change, which may result in moving the
  610. * previous tool out of the way and the new tool into place.
  611. */
  612. void tool_change(const uint8_t new_tool, bool no_move/*=false*/) {
  613. if (TERN0(MAGNETIC_SWITCHING_TOOLHEAD, new_tool == active_extruder))
  614. return;
  615. #if ENABLED(MIXING_EXTRUDER)
  616. UNUSED(no_move);
  617. if (new_tool >= MIXING_VIRTUAL_TOOLS)
  618. return invalid_extruder_error(new_tool);
  619. #if MIXING_VIRTUAL_TOOLS > 1
  620. // T0-Tnnn: Switch virtual tool by changing the index to the mix
  621. mixer.T(new_tool);
  622. #endif
  623. #elif ENABLED(PRUSA_MMU2)
  624. UNUSED(no_move);
  625. mmu2.tool_change(new_tool);
  626. #elif EXTRUDERS == 0
  627. // Nothing to do
  628. UNUSED(new_tool); UNUSED(no_move);
  629. #elif EXTRUDERS < 2
  630. UNUSED(no_move);
  631. if (new_tool) invalid_extruder_error(new_tool);
  632. return;
  633. #elif HAS_MULTI_EXTRUDER
  634. planner.synchronize();
  635. #if ENABLED(DUAL_X_CARRIAGE) // Only T0 allowed if the Printer is in DXC_DUPLICATION_MODE or DXC_MIRRORED_MODE
  636. if (new_tool != 0 && dxc_is_duplicating())
  637. return invalid_extruder_error(new_tool);
  638. #endif
  639. if (new_tool >= EXTRUDERS)
  640. return invalid_extruder_error(new_tool);
  641. if (!no_move && homing_needed()) {
  642. no_move = true;
  643. DEBUG_ECHOLNPGM("No move (not homed)");
  644. }
  645. TERN_(HAS_LCD_MENU, if (!no_move) ui.return_to_status());
  646. #if ENABLED(DUAL_X_CARRIAGE)
  647. const bool idex_full_control = dual_x_carriage_mode == DXC_FULL_CONTROL_MODE;
  648. #else
  649. constexpr bool idex_full_control = false;
  650. #endif
  651. const uint8_t old_tool = active_extruder;
  652. const bool can_move_away = !no_move && !idex_full_control;
  653. #if HAS_LEVELING
  654. // Set current position to the physical position
  655. TEMPORARY_BED_LEVELING_STATE(false);
  656. #endif
  657. // First tool priming. To prime again, reboot the machine.
  658. #if BOTH(TOOLCHANGE_FILAMENT_SWAP, TOOLCHANGE_FS_PRIME_FIRST_USED)
  659. static bool first_tool_is_primed = false;
  660. if (new_tool == old_tool && !first_tool_is_primed && enable_first_prime) {
  661. tool_change_prime();
  662. first_tool_is_primed = true;
  663. toolchange_extruder_ready[old_tool] = true; // Primed and initialized
  664. }
  665. #endif
  666. if (new_tool != old_tool) {
  667. destination = current_position;
  668. #if BOTH(TOOLCHANGE_FILAMENT_SWAP, HAS_FAN) && TOOLCHANGE_FS_FAN >= 0
  669. // Store and stop fan. Restored on any exit.
  670. REMEMBER(fan, thermalManager.fan_speed[TOOLCHANGE_FS_FAN], 0);
  671. #endif
  672. // Z raise before retraction
  673. #if ENABLED(TOOLCHANGE_ZRAISE_BEFORE_RETRACT) && DISABLED(SWITCHING_NOZZLE)
  674. if (can_move_away && TERN1(TOOLCHANGE_PARK, toolchange_settings.enable_park)) {
  675. // Do a small lift to avoid the workpiece in the move back (below)
  676. current_position.z += toolchange_settings.z_raise;
  677. #if HAS_SOFTWARE_ENDSTOPS
  678. NOMORE(current_position.z, soft_endstop.max.z);
  679. #endif
  680. fast_line_to_current(Z_AXIS);
  681. planner.synchronize();
  682. }
  683. #endif
  684. // Unload / Retract
  685. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  686. const bool should_swap = can_move_away && toolchange_settings.swap_length,
  687. too_cold = TERN0(PREVENT_COLD_EXTRUSION,
  688. !DEBUGGING(DRYRUN) && (thermalManager.targetTooColdToExtrude(old_tool) || thermalManager.targetTooColdToExtrude(new_tool))
  689. );
  690. if (should_swap) {
  691. if (too_cold) {
  692. SERIAL_ECHO_MSG(STR_ERR_HOTEND_TOO_COLD);
  693. if (ENABLED(SINGLENOZZLE)) { active_extruder = new_tool; return; }
  694. }
  695. else {
  696. #if ENABLED(TOOLCHANGE_FS_PRIME_FIRST_USED)
  697. // For first new tool, change without unloading the old. 'Just prime/init the new'
  698. if (first_tool_is_primed)
  699. unscaled_e_move(-toolchange_settings.swap_length, MMM_TO_MMS(toolchange_settings.retract_speed));
  700. first_tool_is_primed = true; // The first new tool will be primed by toolchanging
  701. #endif
  702. }
  703. }
  704. #endif
  705. TERN_(SWITCHING_NOZZLE_TWO_SERVOS, raise_nozzle(old_tool));
  706. REMEMBER(fr, feedrate_mm_s, XY_PROBE_FEEDRATE_MM_S);
  707. #if HAS_SOFTWARE_ENDSTOPS
  708. #if HAS_HOTEND_OFFSET
  709. #define _EXT_ARGS , old_tool, new_tool
  710. #else
  711. #define _EXT_ARGS
  712. #endif
  713. update_software_endstops(X_AXIS _EXT_ARGS);
  714. #if DISABLED(DUAL_X_CARRIAGE)
  715. update_software_endstops(Y_AXIS _EXT_ARGS);
  716. update_software_endstops(Z_AXIS _EXT_ARGS);
  717. #endif
  718. #endif
  719. #if DISABLED(TOOLCHANGE_ZRAISE_BEFORE_RETRACT) && DISABLED(SWITCHING_NOZZLE)
  720. if (can_move_away && TERN1(TOOLCHANGE_PARK, toolchange_settings.enable_park)) {
  721. // Do a small lift to avoid the workpiece in the move back (below)
  722. current_position.z += toolchange_settings.z_raise;
  723. #if HAS_SOFTWARE_ENDSTOPS
  724. NOMORE(current_position.z, soft_endstop.max.z);
  725. #endif
  726. fast_line_to_current(Z_AXIS);
  727. }
  728. #endif
  729. // Toolchange park
  730. #if ENABLED(TOOLCHANGE_PARK) && DISABLED(SWITCHING_NOZZLE)
  731. if (can_move_away && toolchange_settings.enable_park) {
  732. TERN(TOOLCHANGE_PARK_Y_ONLY,,current_position.x = toolchange_settings.change_point.x);
  733. TERN(TOOLCHANGE_PARK_X_ONLY,,current_position.y = toolchange_settings.change_point.y);
  734. planner.buffer_line(current_position, MMM_TO_MMS(TOOLCHANGE_PARK_XY_FEEDRATE), old_tool);
  735. planner.synchronize();
  736. }
  737. #endif
  738. #if HAS_HOTEND_OFFSET
  739. xyz_pos_t diff = hotend_offset[new_tool] - hotend_offset[old_tool];
  740. TERN_(DUAL_X_CARRIAGE, diff.x = 0);
  741. #else
  742. constexpr xyz_pos_t diff{0};
  743. #endif
  744. #if ENABLED(DUAL_X_CARRIAGE)
  745. dualx_tool_change(new_tool, no_move);
  746. #elif ENABLED(PARKING_EXTRUDER) // Dual Parking extruder
  747. parking_extruder_tool_change(new_tool, no_move);
  748. #elif ENABLED(MAGNETIC_PARKING_EXTRUDER) // Magnetic Parking extruder
  749. magnetic_parking_extruder_tool_change(new_tool);
  750. #elif ENABLED(SWITCHING_TOOLHEAD) // Switching Toolhead
  751. switching_toolhead_tool_change(new_tool, no_move);
  752. #elif ENABLED(MAGNETIC_SWITCHING_TOOLHEAD) // Magnetic Switching Toolhead
  753. magnetic_switching_toolhead_tool_change(new_tool, no_move);
  754. #elif ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD) // Magnetic Switching ToolChanger
  755. em_switching_toolhead_tool_change(new_tool, no_move);
  756. #elif ENABLED(SWITCHING_NOZZLE) && !SWITCHING_NOZZLE_TWO_SERVOS // Switching Nozzle (single servo)
  757. // Raise by a configured distance to avoid workpiece, except with
  758. // SWITCHING_NOZZLE_TWO_SERVOS, as both nozzles will lift instead.
  759. if (!no_move) {
  760. const float newz = current_position.z + _MAX(-diff.z, 0.0);
  761. // Check if Z has space to compensate at least z_offset, and if not, just abort now
  762. const float maxz = _MIN(TERN(HAS_SOFTWARE_ENDSTOPS, soft_endstop.max.z, Z_MAX_POS), Z_MAX_POS);
  763. if (newz > maxz) return;
  764. current_position.z = _MIN(newz + toolchange_settings.z_raise, maxz);
  765. fast_line_to_current(Z_AXIS);
  766. }
  767. move_nozzle_servo(new_tool);
  768. #endif
  769. // Set the new active extruder
  770. if (DISABLED(DUAL_X_CARRIAGE)) active_extruder = new_tool;
  771. // The newly-selected extruder XYZ is actually at...
  772. DEBUG_ECHOLNPAIR("Offset Tool XYZ by { ", diff.x, ", ", diff.y, ", ", diff.z, " }");
  773. current_position += diff;
  774. // Tell the planner the new "current position"
  775. sync_plan_position();
  776. #if ENABLED(DELTA)
  777. //LOOP_XYZ(i) update_software_endstops(i); // or modify the constrain function
  778. const bool safe_to_move = current_position.z < delta_clip_start_height - 1;
  779. #else
  780. constexpr bool safe_to_move = true;
  781. #endif
  782. // Return to position and lower again
  783. const bool should_move = safe_to_move && !no_move && IsRunning();
  784. if (should_move) {
  785. #if BOTH(HAS_FAN, SINGLENOZZLE_STANDBY_FAN)
  786. singlenozzle_fan_speed[old_tool] = thermalManager.fan_speed[0];
  787. thermalManager.fan_speed[0] = singlenozzle_fan_speed[new_tool];
  788. #endif
  789. #if ENABLED(SINGLENOZZLE_STANDBY_TEMP)
  790. singlenozzle_temp[old_tool] = thermalManager.temp_hotend[0].target;
  791. if (singlenozzle_temp[new_tool] && singlenozzle_temp[new_tool] != singlenozzle_temp[old_tool]) {
  792. thermalManager.setTargetHotend(singlenozzle_temp[new_tool], 0);
  793. TERN_(AUTOTEMP, planner.autotemp_update());
  794. TERN_(HAS_DISPLAY, thermalManager.set_heating_message(0));
  795. (void)thermalManager.wait_for_hotend(0, false); // Wait for heating or cooling
  796. }
  797. #endif
  798. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  799. if (should_swap && !too_cold) {
  800. float fr = toolchange_settings.unretract_speed;
  801. #if ENABLED(TOOLCHANGE_FS_INIT_BEFORE_SWAP)
  802. if (!toolchange_extruder_ready[new_tool]) {
  803. toolchange_extruder_ready[new_tool] = true;
  804. fr = toolchange_settings.prime_speed; // Next move is a prime
  805. unscaled_e_move(0, MMM_TO_MMS(fr)); // Init planner with 0 length move
  806. }
  807. #endif
  808. // Unretract (or Prime)
  809. unscaled_e_move(toolchange_settings.swap_length, MMM_TO_MMS(fr));
  810. // Extra Prime
  811. unscaled_e_move(toolchange_settings.extra_prime, MMM_TO_MMS(toolchange_settings.prime_speed));
  812. // Cutting retraction
  813. #if TOOLCHANGE_FS_WIPE_RETRACT
  814. unscaled_e_move(-(TOOLCHANGE_FS_WIPE_RETRACT), MMM_TO_MMS(toolchange_settings.retract_speed));
  815. #endif
  816. // Cool down with fan
  817. #if HAS_FAN && TOOLCHANGE_FS_FAN >= 0
  818. thermalManager.fan_speed[TOOLCHANGE_FS_FAN] = toolchange_settings.fan_speed;
  819. gcode.dwell(toolchange_settings.fan_time * 1000);
  820. thermalManager.fan_speed[TOOLCHANGE_FS_FAN] = 0;
  821. #endif
  822. }
  823. #endif
  824. // Prevent a move outside physical bounds
  825. #if ENABLED(MAGNETIC_SWITCHING_TOOLHEAD)
  826. // If the original position is within tool store area, go to X origin at once
  827. if (destination.y < SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_CLEAR) {
  828. current_position.x = 0;
  829. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[X_AXIS], new_tool);
  830. planner.synchronize();
  831. }
  832. #else
  833. apply_motion_limits(destination);
  834. #endif
  835. // Should the nozzle move back to the old position?
  836. if (can_move_away) {
  837. #if ENABLED(TOOLCHANGE_NO_RETURN)
  838. // Just move back down
  839. DEBUG_ECHOLNPGM("Move back Z only");
  840. #if ENABLED(TOOLCHANGE_PARK)
  841. if (toolchange_settings.enable_park)
  842. #endif
  843. do_blocking_move_to_z(destination.z, planner.settings.max_feedrate_mm_s[Z_AXIS]);
  844. #else
  845. // Move back to the original (or adjusted) position
  846. DEBUG_POS("Move back", destination);
  847. #if ENABLED(TOOLCHANGE_PARK)
  848. if (toolchange_settings.enable_park) do_blocking_move_to_xy_z(destination, destination.z, MMM_TO_MMS(TOOLCHANGE_PARK_XY_FEEDRATE));
  849. #else
  850. do_blocking_move_to_xy(destination, planner.settings.max_feedrate_mm_s[X_AXIS]);
  851. do_blocking_move_to_z(destination.z, planner.settings.max_feedrate_mm_s[Z_AXIS]);
  852. #endif
  853. #endif
  854. }
  855. else DEBUG_ECHOLNPGM("Move back skipped");
  856. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  857. if (should_swap && !too_cold) {
  858. // Cutting recover
  859. unscaled_e_move(toolchange_settings.extra_resume + TOOLCHANGE_FS_WIPE_RETRACT, MMM_TO_MMS(toolchange_settings.unretract_speed));
  860. current_position.e = 0;
  861. sync_plan_position_e(); // New extruder primed and set to 0
  862. // Restart Fan
  863. #if HAS_FAN && TOOLCHANGE_FS_FAN >= 0
  864. RESTORE(fan);
  865. #endif
  866. }
  867. #endif
  868. TERN_(DUAL_X_CARRIAGE, active_extruder_parked = false);
  869. }
  870. #if ENABLED(SWITCHING_NOZZLE)
  871. // Move back down. (Including when the new tool is higher.)
  872. if (!should_move)
  873. do_blocking_move_to_z(destination.z, planner.settings.max_feedrate_mm_s[Z_AXIS]);
  874. #endif
  875. TERN_(PRUSA_MMU2, mmu2.tool_change(new_tool));
  876. TERN_(SWITCHING_NOZZLE_TWO_SERVOS, lower_nozzle(new_tool));
  877. } // (new_tool != old_tool)
  878. planner.synchronize();
  879. #if ENABLED(EXT_SOLENOID) && DISABLED(PARKING_EXTRUDER)
  880. disable_all_solenoids();
  881. enable_solenoid_on_active_extruder();
  882. #endif
  883. #if ENABLED(MK2_MULTIPLEXER)
  884. if (new_tool >= E_STEPPERS) return invalid_extruder_error(new_tool);
  885. select_multiplexed_stepper(new_tool);
  886. #endif
  887. #if DO_SWITCH_EXTRUDER
  888. planner.synchronize();
  889. move_extruder_servo(active_extruder);
  890. #endif
  891. TERN_(HAS_FANMUX, fanmux_switch(active_extruder));
  892. #ifdef EVENT_GCODE_AFTER_TOOLCHANGE
  893. if (!no_move && TERN1(DUAL_X_CARRIAGE, dual_x_carriage_mode == DXC_AUTO_PARK_MODE))
  894. gcode.process_subcommands_now_P(PSTR(EVENT_GCODE_AFTER_TOOLCHANGE));
  895. #endif
  896. SERIAL_ECHO_START();
  897. SERIAL_ECHOLNPAIR(STR_ACTIVE_EXTRUDER, int(active_extruder));
  898. #endif // HAS_MULTI_EXTRUDER
  899. }
  900. #if ENABLED(TOOLCHANGE_MIGRATION_FEATURE)
  901. bool extruder_migration() {
  902. #if ENABLED(PREVENT_COLD_EXTRUSION)
  903. if (thermalManager.targetTooColdToExtrude(active_extruder)) {
  904. #if ENABLED(DEBUG_TOOLCHANGE_MIGRATION_FEATURE)
  905. SERIAL_ECHOLN("Migration Source Too Cold");
  906. #endif
  907. return false;
  908. }
  909. #endif
  910. // No auto-migration or specified target?
  911. if (!migration.target && active_extruder >= migration.last) {
  912. #if ENABLED(DEBUG_TOOLCHANGE_MIGRATION_FEATURE)
  913. SERIAL_ECHO_MSG("No Migration Target");
  914. SERIAL_ECHO_MSG("Target: ", migration.target,
  915. " Last: ", migration.last,
  916. " Active: ", active_extruder);
  917. #endif
  918. migration.automode = false;
  919. return false;
  920. }
  921. // Migrate to a target or the next extruder
  922. uint8_t migration_extruder = active_extruder;
  923. if (migration.target) {
  924. #if ENABLED(DEBUG_TOOLCHANGE_MIGRATION_FEATURE)
  925. SERIAL_ECHOLN("Migration using fixed target");
  926. #endif
  927. // Specified target ok?
  928. const int16_t t = migration.target - 1;
  929. if (t != active_extruder) migration_extruder = t;
  930. }
  931. else if (migration.automode && migration_extruder < migration.last && migration_extruder < EXTRUDERS - 1)
  932. migration_extruder++;
  933. if (migration_extruder == active_extruder) {
  934. #if ENABLED(DEBUG_TOOLCHANGE_MIGRATION_FEATURE)
  935. SERIAL_ECHOLN("Migration source matches active");
  936. #endif
  937. return false;
  938. }
  939. // Migration begins
  940. #if ENABLED(DEBUG_TOOLCHANGE_MIGRATION_FEATURE)
  941. SERIAL_ECHOLN("Beginning migration");
  942. #endif
  943. migration.in_progress = true; // Prevent runout script
  944. planner.synchronize();
  945. // Remember position before migration
  946. const float resume_current_e = current_position.e;
  947. // Migrate the flow
  948. planner.set_flow(migration_extruder, planner.flow_percentage[active_extruder]);
  949. // Migrate the retracted state
  950. #if ENABLED(FWRETRACT)
  951. fwretract.retracted[migration_extruder] = fwretract.retracted[active_extruder];
  952. #endif
  953. // Migrate the temperature to the new hotend
  954. #if HAS_MULTI_HOTEND
  955. thermalManager.setTargetHotend(thermalManager.temp_hotend[active_extruder].target, migration_extruder);
  956. TERN_(AUTOTEMP, planner.autotemp_update());
  957. TERN_(HAS_DISPLAY, thermalManager.set_heating_message(0));
  958. thermalManager.wait_for_hotend(active_extruder);
  959. #endif
  960. // Migrate Linear Advance K factor to the new extruder
  961. TERN_(LIN_ADVANCE, planner.extruder_advance_K[active_extruder] = planner.extruder_advance_K[migration_extruder]);
  962. // Perform the tool change
  963. tool_change(migration_extruder);
  964. // Retract if previously retracted
  965. #if ENABLED(FWRETRACT)
  966. if (fwretract.retracted[active_extruder])
  967. unscaled_e_move(-fwretract.settings.retract_length, fwretract.settings.retract_feedrate_mm_s);
  968. #endif
  969. // If no available extruder
  970. if (EXTRUDERS < 2 || active_extruder >= EXTRUDERS - 2 || active_extruder == migration.last)
  971. migration.automode = false;
  972. migration.in_progress = false;
  973. current_position.e = resume_current_e;
  974. planner.synchronize();
  975. planner.set_e_position_mm(current_position.e); // New extruder primed and ready
  976. #if ENABLED(DEBUG_TOOLCHANGE_MIGRATION_FEATURE)
  977. SERIAL_ECHOLN("Migration Complete");
  978. #endif
  979. return true;
  980. }
  981. #endif // TOOLCHANGE_MIGRATION_FEATURE