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

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