My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

tool_change.cpp 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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 ENABLED(ADVANCED_PAUSE_FEATURE)
  68. #include "../feature/pause.h"
  69. #endif
  70. #if DO_SWITCH_EXTRUDER
  71. #if EXTRUDERS > 3
  72. #define _SERVO_NR(E) ((E) < 2 ? SWITCHING_EXTRUDER_SERVO_NR : SWITCHING_EXTRUDER_E23_SERVO_NR)
  73. #else
  74. #define _SERVO_NR(E) SWITCHING_EXTRUDER_SERVO_NR
  75. #endif
  76. void move_extruder_servo(const uint8_t e) {
  77. planner.synchronize();
  78. #if EXTRUDERS & 1
  79. if (e < EXTRUDERS - 1)
  80. #endif
  81. {
  82. MOVE_SERVO(_SERVO_NR(e), servo_angles[_SERVO_NR(e)][e]);
  83. safe_delay(500);
  84. }
  85. }
  86. #endif // DO_SWITCH_EXTRUDER
  87. #if ENABLED(SWITCHING_NOZZLE)
  88. #if SWITCHING_NOZZLE_TWO_SERVOS
  89. inline void _move_nozzle_servo(const uint8_t e, const uint8_t angle_index) {
  90. constexpr int8_t sns_index[2] = { SWITCHING_NOZZLE_SERVO_NR, SWITCHING_NOZZLE_E1_SERVO_NR };
  91. constexpr int16_t sns_angles[2] = SWITCHING_NOZZLE_SERVO_ANGLES;
  92. planner.synchronize();
  93. MOVE_SERVO(sns_index[e], sns_angles[angle_index]);
  94. safe_delay(500);
  95. }
  96. void lower_nozzle(const uint8_t e) { _move_nozzle_servo(e, 0); }
  97. void raise_nozzle(const uint8_t e) { _move_nozzle_servo(e, 1); }
  98. #else
  99. void move_nozzle_servo(const uint8_t angle_index) {
  100. planner.synchronize();
  101. MOVE_SERVO(SWITCHING_NOZZLE_SERVO_NR, servo_angles[SWITCHING_NOZZLE_SERVO_NR][angle_index]);
  102. safe_delay(500);
  103. }
  104. #endif
  105. #endif // SWITCHING_NOZZLE
  106. inline void _line_to_current(const AxisEnum fr_axis, const float fscale=1) {
  107. line_to_current_position(planner.settings.max_feedrate_mm_s[fr_axis] * fscale);
  108. }
  109. inline void slow_line_to_current(const AxisEnum fr_axis) { _line_to_current(fr_axis, 0.5f); }
  110. inline void fast_line_to_current(const AxisEnum fr_axis) { _line_to_current(fr_axis); }
  111. #if ENABLED(MAGNETIC_PARKING_EXTRUDER)
  112. float parkingposx[2], // M951 R L
  113. parkinggrabdistance, // M951 I
  114. parkingslowspeed, // M951 J
  115. parkinghighspeed, // M951 H
  116. parkingtraveldistance, // M951 D
  117. compensationmultiplier;
  118. inline void magnetic_parking_extruder_tool_change(const uint8_t new_tool) {
  119. const float oldx = current_position.x,
  120. grabpos = mpe_settings.parking_xpos[new_tool] + (new_tool ? mpe_settings.grab_distance : -mpe_settings.grab_distance),
  121. offsetcompensation = (0
  122. #if HAS_HOTEND_OFFSET
  123. + hotend_offset[active_extruder].x * mpe_settings.compensation_factor
  124. #endif
  125. );
  126. if (axis_unhomed_error(_BV(X_AXIS))) return;
  127. /**
  128. * Z Lift and Nozzle Offset shift ar defined in caller method to work equal with any Multi Hotend realization
  129. *
  130. * Steps:
  131. * 1. Move high speed to park position of new extruder
  132. * 2. Move to couple position of new extruder (this also discouple the old extruder)
  133. * 3. Move to park position of new extruder
  134. * 4. Move high speed to approach park position of old extruder
  135. * 5. Move to park position of old extruder
  136. * 6. Move to starting position
  137. */
  138. // STEP 1
  139. current_position.x = mpe_settings.parking_xpos[new_tool] + offsetcompensation;
  140. if (DEBUGGING(LEVELING)) {
  141. DEBUG_ECHOPAIR("(1) Move extruder ", int(new_tool));
  142. DEBUG_POS(" to new extruder ParkPos", current_position);
  143. }
  144. planner.buffer_line(current_position, mpe_settings.fast_feedrate, new_tool);
  145. planner.synchronize();
  146. // STEP 2
  147. current_position.x = grabpos + offsetcompensation;
  148. if (DEBUGGING(LEVELING)) {
  149. DEBUG_ECHOPAIR("(2) Couple extruder ", int(new_tool));
  150. DEBUG_POS(" to new extruder GrabPos", current_position);
  151. }
  152. planner.buffer_line(current_position, mpe_settings.slow_feedrate, new_tool);
  153. planner.synchronize();
  154. // Delay before moving tool, to allow magnetic coupling
  155. gcode.dwell(150);
  156. // STEP 3
  157. current_position.x = mpe_settings.parking_xpos[new_tool] + offsetcompensation;
  158. if (DEBUGGING(LEVELING)) {
  159. DEBUG_ECHOPAIR("(3) Move extruder ", int(new_tool));
  160. DEBUG_POS(" back to new extruder ParkPos", current_position);
  161. }
  162. planner.buffer_line(current_position, mpe_settings.slow_feedrate, new_tool);
  163. planner.synchronize();
  164. // STEP 4
  165. current_position.x = mpe_settings.parking_xpos[active_extruder] + (active_extruder == 0 ? MPE_TRAVEL_DISTANCE : -MPE_TRAVEL_DISTANCE) + offsetcompensation;
  166. if (DEBUGGING(LEVELING)) {
  167. DEBUG_ECHOPAIR("(4) Move extruder ", int(new_tool));
  168. DEBUG_POS(" close to old extruder ParkPos", current_position);
  169. }
  170. planner.buffer_line(current_position, mpe_settings.fast_feedrate, new_tool);
  171. planner.synchronize();
  172. // STEP 5
  173. current_position.x = mpe_settings.parking_xpos[active_extruder] + offsetcompensation;
  174. if (DEBUGGING(LEVELING)) {
  175. DEBUG_ECHOPAIR("(5) Park extruder ", int(new_tool));
  176. DEBUG_POS(" at old extruder ParkPos", current_position);
  177. }
  178. planner.buffer_line(current_position, mpe_settings.slow_feedrate, new_tool);
  179. planner.synchronize();
  180. // STEP 6
  181. current_position.x = oldx;
  182. if (DEBUGGING(LEVELING)) {
  183. DEBUG_ECHOPAIR("(6) Move extruder ", int(new_tool));
  184. DEBUG_POS(" to starting position", current_position);
  185. }
  186. planner.buffer_line(current_position, mpe_settings.fast_feedrate, new_tool);
  187. planner.synchronize();
  188. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Autopark done.");
  189. }
  190. #elif ENABLED(PARKING_EXTRUDER)
  191. void pe_solenoid_init() {
  192. for (uint8_t n = 0; n <= 1; ++n)
  193. #if ENABLED(PARKING_EXTRUDER_SOLENOIDS_INVERT)
  194. pe_activate_solenoid(n);
  195. #else
  196. pe_deactivate_solenoid(n);
  197. #endif
  198. }
  199. void pe_set_solenoid(const uint8_t extruder_num, const uint8_t state) {
  200. switch (extruder_num) {
  201. case 1: OUT_WRITE(SOL1_PIN, state); break;
  202. default: OUT_WRITE(SOL0_PIN, state); break;
  203. }
  204. #if PARKING_EXTRUDER_SOLENOIDS_DELAY > 0
  205. gcode.dwell(PARKING_EXTRUDER_SOLENOIDS_DELAY);
  206. #endif
  207. }
  208. inline void parking_extruder_tool_change(const uint8_t new_tool, bool no_move) {
  209. if (!no_move) {
  210. constexpr float parkingposx[] = PARKING_EXTRUDER_PARKING_X;
  211. #if HAS_HOTEND_OFFSET
  212. const float x_offset = hotend_offset[active_extruder].x;
  213. #else
  214. constexpr float x_offset = 0;
  215. #endif
  216. const float midpos = (parkingposx[0] + parkingposx[1]) * 0.5f + x_offset,
  217. grabpos = parkingposx[new_tool] + (new_tool ? PARKING_EXTRUDER_GRAB_DISTANCE : -(PARKING_EXTRUDER_GRAB_DISTANCE)) + x_offset;
  218. /**
  219. * 1. Move to park position of old extruder
  220. * 2. Disengage magnetic field, wait for delay
  221. * 3. Move near new extruder
  222. * 4. Engage magnetic field for new extruder
  223. * 5. Move to parking incl. offset of new extruder
  224. * 6. Lower Z-Axis
  225. */
  226. // STEP 1
  227. if (DEBUGGING(LEVELING)) DEBUG_POS("Start PE Tool-Change", current_position);
  228. current_position.x = parkingposx[active_extruder] + x_offset;
  229. if (DEBUGGING(LEVELING)) {
  230. DEBUG_ECHOLNPAIR("(1) Park extruder ", int(active_extruder));
  231. DEBUG_POS("Moving ParkPos", current_position);
  232. }
  233. fast_line_to_current(X_AXIS);
  234. // STEP 2
  235. planner.synchronize();
  236. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("(2) Disengage magnet");
  237. pe_deactivate_solenoid(active_extruder);
  238. // STEP 3
  239. current_position.x += active_extruder ? -10 : 10; // move 10mm away from parked extruder
  240. if (DEBUGGING(LEVELING)) {
  241. DEBUG_ECHOLNPGM("(3) Move near new extruder");
  242. DEBUG_POS("Move away from parked extruder", current_position);
  243. }
  244. fast_line_to_current(X_AXIS);
  245. // STEP 4
  246. planner.synchronize();
  247. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("(4) Engage magnetic field");
  248. #if ENABLED(PARKING_EXTRUDER_SOLENOIDS_INVERT)
  249. pe_activate_solenoid(active_extruder); // Just save power for inverted magnets
  250. #endif
  251. pe_activate_solenoid(new_tool);
  252. // STEP 5
  253. current_position.x = grabpos + (new_tool ? -10 : 10);
  254. fast_line_to_current(X_AXIS);
  255. current_position.x = grabpos;
  256. if (DEBUGGING(LEVELING)) {
  257. planner.synchronize();
  258. DEBUG_POS("(5) Unpark extruder", current_position);
  259. }
  260. slow_line_to_current(X_AXIS);
  261. // STEP 6
  262. current_position.x = midpos
  263. #if HAS_HOTEND_OFFSET
  264. - hotend_offset[new_tool].x
  265. #endif
  266. ;
  267. if (DEBUGGING(LEVELING)) {
  268. planner.synchronize();
  269. DEBUG_POS("(6) Move midway between hotends", current_position);
  270. }
  271. fast_line_to_current(X_AXIS);
  272. planner.synchronize(); // Always sync the final move
  273. if (DEBUGGING(LEVELING)) DEBUG_POS("PE Tool-Change done.", current_position);
  274. }
  275. else { // nomove == true
  276. // Only engage magnetic field for new extruder
  277. pe_activate_solenoid(new_tool);
  278. #if ENABLED(PARKING_EXTRUDER_SOLENOIDS_INVERT)
  279. pe_activate_solenoid(active_extruder); // Just save power for inverted magnets
  280. #endif
  281. }
  282. }
  283. #endif // PARKING_EXTRUDER
  284. #if ENABLED(SWITCHING_TOOLHEAD)
  285. inline void switching_toolhead_tool_change(const uint8_t new_tool, bool no_move/*=false*/) {
  286. if (no_move) return;
  287. constexpr uint16_t angles[2] = SWITCHING_TOOLHEAD_SERVO_ANGLES;
  288. constexpr float toolheadposx[] = SWITCHING_TOOLHEAD_X_POS;
  289. const float placexpos = toolheadposx[active_extruder],
  290. grabxpos = toolheadposx[new_tool];
  291. /**
  292. * 1. Move to switch position of current toolhead
  293. * 2. Unlock tool and drop it in the dock
  294. * 3. Move to the new toolhead
  295. * 4. Grab and lock the new toolhead
  296. */
  297. // 1. Move to switch position of current toolhead
  298. if (DEBUGGING(LEVELING)) DEBUG_POS("Start ST Tool-Change", current_position);
  299. current_position.x = placexpos;
  300. if (DEBUGGING(LEVELING)) {
  301. DEBUG_ECHOLNPAIR("(1) Place old tool ", int(active_extruder));
  302. DEBUG_POS("Move X SwitchPos", current_position);
  303. }
  304. fast_line_to_current(X_AXIS);
  305. current_position.y = SWITCHING_TOOLHEAD_Y_POS - (SWITCHING_TOOLHEAD_Y_SECURITY);
  306. if (DEBUGGING(LEVELING)) {
  307. planner.synchronize();
  308. DEBUG_POS("Move Y SwitchPos + Security", current_position);
  309. }
  310. fast_line_to_current(Y_AXIS);
  311. // 2. Unlock tool and drop it in the dock
  312. planner.synchronize();
  313. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("(2) Unlock and Place Toolhead");
  314. MOVE_SERVO(SWITCHING_TOOLHEAD_SERVO_NR, angles[1]);
  315. safe_delay(500);
  316. current_position.y = SWITCHING_TOOLHEAD_Y_POS;
  317. if (DEBUGGING(LEVELING)) DEBUG_POS("Move Y SwitchPos", current_position);
  318. slow_line_to_current(Y_AXIS);
  319. // Wait for move to complete, then another 0.2s
  320. planner.synchronize();
  321. safe_delay(200);
  322. current_position.y -= SWITCHING_TOOLHEAD_Y_CLEAR;
  323. if (DEBUGGING(LEVELING)) DEBUG_POS("Move back Y clear", current_position);
  324. fast_line_to_current(Y_AXIS); // move away from docked toolhead
  325. // 3. Move to the new toolhead
  326. current_position.x = grabxpos;
  327. if (DEBUGGING(LEVELING)) {
  328. planner.synchronize();
  329. DEBUG_ECHOLNPGM("(3) Move to new toolhead position");
  330. DEBUG_POS("Move to new toolhead X", current_position);
  331. }
  332. fast_line_to_current(X_AXIS);
  333. current_position.y = SWITCHING_TOOLHEAD_Y_POS - (SWITCHING_TOOLHEAD_Y_SECURITY);
  334. if (DEBUGGING(LEVELING)) {
  335. planner.synchronize();
  336. DEBUG_POS("Move Y SwitchPos + Security", current_position);
  337. }
  338. fast_line_to_current(Y_AXIS);
  339. // 4. Grab and lock the new toolhead
  340. current_position.y = SWITCHING_TOOLHEAD_Y_POS;
  341. if (DEBUGGING(LEVELING)) {
  342. planner.synchronize();
  343. DEBUG_ECHOLNPGM("(4) Grab and lock new toolhead");
  344. DEBUG_POS("Move Y SwitchPos", current_position);
  345. }
  346. slow_line_to_current(Y_AXIS);
  347. // Wait for move to finish, pause 0.2s, move servo, pause 0.5s
  348. planner.synchronize();
  349. safe_delay(200);
  350. MOVE_SERVO(SWITCHING_TOOLHEAD_SERVO_NR, angles[0]);
  351. safe_delay(500);
  352. current_position.y -= SWITCHING_TOOLHEAD_Y_CLEAR;
  353. if (DEBUGGING(LEVELING)) DEBUG_POS("Move back Y clear", current_position);
  354. fast_line_to_current(Y_AXIS); // Move away from docked toolhead
  355. planner.synchronize(); // Always sync the final move
  356. if (DEBUGGING(LEVELING)) DEBUG_POS("ST Tool-Change done.", current_position);
  357. }
  358. #elif ENABLED(MAGNETIC_SWITCHING_TOOLHEAD)
  359. inline void magnetic_switching_toolhead_tool_change(const uint8_t new_tool, bool no_move/*=false*/) {
  360. if (no_move) return;
  361. constexpr float toolheadposx[] = SWITCHING_TOOLHEAD_X_POS,
  362. toolheadclearx[] = SWITCHING_TOOLHEAD_X_SECURITY;
  363. const float placexpos = toolheadposx[active_extruder],
  364. placexclear = toolheadclearx[active_extruder],
  365. grabxpos = toolheadposx[new_tool],
  366. grabxclear = toolheadclearx[new_tool];
  367. /**
  368. * 1. Move to switch position of current toolhead
  369. * 2. Release and place toolhead in the dock
  370. * 3. Move to the new toolhead
  371. * 4. Grab the new toolhead and move to security position
  372. */
  373. if (DEBUGGING(LEVELING)) DEBUG_POS("Start MST Tool-Change", current_position);
  374. // 1. Move to switch position current toolhead
  375. current_position.y = SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_CLEAR;
  376. if (DEBUGGING(LEVELING)) {
  377. SERIAL_ECHOLNPAIR("(1) Place old tool ", int(active_extruder));
  378. DEBUG_POS("Move Y SwitchPos + Security", current_position);
  379. }
  380. fast_line_to_current(Y_AXIS);
  381. current_position.x = placexclear;
  382. if (DEBUGGING(LEVELING)) {
  383. planner.synchronize();
  384. DEBUG_POS("Move X SwitchPos + Security", current_position);
  385. }
  386. fast_line_to_current(X_AXIS);
  387. current_position.y = SWITCHING_TOOLHEAD_Y_POS;
  388. if (DEBUGGING(LEVELING)) {
  389. planner.synchronize();
  390. DEBUG_POS("Move Y SwitchPos", current_position);
  391. }
  392. fast_line_to_current(Y_AXIS);
  393. current_position.x = placexpos;
  394. if (DEBUGGING(LEVELING)) {
  395. planner.synchronize();
  396. DEBUG_POS("Move X SwitchPos", current_position);
  397. }
  398. line_to_current_position(planner.settings.max_feedrate_mm_s[X_AXIS] * 0.25f);
  399. // 2. Release and place toolhead in the dock
  400. if (DEBUGGING(LEVELING)) {
  401. planner.synchronize();
  402. DEBUG_ECHOLNPGM("(2) Release and Place Toolhead");
  403. }
  404. current_position.y = SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_RELEASE;
  405. if (DEBUGGING(LEVELING)) DEBUG_POS("Move Y SwitchPos + Release", current_position);
  406. line_to_current_position(planner.settings.max_feedrate_mm_s[Y_AXIS] * 0.1f);
  407. current_position.y = SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_SECURITY;
  408. if (DEBUGGING(LEVELING)) {
  409. planner.synchronize();
  410. DEBUG_POS("Move Y SwitchPos + Security", current_position);
  411. }
  412. line_to_current_position(planner.settings.max_feedrate_mm_s[Y_AXIS]);
  413. // 3. Move to new toolhead position
  414. if (DEBUGGING(LEVELING)) {
  415. planner.synchronize();
  416. DEBUG_ECHOLNPGM("(3) Move to new toolhead position");
  417. }
  418. current_position.x = grabxpos;
  419. if (DEBUGGING(LEVELING)) DEBUG_POS("Move to new toolhead X", current_position);
  420. fast_line_to_current(X_AXIS);
  421. // 4. Grab the new toolhead and move to security position
  422. if (DEBUGGING(LEVELING)) {
  423. planner.synchronize();
  424. DEBUG_ECHOLNPGM("(4) Grab new toolhead, move to security position");
  425. }
  426. current_position.y = SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_RELEASE;
  427. if (DEBUGGING(LEVELING)) DEBUG_POS("Move Y SwitchPos + Release", current_position);
  428. line_to_current_position(planner.settings.max_feedrate_mm_s[Y_AXIS]);
  429. current_position.y = SWITCHING_TOOLHEAD_Y_POS;
  430. if (DEBUGGING(LEVELING)) {
  431. planner.synchronize();
  432. DEBUG_POS("Move Y SwitchPos", current_position);
  433. }
  434. _line_to_current(Y_AXIS, 0.2f);
  435. #if ENABLED(PRIME_BEFORE_REMOVE) && (SWITCHING_TOOLHEAD_PRIME_MM || SWITCHING_TOOLHEAD_RETRACT_MM)
  436. #if SWITCHING_TOOLHEAD_PRIME_MM
  437. current_position.e += SWITCHING_TOOLHEAD_PRIME_MM;
  438. planner.buffer_line(current_position, MMM_TO_MMS(SWITCHING_TOOLHEAD_PRIME_FEEDRATE), new_tool);
  439. #endif
  440. #if SWITCHING_TOOLHEAD_RETRACT_MM
  441. current_position.e -= SWITCHING_TOOLHEAD_RETRACT_MM;
  442. planner.buffer_line(current_position, MMM_TO_MMS(SWITCHING_TOOLHEAD_RETRACT_FEEDRATE), new_tool);
  443. #endif
  444. #else
  445. planner.synchronize();
  446. safe_delay(100); // Give switch time to settle
  447. #endif
  448. current_position.x = grabxclear;
  449. if (DEBUGGING(LEVELING)) DEBUG_POS("Move to new toolhead X + Security", current_position);
  450. _line_to_current(X_AXIS, 0.1f);
  451. planner.synchronize();
  452. safe_delay(100); // Give switch time to settle
  453. current_position.y += SWITCHING_TOOLHEAD_Y_CLEAR;
  454. if (DEBUGGING(LEVELING)) DEBUG_POS("Move back Y clear", current_position);
  455. fast_line_to_current(Y_AXIS); // move away from docked toolhead
  456. planner.synchronize(); // Always sync last tool-change move
  457. if (DEBUGGING(LEVELING)) DEBUG_POS("MST Tool-Change done.", current_position);
  458. }
  459. #elif ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  460. inline void est_activate_solenoid() { OUT_WRITE(SOL0_PIN, HIGH); }
  461. inline void est_deactivate_solenoid() { OUT_WRITE(SOL0_PIN, LOW); }
  462. void est_init() { est_activate_solenoid(); }
  463. inline void em_switching_toolhead_tool_change(const uint8_t new_tool, bool no_move) {
  464. if (no_move) return;
  465. constexpr float toolheadposx[] = SWITCHING_TOOLHEAD_X_POS;
  466. const float placexpos = toolheadposx[active_extruder],
  467. grabxpos = toolheadposx[new_tool];
  468. const xyz_pos_t &hoffs = hotend_offset[active_extruder];
  469. /**
  470. * 1. Raise Z-Axis to give enough clearance
  471. * 2. Move to position near active extruder parking
  472. * 3. Move gently to park position of active extruder
  473. * 4. Disengage magnetic field, wait for delay
  474. * 5. Leave extruder and move to position near new extruder parking
  475. * 6. Move gently to park position of new extruder
  476. * 7. Engage magnetic field for new extruder parking
  477. * 8. Unpark extruder
  478. * 9. Apply Z hotend offset to current position
  479. */
  480. if (DEBUGGING(LEVELING)) DEBUG_POS("Start EMST Tool-Change", current_position);
  481. // 1. Raise Z-Axis to give enough clearance
  482. current_position.z += SWITCHING_TOOLHEAD_Z_HOP;
  483. if (DEBUGGING(LEVELING)) DEBUG_POS("(1) Raise Z-Axis ", current_position);
  484. fast_line_to_current(Z_AXIS);
  485. // 2. Move to position near active extruder parking
  486. if (DEBUGGING(LEVELING)) {
  487. planner.synchronize();
  488. SERIAL_ECHOLNPAIR("(2) Move near active extruder parking", active_extruder);
  489. DEBUG_POS("Moving ParkPos", current_position);
  490. }
  491. current_position.set(hoffs.x + placexpos,
  492. hoffs.y + SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_CLEAR);
  493. fast_line_to_current(X_AXIS);
  494. // 3. Move gently to park position of active extruder
  495. if (DEBUGGING(LEVELING)) {
  496. planner.synchronize();
  497. SERIAL_ECHOLNPAIR("(3) Move gently to park position of active extruder", active_extruder);
  498. DEBUG_POS("Moving ParkPos", current_position);
  499. }
  500. current_position.y -= SWITCHING_TOOLHEAD_Y_CLEAR;
  501. slow_line_to_current(Y_AXIS);
  502. // 4. Disengage magnetic field, wait for delay
  503. planner.synchronize();
  504. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("(4) Disengage magnet");
  505. est_deactivate_solenoid();
  506. // 5. Leave extruder and move to position near new extruder parking
  507. if (DEBUGGING(LEVELING)) {
  508. DEBUG_ECHOLNPGM("(5) Move near new extruder parking");
  509. DEBUG_POS("Moving ParkPos", current_position);
  510. }
  511. current_position.y += SWITCHING_TOOLHEAD_Y_CLEAR;
  512. slow_line_to_current(Y_AXIS);
  513. current_position.set(hoffs.x + grabxpos,
  514. hoffs.y + SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_CLEAR);
  515. fast_line_to_current(X_AXIS);
  516. // 6. Move gently to park position of new extruder
  517. current_position.y -= SWITCHING_TOOLHEAD_Y_CLEAR;
  518. if (DEBUGGING(LEVELING)) {
  519. planner.synchronize();
  520. DEBUG_ECHOLNPGM("(6) Move near new extruder");
  521. }
  522. slow_line_to_current(Y_AXIS);
  523. // 7. Engage magnetic field for new extruder parking
  524. planner.synchronize();
  525. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("(7) Engage magnetic field");
  526. est_activate_solenoid();
  527. // 8. Unpark extruder
  528. current_position.y += SWITCHING_TOOLHEAD_Y_CLEAR;
  529. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("(8) Unpark extruder");
  530. slow_line_to_current(X_AXIS);
  531. planner.synchronize(); // Always sync the final move
  532. // 9. Apply Z hotend offset to current position
  533. if (DEBUGGING(LEVELING)) DEBUG_POS("(9) Applying Z-offset", current_position);
  534. current_position.z += hoffs.z - hotend_offset[new_tool].z;
  535. if (DEBUGGING(LEVELING)) DEBUG_POS("EMST Tool-Change done.", current_position);
  536. }
  537. #endif // ELECTROMAGNETIC_SWITCHING_TOOLHEAD
  538. #if EXTRUDERS
  539. inline void invalid_extruder_error(const uint8_t e) {
  540. SERIAL_ECHO_START();
  541. SERIAL_CHAR('T'); SERIAL_ECHO(int(e));
  542. SERIAL_CHAR(' '); SERIAL_ECHOLNPGM(MSG_INVALID_EXTRUDER);
  543. }
  544. #endif
  545. #if ENABLED(DUAL_X_CARRIAGE)
  546. inline void dualx_tool_change(const uint8_t new_tool, bool &no_move) {
  547. if (DEBUGGING(LEVELING)) {
  548. DEBUG_ECHOPGM("Dual X Carriage Mode ");
  549. switch (dual_x_carriage_mode) {
  550. case DXC_FULL_CONTROL_MODE: DEBUG_ECHOLNPGM("FULL_CONTROL"); break;
  551. case DXC_AUTO_PARK_MODE: DEBUG_ECHOLNPGM("AUTO_PARK"); break;
  552. case DXC_DUPLICATION_MODE: DEBUG_ECHOLNPGM("DUPLICATION"); break;
  553. case DXC_MIRRORED_MODE: DEBUG_ECHOLNPGM("MIRRORED"); break;
  554. }
  555. }
  556. const float xhome = x_home_pos(active_extruder);
  557. if (dual_x_carriage_mode == DXC_AUTO_PARK_MODE
  558. && IsRunning() && !no_move
  559. && (delayed_move_time || current_position.x != xhome)
  560. ) {
  561. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("MoveX to ", xhome);
  562. // Park old head
  563. current_position.x = xhome;
  564. line_to_current_position(planner.settings.max_feedrate_mm_s[X_AXIS]);
  565. planner.synchronize();
  566. }
  567. // Activate the new extruder ahead of calling set_axis_is_at_home!
  568. active_extruder = new_tool;
  569. // This function resets the max/min values - the current position may be overwritten below.
  570. set_axis_is_at_home(X_AXIS);
  571. if (DEBUGGING(LEVELING)) DEBUG_POS("New Extruder", current_position);
  572. switch (dual_x_carriage_mode) {
  573. case DXC_FULL_CONTROL_MODE:
  574. // New current position is the position of the activated extruder
  575. current_position.x = inactive_extruder_x_pos;
  576. // Save the inactive extruder's position (from the old current_position)
  577. inactive_extruder_x_pos = destination.x;
  578. break;
  579. case DXC_AUTO_PARK_MODE:
  580. // record current raised toolhead position for use by unpark
  581. raised_parked_position = current_position;
  582. active_extruder_parked = true;
  583. delayed_move_time = 0;
  584. break;
  585. default:
  586. break;
  587. }
  588. if (DEBUGGING(LEVELING)) {
  589. DEBUG_ECHOLNPAIR("Active extruder parked: ", active_extruder_parked ? "yes" : "no");
  590. DEBUG_POS("New extruder (parked)", current_position);
  591. }
  592. }
  593. #endif // DUAL_X_CARRIAGE
  594. /**
  595. * Perform a tool-change, which may result in moving the
  596. * previous tool out of the way and the new tool into place.
  597. */
  598. void tool_change(const uint8_t new_tool, bool no_move/*=false*/) {
  599. #if ENABLED(MAGNETIC_SWITCHING_TOOLHEAD)
  600. if (new_tool == active_extruder) return;
  601. #endif
  602. #if ENABLED(MIXING_EXTRUDER)
  603. UNUSED(no_move);
  604. if (new_tool >= MIXING_VIRTUAL_TOOLS)
  605. return invalid_extruder_error(new_tool);
  606. #if MIXING_VIRTUAL_TOOLS > 1
  607. // T0-Tnnn: Switch virtual tool by changing the index to the mix
  608. mixer.T(new_tool);
  609. #endif
  610. #elif ENABLED(PRUSA_MMU2)
  611. UNUSED(no_move);
  612. mmu2.tool_change(new_tool);
  613. #elif EXTRUDERS == 0
  614. // Nothing to do
  615. UNUSED(new_tool); UNUSED(no_move);
  616. #elif EXTRUDERS < 2
  617. UNUSED(no_move);
  618. if (new_tool) invalid_extruder_error(new_tool);
  619. return;
  620. #else // EXTRUDERS > 1
  621. planner.synchronize();
  622. #if ENABLED(DUAL_X_CARRIAGE) // Only T0 allowed if the Printer is in DXC_DUPLICATION_MODE or DXC_MIRRORED_MODE
  623. if (new_tool != 0 && dxc_is_duplicating())
  624. return invalid_extruder_error(new_tool);
  625. #endif
  626. if (new_tool >= EXTRUDERS)
  627. return invalid_extruder_error(new_tool);
  628. if (!no_move && !all_axes_homed()) {
  629. no_move = true;
  630. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("No move (not homed)");
  631. }
  632. #if HAS_LCD_MENU
  633. ui.return_to_status();
  634. #endif
  635. #if ENABLED(DUAL_X_CARRIAGE)
  636. const bool idex_full_control = dual_x_carriage_mode == DXC_FULL_CONTROL_MODE;
  637. #else
  638. constexpr bool idex_full_control = false;
  639. #endif
  640. const uint8_t old_tool = active_extruder;
  641. const bool can_move_away = !no_move && !idex_full_control;
  642. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  643. const bool should_swap = can_move_away && toolchange_settings.swap_length;
  644. #if ENABLED(PREVENT_COLD_EXTRUSION)
  645. const bool too_cold = !DEBUGGING(DRYRUN) && (thermalManager.targetTooColdToExtrude(old_tool) || thermalManager.targetTooColdToExtrude(new_tool));
  646. #else
  647. constexpr bool too_cold = false;
  648. #endif
  649. if (should_swap) {
  650. if (too_cold) {
  651. SERIAL_ECHO_MSG(MSG_ERR_HOTEND_TOO_COLD);
  652. #if ENABLED(SINGLENOZZLE)
  653. active_extruder = new_tool;
  654. return;
  655. #endif
  656. }
  657. else {
  658. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  659. do_pause_e_move(-toolchange_settings.swap_length, MMM_TO_MMS(toolchange_settings.retract_speed));
  660. #else
  661. current_position.e -= toolchange_settings.swap_length / planner.e_factor[old_tool];
  662. planner.buffer_line(current_position, MMM_TO_MMS(toolchange_settings.retract_speed), old_tool);
  663. planner.synchronize();
  664. #endif
  665. }
  666. }
  667. #endif // TOOLCHANGE_FILAMENT_SWAP
  668. #if HAS_LEVELING
  669. // Set current position to the physical position
  670. TEMPORARY_BED_LEVELING_STATE(false);
  671. #endif
  672. if (new_tool != old_tool) {
  673. #if SWITCHING_NOZZLE_TWO_SERVOS
  674. raise_nozzle(old_tool);
  675. #endif
  676. REMEMBER(fr, feedrate_mm_s, XY_PROBE_FEEDRATE_MM_S);
  677. #if HAS_SOFTWARE_ENDSTOPS
  678. #if HAS_HOTEND_OFFSET
  679. #define _EXT_ARGS , old_tool, new_tool
  680. #else
  681. #define _EXT_ARGS
  682. #endif
  683. update_software_endstops(X_AXIS _EXT_ARGS);
  684. #if DISABLED(DUAL_X_CARRIAGE)
  685. update_software_endstops(Y_AXIS _EXT_ARGS);
  686. update_software_endstops(Z_AXIS _EXT_ARGS);
  687. #endif
  688. #endif
  689. destination = current_position;
  690. #if DISABLED(SWITCHING_NOZZLE)
  691. if (can_move_away) {
  692. // Do a small lift to avoid the workpiece in the move back (below)
  693. current_position.z += toolchange_settings.z_raise;
  694. #if HAS_SOFTWARE_ENDSTOPS
  695. NOMORE(current_position.z, soft_endstop.max.z);
  696. #endif
  697. fast_line_to_current(Z_AXIS);
  698. #if ENABLED(TOOLCHANGE_PARK)
  699. current_position = toolchange_settings.change_point;
  700. #endif
  701. planner.buffer_line(current_position, feedrate_mm_s, old_tool);
  702. planner.synchronize();
  703. }
  704. #endif
  705. #if HAS_HOTEND_OFFSET
  706. xyz_pos_t diff = hotend_offset[new_tool];
  707. #if ENABLED(DUAL_X_CARRIAGE)
  708. diff.x = 0;
  709. #endif
  710. #else
  711. constexpr xyz_pos_t diff{0};
  712. #endif
  713. #if ENABLED(DUAL_X_CARRIAGE)
  714. dualx_tool_change(new_tool, no_move);
  715. #elif ENABLED(PARKING_EXTRUDER) // Dual Parking extruder
  716. parking_extruder_tool_change(new_tool, no_move);
  717. #elif ENABLED(MAGNETIC_PARKING_EXTRUDER) // Magnetic Parking extruder
  718. magnetic_parking_extruder_tool_change(new_tool);
  719. #elif ENABLED(SWITCHING_TOOLHEAD) // Switching Toolhead
  720. switching_toolhead_tool_change(new_tool, no_move);
  721. #elif ENABLED(MAGNETIC_SWITCHING_TOOLHEAD) // Magnetic Switching Toolhead
  722. magnetic_switching_toolhead_tool_change(new_tool, no_move);
  723. #elif ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD) // Magnetic Switching ToolChanger
  724. em_switching_toolhead_tool_change(new_tool, no_move);
  725. #elif ENABLED(SWITCHING_NOZZLE) && !SWITCHING_NOZZLE_TWO_SERVOS // Switching Nozzle (single servo)
  726. // Raise by a configured distance to avoid workpiece, except with
  727. // SWITCHING_NOZZLE_TWO_SERVOS, as both nozzles will lift instead.
  728. current_position.z += _MAX(-diff.z, 0.0) + toolchange_settings.z_raise;
  729. #if HAS_SOFTWARE_ENDSTOPS
  730. NOMORE(current_position.z, soft_endstop.max.z);
  731. #endif
  732. if (!no_move) fast_line_to_current(Z_AXIS);
  733. move_nozzle_servo(new_tool);
  734. #endif
  735. #if DISABLED(DUAL_X_CARRIAGE)
  736. active_extruder = new_tool; // Set the new active extruder
  737. #endif
  738. // The newly-selected extruder XYZ is actually at...
  739. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("Offset Tool XY by { ", diff.x, ", ", diff.y, ", ", diff.z, " }");
  740. current_position += diff;
  741. // Tell the planner the new "current position"
  742. sync_plan_position();
  743. #if ENABLED(DELTA)
  744. //LOOP_XYZ(i) update_software_endstops(i); // or modify the constrain function
  745. const bool safe_to_move = current_position.z < delta_clip_start_height - 1;
  746. #else
  747. constexpr bool safe_to_move = true;
  748. #endif
  749. // Return to position and lower again
  750. if (safe_to_move && !no_move && IsRunning()) {
  751. #if ENABLED(SINGLENOZZLE)
  752. #if FAN_COUNT > 0
  753. singlenozzle_fan_speed[old_tool] = thermalManager.fan_speed[0];
  754. thermalManager.fan_speed[0] = singlenozzle_fan_speed[new_tool];
  755. #endif
  756. singlenozzle_temp[old_tool] = thermalManager.temp_hotend[0].target;
  757. if (singlenozzle_temp[new_tool] && singlenozzle_temp[new_tool] != singlenozzle_temp[old_tool]) {
  758. thermalManager.setTargetHotend(singlenozzle_temp[new_tool], 0);
  759. #if HAS_DISPLAY
  760. thermalManager.set_heating_message(0);
  761. #endif
  762. (void)thermalManager.wait_for_hotend(0, false); // Wait for heating or cooling
  763. }
  764. #endif
  765. #if ENABLED(TOOLCHANGE_FILAMENT_SWAP)
  766. if (should_swap && !too_cold) {
  767. #if ENABLED(ADVANCED_PAUSE_FEATURE)
  768. do_pause_e_move(toolchange_settings.swap_length, MMM_TO_MMS(toolchange_settings.prime_speed));
  769. do_pause_e_move(toolchange_settings.extra_prime, ADVANCED_PAUSE_PURGE_FEEDRATE);
  770. #else
  771. current_position.e += toolchange_settings.swap_length / planner.e_factor[new_tool];
  772. planner.buffer_line(current_position, MMM_TO_MMS(toolchange_settings.prime_speed), new_tool);
  773. current_position.e += toolchange_settings.extra_prime / planner.e_factor[new_tool];
  774. planner.buffer_line(current_position, MMM_TO_MMS(toolchange_settings.prime_speed * 0.2f), new_tool);
  775. #endif
  776. planner.synchronize();
  777. planner.set_e_position_mm((destination.e = current_position.e = current_position.e - (TOOLCHANGE_FIL_EXTRA_PRIME)));
  778. }
  779. #endif
  780. // Prevent a move outside physical bounds
  781. #if ENABLED(MAGNETIC_SWITCHING_TOOLHEAD)
  782. // If the original position is within tool store area, go to X origin at once
  783. if (destination.y < SWITCHING_TOOLHEAD_Y_POS + SWITCHING_TOOLHEAD_Y_CLEAR) {
  784. current_position.x = 0;
  785. planner.buffer_line(current_position, planner.settings.max_feedrate_mm_s[X_AXIS], new_tool);
  786. planner.synchronize();
  787. }
  788. #else
  789. apply_motion_limits(destination);
  790. #endif
  791. // Should the nozzle move back to the old position?
  792. if (can_move_away) {
  793. #if ENABLED(TOOLCHANGE_NO_RETURN)
  794. // Just move back down
  795. if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Move back Z only");
  796. do_blocking_move_to_z(destination.z, planner.settings.max_feedrate_mm_s[Z_AXIS]);
  797. #else
  798. // Move back to the original (or adjusted) position
  799. if (DEBUGGING(LEVELING)) DEBUG_POS("Move back", destination);
  800. do_blocking_move_to(destination);
  801. #endif
  802. }
  803. else if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Move back skipped");
  804. #if ENABLED(DUAL_X_CARRIAGE)
  805. active_extruder_parked = false;
  806. #endif
  807. }
  808. #if ENABLED(SWITCHING_NOZZLE)
  809. else {
  810. // Move back down. (Including when the new tool is higher.)
  811. do_blocking_move_to_z(destination.z, planner.settings.max_feedrate_mm_s[Z_AXIS]);
  812. }
  813. #endif
  814. #if ENABLED(PRUSA_MMU2)
  815. mmu2.tool_change(new_tool);
  816. #endif
  817. #if SWITCHING_NOZZLE_TWO_SERVOS
  818. lower_nozzle(new_tool);
  819. #endif
  820. } // (new_tool != old_tool)
  821. planner.synchronize();
  822. #if ENABLED(EXT_SOLENOID) && DISABLED(PARKING_EXTRUDER)
  823. disable_all_solenoids();
  824. enable_solenoid_on_active_extruder();
  825. #endif
  826. #if ENABLED(MK2_MULTIPLEXER)
  827. if (new_tool >= E_STEPPERS) return invalid_extruder_error(new_tool);
  828. select_multiplexed_stepper(new_tool);
  829. #endif
  830. #if DO_SWITCH_EXTRUDER
  831. planner.synchronize();
  832. move_extruder_servo(active_extruder);
  833. #endif
  834. #if HAS_FANMUX
  835. fanmux_switch(active_extruder);
  836. #endif
  837. SERIAL_ECHO_START();
  838. SERIAL_ECHOLNPAIR(MSG_ACTIVE_EXTRUDER, int(active_extruder));
  839. #endif // EXTRUDERS > 1
  840. }