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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2019 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. #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE)
  30. #include "../core/debug_out.h"
  31. #if EXTRUDERS > 1
  32. toolchange_settings_t toolchange_settings; // Initialized by settings.load()
  33. #endif
  34. #if ENABLED(SINGLENOZZLE)
  35. uint16_t singlenozzle_temp[EXTRUDERS];
  36. #if FAN_COUNT > 0
  37. uint8_t singlenozzle_fan_speed[EXTRUDERS];
  38. #endif
  39. #endif
  40. #if ENABLED(MAGNETIC_PARKING_EXTRUDER) || (ENABLED(PARKING_EXTRUDER) && PARKING_EXTRUDER_SOLENOIDS_DELAY > 0)
  41. #include "../gcode/gcode.h" // for dwell()
  42. #endif
  43. #if ANY(SWITCHING_EXTRUDER, SWITCHING_NOZZLE, SWITCHING_TOOLHEAD)
  44. #include "servo.h"
  45. #endif
  46. #if ENABLED(EXT_SOLENOID) && DISABLED(PARKING_EXTRUDER)
  47. #include "../feature/solenoid.h"
  48. #endif
  49. #if ENABLED(MK2_MULTIPLEXER)
  50. #include "../feature/snmm.h"
  51. #endif
  52. #if ENABLED(MIXING_EXTRUDER)
  53. #include "../feature/mixing.h"
  54. #endif
  55. #if HAS_LEVELING
  56. #include "../feature/bedlevel/bedlevel.h"
  57. #endif
  58. #if HAS_FANMUX
  59. #include "../feature/fanmux.h"
  60. #endif
  61. #if ENABLED(PRUSA_MMU2)
  62. #include "../feature/prusa_MMU2/mmu2.h"
  63. #endif
  64. #if HAS_LCD_MENU
  65. #include "../lcd/ultralcd.h"
  66. #endif
  67. #if DO_SWITCH_EXTRUDER
  68. #if EXTRUDERS > 3
  69. #define _SERVO_NR(E) ((E) < 2 ? SWITCHING_EXTRUDER_SERVO_NR : SWITCHING_EXTRUDER_E23_SERVO_NR)
  70. #else
  71. #define _SERVO_NR(E) SWITCHING_EXTRUDER_SERVO_NR
  72. #endif
  73. void move_extruder_servo(const uint8_t e) {
  74. planner.synchronize();
  75. #if EXTRUDERS & 1
  76. if (e < EXTRUDERS - 1)
  77. #endif
  78. {
  79. MOVE_SERVO(_SERVO_NR(e), servo_angles[_SERVO_NR(e)][e]);
  80. safe_delay(500);
  81. }
  82. }
  83. #endif // DO_SWITCH_EXTRUDER
  84. #if ENABLED(SWITCHING_NOZZLE)
  85. #if SWITCHING_NOZZLE_TWO_SERVOS
  86. inline void _move_nozzle_servo(const uint8_t e, const uint8_t angle_index) {
  87. constexpr int8_t sns_index[2] = { SWITCHING_NOZZLE_SERVO_NR, SWITCHING_NOZZLE_E1_SERVO_NR };
  88. constexpr int16_t sns_angles[2] = SWITCHING_NOZZLE_SERVO_ANGLES;
  89. planner.synchronize();
  90. MOVE_SERVO(sns_index[e], sns_angles[angle_index]);
  91. safe_delay(500);
  92. }
  93. void lower_nozzle(const uint8_t e) { _move_nozzle_servo(e, 0); }
  94. void raise_nozzle(const uint8_t e) { _move_nozzle_servo(e, 1); }
  95. #else
  96. void move_nozzle_servo(const uint8_t angle_index) {
  97. planner.synchronize();
  98. MOVE_SERVO(SWITCHING_NOZZLE_SERVO_NR, servo_angles[SWITCHING_NOZZLE_SERVO_NR][angle_index]);
  99. safe_delay(500);
  100. }
  101. #endif
  102. #endif // SWITCHING_NOZZLE
  103. inline void fast_line_to_current(const AxisEnum fr_axis) {
  104. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[fr_axis], active_extruder);
  105. }
  106. #if ENABLED(MAGNETIC_PARKING_EXTRUDER)
  107. float parkingposx[2] , // M951 R L
  108. parkinggrabdistance , // M951 I
  109. parkingslowspeed, // M951 J
  110. parkinghighspeed , // M951 H
  111. parkingtraveldistance, // M951 D
  112. compensationmultiplier;
  113. inline void magnetic_parking_extruder_tool_change(const uint8_t tmp_extruder) {
  114. const float oldx = current_position[X_AXIS],
  115. grabpos = mpe_settings.parking_xpos[tmp_extruder] + (tmp_extruder ? mpe_settings.grab_distance : -mpe_settings.grab_distance),
  116. offsetcompensation =
  117. #if HAS_HOTEND_OFFSET
  118. hotend_offset[X_AXIS][active_extruder] * mpe_settings.compensation_factor
  119. #else
  120. 0
  121. #endif
  122. ;
  123. if (axis_unhomed_error(true, false, false)) return;
  124. /**
  125. * Z Lift and Nozzle Offset shift ar defined in caller method to work equal with any Multi Hotend realization
  126. *
  127. * Steps:
  128. * 1. Move high speed to park position of new extruder
  129. * 2. Move to couple position of new extruder (this also discouple the old extruder)
  130. * 3. Move to park position of new extruder
  131. * 4. Move high speed to approach park position of old extruder
  132. * 5. Move to park position of old extruder
  133. * 6. Move to starting position
  134. */
  135. // STEP 1
  136. current_position[X_AXIS] = mpe_settings.parking_xpos[tmp_extruder] + offsetcompensation;
  137. if (DEBUGGING(LEVELING)) {
  138. DEBUG_ECHOPAIR("(1) Move extruder ", int(tmp_extruder));
  139. DEBUG_POS(" to new extruder ParkPos", current_position);
  140. }
  141. planner.buffer_line(current_position, mpe_settings.fast_feedrate, tmp_extruder);
  142. planner.synchronize();
  143. // STEP 2
  144. current_position[X_AXIS] = grabpos + offsetcompensation;
  145. if (DEBUGGING(LEVELING)) {
  146. DEBUG_ECHOPAIR("(2) Couple extruder ", int(tmp_extruder));
  147. DEBUG_POS(" to new extruder GrabPos", current_position);
  148. }
  149. planner.buffer_line(current_position, mpe_settings.slow_feedrate, tmp_extruder);
  150. planner.synchronize();
  151. // Delay before moving tool, to allow magnetic coupling
  152. gcode.dwell(150);
  153. // STEP 3
  154. current_position[X_AXIS] = mpe_settings.parking_xpos[tmp_extruder] + offsetcompensation;
  155. if (DEBUGGING(LEVELING)) {
  156. DEBUG_ECHOPAIR("(3) Move extruder ", int(tmp_extruder));
  157. DEBUG_POS(" back to new extruder ParkPos", current_position);
  158. }
  159. planner.buffer_line(current_position, mpe_settings.slow_feedrate, tmp_extruder);
  160. planner.synchronize();
  161. // STEP 4
  162. current_position[X_AXIS] = mpe_settings.parking_xpos[active_extruder] + (active_extruder == 0 ? MPE_TRAVEL_DISTANCE : -MPE_TRAVEL_DISTANCE) + offsetcompensation;
  163. if (DEBUGGING(LEVELING)) {
  164. DEBUG_ECHOPAIR("(4) Move extruder ", int(tmp_extruder));
  165. DEBUG_POS(" close to old extruder ParkPos", current_position);
  166. }
  167. planner.buffer_line(current_position, mpe_settings.fast_feedrate, tmp_extruder);
  168. planner.synchronize();
  169. // STEP 5
  170. current_position[X_AXIS] = mpe_settings.parking_xpos[active_extruder] + offsetcompensation;
  171. if (DEBUGGING(LEVELING)) {
  172. DEBUG_ECHOPAIR("(5) Park extruder ", int(tmp_extruder));
  173. DEBUG_POS(" at old extruder ParkPos", current_position);
  174. }
  175. planner.buffer_line(current_position, mpe_settings.slow_feedrate, tmp_extruder);
  176. planner.synchronize();
  177. // STEP 6
  178. current_position[X_AXIS] = oldx;
  179. if (DEBUGGING(LEVELING)) {
  180. DEBUG_ECHOPAIR("(6) Move extruder ", int(tmp_extruder));
  181. DEBUG_POS(" to starting position", current_position);
  182. }
  183. planner.buffer_line(current_position, mpe_settings.fast_feedrate, tmp_extruder);
  184. planner.synchronize();
  185. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Autopark done.");
  186. }
  187. #elif ENABLED(PARKING_EXTRUDER)
  188. void pe_solenoid_init() {
  189. for (uint8_t n = 0; n <= 1; ++n)
  190. #if ENABLED(PARKING_EXTRUDER_SOLENOIDS_INVERT)
  191. pe_activate_solenoid(n);
  192. #else
  193. pe_deactivate_solenoid(n);
  194. #endif
  195. }
  196. void pe_set_solenoid(const uint8_t extruder_num, const uint8_t state) {
  197. switch (extruder_num) {
  198. case 1: OUT_WRITE(SOL1_PIN, state); break;
  199. default: OUT_WRITE(SOL0_PIN, state); break;
  200. }
  201. #if PARKING_EXTRUDER_SOLENOIDS_DELAY > 0
  202. gcode.dwell(PARKING_EXTRUDER_SOLENOIDS_DELAY);
  203. #endif
  204. }
  205. inline void parking_extruder_tool_change(const uint8_t tmp_extruder, bool no_move) {
  206. if (!no_move) {
  207. constexpr float parkingposx[] = PARKING_EXTRUDER_PARKING_X;
  208. #if HAS_HOTEND_OFFSET
  209. const float x_offset = hotend_offset[X_AXIS][active_extruder];
  210. #else
  211. constexpr float x_offset = 0;
  212. #endif
  213. const float midpos = (parkingposx[0] + parkingposx[1]) * 0.5 + x_offset,
  214. grabpos = parkingposx[tmp_extruder] + (tmp_extruder ? PARKING_EXTRUDER_GRAB_DISTANCE : -(PARKING_EXTRUDER_GRAB_DISTANCE)) + x_offset;
  215. /**
  216. * 1. Move to park position of old extruder
  217. * 2. Disengage magnetic field, wait for delay
  218. * 3. Move near new extruder
  219. * 4. Engage magnetic field for new extruder
  220. * 5. Move to parking incl. offset of new extruder
  221. * 6. Lower Z-Axis
  222. */
  223. // STEP 1
  224. if (DEBUGGING(LEVELING)) DEBUG_POS("Start Autopark", current_position);
  225. current_position[X_AXIS] = parkingposx[active_extruder] + x_offset;
  226. if (DEBUGGING(LEVELING)) {
  227. DEBUG_ECHOLNPAIR("(1) Park extruder ", int(active_extruder));
  228. DEBUG_POS("Moving ParkPos", current_position);
  229. }
  230. fast_line_to_current(X_AXIS);
  231. planner.synchronize();
  232. // STEP 2
  233. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("(2) Disengage magnet ");
  234. pe_deactivate_solenoid(active_extruder);
  235. // STEP 3
  236. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("(3) Move to position near new extruder");
  237. current_position[X_AXIS] += active_extruder ? -10 : 10; // move 10mm away from parked extruder
  238. if (DEBUGGING(LEVELING)) DEBUG_POS("Move away from parked extruder", current_position);
  239. fast_line_to_current(X_AXIS);
  240. planner.synchronize();
  241. // STEP 4
  242. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("(4) Engage magnetic field");
  243. #if ENABLED(PARKING_EXTRUDER_SOLENOIDS_INVERT)
  244. pe_activate_solenoid(active_extruder); //just save power for inverted magnets
  245. #endif
  246. pe_activate_solenoid(tmp_extruder);
  247. // STEP 5
  248. current_position[X_AXIS] = grabpos + (tmp_extruder ? -10 : 10);
  249. fast_line_to_current(X_AXIS);
  250. current_position[X_AXIS] = grabpos;
  251. if (DEBUGGING(LEVELING)) DEBUG_POS("(5) Unpark extruder", current_position);
  252. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[X_AXIS] * 0.5, active_extruder);
  253. planner.synchronize();
  254. // STEP 6
  255. current_position[X_AXIS] = midpos
  256. #if HAS_HOTEND_OFFSET
  257. - hotend_offset[X_AXIS][tmp_extruder]
  258. #endif
  259. ;
  260. if (DEBUGGING(LEVELING)) DEBUG_POS("(6) Move midway between hotends", current_position);
  261. fast_line_to_current(X_AXIS);
  262. planner.synchronize();
  263. DEBUG_ECHOLNPGM("Autopark done.");
  264. }
  265. else { // nomove == true
  266. // Only engage magnetic field for new extruder
  267. pe_activate_solenoid(tmp_extruder);
  268. #if ENABLED(PARKING_EXTRUDER_SOLENOIDS_INVERT)
  269. pe_activate_solenoid(active_extruder); // Just save power for inverted magnets
  270. #endif
  271. }
  272. }
  273. #endif // PARKING_EXTRUDER
  274. #if ENABLED(SWITCHING_TOOLHEAD)
  275. inline void switching_toolhead_tool_change(const uint8_t tmp_extruder, const float fr_mm_s/*=0.0*/, bool no_move/*=false*/) {
  276. if (no_move) return;
  277. constexpr uint16_t angles[2] = SWITCHING_TOOLHEAD_SERVO_ANGLES;
  278. const float toolheadposx[] = SWITCHING_TOOLHEAD_X_POS,
  279. placexpos = toolheadposx[active_extruder],
  280. grabxpos = toolheadposx[tmp_extruder];
  281. /**
  282. * 1. Move to switch position of current toolhead
  283. * 2. Unlock tool and drop it in the dock
  284. * 3. Move to the new toolhead
  285. * 4. Grab and lock the new toolhead
  286. */
  287. // 1. Move to switch position of current toolhead
  288. if (DEBUGGING(LEVELING)) DEBUG_POS("Starting Toolhead change", current_position);
  289. current_position[X_AXIS] = placexpos;
  290. if (DEBUGGING(LEVELING)) {
  291. DEBUG_ECHOLNPAIR("(1) Place old tool ", int(active_extruder));
  292. DEBUG_POS("Move X SwitchPos", current_position);
  293. }
  294. fast_line_to_current(X_AXIS);
  295. planner.synchronize();
  296. current_position[Y_AXIS] = SWITCHING_TOOLHEAD_Y_POS - SWITCHING_TOOLHEAD_Y_SECURITY;
  297. if (DEBUGGING(LEVELING)) DEBUG_POS("Move Y SwitchPos + Security", current_position);
  298. fast_line_to_current(Y_AXIS);
  299. planner.synchronize();
  300. // 2. Unlock tool and drop it in the dock
  301. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("(2) Unlock and Place Toolhead");
  302. MOVE_SERVO(SWITCHING_TOOLHEAD_SERVO_NR, angles[1]);
  303. safe_delay(500);
  304. current_position[Y_AXIS] = SWITCHING_TOOLHEAD_Y_POS;
  305. if (DEBUGGING(LEVELING)) DEBUG_POS("Move Y SwitchPos", current_position);
  306. planner.buffer_line(current_position,(planner.settings.max_feedrate_mm_s[Y_AXIS] * 0.5), active_extruder);
  307. planner.synchronize();
  308. safe_delay(200);
  309. current_position[Y_AXIS] -= SWITCHING_TOOLHEAD_Y_CLEAR;
  310. if (DEBUGGING(LEVELING)) DEBUG_POS("Move back Y clear", current_position);
  311. fast_line_to_current(Y_AXIS); // move away from docked toolhead
  312. planner.synchronize();
  313. // 3. Move to the new toolhead
  314. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("(3) Move to new toolhead position");
  315. current_position[X_AXIS] = grabxpos;
  316. if (DEBUGGING(LEVELING)) DEBUG_POS("Move to new toolhead X", current_position);
  317. fast_line_to_current(X_AXIS);
  318. planner.synchronize();
  319. current_position[Y_AXIS] = SWITCHING_TOOLHEAD_Y_POS - SWITCHING_TOOLHEAD_Y_SECURITY;
  320. if (DEBUGGING(LEVELING)) DEBUG_POS("Move Y SwitchPos + Security", current_position);
  321. fast_line_to_current(Y_AXIS);
  322. planner.synchronize();
  323. // 4. Grab and lock the new toolhead
  324. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("(4) Grab and lock new toolhead ");
  325. current_position[Y_AXIS] = SWITCHING_TOOLHEAD_Y_POS;
  326. if (DEBUGGING(LEVELING)) DEBUG_POS("Move Y SwitchPos", current_position);
  327. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[Y_AXIS] * 0.5, active_extruder);
  328. planner.synchronize();
  329. safe_delay(200);
  330. MOVE_SERVO(SWITCHING_TOOLHEAD_SERVO_NR, angles[0]);
  331. safe_delay(500);
  332. current_position[Y_AXIS] -= SWITCHING_TOOLHEAD_Y_CLEAR;
  333. if (DEBUGGING(LEVELING)) DEBUG_POS("Move back Y clear", current_position);
  334. fast_line_to_current(Y_AXIS); // move away from docked toolhead
  335. planner.synchronize();
  336. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Toolhead change done.");
  337. }
  338. #endif // SWITCHING_TOOLHEAD
  339. #if ENABLED(MAGNETIC_SWITCHING_TOOLHEAD)
  340. inline void magnetic_switching_toolhead_tool_change(const uint8_t tmp_extruder, const float fr_mm_s/*=0.0*/, bool no_move/*=false*/) {
  341. if (no_move) return;
  342. const float toolheadposx[] = SWITCHING_TOOLHEAD_X_POS,
  343. placexpos = toolheadposx[active_extruder],
  344. grabxpos = toolheadposx[tmp_extruder];
  345. /**
  346. * 1. Move to switch position of current toolhead
  347. * 2. Release and place toolhead in the dock
  348. * 3. Move to the new toolhead
  349. * 4. Grab the new toolhead and move to security position
  350. */
  351. if (DEBUGGING(LEVELING)) DEBUG_POS("Starting Toolhead change", current_position);
  352. // 1. Move to switch position current toolhead
  353. current_position[Y_AXIS] = SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_CLEAR;
  354. if (DEBUGGING(LEVELING)) {
  355. SERIAL_ECHOLNPAIR("(1) Place old tool ", int(active_extruder));
  356. DEBUG_POS("Move Y SwitchPos + Security", current_position);
  357. }
  358. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[Y_AXIS], active_extruder);
  359. planner.synchronize();
  360. current_position[X_AXIS] = placexpos + SWITCHING_TOOLHEAD_X_SECURITY;
  361. if (DEBUGGING(LEVELING)) DEBUG_POS("Move X SwitchPos + Security", current_position);
  362. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[X_AXIS], active_extruder);
  363. planner.synchronize();
  364. current_position[Y_AXIS] = SWITCHING_TOOLHEAD_Y_POS;
  365. if (DEBUGGING(LEVELING)) DEBUG_POS("Move Y SwitchPos", current_position);
  366. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[Y_AXIS], active_extruder);
  367. planner.synchronize();
  368. current_position[X_AXIS] = placexpos;
  369. if (DEBUGGING(LEVELING)) DEBUG_POS("Move X SwitchPos", current_position);
  370. planner.buffer_line(current_position, (planner.settings.max_feedrate_mm_s[X_AXIS] * 0.25), active_extruder);
  371. planner.synchronize();
  372. // 2. Release and place toolhead in the dock
  373. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("(2) Release and Place Toolhead");
  374. current_position[Y_AXIS] = SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_RELEASE;
  375. if (DEBUGGING(LEVELING)) DEBUG_POS("Move Y SwitchPos + Release", current_position);
  376. planner.buffer_line(current_position, (planner.settings.max_feedrate_mm_s[Y_AXIS] * 0.1), active_extruder);
  377. planner.synchronize();
  378. current_position[Y_AXIS] = SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_SECURITY;
  379. if (DEBUGGING(LEVELING)) DEBUG_POS("Move Y SwitchPos + Security", current_position);
  380. planner.buffer_line(current_position, (planner.settings.max_feedrate_mm_s[Y_AXIS]), active_extruder);
  381. planner.synchronize();
  382. // 3. Move to new toolhead position
  383. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("(3) Move to new toolhead position");
  384. current_position[X_AXIS] = grabxpos;
  385. if (DEBUGGING(LEVELING)) DEBUG_POS("Move to new toolhead X", current_position);
  386. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[X_AXIS], active_extruder);
  387. planner.synchronize();
  388. // 4. Grab the new toolhead and move to security position
  389. if (DEBUGGING(LEVELING)) SERIAL_ECHOLNPGM("(4) Grab new toolhead and move to security position");
  390. current_position[Y_AXIS] = SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_RELEASE;
  391. if (DEBUGGING(LEVELING)) DEBUG_POS("Move Y SwitchPos + Release", current_position);
  392. planner.buffer_line(current_position, (planner.settings.max_feedrate_mm_s[Y_AXIS]), active_extruder);
  393. planner.synchronize();
  394. current_position[Y_AXIS] = SWITCHING_TOOLHEAD_Y_POS;
  395. if (DEBUGGING(LEVELING)) DEBUG_POS("Move Y SwitchPos", current_position);
  396. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[Y_AXIS] * 0.2, active_extruder);
  397. planner.synchronize();
  398. safe_delay(100);
  399. current_position[X_AXIS] = grabxpos + SWITCHING_TOOLHEAD_X_SECURITY;
  400. if (DEBUGGING(LEVELING)) DEBUG_POS("Move to new toolhead X + Security", current_position);
  401. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[X_AXIS] * 0.1, active_extruder);
  402. planner.synchronize();
  403. safe_delay(100);
  404. current_position[Y_AXIS] += SWITCHING_TOOLHEAD_Y_CLEAR;
  405. if (DEBUGGING(LEVELING)) DEBUG_POS("Move back Y clear", current_position);
  406. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[Y_AXIS], active_extruder); // move away from docked toolhead
  407. planner.synchronize();
  408. }
  409. #endif // MAGNETIC_SWITCHING_TOOLHEAD
  410. inline void invalid_extruder_error(const uint8_t e) {
  411. SERIAL_ECHO_START();
  412. SERIAL_CHAR('T'); SERIAL_ECHO(int(e));
  413. SERIAL_CHAR(' '); SERIAL_ECHOLNPGM(MSG_INVALID_EXTRUDER);
  414. }
  415. #if ENABLED(DUAL_X_CARRIAGE)
  416. inline void dualx_tool_change(const uint8_t tmp_extruder, bool &no_move) {
  417. if (DEBUGGING(LEVELING)) {
  418. DEBUG_ECHOPGM("Dual X Carriage Mode ");
  419. switch (dual_x_carriage_mode) {
  420. case DXC_FULL_CONTROL_MODE: DEBUG_ECHOLNPGM("FULL_CONTROL"); break;
  421. case DXC_AUTO_PARK_MODE: DEBUG_ECHOLNPGM("AUTO_PARK"); break;
  422. case DXC_DUPLICATION_MODE: DEBUG_ECHOLNPGM("DUPLICATION"); break;
  423. case DXC_MIRRORED_MODE: DEBUG_ECHOLNPGM("MIRRORED"); break;
  424. }
  425. }
  426. const float xhome = x_home_pos(active_extruder);
  427. if (dual_x_carriage_mode == DXC_AUTO_PARK_MODE
  428. && IsRunning()
  429. && (delayed_move_time || current_position[X_AXIS] != xhome) && ! no_move
  430. ) {
  431. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("MoveX to ", xhome);
  432. // Park old head
  433. 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);
  434. planner.synchronize();
  435. }
  436. // Activate the new extruder ahead of calling set_axis_is_at_home!
  437. active_extruder = tmp_extruder;
  438. // This function resets the max/min values - the current position may be overwritten below.
  439. set_axis_is_at_home(X_AXIS);
  440. if (DEBUGGING(LEVELING)) DEBUG_POS("New Extruder", current_position);
  441. switch (dual_x_carriage_mode) {
  442. case DXC_FULL_CONTROL_MODE:
  443. // New current position is the position of the activated extruder
  444. current_position[X_AXIS] = inactive_extruder_x_pos;
  445. // Save the inactive extruder's position (from the old current_position)
  446. inactive_extruder_x_pos = destination[X_AXIS];
  447. break;
  448. case DXC_AUTO_PARK_MODE:
  449. // record current raised toolhead position for use by unpark
  450. COPY(raised_parked_position, current_position);
  451. active_extruder_parked = true;
  452. delayed_move_time = 0;
  453. break;
  454. default:
  455. break;
  456. }
  457. if (DEBUGGING(LEVELING)) {
  458. DEBUG_ECHOLNPAIR("Active extruder parked: ", active_extruder_parked ? "yes" : "no");
  459. DEBUG_POS("New extruder (parked)", current_position);
  460. }
  461. }
  462. #endif // DUAL_X_CARRIAGE
  463. /**
  464. * Perform a tool-change, which may result in moving the
  465. * previous tool out of the way and the new tool into place.
  466. */
  467. void tool_change(const uint8_t tmp_extruder, const float fr_mm_s/*=0.0*/, bool no_move/*=false*/) {
  468. #if ENABLED(MAGNETIC_SWITCHING_TOOLHEAD)
  469. if (tmp_extruder == active_extruder) return;
  470. #endif
  471. #if ENABLED(MIXING_EXTRUDER)
  472. UNUSED(fr_mm_s); UNUSED(no_move);
  473. if (tmp_extruder >= MIXING_VIRTUAL_TOOLS)
  474. return invalid_extruder_error(tmp_extruder);
  475. #if MIXING_VIRTUAL_TOOLS > 1
  476. // T0-Tnnn: Switch virtual tool by changing the index to the mix
  477. mixer.T(tmp_extruder);
  478. #endif
  479. #elif ENABLED(PRUSA_MMU2)
  480. UNUSED(fr_mm_s); UNUSED(no_move);
  481. mmu2.tool_change(tmp_extruder);
  482. #elif EXTRUDERS < 2
  483. UNUSED(fr_mm_s); UNUSED(no_move);
  484. if (tmp_extruder) invalid_extruder_error(tmp_extruder);
  485. return;
  486. #else // EXTRUDERS > 1
  487. planner.synchronize();
  488. #if ENABLED(DUAL_X_CARRIAGE) // Only T0 allowed if the Printer is in DXC_DUPLICATION_MODE or DXC_MIRRORED_MODE
  489. if (tmp_extruder != 0 && dxc_is_duplicating())
  490. return invalid_extruder_error(tmp_extruder);
  491. #endif
  492. #if HAS_LEVELING
  493. // Set current position to the physical position
  494. const bool leveling_was_active = planner.leveling_active;
  495. set_bed_leveling_enabled(false);
  496. #endif
  497. if (tmp_extruder >= EXTRUDERS)
  498. return invalid_extruder_error(tmp_extruder);
  499. if (!no_move && !all_axes_homed()) {
  500. no_move = true;
  501. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("No move on toolchange");
  502. }
  503. #if HAS_LCD_MENU
  504. ui.return_to_status();
  505. #endif
  506. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  507. const bool should_swap = !no_move && toolchange_settings.swap_length;
  508. #if ENABLED(PREVENT_COLD_EXTRUSION)
  509. const bool too_cold = !DEBUGGING(DRYRUN) && (thermalManager.targetTooColdToExtrude(active_extruder) || thermalManager.targetTooColdToExtrude(tmp_extruder));
  510. #else
  511. constexpr bool too_cold = false;
  512. #endif
  513. if (should_swap) {
  514. if (too_cold) {
  515. SERIAL_ECHO_MSG(MSG_ERR_HOTEND_TOO_COLD);
  516. #if ENABLED(SINGLENOZZLE)
  517. active_extruder = tmp_extruder;
  518. return;
  519. #endif
  520. }
  521. else {
  522. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  523. do_pause_e_move(-toolchange_settings.swap_length, MMM_TO_MMS(toolchange_settings.retract_speed));
  524. #else
  525. current_position[E_AXIS] -= toolchange_settings.swap_length / planner.e_factor[active_extruder];
  526. planner.buffer_line(current_position, MMM_TO_MMS(toolchange_settings.retract_speed), active_extruder);
  527. #endif
  528. }
  529. }
  530. #endif // TOOLCHANGE_FILAMENT_SWAP
  531. if (tmp_extruder != active_extruder) {
  532. #if SWITCHING_NOZZLE_TWO_SERVOS
  533. raise_nozzle(active_extruder);
  534. #endif
  535. const float old_feedrate_mm_s = fr_mm_s > 0.0 ? fr_mm_s : feedrate_mm_s;
  536. feedrate_mm_s = fr_mm_s > 0.0 ? fr_mm_s : XY_PROBE_FEEDRATE_MM_S;
  537. #if HAS_SOFTWARE_ENDSTOPS && ENABLED(DUAL_X_CARRIAGE)
  538. update_software_endstops(X_AXIS, active_extruder, tmp_extruder);
  539. #endif
  540. set_destination_from_current();
  541. if (!no_move) {
  542. #if DISABLED(SWITCHING_NOZZLE)
  543. // Do a small lift to avoid the workpiece in the move back (below)
  544. current_position[Z_AXIS] += toolchange_settings.z_raise;
  545. #if HAS_SOFTWARE_ENDSTOPS
  546. NOMORE(current_position[Z_AXIS], soft_endstop[Z_AXIS].max);
  547. #endif
  548. fast_line_to_current(Z_AXIS);
  549. #if ENABLED(TOOLCHANGE_PARK)
  550. current_position[X_AXIS] = toolchange_settings.change_point.x;
  551. current_position[Y_AXIS] = toolchange_settings.change_point.y;
  552. #endif
  553. planner.buffer_line(current_position, feedrate_mm_s, active_extruder);
  554. #endif
  555. planner.synchronize();
  556. }
  557. #if HAS_HOTEND_OFFSET
  558. #if ENABLED(DUAL_X_CARRIAGE)
  559. constexpr float xdiff = 0;
  560. #else
  561. const float xdiff = hotend_offset[X_AXIS][tmp_extruder] - hotend_offset[X_AXIS][active_extruder];
  562. #endif
  563. const float ydiff = hotend_offset[Y_AXIS][tmp_extruder] - hotend_offset[Y_AXIS][active_extruder],
  564. zdiff = hotend_offset[Z_AXIS][tmp_extruder] - hotend_offset[Z_AXIS][active_extruder];
  565. #else
  566. constexpr float xdiff = 0, ydiff = 0, zdiff = 0;
  567. #endif
  568. #if ENABLED(DUAL_X_CARRIAGE)
  569. dualx_tool_change(tmp_extruder, no_move);
  570. #elif ENABLED(PARKING_EXTRUDER) // Dual Parking extruder
  571. parking_extruder_tool_change(tmp_extruder, no_move);
  572. #elif ENABLED(MAGNETIC_PARKING_EXTRUDER) // Magnetic Parking extruder
  573. magnetic_parking_extruder_tool_change(tmp_extruder);
  574. #elif ENABLED(SWITCHING_TOOLHEAD) // Switching Toolhead
  575. switching_toolhead_tool_change(tmp_extruder, fr_mm_s, no_move);
  576. #elif ENABLED(MAGNETIC_SWITCHING_TOOLHEAD) // Magnetic Switching Toolhead
  577. magnetic_switching_toolhead_tool_change(tmp_extruder, fr_mm_s, no_move);
  578. #elif ENABLED(SWITCHING_NOZZLE) && !SWITCHING_NOZZLE_TWO_SERVOS
  579. // Raise by a configured distance to avoid workpiece, except with
  580. // SWITCHING_NOZZLE_TWO_SERVOS, as both nozzles will lift instead.
  581. current_position[Z_AXIS] += MAX(-zdiff, 0.0) + toolchange_settings.z_raise;
  582. #if HAS_SOFTWARE_ENDSTOPS
  583. NOMORE(current_position[Z_AXIS], soft_endstop[Z_AXIS].max);
  584. #endif
  585. if (!no_move) fast_line_to_current(Z_AXIS);
  586. move_nozzle_servo(tmp_extruder);
  587. #endif
  588. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Offset Tool XY by { ", xdiff, ", ", ydiff, ", ", zdiff, " }");
  589. // The newly-selected extruder XY is actually at...
  590. current_position[X_AXIS] += xdiff;
  591. current_position[Y_AXIS] += ydiff;
  592. current_position[Z_AXIS] += zdiff;
  593. // Set the new active extruder if not already done in tool specific function above
  594. active_extruder = tmp_extruder;
  595. // Tell the planner the new "current position"
  596. sync_plan_position();
  597. #if ENABLED(DELTA)
  598. //LOOP_XYZ(i) update_software_endstops(i); // or modify the constrain function
  599. const bool safe_to_move = current_position[Z_AXIS] < delta_clip_start_height - 1;
  600. #else
  601. constexpr bool safe_to_move = true;
  602. #endif
  603. // Return to position and lower again
  604. if (safe_to_move && !no_move && IsRunning()) {
  605. if (DEBUGGING(LEVELING)) DEBUG_POS("Move back", destination);
  606. #if ENABLED(SINGLENOZZLE)
  607. #if FAN_COUNT > 0
  608. singlenozzle_fan_speed[active_extruder] = thermalManager.fan_speed[0];
  609. thermalManager.fan_speed[0] = singlenozzle_fan_speed[tmp_extruder];
  610. #endif
  611. singlenozzle_temp[active_extruder] = thermalManager.temp_hotend[0].target;
  612. if (singlenozzle_temp[tmp_extruder] && singlenozzle_temp[tmp_extruder] != singlenozzle_temp[active_extruder]) {
  613. thermalManager.setTargetHotend(singlenozzle_temp[tmp_extruder], 0);
  614. #if EITHER(ULTRA_LCD, EXTENSIBLE_UI)
  615. thermalManager.set_heating_message(0);
  616. #endif
  617. (void)thermalManager.wait_for_hotend(0, false); // Wait for heating or cooling
  618. }
  619. active_extruder = tmp_extruder;
  620. #endif
  621. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  622. if (should_swap && !too_cold) {
  623. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  624. do_pause_e_move(toolchange_settings.swap_length + TOOLCHANGE_FIL_EXTRA_PRIME, MMM_TO_MMS(toolchange_settings.prime_speed));
  625. #else
  626. current_position[E_AXIS] += (toolchange_settings.swap_length + TOOLCHANGE_FIL_EXTRA_PRIME) / planner.e_factor[tmp_extruder];
  627. planner.buffer_line(current_position, MMM_TO_MMS(toolchange_settings.prime_speed), tmp_extruder);
  628. #endif
  629. planner.synchronize();
  630. #if TOOLCHANGE_FIL_EXTRA_PRIME
  631. planner.set_e_position_mm((destination[E_AXIS] = current_position[E_AXIS] = current_position[E_AXIS] - (TOOLCHANGE_FIL_EXTRA_PRIME)));
  632. #endif
  633. }
  634. #endif
  635. // Prevent a move outside physical bounds
  636. #if ENABLED(MAGNETIC_SWITCHING_TOOLHEAD)
  637. // If the original position is within tool store area, go to X origin at once
  638. if (destination[Y_AXIS] < SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_CLEAR) {
  639. current_position[X_AXIS] = 0;
  640. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[X_AXIS], active_extruder);
  641. planner.synchronize();
  642. }
  643. #else
  644. apply_motion_limits(destination);
  645. #endif
  646. // Move back to the original (or tweaked) position
  647. do_blocking_move_to(destination);
  648. #if ENABLED(DUAL_X_CARRIAGE)
  649. active_extruder_parked = false;
  650. #endif
  651. feedrate_mm_s = old_feedrate_mm_s;
  652. }
  653. #if ENABLED(SWITCHING_NOZZLE)
  654. else {
  655. // Move back down. (Including when the new tool is higher.)
  656. do_blocking_move_to_z(destination[Z_AXIS], planner.settings.max_feedrate_mm_s[Z_AXIS]);
  657. }
  658. #endif
  659. #if ENABLED(PRUSA_MMU2)
  660. mmu2.tool_change(tmp_extruder);
  661. #endif
  662. #if SWITCHING_NOZZLE_TWO_SERVOS
  663. lower_nozzle(active_extruder);
  664. #endif
  665. } // (tmp_extruder != active_extruder)
  666. planner.synchronize();
  667. #if ENABLED(EXT_SOLENOID) && DISABLED(PARKING_EXTRUDER)
  668. disable_all_solenoids();
  669. enable_solenoid_on_active_extruder();
  670. #endif
  671. #if ENABLED(MK2_MULTIPLEXER)
  672. if (tmp_extruder >= E_STEPPERS) return invalid_extruder_error(tmp_extruder);
  673. select_multiplexed_stepper(tmp_extruder);
  674. #endif
  675. #if DO_SWITCH_EXTRUDER
  676. planner.synchronize();
  677. move_extruder_servo(active_extruder);
  678. #endif
  679. #if HAS_FANMUX
  680. fanmux_switch(active_extruder);
  681. #endif
  682. #if HAS_LEVELING
  683. // Restore leveling to re-establish the logical position
  684. set_bed_leveling_enabled(leveling_was_active);
  685. #endif
  686. SERIAL_ECHO_START();
  687. SERIAL_ECHOLNPAIR(MSG_ACTIVE_EXTRUDER, int(active_extruder));
  688. #endif // EXTRUDERS > 1
  689. }