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

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