My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Marlin.pde 55KB

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