My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Marlin_main.cpp 75KB

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