My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

tool_change.cpp 31KB

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