Advertisement
Guest User

Untitled

a guest
Jul 20th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 130.84 KB | None | 0 0
  1. /* -*- c++ -*- */
  2.  
  3. /*
  4. Reprap firmware based on Sprinter and grbl.
  5. Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  6.  
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20.  
  21. /*
  22. This firmware is a mashup between Sprinter and grbl.
  23. (https://github.com/kliment/Sprinter)
  24. (https://github.com/simen/grbl/tree)
  25.  
  26. It has preliminary support for Matthew Roberts advance algorithm
  27. http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
  28. */
  29.  
  30. #include "Marlin.h"
  31.  
  32. #ifdef ENABLE_AUTO_BED_LEVELING
  33. #include "vector_3.h"
  34. #ifdef AUTO_BED_LEVELING_GRID
  35. #include "qr_solve.h"
  36. #endif
  37. #endif // ENABLE_AUTO_BED_LEVELING
  38.  
  39. #include "ultralcd.h"
  40. #include "planner.h"
  41. #include "stepper.h"
  42. #include "temperature.h"
  43. #include "motion_control.h"
  44. #include "cardreader.h"
  45. #include "watchdog.h"
  46. #include "ConfigurationStore.h"
  47. #include "language.h"
  48. #include "pins_arduino.h"
  49. #include "math.h"
  50.  
  51. #ifdef BLINKM
  52. #include "BlinkM.h"
  53. #include "Wire.h"
  54. #endif
  55.  
  56. #if NUM_SERVOS > 0
  57. #include "Servo.h"
  58. #endif
  59.  
  60. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  61. #include <SPI.h>
  62. #endif
  63.  
  64. #define VERSION_STRING "1.0.0"
  65.  
  66. // look here for descriptions of G-codes: http://linuxcnc.org/handbook/gcode/g-code.html
  67. // http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes
  68.  
  69. //Implemented Codes
  70. //-------------------
  71. // G0 -> G1
  72. // G1 - Coordinated Movement X Y Z E
  73. // G2 - CW ARC
  74. // G3 - CCW ARC
  75. // G4 - Dwell S<seconds> or P<milliseconds>
  76. // G10 - retract filament according to settings of M207
  77. // G11 - retract recover filament according to settings of M208
  78. // G28 - Home all Axis
  79. // G29 - Detailed Z-Probe, probes the bed at 3 or more points. Will fail if you haven't homed yet.
  80. // G30 - Single Z Probe, probes bed at current XY location.
  81. // G90 - Use Absolute Coordinates
  82. // G91 - Use Relative Coordinates
  83. // G92 - Set current position to coordinates given
  84.  
  85. // M Codes
  86. // M0 - Unconditional stop - Wait for user to press a button on the LCD (Only if ULTRA_LCD is enabled)
  87. // M1 - Same as M0
  88. // M17 - Enable/Power all stepper motors
  89. // M18 - Disable all stepper motors; same as M84
  90. // M20 - List SD card
  91. // M21 - Init SD card
  92. // M22 - Release SD card
  93. // M23 - Select SD file (M23 filename.g)
  94. // M24 - Start/resume SD print
  95. // M25 - Pause SD print
  96. // M26 - Set SD position in bytes (M26 S12345)
  97. // M27 - Report SD print status
  98. // M28 - Start SD write (M28 filename.g)
  99. // M29 - Stop SD write
  100. // M30 - Delete file from SD (M30 filename.g)
  101. // M31 - Output time since last M109 or SD card start to serial
  102. // M32 - Select file and start SD print (Can be used _while_ printing from SD card files):
  103. // syntax "M32 /path/filename#", or "M32 S<startpos bytes> !filename#"
  104. // Call gcode file : "M32 P !filename#" and return to caller file after finishing (similar to #include).
  105. // The '#' is necessary when calling from within sd files, as it stops buffer prereading
  106. // M42 - Change pin status via gcode Use M42 Px Sy to set pin x to value y, when omitting Px the onboard led will be used.
  107. // M80 - Turn on Power Supply
  108. // M81 - Turn off Power Supply
  109. // M82 - Set E codes absolute (default)
  110. // M83 - Set E codes relative while in Absolute Coordinates (G90) mode
  111. // M84 - Disable steppers until next move,
  112. // or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
  113. // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  114. // M92 - Set axis_steps_per_unit - same syntax as G92
  115. // M104 - Set extruder target temp
  116. // M105 - Read current temp
  117. // M106 - Fan on
  118. // M107 - Fan off
  119. // M109 - Sxxx Wait for extruder current temp to reach target temp. Waits only when heating
  120. // Rxxx Wait for extruder current temp to reach target temp. Waits when heating and cooling
  121. // IF AUTOTEMP is enabled, S<mintemp> B<maxtemp> F<factor>. Exit autotemp by any M109 without F
  122. // M112 - Emergency stop
  123. // M114 - Output current position to serial port
  124. // M115 - Capabilities string
  125. // M117 - display message
  126. // M119 - Output Endstop status to serial port
  127. // M126 - Solenoid Air Valve Open (BariCUDA support by jmil)
  128. // M127 - Solenoid Air Valve Closed (BariCUDA vent to atmospheric pressure by jmil)
  129. // M128 - EtoP Open (BariCUDA EtoP = electricity to air pressure transducer by jmil)
  130. // M129 - EtoP Closed (BariCUDA EtoP = electricity to air pressure transducer by jmil)
  131. // M140 - Set bed target temp
  132. // M150 - Set BlinkM Color Output R: Red<0-255> U(!): Green<0-255> B: Blue<0-255> over i2c, G for green does not work.
  133. // M190 - Sxxx Wait for bed current temp to reach target temp. Waits only when heating
  134. // Rxxx Wait for bed current temp to reach target temp. Waits when heating and cooling
  135. // M200 D<millimeters>- set filament diameter and set E axis units to cubic millimeters (use S0 to set back to millimeters).
  136. // M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
  137. // M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
  138. // M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
  139. // M204 - Set default acceleration: S normal moves T filament only moves (M204 S3000 T7000) in mm/sec^2 also sets minimum segment time in ms (B20000) to prevent buffer under-runs and M20 minimum feedrate
  140. // M205 - advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk, E=maximum E jerk
  141. // M206 - set additional homing offset
  142. // M207 - set retract length S[positive mm] F[feedrate mm/min] Z[additional zlift/hop], stays in mm regardless of M200 setting
  143. // M208 - set recover=unretract length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  144. // M209 - S<1=true/0=false> enable automatic retract detect if the slicer did not support G10/11: every normal extrude-only move will be classified as retract depending on the direction.
  145. // M218 - set hotend offset (in mm): T<extruder_number> X<offset_on_X> Y<offset_on_Y>
  146. // M220 S<factor in percent>- set speed factor override percentage
  147. // M221 S<factor in percent>- set extrude factor override percentage
  148. // M226 P<pin number> S<pin state>- Wait until the specified pin reaches the state required
  149. // M240 - Trigger a camera to take a photograph
  150. // M250 - Set LCD contrast C<contrast value> (value 0..63)
  151. // M280 - set servo position absolute. P: servo index, S: angle or microseconds
  152. // M300 - Play beep sound S<frequency Hz> P<duration ms>
  153. // M301 - Set PID parameters P I and D
  154. // M302 - Allow cold extrudes, or set the minimum extrude S<temperature>.
  155. // M303 - PID relay autotune S<temperature> sets the target temperature. (default target temperature = 150C)
  156. // M304 - Set bed PID parameters P I and D
  157. // M400 - Finish all moves
  158. // M401 - Lower z-probe if present
  159. // M402 - Raise z-probe if present
  160. // M500 - stores parameters in EEPROM
  161. // M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
  162. // M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
  163. // M503 - print the current settings (from memory not from EEPROM)
  164. // M540 - Use S[0|1] to enable or disable the stop SD card print on endstop hit (requires ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
  165. // M600 - Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
  166. // M665 - set delta configurations
  167. // M666 - set delta endstop adjustment
  168. // M605 - Set dual x-carriage movement mode: S<mode> [ X<duplication x-offset> R<duplication temp offset> ]
  169. // M907 - Set digital trimpot motor current using axis codes.
  170. // M908 - Control digital trimpot directly.
  171. // M350 - Set microstepping mode.
  172. // M351 - Toggle MS1 MS2 pins directly.
  173. // M928 - Start SD logging (M928 filename.g) - ended by M29
  174. // M999 - Restart after being stopped by error
  175.  
  176. //Stepper Movement Variables
  177.  
  178. //===========================================================================
  179. //=============================imported variables============================
  180. //===========================================================================
  181.  
  182.  
  183. //===========================================================================
  184. //=============================public variables=============================
  185. //===========================================================================
  186. #ifdef SDSUPPORT
  187. CardReader card;
  188. #endif
  189. float homing_feedrate[] = HOMING_FEEDRATE;
  190. bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
  191. int feedmultiply=100; //100->1 200->2
  192. int saved_feedmultiply;
  193. int extrudemultiply=100; //100->1 200->2
  194. int extruder_multiply[EXTRUDERS] = {100
  195. #if EXTRUDERS > 1
  196. , 100
  197. #if EXTRUDERS > 2
  198. , 100
  199. #endif
  200. #endif
  201. };
  202. float volumetric_multiplier[EXTRUDERS] = {1.0
  203. #if EXTRUDERS > 1
  204. , 1.0
  205. #if EXTRUDERS > 2
  206. , 1.0
  207. #endif
  208. #endif
  209. };
  210. float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
  211. float add_homeing[3]={0,0,0};
  212. #ifdef DELTA
  213. float endstop_adj[3]={0,0,0};
  214. #endif
  215. float min_pos[3] = { X_MIN_POS, Y_MIN_POS, Z_MIN_POS };
  216. float max_pos[3] = { X_MAX_POS, Y_MAX_POS, Z_MAX_POS };
  217. bool axis_known_position[3] = {false, false, false};
  218. float zprobe_zoffset;
  219.  
  220. // Extruder offset
  221. #if EXTRUDERS > 1
  222. #ifndef DUAL_X_CARRIAGE
  223. #define NUM_EXTRUDER_OFFSETS 2 // only in XY plane
  224. #else
  225. #define NUM_EXTRUDER_OFFSETS 3 // supports offsets in XYZ plane
  226. #endif
  227. float extruder_offset[NUM_EXTRUDER_OFFSETS][EXTRUDERS] = {
  228. #if defined(EXTRUDER_OFFSET_X) && defined(EXTRUDER_OFFSET_Y)
  229. EXTRUDER_OFFSET_X, EXTRUDER_OFFSET_Y
  230. #endif
  231. };
  232. #endif
  233. uint8_t active_extruder = 0;
  234. int fanSpeed=0;
  235. #ifdef SERVO_ENDSTOPS
  236. int servo_endstops[] = SERVO_ENDSTOPS;
  237. int servo_endstop_angles[] = SERVO_ENDSTOP_ANGLES;
  238. #endif
  239. #ifdef BARICUDA
  240. int ValvePressure=0;
  241. int EtoPPressure=0;
  242. #endif
  243.  
  244. #ifdef FWRETRACT
  245. bool autoretract_enabled=false;
  246. bool retracted[EXTRUDERS]={false
  247. #if EXTRUDERS > 1
  248. , false
  249. #if EXTRUDERS > 2
  250. , false
  251. #endif
  252. #endif
  253. };
  254. bool retracted_swap[EXTRUDERS]={false
  255. #if EXTRUDERS > 1
  256. , false
  257. #if EXTRUDERS > 2
  258. , false
  259. #endif
  260. #endif
  261. };
  262.  
  263. float retract_length = RETRACT_LENGTH;
  264. float retract_length_swap = RETRACT_LENGTH_SWAP;
  265. float retract_feedrate = RETRACT_FEEDRATE;
  266. float retract_zlift = RETRACT_ZLIFT;
  267. float retract_recover_length = RETRACT_RECOVER_LENGTH;
  268. float retract_recover_length_swap = RETRACT_RECOVER_LENGTH_SWAP;
  269. float retract_recover_feedrate = RETRACT_RECOVER_FEEDRATE;
  270. #endif
  271.  
  272. #ifdef ULTIPANEL
  273. #ifdef PS_DEFAULT_OFF
  274. bool powersupply = false;
  275. #else
  276. bool powersupply = true;
  277. #endif
  278. #endif
  279.  
  280. #ifdef DELTA
  281. float delta[3] = {0.0, 0.0, 0.0};
  282. #define SIN_60 0.8660254037844386
  283. #define COS_60 0.5
  284. // these are the default values, can be overriden with M665
  285. float delta_radius= DELTA_RADIUS;
  286. float delta_tower1_x= -SIN_60*delta_radius; // front left tower
  287. float delta_tower1_y= -COS_60*delta_radius;
  288. float delta_tower2_x= SIN_60*delta_radius; // front right tower
  289. float delta_tower2_y= -COS_60*delta_radius;
  290. float delta_tower3_x= 0.0; // back middle tower
  291. float delta_tower3_y= delta_radius;
  292. float delta_diagonal_rod= DELTA_DIAGONAL_ROD;
  293. float delta_diagonal_rod_2= sq(delta_diagonal_rod);
  294. float delta_segments_per_second= DELTA_SEGMENTS_PER_SECOND;
  295. #endif
  296.  
  297. bool cancel_heatup = false ;
  298.  
  299. //===========================================================================
  300. //=============================Private Variables=============================
  301. //===========================================================================
  302. const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
  303. static float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
  304. static float offset[3] = {0.0, 0.0, 0.0};
  305. static bool home_all_axis = true;
  306. static float feedrate = 1500.0, next_feedrate, saved_feedrate;
  307. static long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
  308.  
  309. static bool relative_mode = false; //Determines Absolute or Relative Coordinates
  310.  
  311. static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
  312. static bool fromsd[BUFSIZE];
  313. static int bufindr = 0;
  314. static int bufindw = 0;
  315. static int buflen = 0;
  316. //static int i = 0;
  317. static char serial_char;
  318. static int serial_count = 0;
  319. static boolean comment_mode = false;
  320. static char *strchr_pointer; // just a pointer to find chars in the command string like X, Y, Z, E, etc
  321.  
  322. const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
  323.  
  324. //static float tt = 0;
  325. //static float bt = 0;
  326.  
  327. //Inactivity shutdown variables
  328. static unsigned long previous_millis_cmd = 0;
  329. static unsigned long max_inactive_time = 0;
  330. static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000l;
  331.  
  332. unsigned long starttime=0;
  333. unsigned long stoptime=0;
  334.  
  335. static uint8_t tmp_extruder;
  336.  
  337.  
  338. bool Stopped=false;
  339.  
  340. #if NUM_SERVOS > 0
  341. Servo servos[NUM_SERVOS];
  342. #endif
  343.  
  344. bool CooldownNoWait = true;
  345. bool target_direction;
  346.  
  347. //Insert variables if CHDK is defined
  348. #ifdef CHDK
  349. unsigned long chdkHigh = 0;
  350. boolean chdkActive = false;
  351. #endif
  352.  
  353. //===========================================================================
  354. //=============================Routines======================================
  355. //===========================================================================
  356.  
  357. void get_arc_coordinates();
  358. bool setTargetedHotend(int code);
  359.  
  360. void serial_echopair_P(const char *s_P, float v)
  361. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  362. void serial_echopair_P(const char *s_P, double v)
  363. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  364. void serial_echopair_P(const char *s_P, unsigned long v)
  365. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  366.  
  367. extern "C"{
  368. extern unsigned int __bss_end;
  369. extern unsigned int __heap_start;
  370. extern void *__brkval;
  371.  
  372. int freeMemory() {
  373. int free_memory;
  374.  
  375. if((int)__brkval == 0)
  376. free_memory = ((int)&free_memory) - ((int)&__bss_end);
  377. else
  378. free_memory = ((int)&free_memory) - ((int)__brkval);
  379.  
  380. return free_memory;
  381. }
  382. }
  383.  
  384. //adds an command to the main command buffer
  385. //thats really done in a non-safe way.
  386. //needs overworking someday
  387. void enquecommand(const char *cmd)
  388. {
  389. if(buflen < BUFSIZE)
  390. {
  391. //this is dangerous if a mixing of serial and this happens
  392. strcpy(&(cmdbuffer[bufindw][0]),cmd);
  393. SERIAL_ECHO_START;
  394. SERIAL_ECHOPGM("enqueing \"");
  395. SERIAL_ECHO(cmdbuffer[bufindw]);
  396. SERIAL_ECHOLNPGM("\"");
  397. bufindw= (bufindw + 1)%BUFSIZE;
  398. buflen += 1;
  399. }
  400. }
  401.  
  402. void enquecommand_P(const char *cmd)
  403. {
  404. if(buflen < BUFSIZE)
  405. {
  406. //this is dangerous if a mixing of serial and this happens
  407. strcpy_P(&(cmdbuffer[bufindw][0]),cmd);
  408. SERIAL_ECHO_START;
  409. SERIAL_ECHOPGM("enqueing \"");
  410. SERIAL_ECHO(cmdbuffer[bufindw]);
  411. SERIAL_ECHOLNPGM("\"");
  412. bufindw= (bufindw + 1)%BUFSIZE;
  413. buflen += 1;
  414. }
  415. }
  416.  
  417. void setup_killpin()
  418. {
  419. #if defined(KILL_PIN) && KILL_PIN > -1
  420. pinMode(KILL_PIN,INPUT);
  421. WRITE(KILL_PIN,HIGH);
  422. #endif
  423. }
  424.  
  425. void setup_photpin()
  426. {
  427. #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
  428. SET_OUTPUT(PHOTOGRAPH_PIN);
  429. WRITE(PHOTOGRAPH_PIN, LOW);
  430. #endif
  431. }
  432.  
  433. void setup_powerhold()
  434. {
  435. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  436. SET_OUTPUT(SUICIDE_PIN);
  437. WRITE(SUICIDE_PIN, HIGH);
  438. #endif
  439. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  440. SET_OUTPUT(PS_ON_PIN);
  441. #if defined(PS_DEFAULT_OFF)
  442. WRITE(PS_ON_PIN, PS_ON_ASLEEP);
  443. #else
  444. WRITE(PS_ON_PIN, PS_ON_AWAKE);
  445. #endif
  446. #endif
  447. }
  448.  
  449. void suicide()
  450. {
  451. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  452. SET_OUTPUT(SUICIDE_PIN);
  453. WRITE(SUICIDE_PIN, LOW);
  454. #endif
  455. }
  456.  
  457. void servo_init()
  458. {
  459. #if (NUM_SERVOS >= 1) && defined(SERVO0_PIN) && (SERVO0_PIN > -1)
  460. servos[0].attach(SERVO0_PIN);
  461. #endif
  462. #if (NUM_SERVOS >= 2) && defined(SERVO1_PIN) && (SERVO1_PIN > -1)
  463. servos[1].attach(SERVO1_PIN);
  464. #endif
  465. #if (NUM_SERVOS >= 3) && defined(SERVO2_PIN) && (SERVO2_PIN > -1)
  466. servos[2].attach(SERVO2_PIN);
  467. #endif
  468. #if (NUM_SERVOS >= 4) && defined(SERVO3_PIN) && (SERVO3_PIN > -1)
  469. servos[3].attach(SERVO3_PIN);
  470. #endif
  471. #if (NUM_SERVOS >= 5)
  472. #error "TODO: enter initalisation code for more servos"
  473. #endif
  474.  
  475. // Set position of Servo Endstops that are defined
  476. #ifdef SERVO_ENDSTOPS
  477. for(int8_t i = 0; i < 3; i++)
  478. {
  479. if(servo_endstops[i] > -1) {
  480. servos[servo_endstops[i]].write(servo_endstop_angles[i * 2 + 1]);
  481. }
  482. }
  483. #endif
  484.  
  485. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  486. delay(PROBE_SERVO_DEACTIVATION_DELAY);
  487. servos[servo_endstops[Z_AXIS]].detach();
  488. #endif
  489. }
  490.  
  491. void setup()
  492. {
  493. setup_killpin();
  494. setup_powerhold();
  495. MYSERIAL.begin(BAUDRATE);
  496. SERIAL_PROTOCOLLNPGM("start");
  497. SERIAL_ECHO_START;
  498.  
  499. // Check startup - does nothing if bootloader sets MCUSR to 0
  500. byte mcu = MCUSR;
  501. if(mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  502. if(mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  503. if(mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  504. if(mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  505. if(mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  506. MCUSR=0;
  507.  
  508. SERIAL_ECHOPGM(MSG_MARLIN);
  509. SERIAL_ECHOLNPGM(VERSION_STRING);
  510. #ifdef STRING_VERSION_CONFIG_H
  511. #ifdef STRING_CONFIG_H_AUTHOR
  512. SERIAL_ECHO_START;
  513. SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
  514. SERIAL_ECHOPGM(STRING_VERSION_CONFIG_H);
  515. SERIAL_ECHOPGM(MSG_AUTHOR);
  516. SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
  517. SERIAL_ECHOPGM("Compiled: ");
  518. SERIAL_ECHOLNPGM(__DATE__);
  519. #endif
  520. #endif
  521. SERIAL_ECHO_START;
  522. SERIAL_ECHOPGM(MSG_FREE_MEMORY);
  523. SERIAL_ECHO(freeMemory());
  524. SERIAL_ECHOPGM(MSG_PLANNER_BUFFER_BYTES);
  525. SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
  526. for(int8_t i = 0; i < BUFSIZE; i++)
  527. {
  528. fromsd[i] = false;
  529. }
  530.  
  531. // loads data from EEPROM if available else uses defaults (and resets step acceleration rate)
  532. Config_RetrieveSettings();
  533.  
  534. tp_init(); // Initialize temperature loop
  535. plan_init(); // Initialize planner;
  536. watchdog_init();
  537. st_init(); // Initialize stepper, this enables interrupts!
  538. setup_photpin();
  539. servo_init();
  540.  
  541. lcd_init();
  542. _delay_ms(1000); // wait 1sec to display the splash screen
  543.  
  544. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  545. SET_OUTPUT(CONTROLLERFAN_PIN); //Set pin used for driver cooling fan
  546. #endif
  547.  
  548. #ifdef DIGIPOT_I2C
  549. digipot_i2c_init();
  550. #endif
  551. }
  552.  
  553.  
  554. void loop()
  555. {
  556. if(buflen < (BUFSIZE-1))
  557. get_command();
  558. #ifdef SDSUPPORT
  559. card.checkautostart(false);
  560. #endif
  561. if(buflen)
  562. {
  563. #ifdef SDSUPPORT
  564. if(card.saving)
  565. {
  566. if(strstr_P(cmdbuffer[bufindr], PSTR("M29")) == NULL)
  567. {
  568. card.write_command(cmdbuffer[bufindr]);
  569. if(card.logging)
  570. {
  571. process_commands();
  572. }
  573. else
  574. {
  575. SERIAL_PROTOCOLLNPGM(MSG_OK);
  576. }
  577. }
  578. else
  579. {
  580. card.closefile();
  581. SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
  582. }
  583. }
  584. else
  585. {
  586. process_commands();
  587. }
  588. #else
  589. process_commands();
  590. #endif //SDSUPPORT
  591. buflen = (buflen-1);
  592. bufindr = (bufindr + 1)%BUFSIZE;
  593. }
  594. //check heater every n milliseconds
  595. manage_heater();
  596. manage_inactivity();
  597. checkHitEndstops();
  598. lcd_update();
  599. }
  600.  
  601. void get_command()
  602. {
  603. while( MYSERIAL.available() > 0 && buflen < BUFSIZE) {
  604. serial_char = MYSERIAL.read();
  605. if(serial_char == '\n' ||
  606. serial_char == '\r' ||
  607. (serial_char == ':' && comment_mode == false) ||
  608. serial_count >= (MAX_CMD_SIZE - 1) )
  609. {
  610. if(!serial_count) { //if empty line
  611. comment_mode = false; //for new command
  612. return;
  613. }
  614. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  615. if(!comment_mode){
  616. comment_mode = false; //for new command
  617. fromsd[bufindw] = false;
  618. if(strchr(cmdbuffer[bufindw], 'N') != NULL)
  619. {
  620. strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
  621. gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL, 10));
  622. if(gcode_N != gcode_LastN+1 && (strstr_P(cmdbuffer[bufindw], PSTR("M110")) == NULL) ) {
  623. SERIAL_ERROR_START;
  624. SERIAL_ERRORPGM(MSG_ERR_LINE_NO);
  625. SERIAL_ERRORLN(gcode_LastN);
  626. //Serial.println(gcode_N);
  627. FlushSerialRequestResend();
  628. serial_count = 0;
  629. return;
  630. }
  631.  
  632. if(strchr(cmdbuffer[bufindw], '*') != NULL)
  633. {
  634. byte checksum = 0;
  635. byte count = 0;
  636. while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
  637. strchr_pointer = strchr(cmdbuffer[bufindw], '*');
  638.  
  639. if( (int)(strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)) != checksum) {
  640. SERIAL_ERROR_START;
  641. SERIAL_ERRORPGM(MSG_ERR_CHECKSUM_MISMATCH);
  642. SERIAL_ERRORLN(gcode_LastN);
  643. FlushSerialRequestResend();
  644. serial_count = 0;
  645. return;
  646. }
  647. //if no errors, continue parsing
  648. }
  649. else
  650. {
  651. SERIAL_ERROR_START;
  652. SERIAL_ERRORPGM(MSG_ERR_NO_CHECKSUM);
  653. SERIAL_ERRORLN(gcode_LastN);
  654. FlushSerialRequestResend();
  655. serial_count = 0;
  656. return;
  657. }
  658.  
  659. gcode_LastN = gcode_N;
  660. //if no errors, continue parsing
  661. }
  662. else // if we don't receive 'N' but still see '*'
  663. {
  664. if((strchr(cmdbuffer[bufindw], '*') != NULL))
  665. {
  666. SERIAL_ERROR_START;
  667. SERIAL_ERRORPGM(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM);
  668. SERIAL_ERRORLN(gcode_LastN);
  669. serial_count = 0;
  670. return;
  671. }
  672. }
  673. if((strchr(cmdbuffer[bufindw], 'G') != NULL)){
  674. strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
  675. switch((int)((strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)))){
  676. case 0:
  677. case 1:
  678. case 2:
  679. case 3:
  680. if(Stopped == false) { // If printer is stopped by an error the G[0-3] codes are ignored.
  681. #ifdef SDSUPPORT
  682. if(card.saving)
  683. break;
  684. #endif //SDSUPPORT
  685. SERIAL_PROTOCOLLNPGM(MSG_OK);
  686. }
  687. else {
  688. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  689. LCD_MESSAGEPGM(MSG_STOPPED);
  690. }
  691. break;
  692. default:
  693. break;
  694. }
  695.  
  696. }
  697.  
  698. //If command was e-stop process now
  699. if(strcmp(cmdbuffer[bufindw], "M112") == 0)
  700. kill();
  701.  
  702. bufindw = (bufindw + 1)%BUFSIZE;
  703. buflen += 1;
  704. }
  705. serial_count = 0; //clear buffer
  706. }
  707. else
  708. {
  709. if(serial_char == ';') comment_mode = true;
  710. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  711. }
  712. }
  713. #ifdef SDSUPPORT
  714. if(!card.sdprinting || serial_count!=0){
  715. return;
  716. }
  717.  
  718. //'#' stops reading from SD to the buffer prematurely, so procedural macro calls are possible
  719. // if it occurs, stop_buffering is triggered and the buffer is ran dry.
  720. // this character _can_ occur in serial com, due to checksums. however, no checksums are used in SD printing
  721.  
  722. static bool stop_buffering=false;
  723. if(buflen==0) stop_buffering=false;
  724.  
  725. while( !card.eof() && buflen < BUFSIZE && !stop_buffering) {
  726. int16_t n=card.get();
  727. serial_char = (char)n;
  728. if(serial_char == '\n' ||
  729. serial_char == '\r' ||
  730. (serial_char == '#' && comment_mode == false) ||
  731. (serial_char == ':' && comment_mode == false) ||
  732. serial_count >= (MAX_CMD_SIZE - 1)||n==-1)
  733. {
  734. if(card.eof()){
  735. SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED);
  736. stoptime=millis();
  737. char time[30];
  738. unsigned long t=(stoptime-starttime)/1000;
  739. int hours, minutes;
  740. minutes=(t/60)%60;
  741. hours=t/60/60;
  742. sprintf_P(time, PSTR("%i hours %i minutes"),hours, minutes);
  743. SERIAL_ECHO_START;
  744. SERIAL_ECHOLN(time);
  745. lcd_setstatus(time);
  746. card.printingHasFinished();
  747. card.checkautostart(true);
  748.  
  749. }
  750. if(serial_char=='#')
  751. stop_buffering=true;
  752.  
  753. if(!serial_count)
  754. {
  755. comment_mode = false; //for new command
  756. return; //if empty line
  757. }
  758. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  759. // if(!comment_mode){
  760. fromsd[bufindw] = true;
  761. buflen += 1;
  762. bufindw = (bufindw + 1)%BUFSIZE;
  763. // }
  764. comment_mode = false; //for new command
  765. serial_count = 0; //clear buffer
  766. }
  767. else
  768. {
  769. if(serial_char == ';') comment_mode = true;
  770. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  771. }
  772. }
  773.  
  774. #endif //SDSUPPORT
  775.  
  776. }
  777.  
  778.  
  779. float code_value()
  780. {
  781. return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL));
  782. }
  783.  
  784. long code_value_long()
  785. {
  786. return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10));
  787. }
  788.  
  789. bool code_seen(char code)
  790. {
  791. strchr_pointer = strchr(cmdbuffer[bufindr], code);
  792. return (strchr_pointer != NULL); //Return True if a character was found
  793. }
  794.  
  795. #define DEFINE_PGM_READ_ANY(type, reader) \
  796. static inline type pgm_read_any(const type *p) \
  797. { return pgm_read_##reader##_near(p); }
  798.  
  799. DEFINE_PGM_READ_ANY(float, float);
  800. DEFINE_PGM_READ_ANY(signed char, byte);
  801.  
  802. #define XYZ_CONSTS_FROM_CONFIG(type, array, CONFIG) \
  803. static const PROGMEM type array##_P[3] = \
  804. { X_##CONFIG, Y_##CONFIG, Z_##CONFIG }; \
  805. static inline type array(int axis) \
  806. { return pgm_read_any(&array##_P[axis]); }
  807.  
  808. XYZ_CONSTS_FROM_CONFIG(float, base_min_pos, MIN_POS);
  809. XYZ_CONSTS_FROM_CONFIG(float, base_max_pos, MAX_POS);
  810. XYZ_CONSTS_FROM_CONFIG(float, base_home_pos, HOME_POS);
  811. XYZ_CONSTS_FROM_CONFIG(float, max_length, MAX_LENGTH);
  812. XYZ_CONSTS_FROM_CONFIG(float, home_retract_mm, HOME_RETRACT_MM);
  813. XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
  814.  
  815. #ifdef DUAL_X_CARRIAGE
  816. #if EXTRUDERS == 1 || defined(COREXY) \
  817. || !defined(X2_ENABLE_PIN) || !defined(X2_STEP_PIN) || !defined(X2_DIR_PIN) \
  818. || !defined(X2_HOME_POS) || !defined(X2_MIN_POS) || !defined(X2_MAX_POS) \
  819. || !defined(X_MAX_PIN) || X_MAX_PIN < 0
  820. #error "Missing or invalid definitions for DUAL_X_CARRIAGE mode."
  821. #endif
  822. #if X_HOME_DIR != -1 || X2_HOME_DIR != 1
  823. #error "Please use canonical x-carriage assignment" // the x-carriages are defined by their homing directions
  824. #endif
  825.  
  826. #define DXC_FULL_CONTROL_MODE 0
  827. #define DXC_AUTO_PARK_MODE 1
  828. #define DXC_DUPLICATION_MODE 2
  829. static int dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  830.  
  831. static float x_home_pos(int extruder) {
  832. if (extruder == 0)
  833. return base_home_pos(X_AXIS) + add_homeing[X_AXIS];
  834. else
  835. // In dual carriage mode the extruder offset provides an override of the
  836. // second X-carriage offset when homed - otherwise X2_HOME_POS is used.
  837. // This allow soft recalibration of the second extruder offset position without firmware reflash
  838. // (through the M218 command).
  839. return (extruder_offset[X_AXIS][1] > 0) ? extruder_offset[X_AXIS][1] : X2_HOME_POS;
  840. }
  841.  
  842. static int x_home_dir(int extruder) {
  843. return (extruder == 0) ? X_HOME_DIR : X2_HOME_DIR;
  844. }
  845.  
  846. static float inactive_extruder_x_pos = X2_MAX_POS; // used in mode 0 & 1
  847. static bool active_extruder_parked = false; // used in mode 1 & 2
  848. static float raised_parked_position[NUM_AXIS]; // used in mode 1
  849. static unsigned long delayed_move_time = 0; // used in mode 1
  850. static float duplicate_extruder_x_offset = DEFAULT_DUPLICATION_X_OFFSET; // used in mode 2
  851. static float duplicate_extruder_temp_offset = 0; // used in mode 2
  852. bool extruder_duplication_enabled = false; // used in mode 2
  853. #endif //DUAL_X_CARRIAGE
  854.  
  855. static void axis_is_at_home(int axis) {
  856. #ifdef DUAL_X_CARRIAGE
  857. if (axis == X_AXIS) {
  858. if (active_extruder != 0) {
  859. current_position[X_AXIS] = x_home_pos(active_extruder);
  860. min_pos[X_AXIS] = X2_MIN_POS;
  861. max_pos[X_AXIS] = max(extruder_offset[X_AXIS][1], X2_MAX_POS);
  862. return;
  863. }
  864. else if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && active_extruder == 0) {
  865. current_position[X_AXIS] = base_home_pos(X_AXIS) + add_homeing[X_AXIS];
  866. min_pos[X_AXIS] = base_min_pos(X_AXIS) + add_homeing[X_AXIS];
  867. max_pos[X_AXIS] = min(base_max_pos(X_AXIS) + add_homeing[X_AXIS],
  868. max(extruder_offset[X_AXIS][1], X2_MAX_POS) - duplicate_extruder_x_offset);
  869. return;
  870. }
  871. }
  872. #endif
  873. current_position[axis] = base_home_pos(axis) + add_homeing[axis];
  874. min_pos[axis] = base_min_pos(axis) + add_homeing[axis];
  875. max_pos[axis] = base_max_pos(axis) + add_homeing[axis];
  876. }
  877.  
  878. #ifdef ENABLE_AUTO_BED_LEVELING
  879. #ifdef AUTO_BED_LEVELING_GRID
  880. static void set_bed_level_equation_lsq(double *plane_equation_coefficients)
  881. {
  882. vector_3 planeNormal = vector_3(-plane_equation_coefficients[0], -plane_equation_coefficients[1], 1);
  883. planeNormal.debug("planeNormal");
  884. plan_bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
  885. //bedLevel.debug("bedLevel");
  886.  
  887. //plan_bed_level_matrix.debug("bed level before");
  888. //vector_3 uncorrected_position = plan_get_position_mm();
  889. //uncorrected_position.debug("position before");
  890.  
  891. vector_3 corrected_position = plan_get_position();
  892. // corrected_position.debug("position after");
  893. current_position[X_AXIS] = corrected_position.x;
  894. current_position[Y_AXIS] = corrected_position.y;
  895. current_position[Z_AXIS] = corrected_position.z;
  896.  
  897. // but the bed at 0 so we don't go below it.
  898. current_position[Z_AXIS] = zprobe_zoffset; // in the lsq we reach here after raising the extruder due to the loop structure
  899.  
  900. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  901. }
  902.  
  903. #else // not AUTO_BED_LEVELING_GRID
  904.  
  905. static void set_bed_level_equation_3pts(float z_at_pt_1, float z_at_pt_2, float z_at_pt_3) {
  906.  
  907. plan_bed_level_matrix.set_to_identity();
  908.  
  909. vector_3 pt1 = vector_3(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, z_at_pt_1);
  910. vector_3 pt2 = vector_3(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, z_at_pt_2);
  911. vector_3 pt3 = vector_3(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, z_at_pt_3);
  912.  
  913. vector_3 from_2_to_1 = (pt1 - pt2).get_normal();
  914. vector_3 from_2_to_3 = (pt3 - pt2).get_normal();
  915. vector_3 planeNormal = vector_3::cross(from_2_to_1, from_2_to_3).get_normal();
  916. planeNormal = vector_3(planeNormal.x, planeNormal.y, abs(planeNormal.z));
  917.  
  918. plan_bed_level_matrix = matrix_3x3::create_look_at(planeNormal);
  919.  
  920. vector_3 corrected_position = plan_get_position();
  921. current_position[X_AXIS] = corrected_position.x;
  922. current_position[Y_AXIS] = corrected_position.y;
  923. current_position[Z_AXIS] = corrected_position.z;
  924.  
  925. // put the bed at 0 so we don't go below it.
  926. current_position[Z_AXIS] = zprobe_zoffset;
  927.  
  928. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  929.  
  930. }
  931.  
  932. #endif // AUTO_BED_LEVELING_GRID
  933.  
  934. static void run_z_probe() {
  935. plan_bed_level_matrix.set_to_identity();
  936. feedrate = homing_feedrate[Z_AXIS];
  937.  
  938. // move down until you find the bed
  939. float zPosition = -10;
  940. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
  941. st_synchronize();
  942.  
  943. // we have to let the planner know where we are right now as it is not where we said to go.
  944. zPosition = st_get_position_mm(Z_AXIS);
  945. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS]);
  946.  
  947. // move up the retract distance
  948. zPosition += home_retract_mm(Z_AXIS);
  949. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
  950. st_synchronize();
  951.  
  952. // move back down slowly to find bed
  953. feedrate = homing_feedrate[Z_AXIS]/4;
  954. zPosition -= home_retract_mm(Z_AXIS) * 2;
  955. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
  956. st_synchronize();
  957.  
  958. current_position[Z_AXIS] = st_get_position_mm(Z_AXIS);
  959. // make sure the planner knows where we are as it may be a bit different than we last said to move to
  960. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  961. }
  962.  
  963. static void do_blocking_move_to(float x, float y, float z) {
  964. float oldFeedRate = feedrate;
  965.  
  966. feedrate = XY_TRAVEL_SPEED;
  967.  
  968. current_position[X_AXIS] = x;
  969. current_position[Y_AXIS] = y;
  970. current_position[Z_AXIS] = z;
  971. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate/60, active_extruder);
  972. st_synchronize();
  973.  
  974. feedrate = oldFeedRate;
  975. }
  976.  
  977. static void do_blocking_move_relative(float offset_x, float offset_y, float offset_z) {
  978. do_blocking_move_to(current_position[X_AXIS] + offset_x, current_position[Y_AXIS] + offset_y, current_position[Z_AXIS] + offset_z);
  979. }
  980.  
  981. static void setup_for_endstop_move() {
  982. saved_feedrate = feedrate;
  983. saved_feedmultiply = feedmultiply;
  984. feedmultiply = 100;
  985. previous_millis_cmd = millis();
  986.  
  987. enable_endstops(true);
  988. }
  989.  
  990. static void clean_up_after_endstop_move() {
  991. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  992. enable_endstops(false);
  993. #endif
  994.  
  995. feedrate = saved_feedrate;
  996. feedmultiply = saved_feedmultiply;
  997. previous_millis_cmd = millis();
  998. }
  999.  
  1000. static void engage_z_probe() {
  1001. // Engage Z Servo endstop if enabled
  1002. #ifdef SERVO_ENDSTOPS
  1003. if (servo_endstops[Z_AXIS] > -1) {
  1004. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  1005. servos[servo_endstops[Z_AXIS]].attach(0);
  1006. #endif
  1007. servos[servo_endstops[Z_AXIS]].write(servo_endstop_angles[Z_AXIS * 2]);
  1008. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  1009. delay(PROBE_SERVO_DEACTIVATION_DELAY);
  1010. servos[servo_endstops[Z_AXIS]].detach();
  1011. #endif
  1012. }
  1013. #endif
  1014. }
  1015.  
  1016. static void retract_z_probe() {
  1017. // Retract Z Servo endstop if enabled
  1018. #ifdef SERVO_ENDSTOPS
  1019. if (servo_endstops[Z_AXIS] > -1) {
  1020. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  1021. servos[servo_endstops[Z_AXIS]].attach(0);
  1022. #endif
  1023. servos[servo_endstops[Z_AXIS]].write(servo_endstop_angles[Z_AXIS * 2 + 1]);
  1024. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  1025. delay(PROBE_SERVO_DEACTIVATION_DELAY);
  1026. servos[servo_endstops[Z_AXIS]].detach();
  1027. #endif
  1028. }
  1029. #endif
  1030. }
  1031.  
  1032. /// Probe bed height at position (x,y), returns the measured z value
  1033. static float probe_pt(float x, float y, float z_before, int retract_probe, int verboseness) {
  1034. // move to right place
  1035. do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], z_before);
  1036. do_blocking_move_to(x - X_PROBE_OFFSET_FROM_EXTRUDER, y - Y_PROBE_OFFSET_FROM_EXTRUDER, current_position[Z_AXIS]);
  1037.  
  1038. engage_z_probe(); // Engage Z Servo endstop if available
  1039. run_z_probe();
  1040. float measured_z = current_position[Z_AXIS];
  1041.  
  1042. if (retract_probe)
  1043. retract_z_probe();
  1044.  
  1045. if ( verboseness > 2 ) {
  1046. SERIAL_PROTOCOLPGM(MSG_BED);
  1047. SERIAL_PROTOCOLPGM(" x: ");
  1048. SERIAL_PROTOCOL(x);
  1049. SERIAL_PROTOCOLPGM(" y: ");
  1050. SERIAL_PROTOCOL(y);
  1051. SERIAL_PROTOCOLPGM(" z: ");
  1052. SERIAL_PROTOCOL(measured_z);
  1053. SERIAL_PROTOCOLPGM("\n");
  1054. }
  1055. return measured_z;
  1056. }
  1057.  
  1058. #endif // #ifdef ENABLE_AUTO_BED_LEVELING
  1059.  
  1060. static void homeaxis(int axis) {
  1061. #define HOMEAXIS_DO(LETTER) \
  1062. ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))
  1063.  
  1064. if (axis==X_AXIS ? HOMEAXIS_DO(X) :
  1065. axis==Y_AXIS ? HOMEAXIS_DO(Y) :
  1066. axis==Z_AXIS ? HOMEAXIS_DO(Z) :
  1067. 0) {
  1068. int axis_home_dir = home_dir(axis);
  1069. #ifdef DUAL_X_CARRIAGE
  1070. if (axis == X_AXIS)
  1071. axis_home_dir = x_home_dir(active_extruder);
  1072. #endif
  1073.  
  1074. current_position[axis] = 0;
  1075. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1076.  
  1077.  
  1078. // Engage Servo endstop if enabled
  1079. #ifdef SERVO_ENDSTOPS
  1080. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  1081. if (axis==Z_AXIS) {
  1082. engage_z_probe();
  1083. }
  1084. else
  1085. #endif
  1086. if (servo_endstops[axis] > -1) {
  1087. servos[servo_endstops[axis]].write(servo_endstop_angles[axis * 2]);
  1088. }
  1089. #endif
  1090.  
  1091. destination[axis] = 1.5 * max_length(axis) * axis_home_dir;
  1092. feedrate = homing_feedrate[axis];
  1093. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1094. st_synchronize();
  1095.  
  1096. current_position[axis] = 0;
  1097. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1098. destination[axis] = -home_retract_mm(axis) * axis_home_dir;
  1099. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1100. st_synchronize();
  1101.  
  1102. destination[axis] = 2*home_retract_mm(axis) * axis_home_dir;
  1103. #ifdef DELTA
  1104. feedrate = homing_feedrate[axis]/10;
  1105. #else
  1106. feedrate = homing_feedrate[axis]/2 ;
  1107. #endif
  1108. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1109. st_synchronize();
  1110. #ifdef DELTA
  1111. // retrace by the amount specified in endstop_adj
  1112. if (endstop_adj[axis] * axis_home_dir < 0) {
  1113. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1114. destination[axis] = endstop_adj[axis];
  1115. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1116. st_synchronize();
  1117. }
  1118. #endif
  1119. axis_is_at_home(axis);
  1120. destination[axis] = current_position[axis];
  1121. feedrate = 0.0;
  1122. endstops_hit_on_purpose();
  1123. axis_known_position[axis] = true;
  1124.  
  1125. // Retract Servo endstop if enabled
  1126. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  1127. if (axis==Z_AXIS)
  1128. do_blocking_move_relative(0, 0, Z_RAISE_BEFORE_PROBING);
  1129. #endif
  1130.  
  1131. #ifdef SERVO_ENDSTOPS
  1132. if (servo_endstops[axis] > -1) {
  1133. servos[servo_endstops[axis]].write(servo_endstop_angles[axis * 2 + 1]);
  1134. }
  1135. #endif
  1136. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  1137. if (axis==Z_AXIS) retract_z_probe();
  1138. #endif
  1139.  
  1140. }
  1141. }
  1142. #define HOMEAXIS(LETTER) homeaxis(LETTER##_AXIS)
  1143. void refresh_cmd_timeout(void)
  1144. {
  1145. previous_millis_cmd = millis();
  1146. }
  1147.  
  1148. #ifdef FWRETRACT
  1149. void retract(bool retracting, bool swapretract = false) {
  1150. if(retracting && !retracted[active_extruder]) {
  1151. destination[X_AXIS]=current_position[X_AXIS];
  1152. destination[Y_AXIS]=current_position[Y_AXIS];
  1153. destination[Z_AXIS]=current_position[Z_AXIS];
  1154. destination[E_AXIS]=current_position[E_AXIS];
  1155. if (swapretract) {
  1156. current_position[E_AXIS]+=retract_length_swap/volumetric_multiplier[active_extruder];
  1157. } else {
  1158. current_position[E_AXIS]+=retract_length/volumetric_multiplier[active_extruder];
  1159. }
  1160. plan_set_e_position(current_position[E_AXIS]);
  1161. float oldFeedrate = feedrate;
  1162. feedrate=retract_feedrate*60;
  1163. retracted[active_extruder]=true;
  1164. prepare_move();
  1165. current_position[Z_AXIS]-=retract_zlift;
  1166. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1167. prepare_move();
  1168. feedrate = oldFeedrate;
  1169. } else if(!retracting && retracted[active_extruder]) {
  1170. destination[X_AXIS]=current_position[X_AXIS];
  1171. destination[Y_AXIS]=current_position[Y_AXIS];
  1172. destination[Z_AXIS]=current_position[Z_AXIS];
  1173. destination[E_AXIS]=current_position[E_AXIS];
  1174. current_position[Z_AXIS]+=retract_zlift;
  1175. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1176. //prepare_move();
  1177. if (swapretract) {
  1178. current_position[E_AXIS]-=(retract_length_swap+retract_recover_length_swap)/volumetric_multiplier[active_extruder];
  1179. } else {
  1180. current_position[E_AXIS]-=(retract_length+retract_recover_length)/volumetric_multiplier[active_extruder];
  1181. }
  1182. plan_set_e_position(current_position[E_AXIS]);
  1183. float oldFeedrate = feedrate;
  1184. feedrate=retract_recover_feedrate*60;
  1185. retracted[active_extruder]=false;
  1186. prepare_move();
  1187. feedrate = oldFeedrate;
  1188. }
  1189. } //retract
  1190. #endif //FWRETRACT
  1191.  
  1192. void process_commands()
  1193. {
  1194. unsigned long codenum; //throw away variable
  1195. char *starpos = NULL;
  1196. #ifdef ENABLE_AUTO_BED_LEVELING
  1197. float x_tmp, y_tmp, z_tmp, real_z;
  1198. #endif
  1199. if(code_seen('G'))
  1200. {
  1201. switch((int)code_value())
  1202. {
  1203. case 0: // G0 -> G1
  1204. case 1: // G1
  1205. if(Stopped == false) {
  1206. get_coordinates(); // For X Y Z E F
  1207. #ifdef FWRETRACT
  1208. if(autoretract_enabled)
  1209. if( !(code_seen('X') || code_seen('Y') || code_seen('Z')) && code_seen('E')) {
  1210. float echange=destination[E_AXIS]-current_position[E_AXIS];
  1211. if((echange<-MIN_RETRACT && !retracted) || (echange>MIN_RETRACT && retracted)) { //move appears to be an attempt to retract or recover
  1212. current_position[E_AXIS] = destination[E_AXIS]; //hide the slicer-generated retract/recover from calculations
  1213. plan_set_e_position(current_position[E_AXIS]); //AND from the planner
  1214. retract(!retracted);
  1215. return;
  1216. }
  1217. }
  1218. #endif //FWRETRACT
  1219. prepare_move();
  1220. //ClearToSend();
  1221. return;
  1222. }
  1223. break;
  1224. case 2: // G2 - CW ARC
  1225. if(Stopped == false) {
  1226. get_arc_coordinates();
  1227. prepare_arc_move(true);
  1228. return;
  1229. }
  1230. break;
  1231. case 3: // G3 - CCW ARC
  1232. if(Stopped == false) {
  1233. get_arc_coordinates();
  1234. prepare_arc_move(false);
  1235. return;
  1236. }
  1237. break;
  1238. case 4: // G4 dwell
  1239. LCD_MESSAGEPGM(MSG_DWELL);
  1240. codenum = 0;
  1241. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  1242. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  1243.  
  1244. st_synchronize();
  1245. codenum += millis(); // keep track of when we started waiting
  1246. previous_millis_cmd = millis();
  1247. while(millis() < codenum ){
  1248. manage_heater();
  1249. manage_inactivity();
  1250. lcd_update();
  1251. }
  1252. break;
  1253. #ifdef FWRETRACT
  1254. case 10: // G10 retract
  1255. #if EXTRUDERS > 1
  1256. retracted_swap[active_extruder]=(code_seen('S') && code_value_long() == 1); // checks for swap retract argument
  1257. retract(true,retracted_swap[active_extruder]);
  1258. #else
  1259. retract(true);
  1260. #endif
  1261. break;
  1262. case 11: // G11 retract_recover
  1263. #if EXTRUDERS > 1
  1264. retract(false,retracted_swap[active_extruder]);
  1265. #else
  1266. retract(false);
  1267. #endif
  1268. break;
  1269. #endif //FWRETRACT
  1270. case 28: //G28 Home all Axis one at a time
  1271. #ifdef ENABLE_AUTO_BED_LEVELING
  1272. plan_bed_level_matrix.set_to_identity(); //Reset the plane ("erase" all leveling data)
  1273. #endif //ENABLE_AUTO_BED_LEVELING
  1274.  
  1275.  
  1276. saved_feedrate = feedrate;
  1277. saved_feedmultiply = feedmultiply;
  1278. feedmultiply = 100;
  1279. previous_millis_cmd = millis();
  1280.  
  1281. enable_endstops(true);
  1282.  
  1283. for(int8_t i=0; i < NUM_AXIS; i++) {
  1284. destination[i] = current_position[i];
  1285. }
  1286. feedrate = 0.0;
  1287.  
  1288. #ifdef DELTA
  1289. // A delta can only safely home all axis at the same time
  1290. // all axis have to home at the same time
  1291.  
  1292. // Move all carriages up together until the first endstop is hit.
  1293. current_position[X_AXIS] = 0;
  1294. current_position[Y_AXIS] = 0;
  1295. current_position[Z_AXIS] = 0;
  1296. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1297.  
  1298. destination[X_AXIS] = 3 * Z_MAX_LENGTH;
  1299. destination[Y_AXIS] = 3 * Z_MAX_LENGTH;
  1300. destination[Z_AXIS] = 3 * Z_MAX_LENGTH;
  1301. feedrate = 1.732 * homing_feedrate[X_AXIS];
  1302. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1303. st_synchronize();
  1304. endstops_hit_on_purpose();
  1305.  
  1306. current_position[X_AXIS] = destination[X_AXIS];
  1307. current_position[Y_AXIS] = destination[Y_AXIS];
  1308. current_position[Z_AXIS] = destination[Z_AXIS];
  1309.  
  1310. // take care of back off and rehome now we are all at the top
  1311. HOMEAXIS(X);
  1312. HOMEAXIS(Y);
  1313. HOMEAXIS(Z);
  1314.  
  1315. calculate_delta(current_position);
  1316. plan_set_position(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], current_position[E_AXIS]);
  1317.  
  1318. #else // NOT DELTA
  1319.  
  1320. home_all_axis = !((code_seen(axis_codes[X_AXIS])) || (code_seen(axis_codes[Y_AXIS])) || (code_seen(axis_codes[Z_AXIS])));
  1321.  
  1322. #if Z_HOME_DIR > 0 // If homing away from BED do Z first
  1323. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  1324. HOMEAXIS(Z);
  1325. }
  1326. #endif
  1327.  
  1328. #ifdef QUICK_HOME
  1329. if((home_all_axis)||( code_seen(axis_codes[X_AXIS]) && code_seen(axis_codes[Y_AXIS])) ) //first diagonal move
  1330. {
  1331. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  1332.  
  1333. #ifndef DUAL_X_CARRIAGE
  1334. int x_axis_home_dir = home_dir(X_AXIS);
  1335. #else
  1336. int x_axis_home_dir = x_home_dir(active_extruder);
  1337. extruder_duplication_enabled = false;
  1338. #endif
  1339.  
  1340. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1341. destination[X_AXIS] = 1.5 * max_length(X_AXIS) * x_axis_home_dir;destination[Y_AXIS] = 1.5 * max_length(Y_AXIS) * home_dir(Y_AXIS);
  1342. feedrate = homing_feedrate[X_AXIS];
  1343. if(homing_feedrate[Y_AXIS]<feedrate)
  1344. feedrate = homing_feedrate[Y_AXIS];
  1345. if (max_length(X_AXIS) > max_length(Y_AXIS)) {
  1346. feedrate *= sqrt(pow(max_length(Y_AXIS) / max_length(X_AXIS), 2) + 1);
  1347. } else {
  1348. feedrate *= sqrt(pow(max_length(X_AXIS) / max_length(Y_AXIS), 2) + 1);
  1349. }
  1350. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1351. st_synchronize();
  1352.  
  1353. axis_is_at_home(X_AXIS);
  1354. axis_is_at_home(Y_AXIS);
  1355. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1356. destination[X_AXIS] = current_position[X_AXIS];
  1357. destination[Y_AXIS] = current_position[Y_AXIS];
  1358. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1359. feedrate = 0.0;
  1360. st_synchronize();
  1361. endstops_hit_on_purpose();
  1362.  
  1363. current_position[X_AXIS] = destination[X_AXIS];
  1364. current_position[Y_AXIS] = destination[Y_AXIS];
  1365. current_position[Z_AXIS] = destination[Z_AXIS];
  1366. }
  1367. #endif
  1368.  
  1369. if((home_all_axis) || (code_seen(axis_codes[X_AXIS])))
  1370. {
  1371. #ifdef DUAL_X_CARRIAGE
  1372. int tmp_extruder = active_extruder;
  1373. extruder_duplication_enabled = false;
  1374. active_extruder = !active_extruder;
  1375. HOMEAXIS(X);
  1376. inactive_extruder_x_pos = current_position[X_AXIS];
  1377. active_extruder = tmp_extruder;
  1378. HOMEAXIS(X);
  1379. // reset state used by the different modes
  1380. memcpy(raised_parked_position, current_position, sizeof(raised_parked_position));
  1381. delayed_move_time = 0;
  1382. active_extruder_parked = true;
  1383. #else
  1384. HOMEAXIS(X);
  1385. #endif
  1386. }
  1387.  
  1388. if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) {
  1389. HOMEAXIS(Y);
  1390. }
  1391.  
  1392. if(code_seen(axis_codes[X_AXIS]))
  1393. {
  1394. if(code_value_long() != 0) {
  1395. current_position[X_AXIS]=code_value()+add_homeing[0];
  1396. }
  1397. }
  1398.  
  1399. if(code_seen(axis_codes[Y_AXIS])) {
  1400. if(code_value_long() != 0) {
  1401. current_position[Y_AXIS]=code_value()+add_homeing[1];
  1402. }
  1403. }
  1404.  
  1405. #if Z_HOME_DIR < 0 // If homing towards BED do Z last
  1406. #ifndef Z_SAFE_HOMING
  1407. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  1408. #if defined (Z_RAISE_BEFORE_HOMING) && (Z_RAISE_BEFORE_HOMING > 0)
  1409. destination[Z_AXIS] = Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS) * (-1); // Set destination away from bed
  1410. feedrate = max_feedrate[Z_AXIS];
  1411. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder);
  1412. st_synchronize();
  1413. #endif
  1414. HOMEAXIS(Z);
  1415. }
  1416. #else // Z Safe mode activated.
  1417. if(home_all_axis) {
  1418. destination[X_AXIS] = round(Z_SAFE_HOMING_X_POINT - X_PROBE_OFFSET_FROM_EXTRUDER);
  1419. destination[Y_AXIS] = round(Z_SAFE_HOMING_Y_POINT - Y_PROBE_OFFSET_FROM_EXTRUDER);
  1420. destination[Z_AXIS] = Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS) * (-1); // Set destination away from bed
  1421. feedrate = XY_TRAVEL_SPEED;
  1422. current_position[Z_AXIS] = 0;
  1423.  
  1424. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1425. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder);
  1426. st_synchronize();
  1427. current_position[X_AXIS] = destination[X_AXIS];
  1428. current_position[Y_AXIS] = destination[Y_AXIS];
  1429.  
  1430. HOMEAXIS(Z);
  1431. }
  1432. // Let's see if X and Y are homed and probe is inside bed area.
  1433. if(code_seen(axis_codes[Z_AXIS])) {
  1434. if ( (axis_known_position[X_AXIS]) && (axis_known_position[Y_AXIS]) \
  1435. && (current_position[X_AXIS]+X_PROBE_OFFSET_FROM_EXTRUDER >= X_MIN_POS) \
  1436. && (current_position[X_AXIS]+X_PROBE_OFFSET_FROM_EXTRUDER <= X_MAX_POS) \
  1437. && (current_position[Y_AXIS]+Y_PROBE_OFFSET_FROM_EXTRUDER >= Y_MIN_POS) \
  1438. && (current_position[Y_AXIS]+Y_PROBE_OFFSET_FROM_EXTRUDER <= Y_MAX_POS)) {
  1439.  
  1440. current_position[Z_AXIS] = 0;
  1441. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1442. destination[Z_AXIS] = Z_RAISE_BEFORE_HOMING * home_dir(Z_AXIS) * (-1); // Set destination away from bed
  1443. feedrate = max_feedrate[Z_AXIS];
  1444. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder);
  1445. st_synchronize();
  1446.  
  1447. HOMEAXIS(Z);
  1448. } else if (!((axis_known_position[X_AXIS]) && (axis_known_position[Y_AXIS]))) {
  1449. LCD_MESSAGEPGM(MSG_POSITION_UNKNOWN);
  1450. SERIAL_ECHO_START;
  1451. SERIAL_ECHOLNPGM(MSG_POSITION_UNKNOWN);
  1452. } else {
  1453. LCD_MESSAGEPGM(MSG_ZPROBE_OUT);
  1454. SERIAL_ECHO_START;
  1455. SERIAL_ECHOLNPGM(MSG_ZPROBE_OUT);
  1456. }
  1457. }
  1458. #endif
  1459. #endif
  1460.  
  1461.  
  1462.  
  1463. if(code_seen(axis_codes[Z_AXIS])) {
  1464. if(code_value_long() != 0) {
  1465. current_position[Z_AXIS]=code_value()+add_homeing[2];
  1466. }
  1467. }
  1468. #ifdef ENABLE_AUTO_BED_LEVELING
  1469. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  1470. current_position[Z_AXIS] += zprobe_zoffset; //Add Z_Probe offset (the distance is negative)
  1471. }
  1472. #endif
  1473. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1474. #endif // else DELTA
  1475.  
  1476. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  1477. enable_endstops(false);
  1478. #endif
  1479.  
  1480. feedrate = saved_feedrate;
  1481. feedmultiply = saved_feedmultiply;
  1482. previous_millis_cmd = millis();
  1483. endstops_hit_on_purpose();
  1484. break;
  1485.  
  1486. #ifdef ENABLE_AUTO_BED_LEVELING
  1487.  
  1488. //
  1489. // Enable one of these defines to configure a topographical map to be printed for your bed:
  1490. //
  1491. //#define ORIGIN_BACK_LEFT
  1492. //#define ORIGIN_FRONT_RIGHT
  1493. #define ORIGIN_BACK_RIGHT
  1494. //#define ORIGIN_FRONT_LEFT
  1495. //
  1496. //
  1497. case 29: // G29 Detailed Z-Probe, probes the bed at 3 or more points.
  1498. // Example Syntax: G29 N 4 V 2 E T
  1499. {
  1500. int retract_flag=0, verbose_level=1, topo_flag=0, n_points=3;
  1501.  
  1502. if ( code_seen('E') || code_seen('e'))
  1503. retract_flag++;
  1504.  
  1505. if ( code_seen('T') || code_seen('t'))
  1506. topo_flag++;
  1507.  
  1508. if ( code_seen('V') || code_seen('v')) {
  1509. verbose_level = code_value();
  1510. if (verbose_level<0 || verbose_level>4) {
  1511. SERIAL_PROTOCOLPGM("?Verbose Level not plausable.\n");
  1512. break;
  1513. }
  1514. if (verbose_level>0 ) {
  1515. SERIAL_PROTOCOLPGM("Roxy's Enhanced G29 Auto_Bed_Leveling Code V1.01:\n");
  1516. if (verbose_level>2 ) {
  1517. topo_flag++;
  1518. }
  1519. }
  1520. }
  1521.  
  1522. if ( code_seen('n') ) {
  1523. n_points = code_value();
  1524. if (n_points<2 || n_points>AUTO_BED_LEVELING_GRID_POINTS ) {
  1525. SERIAL_PROTOCOLPGM("?Number of probed points not plausable.\n");
  1526. break;
  1527. }
  1528. }
  1529.  
  1530.  
  1531. #if Z_MIN_PIN == -1
  1532. #error "You must have a Z_MIN endstop in order to enable Auto Bed Leveling feature!!! Z_MIN_PIN must point to a valid hardware pin."
  1533. #endif
  1534.  
  1535. // Prevent user from running a G29 without first homing in X and Y
  1536. if (! (axis_known_position[X_AXIS] && axis_known_position[Y_AXIS]) )
  1537. {
  1538. LCD_MESSAGEPGM(MSG_POSITION_UNKNOWN);
  1539. SERIAL_ECHO_START;
  1540. SERIAL_ECHOLNPGM(MSG_POSITION_UNKNOWN);
  1541. break; // abort G29, since we don't know where we are
  1542. }
  1543.  
  1544. st_synchronize();
  1545. // make sure the bed_level_rotation_matrix is identity or the planner will get it incorectly
  1546. //vector_3 corrected_position = plan_get_position_mm();
  1547. //corrected_position.debug("position before G29");
  1548. plan_bed_level_matrix.set_to_identity();
  1549. vector_3 uncorrected_position = plan_get_position();
  1550. //uncorrected_position.debug("position durring G29");
  1551. current_position[X_AXIS] = uncorrected_position.x;
  1552. current_position[Y_AXIS] = uncorrected_position.y;
  1553. current_position[Z_AXIS] = uncorrected_position.z;
  1554. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1555. setup_for_endstop_move();
  1556.  
  1557. feedrate = homing_feedrate[Z_AXIS];
  1558. #ifdef AUTO_BED_LEVELING_GRID
  1559. // probe at the points of a lattice grid
  1560.  
  1561. int xGridSpacing = (RIGHT_PROBE_BED_POSITION - LEFT_PROBE_BED_POSITION) / (n_points-1);
  1562. int yGridSpacing = (BACK_PROBE_BED_POSITION - FRONT_PROBE_BED_POSITION) / (n_points-1);
  1563.  
  1564.  
  1565. // solve the plane equation ax + by + d = z
  1566. // A is the matrix with rows [x y 1] for all the probed points
  1567. // B is the vector of the Z positions
  1568. // the normal vector to the plane is formed by the coefficients of the plane equation in the standard form, which is Vx*x+Vy*y+Vz*z+d = 0
  1569. // so Vx = -a Vy = -b Vz = 1 (we want the vector facing towards positive Z
  1570.  
  1571. // "A" matrix of the linear system of equations
  1572. double eqnAMatrix[AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS*3];
  1573. // "B" vector of Z points
  1574. double eqnBVector[AUTO_BED_LEVELING_GRID_POINTS*AUTO_BED_LEVELING_GRID_POINTS];
  1575.  
  1576. double mean=0.0;
  1577.  
  1578.  
  1579. int probePointCounter = 0;
  1580. bool zig = true;
  1581.  
  1582. for (int yProbe=FRONT_PROBE_BED_POSITION; yProbe <= BACK_PROBE_BED_POSITION; yProbe += yGridSpacing)
  1583. {
  1584. int xProbe, xInc;
  1585.  
  1586. //
  1587. // if the topo_flag is set, we are not going to zig-zag. We just go one direction in our scanning.
  1588. // This facilitates getting the probe points in an easier to use order
  1589. //
  1590. if (topo_flag)
  1591. zig = true;
  1592.  
  1593. if (zig)
  1594. {
  1595. xProbe = LEFT_PROBE_BED_POSITION;
  1596. //xEnd = RIGHT_PROBE_BED_POSITION;
  1597. xInc = xGridSpacing;
  1598. zig = false;
  1599. } else // zag
  1600. {
  1601. xProbe = RIGHT_PROBE_BED_POSITION;
  1602. //xEnd = LEFT_PROBE_BED_POSITION;
  1603. xInc = -xGridSpacing;
  1604. zig = true;
  1605. }
  1606.  
  1607. for (int xCount=0; xCount < n_points; xCount++)
  1608. {
  1609. float z_before;
  1610. if (probePointCounter == 0)
  1611. {
  1612. // raise before probing
  1613. z_before = Z_RAISE_BEFORE_PROBING;
  1614. } else
  1615. {
  1616. // raise extruder
  1617. z_before = current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS;
  1618. }
  1619.  
  1620. float measured_z = probe_pt(xProbe, yProbe, z_before, retract_flag, verbose_level);
  1621.  
  1622. mean += measured_z;
  1623.  
  1624. eqnBVector[probePointCounter] = measured_z;
  1625.  
  1626. eqnAMatrix[probePointCounter + 0*n_points*n_points] = xProbe;
  1627. eqnAMatrix[probePointCounter + 1*n_points*n_points] = yProbe;
  1628. eqnAMatrix[probePointCounter + 2*n_points*n_points] = 1;
  1629.  
  1630. probePointCounter++;
  1631. xProbe += xInc;
  1632. }
  1633. }
  1634.  
  1635. clean_up_after_endstop_move();
  1636.  
  1637. // solve lsq problem
  1638. double *plane_equation_coefficients = qr_solve(n_points*n_points, 3, eqnAMatrix, eqnBVector);
  1639.  
  1640. if (verbose_level ) {
  1641.  
  1642. SERIAL_PROTOCOLPGM("Eqn coefficients: a: ");
  1643. SERIAL_PROTOCOL(plane_equation_coefficients[0]);
  1644. SERIAL_PROTOCOLPGM(" b: ");
  1645. SERIAL_PROTOCOL(plane_equation_coefficients[1]);
  1646. SERIAL_PROTOCOLPGM(" d: ");
  1647. SERIAL_PROTOCOLLN(plane_equation_coefficients[2]);
  1648. }
  1649.  
  1650. mean = mean / (n_points*n_points);
  1651. if (verbose_level > 2) {
  1652. SERIAL_PROTOCOLPGM("Mean of sampled points: ");
  1653. SERIAL_PROTOCOL_F(mean,6);
  1654. SERIAL_PROTOCOLPGM(" \n");
  1655. }
  1656.  
  1657. if (topo_flag) {
  1658. int xx,yy;
  1659.  
  1660. SERIAL_PROTOCOLPGM(" \nBed Height Topography: \n");
  1661. #ifdef ORIGIN_BACK_RIGHT
  1662. for(yy=0; yy<n_points; yy++) {
  1663. for(xx=n_points-1; xx>=0; xx--) {
  1664. SERIAL_PROTOCOLPGM(" ");
  1665. if ( eqnBVector[yy*n_points+xx]-mean >= 0.0)
  1666. SERIAL_PROTOCOLPGM("+");
  1667. else
  1668. SERIAL_PROTOCOLPGM("-"); // we need this extra - because Proterface uses a preportional
  1669. // font and it causes the columns to not line up nice without it.
  1670. SERIAL_PROTOCOL_F( eqnBVector[yy*n_points+xx]-mean, 5);
  1671. }
  1672. SERIAL_PROTOCOLPGM(" \n");
  1673. }
  1674. SERIAL_PROTOCOLPGM(" \n");
  1675. #endif // ORIGIN_BACK_RIGHT
  1676.  
  1677. #ifdef ORIGIN_BACK_LEFT
  1678. for(yy=0; yy<n_points; yy++) {
  1679. for(xx=0; xx<n_points; xx++) {
  1680. SERIAL_PROTOCOLPGM(" ");
  1681. if ( eqnBVector[yy+xx*n_points]-mean >= 0.0)
  1682. SERIAL_PROTOCOLPGM("+");
  1683. else
  1684. SERIAL_PROTOCOLPGM("-"); // we need this extra - because Proterface uses a preportional
  1685. // font and it causes the columns to not line up nice without it.
  1686. SERIAL_PROTOCOL_F( eqnBVector[yy+xx*n_points]-mean, 5);
  1687. }
  1688. SERIAL_PROTOCOLPGM(" \n");
  1689. }
  1690. SERIAL_PROTOCOLPGM(" \n");
  1691. #endif // ORIGIN_BACK_LEFT
  1692.  
  1693.  
  1694. #ifdef ORIGIN_FRONT_LEFT
  1695. for(yy=n_points-1; yy>=0; yy--) {
  1696. for(xx=0; xx<n_points; xx++) {
  1697. SERIAL_PROTOCOLPGM(" ");
  1698. if ( eqnBVector[yy*n_points+xx]-mean >= 0.0)
  1699. SERIAL_PROTOCOLPGM("+");
  1700. else
  1701. SERIAL_PROTOCOLPGM("-"); // we need this extra - because Proterface uses a preportional
  1702. // font and it causes the columns to not line up nice without it.
  1703. SERIAL_PROTOCOL_F( eqnBVector[yy*n_points+xx]-mean, 5);
  1704. }
  1705. SERIAL_PROTOCOLPGM(" \n");
  1706. }
  1707. SERIAL_PROTOCOLPGM(" \n");
  1708. #endif // ORIGIN_FRONT_LEFT
  1709.  
  1710. #ifdef ORIGIN_FRONT_RIGHT
  1711. for(yy=0; yy<n_points; yy--) {
  1712. for(xx=0; xx<n_points; xx++) {
  1713. SERIAL_PROTOCOLPGM(" ");
  1714. if ( eqnBVector[n_points*n_points-yy-n_points*xx-1]-mean >= 0.0)
  1715. SERIAL_PROTOCOLPGM("+");
  1716. else
  1717. SERIAL_PROTOCOLPGM("-"); // we need this extra - because Proterface uses a preportional
  1718. // font and it causes the columns to not line up nice without it.
  1719. SERIAL_PROTOCOL_F( eqnBVector[n_points*n_points-yy-n_points*xx-1]-mean, 5);
  1720. }
  1721. SERIAL_PROTOCOLPGM(" \n");
  1722. }
  1723. SERIAL_PROTOCOLPGM(" \n");
  1724. #endif // ORIGIN_FRONT_RIGHT
  1725. }
  1726.  
  1727.  
  1728. set_bed_level_equation_lsq(plane_equation_coefficients);
  1729.  
  1730. free(plane_equation_coefficients);
  1731.  
  1732. #else // AUTO_BED_LEVELING_GRID not defined
  1733.  
  1734. // Probe at 3 arbitrary points
  1735. // probe 1
  1736. float z_at_pt_1 = probe_pt(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, Z_RAISE_BEFORE_PROBING, retract_flag, verbose_level);
  1737.  
  1738. // probe 2
  1739. float z_at_pt_2 = probe_pt(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS, retract_flag, verbose_level);
  1740.  
  1741. // probe 3
  1742. float z_at_pt_3 = probe_pt(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS, retract_flag, verbose_level);
  1743.  
  1744. clean_up_after_endstop_move();
  1745.  
  1746. set_bed_level_equation_3pts(z_at_pt_1, z_at_pt_2, z_at_pt_3);
  1747.  
  1748.  
  1749. #endif // AUTO_BED_LEVELING_GRID
  1750. st_synchronize();
  1751.  
  1752. if (verbose_level>0)
  1753. plan_bed_level_matrix.debug(" \n\nBed Level Correction Matrix:");
  1754.  
  1755. // The following code correct the Z height difference from z-probe position and hotend tip position.
  1756. // The Z height on homing is measured by Z-Probe, but the probe is quite far from the hotend.
  1757. // When the bed is uneven, this height must be corrected.
  1758. real_z = float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]; //get the real Z (since the auto bed leveling is already correcting the plane)
  1759. x_tmp = current_position[X_AXIS] + X_PROBE_OFFSET_FROM_EXTRUDER;
  1760. y_tmp = current_position[Y_AXIS] + Y_PROBE_OFFSET_FROM_EXTRUDER;
  1761. z_tmp = current_position[Z_AXIS];
  1762.  
  1763. apply_rotation_xyz(plan_bed_level_matrix, x_tmp, y_tmp, z_tmp); //Apply the correction sending the probe offset
  1764. current_position[Z_AXIS] = z_tmp - real_z + current_position[Z_AXIS]; //The difference is added to current position and sent to planner.
  1765. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1766.  
  1767. do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS);
  1768.  
  1769. retract_z_probe();
  1770. }
  1771. break;
  1772.  
  1773. case 30: // G30 Single Z Probe
  1774. {
  1775. engage_z_probe(); // Engage Z Servo endstop if available
  1776.  
  1777. st_synchronize();
  1778. // TODO: make sure the bed_level_rotation_matrix is identity or the planner will get set incorectly
  1779. setup_for_endstop_move();
  1780.  
  1781. feedrate = homing_feedrate[Z_AXIS];
  1782.  
  1783. run_z_probe();
  1784. SERIAL_PROTOCOLPGM(MSG_BED);
  1785. SERIAL_PROTOCOLPGM(" X: ");
  1786. SERIAL_PROTOCOL(current_position[X_AXIS]);
  1787. SERIAL_PROTOCOLPGM(" Y: ");
  1788. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  1789. SERIAL_PROTOCOLPGM(" Z: ");
  1790. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  1791. SERIAL_PROTOCOLPGM("\n");
  1792.  
  1793. clean_up_after_endstop_move();
  1794.  
  1795. retract_z_probe(); // Retract Z Servo endstop if available
  1796. }
  1797. break;
  1798. #endif // ENABLE_AUTO_BED_LEVELING
  1799. case 90: // G90
  1800. relative_mode = false;
  1801. break;
  1802. case 91: // G91
  1803. relative_mode = true;
  1804. break;
  1805. case 92: // G92
  1806. if(!code_seen(axis_codes[E_AXIS]))
  1807. st_synchronize();
  1808. for(int8_t i=0; i < NUM_AXIS; i++) {
  1809. if(code_seen(axis_codes[i])) {
  1810. if(i == E_AXIS) {
  1811. current_position[i] = code_value();
  1812. plan_set_e_position(current_position[E_AXIS]);
  1813. }
  1814. else {
  1815. current_position[i] = code_value()+add_homeing[i];
  1816. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1817. }
  1818. }
  1819. }
  1820. break;
  1821. }
  1822. }
  1823.  
  1824. else if(code_seen('M'))
  1825. {
  1826. switch( (int)code_value() )
  1827. {
  1828. #ifdef ULTIPANEL
  1829. case 0: // M0 - Unconditional stop - Wait for user button press on LCD
  1830. case 1: // M1 - Conditional stop - Wait for user button press on LCD
  1831. {
  1832. LCD_MESSAGEPGM(MSG_USERWAIT);
  1833. codenum = 0;
  1834. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  1835. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  1836.  
  1837. st_synchronize();
  1838. previous_millis_cmd = millis();
  1839. if (codenum > 0){
  1840. codenum += millis(); // keep track of when we started waiting
  1841. while(millis() < codenum && !lcd_clicked()){
  1842. manage_heater();
  1843. manage_inactivity();
  1844. lcd_update();
  1845. }
  1846. }else{
  1847. while(!lcd_clicked()){
  1848. manage_heater();
  1849. manage_inactivity();
  1850. lcd_update();
  1851. }
  1852. }
  1853. LCD_MESSAGEPGM(MSG_RESUMING);
  1854. }
  1855. break;
  1856. #endif
  1857. case 17:
  1858. LCD_MESSAGEPGM(MSG_NO_MOVE);
  1859. enable_x();
  1860. enable_y();
  1861. enable_z();
  1862. enable_e0();
  1863. enable_e1();
  1864. enable_e2();
  1865. break;
  1866.  
  1867. #ifdef SDSUPPORT
  1868. case 20: // M20 - list SD card
  1869. SERIAL_PROTOCOLLNPGM(MSG_BEGIN_FILE_LIST);
  1870. card.ls();
  1871. SERIAL_PROTOCOLLNPGM(MSG_END_FILE_LIST);
  1872. break;
  1873. case 21: // M21 - init SD card
  1874.  
  1875. card.initsd();
  1876.  
  1877. break;
  1878. case 22: //M22 - release SD card
  1879. card.release();
  1880.  
  1881. break;
  1882. case 23: //M23 - Select file
  1883. starpos = (strchr(strchr_pointer + 4,'*'));
  1884. if(starpos!=NULL)
  1885. *(starpos-1)='\0';
  1886. card.openFile(strchr_pointer + 4,true);
  1887. break;
  1888. case 24: //M24 - Start SD print
  1889. card.startFileprint();
  1890. starttime=millis();
  1891. break;
  1892. case 25: //M25 - Pause SD print
  1893. card.pauseSDPrint();
  1894. break;
  1895. case 26: //M26 - Set SD index
  1896. if(card.cardOK && code_seen('S')) {
  1897. card.setIndex(code_value_long());
  1898. }
  1899. break;
  1900. case 27: //M27 - Get SD status
  1901. card.getStatus();
  1902. break;
  1903. case 28: //M28 - Start SD write
  1904. starpos = (strchr(strchr_pointer + 4,'*'));
  1905. if(starpos != NULL){
  1906. char* npos = strchr(cmdbuffer[bufindr], 'N');
  1907. strchr_pointer = strchr(npos,' ') + 1;
  1908. *(starpos-1) = '\0';
  1909. }
  1910. card.openFile(strchr_pointer+4,false);
  1911. break;
  1912. case 29: //M29 - Stop SD write
  1913. //processed in write to file routine above
  1914. //card,saving = false;
  1915. break;
  1916. case 30: //M30 <filename> Delete File
  1917. if (card.cardOK){
  1918. card.closefile();
  1919. starpos = (strchr(strchr_pointer + 4,'*'));
  1920. if(starpos != NULL){
  1921. char* npos = strchr(cmdbuffer[bufindr], 'N');
  1922. strchr_pointer = strchr(npos,' ') + 1;
  1923. *(starpos-1) = '\0';
  1924. }
  1925. card.removeFile(strchr_pointer + 4);
  1926. }
  1927. break;
  1928. case 32: //M32 - Select file and start SD print
  1929. {
  1930. if(card.sdprinting) {
  1931. st_synchronize();
  1932.  
  1933. }
  1934. starpos = (strchr(strchr_pointer + 4,'*'));
  1935.  
  1936. char* namestartpos = (strchr(strchr_pointer + 4,'!')); //find ! to indicate filename string start.
  1937. if(namestartpos==NULL)
  1938. {
  1939. namestartpos=strchr_pointer + 4; //default name position, 4 letters after the M
  1940. }
  1941. else
  1942. namestartpos++; //to skip the '!'
  1943.  
  1944. if(starpos!=NULL)
  1945. *(starpos-1)='\0';
  1946.  
  1947. bool call_procedure=(code_seen('P'));
  1948.  
  1949. if(strchr_pointer>namestartpos)
  1950. call_procedure=false; //false alert, 'P' found within filename
  1951.  
  1952. if( card.cardOK )
  1953. {
  1954. card.openFile(namestartpos,true,!call_procedure);
  1955. if(code_seen('S'))
  1956. if(strchr_pointer<namestartpos) //only if "S" is occuring _before_ the filename
  1957. card.setIndex(code_value_long());
  1958. card.startFileprint();
  1959. if(!call_procedure)
  1960. starttime=millis(); //procedure calls count as normal print time.
  1961. }
  1962. } break;
  1963. case 928: //M928 - Start SD write
  1964. starpos = (strchr(strchr_pointer + 5,'*'));
  1965. if(starpos != NULL){
  1966. char* npos = strchr(cmdbuffer[bufindr], 'N');
  1967. strchr_pointer = strchr(npos,' ') + 1;
  1968. *(starpos-1) = '\0';
  1969. }
  1970. card.openLogFile(strchr_pointer+5);
  1971. break;
  1972.  
  1973. #endif //SDSUPPORT
  1974.  
  1975. case 31: //M31 take time since the start of the SD print or an M109 command
  1976. {
  1977. stoptime=millis();
  1978. char time[30];
  1979. unsigned long t=(stoptime-starttime)/1000;
  1980. int sec,min;
  1981. min=t/60;
  1982. sec=t%60;
  1983. sprintf_P(time, PSTR("%i min, %i sec"), min, sec);
  1984. SERIAL_ECHO_START;
  1985. SERIAL_ECHOLN(time);
  1986. lcd_setstatus(time);
  1987. autotempShutdown();
  1988. }
  1989. break;
  1990. case 42: //M42 -Change pin status via gcode
  1991. if (code_seen('S'))
  1992. {
  1993. int pin_status = code_value();
  1994. int pin_number = LED_PIN;
  1995. if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
  1996. pin_number = code_value();
  1997. for(int8_t i = 0; i < (int8_t)(sizeof(sensitive_pins)/sizeof(int)); i++)
  1998. {
  1999. if (sensitive_pins[i] == pin_number)
  2000. {
  2001. pin_number = -1;
  2002. break;
  2003. }
  2004. }
  2005. #if defined(FAN_PIN) && FAN_PIN > -1
  2006. if (pin_number == FAN_PIN)
  2007. fanSpeed = pin_status;
  2008. #endif
  2009. if (pin_number > -1)
  2010. {
  2011. pinMode(pin_number, OUTPUT);
  2012. digitalWrite(pin_number, pin_status);
  2013. analogWrite(pin_number, pin_status);
  2014. }
  2015. }
  2016. break;
  2017. case 104: // M104
  2018. if(setTargetedHotend(104)){
  2019. break;
  2020. }
  2021. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  2022. #ifdef DUAL_X_CARRIAGE
  2023. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && tmp_extruder == 0)
  2024. setTargetHotend1(code_value() == 0.0 ? 0.0 : code_value() + duplicate_extruder_temp_offset);
  2025. #endif
  2026. setWatch();
  2027. break;
  2028. case 112: // M112 -Emergency Stop
  2029. kill();
  2030. break;
  2031. case 140: // M140 set bed temp
  2032. if (code_seen('S')) setTargetBed(code_value());
  2033. break;
  2034. case 105 : // M105
  2035. if(setTargetedHotend(105)){
  2036. break;
  2037. }
  2038. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  2039. SERIAL_PROTOCOLPGM("ok T:");
  2040. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  2041. SERIAL_PROTOCOLPGM(" /");
  2042. SERIAL_PROTOCOL_F(degTargetHotend(tmp_extruder),1);
  2043. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  2044. SERIAL_PROTOCOLPGM(" B:");
  2045. SERIAL_PROTOCOL_F(degBed(),1);
  2046. SERIAL_PROTOCOLPGM(" /");
  2047. SERIAL_PROTOCOL_F(degTargetBed(),1);
  2048. #endif //TEMP_BED_PIN
  2049. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  2050. SERIAL_PROTOCOLPGM(" T");
  2051. SERIAL_PROTOCOL(cur_extruder);
  2052. SERIAL_PROTOCOLPGM(":");
  2053. SERIAL_PROTOCOL_F(degHotend(cur_extruder),1);
  2054. SERIAL_PROTOCOLPGM(" /");
  2055. SERIAL_PROTOCOL_F(degTargetHotend(cur_extruder),1);
  2056. }
  2057. #else
  2058. SERIAL_ERROR_START;
  2059. SERIAL_ERRORLNPGM(MSG_ERR_NO_THERMISTORS);
  2060. #endif
  2061.  
  2062. SERIAL_PROTOCOLPGM(" @:");
  2063. #ifdef EXTRUDER_WATTS
  2064. SERIAL_PROTOCOL((EXTRUDER_WATTS * getHeaterPower(tmp_extruder))/127);
  2065. SERIAL_PROTOCOLPGM("W");
  2066. #else
  2067. SERIAL_PROTOCOL(getHeaterPower(tmp_extruder));
  2068. #endif
  2069.  
  2070. SERIAL_PROTOCOLPGM(" B@:");
  2071. #ifdef BED_WATTS
  2072. SERIAL_PROTOCOL((BED_WATTS * getHeaterPower(-1))/127);
  2073. SERIAL_PROTOCOLPGM("W");
  2074. #else
  2075. SERIAL_PROTOCOL(getHeaterPower(-1));
  2076. #endif
  2077.  
  2078. #ifdef SHOW_TEMP_ADC_VALUES
  2079. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  2080. SERIAL_PROTOCOLPGM(" ADC B:");
  2081. SERIAL_PROTOCOL_F(degBed(),1);
  2082. SERIAL_PROTOCOLPGM("C->");
  2083. SERIAL_PROTOCOL_F(rawBedTemp()/OVERSAMPLENR,0);
  2084. #endif
  2085. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  2086. SERIAL_PROTOCOLPGM(" T");
  2087. SERIAL_PROTOCOL(cur_extruder);
  2088. SERIAL_PROTOCOLPGM(":");
  2089. SERIAL_PROTOCOL_F(degHotend(cur_extruder),1);
  2090. SERIAL_PROTOCOLPGM("C->");
  2091. SERIAL_PROTOCOL_F(rawHotendTemp(cur_extruder)/OVERSAMPLENR,0);
  2092. }
  2093. #endif
  2094.  
  2095. SERIAL_PROTOCOLLN("");
  2096. return;
  2097. break;
  2098. case 109:
  2099. {// M109 - Wait for extruder heater to reach target.
  2100. if(setTargetedHotend(109)){
  2101. break;
  2102. }
  2103. LCD_MESSAGEPGM(MSG_HEATING);
  2104. #ifdef AUTOTEMP
  2105. autotemp_enabled=false;
  2106. #endif
  2107. if (code_seen('S')) {
  2108. setTargetHotend(code_value(), tmp_extruder);
  2109. #ifdef DUAL_X_CARRIAGE
  2110. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && tmp_extruder == 0)
  2111. setTargetHotend1(code_value() == 0.0 ? 0.0 : code_value() + duplicate_extruder_temp_offset);
  2112. #endif
  2113. CooldownNoWait = true;
  2114. } else if (code_seen('R')) {
  2115. setTargetHotend(code_value(), tmp_extruder);
  2116. #ifdef DUAL_X_CARRIAGE
  2117. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && tmp_extruder == 0)
  2118. setTargetHotend1(code_value() == 0.0 ? 0.0 : code_value() + duplicate_extruder_temp_offset);
  2119. #endif
  2120. CooldownNoWait = false;
  2121. }
  2122. #ifdef AUTOTEMP
  2123. if (code_seen('S')) autotemp_min=code_value();
  2124. if (code_seen('B')) autotemp_max=code_value();
  2125. if (code_seen('F'))
  2126. {
  2127. autotemp_factor=code_value();
  2128. autotemp_enabled=true;
  2129. }
  2130. #endif
  2131.  
  2132. setWatch();
  2133. codenum = millis();
  2134.  
  2135. /* See if we are heating up or cooling down */
  2136. target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling
  2137.  
  2138. cancel_heatup = false;
  2139.  
  2140. #ifdef TEMP_RESIDENCY_TIME
  2141. long residencyStart;
  2142. residencyStart = -1;
  2143. /* continue to loop until we have reached the target temp
  2144. _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
  2145. while((!cancel_heatup)&&((residencyStart == -1) ||
  2146. (residencyStart >= 0 && (((unsigned int) (millis() - residencyStart)) < (TEMP_RESIDENCY_TIME * 1000UL)))) ) {
  2147. #else
  2148. while ( target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder)&&(CooldownNoWait==false)) ) {
  2149. #endif //TEMP_RESIDENCY_TIME
  2150. if( (millis() - codenum) > 1000UL )
  2151. { //Print Temp Reading and remaining time every 1 second while heating up/cooling down
  2152. SERIAL_PROTOCOLPGM("T:");
  2153. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  2154. SERIAL_PROTOCOLPGM(" E:");
  2155. SERIAL_PROTOCOL((int)tmp_extruder);
  2156. #ifdef TEMP_RESIDENCY_TIME
  2157. SERIAL_PROTOCOLPGM(" W:");
  2158. if(residencyStart > -1)
  2159. {
  2160. codenum = ((TEMP_RESIDENCY_TIME * 1000UL) - (millis() - residencyStart)) / 1000UL;
  2161. SERIAL_PROTOCOLLN( codenum );
  2162. }
  2163. else
  2164. {
  2165. SERIAL_PROTOCOLLN( "?" );
  2166. }
  2167. #else
  2168. SERIAL_PROTOCOLLN("");
  2169. #endif
  2170. codenum = millis();
  2171. }
  2172. manage_heater();
  2173. manage_inactivity();
  2174. lcd_update();
  2175. #ifdef TEMP_RESIDENCY_TIME
  2176. /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
  2177. or when current temp falls outside the hysteresis after target temp was reached */
  2178. if ((residencyStart == -1 && target_direction && (degHotend(tmp_extruder) >= (degTargetHotend(tmp_extruder)-TEMP_WINDOW))) ||
  2179. (residencyStart == -1 && !target_direction && (degHotend(tmp_extruder) <= (degTargetHotend(tmp_extruder)+TEMP_WINDOW))) ||
  2180. (residencyStart > -1 && labs(degHotend(tmp_extruder) - degTargetHotend(tmp_extruder)) > TEMP_HYSTERESIS) )
  2181. {
  2182. residencyStart = millis();
  2183. }
  2184. #endif //TEMP_RESIDENCY_TIME
  2185. }
  2186. LCD_MESSAGEPGM(MSG_HEATING_COMPLETE);
  2187. starttime=millis();
  2188. previous_millis_cmd = millis();
  2189. }
  2190. break;
  2191. case 190: // M190 - Wait for bed heater to reach target.
  2192. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  2193. LCD_MESSAGEPGM(MSG_BED_HEATING);
  2194. if (code_seen('S')) {
  2195. setTargetBed(code_value());
  2196. CooldownNoWait = true;
  2197. } else if (code_seen('R')) {
  2198. setTargetBed(code_value());
  2199. CooldownNoWait = false;
  2200. }
  2201. codenum = millis();
  2202.  
  2203. cancel_heatup = false;
  2204. target_direction = isHeatingBed(); // true if heating, false if cooling
  2205.  
  2206. while ( (target_direction)&&(!cancel_heatup) ? (isHeatingBed()) : (isCoolingBed()&&(CooldownNoWait==false)) )
  2207. {
  2208. if(( millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
  2209. {
  2210. float tt=degHotend(active_extruder);
  2211. SERIAL_PROTOCOLPGM("T:");
  2212. SERIAL_PROTOCOL(tt);
  2213. SERIAL_PROTOCOLPGM(" E:");
  2214. SERIAL_PROTOCOL((int)active_extruder);
  2215. SERIAL_PROTOCOLPGM(" B:");
  2216. SERIAL_PROTOCOL_F(degBed(),1);
  2217. SERIAL_PROTOCOLLN("");
  2218. codenum = millis();
  2219. }
  2220. manage_heater();
  2221. manage_inactivity();
  2222. lcd_update();
  2223. }
  2224. LCD_MESSAGEPGM(MSG_BED_DONE);
  2225. previous_millis_cmd = millis();
  2226. #endif
  2227. break;
  2228.  
  2229. #if defined(FAN_PIN) && FAN_PIN > -1
  2230. case 106: //M106 Fan On
  2231. if (code_seen('S')){
  2232. fanSpeed=constrain(code_value(),0,255);
  2233. }
  2234. else {
  2235. fanSpeed=255;
  2236. }
  2237. break;
  2238. case 107: //M107 Fan Off
  2239. fanSpeed = 0;
  2240. break;
  2241. #endif //FAN_PIN
  2242. #ifdef BARICUDA
  2243. // PWM for HEATER_1_PIN
  2244. #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1
  2245. case 126: //M126 valve open
  2246. if (code_seen('S')){
  2247. ValvePressure=constrain(code_value(),0,255);
  2248. }
  2249. else {
  2250. ValvePressure=255;
  2251. }
  2252. break;
  2253. case 127: //M127 valve closed
  2254. ValvePressure = 0;
  2255. break;
  2256. #endif //HEATER_1_PIN
  2257.  
  2258. // PWM for HEATER_2_PIN
  2259. #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1
  2260. case 128: //M128 valve open
  2261. if (code_seen('S')){
  2262. EtoPPressure=constrain(code_value(),0,255);
  2263. }
  2264. else {
  2265. EtoPPressure=255;
  2266. }
  2267. break;
  2268. case 129: //M129 valve closed
  2269. EtoPPressure = 0;
  2270. break;
  2271. #endif //HEATER_2_PIN
  2272. #endif
  2273.  
  2274. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  2275. case 80: // M80 - Turn on Power Supply
  2276. SET_OUTPUT(PS_ON_PIN); //GND
  2277. WRITE(PS_ON_PIN, PS_ON_AWAKE);
  2278.  
  2279. // If you have a switch on suicide pin, this is useful
  2280. // if you want to start another print with suicide feature after
  2281. // a print without suicide...
  2282. #if defined SUICIDE_PIN && SUICIDE_PIN > -1
  2283. SET_OUTPUT(SUICIDE_PIN);
  2284. WRITE(SUICIDE_PIN, HIGH);
  2285. #endif
  2286.  
  2287. #ifdef ULTIPANEL
  2288. powersupply = true;
  2289. LCD_MESSAGEPGM(WELCOME_MSG);
  2290. lcd_update();
  2291. #endif
  2292. break;
  2293. #endif
  2294.  
  2295. case 81: // M81 - Turn off Power Supply
  2296. disable_heater();
  2297. st_synchronize();
  2298. disable_e0();
  2299. disable_e1();
  2300. disable_e2();
  2301. finishAndDisableSteppers();
  2302. fanSpeed = 0;
  2303. delay(1000); // Wait a little before to switch off
  2304. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  2305. st_synchronize();
  2306. suicide();
  2307. #elif defined(PS_ON_PIN) && PS_ON_PIN > -1
  2308. SET_OUTPUT(PS_ON_PIN);
  2309. WRITE(PS_ON_PIN, PS_ON_ASLEEP);
  2310. #endif
  2311. #ifdef ULTIPANEL
  2312. powersupply = false;
  2313. LCD_MESSAGEPGM(MACHINE_NAME" "MSG_OFF".");
  2314. lcd_update();
  2315. #endif
  2316. break;
  2317.  
  2318. case 82:
  2319. axis_relative_modes[3] = false;
  2320. break;
  2321. case 83:
  2322. axis_relative_modes[3] = true;
  2323. break;
  2324. case 18: //compatibility
  2325. case 84: // M84
  2326. if(code_seen('S')){
  2327. stepper_inactive_time = code_value() * 1000;
  2328. }
  2329. else
  2330. {
  2331. bool all_axis = !((code_seen(axis_codes[X_AXIS])) || (code_seen(axis_codes[Y_AXIS])) || (code_seen(axis_codes[Z_AXIS]))|| (code_seen(axis_codes[E_AXIS])));
  2332. if(all_axis)
  2333. {
  2334. st_synchronize();
  2335. disable_e0();
  2336. disable_e1();
  2337. disable_e2();
  2338. finishAndDisableSteppers();
  2339. }
  2340. else
  2341. {
  2342. st_synchronize();
  2343. if(code_seen('X')) disable_x();
  2344. if(code_seen('Y')) disable_y();
  2345. if(code_seen('Z')) disable_z();
  2346. #if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
  2347. if(code_seen('E')) {
  2348. disable_e0();
  2349. disable_e1();
  2350. disable_e2();
  2351. }
  2352. #endif
  2353. }
  2354. }
  2355. break;
  2356. case 85: // M85
  2357. if(code_seen('S')) {
  2358. max_inactive_time = code_value() * 1000;
  2359. }
  2360. break;
  2361. case 92: // M92
  2362. for(int8_t i=0; i < NUM_AXIS; i++)
  2363. {
  2364. if(code_seen(axis_codes[i]))
  2365. {
  2366. if(i == 3) { // E
  2367. float value = code_value();
  2368. if(value < 20.0) {
  2369. float factor = axis_steps_per_unit[i] / value; // increase e constants if M92 E14 is given for netfab.
  2370. max_e_jerk *= factor;
  2371. max_feedrate[i] *= factor;
  2372. axis_steps_per_sqr_second[i] *= factor;
  2373. }
  2374. axis_steps_per_unit[i] = value;
  2375. }
  2376. else {
  2377. axis_steps_per_unit[i] = code_value();
  2378. }
  2379. }
  2380. }
  2381. break;
  2382. case 115: // M115
  2383. SERIAL_PROTOCOLPGM(MSG_M115_REPORT);
  2384. break;
  2385. case 117: // M117 display message
  2386. starpos = (strchr(strchr_pointer + 5,'*'));
  2387. if(starpos!=NULL)
  2388. *(starpos-1)='\0';
  2389. lcd_setstatus(strchr_pointer + 5);
  2390. break;
  2391. case 114: // M114
  2392. SERIAL_PROTOCOLPGM("X:");
  2393. SERIAL_PROTOCOL(current_position[X_AXIS]);
  2394. SERIAL_PROTOCOLPGM(" Y:");
  2395. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  2396. SERIAL_PROTOCOLPGM(" Z:");
  2397. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  2398. SERIAL_PROTOCOLPGM(" E:");
  2399. SERIAL_PROTOCOL(current_position[E_AXIS]);
  2400.  
  2401. SERIAL_PROTOCOLPGM(MSG_COUNT_X);
  2402. SERIAL_PROTOCOL(float(st_get_position(X_AXIS))/axis_steps_per_unit[X_AXIS]);
  2403. SERIAL_PROTOCOLPGM(" Y:");
  2404. SERIAL_PROTOCOL(float(st_get_position(Y_AXIS))/axis_steps_per_unit[Y_AXIS]);
  2405. SERIAL_PROTOCOLPGM(" Z:");
  2406. SERIAL_PROTOCOL(float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]);
  2407.  
  2408. SERIAL_PROTOCOLLN("");
  2409. break;
  2410. case 120: // M120
  2411. enable_endstops(false) ;
  2412. break;
  2413. case 121: // M121
  2414. enable_endstops(true) ;
  2415. break;
  2416. case 119: // M119
  2417. SERIAL_PROTOCOLLN(MSG_M119_REPORT);
  2418. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  2419. SERIAL_PROTOCOLPGM(MSG_X_MIN);
  2420. SERIAL_PROTOCOLLN(((READ(X_MIN_PIN)^X_MIN_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  2421. #endif
  2422. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  2423. SERIAL_PROTOCOLPGM(MSG_X_MAX);
  2424. SERIAL_PROTOCOLLN(((READ(X_MAX_PIN)^X_MAX_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  2425. #endif
  2426. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  2427. SERIAL_PROTOCOLPGM(MSG_Y_MIN);
  2428. SERIAL_PROTOCOLLN(((READ(Y_MIN_PIN)^Y_MIN_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  2429. #endif
  2430. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  2431. SERIAL_PROTOCOLPGM(MSG_Y_MAX);
  2432. SERIAL_PROTOCOLLN(((READ(Y_MAX_PIN)^Y_MAX_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  2433. #endif
  2434. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  2435. SERIAL_PROTOCOLPGM(MSG_Z_MIN);
  2436. SERIAL_PROTOCOLLN(((READ(Z_MIN_PIN)^Z_MIN_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  2437. #endif
  2438. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  2439. SERIAL_PROTOCOLPGM(MSG_Z_MAX);
  2440. SERIAL_PROTOCOLLN(((READ(Z_MAX_PIN)^Z_MAX_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  2441. #endif
  2442. break;
  2443. //TODO: update for all axis, use for loop
  2444. #ifdef BLINKM
  2445. case 150: // M150
  2446. {
  2447. byte red;
  2448. byte grn;
  2449. byte blu;
  2450.  
  2451. if(code_seen('R')) red = code_value();
  2452. if(code_seen('U')) grn = code_value();
  2453. if(code_seen('B')) blu = code_value();
  2454.  
  2455. SendColors(red,grn,blu);
  2456. }
  2457. break;
  2458. #endif //BLINKM
  2459. case 200: // M200 D<millimeters> set filament diameter and set E axis units to cubic millimeters (use S0 to set back to millimeters).
  2460. {
  2461. float area = .0;
  2462. float radius = .0;
  2463. if(code_seen('D')) {
  2464. radius = (float)code_value() * .5;
  2465. if(radius == 0) {
  2466. area = 1;
  2467. } else {
  2468. area = M_PI * pow(radius, 2);
  2469. }
  2470. } else {
  2471. //reserved for setting filament diameter via UFID or filament measuring device
  2472. break;
  2473. }
  2474. tmp_extruder = active_extruder;
  2475. if(code_seen('T')) {
  2476. tmp_extruder = code_value();
  2477. if(tmp_extruder >= EXTRUDERS) {
  2478. SERIAL_ECHO_START;
  2479. SERIAL_ECHO(MSG_M200_INVALID_EXTRUDER);
  2480. break;
  2481. }
  2482. }
  2483. volumetric_multiplier[tmp_extruder] = 1 / area;
  2484. }
  2485. break;
  2486. case 201: // M201
  2487. for(int8_t i=0; i < NUM_AXIS; i++)
  2488. {
  2489. if(code_seen(axis_codes[i]))
  2490. {
  2491. max_acceleration_units_per_sq_second[i] = code_value();
  2492. }
  2493. }
  2494. // steps per sq second need to be updated to agree with the units per sq second (as they are what is used in the planner)
  2495. reset_acceleration_rates();
  2496. break;
  2497. #if 0 // Not used for Sprinter/grbl gen6
  2498. case 202: // M202
  2499. for(int8_t i=0; i < NUM_AXIS; i++) {
  2500. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  2501. }
  2502. break;
  2503. #endif
  2504. case 203: // M203 max feedrate mm/sec
  2505. for(int8_t i=0; i < NUM_AXIS; i++) {
  2506. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value();
  2507. }
  2508. break;
  2509. case 204: // M204 acclereration S normal moves T filmanent only moves
  2510. {
  2511. if(code_seen('S')) acceleration = code_value() ;
  2512. if(code_seen('T')) retract_acceleration = code_value() ;
  2513. }
  2514. break;
  2515. case 205: //M205 advanced settings: minimum travel speed S=while printing T=travel only, B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk
  2516. {
  2517. if(code_seen('S')) minimumfeedrate = code_value();
  2518. if(code_seen('T')) mintravelfeedrate = code_value();
  2519. if(code_seen('B')) minsegmenttime = code_value() ;
  2520. if(code_seen('X')) max_xy_jerk = code_value() ;
  2521. if(code_seen('Z')) max_z_jerk = code_value() ;
  2522. if(code_seen('E')) max_e_jerk = code_value() ;
  2523. }
  2524. break;
  2525. case 206: // M206 additional homeing offset
  2526. for(int8_t i=0; i < 3; i++)
  2527. {
  2528. if(code_seen(axis_codes[i])) add_homeing[i] = code_value();
  2529. }
  2530. break;
  2531. #ifdef DELTA
  2532. case 665: // M665 set delta configurations L<diagonal_rod> R<delta_radius> S<segments_per_sec>
  2533. if(code_seen('L')) {
  2534. delta_diagonal_rod= code_value();
  2535. }
  2536. if(code_seen('R')) {
  2537. delta_radius= code_value();
  2538. }
  2539. if(code_seen('S')) {
  2540. delta_segments_per_second= code_value();
  2541. }
  2542.  
  2543. recalc_delta_settings(delta_radius, delta_diagonal_rod);
  2544. break;
  2545. case 666: // M666 set delta endstop adjustemnt
  2546. for(int8_t i=0; i < 3; i++)
  2547. {
  2548. if(code_seen(axis_codes[i])) endstop_adj[i] = code_value();
  2549. }
  2550. break;
  2551. #endif
  2552. #ifdef FWRETRACT
  2553. case 207: //M207 - set retract length S[positive mm] F[feedrate mm/min] Z[additional zlift/hop]
  2554. {
  2555. if(code_seen('S'))
  2556. {
  2557. retract_length = code_value() ;
  2558. }
  2559. if(code_seen('F'))
  2560. {
  2561. retract_feedrate = code_value()/60 ;
  2562. }
  2563. if(code_seen('Z'))
  2564. {
  2565. retract_zlift = code_value() ;
  2566. }
  2567. }break;
  2568. case 208: // M208 - set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/min]
  2569. {
  2570. if(code_seen('S'))
  2571. {
  2572. retract_recover_length = code_value() ;
  2573. }
  2574. if(code_seen('F'))
  2575. {
  2576. retract_recover_feedrate = code_value()/60 ;
  2577. }
  2578. }break;
  2579. case 209: // M209 - S<1=true/0=false> enable automatic retract detect if the slicer did not support G10/11: every normal extrude-only move will be classified as retract depending on the direction.
  2580. {
  2581. if(code_seen('S'))
  2582. {
  2583. int t= code_value() ;
  2584. switch(t)
  2585. {
  2586. case 0:
  2587. {
  2588. autoretract_enabled=false;
  2589. retracted[0]=false;
  2590. #if EXTRUDERS > 1
  2591. retracted[1]=false;
  2592. #endif
  2593. #if EXTRUDERS > 2
  2594. retracted[2]=false;
  2595. #endif
  2596. }break;
  2597. case 1:
  2598. {
  2599. autoretract_enabled=true;
  2600. retracted[0]=false;
  2601. #if EXTRUDERS > 1
  2602. retracted[1]=false;
  2603. #endif
  2604. #if EXTRUDERS > 2
  2605. retracted[2]=false;
  2606. #endif
  2607. }break;
  2608. default:
  2609. SERIAL_ECHO_START;
  2610. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  2611. SERIAL_ECHO(cmdbuffer[bufindr]);
  2612. SERIAL_ECHOLNPGM("\"");
  2613. }
  2614. }
  2615.  
  2616. }break;
  2617. #endif // FWRETRACT
  2618. #if EXTRUDERS > 1
  2619. case 218: // M218 - set hotend offset (in mm), T<extruder_number> X<offset_on_X> Y<offset_on_Y>
  2620. {
  2621. if(setTargetedHotend(218)){
  2622. break;
  2623. }
  2624. if(code_seen('X'))
  2625. {
  2626. extruder_offset[X_AXIS][tmp_extruder] = code_value();
  2627. }
  2628. if(code_seen('Y'))
  2629. {
  2630. extruder_offset[Y_AXIS][tmp_extruder] = code_value();
  2631. }
  2632. #ifdef DUAL_X_CARRIAGE
  2633. if(code_seen('Z'))
  2634. {
  2635. extruder_offset[Z_AXIS][tmp_extruder] = code_value();
  2636. }
  2637. #endif
  2638. SERIAL_ECHO_START;
  2639. SERIAL_ECHOPGM(MSG_HOTEND_OFFSET);
  2640. for(tmp_extruder = 0; tmp_extruder < EXTRUDERS; tmp_extruder++)
  2641. {
  2642. SERIAL_ECHO(" ");
  2643. SERIAL_ECHO(extruder_offset[X_AXIS][tmp_extruder]);
  2644. SERIAL_ECHO(",");
  2645. SERIAL_ECHO(extruder_offset[Y_AXIS][tmp_extruder]);
  2646. #ifdef DUAL_X_CARRIAGE
  2647. SERIAL_ECHO(",");
  2648. SERIAL_ECHO(extruder_offset[Z_AXIS][tmp_extruder]);
  2649. #endif
  2650. }
  2651. SERIAL_ECHOLN("");
  2652. }break;
  2653. #endif
  2654. case 220: // M220 S<factor in percent>- set speed factor override percentage
  2655. {
  2656. if(code_seen('S'))
  2657. {
  2658. feedmultiply = code_value() ;
  2659. }
  2660. }
  2661. break;
  2662. case 221: // M221 S<factor in percent>- set extrude factor override percentage
  2663. {
  2664. if(code_seen('S'))
  2665. {
  2666. int tmp_code = code_value();
  2667. if (code_seen('T'))
  2668. {
  2669. if(setTargetedHotend(221)){
  2670. break;
  2671. }
  2672. extruder_multiply[tmp_extruder] = tmp_code;
  2673. }
  2674. else
  2675. {
  2676. extrudemultiply = tmp_code ;
  2677. }
  2678. }
  2679. }
  2680. break;
  2681.  
  2682. case 226: // M226 P<pin number> S<pin state>- Wait until the specified pin reaches the state required
  2683. {
  2684. if(code_seen('P')){
  2685. int pin_number = code_value(); // pin number
  2686. int pin_state = -1; // required pin state - default is inverted
  2687.  
  2688. if(code_seen('S')) pin_state = code_value(); // required pin state
  2689.  
  2690. if(pin_state >= -1 && pin_state <= 1){
  2691.  
  2692. for(int8_t i = 0; i < (int8_t)(sizeof(sensitive_pins)/sizeof(int)); i++)
  2693. {
  2694. if (sensitive_pins[i] == pin_number)
  2695. {
  2696. pin_number = -1;
  2697. break;
  2698. }
  2699. }
  2700.  
  2701. if (pin_number > -1)
  2702. {
  2703. st_synchronize();
  2704.  
  2705. pinMode(pin_number, INPUT);
  2706.  
  2707. int target;
  2708. switch(pin_state){
  2709. case 1:
  2710. target = HIGH;
  2711. break;
  2712.  
  2713. case 0:
  2714. target = LOW;
  2715. break;
  2716.  
  2717. case -1:
  2718. target = !digitalRead(pin_number);
  2719. break;
  2720. }
  2721.  
  2722. while(digitalRead(pin_number) != target){
  2723. manage_heater();
  2724. manage_inactivity();
  2725. lcd_update();
  2726. }
  2727. }
  2728. }
  2729. }
  2730. }
  2731. break;
  2732.  
  2733. #if NUM_SERVOS > 0
  2734. case 280: // M280 - set servo position absolute. P: servo index, S: angle or microseconds
  2735. {
  2736. int servo_index = -1;
  2737. int servo_position = 0;
  2738. if (code_seen('P'))
  2739. servo_index = code_value();
  2740. if (code_seen('S')) {
  2741. servo_position = code_value();
  2742. if ((servo_index >= 0) && (servo_index < NUM_SERVOS)) {
  2743. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  2744. servos[servo_index].attach(0);
  2745. #endif
  2746. servos[servo_index].write(servo_position);
  2747. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  2748. delay(PROBE_SERVO_DEACTIVATION_DELAY);
  2749. servos[servo_index].detach();
  2750. #endif
  2751. }
  2752. else {
  2753. SERIAL_ECHO_START;
  2754. SERIAL_ECHO("Servo ");
  2755. SERIAL_ECHO(servo_index);
  2756. SERIAL_ECHOLN(" out of range");
  2757. }
  2758. }
  2759. else if (servo_index >= 0) {
  2760. SERIAL_PROTOCOL(MSG_OK);
  2761. SERIAL_PROTOCOL(" Servo ");
  2762. SERIAL_PROTOCOL(servo_index);
  2763. SERIAL_PROTOCOL(": ");
  2764. SERIAL_PROTOCOL(servos[servo_index].read());
  2765. SERIAL_PROTOCOLLN("");
  2766. }
  2767. }
  2768. break;
  2769. #endif // NUM_SERVOS > 0
  2770.  
  2771. #if (LARGE_FLASH == true && ( BEEPER > 0 || defined(ULTRALCD) || defined(LCD_USE_I2C_BUZZER)))
  2772. case 300: // M300
  2773. {
  2774. int beepS = code_seen('S') ? code_value() : 110;
  2775. int beepP = code_seen('P') ? code_value() : 1000;
  2776. if (beepS > 0)
  2777. {
  2778. #if BEEPER > 0
  2779. tone(BEEPER, beepS);
  2780. delay(beepP);
  2781. noTone(BEEPER);
  2782. #elif defined(ULTRALCD)
  2783. lcd_buzz(beepS, beepP);
  2784. #elif defined(LCD_USE_I2C_BUZZER)
  2785. lcd_buzz(beepP, beepS);
  2786. #endif
  2787. }
  2788. else
  2789. {
  2790. delay(beepP);
  2791. }
  2792. }
  2793. break;
  2794. #endif // M300
  2795.  
  2796. #ifdef PIDTEMP
  2797. case 301: // M301
  2798. {
  2799. if(code_seen('P')) Kp = code_value();
  2800. if(code_seen('I')) Ki = scalePID_i(code_value());
  2801. if(code_seen('D')) Kd = scalePID_d(code_value());
  2802.  
  2803. #ifdef PID_ADD_EXTRUSION_RATE
  2804. if(code_seen('C')) Kc = code_value();
  2805. #endif
  2806.  
  2807. updatePID();
  2808. SERIAL_PROTOCOL(MSG_OK);
  2809. SERIAL_PROTOCOL(" p:");
  2810. SERIAL_PROTOCOL(Kp);
  2811. SERIAL_PROTOCOL(" i:");
  2812. SERIAL_PROTOCOL(unscalePID_i(Ki));
  2813. SERIAL_PROTOCOL(" d:");
  2814. SERIAL_PROTOCOL(unscalePID_d(Kd));
  2815. #ifdef PID_ADD_EXTRUSION_RATE
  2816. SERIAL_PROTOCOL(" c:");
  2817. //Kc does not have scaling applied above, or in resetting defaults
  2818. SERIAL_PROTOCOL(Kc);
  2819. #endif
  2820. SERIAL_PROTOCOLLN("");
  2821. }
  2822. break;
  2823. #endif //PIDTEMP
  2824. #ifdef PIDTEMPBED
  2825. case 304: // M304
  2826. {
  2827. if(code_seen('P')) bedKp = code_value();
  2828. if(code_seen('I')) bedKi = scalePID_i(code_value());
  2829. if(code_seen('D')) bedKd = scalePID_d(code_value());
  2830.  
  2831. updatePID();
  2832. SERIAL_PROTOCOL(MSG_OK);
  2833. SERIAL_PROTOCOL(" p:");
  2834. SERIAL_PROTOCOL(bedKp);
  2835. SERIAL_PROTOCOL(" i:");
  2836. SERIAL_PROTOCOL(unscalePID_i(bedKi));
  2837. SERIAL_PROTOCOL(" d:");
  2838. SERIAL_PROTOCOL(unscalePID_d(bedKd));
  2839. SERIAL_PROTOCOLLN("");
  2840. }
  2841. break;
  2842. #endif //PIDTEMP
  2843. case 240: // M240 Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
  2844. {
  2845. #ifdef CHDK
  2846.  
  2847. SET_OUTPUT(CHDK);
  2848. WRITE(CHDK, HIGH);
  2849. chdkHigh = millis();
  2850. chdkActive = true;
  2851.  
  2852. #else
  2853.  
  2854. #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
  2855. const uint8_t NUM_PULSES=16;
  2856. const float PULSE_LENGTH=0.01524;
  2857. for(int i=0; i < NUM_PULSES; i++) {
  2858. WRITE(PHOTOGRAPH_PIN, HIGH);
  2859. _delay_ms(PULSE_LENGTH);
  2860. WRITE(PHOTOGRAPH_PIN, LOW);
  2861. _delay_ms(PULSE_LENGTH);
  2862. }
  2863. delay(7.33);
  2864. for(int i=0; i < NUM_PULSES; i++) {
  2865. WRITE(PHOTOGRAPH_PIN, HIGH);
  2866. _delay_ms(PULSE_LENGTH);
  2867. WRITE(PHOTOGRAPH_PIN, LOW);
  2868. _delay_ms(PULSE_LENGTH);
  2869. }
  2870. #endif
  2871. #endif //chdk end if
  2872. }
  2873. break;
  2874. #ifdef DOGLCD
  2875. case 250: // M250 Set LCD contrast value: C<value> (value 0..63)
  2876. {
  2877. if (code_seen('C')) {
  2878. lcd_setcontrast( ((int)code_value())&63 );
  2879. }
  2880. SERIAL_PROTOCOLPGM("lcd contrast value: ");
  2881. SERIAL_PROTOCOL(lcd_contrast);
  2882. SERIAL_PROTOCOLLN("");
  2883. }
  2884. break;
  2885. #endif
  2886. #ifdef PREVENT_DANGEROUS_EXTRUDE
  2887. case 302: // allow cold extrudes, or set the minimum extrude temperature
  2888. {
  2889. float temp = .0;
  2890. if (code_seen('S')) temp=code_value();
  2891. set_extrude_min_temp(temp);
  2892. }
  2893. break;
  2894. #endif
  2895. case 303: // M303 PID autotune
  2896. {
  2897. float temp = 150.0;
  2898. int e=0;
  2899. int c=5;
  2900. if (code_seen('E')) e=code_value();
  2901. if (e<0)
  2902. temp=70;
  2903. if (code_seen('S')) temp=code_value();
  2904. if (code_seen('C')) c=code_value();
  2905. PID_autotune(temp, e, c);
  2906. }
  2907. break;
  2908. case 400: // M400 finish all moves
  2909. {
  2910. st_synchronize();
  2911. }
  2912. break;
  2913. #if defined(ENABLE_AUTO_BED_LEVELING) && defined(SERVO_ENDSTOPS)
  2914. case 401:
  2915. {
  2916. engage_z_probe(); // Engage Z Servo endstop if available
  2917. }
  2918. break;
  2919.  
  2920. case 402:
  2921. {
  2922. retract_z_probe(); // Retract Z Servo endstop if enabled
  2923. }
  2924. break;
  2925. #endif
  2926. case 500: // M500 Store settings in EEPROM
  2927. {
  2928. Config_StoreSettings();
  2929. }
  2930. break;
  2931. case 501: // M501 Read settings from EEPROM
  2932. {
  2933. Config_RetrieveSettings();
  2934. }
  2935. break;
  2936. case 502: // M502 Revert to default settings
  2937. {
  2938. Config_ResetDefault();
  2939. }
  2940. break;
  2941. case 503: // M503 print settings currently in memory
  2942. {
  2943. Config_PrintSettings();
  2944. }
  2945. break;
  2946. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  2947. case 540:
  2948. {
  2949. if(code_seen('S')) abort_on_endstop_hit = code_value() > 0;
  2950. }
  2951. break;
  2952. #endif
  2953.  
  2954. #ifdef CUSTOM_M_CODE_SET_Z_PROBE_OFFSET
  2955. case CUSTOM_M_CODE_SET_Z_PROBE_OFFSET:
  2956. {
  2957. float value;
  2958. if (code_seen('Z'))
  2959. {
  2960. value = code_value();
  2961. if ((Z_PROBE_OFFSET_RANGE_MIN <= value) && (value <= Z_PROBE_OFFSET_RANGE_MAX))
  2962. {
  2963. zprobe_zoffset = -value; // compare w/ line 278 of ConfigurationStore.cpp
  2964. SERIAL_ECHO_START;
  2965. SERIAL_ECHOLNPGM(MSG_ZPROBE_ZOFFSET " " MSG_OK);
  2966. SERIAL_PROTOCOLLN("");
  2967. }
  2968. else
  2969. {
  2970. SERIAL_ECHO_START;
  2971. SERIAL_ECHOPGM(MSG_ZPROBE_ZOFFSET);
  2972. SERIAL_ECHOPGM(MSG_Z_MIN);
  2973. SERIAL_ECHO(Z_PROBE_OFFSET_RANGE_MIN);
  2974. SERIAL_ECHOPGM(MSG_Z_MAX);
  2975. SERIAL_ECHO(Z_PROBE_OFFSET_RANGE_MAX);
  2976. SERIAL_PROTOCOLLN("");
  2977. }
  2978. }
  2979. else
  2980. {
  2981. SERIAL_ECHO_START;
  2982. SERIAL_ECHOLNPGM(MSG_ZPROBE_ZOFFSET " : ");
  2983. SERIAL_ECHO(-zprobe_zoffset);
  2984. SERIAL_PROTOCOLLN("");
  2985. }
  2986. break;
  2987. }
  2988. #endif // CUSTOM_M_CODE_SET_Z_PROBE_OFFSET
  2989.  
  2990. #ifdef FILAMENTCHANGEENABLE
  2991. case 600: //Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
  2992. {
  2993. float target[4];
  2994. float lastpos[4];
  2995. target[X_AXIS]=current_position[X_AXIS];
  2996. target[Y_AXIS]=current_position[Y_AXIS];
  2997. target[Z_AXIS]=current_position[Z_AXIS];
  2998. target[E_AXIS]=current_position[E_AXIS];
  2999. lastpos[X_AXIS]=current_position[X_AXIS];
  3000. lastpos[Y_AXIS]=current_position[Y_AXIS];
  3001. lastpos[Z_AXIS]=current_position[Z_AXIS];
  3002. lastpos[E_AXIS]=current_position[E_AXIS];
  3003. //retract by E
  3004. if(code_seen('E'))
  3005. {
  3006. target[E_AXIS]+= code_value();
  3007. }
  3008. else
  3009. {
  3010. #ifdef FILAMENTCHANGE_FIRSTRETRACT
  3011. target[E_AXIS]+= FILAMENTCHANGE_FIRSTRETRACT ;
  3012. #endif
  3013. }
  3014. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder);
  3015.  
  3016. //lift Z
  3017. if(code_seen('Z'))
  3018. {
  3019. target[Z_AXIS]+= code_value();
  3020. }
  3021. else
  3022. {
  3023. #ifdef FILAMENTCHANGE_ZADD
  3024. target[Z_AXIS]+= FILAMENTCHANGE_ZADD ;
  3025. #endif
  3026. }
  3027. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder);
  3028.  
  3029. //move xy
  3030. if(code_seen('X'))
  3031. {
  3032. target[X_AXIS]+= code_value();
  3033. }
  3034. else
  3035. {
  3036. #ifdef FILAMENTCHANGE_XPOS
  3037. target[X_AXIS]= FILAMENTCHANGE_XPOS ;
  3038. #endif
  3039. }
  3040. if(code_seen('Y'))
  3041. {
  3042. target[Y_AXIS]= code_value();
  3043. }
  3044. else
  3045. {
  3046. #ifdef FILAMENTCHANGE_YPOS
  3047. target[Y_AXIS]= FILAMENTCHANGE_YPOS ;
  3048. #endif
  3049. }
  3050.  
  3051. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder);
  3052.  
  3053. if(code_seen('L'))
  3054. {
  3055. target[E_AXIS]+= code_value();
  3056. }
  3057. else
  3058. {
  3059. #ifdef FILAMENTCHANGE_FINALRETRACT
  3060. target[E_AXIS]+= FILAMENTCHANGE_FINALRETRACT ;
  3061. #endif
  3062. }
  3063.  
  3064. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder);
  3065.  
  3066. //finish moves
  3067. st_synchronize();
  3068. //disable extruder steppers so filament can be removed
  3069. disable_e0();
  3070. disable_e1();
  3071. disable_e2();
  3072. delay(100);
  3073. LCD_ALERTMESSAGEPGM(MSG_FILAMENTCHANGE);
  3074. uint8_t cnt=0;
  3075. while(!lcd_clicked()){
  3076. cnt++;
  3077. manage_heater();
  3078. manage_inactivity();
  3079. lcd_update();
  3080. if(cnt==0)
  3081. {
  3082. #if BEEPER > 0
  3083. SET_OUTPUT(BEEPER);
  3084.  
  3085. WRITE(BEEPER,HIGH);
  3086. delay(3);
  3087. WRITE(BEEPER,LOW);
  3088. delay(3);
  3089. #else
  3090. #if !defined(LCD_FEEDBACK_FREQUENCY_HZ) || !defined(LCD_FEEDBACK_FREQUENCY_DURATION_MS)
  3091. lcd_buzz(1000/6,100);
  3092. #else
  3093. lcd_buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS,LCD_FEEDBACK_FREQUENCY_HZ);
  3094. #endif
  3095. #endif
  3096. }
  3097. }
  3098.  
  3099. //return to normal
  3100. if(code_seen('L'))
  3101. {
  3102. target[E_AXIS]+= -code_value();
  3103. }
  3104. else
  3105. {
  3106. #ifdef FILAMENTCHANGE_FINALRETRACT
  3107. target[E_AXIS]+=(-1)*FILAMENTCHANGE_FINALRETRACT ;
  3108. #endif
  3109. }
  3110. current_position[E_AXIS]=target[E_AXIS]; //the long retract of L is compensated by manual filament feeding
  3111. plan_set_e_position(current_position[E_AXIS]);
  3112. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder); //should do nothing
  3113. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder); //move xy back
  3114. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder); //move z back
  3115. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], lastpos[E_AXIS], feedrate/60, active_extruder); //final untretract
  3116. }
  3117. break;
  3118. #endif //FILAMENTCHANGEENABLE
  3119. #ifdef DUAL_X_CARRIAGE
  3120. case 605: // Set dual x-carriage movement mode:
  3121. // M605 S0: Full control mode. The slicer has full control over x-carriage movement
  3122. // M605 S1: Auto-park mode. The inactive head will auto park/unpark without slicer involvement
  3123. // M605 S2 [Xnnn] [Rmmm]: Duplication mode. The second extruder will duplicate the first with nnn
  3124. // millimeters x-offset and an optional differential hotend temperature of
  3125. // mmm degrees. E.g., with "M605 S2 X100 R2" the second extruder will duplicate
  3126. // the first with a spacing of 100mm in the x direction and 2 degrees hotter.
  3127. //
  3128. // Note: the X axis should be homed after changing dual x-carriage mode.
  3129. {
  3130. st_synchronize();
  3131.  
  3132. if (code_seen('S'))
  3133. dual_x_carriage_mode = code_value();
  3134.  
  3135. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE)
  3136. {
  3137. if (code_seen('X'))
  3138. duplicate_extruder_x_offset = max(code_value(),X2_MIN_POS - x_home_pos(0));
  3139.  
  3140. if (code_seen('R'))
  3141. duplicate_extruder_temp_offset = code_value();
  3142.  
  3143. SERIAL_ECHO_START;
  3144. SERIAL_ECHOPGM(MSG_HOTEND_OFFSET);
  3145. SERIAL_ECHO(" ");
  3146. SERIAL_ECHO(extruder_offset[X_AXIS][0]);
  3147. SERIAL_ECHO(",");
  3148. SERIAL_ECHO(extruder_offset[Y_AXIS][0]);
  3149. SERIAL_ECHO(" ");
  3150. SERIAL_ECHO(duplicate_extruder_x_offset);
  3151. SERIAL_ECHO(",");
  3152. SERIAL_ECHOLN(extruder_offset[Y_AXIS][1]);
  3153. }
  3154. else if (dual_x_carriage_mode != DXC_FULL_CONTROL_MODE && dual_x_carriage_mode != DXC_AUTO_PARK_MODE)
  3155. {
  3156. dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  3157. }
  3158.  
  3159. active_extruder_parked = false;
  3160. extruder_duplication_enabled = false;
  3161. delayed_move_time = 0;
  3162. }
  3163. break;
  3164. #endif //DUAL_X_CARRIAGE
  3165.  
  3166. case 907: // M907 Set digital trimpot motor current using axis codes.
  3167. {
  3168. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  3169. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) digipot_current(i,code_value());
  3170. if(code_seen('B')) digipot_current(4,code_value());
  3171. if(code_seen('S')) for(int i=0;i<=4;i++) digipot_current(i,code_value());
  3172. #endif
  3173. #ifdef MOTOR_CURRENT_PWM_XY_PIN
  3174. if(code_seen('X')) digipot_current(0, code_value());
  3175. #endif
  3176. #ifdef MOTOR_CURRENT_PWM_Z_PIN
  3177. if(code_seen('Z')) digipot_current(1, code_value());
  3178. #endif
  3179. #ifdef MOTOR_CURRENT_PWM_E_PIN
  3180. if(code_seen('E')) digipot_current(2, code_value());
  3181. #endif
  3182. #ifdef DIGIPOT_I2C
  3183. // this one uses actual amps in floating point
  3184. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) digipot_i2c_set_current(i, code_value());
  3185. // for each additional extruder (named B,C,D,E..., channels 4,5,6,7...)
  3186. for(int i=NUM_AXIS;i<DIGIPOT_I2C_NUM_CHANNELS;i++) if(code_seen('B'+i-NUM_AXIS)) digipot_i2c_set_current(i, code_value());
  3187. #endif
  3188. }
  3189. break;
  3190. case 908: // M908 Control digital trimpot directly.
  3191. {
  3192. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  3193. uint8_t channel,current;
  3194. if(code_seen('P')) channel=code_value();
  3195. if(code_seen('S')) current=code_value();
  3196. digitalPotWrite(channel, current);
  3197. #endif
  3198. }
  3199. break;
  3200. case 350: // M350 Set microstepping mode. Warning: Steps per unit remains unchanged. S code sets stepping mode for all drivers.
  3201. {
  3202. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  3203. if(code_seen('S')) for(int i=0;i<=4;i++) microstep_mode(i,code_value());
  3204. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_mode(i,(uint8_t)code_value());
  3205. if(code_seen('B')) microstep_mode(4,code_value());
  3206. microstep_readings();
  3207. #endif
  3208. }
  3209. break;
  3210. case 351: // M351 Toggle MS1 MS2 pins directly, S# determines MS1 or MS2, X# sets the pin high/low.
  3211. {
  3212. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  3213. if(code_seen('S')) switch((int)code_value())
  3214. {
  3215. case 1:
  3216. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,code_value(),-1);
  3217. if(code_seen('B')) microstep_ms(4,code_value(),-1);
  3218. break;
  3219. case 2:
  3220. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,-1,code_value());
  3221. if(code_seen('B')) microstep_ms(4,-1,code_value());
  3222. break;
  3223. }
  3224. microstep_readings();
  3225. #endif
  3226. }
  3227. break;
  3228. case 999: // M999: Restart after being stopped
  3229. Stopped = false;
  3230. lcd_reset_alert_level();
  3231. gcode_LastN = Stopped_gcode_LastN;
  3232. FlushSerialRequestResend();
  3233. break;
  3234. }
  3235. }
  3236.  
  3237. else if(code_seen('T'))
  3238. {
  3239. tmp_extruder = code_value();
  3240. if(tmp_extruder >= EXTRUDERS) {
  3241. SERIAL_ECHO_START;
  3242. SERIAL_ECHO("T");
  3243. SERIAL_ECHO(tmp_extruder);
  3244. SERIAL_ECHOLN(MSG_INVALID_EXTRUDER);
  3245. }
  3246. else {
  3247. boolean make_move = false;
  3248. if(code_seen('F')) {
  3249. make_move = true;
  3250. next_feedrate = code_value();
  3251. if(next_feedrate > 0.0) {
  3252. feedrate = next_feedrate;
  3253. }
  3254. }
  3255. #if EXTRUDERS > 1
  3256. if(tmp_extruder != active_extruder) {
  3257. // Save current position to return to after applying extruder offset
  3258. memcpy(destination, current_position, sizeof(destination));
  3259. #ifdef DUAL_X_CARRIAGE
  3260. if (dual_x_carriage_mode == DXC_AUTO_PARK_MODE && Stopped == false &&
  3261. (delayed_move_time != 0 || current_position[X_AXIS] != x_home_pos(active_extruder)))
  3262. {
  3263. // Park old head: 1) raise 2) move to park position 3) lower
  3264. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] + TOOLCHANGE_PARK_ZLIFT,
  3265. current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  3266. plan_buffer_line(x_home_pos(active_extruder), current_position[Y_AXIS], current_position[Z_AXIS] + TOOLCHANGE_PARK_ZLIFT,
  3267. current_position[E_AXIS], max_feedrate[X_AXIS], active_extruder);
  3268. plan_buffer_line(x_home_pos(active_extruder), current_position[Y_AXIS], current_position[Z_AXIS],
  3269. current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  3270. st_synchronize();
  3271. }
  3272.  
  3273. // apply Y & Z extruder offset (x offset is already used in determining home pos)
  3274. current_position[Y_AXIS] = current_position[Y_AXIS] -
  3275. extruder_offset[Y_AXIS][active_extruder] +
  3276. extruder_offset[Y_AXIS][tmp_extruder];
  3277. current_position[Z_AXIS] = current_position[Z_AXIS] -
  3278. extruder_offset[Z_AXIS][active_extruder] +
  3279. extruder_offset[Z_AXIS][tmp_extruder];
  3280.  
  3281. active_extruder = tmp_extruder;
  3282.  
  3283. // This function resets the max/min values - the current position may be overwritten below.
  3284. axis_is_at_home(X_AXIS);
  3285.  
  3286. if (dual_x_carriage_mode == DXC_FULL_CONTROL_MODE)
  3287. {
  3288. current_position[X_AXIS] = inactive_extruder_x_pos;
  3289. inactive_extruder_x_pos = destination[X_AXIS];
  3290. }
  3291. else if (dual_x_carriage_mode == DXC_DUPLICATION_MODE)
  3292. {
  3293. active_extruder_parked = (active_extruder == 0); // this triggers the second extruder to move into the duplication position
  3294. if (active_extruder == 0 || active_extruder_parked)
  3295. current_position[X_AXIS] = inactive_extruder_x_pos;
  3296. else
  3297. current_position[X_AXIS] = destination[X_AXIS] + duplicate_extruder_x_offset;
  3298. inactive_extruder_x_pos = destination[X_AXIS];
  3299. extruder_duplication_enabled = false;
  3300. }
  3301. else
  3302. {
  3303. // record raised toolhead position for use by unpark
  3304. memcpy(raised_parked_position, current_position, sizeof(raised_parked_position));
  3305. raised_parked_position[Z_AXIS] += TOOLCHANGE_UNPARK_ZLIFT;
  3306. active_extruder_parked = true;
  3307. delayed_move_time = 0;
  3308. }
  3309. #else
  3310. // Offset extruder (only by XY)
  3311. int i;
  3312. for(i = 0; i < 2; i++) {
  3313. current_position[i] = current_position[i] -
  3314. extruder_offset[i][active_extruder] +
  3315. extruder_offset[i][tmp_extruder];
  3316. }
  3317. // Set the new active extruder and position
  3318. active_extruder = tmp_extruder;
  3319. #endif //else DUAL_X_CARRIAGE
  3320. #ifdef DELTA
  3321.  
  3322. calculate_delta(current_position); // change cartesian kinematic to delta kinematic;
  3323. //sent position to plan_set_position();
  3324. plan_set_position(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS],current_position[E_AXIS]);
  3325.  
  3326. #else
  3327. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  3328.  
  3329. #endif
  3330. // Move to the old position if 'F' was in the parameters
  3331. if(make_move && Stopped == false) {
  3332. prepare_move();
  3333. }
  3334. }
  3335. #endif
  3336. SERIAL_ECHO_START;
  3337. SERIAL_ECHO(MSG_ACTIVE_EXTRUDER);
  3338. SERIAL_PROTOCOLLN((int)active_extruder);
  3339. }
  3340. }
  3341.  
  3342. else
  3343. {
  3344. SERIAL_ECHO_START;
  3345. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  3346. SERIAL_ECHO(cmdbuffer[bufindr]);
  3347. SERIAL_ECHOLNPGM("\"");
  3348. }
  3349.  
  3350. ClearToSend();
  3351. }
  3352.  
  3353. void FlushSerialRequestResend()
  3354. {
  3355. //char cmdbuffer[bufindr][100]="Resend:";
  3356. MYSERIAL.flush();
  3357. SERIAL_PROTOCOLPGM(MSG_RESEND);
  3358. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  3359. ClearToSend();
  3360. }
  3361.  
  3362. void ClearToSend()
  3363. {
  3364. previous_millis_cmd = millis();
  3365. #ifdef SDSUPPORT
  3366. if(fromsd[bufindr])
  3367. return;
  3368. #endif //SDSUPPORT
  3369. SERIAL_PROTOCOLLNPGM(MSG_OK);
  3370. }
  3371.  
  3372. void get_coordinates()
  3373. {
  3374. bool seen[4]={false,false,false,false};
  3375. for(int8_t i=0; i < NUM_AXIS; i++) {
  3376. if(code_seen(axis_codes[i]))
  3377. {
  3378. destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
  3379. seen[i]=true;
  3380. }
  3381. else destination[i] = current_position[i]; //Are these else lines really needed?
  3382. }
  3383. if(code_seen('F')) {
  3384. next_feedrate = code_value();
  3385. if(next_feedrate > 0.0) feedrate = next_feedrate;
  3386. }
  3387. }
  3388.  
  3389. void get_arc_coordinates()
  3390. {
  3391. #ifdef SF_ARC_FIX
  3392. bool relative_mode_backup = relative_mode;
  3393. relative_mode = true;
  3394. #endif
  3395. get_coordinates();
  3396. #ifdef SF_ARC_FIX
  3397. relative_mode=relative_mode_backup;
  3398. #endif
  3399.  
  3400. if(code_seen('I')) {
  3401. offset[0] = code_value();
  3402. }
  3403. else {
  3404. offset[0] = 0.0;
  3405. }
  3406. if(code_seen('J')) {
  3407. offset[1] = code_value();
  3408. }
  3409. else {
  3410. offset[1] = 0.0;
  3411. }
  3412. }
  3413.  
  3414. void clamp_to_software_endstops(float target[3])
  3415. {
  3416. if (min_software_endstops) {
  3417. if (target[X_AXIS] < min_pos[X_AXIS]) target[X_AXIS] = min_pos[X_AXIS];
  3418. if (target[Y_AXIS] < min_pos[Y_AXIS]) target[Y_AXIS] = min_pos[Y_AXIS];
  3419. if (target[Z_AXIS] < min_pos[Z_AXIS]) target[Z_AXIS] = min_pos[Z_AXIS];
  3420. }
  3421.  
  3422. if (max_software_endstops) {
  3423. if (target[X_AXIS] > max_pos[X_AXIS]) target[X_AXIS] = max_pos[X_AXIS];
  3424. if (target[Y_AXIS] > max_pos[Y_AXIS]) target[Y_AXIS] = max_pos[Y_AXIS];
  3425. if (target[Z_AXIS] > max_pos[Z_AXIS]) target[Z_AXIS] = max_pos[Z_AXIS];
  3426. }
  3427. }
  3428.  
  3429. #ifdef DELTA
  3430. void recalc_delta_settings(float radius, float diagonal_rod)
  3431. {
  3432. delta_tower1_x= -SIN_60*radius; // front left tower
  3433. delta_tower1_y= -COS_60*radius;
  3434. delta_tower2_x= SIN_60*radius; // front right tower
  3435. delta_tower2_y= -COS_60*radius;
  3436. delta_tower3_x= 0.0; // back middle tower
  3437. delta_tower3_y= radius;
  3438. delta_diagonal_rod_2= sq(diagonal_rod);
  3439. }
  3440.  
  3441. void calculate_delta(float cartesian[3])
  3442. {
  3443. delta[X_AXIS] = sqrt(delta_diagonal_rod_2
  3444. - sq(delta_tower1_x-cartesian[X_AXIS])
  3445. - sq(delta_tower1_y-cartesian[Y_AXIS])
  3446. ) + cartesian[Z_AXIS];
  3447. delta[Y_AXIS] = sqrt(delta_diagonal_rod_2
  3448. - sq(delta_tower2_x-cartesian[X_AXIS])
  3449. - sq(delta_tower2_y-cartesian[Y_AXIS])
  3450. ) + cartesian[Z_AXIS];
  3451. delta[Z_AXIS] = sqrt(delta_diagonal_rod_2
  3452. - sq(delta_tower3_x-cartesian[X_AXIS])
  3453. - sq(delta_tower3_y-cartesian[Y_AXIS])
  3454. ) + cartesian[Z_AXIS];
  3455. /*
  3456. SERIAL_ECHOPGM("cartesian x="); SERIAL_ECHO(cartesian[X_AXIS]);
  3457. SERIAL_ECHOPGM(" y="); SERIAL_ECHO(cartesian[Y_AXIS]);
  3458. SERIAL_ECHOPGM(" z="); SERIAL_ECHOLN(cartesian[Z_AXIS]);
  3459.  
  3460. SERIAL_ECHOPGM("delta x="); SERIAL_ECHO(delta[X_AXIS]);
  3461. SERIAL_ECHOPGM(" y="); SERIAL_ECHO(delta[Y_AXIS]);
  3462. SERIAL_ECHOPGM(" z="); SERIAL_ECHOLN(delta[Z_AXIS]);
  3463. */
  3464. }
  3465. #endif
  3466.  
  3467. void prepare_move()
  3468. {
  3469. clamp_to_software_endstops(destination);
  3470.  
  3471. previous_millis_cmd = millis();
  3472. #ifdef DELTA
  3473. float difference[NUM_AXIS];
  3474. for (int8_t i=0; i < NUM_AXIS; i++) {
  3475. difference[i] = destination[i] - current_position[i];
  3476. }
  3477. float cartesian_mm = sqrt(sq(difference[X_AXIS]) +
  3478. sq(difference[Y_AXIS]) +
  3479. sq(difference[Z_AXIS]));
  3480. if (cartesian_mm < 0.000001) { cartesian_mm = abs(difference[E_AXIS]); }
  3481. if (cartesian_mm < 0.000001) { return; }
  3482. float seconds = 6000 * cartesian_mm / feedrate / feedmultiply;
  3483. int steps = max(1, int(delta_segments_per_second * seconds));
  3484. // SERIAL_ECHOPGM("mm="); SERIAL_ECHO(cartesian_mm);
  3485. // SERIAL_ECHOPGM(" seconds="); SERIAL_ECHO(seconds);
  3486. // SERIAL_ECHOPGM(" steps="); SERIAL_ECHOLN(steps);
  3487. for (int s = 1; s <= steps; s++) {
  3488. float fraction = float(s) / float(steps);
  3489. for(int8_t i=0; i < NUM_AXIS; i++) {
  3490. destination[i] = current_position[i] + difference[i] * fraction;
  3491. }
  3492. calculate_delta(destination);
  3493. plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS],
  3494. destination[E_AXIS], feedrate*feedmultiply/60/100.0,
  3495. active_extruder);
  3496. }
  3497. #else
  3498.  
  3499. #ifdef DUAL_X_CARRIAGE
  3500. if (active_extruder_parked)
  3501. {
  3502. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && active_extruder == 0)
  3503. {
  3504. // move duplicate extruder into correct duplication position.
  3505. plan_set_position(inactive_extruder_x_pos, current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  3506. plan_buffer_line(current_position[X_AXIS] + duplicate_extruder_x_offset, current_position[Y_AXIS], current_position[Z_AXIS],
  3507. current_position[E_AXIS], max_feedrate[X_AXIS], 1);
  3508. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  3509. st_synchronize();
  3510. extruder_duplication_enabled = true;
  3511. active_extruder_parked = false;
  3512. }
  3513. else if (dual_x_carriage_mode == DXC_AUTO_PARK_MODE) // handle unparking of head
  3514. {
  3515. if (current_position[E_AXIS] == destination[E_AXIS])
  3516. {
  3517. // this is a travel move - skit it but keep track of current position (so that it can later
  3518. // be used as start of first non-travel move)
  3519. if (delayed_move_time != 0xFFFFFFFFUL)
  3520. {
  3521. memcpy(current_position, destination, sizeof(current_position));
  3522. if (destination[Z_AXIS] > raised_parked_position[Z_AXIS])
  3523. raised_parked_position[Z_AXIS] = destination[Z_AXIS];
  3524. delayed_move_time = millis();
  3525. return;
  3526. }
  3527. }
  3528. delayed_move_time = 0;
  3529. // unpark extruder: 1) raise, 2) move into starting XY position, 3) lower
  3530. plan_buffer_line(raised_parked_position[X_AXIS], raised_parked_position[Y_AXIS], raised_parked_position[Z_AXIS], current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  3531. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], raised_parked_position[Z_AXIS],
  3532. current_position[E_AXIS], min(max_feedrate[X_AXIS],max_feedrate[Y_AXIS]), active_extruder);
  3533. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  3534. current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  3535. active_extruder_parked = false;
  3536. }
  3537. }
  3538. #endif //DUAL_X_CARRIAGE
  3539.  
  3540. // Do not use feedmultiply for E or Z only moves
  3541. if( (current_position[X_AXIS] == destination [X_AXIS]) && (current_position[Y_AXIS] == destination [Y_AXIS])) {
  3542. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  3543. }
  3544. else {
  3545. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
  3546. }
  3547. #endif //else DELTA
  3548. for(int8_t i=0; i < NUM_AXIS; i++) {
  3549. current_position[i] = destination[i];
  3550. }
  3551. }
  3552.  
  3553. void prepare_arc_move(char isclockwise) {
  3554. float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
  3555.  
  3556. // Trace the arc
  3557. mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
  3558.  
  3559. // As far as the parser is concerned, the position is now == target. In reality the
  3560. // motion control system might still be processing the action and the real tool position
  3561. // in any intermediate location.
  3562. for(int8_t i=0; i < NUM_AXIS; i++) {
  3563. current_position[i] = destination[i];
  3564. }
  3565. previous_millis_cmd = millis();
  3566. }
  3567.  
  3568. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  3569.  
  3570. #if defined(FAN_PIN)
  3571. #if CONTROLLERFAN_PIN == FAN_PIN
  3572. #error "You cannot set CONTROLLERFAN_PIN equal to FAN_PIN"
  3573. #endif
  3574. #endif
  3575.  
  3576. unsigned long lastMotor = 0; //Save the time for when a motor was turned on last
  3577. unsigned long lastMotorCheck = 0;
  3578.  
  3579. void controllerFan()
  3580. {
  3581. if ((millis() - lastMotorCheck) >= 2500) //Not a time critical function, so we only check every 2500ms
  3582. {
  3583. lastMotorCheck = millis();
  3584.  
  3585. if(!READ(X_ENABLE_PIN) || !READ(Y_ENABLE_PIN) || !READ(Z_ENABLE_PIN) || (soft_pwm_bed > 0)
  3586. #if EXTRUDERS > 2
  3587. || !READ(E2_ENABLE_PIN)
  3588. #endif
  3589. #if EXTRUDER > 1
  3590. #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1
  3591. || !READ(X2_ENABLE_PIN)
  3592. #endif
  3593. || !READ(E1_ENABLE_PIN)
  3594. #endif
  3595. || !READ(E0_ENABLE_PIN)) //If any of the drivers are enabled...
  3596. {
  3597. lastMotor = millis(); //... set time to NOW so the fan will turn on
  3598. }
  3599.  
  3600. if ((millis() - lastMotor) >= (CONTROLLERFAN_SECS*1000UL) || lastMotor == 0) //If the last time any driver was enabled, is longer since than CONTROLLERSEC...
  3601. {
  3602. digitalWrite(CONTROLLERFAN_PIN, 0);
  3603. analogWrite(CONTROLLERFAN_PIN, 0);
  3604. }
  3605. else
  3606. {
  3607. // allows digital or PWM fan output to be used (see M42 handling)
  3608. digitalWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED);
  3609. analogWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED);
  3610. }
  3611. }
  3612. }
  3613. #endif
  3614.  
  3615. #ifdef TEMP_STAT_LEDS
  3616. static bool blue_led = false;
  3617. static bool red_led = false;
  3618. static uint32_t stat_update = 0;
  3619.  
  3620. void handle_status_leds(void) {
  3621. float max_temp = 0.0;
  3622. if(millis() > stat_update) {
  3623. stat_update += 500; // Update every 0.5s
  3624. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  3625. max_temp = max(max_temp, degHotend(cur_extruder));
  3626. max_temp = max(max_temp, degTargetHotend(cur_extruder));
  3627. }
  3628. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  3629. max_temp = max(max_temp, degTargetBed());
  3630. max_temp = max(max_temp, degBed());
  3631. #endif
  3632. if((max_temp > 55.0) && (red_led == false)) {
  3633. digitalWrite(STAT_LED_RED, 1);
  3634. digitalWrite(STAT_LED_BLUE, 0);
  3635. red_led = true;
  3636. blue_led = false;
  3637. }
  3638. if((max_temp < 54.0) && (blue_led == false)) {
  3639. digitalWrite(STAT_LED_RED, 0);
  3640. digitalWrite(STAT_LED_BLUE, 1);
  3641. red_led = false;
  3642. blue_led = true;
  3643. }
  3644. }
  3645. }
  3646. #endif
  3647.  
  3648. void manage_inactivity()
  3649. {
  3650. if(buflen < (BUFSIZE-1))
  3651. get_command();
  3652.  
  3653. if( (millis() - previous_millis_cmd) > max_inactive_time )
  3654. if(max_inactive_time)
  3655. kill();
  3656. if(stepper_inactive_time) {
  3657. if( (millis() - previous_millis_cmd) > stepper_inactive_time )
  3658. {
  3659. if(blocks_queued() == false) {
  3660. disable_x();
  3661. disable_y();
  3662. disable_z();
  3663. disable_e0();
  3664. disable_e1();
  3665. disable_e2();
  3666. }
  3667. }
  3668. }
  3669.  
  3670. #ifdef CHDK //Check if pin should be set to LOW after M240 set it to HIGH
  3671. if (chdkActive)
  3672. {
  3673. chdkActive = false;
  3674. if (millis()-chdkHigh < CHDK_DELAY) return;
  3675. WRITE(CHDK, LOW);
  3676. }
  3677. #endif
  3678.  
  3679. #if defined(KILL_PIN) && KILL_PIN > -1
  3680. if( 0 == READ(KILL_PIN) )
  3681. kill();
  3682. #endif
  3683. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  3684. controllerFan(); //Check if fan should be turned on to cool stepper drivers down
  3685. #endif
  3686. #ifdef EXTRUDER_RUNOUT_PREVENT
  3687. if( (millis() - previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 )
  3688. if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
  3689. {
  3690. bool oldstatus=READ(E0_ENABLE_PIN);
  3691. enable_e0();
  3692. float oldepos=current_position[E_AXIS];
  3693. float oldedes=destination[E_AXIS];
  3694. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS],
  3695. destination[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS],
  3696. EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
  3697. current_position[E_AXIS]=oldepos;
  3698. destination[E_AXIS]=oldedes;
  3699. plan_set_e_position(oldepos);
  3700. previous_millis_cmd=millis();
  3701. st_synchronize();
  3702. WRITE(E0_ENABLE_PIN,oldstatus);
  3703. }
  3704. #endif
  3705. #if defined(DUAL_X_CARRIAGE)
  3706. // handle delayed move timeout
  3707. if (delayed_move_time != 0 && (millis() - delayed_move_time) > 1000 && Stopped == false)
  3708. {
  3709. // travel moves have been received so enact them
  3710. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  3711. memcpy(destination,current_position,sizeof(destination));
  3712. prepare_move();
  3713. }
  3714. #endif
  3715. #ifdef TEMP_STAT_LEDS
  3716. handle_status_leds();
  3717. #endif
  3718. check_axes_activity();
  3719. }
  3720.  
  3721. void kill()
  3722. {
  3723. cli(); // Stop interrupts
  3724. disable_heater();
  3725.  
  3726. disable_x();
  3727. disable_y();
  3728. disable_z();
  3729. disable_e0();
  3730. disable_e1();
  3731. disable_e2();
  3732.  
  3733. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  3734. pinMode(PS_ON_PIN,INPUT);
  3735. #endif
  3736. SERIAL_ERROR_START;
  3737. SERIAL_ERRORLNPGM(MSG_ERR_KILLED);
  3738. LCD_ALERTMESSAGEPGM(MSG_KILLED);
  3739. suicide();
  3740. while(1) { /* Intentionally left empty */ } // Wait for reset
  3741. }
  3742.  
  3743. void Stop()
  3744. {
  3745. disable_heater();
  3746. if(Stopped == false) {
  3747. Stopped = true;
  3748. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  3749. SERIAL_ERROR_START;
  3750. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  3751. LCD_MESSAGEPGM(MSG_STOPPED);
  3752. }
  3753. }
  3754.  
  3755. bool IsStopped() { return Stopped; };
  3756.  
  3757. #ifdef FAST_PWM_FAN
  3758. void setPwmFrequency(uint8_t pin, int val)
  3759. {
  3760. val &= 0x07;
  3761. switch(digitalPinToTimer(pin))
  3762. {
  3763.  
  3764. #if defined(TCCR0A)
  3765. case TIMER0A:
  3766. case TIMER0B:
  3767. // TCCR0B &= ~(_BV(CS00) | _BV(CS01) | _BV(CS02));
  3768. // TCCR0B |= val;
  3769. break;
  3770. #endif
  3771.  
  3772. #if defined(TCCR1A)
  3773. case TIMER1A:
  3774. case TIMER1B:
  3775. // TCCR1B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
  3776. // TCCR1B |= val;
  3777. break;
  3778. #endif
  3779.  
  3780. #if defined(TCCR2)
  3781. case TIMER2:
  3782. case TIMER2:
  3783. TCCR2 &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
  3784. TCCR2 |= val;
  3785. break;
  3786. #endif
  3787.  
  3788. #if defined(TCCR2A)
  3789. case TIMER2A:
  3790. case TIMER2B:
  3791. TCCR2B &= ~(_BV(CS20) | _BV(CS21) | _BV(CS22));
  3792. TCCR2B |= val;
  3793. break;
  3794. #endif
  3795.  
  3796. #if defined(TCCR3A)
  3797. case TIMER3A:
  3798. case TIMER3B:
  3799. case TIMER3C:
  3800. TCCR3B &= ~(_BV(CS30) | _BV(CS31) | _BV(CS32));
  3801. TCCR3B |= val;
  3802. break;
  3803. #endif
  3804.  
  3805. #if defined(TCCR4A)
  3806. case TIMER4A:
  3807. case TIMER4B:
  3808. case TIMER4C:
  3809. TCCR4B &= ~(_BV(CS40) | _BV(CS41) | _BV(CS42));
  3810. TCCR4B |= val;
  3811. break;
  3812. #endif
  3813.  
  3814. #if defined(TCCR5A)
  3815. case TIMER5A:
  3816. case TIMER5B:
  3817. case TIMER5C:
  3818. TCCR5B &= ~(_BV(CS50) | _BV(CS51) | _BV(CS52));
  3819. TCCR5B |= val;
  3820. break;
  3821. #endif
  3822.  
  3823. }
  3824. }
  3825. #endif //FAST_PWM_FAN
  3826.  
  3827. bool setTargetedHotend(int code){
  3828. tmp_extruder = active_extruder;
  3829. if(code_seen('T')) {
  3830. tmp_extruder = code_value();
  3831. if(tmp_extruder >= EXTRUDERS) {
  3832. SERIAL_ECHO_START;
  3833. switch(code){
  3834. case 104:
  3835. SERIAL_ECHO(MSG_M104_INVALID_EXTRUDER);
  3836. break;
  3837. case 105:
  3838. SERIAL_ECHO(MSG_M105_INVALID_EXTRUDER);
  3839. break;
  3840. case 109:
  3841. SERIAL_ECHO(MSG_M109_INVALID_EXTRUDER);
  3842. break;
  3843. case 218:
  3844. SERIAL_ECHO(MSG_M218_INVALID_EXTRUDER);
  3845. break;
  3846. case 221:
  3847. SERIAL_ECHO(MSG_M221_INVALID_EXTRUDER);
  3848. break;
  3849. }
  3850. SERIAL_ECHOLN(tmp_extruder);
  3851. return true;
  3852. }
  3853. }
  3854. return false;
  3855. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement