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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  1. /* -*- c++ -*- */
  2. /*
  3. Reprap firmware based on Sprinter and grbl.
  4. Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /*
  17. This firmware is a mashup between Sprinter and grbl.
  18. (https://github.com/kliment/Sprinter)
  19. (https://github.com/simen/grbl/tree)
  20. It has preliminary support for Matthew Roberts advance algorithm
  21. http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
  22. */
  23. #include "Marlin.h"
  24. #include "ultralcd.h"
  25. #include "planner.h"
  26. #include "stepper.h"
  27. #include "temperature.h"
  28. #include "motion_control.h"
  29. #include "cardreader.h"
  30. #include "watchdog.h"
  31. #include "EEPROMwrite.h"
  32. #include "language.h"
  33. #include "pins_arduino.h"
  34. #define VERSION_STRING "1.0.0 RC2"
  35. // look here for descriptions of gcodes: http://linuxcnc.org/handbook/gcode/g-code.html
  36. // http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes
  37. //Implemented Codes
  38. //-------------------
  39. // G0 -> G1
  40. // G1 - Coordinated Movement X Y Z E
  41. // G2 - CW ARC
  42. // G3 - CCW ARC
  43. // G4 - Dwell S<seconds> or P<milliseconds>
  44. // G10 - retract filament according to settings of M207
  45. // G11 - retract recover filament according to settings of M208
  46. // G28 - Home all Axis
  47. // G90 - Use Absolute Coordinates
  48. // G91 - Use Relative Coordinates
  49. // G92 - Set current position to cordinates given
  50. //RepRap M Codes
  51. // M0 - Unconditional stop - Wait for user to press a button on the LCD (Only if ULTRA_LCD is enabled)
  52. // M1 - Same as M0
  53. // M104 - Set extruder target temp
  54. // M105 - Read current temp
  55. // M106 - Fan on
  56. // M107 - Fan off
  57. // M109 - Wait for extruder current temp to reach target temp.
  58. // M114 - Display current position
  59. //Custom M Codes
  60. // M17 - Enable/Power all stepper motors
  61. // M18 - Disable all stepper motors; same as M84
  62. // M20 - List SD card
  63. // M21 - Init SD card
  64. // M22 - Release SD card
  65. // M23 - Select SD file (M23 filename.g)
  66. // M24 - Start/resume SD print
  67. // M25 - Pause SD print
  68. // M26 - Set SD position in bytes (M26 S12345)
  69. // M27 - Report SD print status
  70. // M28 - Start SD write (M28 filename.g)
  71. // M29 - Stop SD write
  72. // M30 - Delete file from SD (M30 filename.g)
  73. // M31 - Output time since last M109 or SD card start to serial
  74. // M42 - Change pin status via gcode
  75. // M80 - Turn on Power Supply
  76. // M81 - Turn off Power Supply
  77. // M82 - Set E codes absolute (default)
  78. // M83 - Set E codes relative while in Absolute Coordinates (G90) mode
  79. // M84 - Disable steppers until next move,
  80. // or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
  81. // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  82. // M92 - Set axis_steps_per_unit - same syntax as G92
  83. // M114 - Output current position to serial port
  84. // M115 - Capabilities string
  85. // M117 - display message
  86. // M119 - Output Endstop status to serial port
  87. // M140 - Set bed target temp
  88. // M190 - Wait for bed current temp to reach target temp.
  89. // M200 - Set filament diameter
  90. // M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
  91. // M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
  92. // M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
  93. // M204 - Set default acceleration: S normal moves T filament only moves (M204 S3000 T7000) im mm/sec^2 also sets minimum segment time in ms (B20000) to prevent buffer underruns and M20 minimum feedrate
  94. // 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
  95. // M206 - set additional homeing offset
  96. // M207 - set retract length S[positive mm] F[feedrate mm/sec] Z[additional zlift/hop]
  97. // M208 - set recover=unretract length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  98. // 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.
  99. // M220 S<factor in percent>- set speed factor override percentage
  100. // M221 S<factor in percent>- set extrude factor override percentage
  101. // M240 - Trigger a camera to take a photograph
  102. // M301 - Set PID parameters P I and D
  103. // M302 - Allow cold extrudes
  104. // M303 - PID relay autotune S<temperature> sets the target temperature. (default target temperature = 150C)
  105. // M400 - Finish all moves
  106. // M500 - stores paramters in EEPROM
  107. // M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
  108. // M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
  109. // M503 - print the current settings (from memory not from eeprom)
  110. // M999 - Restart after being stopped by error
  111. //Stepper Movement Variables
  112. //===========================================================================
  113. //=============================imported variables============================
  114. //===========================================================================
  115. //===========================================================================
  116. //=============================public variables=============================
  117. //===========================================================================
  118. #ifdef SDSUPPORT
  119. CardReader card;
  120. #endif
  121. float homing_feedrate[] = HOMING_FEEDRATE;
  122. bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
  123. volatile int feedmultiply=100; //100->1 200->2
  124. int saved_feedmultiply;
  125. volatile bool feedmultiplychanged=false;
  126. volatile int extrudemultiply=100; //100->1 200->2
  127. float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
  128. float add_homeing[3]={0,0,0};
  129. float min_pos[3] = { X_MIN_POS, Y_MIN_POS, Z_MIN_POS };
  130. float max_pos[3] = { X_MAX_POS, Y_MAX_POS, Z_MAX_POS };
  131. uint8_t active_extruder = 0;
  132. unsigned char FanSpeed=0;
  133. #ifdef FWRETRACT
  134. bool autoretract_enabled=true;
  135. bool retracted=false;
  136. float retract_length=3, retract_feedrate=17*60, retract_zlift=0.8;
  137. float retract_recover_length=0, retract_recover_feedrate=8*60;
  138. #endif
  139. //===========================================================================
  140. //=============================private variables=============================
  141. //===========================================================================
  142. const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
  143. static float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
  144. static float offset[3] = {0.0, 0.0, 0.0};
  145. static bool home_all_axis = true;
  146. static float feedrate = 1500.0, next_feedrate, saved_feedrate;
  147. static long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
  148. static bool relative_mode = false; //Determines Absolute or Relative Coordinates
  149. static bool relative_mode_e = false; //Determines Absolute or Relative E Codes while in Absolute Coordinates mode. E is always relative in Relative Coordinates mode.
  150. static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
  151. static bool fromsd[BUFSIZE];
  152. static int bufindr = 0;
  153. static int bufindw = 0;
  154. static int buflen = 0;
  155. //static int i = 0;
  156. static char serial_char;
  157. static int serial_count = 0;
  158. static boolean comment_mode = false;
  159. static char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
  160. const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
  161. //static float tt = 0;
  162. //static float bt = 0;
  163. //Inactivity shutdown variables
  164. static unsigned long previous_millis_cmd = 0;
  165. static unsigned long max_inactive_time = 0;
  166. static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000l;
  167. static unsigned long starttime=0;
  168. static unsigned long stoptime=0;
  169. static uint8_t tmp_extruder;
  170. bool Stopped=false;
  171. //===========================================================================
  172. //=============================ROUTINES=============================
  173. //===========================================================================
  174. void get_arc_coordinates();
  175. void serial_echopair_P(const char *s_P, float v)
  176. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  177. void serial_echopair_P(const char *s_P, double v)
  178. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  179. void serial_echopair_P(const char *s_P, unsigned long v)
  180. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  181. extern "C"{
  182. extern unsigned int __bss_end;
  183. extern unsigned int __heap_start;
  184. extern void *__brkval;
  185. int freeMemory() {
  186. int free_memory;
  187. if((int)__brkval == 0)
  188. free_memory = ((int)&free_memory) - ((int)&__bss_end);
  189. else
  190. free_memory = ((int)&free_memory) - ((int)__brkval);
  191. return free_memory;
  192. }
  193. }
  194. //adds an command to the main command buffer
  195. //thats really done in a non-safe way.
  196. //needs overworking someday
  197. void enquecommand(const char *cmd)
  198. {
  199. if(buflen < BUFSIZE)
  200. {
  201. //this is dangerous if a mixing of serial and this happsens
  202. strcpy(&(cmdbuffer[bufindw][0]),cmd);
  203. SERIAL_ECHO_START;
  204. SERIAL_ECHOPGM("enqueing \"");
  205. SERIAL_ECHO(cmdbuffer[bufindw]);
  206. SERIAL_ECHOLNPGM("\"");
  207. bufindw= (bufindw + 1)%BUFSIZE;
  208. buflen += 1;
  209. }
  210. }
  211. void setup_photpin()
  212. {
  213. #ifdef PHOTOGRAPH_PIN
  214. #if (PHOTOGRAPH_PIN > -1)
  215. SET_OUTPUT(PHOTOGRAPH_PIN);
  216. WRITE(PHOTOGRAPH_PIN, LOW);
  217. #endif
  218. #endif
  219. }
  220. void setup_powerhold()
  221. {
  222. #ifdef SUICIDE_PIN
  223. #if (SUICIDE_PIN> -1)
  224. SET_OUTPUT(SUICIDE_PIN);
  225. WRITE(SUICIDE_PIN, HIGH);
  226. #endif
  227. #endif
  228. }
  229. void suicide()
  230. {
  231. #ifdef SUICIDE_PIN
  232. #if (SUICIDE_PIN> -1)
  233. SET_OUTPUT(SUICIDE_PIN);
  234. WRITE(SUICIDE_PIN, LOW);
  235. #endif
  236. #endif
  237. }
  238. void setup()
  239. {
  240. setup_powerhold();
  241. MYSERIAL.begin(BAUDRATE);
  242. SERIAL_PROTOCOLLNPGM("start");
  243. SERIAL_ECHO_START;
  244. // Check startup - does nothing if bootloader sets MCUSR to 0
  245. byte mcu = MCUSR;
  246. if(mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  247. if(mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  248. if(mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  249. if(mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  250. if(mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  251. MCUSR=0;
  252. SERIAL_ECHOPGM(MSG_MARLIN);
  253. SERIAL_ECHOLNPGM(VERSION_STRING);
  254. #ifdef STRING_VERSION_CONFIG_H
  255. #ifdef STRING_CONFIG_H_AUTHOR
  256. SERIAL_ECHO_START;
  257. SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
  258. SERIAL_ECHOPGM(STRING_VERSION_CONFIG_H);
  259. SERIAL_ECHOPGM(MSG_AUTHOR);
  260. SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
  261. #endif
  262. #endif
  263. SERIAL_ECHO_START;
  264. SERIAL_ECHOPGM(MSG_FREE_MEMORY);
  265. SERIAL_ECHO(freeMemory());
  266. SERIAL_ECHOPGM(MSG_PLANNER_BUFFER_BYTES);
  267. SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
  268. for(int8_t i = 0; i < BUFSIZE; i++)
  269. {
  270. fromsd[i] = false;
  271. }
  272. EEPROM_RetrieveSettings(); // loads data from EEPROM if available
  273. for(int8_t i=0; i < NUM_AXIS; i++)
  274. {
  275. axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i];
  276. }
  277. tp_init(); // Initialize temperature loop
  278. plan_init(); // Initialize planner;
  279. st_init(); // Initialize stepper;
  280. wd_init();
  281. setup_photpin();
  282. LCD_INIT;
  283. }
  284. void loop()
  285. {
  286. if(buflen < (BUFSIZE-1))
  287. get_command();
  288. #ifdef SDSUPPORT
  289. card.checkautostart(false);
  290. #endif
  291. if(buflen)
  292. {
  293. #ifdef SDSUPPORT
  294. if(card.saving)
  295. {
  296. if(strstr(cmdbuffer[bufindr],"M29") == NULL)
  297. {
  298. card.write_command(cmdbuffer[bufindr]);
  299. SERIAL_PROTOCOLLNPGM(MSG_OK);
  300. }
  301. else
  302. {
  303. card.closefile();
  304. SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
  305. }
  306. }
  307. else
  308. {
  309. process_commands();
  310. }
  311. #else
  312. process_commands();
  313. #endif //SDSUPPORT
  314. buflen = (buflen-1);
  315. bufindr = (bufindr + 1)%BUFSIZE;
  316. }
  317. //check heater every n milliseconds
  318. manage_heater();
  319. manage_inactivity(1);
  320. checkHitEndstops();
  321. LCD_STATUS;
  322. }
  323. void get_command()
  324. {
  325. while( MYSERIAL.available() > 0 && buflen < BUFSIZE) {
  326. serial_char = MYSERIAL.read();
  327. if(serial_char == '\n' ||
  328. serial_char == '\r' ||
  329. (serial_char == ':' && comment_mode == false) ||
  330. serial_count >= (MAX_CMD_SIZE - 1) )
  331. {
  332. if(!serial_count) { //if empty line
  333. comment_mode = false; //for new command
  334. return;
  335. }
  336. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  337. if(!comment_mode){
  338. comment_mode = false; //for new command
  339. fromsd[bufindw] = false;
  340. if(strstr(cmdbuffer[bufindw], "N") != NULL)
  341. {
  342. strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
  343. gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL, 10));
  344. if(gcode_N != gcode_LastN+1 && (strstr(cmdbuffer[bufindw], "M110") == NULL) ) {
  345. SERIAL_ERROR_START;
  346. SERIAL_ERRORPGM(MSG_ERR_LINE_NO);
  347. SERIAL_ERRORLN(gcode_LastN);
  348. //Serial.println(gcode_N);
  349. FlushSerialRequestResend();
  350. serial_count = 0;
  351. return;
  352. }
  353. if(strstr(cmdbuffer[bufindw], "*") != NULL)
  354. {
  355. byte checksum = 0;
  356. byte count = 0;
  357. while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
  358. strchr_pointer = strchr(cmdbuffer[bufindw], '*');
  359. if( (int)(strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)) != checksum) {
  360. SERIAL_ERROR_START;
  361. SERIAL_ERRORPGM(MSG_ERR_CHECKSUM_MISMATCH);
  362. SERIAL_ERRORLN(gcode_LastN);
  363. FlushSerialRequestResend();
  364. serial_count = 0;
  365. return;
  366. }
  367. //if no errors, continue parsing
  368. }
  369. else
  370. {
  371. SERIAL_ERROR_START;
  372. SERIAL_ERRORPGM(MSG_ERR_NO_CHECKSUM);
  373. SERIAL_ERRORLN(gcode_LastN);
  374. FlushSerialRequestResend();
  375. serial_count = 0;
  376. return;
  377. }
  378. gcode_LastN = gcode_N;
  379. //if no errors, continue parsing
  380. }
  381. else // if we don't receive 'N' but still see '*'
  382. {
  383. if((strstr(cmdbuffer[bufindw], "*") != NULL))
  384. {
  385. SERIAL_ERROR_START;
  386. SERIAL_ERRORPGM(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM);
  387. SERIAL_ERRORLN(gcode_LastN);
  388. serial_count = 0;
  389. return;
  390. }
  391. }
  392. if((strstr(cmdbuffer[bufindw], "G") != NULL)){
  393. strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
  394. switch((int)((strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)))){
  395. case 0:
  396. case 1:
  397. case 2:
  398. case 3:
  399. if(Stopped == false) { // If printer is stopped by an error the G[0-3] codes are ignored.
  400. #ifdef SDSUPPORT
  401. if(card.saving)
  402. break;
  403. #endif //SDSUPPORT
  404. SERIAL_PROTOCOLLNPGM(MSG_OK);
  405. }
  406. else {
  407. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  408. LCD_MESSAGEPGM(MSG_STOPPED);
  409. }
  410. break;
  411. default:
  412. break;
  413. }
  414. }
  415. bufindw = (bufindw + 1)%BUFSIZE;
  416. buflen += 1;
  417. }
  418. serial_count = 0; //clear buffer
  419. }
  420. else
  421. {
  422. if(serial_char == ';') comment_mode = true;
  423. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  424. }
  425. }
  426. #ifdef SDSUPPORT
  427. if(!card.sdprinting || serial_count!=0){
  428. return;
  429. }
  430. while( !card.eof() && buflen < BUFSIZE) {
  431. int16_t n=card.get();
  432. serial_char = (char)n;
  433. if(serial_char == '\n' ||
  434. serial_char == '\r' ||
  435. (serial_char == ':' && comment_mode == false) ||
  436. serial_count >= (MAX_CMD_SIZE - 1)||n==-1)
  437. {
  438. if(card.eof()){
  439. SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED);
  440. stoptime=millis();
  441. char time[30];
  442. unsigned long t=(stoptime-starttime)/1000;
  443. int sec,min;
  444. min=t/60;
  445. sec=t%60;
  446. sprintf(time,"%i min, %i sec",min,sec);
  447. SERIAL_ECHO_START;
  448. SERIAL_ECHOLN(time);
  449. LCD_MESSAGE(time);
  450. card.printingHasFinished();
  451. card.checkautostart(true);
  452. }
  453. if(!serial_count)
  454. {
  455. comment_mode = false; //for new command
  456. return; //if empty line
  457. }
  458. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  459. // if(!comment_mode){
  460. fromsd[bufindw] = true;
  461. buflen += 1;
  462. bufindw = (bufindw + 1)%BUFSIZE;
  463. // }
  464. comment_mode = false; //for new command
  465. serial_count = 0; //clear buffer
  466. }
  467. else
  468. {
  469. if(serial_char == ';') comment_mode = true;
  470. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  471. }
  472. }
  473. #endif //SDSUPPORT
  474. }
  475. float code_value()
  476. {
  477. return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL));
  478. }
  479. long code_value_long()
  480. {
  481. return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10));
  482. }
  483. bool code_seen(char code_string[]) //Return True if the string was found
  484. {
  485. return (strstr(cmdbuffer[bufindr], code_string) != NULL);
  486. }
  487. bool code_seen(char code)
  488. {
  489. strchr_pointer = strchr(cmdbuffer[bufindr], code);
  490. return (strchr_pointer != NULL); //Return True if a character was found
  491. }
  492. #define DEFINE_PGM_READ_ANY(type, reader) \
  493. static inline type pgm_read_any(const type *p) \
  494. { return pgm_read_##reader##_near(p); }
  495. DEFINE_PGM_READ_ANY(float, float);
  496. #define XYZ_CONSTS_FROM_CONFIG(type, array, CONFIG) \
  497. static const PROGMEM type array##_P[3] = \
  498. { X_##CONFIG, Y_##CONFIG, Z_##CONFIG }; \
  499. static inline type array(int axis) \
  500. { return pgm_read_any(&array##_P[axis]); }
  501. XYZ_CONSTS_FROM_CONFIG(float, base_min_pos, MIN_POS);
  502. XYZ_CONSTS_FROM_CONFIG(float, base_max_pos, MAX_POS);
  503. XYZ_CONSTS_FROM_CONFIG(float, base_home_pos, HOME_POS);
  504. static void axis_is_at_home(int axis) {
  505. current_position[axis] = base_home_pos(axis) + add_homeing[axis];
  506. min_pos[axis] = base_min_pos(axis) + add_homeing[axis];
  507. max_pos[axis] = base_max_pos(axis) + add_homeing[axis];
  508. }
  509. #define HOMEAXIS(LETTER) \
  510. if ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))\
  511. { \
  512. current_position[LETTER##_AXIS] = 0; \
  513. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); \
  514. destination[LETTER##_AXIS] = 1.5 * LETTER##_MAX_LENGTH * LETTER##_HOME_DIR; \
  515. feedrate = homing_feedrate[LETTER##_AXIS]; \
  516. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
  517. st_synchronize();\
  518. \
  519. current_position[LETTER##_AXIS] = 0;\
  520. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\
  521. destination[LETTER##_AXIS] = -LETTER##_HOME_RETRACT_MM * LETTER##_HOME_DIR;\
  522. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
  523. st_synchronize();\
  524. \
  525. destination[LETTER##_AXIS] = 2*LETTER##_HOME_RETRACT_MM * LETTER##_HOME_DIR;\
  526. feedrate = homing_feedrate[LETTER##_AXIS]/2 ; \
  527. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder); \
  528. st_synchronize();\
  529. \
  530. axis_is_at_home(LETTER##_AXIS); \
  531. destination[LETTER##_AXIS] = current_position[LETTER##_AXIS]; \
  532. feedrate = 0.0;\
  533. endstops_hit_on_purpose();\
  534. }
  535. void process_commands()
  536. {
  537. unsigned long codenum; //throw away variable
  538. char *starpos = NULL;
  539. if(code_seen('G'))
  540. {
  541. switch((int)code_value())
  542. {
  543. case 0: // G0 -> G1
  544. case 1: // G1
  545. if(Stopped == false) {
  546. get_coordinates(); // For X Y Z E F
  547. prepare_move();
  548. //ClearToSend();
  549. return;
  550. }
  551. //break;
  552. case 2: // G2 - CW ARC
  553. if(Stopped == false) {
  554. get_arc_coordinates();
  555. prepare_arc_move(true);
  556. return;
  557. }
  558. case 3: // G3 - CCW ARC
  559. if(Stopped == false) {
  560. get_arc_coordinates();
  561. prepare_arc_move(false);
  562. return;
  563. }
  564. case 4: // G4 dwell
  565. LCD_MESSAGEPGM(MSG_DWELL);
  566. codenum = 0;
  567. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  568. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  569. st_synchronize();
  570. codenum += millis(); // keep track of when we started waiting
  571. previous_millis_cmd = millis();
  572. while(millis() < codenum ){
  573. manage_heater();
  574. manage_inactivity(1);
  575. LCD_STATUS;
  576. }
  577. break;
  578. #ifdef FWRETRACT
  579. case 10: // G10 retract
  580. if(!retracted)
  581. {
  582. destination[X_AXIS]=current_position[X_AXIS];
  583. destination[Y_AXIS]=current_position[Y_AXIS];
  584. destination[Z_AXIS]=current_position[Z_AXIS];
  585. current_position[Z_AXIS]+=-retract_zlift;
  586. destination[E_AXIS]=current_position[E_AXIS]-retract_length;
  587. feedrate=retract_feedrate;
  588. retracted=true;
  589. prepare_move();
  590. }
  591. break;
  592. case 11: // G10 retract_recover
  593. if(!retracted)
  594. {
  595. destination[X_AXIS]=current_position[X_AXIS];
  596. destination[Y_AXIS]=current_position[Y_AXIS];
  597. destination[Z_AXIS]=current_position[Z_AXIS];
  598. current_position[Z_AXIS]+=retract_zlift;
  599. current_position[E_AXIS]+=-retract_recover_length;
  600. feedrate=retract_recover_feedrate;
  601. retracted=false;
  602. prepare_move();
  603. }
  604. break;
  605. #endif //FWRETRACT
  606. case 28: //G28 Home all Axis one at a time
  607. saved_feedrate = feedrate;
  608. saved_feedmultiply = feedmultiply;
  609. feedmultiply = 100;
  610. previous_millis_cmd = millis();
  611. enable_endstops(true);
  612. for(int8_t i=0; i < NUM_AXIS; i++) {
  613. destination[i] = current_position[i];
  614. }
  615. feedrate = 0.0;
  616. home_all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2])));
  617. #if Z_HOME_DIR > 0 // If homing away from BED do Z first
  618. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  619. HOMEAXIS(Z);
  620. }
  621. #endif
  622. #ifdef QUICK_HOME
  623. if((home_all_axis)||( code_seen(axis_codes[X_AXIS]) && code_seen(axis_codes[Y_AXIS])) ) //first diagonal move
  624. {
  625. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  626. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  627. destination[X_AXIS] = 1.5 * X_MAX_LENGTH * X_HOME_DIR;destination[Y_AXIS] = 1.5 * Y_MAX_LENGTH * Y_HOME_DIR;
  628. feedrate = homing_feedrate[X_AXIS];
  629. if(homing_feedrate[Y_AXIS]<feedrate)
  630. feedrate =homing_feedrate[Y_AXIS];
  631. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  632. st_synchronize();
  633. axis_is_at_home(X_AXIS);
  634. axis_is_at_home(Y_AXIS);
  635. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  636. destination[X_AXIS] = current_position[X_AXIS];
  637. destination[Y_AXIS] = current_position[Y_AXIS];
  638. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  639. feedrate = 0.0;
  640. st_synchronize();
  641. endstops_hit_on_purpose();
  642. }
  643. #endif
  644. if((home_all_axis) || (code_seen(axis_codes[X_AXIS])))
  645. {
  646. HOMEAXIS(X);
  647. }
  648. if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) {
  649. HOMEAXIS(Y);
  650. }
  651. #if Z_HOME_DIR < 0 // If homing towards BED do Z last
  652. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  653. HOMEAXIS(Z);
  654. }
  655. #endif
  656. if(code_seen(axis_codes[X_AXIS]))
  657. {
  658. if(code_value_long() != 0) {
  659. current_position[X_AXIS]=code_value()+add_homeing[0];
  660. }
  661. }
  662. if(code_seen(axis_codes[Y_AXIS])) {
  663. if(code_value_long() != 0) {
  664. current_position[Y_AXIS]=code_value()+add_homeing[1];
  665. }
  666. }
  667. if(code_seen(axis_codes[Z_AXIS])) {
  668. if(code_value_long() != 0) {
  669. current_position[Z_AXIS]=code_value()+add_homeing[2];
  670. }
  671. }
  672. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  673. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  674. enable_endstops(false);
  675. #endif
  676. feedrate = saved_feedrate;
  677. feedmultiply = saved_feedmultiply;
  678. previous_millis_cmd = millis();
  679. endstops_hit_on_purpose();
  680. break;
  681. case 90: // G90
  682. relative_mode = false;
  683. break;
  684. case 91: // G91
  685. relative_mode = true;
  686. break;
  687. case 92: // G92
  688. if(!code_seen(axis_codes[E_AXIS]))
  689. st_synchronize();
  690. for(int8_t i=0; i < NUM_AXIS; i++) {
  691. if(code_seen(axis_codes[i])) {
  692. if(i == E_AXIS) {
  693. current_position[i] = code_value();
  694. plan_set_e_position(current_position[E_AXIS]);
  695. }
  696. else {
  697. current_position[i] = code_value()+add_homeing[i];
  698. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  699. }
  700. }
  701. }
  702. break;
  703. }
  704. }
  705. else if(code_seen('M'))
  706. {
  707. switch( (int)code_value() )
  708. {
  709. #ifdef ULTRA_LCD
  710. case 0: // M0 - Unconditional stop - Wait for user button press on LCD
  711. case 1: // M1 - Conditional stop - Wait for user button press on LCD
  712. {
  713. LCD_MESSAGEPGM(MSG_USERWAIT);
  714. codenum = 0;
  715. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  716. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  717. st_synchronize();
  718. previous_millis_cmd = millis();
  719. if (codenum > 0)
  720. {
  721. codenum += millis(); // keep track of when we started waiting
  722. while(millis() < codenum && !CLICKED){
  723. manage_heater();
  724. manage_inactivity(1);
  725. LCD_STATUS;
  726. }
  727. }else{
  728. while(!CLICKED) {
  729. manage_heater();
  730. manage_inactivity(1);
  731. LCD_STATUS;
  732. }
  733. }
  734. }
  735. break;
  736. #endif
  737. case 17:
  738. LCD_MESSAGEPGM(MSG_NO_MOVE);
  739. enable_x();
  740. enable_y();
  741. enable_z();
  742. enable_e0();
  743. enable_e1();
  744. enable_e2();
  745. break;
  746. #ifdef SDSUPPORT
  747. case 20: // M20 - list SD card
  748. SERIAL_PROTOCOLLNPGM(MSG_BEGIN_FILE_LIST);
  749. card.ls();
  750. SERIAL_PROTOCOLLNPGM(MSG_END_FILE_LIST);
  751. break;
  752. case 21: // M21 - init SD card
  753. card.initsd();
  754. break;
  755. case 22: //M22 - release SD card
  756. card.release();
  757. break;
  758. case 23: //M23 - Select file
  759. starpos = (strchr(strchr_pointer + 4,'*'));
  760. if(starpos!=NULL)
  761. *(starpos-1)='\0';
  762. card.openFile(strchr_pointer + 4,true);
  763. break;
  764. case 24: //M24 - Start SD print
  765. card.startFileprint();
  766. starttime=millis();
  767. break;
  768. case 25: //M25 - Pause SD print
  769. card.pauseSDPrint();
  770. break;
  771. case 26: //M26 - Set SD index
  772. if(card.cardOK && code_seen('S')) {
  773. card.setIndex(code_value_long());
  774. }
  775. break;
  776. case 27: //M27 - Get SD status
  777. card.getStatus();
  778. break;
  779. case 28: //M28 - Start SD write
  780. starpos = (strchr(strchr_pointer + 4,'*'));
  781. if(starpos != NULL){
  782. char* npos = strchr(cmdbuffer[bufindr], 'N');
  783. strchr_pointer = strchr(npos,' ') + 1;
  784. *(starpos-1) = '\0';
  785. }
  786. card.openFile(strchr_pointer+4,false);
  787. break;
  788. case 29: //M29 - Stop SD write
  789. //processed in write to file routine above
  790. //card,saving = false;
  791. break;
  792. case 30: //M30 <filename> Delete File
  793. if (card.cardOK){
  794. card.closefile();
  795. starpos = (strchr(strchr_pointer + 4,'*'));
  796. if(starpos != NULL){
  797. char* npos = strchr(cmdbuffer[bufindr], 'N');
  798. strchr_pointer = strchr(npos,' ') + 1;
  799. *(starpos-1) = '\0';
  800. }
  801. card.removeFile(strchr_pointer + 4);
  802. }
  803. break;
  804. #endif //SDSUPPORT
  805. case 31: //M31 take time since the start of the SD print or an M109 command
  806. {
  807. stoptime=millis();
  808. char time[30];
  809. unsigned long t=(stoptime-starttime)/1000;
  810. int sec,min;
  811. min=t/60;
  812. sec=t%60;
  813. sprintf(time,"%i min, %i sec",min,sec);
  814. SERIAL_ECHO_START;
  815. SERIAL_ECHOLN(time);
  816. LCD_MESSAGE(time);
  817. autotempShutdown();
  818. }
  819. break;
  820. case 42: //M42 -Change pin status via gcode
  821. if (code_seen('S'))
  822. {
  823. int pin_status = code_value();
  824. if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
  825. {
  826. int pin_number = code_value();
  827. for(int8_t i = 0; i < (int8_t)sizeof(sensitive_pins); i++)
  828. {
  829. if (sensitive_pins[i] == pin_number)
  830. {
  831. pin_number = -1;
  832. break;
  833. }
  834. }
  835. if (pin_number > -1)
  836. {
  837. pinMode(pin_number, OUTPUT);
  838. digitalWrite(pin_number, pin_status);
  839. analogWrite(pin_number, pin_status);
  840. }
  841. }
  842. }
  843. break;
  844. case 104: // M104
  845. tmp_extruder = active_extruder;
  846. if(code_seen('T')) {
  847. tmp_extruder = code_value();
  848. if(tmp_extruder >= EXTRUDERS) {
  849. SERIAL_ECHO_START;
  850. SERIAL_ECHO(MSG_M104_INVALID_EXTRUDER);
  851. SERIAL_ECHOLN(tmp_extruder);
  852. break;
  853. }
  854. }
  855. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  856. setWatch();
  857. break;
  858. case 140: // M140 set bed temp
  859. if (code_seen('S')) setTargetBed(code_value());
  860. break;
  861. case 105 : // M105
  862. tmp_extruder = active_extruder;
  863. if(code_seen('T')) {
  864. tmp_extruder = code_value();
  865. if(tmp_extruder >= EXTRUDERS) {
  866. SERIAL_ECHO_START;
  867. SERIAL_ECHO(MSG_M105_INVALID_EXTRUDER);
  868. SERIAL_ECHOLN(tmp_extruder);
  869. break;
  870. }
  871. }
  872. #if (TEMP_0_PIN > -1)
  873. SERIAL_PROTOCOLPGM("ok T:");
  874. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  875. SERIAL_PROTOCOLPGM(" /");
  876. SERIAL_PROTOCOL_F(degTargetHotend(tmp_extruder),1);
  877. #if TEMP_BED_PIN > -1
  878. SERIAL_PROTOCOLPGM(" B:");
  879. SERIAL_PROTOCOL_F(degBed(),1);
  880. SERIAL_PROTOCOLPGM(" /");
  881. SERIAL_PROTOCOL_F(degTargetBed(),1);
  882. #endif //TEMP_BED_PIN
  883. #else
  884. SERIAL_ERROR_START;
  885. SERIAL_ERRORLNPGM(MSG_ERR_NO_THERMISTORS);
  886. #endif
  887. #ifdef PIDTEMP
  888. SERIAL_PROTOCOLPGM(" @:");
  889. SERIAL_PROTOCOL(getHeaterPower(tmp_extruder));
  890. #endif
  891. SERIAL_PROTOCOLLN("");
  892. return;
  893. break;
  894. case 109:
  895. {// M109 - Wait for extruder heater to reach target.
  896. tmp_extruder = active_extruder;
  897. if(code_seen('T')) {
  898. tmp_extruder = code_value();
  899. if(tmp_extruder >= EXTRUDERS) {
  900. SERIAL_ECHO_START;
  901. SERIAL_ECHO(MSG_M109_INVALID_EXTRUDER);
  902. SERIAL_ECHOLN(tmp_extruder);
  903. break;
  904. }
  905. }
  906. LCD_MESSAGEPGM(MSG_HEATING);
  907. #ifdef AUTOTEMP
  908. autotemp_enabled=false;
  909. #endif
  910. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  911. #ifdef AUTOTEMP
  912. if (code_seen('S')) autotemp_min=code_value();
  913. if (code_seen('B')) autotemp_max=code_value();
  914. if (code_seen('F'))
  915. {
  916. autotemp_factor=code_value();
  917. autotemp_enabled=true;
  918. }
  919. #endif
  920. setWatch();
  921. codenum = millis();
  922. /* See if we are heating up or cooling down */
  923. bool target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling
  924. #ifdef TEMP_RESIDENCY_TIME
  925. long residencyStart;
  926. residencyStart = -1;
  927. /* continue to loop until we have reached the target temp
  928. _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
  929. while((residencyStart == -1) ||
  930. (residencyStart >= 0 && (((unsigned int) (millis() - residencyStart)) < (TEMP_RESIDENCY_TIME * 1000UL))) ) {
  931. #else
  932. while ( target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder)&&(CooldownNoWait==false)) ) {
  933. #endif //TEMP_RESIDENCY_TIME
  934. if( (millis() - codenum) > 1000UL )
  935. { //Print Temp Reading and remaining time every 1 second while heating up/cooling down
  936. SERIAL_PROTOCOLPGM("T:");
  937. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  938. SERIAL_PROTOCOLPGM(" E:");
  939. SERIAL_PROTOCOL((int)tmp_extruder);
  940. #ifdef TEMP_RESIDENCY_TIME
  941. SERIAL_PROTOCOLPGM(" W:");
  942. if(residencyStart > -1)
  943. {
  944. codenum = ((TEMP_RESIDENCY_TIME * 1000UL) - (millis() - residencyStart)) / 1000UL;
  945. SERIAL_PROTOCOLLN( codenum );
  946. }
  947. else
  948. {
  949. SERIAL_PROTOCOLLN( "?" );
  950. }
  951. #else
  952. SERIAL_PROTOCOLLN("");
  953. #endif
  954. codenum = millis();
  955. }
  956. manage_heater();
  957. manage_inactivity(1);
  958. LCD_STATUS;
  959. #ifdef TEMP_RESIDENCY_TIME
  960. /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
  961. or when current temp falls outside the hysteresis after target temp was reached */
  962. if ((residencyStart == -1 && target_direction && (degHotend(tmp_extruder) >= (degTargetHotend(tmp_extruder)-TEMP_WINDOW))) ||
  963. (residencyStart == -1 && !target_direction && (degHotend(tmp_extruder) <= (degTargetHotend(tmp_extruder)+TEMP_WINDOW))) ||
  964. (residencyStart > -1 && labs(degHotend(tmp_extruder) - degTargetHotend(tmp_extruder)) > TEMP_HYSTERESIS) )
  965. {
  966. residencyStart = millis();
  967. }
  968. #endif //TEMP_RESIDENCY_TIME
  969. }
  970. LCD_MESSAGEPGM(MSG_HEATING_COMPLETE);
  971. starttime=millis();
  972. previous_millis_cmd = millis();
  973. }
  974. break;
  975. case 190: // M190 - Wait for bed heater to reach target.
  976. #if TEMP_BED_PIN > -1
  977. LCD_MESSAGEPGM(MSG_BED_HEATING);
  978. if (code_seen('S')) setTargetBed(code_value());
  979. codenum = millis();
  980. while(isHeatingBed())
  981. {
  982. if(( millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
  983. {
  984. float tt=degHotend(active_extruder);
  985. SERIAL_PROTOCOLPGM("T:");
  986. SERIAL_PROTOCOL(tt);
  987. SERIAL_PROTOCOLPGM(" E:");
  988. SERIAL_PROTOCOL((int)active_extruder);
  989. SERIAL_PROTOCOLPGM(" B:");
  990. SERIAL_PROTOCOL_F(degBed(),1);
  991. SERIAL_PROTOCOLLN("");
  992. codenum = millis();
  993. }
  994. manage_heater();
  995. manage_inactivity(1);
  996. LCD_STATUS;
  997. }
  998. LCD_MESSAGEPGM(MSG_BED_DONE);
  999. previous_millis_cmd = millis();
  1000. #endif
  1001. break;
  1002. #if FAN_PIN > -1
  1003. case 106: //M106 Fan On
  1004. if (code_seen('S')){
  1005. FanSpeed=constrain(code_value(),0,255);
  1006. }
  1007. else {
  1008. FanSpeed=255;
  1009. }
  1010. break;
  1011. case 107: //M107 Fan Off
  1012. FanSpeed = 0;
  1013. break;
  1014. #endif //FAN_PIN
  1015. #if (PS_ON_PIN > -1)
  1016. case 80: // M80 - ATX Power On
  1017. SET_OUTPUT(PS_ON_PIN); //GND
  1018. WRITE(PS_ON_PIN, LOW);
  1019. break;
  1020. #endif
  1021. case 81: // M81 - ATX Power Off
  1022. #if defined SUICIDE_PIN && SUICIDE_PIN > -1
  1023. st_synchronize();
  1024. suicide();
  1025. #elif (PS_ON_PIN > -1)
  1026. SET_INPUT(PS_ON_PIN); //Floating
  1027. #endif
  1028. break;
  1029. case 82:
  1030. axis_relative_modes[3] = false;
  1031. break;
  1032. case 83:
  1033. axis_relative_modes[3] = true;
  1034. break;
  1035. case 18: //compatibility
  1036. case 84: // M84
  1037. if(code_seen('S')){
  1038. stepper_inactive_time = code_value() * 1000;
  1039. }
  1040. else
  1041. {
  1042. bool all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2]))|| (code_seen(axis_codes[3])));
  1043. if(all_axis)
  1044. {
  1045. st_synchronize();
  1046. disable_e0();
  1047. disable_e1();
  1048. disable_e2();
  1049. finishAndDisableSteppers();
  1050. }
  1051. else
  1052. {
  1053. st_synchronize();
  1054. if(code_seen('X')) disable_x();
  1055. if(code_seen('Y')) disable_y();
  1056. if(code_seen('Z')) disable_z();
  1057. #if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
  1058. if(code_seen('E')) {
  1059. disable_e0();
  1060. disable_e1();
  1061. disable_e2();
  1062. }
  1063. #endif
  1064. LCD_MESSAGEPGM(MSG_PART_RELEASE);
  1065. }
  1066. }
  1067. break;
  1068. case 85: // M85
  1069. code_seen('S');
  1070. max_inactive_time = code_value() * 1000;
  1071. break;
  1072. case 92: // M92
  1073. for(int8_t i=0; i < NUM_AXIS; i++)
  1074. {
  1075. if(code_seen(axis_codes[i]))
  1076. if(i == 3) { // E
  1077. float value = code_value();
  1078. if(value < 20.0) {
  1079. float factor = axis_steps_per_unit[i] / value; // increase e constants if M92 E14 is given for netfab.
  1080. max_e_jerk *= factor;
  1081. max_feedrate[i] *= factor;
  1082. axis_steps_per_sqr_second[i] *= factor;
  1083. }
  1084. axis_steps_per_unit[i] = value;
  1085. }
  1086. else {
  1087. axis_steps_per_unit[i] = code_value();
  1088. }
  1089. }
  1090. break;
  1091. case 115: // M115
  1092. SerialprintPGM(MSG_M115_REPORT);
  1093. break;
  1094. case 117: // M117 display message
  1095. LCD_MESSAGE(cmdbuffer[bufindr]+5);
  1096. break;
  1097. case 114: // M114
  1098. SERIAL_PROTOCOLPGM("X:");
  1099. SERIAL_PROTOCOL(current_position[X_AXIS]);
  1100. SERIAL_PROTOCOLPGM("Y:");
  1101. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  1102. SERIAL_PROTOCOLPGM("Z:");
  1103. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  1104. SERIAL_PROTOCOLPGM("E:");
  1105. SERIAL_PROTOCOL(current_position[E_AXIS]);
  1106. SERIAL_PROTOCOLPGM(MSG_COUNT_X);
  1107. SERIAL_PROTOCOL(float(st_get_position(X_AXIS))/axis_steps_per_unit[X_AXIS]);
  1108. SERIAL_PROTOCOLPGM("Y:");
  1109. SERIAL_PROTOCOL(float(st_get_position(Y_AXIS))/axis_steps_per_unit[Y_AXIS]);
  1110. SERIAL_PROTOCOLPGM("Z:");
  1111. SERIAL_PROTOCOL(float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]);
  1112. SERIAL_PROTOCOLLN("");
  1113. break;
  1114. case 120: // M120
  1115. enable_endstops(false) ;
  1116. break;
  1117. case 121: // M121
  1118. enable_endstops(true) ;
  1119. break;
  1120. case 119: // M119
  1121. #if (X_MIN_PIN > -1)
  1122. SERIAL_PROTOCOLPGM(MSG_X_MIN);
  1123. SERIAL_PROTOCOL(((READ(X_MIN_PIN)^X_ENDSTOPS_INVERTING)?"H ":"L "));
  1124. #endif
  1125. #if (X_MAX_PIN > -1)
  1126. SERIAL_PROTOCOLPGM(MSG_X_MAX);
  1127. SERIAL_PROTOCOL(((READ(X_MAX_PIN)^X_ENDSTOPS_INVERTING)?"H ":"L "));
  1128. #endif
  1129. #if (Y_MIN_PIN > -1)
  1130. SERIAL_PROTOCOLPGM(MSG_Y_MIN);
  1131. SERIAL_PROTOCOL(((READ(Y_MIN_PIN)^Y_ENDSTOPS_INVERTING)?"H ":"L "));
  1132. #endif
  1133. #if (Y_MAX_PIN > -1)
  1134. SERIAL_PROTOCOLPGM(MSG_Y_MAX);
  1135. SERIAL_PROTOCOL(((READ(Y_MAX_PIN)^Y_ENDSTOPS_INVERTING)?"H ":"L "));
  1136. #endif
  1137. #if (Z_MIN_PIN > -1)
  1138. SERIAL_PROTOCOLPGM(MSG_Z_MIN);
  1139. SERIAL_PROTOCOL(((READ(Z_MIN_PIN)^Z_ENDSTOPS_INVERTING)?"H ":"L "));
  1140. #endif
  1141. #if (Z_MAX_PIN > -1)
  1142. SERIAL_PROTOCOLPGM(MSG_Z_MAX);
  1143. SERIAL_PROTOCOL(((READ(Z_MAX_PIN)^Z_ENDSTOPS_INVERTING)?"H ":"L "));
  1144. #endif
  1145. SERIAL_PROTOCOLLN("");
  1146. break;
  1147. //TODO: update for all axis, use for loop
  1148. case 201: // M201
  1149. for(int8_t i=0; i < NUM_AXIS; i++)
  1150. {
  1151. if(code_seen(axis_codes[i]))
  1152. {
  1153. max_acceleration_units_per_sq_second[i] = code_value();
  1154. axis_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  1155. }
  1156. }
  1157. break;
  1158. #if 0 // Not used for Sprinter/grbl gen6
  1159. case 202: // M202
  1160. for(int8_t i=0; i < NUM_AXIS; i++) {
  1161. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  1162. }
  1163. break;
  1164. #endif
  1165. case 203: // M203 max feedrate mm/sec
  1166. for(int8_t i=0; i < NUM_AXIS; i++) {
  1167. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value();
  1168. }
  1169. break;
  1170. case 204: // M204 acclereration S normal moves T filmanent only moves
  1171. {
  1172. if(code_seen('S')) acceleration = code_value() ;
  1173. if(code_seen('T')) retract_acceleration = code_value() ;
  1174. }
  1175. break;
  1176. 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
  1177. {
  1178. if(code_seen('S')) minimumfeedrate = code_value();
  1179. if(code_seen('T')) mintravelfeedrate = code_value();
  1180. if(code_seen('B')) minsegmenttime = code_value() ;
  1181. if(code_seen('X')) max_xy_jerk = code_value() ;
  1182. if(code_seen('Z')) max_z_jerk = code_value() ;
  1183. if(code_seen('E')) max_e_jerk = code_value() ;
  1184. }
  1185. break;
  1186. case 206: // M206 additional homeing offset
  1187. for(int8_t i=0; i < 3; i++)
  1188. {
  1189. if(code_seen(axis_codes[i])) add_homeing[i] = code_value();
  1190. }
  1191. break;
  1192. #ifdef FWRETRACT
  1193. case 207: //M207 - set retract length S[positive mm] F[feedrate mm/sec] Z[additional zlift/hop]
  1194. {
  1195. if(code_seen('S'))
  1196. {
  1197. retract_length = code_value() ;
  1198. }
  1199. if(code_seen('F'))
  1200. {
  1201. retract_feedrate = code_value() ;
  1202. }
  1203. if(code_seen('Z'))
  1204. {
  1205. retract_zlift = code_value() ;
  1206. }
  1207. }break;
  1208. case 208: // M208 - set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  1209. {
  1210. if(code_seen('S'))
  1211. {
  1212. retract_recover_length = code_value() ;
  1213. }
  1214. if(code_seen('F'))
  1215. {
  1216. retract_recover_feedrate = code_value() ;
  1217. }
  1218. }break;
  1219. 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.
  1220. {
  1221. if(code_seen('S'))
  1222. {
  1223. int t= code_value() ;
  1224. switch(t)
  1225. {
  1226. case 0: autoretract_enabled=false;retracted=false;break;
  1227. case 1: autoretract_enabled=true;retracted=false;break;
  1228. default:
  1229. SERIAL_ECHO_START;
  1230. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  1231. SERIAL_ECHO(cmdbuffer[bufindr]);
  1232. SERIAL_ECHOLNPGM("\"");
  1233. }
  1234. }
  1235. }break;
  1236. #endif
  1237. case 220: // M220 S<factor in percent>- set speed factor override percentage
  1238. {
  1239. if(code_seen('S'))
  1240. {
  1241. feedmultiply = code_value() ;
  1242. feedmultiplychanged=true;
  1243. }
  1244. }
  1245. break;
  1246. case 221: // M221 S<factor in percent>- set extrude factor override percentage
  1247. {
  1248. if(code_seen('S'))
  1249. {
  1250. extrudemultiply = code_value() ;
  1251. }
  1252. }
  1253. break;
  1254. #ifdef PIDTEMP
  1255. case 301: // M301
  1256. {
  1257. if(code_seen('P')) Kp = code_value();
  1258. if(code_seen('I')) Ki = code_value()*PID_dT;
  1259. if(code_seen('D')) Kd = code_value()/PID_dT;
  1260. #ifdef PID_ADD_EXTRUSION_RATE
  1261. if(code_seen('C')) Kc = code_value();
  1262. #endif
  1263. updatePID();
  1264. SERIAL_PROTOCOL(MSG_OK);
  1265. SERIAL_PROTOCOL(" p:");
  1266. SERIAL_PROTOCOL(Kp);
  1267. SERIAL_PROTOCOL(" i:");
  1268. SERIAL_PROTOCOL(Ki/PID_dT);
  1269. SERIAL_PROTOCOL(" d:");
  1270. SERIAL_PROTOCOL(Kd*PID_dT);
  1271. #ifdef PID_ADD_EXTRUSION_RATE
  1272. SERIAL_PROTOCOL(" c:");
  1273. SERIAL_PROTOCOL(Kc*PID_dT);
  1274. #endif
  1275. SERIAL_PROTOCOLLN("");
  1276. }
  1277. break;
  1278. #endif //PIDTEMP
  1279. case 240: // M240 Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
  1280. {
  1281. #ifdef PHOTOGRAPH_PIN
  1282. #if (PHOTOGRAPH_PIN > -1)
  1283. const uint8_t NUM_PULSES=16;
  1284. const float PULSE_LENGTH=0.01524;
  1285. for(int i=0; i < NUM_PULSES; i++) {
  1286. WRITE(PHOTOGRAPH_PIN, HIGH);
  1287. _delay_ms(PULSE_LENGTH);
  1288. WRITE(PHOTOGRAPH_PIN, LOW);
  1289. _delay_ms(PULSE_LENGTH);
  1290. }
  1291. delay(7.33);
  1292. for(int i=0; i < NUM_PULSES; i++) {
  1293. WRITE(PHOTOGRAPH_PIN, HIGH);
  1294. _delay_ms(PULSE_LENGTH);
  1295. WRITE(PHOTOGRAPH_PIN, LOW);
  1296. _delay_ms(PULSE_LENGTH);
  1297. }
  1298. #endif
  1299. #endif
  1300. }
  1301. break;
  1302. case 302: // allow cold extrudes
  1303. {
  1304. allow_cold_extrudes(true);
  1305. }
  1306. break;
  1307. case 303: // M303 PID autotune
  1308. {
  1309. float temp = 150.0;
  1310. if (code_seen('S')) temp=code_value();
  1311. PID_autotune(temp);
  1312. }
  1313. break;
  1314. case 400: // M400 finish all moves
  1315. {
  1316. st_synchronize();
  1317. }
  1318. break;
  1319. case 500: // Store settings in EEPROM
  1320. {
  1321. EEPROM_StoreSettings();
  1322. }
  1323. break;
  1324. case 501: // Read settings from EEPROM
  1325. {
  1326. EEPROM_RetrieveSettings();
  1327. }
  1328. break;
  1329. case 502: // Revert to default settings
  1330. {
  1331. EEPROM_RetrieveSettings(true);
  1332. }
  1333. break;
  1334. case 503: // print settings currently in memory
  1335. {
  1336. EEPROM_printSettings();
  1337. }
  1338. break;
  1339. case 999: // Restart after being stopped
  1340. Stopped = false;
  1341. gcode_LastN = Stopped_gcode_LastN;
  1342. FlushSerialRequestResend();
  1343. break;
  1344. }
  1345. }
  1346. else if(code_seen('T'))
  1347. {
  1348. tmp_extruder = code_value();
  1349. if(tmp_extruder >= EXTRUDERS) {
  1350. SERIAL_ECHO_START;
  1351. SERIAL_ECHO("T");
  1352. SERIAL_ECHO(tmp_extruder);
  1353. SERIAL_ECHOLN(MSG_INVALID_EXTRUDER);
  1354. }
  1355. else {
  1356. active_extruder = tmp_extruder;
  1357. SERIAL_ECHO_START;
  1358. SERIAL_ECHO(MSG_ACTIVE_EXTRUDER);
  1359. SERIAL_PROTOCOLLN((int)active_extruder);
  1360. }
  1361. }
  1362. else
  1363. {
  1364. SERIAL_ECHO_START;
  1365. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  1366. SERIAL_ECHO(cmdbuffer[bufindr]);
  1367. SERIAL_ECHOLNPGM("\"");
  1368. }
  1369. ClearToSend();
  1370. }
  1371. void FlushSerialRequestResend()
  1372. {
  1373. //char cmdbuffer[bufindr][100]="Resend:";
  1374. MYSERIAL.flush();
  1375. SERIAL_PROTOCOLPGM(MSG_RESEND);
  1376. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  1377. ClearToSend();
  1378. }
  1379. void ClearToSend()
  1380. {
  1381. previous_millis_cmd = millis();
  1382. #ifdef SDSUPPORT
  1383. if(fromsd[bufindr])
  1384. return;
  1385. #endif //SDSUPPORT
  1386. SERIAL_PROTOCOLLNPGM(MSG_OK);
  1387. }
  1388. void get_coordinates()
  1389. {
  1390. bool seen[4]={false,false,false,false};
  1391. for(int8_t i=0; i < NUM_AXIS; i++) {
  1392. if(code_seen(axis_codes[i]))
  1393. {
  1394. destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
  1395. seen[i]=true;
  1396. }
  1397. else destination[i] = current_position[i]; //Are these else lines really needed?
  1398. }
  1399. if(code_seen('F')) {
  1400. next_feedrate = code_value();
  1401. if(next_feedrate > 0.0) feedrate = next_feedrate;
  1402. }
  1403. #ifdef FWRETRACT
  1404. if(autoretract_enabled)
  1405. if( !(seen[X_AXIS] || seen[Y_AXIS] || seen[Z_AXIS]) && seen[E_AXIS])
  1406. {
  1407. float echange=destination[E_AXIS]-current_position[E_AXIS];
  1408. if(echange<-MIN_RETRACT) //retract
  1409. {
  1410. if(!retracted)
  1411. {
  1412. destination[Z_AXIS]+=retract_zlift; //not sure why chaninging current_position negatively does not work.
  1413. //if slicer retracted by echange=-1mm and you want to retract 3mm, corrrectede=-2mm additionally
  1414. float correctede=-echange-retract_length;
  1415. //to generate the additional steps, not the destination is changed, but inversely the current position
  1416. current_position[E_AXIS]+=-correctede;
  1417. feedrate=retract_feedrate;
  1418. retracted=true;
  1419. }
  1420. }
  1421. else
  1422. if(echange>MIN_RETRACT) //retract_recover
  1423. {
  1424. if(retracted)
  1425. {
  1426. //current_position[Z_AXIS]+=-retract_zlift;
  1427. //if slicer retracted_recovered by echange=+1mm and you want to retract_recover 3mm, corrrectede=2mm additionally
  1428. float correctede=-echange+1*retract_length+retract_recover_length; //total unretract=retract_length+retract_recover_length[surplus]
  1429. current_position[E_AXIS]+=correctede; //to generate the additional steps, not the destination is changed, but inversely the current position
  1430. feedrate=retract_recover_feedrate;
  1431. retracted=false;
  1432. }
  1433. }
  1434. }
  1435. #endif //FWRETRACT
  1436. }
  1437. void get_arc_coordinates()
  1438. {
  1439. get_coordinates();
  1440. if(code_seen('I')) {
  1441. offset[0] = code_value();
  1442. }
  1443. else {
  1444. offset[0] = 0.0;
  1445. }
  1446. if(code_seen('J')) {
  1447. offset[1] = code_value();
  1448. }
  1449. else {
  1450. offset[1] = 0.0;
  1451. }
  1452. }
  1453. void clamp_to_software_endstops(float target[3])
  1454. {
  1455. if (min_software_endstops) {
  1456. if (target[X_AXIS] < min_pos[X_AXIS]) target[X_AXIS] = min_pos[X_AXIS];
  1457. if (target[Y_AXIS] < min_pos[Y_AXIS]) target[Y_AXIS] = min_pos[Y_AXIS];
  1458. if (target[Z_AXIS] < min_pos[Z_AXIS]) target[Z_AXIS] = min_pos[Z_AXIS];
  1459. }
  1460. if (max_software_endstops) {
  1461. if (target[X_AXIS] > max_pos[X_AXIS]) target[X_AXIS] = max_pos[X_AXIS];
  1462. if (target[Y_AXIS] > max_pos[Y_AXIS]) target[Y_AXIS] = max_pos[Y_AXIS];
  1463. if (target[Z_AXIS] > max_pos[Z_AXIS]) target[Z_AXIS] = max_pos[Z_AXIS];
  1464. }
  1465. }
  1466. void prepare_move()
  1467. {
  1468. clamp_to_software_endstops(destination);
  1469. previous_millis_cmd = millis();
  1470. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
  1471. for(int8_t i=0; i < NUM_AXIS; i++) {
  1472. current_position[i] = destination[i];
  1473. }
  1474. }
  1475. void prepare_arc_move(char isclockwise) {
  1476. float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
  1477. // Trace the arc
  1478. mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
  1479. // As far as the parser is concerned, the position is now == target. In reality the
  1480. // motion control system might still be processing the action and the real tool position
  1481. // in any intermediate location.
  1482. for(int8_t i=0; i < NUM_AXIS; i++) {
  1483. current_position[i] = destination[i];
  1484. }
  1485. previous_millis_cmd = millis();
  1486. }
  1487. #ifdef CONTROLLERFAN_PIN
  1488. unsigned long lastMotor = 0; //Save the time for when a motor was turned on last
  1489. unsigned long lastMotorCheck = 0;
  1490. void controllerFan()
  1491. {
  1492. if ((millis() - lastMotorCheck) >= 2500) //Not a time critical function, so we only check every 2500ms
  1493. {
  1494. lastMotorCheck = millis();
  1495. if(!READ(X_ENABLE_PIN) || !READ(Y_ENABLE_PIN) || !READ(Z_ENABLE_PIN)
  1496. #if EXTRUDERS > 2
  1497. || !READ(E2_ENABLE_PIN)
  1498. #endif
  1499. #if EXTRUDER > 1
  1500. || !READ(E2_ENABLE_PIN)
  1501. #endif
  1502. || !READ(E0_ENABLE_PIN)) //If any of the drivers are enabled...
  1503. {
  1504. lastMotor = millis(); //... set time to NOW so the fan will turn on
  1505. }
  1506. if ((millis() - lastMotor) >= (CONTROLLERFAN_SEC*1000UL) || lastMotor == 0) //If the last time any driver was enabled, is longer since than CONTROLLERSEC...
  1507. {
  1508. WRITE(CONTROLLERFAN_PIN, LOW); //... turn the fan off
  1509. }
  1510. else
  1511. {
  1512. WRITE(CONTROLLERFAN_PIN, HIGH); //... turn the fan on
  1513. }
  1514. }
  1515. }
  1516. #endif
  1517. void manage_inactivity(byte debug)
  1518. {
  1519. if( (millis() - previous_millis_cmd) > max_inactive_time )
  1520. if(max_inactive_time)
  1521. kill();
  1522. if(stepper_inactive_time) {
  1523. if( (millis() - previous_millis_cmd) > stepper_inactive_time )
  1524. {
  1525. if(blocks_queued() == false) {
  1526. disable_x();
  1527. disable_y();
  1528. disable_z();
  1529. disable_e0();
  1530. disable_e1();
  1531. disable_e2();
  1532. }
  1533. }
  1534. }
  1535. #ifdef CONTROLLERFAN_PIN
  1536. controllerFan(); //Check if fan should be turned on to cool stepper drivers down
  1537. #endif
  1538. #ifdef EXTRUDER_RUNOUT_PREVENT
  1539. if( (millis() - previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 )
  1540. if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
  1541. {
  1542. bool oldstatus=READ(E0_ENABLE_PIN);
  1543. enable_e0();
  1544. float oldepos=current_position[E_AXIS];
  1545. float oldedes=destination[E_AXIS];
  1546. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  1547. current_position[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS],
  1548. EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
  1549. current_position[E_AXIS]=oldepos;
  1550. destination[E_AXIS]=oldedes;
  1551. plan_set_e_position(oldepos);
  1552. previous_millis_cmd=millis();
  1553. st_synchronize();
  1554. WRITE(E0_ENABLE_PIN,oldstatus);
  1555. }
  1556. #endif
  1557. check_axes_activity();
  1558. }
  1559. void kill()
  1560. {
  1561. cli(); // Stop interrupts
  1562. disable_heater();
  1563. disable_x();
  1564. disable_y();
  1565. disable_z();
  1566. disable_e0();
  1567. disable_e1();
  1568. disable_e2();
  1569. if(PS_ON_PIN > -1) pinMode(PS_ON_PIN,INPUT);
  1570. SERIAL_ERROR_START;
  1571. SERIAL_ERRORLNPGM(MSG_ERR_KILLED);
  1572. LCD_MESSAGEPGM(MSG_KILLED);
  1573. suicide();
  1574. while(1); // Wait for reset
  1575. }
  1576. void Stop()
  1577. {
  1578. disable_heater();
  1579. if(Stopped == false) {
  1580. Stopped = true;
  1581. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  1582. SERIAL_ERROR_START;
  1583. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  1584. LCD_MESSAGEPGM(MSG_STOPPED);
  1585. }
  1586. }
  1587. bool IsStopped() { return Stopped; };
  1588. #ifdef FAST_PWM_FAN
  1589. void setPwmFrequency(uint8_t pin, int val)
  1590. {
  1591. val &= 0x07;
  1592. switch(digitalPinToTimer(pin))
  1593. {
  1594. #if defined(TCCR0A)
  1595. case TIMER0A:
  1596. case TIMER0B:
  1597. // TCCR0B &= ~(CS00 | CS01 | CS02);
  1598. // TCCR0B |= val;
  1599. break;
  1600. #endif
  1601. #if defined(TCCR1A)
  1602. case TIMER1A:
  1603. case TIMER1B:
  1604. // TCCR1B &= ~(CS10 | CS11 | CS12);
  1605. // TCCR1B |= val;
  1606. break;
  1607. #endif
  1608. #if defined(TCCR2)
  1609. case TIMER2:
  1610. case TIMER2:
  1611. TCCR2 &= ~(CS10 | CS11 | CS12);
  1612. TCCR2 |= val;
  1613. break;
  1614. #endif
  1615. #if defined(TCCR2A)
  1616. case TIMER2A:
  1617. case TIMER2B:
  1618. TCCR2B &= ~(CS20 | CS21 | CS22);
  1619. TCCR2B |= val;
  1620. break;
  1621. #endif
  1622. #if defined(TCCR3A)
  1623. case TIMER3A:
  1624. case TIMER3B:
  1625. case TIMER3C:
  1626. TCCR3B &= ~(CS30 | CS31 | CS32);
  1627. TCCR3B |= val;
  1628. break;
  1629. #endif
  1630. #if defined(TCCR4A)
  1631. case TIMER4A:
  1632. case TIMER4B:
  1633. case TIMER4C:
  1634. TCCR4B &= ~(CS40 | CS41 | CS42);
  1635. TCCR4B |= val;
  1636. break;
  1637. #endif
  1638. #if defined(TCCR5A)
  1639. case TIMER5A:
  1640. case TIMER5B:
  1641. case TIMER5C:
  1642. TCCR5B &= ~(CS50 | CS51 | CS52);
  1643. TCCR5B |= val;
  1644. break;
  1645. #endif
  1646. }
  1647. }
  1648. #endif