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

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