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

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