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

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