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 56KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861
  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. // M304 - Set bed PID parameters P I and D
  104. // M302 - Allow cold extrudes
  105. // M303 - PID relay autotune S<temperature> sets the target temperature. (default target temperature = 150C)
  106. // M400 - Finish all moves
  107. // M500 - stores paramters in EEPROM
  108. // M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
  109. // M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
  110. // M503 - print the current settings (from memory not from eeprom)
  111. // M999 - Restart after being stopped by error
  112. //Stepper Movement Variables
  113. //===========================================================================
  114. //=============================imported variables============================
  115. //===========================================================================
  116. //===========================================================================
  117. //=============================public variables=============================
  118. //===========================================================================
  119. #ifdef SDSUPPORT
  120. CardReader card;
  121. #endif
  122. float homing_feedrate[] = HOMING_FEEDRATE;
  123. bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
  124. volatile int feedmultiply=100; //100->1 200->2
  125. int saved_feedmultiply;
  126. volatile bool feedmultiplychanged=false;
  127. volatile int extrudemultiply=100; //100->1 200->2
  128. float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
  129. float add_homeing[3]={0,0,0};
  130. float min_pos[3] = { X_MIN_POS, Y_MIN_POS, Z_MIN_POS };
  131. float max_pos[3] = { X_MAX_POS, Y_MAX_POS, Z_MAX_POS };
  132. uint8_t active_extruder = 0;
  133. unsigned char FanSpeed=0;
  134. #ifdef FWRETRACT
  135. bool autoretract_enabled=true;
  136. bool retracted=false;
  137. float retract_length=3, retract_feedrate=17*60, retract_zlift=0.8;
  138. float retract_recover_length=0, retract_recover_feedrate=8*60;
  139. #endif
  140. //===========================================================================
  141. //=============================private variables=============================
  142. //===========================================================================
  143. const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
  144. static float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
  145. static float offset[3] = {0.0, 0.0, 0.0};
  146. static bool home_all_axis = true;
  147. static float feedrate = 1500.0, next_feedrate, saved_feedrate;
  148. static long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
  149. static bool relative_mode = false; //Determines Absolute or Relative Coordinates
  150. 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.
  151. static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
  152. static bool fromsd[BUFSIZE];
  153. static int bufindr = 0;
  154. static int bufindw = 0;
  155. static int buflen = 0;
  156. //static int i = 0;
  157. static char serial_char;
  158. static int serial_count = 0;
  159. static boolean comment_mode = false;
  160. static char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
  161. const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
  162. //static float tt = 0;
  163. //static float bt = 0;
  164. //Inactivity shutdown variables
  165. static unsigned long previous_millis_cmd = 0;
  166. static unsigned long max_inactive_time = 0;
  167. static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000l;
  168. static unsigned long starttime=0;
  169. static unsigned long stoptime=0;
  170. static uint8_t tmp_extruder;
  171. bool Stopped=false;
  172. //===========================================================================
  173. //=============================ROUTINES=============================
  174. //===========================================================================
  175. void get_arc_coordinates();
  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. tmp_extruder = active_extruder;
  861. if(code_seen('T')) {
  862. tmp_extruder = code_value();
  863. if(tmp_extruder >= EXTRUDERS) {
  864. SERIAL_ECHO_START;
  865. SERIAL_ECHO(MSG_M104_INVALID_EXTRUDER);
  866. SERIAL_ECHOLN(tmp_extruder);
  867. break;
  868. }
  869. }
  870. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  871. setWatch();
  872. break;
  873. case 140: // M140 set bed temp
  874. if (code_seen('S')) setTargetBed(code_value());
  875. break;
  876. case 105 : // M105
  877. tmp_extruder = active_extruder;
  878. if(code_seen('T')) {
  879. tmp_extruder = code_value();
  880. if(tmp_extruder >= EXTRUDERS) {
  881. SERIAL_ECHO_START;
  882. SERIAL_ECHO(MSG_M105_INVALID_EXTRUDER);
  883. SERIAL_ECHOLN(tmp_extruder);
  884. break;
  885. }
  886. }
  887. #if (TEMP_0_PIN > -1)
  888. SERIAL_PROTOCOLPGM("ok T:");
  889. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  890. SERIAL_PROTOCOLPGM(" /");
  891. SERIAL_PROTOCOL_F(degTargetHotend(tmp_extruder),1);
  892. #if TEMP_BED_PIN > -1
  893. SERIAL_PROTOCOLPGM(" B:");
  894. SERIAL_PROTOCOL_F(degBed(),1);
  895. SERIAL_PROTOCOLPGM(" /");
  896. SERIAL_PROTOCOL_F(degTargetBed(),1);
  897. #endif //TEMP_BED_PIN
  898. #else
  899. SERIAL_ERROR_START;
  900. SERIAL_ERRORLNPGM(MSG_ERR_NO_THERMISTORS);
  901. #endif
  902. #ifdef PIDTEMP
  903. SERIAL_PROTOCOLPGM(" @:");
  904. SERIAL_PROTOCOL(getHeaterPower(tmp_extruder));
  905. #endif
  906. #ifdef PIDTEMPBED
  907. SERIAL_PROTOCOLPGM(" B@:");
  908. SERIAL_PROTOCOL(getHeaterPower(-1));
  909. #endif
  910. SERIAL_PROTOCOLLN("");
  911. return;
  912. break;
  913. case 109:
  914. {// M109 - Wait for extruder heater to reach target.
  915. tmp_extruder = active_extruder;
  916. if(code_seen('T')) {
  917. tmp_extruder = code_value();
  918. if(tmp_extruder >= EXTRUDERS) {
  919. SERIAL_ECHO_START;
  920. SERIAL_ECHO(MSG_M109_INVALID_EXTRUDER);
  921. SERIAL_ECHOLN(tmp_extruder);
  922. break;
  923. }
  924. }
  925. LCD_MESSAGEPGM(MSG_HEATING);
  926. #ifdef AUTOTEMP
  927. autotemp_enabled=false;
  928. #endif
  929. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  930. #ifdef AUTOTEMP
  931. if (code_seen('S')) autotemp_min=code_value();
  932. if (code_seen('B')) autotemp_max=code_value();
  933. if (code_seen('F'))
  934. {
  935. autotemp_factor=code_value();
  936. autotemp_enabled=true;
  937. }
  938. #endif
  939. setWatch();
  940. codenum = millis();
  941. /* See if we are heating up or cooling down */
  942. bool target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling
  943. #ifdef TEMP_RESIDENCY_TIME
  944. long residencyStart;
  945. residencyStart = -1;
  946. /* continue to loop until we have reached the target temp
  947. _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
  948. while((residencyStart == -1) ||
  949. (residencyStart >= 0 && (((unsigned int) (millis() - residencyStart)) < (TEMP_RESIDENCY_TIME * 1000UL))) ) {
  950. #else
  951. while ( target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder)&&(CooldownNoWait==false)) ) {
  952. #endif //TEMP_RESIDENCY_TIME
  953. if( (millis() - codenum) > 1000UL )
  954. { //Print Temp Reading and remaining time every 1 second while heating up/cooling down
  955. SERIAL_PROTOCOLPGM("T:");
  956. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  957. SERIAL_PROTOCOLPGM(" E:");
  958. SERIAL_PROTOCOL((int)tmp_extruder);
  959. #ifdef TEMP_RESIDENCY_TIME
  960. SERIAL_PROTOCOLPGM(" W:");
  961. if(residencyStart > -1)
  962. {
  963. codenum = ((TEMP_RESIDENCY_TIME * 1000UL) - (millis() - residencyStart)) / 1000UL;
  964. SERIAL_PROTOCOLLN( codenum );
  965. }
  966. else
  967. {
  968. SERIAL_PROTOCOLLN( "?" );
  969. }
  970. #else
  971. SERIAL_PROTOCOLLN("");
  972. #endif
  973. codenum = millis();
  974. }
  975. manage_heater();
  976. manage_inactivity();
  977. LCD_STATUS;
  978. #ifdef TEMP_RESIDENCY_TIME
  979. /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
  980. or when current temp falls outside the hysteresis after target temp was reached */
  981. if ((residencyStart == -1 && target_direction && (degHotend(tmp_extruder) >= (degTargetHotend(tmp_extruder)-TEMP_WINDOW))) ||
  982. (residencyStart == -1 && !target_direction && (degHotend(tmp_extruder) <= (degTargetHotend(tmp_extruder)+TEMP_WINDOW))) ||
  983. (residencyStart > -1 && labs(degHotend(tmp_extruder) - degTargetHotend(tmp_extruder)) > TEMP_HYSTERESIS) )
  984. {
  985. residencyStart = millis();
  986. }
  987. #endif //TEMP_RESIDENCY_TIME
  988. }
  989. LCD_MESSAGEPGM(MSG_HEATING_COMPLETE);
  990. starttime=millis();
  991. previous_millis_cmd = millis();
  992. }
  993. break;
  994. case 190: // M190 - Wait for bed heater to reach target.
  995. #if TEMP_BED_PIN > -1
  996. LCD_MESSAGEPGM(MSG_BED_HEATING);
  997. if (code_seen('S')) setTargetBed(code_value());
  998. codenum = millis();
  999. while(isHeatingBed())
  1000. {
  1001. if(( millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
  1002. {
  1003. float tt=degHotend(active_extruder);
  1004. SERIAL_PROTOCOLPGM("T:");
  1005. SERIAL_PROTOCOL(tt);
  1006. SERIAL_PROTOCOLPGM(" E:");
  1007. SERIAL_PROTOCOL((int)active_extruder);
  1008. SERIAL_PROTOCOLPGM(" B:");
  1009. SERIAL_PROTOCOL_F(degBed(),1);
  1010. SERIAL_PROTOCOLLN("");
  1011. codenum = millis();
  1012. }
  1013. manage_heater();
  1014. manage_inactivity();
  1015. LCD_STATUS;
  1016. }
  1017. LCD_MESSAGEPGM(MSG_BED_DONE);
  1018. previous_millis_cmd = millis();
  1019. #endif
  1020. break;
  1021. #if FAN_PIN > -1
  1022. case 106: //M106 Fan On
  1023. if (code_seen('S')){
  1024. FanSpeed=constrain(code_value(),0,255);
  1025. }
  1026. else {
  1027. FanSpeed=255;
  1028. }
  1029. break;
  1030. case 107: //M107 Fan Off
  1031. FanSpeed = 0;
  1032. break;
  1033. #endif //FAN_PIN
  1034. #if (PS_ON_PIN > -1)
  1035. case 80: // M80 - ATX Power On
  1036. SET_OUTPUT(PS_ON_PIN); //GND
  1037. WRITE(PS_ON_PIN, LOW);
  1038. break;
  1039. #endif
  1040. case 81: // M81 - ATX Power Off
  1041. #if defined SUICIDE_PIN && SUICIDE_PIN > -1
  1042. st_synchronize();
  1043. suicide();
  1044. #elif (PS_ON_PIN > -1)
  1045. SET_INPUT(PS_ON_PIN); //Floating
  1046. #endif
  1047. break;
  1048. case 82:
  1049. axis_relative_modes[3] = false;
  1050. break;
  1051. case 83:
  1052. axis_relative_modes[3] = true;
  1053. break;
  1054. case 18: //compatibility
  1055. case 84: // M84
  1056. if(code_seen('S')){
  1057. stepper_inactive_time = code_value() * 1000;
  1058. }
  1059. else
  1060. {
  1061. bool all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2]))|| (code_seen(axis_codes[3])));
  1062. if(all_axis)
  1063. {
  1064. st_synchronize();
  1065. disable_e0();
  1066. disable_e1();
  1067. disable_e2();
  1068. finishAndDisableSteppers();
  1069. }
  1070. else
  1071. {
  1072. st_synchronize();
  1073. if(code_seen('X')) disable_x();
  1074. if(code_seen('Y')) disable_y();
  1075. if(code_seen('Z')) disable_z();
  1076. #if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
  1077. if(code_seen('E')) {
  1078. disable_e0();
  1079. disable_e1();
  1080. disable_e2();
  1081. }
  1082. #endif
  1083. LCD_MESSAGEPGM(MSG_PART_RELEASE);
  1084. }
  1085. }
  1086. break;
  1087. case 85: // M85
  1088. code_seen('S');
  1089. max_inactive_time = code_value() * 1000;
  1090. break;
  1091. case 92: // M92
  1092. for(int8_t i=0; i < NUM_AXIS; i++)
  1093. {
  1094. if(code_seen(axis_codes[i]))
  1095. if(i == 3) { // E
  1096. float value = code_value();
  1097. if(value < 20.0) {
  1098. float factor = axis_steps_per_unit[i] / value; // increase e constants if M92 E14 is given for netfab.
  1099. max_e_jerk *= factor;
  1100. max_feedrate[i] *= factor;
  1101. axis_steps_per_sqr_second[i] *= factor;
  1102. }
  1103. axis_steps_per_unit[i] = value;
  1104. }
  1105. else {
  1106. axis_steps_per_unit[i] = code_value();
  1107. }
  1108. }
  1109. break;
  1110. case 115: // M115
  1111. SerialprintPGM(MSG_M115_REPORT);
  1112. break;
  1113. case 117: // M117 display message
  1114. LCD_MESSAGE(cmdbuffer[bufindr]+5);
  1115. break;
  1116. case 114: // M114
  1117. SERIAL_PROTOCOLPGM("X:");
  1118. SERIAL_PROTOCOL(current_position[X_AXIS]);
  1119. SERIAL_PROTOCOLPGM("Y:");
  1120. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  1121. SERIAL_PROTOCOLPGM("Z:");
  1122. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  1123. SERIAL_PROTOCOLPGM("E:");
  1124. SERIAL_PROTOCOL(current_position[E_AXIS]);
  1125. SERIAL_PROTOCOLPGM(MSG_COUNT_X);
  1126. SERIAL_PROTOCOL(float(st_get_position(X_AXIS))/axis_steps_per_unit[X_AXIS]);
  1127. SERIAL_PROTOCOLPGM("Y:");
  1128. SERIAL_PROTOCOL(float(st_get_position(Y_AXIS))/axis_steps_per_unit[Y_AXIS]);
  1129. SERIAL_PROTOCOLPGM("Z:");
  1130. SERIAL_PROTOCOL(float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]);
  1131. SERIAL_PROTOCOLLN("");
  1132. break;
  1133. case 120: // M120
  1134. enable_endstops(false) ;
  1135. break;
  1136. case 121: // M121
  1137. enable_endstops(true) ;
  1138. break;
  1139. case 119: // M119
  1140. #if (X_MIN_PIN > -1)
  1141. SERIAL_PROTOCOLPGM(MSG_X_MIN);
  1142. SERIAL_PROTOCOL(((READ(X_MIN_PIN)^X_ENDSTOPS_INVERTING)?"H ":"L "));
  1143. #endif
  1144. #if (X_MAX_PIN > -1)
  1145. SERIAL_PROTOCOLPGM(MSG_X_MAX);
  1146. SERIAL_PROTOCOL(((READ(X_MAX_PIN)^X_ENDSTOPS_INVERTING)?"H ":"L "));
  1147. #endif
  1148. #if (Y_MIN_PIN > -1)
  1149. SERIAL_PROTOCOLPGM(MSG_Y_MIN);
  1150. SERIAL_PROTOCOL(((READ(Y_MIN_PIN)^Y_ENDSTOPS_INVERTING)?"H ":"L "));
  1151. #endif
  1152. #if (Y_MAX_PIN > -1)
  1153. SERIAL_PROTOCOLPGM(MSG_Y_MAX);
  1154. SERIAL_PROTOCOL(((READ(Y_MAX_PIN)^Y_ENDSTOPS_INVERTING)?"H ":"L "));
  1155. #endif
  1156. #if (Z_MIN_PIN > -1)
  1157. SERIAL_PROTOCOLPGM(MSG_Z_MIN);
  1158. SERIAL_PROTOCOL(((READ(Z_MIN_PIN)^Z_ENDSTOPS_INVERTING)?"H ":"L "));
  1159. #endif
  1160. #if (Z_MAX_PIN > -1)
  1161. SERIAL_PROTOCOLPGM(MSG_Z_MAX);
  1162. SERIAL_PROTOCOL(((READ(Z_MAX_PIN)^Z_ENDSTOPS_INVERTING)?"H ":"L "));
  1163. #endif
  1164. SERIAL_PROTOCOLLN("");
  1165. break;
  1166. //TODO: update for all axis, use for loop
  1167. case 201: // M201
  1168. for(int8_t i=0; i < NUM_AXIS; i++)
  1169. {
  1170. if(code_seen(axis_codes[i]))
  1171. {
  1172. max_acceleration_units_per_sq_second[i] = code_value();
  1173. axis_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  1174. }
  1175. }
  1176. break;
  1177. #if 0 // Not used for Sprinter/grbl gen6
  1178. case 202: // M202
  1179. for(int8_t i=0; i < NUM_AXIS; i++) {
  1180. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  1181. }
  1182. break;
  1183. #endif
  1184. case 203: // M203 max feedrate mm/sec
  1185. for(int8_t i=0; i < NUM_AXIS; i++) {
  1186. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value();
  1187. }
  1188. break;
  1189. case 204: // M204 acclereration S normal moves T filmanent only moves
  1190. {
  1191. if(code_seen('S')) acceleration = code_value() ;
  1192. if(code_seen('T')) retract_acceleration = code_value() ;
  1193. }
  1194. break;
  1195. 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
  1196. {
  1197. if(code_seen('S')) minimumfeedrate = code_value();
  1198. if(code_seen('T')) mintravelfeedrate = code_value();
  1199. if(code_seen('B')) minsegmenttime = code_value() ;
  1200. if(code_seen('X')) max_xy_jerk = code_value() ;
  1201. if(code_seen('Z')) max_z_jerk = code_value() ;
  1202. if(code_seen('E')) max_e_jerk = code_value() ;
  1203. }
  1204. break;
  1205. case 206: // M206 additional homeing offset
  1206. for(int8_t i=0; i < 3; i++)
  1207. {
  1208. if(code_seen(axis_codes[i])) add_homeing[i] = code_value();
  1209. }
  1210. break;
  1211. #ifdef FWRETRACT
  1212. case 207: //M207 - set retract length S[positive mm] F[feedrate mm/sec] Z[additional zlift/hop]
  1213. {
  1214. if(code_seen('S'))
  1215. {
  1216. retract_length = code_value() ;
  1217. }
  1218. if(code_seen('F'))
  1219. {
  1220. retract_feedrate = code_value() ;
  1221. }
  1222. if(code_seen('Z'))
  1223. {
  1224. retract_zlift = code_value() ;
  1225. }
  1226. }break;
  1227. case 208: // M208 - set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  1228. {
  1229. if(code_seen('S'))
  1230. {
  1231. retract_recover_length = code_value() ;
  1232. }
  1233. if(code_seen('F'))
  1234. {
  1235. retract_recover_feedrate = code_value() ;
  1236. }
  1237. }break;
  1238. 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.
  1239. {
  1240. if(code_seen('S'))
  1241. {
  1242. int t= code_value() ;
  1243. switch(t)
  1244. {
  1245. case 0: autoretract_enabled=false;retracted=false;break;
  1246. case 1: autoretract_enabled=true;retracted=false;break;
  1247. default:
  1248. SERIAL_ECHO_START;
  1249. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  1250. SERIAL_ECHO(cmdbuffer[bufindr]);
  1251. SERIAL_ECHOLNPGM("\"");
  1252. }
  1253. }
  1254. }break;
  1255. #endif
  1256. case 220: // M220 S<factor in percent>- set speed factor override percentage
  1257. {
  1258. if(code_seen('S'))
  1259. {
  1260. feedmultiply = code_value() ;
  1261. feedmultiplychanged=true;
  1262. }
  1263. }
  1264. break;
  1265. case 221: // M221 S<factor in percent>- set extrude factor override percentage
  1266. {
  1267. if(code_seen('S'))
  1268. {
  1269. extrudemultiply = code_value() ;
  1270. }
  1271. }
  1272. break;
  1273. #ifdef PIDTEMP
  1274. case 301: // M301
  1275. {
  1276. if(code_seen('P')) Kp = code_value();
  1277. if(code_seen('I')) Ki = code_value()*PID_dT;
  1278. if(code_seen('D')) Kd = code_value()/PID_dT;
  1279. #ifdef PID_ADD_EXTRUSION_RATE
  1280. if(code_seen('C')) Kc = code_value();
  1281. #endif
  1282. updatePID();
  1283. SERIAL_PROTOCOL(MSG_OK);
  1284. SERIAL_PROTOCOL(" p:");
  1285. SERIAL_PROTOCOL(Kp);
  1286. SERIAL_PROTOCOL(" i:");
  1287. SERIAL_PROTOCOL(Ki/PID_dT);
  1288. SERIAL_PROTOCOL(" d:");
  1289. SERIAL_PROTOCOL(Kd*PID_dT);
  1290. #ifdef PID_ADD_EXTRUSION_RATE
  1291. SERIAL_PROTOCOL(" c:");
  1292. SERIAL_PROTOCOL(Kc*PID_dT);
  1293. #endif
  1294. SERIAL_PROTOCOLLN("");
  1295. }
  1296. break;
  1297. #endif //PIDTEMP
  1298. #ifdef PIDTEMPBED
  1299. case 304: // M304
  1300. {
  1301. if(code_seen('P')) bedKp = code_value();
  1302. if(code_seen('I')) bedKi = code_value()*PID_dT;
  1303. if(code_seen('D')) bedKd = code_value()/PID_dT;
  1304. updatePID();
  1305. SERIAL_PROTOCOL(MSG_OK);
  1306. SERIAL_PROTOCOL(" p:");
  1307. SERIAL_PROTOCOL(Kp);
  1308. SERIAL_PROTOCOL(" i:");
  1309. SERIAL_PROTOCOL(Ki/PID_dT);
  1310. SERIAL_PROTOCOL(" d:");
  1311. SERIAL_PROTOCOL(Kd*PID_dT);
  1312. SERIAL_PROTOCOLLN("");
  1313. }
  1314. break;
  1315. #endif //PIDTEMP
  1316. case 240: // M240 Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
  1317. {
  1318. #ifdef PHOTOGRAPH_PIN
  1319. #if (PHOTOGRAPH_PIN > -1)
  1320. const uint8_t NUM_PULSES=16;
  1321. const float PULSE_LENGTH=0.01524;
  1322. for(int i=0; i < NUM_PULSES; i++) {
  1323. WRITE(PHOTOGRAPH_PIN, HIGH);
  1324. _delay_ms(PULSE_LENGTH);
  1325. WRITE(PHOTOGRAPH_PIN, LOW);
  1326. _delay_ms(PULSE_LENGTH);
  1327. }
  1328. delay(7.33);
  1329. for(int i=0; i < NUM_PULSES; i++) {
  1330. WRITE(PHOTOGRAPH_PIN, HIGH);
  1331. _delay_ms(PULSE_LENGTH);
  1332. WRITE(PHOTOGRAPH_PIN, LOW);
  1333. _delay_ms(PULSE_LENGTH);
  1334. }
  1335. #endif
  1336. #endif
  1337. }
  1338. break;
  1339. case 302: // allow cold extrudes
  1340. {
  1341. allow_cold_extrudes(true);
  1342. }
  1343. break;
  1344. case 303: // M303 PID autotune
  1345. {
  1346. float temp = 150.0;
  1347. int e=0;
  1348. int c=5;
  1349. if (code_seen('E')) e=code_value();
  1350. if (e<0)
  1351. temp=70;
  1352. if (code_seen('S')) temp=code_value();
  1353. if (code_seen('C')) c=code_value();
  1354. PID_autotune(temp, e, c);
  1355. }
  1356. break;
  1357. case 400: // M400 finish all moves
  1358. {
  1359. st_synchronize();
  1360. }
  1361. break;
  1362. case 500: // Store settings in EEPROM
  1363. {
  1364. EEPROM_StoreSettings();
  1365. }
  1366. break;
  1367. case 501: // Read settings from EEPROM
  1368. {
  1369. EEPROM_RetrieveSettings();
  1370. }
  1371. break;
  1372. case 502: // Revert to default settings
  1373. {
  1374. EEPROM_RetrieveSettings(true);
  1375. }
  1376. break;
  1377. case 503: // print settings currently in memory
  1378. {
  1379. EEPROM_printSettings();
  1380. }
  1381. break;
  1382. case 999: // Restart after being stopped
  1383. Stopped = false;
  1384. gcode_LastN = Stopped_gcode_LastN;
  1385. FlushSerialRequestResend();
  1386. break;
  1387. }
  1388. }
  1389. else if(code_seen('T'))
  1390. {
  1391. tmp_extruder = code_value();
  1392. if(tmp_extruder >= EXTRUDERS) {
  1393. SERIAL_ECHO_START;
  1394. SERIAL_ECHO("T");
  1395. SERIAL_ECHO(tmp_extruder);
  1396. SERIAL_ECHOLN(MSG_INVALID_EXTRUDER);
  1397. }
  1398. else {
  1399. active_extruder = tmp_extruder;
  1400. SERIAL_ECHO_START;
  1401. SERIAL_ECHO(MSG_ACTIVE_EXTRUDER);
  1402. SERIAL_PROTOCOLLN((int)active_extruder);
  1403. }
  1404. }
  1405. else
  1406. {
  1407. SERIAL_ECHO_START;
  1408. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  1409. SERIAL_ECHO(cmdbuffer[bufindr]);
  1410. SERIAL_ECHOLNPGM("\"");
  1411. }
  1412. ClearToSend();
  1413. }
  1414. void FlushSerialRequestResend()
  1415. {
  1416. //char cmdbuffer[bufindr][100]="Resend:";
  1417. MYSERIAL.flush();
  1418. SERIAL_PROTOCOLPGM(MSG_RESEND);
  1419. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  1420. ClearToSend();
  1421. }
  1422. void ClearToSend()
  1423. {
  1424. previous_millis_cmd = millis();
  1425. #ifdef SDSUPPORT
  1426. if(fromsd[bufindr])
  1427. return;
  1428. #endif //SDSUPPORT
  1429. SERIAL_PROTOCOLLNPGM(MSG_OK);
  1430. }
  1431. void get_coordinates()
  1432. {
  1433. bool seen[4]={false,false,false,false};
  1434. for(int8_t i=0; i < NUM_AXIS; i++) {
  1435. if(code_seen(axis_codes[i]))
  1436. {
  1437. destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
  1438. seen[i]=true;
  1439. }
  1440. else destination[i] = current_position[i]; //Are these else lines really needed?
  1441. }
  1442. if(code_seen('F')) {
  1443. next_feedrate = code_value();
  1444. if(next_feedrate > 0.0) feedrate = next_feedrate;
  1445. }
  1446. #ifdef FWRETRACT
  1447. if(autoretract_enabled)
  1448. if( !(seen[X_AXIS] || seen[Y_AXIS] || seen[Z_AXIS]) && seen[E_AXIS])
  1449. {
  1450. float echange=destination[E_AXIS]-current_position[E_AXIS];
  1451. if(echange<-MIN_RETRACT) //retract
  1452. {
  1453. if(!retracted)
  1454. {
  1455. destination[Z_AXIS]+=retract_zlift; //not sure why chaninging current_position negatively does not work.
  1456. //if slicer retracted by echange=-1mm and you want to retract 3mm, corrrectede=-2mm additionally
  1457. float correctede=-echange-retract_length;
  1458. //to generate the additional steps, not the destination is changed, but inversely the current position
  1459. current_position[E_AXIS]+=-correctede;
  1460. feedrate=retract_feedrate;
  1461. retracted=true;
  1462. }
  1463. }
  1464. else
  1465. if(echange>MIN_RETRACT) //retract_recover
  1466. {
  1467. if(retracted)
  1468. {
  1469. //current_position[Z_AXIS]+=-retract_zlift;
  1470. //if slicer retracted_recovered by echange=+1mm and you want to retract_recover 3mm, corrrectede=2mm additionally
  1471. float correctede=-echange+1*retract_length+retract_recover_length; //total unretract=retract_length+retract_recover_length[surplus]
  1472. current_position[E_AXIS]+=correctede; //to generate the additional steps, not the destination is changed, but inversely the current position
  1473. feedrate=retract_recover_feedrate;
  1474. retracted=false;
  1475. }
  1476. }
  1477. }
  1478. #endif //FWRETRACT
  1479. }
  1480. void get_arc_coordinates()
  1481. {
  1482. #ifdef SF_ARC_FIX
  1483. bool relative_mode_backup = relative_mode;
  1484. relative_mode = true;
  1485. #endif
  1486. get_coordinates();
  1487. #ifdef SF_ARC_FIX
  1488. relative_mode=relative_mode_backup;
  1489. #endif
  1490. if(code_seen('I')) {
  1491. offset[0] = code_value();
  1492. }
  1493. else {
  1494. offset[0] = 0.0;
  1495. }
  1496. if(code_seen('J')) {
  1497. offset[1] = code_value();
  1498. }
  1499. else {
  1500. offset[1] = 0.0;
  1501. }
  1502. }
  1503. void clamp_to_software_endstops(float target[3])
  1504. {
  1505. if (min_software_endstops) {
  1506. if (target[X_AXIS] < min_pos[X_AXIS]) target[X_AXIS] = min_pos[X_AXIS];
  1507. if (target[Y_AXIS] < min_pos[Y_AXIS]) target[Y_AXIS] = min_pos[Y_AXIS];
  1508. if (target[Z_AXIS] < min_pos[Z_AXIS]) target[Z_AXIS] = min_pos[Z_AXIS];
  1509. }
  1510. if (max_software_endstops) {
  1511. if (target[X_AXIS] > max_pos[X_AXIS]) target[X_AXIS] = max_pos[X_AXIS];
  1512. if (target[Y_AXIS] > max_pos[Y_AXIS]) target[Y_AXIS] = max_pos[Y_AXIS];
  1513. if (target[Z_AXIS] > max_pos[Z_AXIS]) target[Z_AXIS] = max_pos[Z_AXIS];
  1514. }
  1515. }
  1516. void prepare_move()
  1517. {
  1518. clamp_to_software_endstops(destination);
  1519. previous_millis_cmd = millis();
  1520. // Do not use feedmultiply for E or Z only moves
  1521. if( (current_position[X_AXIS] == destination [X_AXIS]) && (current_position[Y_AXIS] == destination [Y_AXIS])) {
  1522. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1523. }
  1524. else {
  1525. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
  1526. }
  1527. for(int8_t i=0; i < NUM_AXIS; i++) {
  1528. current_position[i] = destination[i];
  1529. }
  1530. }
  1531. void prepare_arc_move(char isclockwise) {
  1532. float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
  1533. // Trace the arc
  1534. mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
  1535. // As far as the parser is concerned, the position is now == target. In reality the
  1536. // motion control system might still be processing the action and the real tool position
  1537. // in any intermediate location.
  1538. for(int8_t i=0; i < NUM_AXIS; i++) {
  1539. current_position[i] = destination[i];
  1540. }
  1541. previous_millis_cmd = millis();
  1542. }
  1543. #ifdef CONTROLLERFAN_PIN
  1544. unsigned long lastMotor = 0; //Save the time for when a motor was turned on last
  1545. unsigned long lastMotorCheck = 0;
  1546. void controllerFan()
  1547. {
  1548. if ((millis() - lastMotorCheck) >= 2500) //Not a time critical function, so we only check every 2500ms
  1549. {
  1550. lastMotorCheck = millis();
  1551. if(!READ(X_ENABLE_PIN) || !READ(Y_ENABLE_PIN) || !READ(Z_ENABLE_PIN)
  1552. #if EXTRUDERS > 2
  1553. || !READ(E2_ENABLE_PIN)
  1554. #endif
  1555. #if EXTRUDER > 1
  1556. || !READ(E2_ENABLE_PIN)
  1557. #endif
  1558. || !READ(E0_ENABLE_PIN)) //If any of the drivers are enabled...
  1559. {
  1560. lastMotor = millis(); //... set time to NOW so the fan will turn on
  1561. }
  1562. if ((millis() - lastMotor) >= (CONTROLLERFAN_SEC*1000UL) || lastMotor == 0) //If the last time any driver was enabled, is longer since than CONTROLLERSEC...
  1563. {
  1564. WRITE(CONTROLLERFAN_PIN, LOW); //... turn the fan off
  1565. }
  1566. else
  1567. {
  1568. WRITE(CONTROLLERFAN_PIN, HIGH); //... turn the fan on
  1569. }
  1570. }
  1571. }
  1572. #endif
  1573. void manage_inactivity()
  1574. {
  1575. if( (millis() - previous_millis_cmd) > max_inactive_time )
  1576. if(max_inactive_time)
  1577. kill();
  1578. if(stepper_inactive_time) {
  1579. if( (millis() - previous_millis_cmd) > stepper_inactive_time )
  1580. {
  1581. if(blocks_queued() == false) {
  1582. disable_x();
  1583. disable_y();
  1584. disable_z();
  1585. disable_e0();
  1586. disable_e1();
  1587. disable_e2();
  1588. }
  1589. }
  1590. }
  1591. #if( KILL_PIN>-1 )
  1592. if( 0 == READ(KILL_PIN) )
  1593. kill();
  1594. #endif
  1595. #ifdef CONTROLLERFAN_PIN
  1596. controllerFan(); //Check if fan should be turned on to cool stepper drivers down
  1597. #endif
  1598. #ifdef EXTRUDER_RUNOUT_PREVENT
  1599. if( (millis() - previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 )
  1600. if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
  1601. {
  1602. bool oldstatus=READ(E0_ENABLE_PIN);
  1603. enable_e0();
  1604. float oldepos=current_position[E_AXIS];
  1605. float oldedes=destination[E_AXIS];
  1606. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  1607. current_position[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS],
  1608. EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
  1609. current_position[E_AXIS]=oldepos;
  1610. destination[E_AXIS]=oldedes;
  1611. plan_set_e_position(oldepos);
  1612. previous_millis_cmd=millis();
  1613. st_synchronize();
  1614. WRITE(E0_ENABLE_PIN,oldstatus);
  1615. }
  1616. #endif
  1617. check_axes_activity();
  1618. }
  1619. void kill()
  1620. {
  1621. cli(); // Stop interrupts
  1622. disable_heater();
  1623. disable_x();
  1624. disable_y();
  1625. disable_z();
  1626. disable_e0();
  1627. disable_e1();
  1628. disable_e2();
  1629. if(PS_ON_PIN > -1) pinMode(PS_ON_PIN,INPUT);
  1630. SERIAL_ERROR_START;
  1631. SERIAL_ERRORLNPGM(MSG_ERR_KILLED);
  1632. LCD_ALERTMESSAGEPGM(MSG_KILLED);
  1633. suicide();
  1634. while(1); // Wait for reset
  1635. }
  1636. void Stop()
  1637. {
  1638. disable_heater();
  1639. if(Stopped == false) {
  1640. Stopped = true;
  1641. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  1642. SERIAL_ERROR_START;
  1643. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  1644. LCD_MESSAGEPGM(MSG_STOPPED);
  1645. }
  1646. }
  1647. bool IsStopped() { return Stopped; };
  1648. #ifdef FAST_PWM_FAN
  1649. void setPwmFrequency(uint8_t pin, int val)
  1650. {
  1651. val &= 0x07;
  1652. switch(digitalPinToTimer(pin))
  1653. {
  1654. #if defined(TCCR0A)
  1655. case TIMER0A:
  1656. case TIMER0B:
  1657. // TCCR0B &= ~(CS00 | CS01 | CS02);
  1658. // TCCR0B |= val;
  1659. break;
  1660. #endif
  1661. #if defined(TCCR1A)
  1662. case TIMER1A:
  1663. case TIMER1B:
  1664. // TCCR1B &= ~(CS10 | CS11 | CS12);
  1665. // TCCR1B |= val;
  1666. break;
  1667. #endif
  1668. #if defined(TCCR2)
  1669. case TIMER2:
  1670. case TIMER2:
  1671. TCCR2 &= ~(CS10 | CS11 | CS12);
  1672. TCCR2 |= val;
  1673. break;
  1674. #endif
  1675. #if defined(TCCR2A)
  1676. case TIMER2A:
  1677. case TIMER2B:
  1678. TCCR2B &= ~(CS20 | CS21 | CS22);
  1679. TCCR2B |= val;
  1680. break;
  1681. #endif
  1682. #if defined(TCCR3A)
  1683. case TIMER3A:
  1684. case TIMER3B:
  1685. case TIMER3C:
  1686. TCCR3B &= ~(CS30 | CS31 | CS32);
  1687. TCCR3B |= val;
  1688. break;
  1689. #endif
  1690. #if defined(TCCR4A)
  1691. case TIMER4A:
  1692. case TIMER4B:
  1693. case TIMER4C:
  1694. TCCR4B &= ~(CS40 | CS41 | CS42);
  1695. TCCR4B |= val;
  1696. break;
  1697. #endif
  1698. #if defined(TCCR5A)
  1699. case TIMER5A:
  1700. case TIMER5B:
  1701. case TIMER5C:
  1702. TCCR5B &= ~(CS50 | CS51 | CS52);
  1703. TCCR5B |= val;
  1704. break;
  1705. #endif
  1706. }
  1707. }
  1708. #endif