My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Marlin.pde 56KB

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