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

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