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_main.cpp 58KB

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