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