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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837
  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_OUTPUT(PS_ON_PIN);
  1021. WRITE(PS_ON_PIN, HIGH);
  1022. #endif
  1023. break;
  1024. case 82:
  1025. axis_relative_modes[3] = false;
  1026. break;
  1027. case 83:
  1028. axis_relative_modes[3] = true;
  1029. break;
  1030. case 18: //compatibility
  1031. case 84: // M84
  1032. if(code_seen('S')){
  1033. stepper_inactive_time = code_value() * 1000;
  1034. }
  1035. else
  1036. {
  1037. bool all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2]))|| (code_seen(axis_codes[3])));
  1038. if(all_axis)
  1039. {
  1040. st_synchronize();
  1041. disable_e0();
  1042. disable_e1();
  1043. disable_e2();
  1044. finishAndDisableSteppers();
  1045. }
  1046. else
  1047. {
  1048. st_synchronize();
  1049. if(code_seen('X')) disable_x();
  1050. if(code_seen('Y')) disable_y();
  1051. if(code_seen('Z')) disable_z();
  1052. #if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
  1053. if(code_seen('E')) {
  1054. disable_e0();
  1055. disable_e1();
  1056. disable_e2();
  1057. }
  1058. #endif
  1059. LCD_MESSAGEPGM(MSG_PART_RELEASE);
  1060. }
  1061. }
  1062. break;
  1063. case 85: // M85
  1064. code_seen('S');
  1065. max_inactive_time = code_value() * 1000;
  1066. break;
  1067. case 92: // M92
  1068. for(int8_t i=0; i < NUM_AXIS; i++)
  1069. {
  1070. if(code_seen(axis_codes[i]))
  1071. if(i == 3) { // E
  1072. float value = code_value();
  1073. if(value < 20.0) {
  1074. float factor = axis_steps_per_unit[i] / value; // increase e constants if M92 E14 is given for netfab.
  1075. max_e_jerk *= factor;
  1076. max_feedrate[i] *= factor;
  1077. axis_steps_per_sqr_second[i] *= factor;
  1078. }
  1079. axis_steps_per_unit[i] = value;
  1080. }
  1081. else {
  1082. axis_steps_per_unit[i] = code_value();
  1083. }
  1084. }
  1085. break;
  1086. case 115: // M115
  1087. SerialprintPGM(MSG_M115_REPORT);
  1088. break;
  1089. case 117: // M117 display message
  1090. LCD_MESSAGE(cmdbuffer[bufindr]+5);
  1091. break;
  1092. case 114: // M114
  1093. SERIAL_PROTOCOLPGM("X:");
  1094. SERIAL_PROTOCOL(current_position[X_AXIS]);
  1095. SERIAL_PROTOCOLPGM("Y:");
  1096. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  1097. SERIAL_PROTOCOLPGM("Z:");
  1098. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  1099. SERIAL_PROTOCOLPGM("E:");
  1100. SERIAL_PROTOCOL(current_position[E_AXIS]);
  1101. SERIAL_PROTOCOLPGM(MSG_COUNT_X);
  1102. SERIAL_PROTOCOL(float(st_get_position(X_AXIS))/axis_steps_per_unit[X_AXIS]);
  1103. SERIAL_PROTOCOLPGM("Y:");
  1104. SERIAL_PROTOCOL(float(st_get_position(Y_AXIS))/axis_steps_per_unit[Y_AXIS]);
  1105. SERIAL_PROTOCOLPGM("Z:");
  1106. SERIAL_PROTOCOL(float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]);
  1107. SERIAL_PROTOCOLLN("");
  1108. break;
  1109. case 120: // M120
  1110. enable_endstops(false) ;
  1111. break;
  1112. case 121: // M121
  1113. enable_endstops(true) ;
  1114. break;
  1115. case 119: // M119
  1116. SERIAL_PROTOCOLLN(MSG_M119_REPORT);
  1117. #if (X_MIN_PIN > -1)
  1118. SERIAL_PROTOCOLPGM(MSG_X_MIN);
  1119. SERIAL_PROTOCOLLN(((READ(X_MIN_PIN)^X_ENDSTOPS_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1120. #endif
  1121. #if (X_MAX_PIN > -1)
  1122. SERIAL_PROTOCOLPGM(MSG_X_MAX);
  1123. SERIAL_PROTOCOLLN(((READ(X_MAX_PIN)^X_ENDSTOPS_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1124. #endif
  1125. #if (Y_MIN_PIN > -1)
  1126. SERIAL_PROTOCOLPGM(MSG_Y_MIN);
  1127. SERIAL_PROTOCOLLN(((READ(Y_MIN_PIN)^Y_ENDSTOPS_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1128. #endif
  1129. #if (Y_MAX_PIN > -1)
  1130. SERIAL_PROTOCOLPGM(MSG_Y_MAX);
  1131. SERIAL_PROTOCOLLN(((READ(Y_MAX_PIN)^Y_ENDSTOPS_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1132. #endif
  1133. #if (Z_MIN_PIN > -1)
  1134. SERIAL_PROTOCOLPGM(MSG_Z_MIN);
  1135. SERIAL_PROTOCOLLN(((READ(Z_MIN_PIN)^Z_ENDSTOPS_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1136. #endif
  1137. #if (Z_MAX_PIN > -1)
  1138. SERIAL_PROTOCOLPGM(MSG_Z_MAX);
  1139. SERIAL_PROTOCOLLN(((READ(Z_MAX_PIN)^Z_ENDSTOPS_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1140. #endif
  1141. break;
  1142. //TODO: update for all axis, use for loop
  1143. case 201: // M201
  1144. for(int8_t i=0; i < NUM_AXIS; i++)
  1145. {
  1146. if(code_seen(axis_codes[i]))
  1147. {
  1148. max_acceleration_units_per_sq_second[i] = code_value();
  1149. axis_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  1150. }
  1151. }
  1152. break;
  1153. #if 0 // Not used for Sprinter/grbl gen6
  1154. case 202: // M202
  1155. for(int8_t i=0; i < NUM_AXIS; i++) {
  1156. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  1157. }
  1158. break;
  1159. #endif
  1160. case 203: // M203 max feedrate mm/sec
  1161. for(int8_t i=0; i < NUM_AXIS; i++) {
  1162. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value();
  1163. }
  1164. break;
  1165. case 204: // M204 acclereration S normal moves T filmanent only moves
  1166. {
  1167. if(code_seen('S')) acceleration = code_value() ;
  1168. if(code_seen('T')) retract_acceleration = code_value() ;
  1169. }
  1170. break;
  1171. 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
  1172. {
  1173. if(code_seen('S')) minimumfeedrate = code_value();
  1174. if(code_seen('T')) mintravelfeedrate = code_value();
  1175. if(code_seen('B')) minsegmenttime = code_value() ;
  1176. if(code_seen('X')) max_xy_jerk = code_value() ;
  1177. if(code_seen('Z')) max_z_jerk = code_value() ;
  1178. if(code_seen('E')) max_e_jerk = code_value() ;
  1179. }
  1180. break;
  1181. case 206: // M206 additional homeing offset
  1182. for(int8_t i=0; i < 3; i++)
  1183. {
  1184. if(code_seen(axis_codes[i])) add_homeing[i] = code_value();
  1185. }
  1186. break;
  1187. #ifdef FWRETRACT
  1188. case 207: //M207 - set retract length S[positive mm] F[feedrate mm/sec] Z[additional zlift/hop]
  1189. {
  1190. if(code_seen('S'))
  1191. {
  1192. retract_length = code_value() ;
  1193. }
  1194. if(code_seen('F'))
  1195. {
  1196. retract_feedrate = code_value() ;
  1197. }
  1198. if(code_seen('Z'))
  1199. {
  1200. retract_zlift = code_value() ;
  1201. }
  1202. }break;
  1203. case 208: // M208 - set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  1204. {
  1205. if(code_seen('S'))
  1206. {
  1207. retract_recover_length = code_value() ;
  1208. }
  1209. if(code_seen('F'))
  1210. {
  1211. retract_recover_feedrate = code_value() ;
  1212. }
  1213. }break;
  1214. 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.
  1215. {
  1216. if(code_seen('S'))
  1217. {
  1218. int t= code_value() ;
  1219. switch(t)
  1220. {
  1221. case 0: autoretract_enabled=false;retracted=false;break;
  1222. case 1: autoretract_enabled=true;retracted=false;break;
  1223. default:
  1224. SERIAL_ECHO_START;
  1225. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  1226. SERIAL_ECHO(cmdbuffer[bufindr]);
  1227. SERIAL_ECHOLNPGM("\"");
  1228. }
  1229. }
  1230. }break;
  1231. #endif
  1232. case 220: // M220 S<factor in percent>- set speed factor override percentage
  1233. {
  1234. if(code_seen('S'))
  1235. {
  1236. feedmultiply = code_value() ;
  1237. feedmultiplychanged=true;
  1238. }
  1239. }
  1240. break;
  1241. case 221: // M221 S<factor in percent>- set extrude factor override percentage
  1242. {
  1243. if(code_seen('S'))
  1244. {
  1245. extrudemultiply = code_value() ;
  1246. }
  1247. }
  1248. break;
  1249. #ifdef PIDTEMP
  1250. case 301: // M301
  1251. {
  1252. if(code_seen('P')) Kp = code_value();
  1253. if(code_seen('I')) Ki = code_value()*PID_dT;
  1254. if(code_seen('D')) Kd = code_value()/PID_dT;
  1255. #ifdef PID_ADD_EXTRUSION_RATE
  1256. if(code_seen('C')) Kc = code_value();
  1257. #endif
  1258. updatePID();
  1259. SERIAL_PROTOCOL(MSG_OK);
  1260. SERIAL_PROTOCOL(" p:");
  1261. SERIAL_PROTOCOL(Kp);
  1262. SERIAL_PROTOCOL(" i:");
  1263. SERIAL_PROTOCOL(Ki/PID_dT);
  1264. SERIAL_PROTOCOL(" d:");
  1265. SERIAL_PROTOCOL(Kd*PID_dT);
  1266. #ifdef PID_ADD_EXTRUSION_RATE
  1267. SERIAL_PROTOCOL(" c:");
  1268. SERIAL_PROTOCOL(Kc*PID_dT);
  1269. #endif
  1270. SERIAL_PROTOCOLLN("");
  1271. }
  1272. break;
  1273. #endif //PIDTEMP
  1274. case 240: // M240 Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
  1275. {
  1276. #ifdef PHOTOGRAPH_PIN
  1277. #if (PHOTOGRAPH_PIN > -1)
  1278. const uint8_t NUM_PULSES=16;
  1279. const float PULSE_LENGTH=0.01524;
  1280. for(int i=0; i < NUM_PULSES; i++) {
  1281. WRITE(PHOTOGRAPH_PIN, HIGH);
  1282. _delay_ms(PULSE_LENGTH);
  1283. WRITE(PHOTOGRAPH_PIN, LOW);
  1284. _delay_ms(PULSE_LENGTH);
  1285. }
  1286. delay(7.33);
  1287. for(int i=0; i < NUM_PULSES; i++) {
  1288. WRITE(PHOTOGRAPH_PIN, HIGH);
  1289. _delay_ms(PULSE_LENGTH);
  1290. WRITE(PHOTOGRAPH_PIN, LOW);
  1291. _delay_ms(PULSE_LENGTH);
  1292. }
  1293. #endif
  1294. #endif
  1295. }
  1296. break;
  1297. case 302: // allow cold extrudes
  1298. {
  1299. allow_cold_extrudes(true);
  1300. }
  1301. break;
  1302. case 303: // M303 PID autotune
  1303. {
  1304. float temp = 150.0;
  1305. if (code_seen('S')) temp=code_value();
  1306. PID_autotune(temp);
  1307. }
  1308. break;
  1309. case 400: // M400 finish all moves
  1310. {
  1311. st_synchronize();
  1312. }
  1313. break;
  1314. case 500: // Store settings in EEPROM
  1315. {
  1316. EEPROM_StoreSettings();
  1317. }
  1318. break;
  1319. case 501: // Read settings from EEPROM
  1320. {
  1321. EEPROM_RetrieveSettings();
  1322. }
  1323. break;
  1324. case 502: // Revert to default settings
  1325. {
  1326. EEPROM_RetrieveSettings(true);
  1327. }
  1328. break;
  1329. case 503: // print settings currently in memory
  1330. {
  1331. EEPROM_printSettings();
  1332. }
  1333. break;
  1334. case 999: // Restart after being stopped
  1335. Stopped = false;
  1336. gcode_LastN = Stopped_gcode_LastN;
  1337. FlushSerialRequestResend();
  1338. break;
  1339. }
  1340. }
  1341. else if(code_seen('T'))
  1342. {
  1343. tmp_extruder = code_value();
  1344. if(tmp_extruder >= EXTRUDERS) {
  1345. SERIAL_ECHO_START;
  1346. SERIAL_ECHO("T");
  1347. SERIAL_ECHO(tmp_extruder);
  1348. SERIAL_ECHOLN(MSG_INVALID_EXTRUDER);
  1349. }
  1350. else {
  1351. active_extruder = tmp_extruder;
  1352. SERIAL_ECHO_START;
  1353. SERIAL_ECHO(MSG_ACTIVE_EXTRUDER);
  1354. SERIAL_PROTOCOLLN((int)active_extruder);
  1355. }
  1356. }
  1357. else
  1358. {
  1359. SERIAL_ECHO_START;
  1360. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  1361. SERIAL_ECHO(cmdbuffer[bufindr]);
  1362. SERIAL_ECHOLNPGM("\"");
  1363. }
  1364. ClearToSend();
  1365. }
  1366. void FlushSerialRequestResend()
  1367. {
  1368. //char cmdbuffer[bufindr][100]="Resend:";
  1369. MYSERIAL.flush();
  1370. SERIAL_PROTOCOLPGM(MSG_RESEND);
  1371. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  1372. ClearToSend();
  1373. }
  1374. void ClearToSend()
  1375. {
  1376. previous_millis_cmd = millis();
  1377. #ifdef SDSUPPORT
  1378. if(fromsd[bufindr])
  1379. return;
  1380. #endif //SDSUPPORT
  1381. SERIAL_PROTOCOLLNPGM(MSG_OK);
  1382. }
  1383. void get_coordinates()
  1384. {
  1385. bool seen[4]={false,false,false,false};
  1386. for(int8_t i=0; i < NUM_AXIS; i++) {
  1387. if(code_seen(axis_codes[i]))
  1388. {
  1389. destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
  1390. seen[i]=true;
  1391. }
  1392. else destination[i] = current_position[i]; //Are these else lines really needed?
  1393. }
  1394. if(code_seen('F')) {
  1395. next_feedrate = code_value();
  1396. if(next_feedrate > 0.0) feedrate = next_feedrate;
  1397. }
  1398. #ifdef FWRETRACT
  1399. if(autoretract_enabled)
  1400. if( !(seen[X_AXIS] || seen[Y_AXIS] || seen[Z_AXIS]) && seen[E_AXIS])
  1401. {
  1402. float echange=destination[E_AXIS]-current_position[E_AXIS];
  1403. if(echange<-MIN_RETRACT) //retract
  1404. {
  1405. if(!retracted)
  1406. {
  1407. destination[Z_AXIS]+=retract_zlift; //not sure why chaninging current_position negatively does not work.
  1408. //if slicer retracted by echange=-1mm and you want to retract 3mm, corrrectede=-2mm additionally
  1409. float correctede=-echange-retract_length;
  1410. //to generate the additional steps, not the destination is changed, but inversely the current position
  1411. current_position[E_AXIS]+=-correctede;
  1412. feedrate=retract_feedrate;
  1413. retracted=true;
  1414. }
  1415. }
  1416. else
  1417. if(echange>MIN_RETRACT) //retract_recover
  1418. {
  1419. if(retracted)
  1420. {
  1421. //current_position[Z_AXIS]+=-retract_zlift;
  1422. //if slicer retracted_recovered by echange=+1mm and you want to retract_recover 3mm, corrrectede=2mm additionally
  1423. float correctede=-echange+1*retract_length+retract_recover_length; //total unretract=retract_length+retract_recover_length[surplus]
  1424. current_position[E_AXIS]+=correctede; //to generate the additional steps, not the destination is changed, but inversely the current position
  1425. feedrate=retract_recover_feedrate;
  1426. retracted=false;
  1427. }
  1428. }
  1429. }
  1430. #endif //FWRETRACT
  1431. }
  1432. void get_arc_coordinates()
  1433. {
  1434. #ifdef SF_ARC_FIX
  1435. bool relative_mode_backup = relative_mode;
  1436. relative_mode = true;
  1437. #endif
  1438. get_coordinates();
  1439. #ifdef SF_ARC_FIX
  1440. relative_mode=relative_mode_backup;
  1441. #endif
  1442. if(code_seen('I')) {
  1443. offset[0] = code_value();
  1444. }
  1445. else {
  1446. offset[0] = 0.0;
  1447. }
  1448. if(code_seen('J')) {
  1449. offset[1] = code_value();
  1450. }
  1451. else {
  1452. offset[1] = 0.0;
  1453. }
  1454. }
  1455. void clamp_to_software_endstops(float target[3])
  1456. {
  1457. if (min_software_endstops) {
  1458. if (target[X_AXIS] < min_pos[X_AXIS]) target[X_AXIS] = min_pos[X_AXIS];
  1459. if (target[Y_AXIS] < min_pos[Y_AXIS]) target[Y_AXIS] = min_pos[Y_AXIS];
  1460. if (target[Z_AXIS] < min_pos[Z_AXIS]) target[Z_AXIS] = min_pos[Z_AXIS];
  1461. }
  1462. if (max_software_endstops) {
  1463. if (target[X_AXIS] > max_pos[X_AXIS]) target[X_AXIS] = max_pos[X_AXIS];
  1464. if (target[Y_AXIS] > max_pos[Y_AXIS]) target[Y_AXIS] = max_pos[Y_AXIS];
  1465. if (target[Z_AXIS] > max_pos[Z_AXIS]) target[Z_AXIS] = max_pos[Z_AXIS];
  1466. }
  1467. }
  1468. void prepare_move()
  1469. {
  1470. clamp_to_software_endstops(destination);
  1471. previous_millis_cmd = millis();
  1472. // Do not use feedmultiply for E or Z only moves
  1473. if( (current_position[X_AXIS] == destination [X_AXIS]) && (current_position[Y_AXIS] == destination [Y_AXIS])) {
  1474. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1475. }
  1476. else {
  1477. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
  1478. }
  1479. for(int8_t i=0; i < NUM_AXIS; i++) {
  1480. current_position[i] = destination[i];
  1481. }
  1482. }
  1483. void prepare_arc_move(char isclockwise) {
  1484. float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
  1485. // Trace the arc
  1486. mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
  1487. // As far as the parser is concerned, the position is now == target. In reality the
  1488. // motion control system might still be processing the action and the real tool position
  1489. // in any intermediate location.
  1490. for(int8_t i=0; i < NUM_AXIS; i++) {
  1491. current_position[i] = destination[i];
  1492. }
  1493. previous_millis_cmd = millis();
  1494. }
  1495. #ifdef CONTROLLERFAN_PIN
  1496. unsigned long lastMotor = 0; //Save the time for when a motor was turned on last
  1497. unsigned long lastMotorCheck = 0;
  1498. void controllerFan()
  1499. {
  1500. if ((millis() - lastMotorCheck) >= 2500) //Not a time critical function, so we only check every 2500ms
  1501. {
  1502. lastMotorCheck = millis();
  1503. if(!READ(X_ENABLE_PIN) || !READ(Y_ENABLE_PIN) || !READ(Z_ENABLE_PIN)
  1504. #if EXTRUDERS > 2
  1505. || !READ(E2_ENABLE_PIN)
  1506. #endif
  1507. #if EXTRUDER > 1
  1508. || !READ(E2_ENABLE_PIN)
  1509. #endif
  1510. || !READ(E0_ENABLE_PIN)) //If any of the drivers are enabled...
  1511. {
  1512. lastMotor = millis(); //... set time to NOW so the fan will turn on
  1513. }
  1514. if ((millis() - lastMotor) >= (CONTROLLERFAN_SEC*1000UL) || lastMotor == 0) //If the last time any driver was enabled, is longer since than CONTROLLERSEC...
  1515. {
  1516. WRITE(CONTROLLERFAN_PIN, LOW); //... turn the fan off
  1517. }
  1518. else
  1519. {
  1520. WRITE(CONTROLLERFAN_PIN, HIGH); //... turn the fan on
  1521. }
  1522. }
  1523. }
  1524. #endif
  1525. void manage_inactivity()
  1526. {
  1527. if( (millis() - previous_millis_cmd) > max_inactive_time )
  1528. if(max_inactive_time)
  1529. kill();
  1530. if(stepper_inactive_time) {
  1531. if( (millis() - previous_millis_cmd) > stepper_inactive_time )
  1532. {
  1533. if(blocks_queued() == false) {
  1534. disable_x();
  1535. disable_y();
  1536. disable_z();
  1537. disable_e0();
  1538. disable_e1();
  1539. disable_e2();
  1540. }
  1541. }
  1542. }
  1543. #if( KILL_PIN>-1 )
  1544. if( 0 == READ(KILL_PIN) )
  1545. kill();
  1546. #endif
  1547. #ifdef CONTROLLERFAN_PIN
  1548. controllerFan(); //Check if fan should be turned on to cool stepper drivers down
  1549. #endif
  1550. #ifdef EXTRUDER_RUNOUT_PREVENT
  1551. if( (millis() - previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 )
  1552. if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
  1553. {
  1554. bool oldstatus=READ(E0_ENABLE_PIN);
  1555. enable_e0();
  1556. float oldepos=current_position[E_AXIS];
  1557. float oldedes=destination[E_AXIS];
  1558. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  1559. current_position[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS],
  1560. EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
  1561. current_position[E_AXIS]=oldepos;
  1562. destination[E_AXIS]=oldedes;
  1563. plan_set_e_position(oldepos);
  1564. previous_millis_cmd=millis();
  1565. st_synchronize();
  1566. WRITE(E0_ENABLE_PIN,oldstatus);
  1567. }
  1568. #endif
  1569. check_axes_activity();
  1570. }
  1571. void kill()
  1572. {
  1573. cli(); // Stop interrupts
  1574. disable_heater();
  1575. disable_x();
  1576. disable_y();
  1577. disable_z();
  1578. disable_e0();
  1579. disable_e1();
  1580. disable_e2();
  1581. if(PS_ON_PIN > -1) pinMode(PS_ON_PIN,INPUT);
  1582. SERIAL_ERROR_START;
  1583. SERIAL_ERRORLNPGM(MSG_ERR_KILLED);
  1584. LCD_ALERTMESSAGEPGM(MSG_KILLED);
  1585. suicide();
  1586. while(1); // Wait for reset
  1587. }
  1588. void Stop()
  1589. {
  1590. disable_heater();
  1591. if(Stopped == false) {
  1592. Stopped = true;
  1593. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  1594. SERIAL_ERROR_START;
  1595. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  1596. LCD_MESSAGEPGM(MSG_STOPPED);
  1597. }
  1598. }
  1599. bool IsStopped() { return Stopped; };
  1600. #ifdef FAST_PWM_FAN
  1601. void setPwmFrequency(uint8_t pin, int val)
  1602. {
  1603. val &= 0x07;
  1604. switch(digitalPinToTimer(pin))
  1605. {
  1606. #if defined(TCCR0A)
  1607. case TIMER0A:
  1608. case TIMER0B:
  1609. // TCCR0B &= ~(CS00 | CS01 | CS02);
  1610. // TCCR0B |= val;
  1611. break;
  1612. #endif
  1613. #if defined(TCCR1A)
  1614. case TIMER1A:
  1615. case TIMER1B:
  1616. // TCCR1B &= ~(CS10 | CS11 | CS12);
  1617. // TCCR1B |= val;
  1618. break;
  1619. #endif
  1620. #if defined(TCCR2)
  1621. case TIMER2:
  1622. case TIMER2:
  1623. TCCR2 &= ~(CS10 | CS11 | CS12);
  1624. TCCR2 |= val;
  1625. break;
  1626. #endif
  1627. #if defined(TCCR2A)
  1628. case TIMER2A:
  1629. case TIMER2B:
  1630. TCCR2B &= ~(CS20 | CS21 | CS22);
  1631. TCCR2B |= val;
  1632. break;
  1633. #endif
  1634. #if defined(TCCR3A)
  1635. case TIMER3A:
  1636. case TIMER3B:
  1637. case TIMER3C:
  1638. TCCR3B &= ~(CS30 | CS31 | CS32);
  1639. TCCR3B |= val;
  1640. break;
  1641. #endif
  1642. #if defined(TCCR4A)
  1643. case TIMER4A:
  1644. case TIMER4B:
  1645. case TIMER4C:
  1646. TCCR4B &= ~(CS40 | CS41 | CS42);
  1647. TCCR4B |= val;
  1648. break;
  1649. #endif
  1650. #if defined(TCCR5A)
  1651. case TIMER5A:
  1652. case TIMER5B:
  1653. case TIMER5C:
  1654. TCCR5B &= ~(CS50 | CS51 | CS52);
  1655. TCCR5B |= val;
  1656. break;
  1657. #endif
  1658. }
  1659. }
  1660. #endif //FAST_PWM_FAN
  1661. bool setTargetedHotend(int code){
  1662. tmp_extruder = active_extruder;
  1663. if(code_seen('T')) {
  1664. tmp_extruder = code_value();
  1665. if(tmp_extruder >= EXTRUDERS) {
  1666. SERIAL_ECHO_START;
  1667. switch(code){
  1668. case 104:
  1669. SERIAL_ECHO(MSG_M104_INVALID_EXTRUDER);
  1670. break;
  1671. case 105:
  1672. SERIAL_ECHO(MSG_M105_INVALID_EXTRUDER);
  1673. break;
  1674. case 109:
  1675. SERIAL_ECHO(MSG_M109_INVALID_EXTRUDER);
  1676. break;
  1677. }
  1678. SERIAL_ECHOLN(tmp_extruder);
  1679. return true;
  1680. }
  1681. }
  1682. return false;
  1683. }