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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #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 "../Marlin.h"
  29. #if EXTRUDERS > 1
  30. toolchange_settings_t toolchange_settings; // Initialized by settings.load()
  31. #endif
  32. #if ENABLED(SINGLENOZZLE)
  33. uint16_t singlenozzle_temp[EXTRUDERS];
  34. #if FAN_COUNT > 0
  35. uint8_t singlenozzle_fan_speed[EXTRUDERS];
  36. #endif
  37. #endif
  38. #if ENABLED(PARKING_EXTRUDER) && PARKING_EXTRUDER_SOLENOIDS_DELAY > 0
  39. #include "../gcode/gcode.h" // for dwell()
  40. #endif
  41. #if ENABLED(SWITCHING_EXTRUDER) || ENABLED(SWITCHING_NOZZLE) || ENABLED(SWITCHING_TOOLHEAD)
  42. #include "../module/servo.h"
  43. #endif
  44. #if ENABLED(EXT_SOLENOID) && DISABLED(PARKING_EXTRUDER)
  45. #include "../feature/solenoid.h"
  46. #endif
  47. #if ENABLED(MK2_MULTIPLEXER)
  48. #include "../feature/snmm.h"
  49. #endif
  50. #if ENABLED(MIXING_EXTRUDER)
  51. #include "../feature/mixing.h"
  52. #endif
  53. #if HAS_LEVELING
  54. #include "../feature/bedlevel/bedlevel.h"
  55. #endif
  56. #if HAS_FANMUX
  57. #include "../feature/fanmux.h"
  58. #endif
  59. #if HAS_LCD_MENU
  60. #include "../lcd/ultralcd.h"
  61. #endif
  62. #if DO_SWITCH_EXTRUDER
  63. #if EXTRUDERS > 3
  64. #define _SERVO_NR(E) ((E) < 2 ? SWITCHING_EXTRUDER_SERVO_NR : SWITCHING_EXTRUDER_E23_SERVO_NR)
  65. #else
  66. #define _SERVO_NR(E) SWITCHING_EXTRUDER_SERVO_NR
  67. #endif
  68. void move_extruder_servo(const uint8_t e) {
  69. planner.synchronize();
  70. #if EXTRUDERS & 1
  71. if (e < EXTRUDERS - 1)
  72. #endif
  73. {
  74. MOVE_SERVO(_SERVO_NR(e), servo_angles[_SERVO_NR(e)][e]);
  75. safe_delay(500);
  76. }
  77. }
  78. #endif // DO_SWITCH_EXTRUDER
  79. #if ENABLED(SWITCHING_NOZZLE)
  80. void move_nozzle_servo(const uint8_t e) {
  81. planner.synchronize();
  82. MOVE_SERVO(SWITCHING_NOZZLE_SERVO_NR, servo_angles[SWITCHING_NOZZLE_SERVO_NR][e]);
  83. safe_delay(500);
  84. }
  85. #endif // SWITCHING_NOZZLE
  86. #if ENABLED(PARKING_EXTRUDER)
  87. void pe_magnet_init() {
  88. for (uint8_t n = 0; n <= 1; ++n)
  89. #if ENABLED(PARKING_EXTRUDER_SOLENOIDS_INVERT)
  90. pe_activate_magnet(n);
  91. #else
  92. pe_deactivate_magnet(n);
  93. #endif
  94. }
  95. void pe_set_magnet(const uint8_t extruder_num, const uint8_t state) {
  96. switch (extruder_num) {
  97. case 1: OUT_WRITE(SOL1_PIN, state); break;
  98. default: OUT_WRITE(SOL0_PIN, state); break;
  99. }
  100. #if PARKING_EXTRUDER_SOLENOIDS_DELAY > 0
  101. gcode.dwell(PARKING_EXTRUDER_SOLENOIDS_DELAY);
  102. #endif
  103. }
  104. inline void parking_extruder_tool_change(const uint8_t tmp_extruder, bool no_move) {
  105. if (!no_move) {
  106. constexpr float parkingposx[] = PARKING_EXTRUDER_PARKING_X;
  107. #if HAS_HOTEND_OFFSET
  108. const float x_offset = hotend_offset[X_AXIS][active_extruder];
  109. #else
  110. constexpr float x_offset = 0;
  111. #endif
  112. const float midpos = (parkingposx[0] + parkingposx[1]) * 0.5 + x_offset,
  113. grabpos = parkingposx[tmp_extruder] + (tmp_extruder ? PARKING_EXTRUDER_GRAB_DISTANCE : -(PARKING_EXTRUDER_GRAB_DISTANCE)) + x_offset;
  114. /**
  115. * 1. Raise Z-Axis to give enough clearance
  116. * 2. Move to park position of old extruder
  117. * 3. Disengage magnetic field, wait for delay
  118. * 4. Move near new extruder
  119. * 5. Engage magnetic field for new extruder
  120. * 6. Move to parking incl. offset of new extruder
  121. * 7. Lower Z-Axis
  122. */
  123. // STEP 1
  124. #if ENABLED(DEBUG_LEVELING_FEATURE)
  125. if (DEBUGGING(LEVELING)) DEBUG_POS("Start Autopark", current_position);
  126. #endif
  127. current_position[Z_AXIS] += toolchange_settings.z_raise;
  128. #if ENABLED(DEBUG_LEVELING_FEATURE)
  129. if (DEBUGGING(LEVELING)) DEBUG_POS("(1) Raise Z-Axis", current_position);
  130. #endif
  131. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[Z_AXIS], active_extruder);
  132. planner.synchronize();
  133. // STEP 2
  134. current_position[X_AXIS] = parkingposx[active_extruder] + x_offset;
  135. #if ENABLED(DEBUG_LEVELING_FEATURE)
  136. if (DEBUGGING(LEVELING)) {
  137. SERIAL_ECHOLNPAIR("(2) Park extruder ", int(active_extruder));
  138. DEBUG_POS("Moving ParkPos", current_position);
  139. }
  140. #endif
  141. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[X_AXIS], active_extruder);
  142. planner.synchronize();
  143. // STEP 3
  144. #if ENABLED(DEBUG_LEVELING_FEATURE)
  145. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("(3) Disengage magnet ");
  146. #endif
  147. pe_deactivate_magnet(active_extruder);
  148. // STEP 4
  149. #if ENABLED(DEBUG_LEVELING_FEATURE)
  150. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("(4) Move to position near new extruder");
  151. #endif
  152. current_position[X_AXIS] += active_extruder ? -10 : 10; // move 10mm away from parked extruder
  153. #if ENABLED(DEBUG_LEVELING_FEATURE)
  154. if (DEBUGGING(LEVELING)) DEBUG_POS("Move away from parked extruder", current_position);
  155. #endif
  156. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[X_AXIS], active_extruder);
  157. planner.synchronize();
  158. // STEP 5
  159. #if ENABLED(DEBUG_LEVELING_FEATURE)
  160. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("(5) Engage magnetic field");
  161. #endif
  162. #if ENABLED(PARKING_EXTRUDER_SOLENOIDS_INVERT)
  163. pe_activate_magnet(active_extruder); //just save power for inverted magnets
  164. #endif
  165. pe_activate_magnet(tmp_extruder);
  166. // STEP 6
  167. current_position[X_AXIS] = grabpos + (tmp_extruder ? -10 : 10);
  168. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[X_AXIS], active_extruder);
  169. current_position[X_AXIS] = grabpos;
  170. #if ENABLED(DEBUG_LEVELING_FEATURE)
  171. if (DEBUGGING(LEVELING)) DEBUG_POS("(6) Unpark extruder", current_position);
  172. #endif
  173. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[X_AXIS]/2, active_extruder);
  174. planner.synchronize();
  175. // STEP 7
  176. current_position[X_AXIS] = midpos
  177. #if HAS_HOTEND_OFFSET
  178. - hotend_offset[X_AXIS][tmp_extruder]
  179. #endif
  180. ;
  181. #if ENABLED(DEBUG_LEVELING_FEATURE)
  182. if (DEBUGGING(LEVELING)) DEBUG_POS("(7) Move midway between hotends", current_position);
  183. #endif
  184. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[X_AXIS], active_extruder);
  185. planner.synchronize();
  186. #if ENABLED(DEBUG_LEVELING_FEATURE)
  187. SERIAL_ECHOLNPGM("Autopark done.");
  188. #endif
  189. }
  190. else { // nomove == true
  191. // Only engage magnetic field for new extruder
  192. pe_activate_magnet(tmp_extruder);
  193. #if ENABLED(PARKING_EXTRUDER_SOLENOIDS_INVERT)
  194. pe_activate_magnet(active_extruder); // Just save power for inverted magnets
  195. #endif
  196. }
  197. #if HAS_HOTEND_OFFSET
  198. current_position[Z_AXIS] += hotend_offset[Z_AXIS][active_extruder] - hotend_offset[Z_AXIS][tmp_extruder];
  199. #endif
  200. #if ENABLED(DEBUG_LEVELING_FEATURE)
  201. if (DEBUGGING(LEVELING)) DEBUG_POS("Applying Z-offset", current_position);
  202. #endif
  203. }
  204. #endif // PARKING_EXTRUDER
  205. #if ENABLED(SWITCHING_TOOLHEAD)
  206. inline void switching_toolhead_tool_change(const uint8_t tmp_extruder, const float fr_mm_s/*=0.0*/, bool no_move/*=false*/) {
  207. if (no_move) return;
  208. constexpr uint16_t angles[2] = SWITCHING_TOOLHEAD_SERVO_ANGLES;
  209. const float toolheadposx[] = SWITCHING_TOOLHEAD_X_POS,
  210. placexpos = toolheadposx[active_extruder],
  211. grabxpos = toolheadposx[tmp_extruder];
  212. /**
  213. * 1. Raise Z to give enough clearance
  214. * 2. Move to switch position of current toolhead
  215. * 3. Unlock tool and drop it in the dock
  216. * 4. Move to the new toolhead
  217. * 5. Grab and lock the new toolhead
  218. * 6. Apply the z-offset of the new toolhead
  219. */
  220. // STEP 1
  221. #if ENABLED(DEBUG_LEVELING_FEATURE)
  222. if (DEBUGGING(LEVELING)) DEBUG_POS("Starting Toolhead change", current_position);
  223. #endif
  224. current_position[Z_AXIS] += toolchange_settings.z_raise;
  225. #if ENABLED(DEBUG_LEVELING_FEATURE)
  226. if (DEBUGGING(LEVELING)) DEBUG_POS("(1) Raise Z-Axis", current_position);
  227. #endif
  228. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[Z_AXIS], active_extruder);
  229. planner.synchronize();
  230. // STEP 2
  231. current_position[X_AXIS] = placexpos;
  232. #if ENABLED(DEBUG_LEVELING_FEATURE)
  233. if (DEBUGGING(LEVELING)) {
  234. SERIAL_ECHOLNPAIR("(2) Place old tool ", int(active_extruder));
  235. DEBUG_POS("Move X SwitchPos", current_position);
  236. }
  237. #endif
  238. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[X_AXIS], active_extruder);
  239. planner.synchronize();
  240. current_position[Y_AXIS] = SWITCHING_TOOLHEAD_Y_POS - SWITCHING_TOOLHEAD_Y_SECURITY;
  241. #if ENABLED(DEBUG_LEVELING_FEATURE)
  242. if (DEBUGGING(LEVELING)) DEBUG_POS("Move Y SwitchPos + Security", current_position);
  243. #endif
  244. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[Y_AXIS], active_extruder);
  245. planner.synchronize();
  246. // STEP 3
  247. #if ENABLED(DEBUG_LEVELING_FEATURE)
  248. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("(3) Unlock and Place Toolhead");
  249. #endif
  250. MOVE_SERVO(SWITCHING_TOOLHEAD_SERVO_NR, angles[1]);
  251. safe_delay(500);
  252. current_position[Y_AXIS] = SWITCHING_TOOLHEAD_Y_POS;
  253. #if ENABLED(DEBUG_LEVELING_FEATURE)
  254. if (DEBUGGING(LEVELING)) DEBUG_POS("Move Y SwitchPos", current_position);
  255. #endif
  256. planner.buffer_line(current_position,(planner.settings.max_feedrate_mm_s[Y_AXIS] * 0.5), active_extruder);
  257. planner.synchronize();
  258. safe_delay(200);
  259. current_position[Y_AXIS] -= SWITCHING_TOOLHEAD_Y_CLEAR;
  260. #if ENABLED(DEBUG_LEVELING_FEATURE)
  261. if (DEBUGGING(LEVELING)) DEBUG_POS("Move back Y clear", current_position);
  262. #endif
  263. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[Y_AXIS], active_extruder); // move away from docked toolhead
  264. planner.synchronize();
  265. // STEP 4
  266. #if ENABLED(DEBUG_LEVELING_FEATURE)
  267. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("(4) Move to new toolhead position");
  268. #endif
  269. current_position[X_AXIS] = grabxpos;
  270. #if ENABLED(DEBUG_LEVELING_FEATURE)
  271. if (DEBUGGING(LEVELING)) DEBUG_POS("Move to new toolhead X", current_position);
  272. #endif
  273. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[X_AXIS], active_extruder);
  274. planner.synchronize();
  275. current_position[Y_AXIS] = SWITCHING_TOOLHEAD_Y_POS - SWITCHING_TOOLHEAD_Y_SECURITY;
  276. #if ENABLED(DEBUG_LEVELING_FEATURE)
  277. if (DEBUGGING(LEVELING)) DEBUG_POS("Move Y SwitchPos + Security", current_position);
  278. #endif
  279. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[Y_AXIS], active_extruder);
  280. planner.synchronize();
  281. // STEP 5
  282. #if ENABLED(DEBUG_LEVELING_FEATURE)
  283. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("(5) Grab and lock new toolhead ");
  284. #endif
  285. current_position[Y_AXIS] = SWITCHING_TOOLHEAD_Y_POS;
  286. #if ENABLED(DEBUG_LEVELING_FEATURE)
  287. if (DEBUGGING(LEVELING)) DEBUG_POS("Move Y SwitchPos", current_position);
  288. #endif
  289. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[Y_AXIS] * 0.5, active_extruder);
  290. planner.synchronize();
  291. safe_delay(200);
  292. MOVE_SERVO(SWITCHING_TOOLHEAD_SERVO_NR, angles[0]);
  293. safe_delay(500);
  294. current_position[Y_AXIS] -= SWITCHING_TOOLHEAD_Y_CLEAR;
  295. #if ENABLED(DEBUG_LEVELING_FEATURE)
  296. if (DEBUGGING(LEVELING)) DEBUG_POS("Move back Y clear", current_position);
  297. #endif
  298. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[Y_AXIS], active_extruder); // move away from docked toolhead
  299. planner.synchronize();
  300. // STEP 6
  301. #if HAS_HOTEND_OFFSET
  302. current_position[Z_AXIS] += hotend_offset[Z_AXIS][active_extruder] - hotend_offset[Z_AXIS][tmp_extruder];
  303. #endif
  304. #if ENABLED(DEBUG_LEVELING_FEATURE)
  305. if (DEBUGGING(LEVELING)) DEBUG_POS("(6) Apply Z offset", current_position);
  306. #endif
  307. #if ENABLED(DEBUG_LEVELING_FEATURE)
  308. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("Toolhead change done.");
  309. #endif
  310. }
  311. #endif // SWITCHING_TOOLHEAD
  312. inline void invalid_extruder_error(const uint8_t e) {
  313. SERIAL_ECHO_START();
  314. SERIAL_CHAR('T'); SERIAL_ECHO(int(e));
  315. SERIAL_CHAR(' '); SERIAL_ECHOLNPGM(MSG_INVALID_EXTRUDER);
  316. }
  317. #if ENABLED(DUAL_X_CARRIAGE)
  318. inline void dualx_tool_change(const uint8_t tmp_extruder, bool &no_move) {
  319. #if ENABLED(DEBUG_LEVELING_FEATURE)
  320. if (DEBUGGING(LEVELING)) {
  321. SERIAL_ECHOPGM("Dual X Carriage Mode ");
  322. switch (dual_x_carriage_mode) {
  323. case DXC_FULL_CONTROL_MODE: SERIAL_ECHOLNPGM("DXC_FULL_CONTROL_MODE"); break;
  324. case DXC_AUTO_PARK_MODE: SERIAL_ECHOLNPGM("DXC_AUTO_PARK_MODE"); break;
  325. case DXC_DUPLICATION_MODE: SERIAL_ECHOLNPGM("DXC_DUPLICATION_MODE"); break;
  326. case DXC_SCALED_DUPLICATION_MODE: SERIAL_ECHOLNPGM("DXC_SCALED_DUPLICATION_MODE"); break;
  327. }
  328. }
  329. #endif
  330. const float xhome = x_home_pos(active_extruder);
  331. if (dual_x_carriage_mode == DXC_AUTO_PARK_MODE
  332. && IsRunning()
  333. && (delayed_move_time || current_position[X_AXIS] != xhome) && ! no_move
  334. ) {
  335. #if ENABLED(DEBUG_LEVELING_FEATURE)
  336. if (DEBUGGING(LEVELING)) {
  337. SERIAL_ECHOLNPAIR("MoveX to ", xhome);
  338. }
  339. #endif
  340. // Park old head
  341. planner.buffer_line(xhome, current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], planner.settings.max_feedrate_mm_s[X_AXIS], active_extruder);
  342. planner.synchronize();
  343. }
  344. // Apply Y & Z extruder offset (X offset is used as home pos with Dual X)
  345. current_position[Y_AXIS] -= hotend_offset[Y_AXIS][active_extruder] - hotend_offset[Y_AXIS][tmp_extruder];
  346. current_position[Z_AXIS] -= hotend_offset[Z_AXIS][active_extruder] - hotend_offset[Z_AXIS][tmp_extruder];
  347. // Activate the new extruder ahead of calling set_axis_is_at_home!
  348. active_extruder = tmp_extruder;
  349. // This function resets the max/min values - the current position may be overwritten below.
  350. set_axis_is_at_home(X_AXIS);
  351. #if ENABLED(DEBUG_LEVELING_FEATURE)
  352. if (DEBUGGING(LEVELING)) DEBUG_POS("New Extruder", current_position);
  353. #endif
  354. switch (dual_x_carriage_mode) {
  355. case DXC_FULL_CONTROL_MODE:
  356. // New current position is the position of the activated extruder
  357. current_position[X_AXIS] = inactive_extruder_x_pos;
  358. // Save the inactive extruder's position (from the old current_position)
  359. inactive_extruder_x_pos = destination[X_AXIS];
  360. break;
  361. case DXC_AUTO_PARK_MODE:
  362. // record current raised toolhead position for use by unpark
  363. COPY(raised_parked_position, current_position);
  364. active_extruder_parked = true;
  365. delayed_move_time = 0;
  366. break;
  367. }
  368. #if ENABLED(DEBUG_LEVELING_FEATURE)
  369. if (DEBUGGING(LEVELING)) {
  370. SERIAL_ECHOLNPAIR("Active extruder parked: ", active_extruder_parked ? "yes" : "no");
  371. DEBUG_POS("New extruder (parked)", current_position);
  372. }
  373. #endif
  374. }
  375. #endif // DUAL_X_CARRIAGE
  376. /**
  377. * Perform a tool-change, which may result in moving the
  378. * previous tool out of the way and the new tool into place.
  379. */
  380. void tool_change(const uint8_t tmp_extruder, const float fr_mm_s/*=0.0*/, bool no_move/*=false*/) {
  381. #if ENABLED(MIXING_EXTRUDER)
  382. UNUSED(fr_mm_s); UNUSED(no_move);
  383. if (tmp_extruder >= MIXING_VIRTUAL_TOOLS)
  384. return invalid_extruder_error(tmp_extruder);
  385. #if MIXING_VIRTUAL_TOOLS > 1
  386. // T0-Tnnn: Switch virtual tool by changing the index to the mix
  387. mixer.T(uint_fast8_t(tmp_extruder));
  388. #endif
  389. #elif EXTRUDERS < 2
  390. UNUSED(fr_mm_s); UNUSED(no_move);
  391. if (tmp_extruder) invalid_extruder_error(tmp_extruder);
  392. return;
  393. #else // EXTRUDERS > 1
  394. planner.synchronize();
  395. #if ENABLED(DUAL_X_CARRIAGE) // Only T0 allowed if the Printer is in DXC_DUPLICATION_MODE or DXC_SCALED_DUPLICATION_MODE
  396. if (tmp_extruder != 0 && dxc_is_duplicating())
  397. return invalid_extruder_error(tmp_extruder);
  398. #endif
  399. #if HAS_LEVELING
  400. // Set current position to the physical position
  401. const bool leveling_was_active = planner.leveling_active;
  402. set_bed_leveling_enabled(false);
  403. #endif
  404. if (tmp_extruder >= EXTRUDERS)
  405. return invalid_extruder_error(tmp_extruder);
  406. if (!no_move && !all_axes_homed()) {
  407. no_move = true;
  408. #if ENABLED(DEBUG_LEVELING_FEATURE)
  409. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("No move on toolchange");
  410. #endif
  411. }
  412. #if HAS_LCD_MENU
  413. ui.return_to_status();
  414. #endif
  415. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  416. const bool should_swap = !no_move && toolchange_settings.swap_length;
  417. #if ENABLED(PREVENT_COLD_EXTRUSION)
  418. const bool too_cold = !DEBUGGING(DRYRUN) && (thermalManager.targetTooColdToExtrude(active_extruder) || thermalManager.targetTooColdToExtrude(tmp_extruder));
  419. #else
  420. constexpr bool too_cold = false;
  421. #endif
  422. if (should_swap) {
  423. if (too_cold) {
  424. SERIAL_ERROR_START();
  425. SERIAL_ERRORLNPGM(MSG_ERR_HOTEND_TOO_COLD);
  426. #if ENABLED(SINGLENOZZLE)
  427. active_extruder = tmp_extruder;
  428. return;
  429. #endif
  430. }
  431. else {
  432. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  433. do_pause_e_move(-toolchange_settings.swap_length, MMM_TO_MMS(toolchange_settings.retract_speed));
  434. #else
  435. current_position[E_AXIS] -= toolchange_settings.swap_length / planner.e_factor[active_extruder];
  436. planner.buffer_line(current_position, MMM_TO_MMS(toolchange_settings.retract_speed), active_extruder);
  437. #endif
  438. }
  439. }
  440. #endif // TOOLCHANGE_FILAMENT_SWAP
  441. if (tmp_extruder != active_extruder) {
  442. const float old_feedrate_mm_s = fr_mm_s > 0.0 ? fr_mm_s : feedrate_mm_s;
  443. feedrate_mm_s = fr_mm_s > 0.0 ? fr_mm_s : XY_PROBE_FEEDRATE_MM_S;
  444. #if ENABLED(DUAL_X_CARRIAGE)
  445. #if HAS_SOFTWARE_ENDSTOPS
  446. // Update the X software endstops early
  447. active_extruder = tmp_extruder;
  448. update_software_endstops(X_AXIS);
  449. active_extruder = !tmp_extruder;
  450. const float minx = soft_endstop_min[X_AXIS], maxx = soft_endstop_max[X_AXIS];
  451. #else
  452. // No software endstops? Use the configured limits
  453. const float minx = tmp_extruder ? X2_MIN_POS : X1_MIN_POS,
  454. maxx = tmp_extruder ? X2_MAX_POS : X1_MAX_POS;
  455. #endif
  456. // Don't move the new extruder out of bounds
  457. if (!WITHIN(current_position[X_AXIS], minx, maxx)) no_move = true;
  458. #endif
  459. if (!no_move) {
  460. set_destination_from_current();
  461. #if DISABLED(SWITCHING_NOZZLE)
  462. // Do a small lift to avoid the workpiece in the move back (below)
  463. #if ENABLED(TOOLCHANGE_PARK)
  464. current_position[X_AXIS] = toolchange_settings.change_point.x;
  465. current_position[Y_AXIS] = toolchange_settings.change_point.y;
  466. #endif
  467. current_position[Z_AXIS] += toolchange_settings.z_raise;
  468. #if HAS_SOFTWARE_ENDSTOPS
  469. NOMORE(current_position[Z_AXIS], soft_endstop_max[Z_AXIS]);
  470. #endif
  471. planner.buffer_line(current_position, feedrate_mm_s, active_extruder);
  472. #endif
  473. planner.synchronize();
  474. }
  475. #if HOTENDS > 1
  476. #if ENABLED(DUAL_X_CARRIAGE)
  477. constexpr float xdiff = 0;
  478. #else
  479. const float xdiff = hotend_offset[X_AXIS][tmp_extruder] - hotend_offset[X_AXIS][active_extruder];
  480. #endif
  481. const float ydiff = hotend_offset[Y_AXIS][tmp_extruder] - hotend_offset[Y_AXIS][active_extruder],
  482. zdiff = hotend_offset[Z_AXIS][tmp_extruder] - hotend_offset[Z_AXIS][active_extruder];
  483. #else
  484. constexpr float xdiff = 0, ydiff = 0, zdiff = 0;
  485. #endif
  486. #if ENABLED(DUAL_X_CARRIAGE)
  487. dualx_tool_change(tmp_extruder, no_move);
  488. #elif ENABLED(PARKING_EXTRUDER) // Dual Parking extruder
  489. parking_extruder_tool_change(tmp_extruder, no_move);
  490. #elif ENABLED(SWITCHING_TOOLHEAD) // Switching Toolhead
  491. switching_toolhead_tool_change(tmp_extruder, fr_mm_s, no_move);
  492. #elif ENABLED(SWITCHING_NOZZLE)
  493. // Always raise by a configured distance to avoid workpiece
  494. current_position[Z_AXIS] += MAX(-zdiff, 0.0) + toolchange_settings.z_raise;
  495. #if HAS_SOFTWARE_ENDSTOPS
  496. NOMORE(current_position[Z_AXIS], soft_endstop_max[Z_AXIS]);
  497. #endif
  498. if (!no_move)planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[Z_AXIS], active_extruder);
  499. move_nozzle_servo(tmp_extruder);
  500. #endif
  501. #if ENABLED(DEBUG_LEVELING_FEATURE)
  502. if (DEBUGGING(LEVELING)) {
  503. SERIAL_ECHOPAIR("Offset Tool XY by { ", xdiff);
  504. SERIAL_ECHOPAIR(", ", ydiff);
  505. SERIAL_ECHOPAIR(", ", zdiff);
  506. SERIAL_ECHOLNPGM(" }");
  507. }
  508. #endif
  509. // The newly-selected extruder XY is actually at...
  510. current_position[X_AXIS] += xdiff;
  511. current_position[Y_AXIS] += ydiff;
  512. current_position[Z_AXIS] += zdiff;
  513. // Set the new active extruder if not already done in tool specific function above
  514. active_extruder = tmp_extruder;
  515. // Tell the planner the new "current position"
  516. sync_plan_position();
  517. #if ENABLED(DELTA)
  518. //LOOP_XYZ(i) update_software_endstops(i); // or modify the constrain function
  519. const bool safe_to_move = current_position[Z_AXIS] < delta_clip_start_height - 1;
  520. #else
  521. constexpr bool safe_to_move = true;
  522. #endif
  523. // Return to position and lower again
  524. if (safe_to_move && !no_move && IsRunning()) {
  525. #if ENABLED(DEBUG_LEVELING_FEATURE)
  526. if (DEBUGGING(LEVELING)) DEBUG_POS("Move back", destination);
  527. #endif
  528. #if ENABLED(SINGLENOZZLE)
  529. #if FAN_COUNT > 0
  530. singlenozzle_fan_speed[active_extruder] = fan_speed[0];
  531. fan_speed[0] = singlenozzle_fan_speed[tmp_extruder];
  532. #endif
  533. singlenozzle_temp[active_extruder] = thermalManager.target_temperature[0];
  534. if (singlenozzle_temp[tmp_extruder] && singlenozzle_temp[tmp_extruder] != singlenozzle_temp[active_extruder]) {
  535. thermalManager.setTargetHotend(singlenozzle_temp[tmp_extruder], 0);
  536. #if ENABLED(ULTRA_LCD)
  537. thermalManager.set_heating_message(0);
  538. #endif
  539. (void)thermalManager.wait_for_hotend(0, false); // Wait for heating or cooling
  540. }
  541. active_extruder = tmp_extruder;
  542. #endif
  543. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  544. if (should_swap && !too_cold) {
  545. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  546. do_pause_e_move(toolchange_settings.swap_length, toolchange_settings.prime_speed);
  547. #else
  548. current_position[E_AXIS] += toolchange_settings.swap_length / planner.e_factor[tmp_extruder];
  549. planner.buffer_line(current_position, toolchange_settings.prime_speed, tmp_extruder);
  550. #endif
  551. planner.synchronize();
  552. }
  553. #endif
  554. // Move back to the original (or tweaked) position
  555. do_blocking_move_to(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS]);
  556. #if ENABLED(DUAL_X_CARRIAGE)
  557. active_extruder_parked = false;
  558. #endif
  559. feedrate_mm_s = old_feedrate_mm_s;
  560. }
  561. #if ENABLED(SWITCHING_NOZZLE)
  562. else {
  563. // Move back down. (Including when the new tool is higher.)
  564. do_blocking_move_to_z(destination[Z_AXIS], planner.settings.max_feedrate_mm_s[Z_AXIS]);
  565. }
  566. #endif
  567. } // (tmp_extruder != active_extruder)
  568. planner.synchronize();
  569. #if ENABLED(EXT_SOLENOID) && DISABLED(PARKING_EXTRUDER)
  570. disable_all_solenoids();
  571. enable_solenoid_on_active_extruder();
  572. #endif
  573. #if HAS_SOFTWARE_ENDSTOPS && ENABLED(DUAL_X_CARRIAGE)
  574. update_software_endstops(X_AXIS);
  575. #endif
  576. #if ENABLED(MK2_MULTIPLEXER)
  577. if (tmp_extruder >= E_STEPPERS) return invalid_extruder_error(tmp_extruder);
  578. select_multiplexed_stepper(tmp_extruder);
  579. #endif
  580. #if DO_SWITCH_EXTRUDER
  581. planner.synchronize();
  582. move_extruder_servo(active_extruder);
  583. #endif
  584. #if HAS_FANMUX
  585. fanmux_switch(active_extruder);
  586. #endif
  587. #if HAS_LEVELING
  588. // Restore leveling to re-establish the logical position
  589. set_bed_leveling_enabled(leveling_was_active);
  590. #endif
  591. SERIAL_ECHO_START();
  592. SERIAL_ECHOLNPAIR(MSG_ACTIVE_EXTRUDER, int(active_extruder));
  593. #endif // EXTRUDERS > 1
  594. }