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

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