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

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