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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206
  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. #ifdef ENABLE_AUTO_BED_LEVELING
  25. #include "vector_3.h"
  26. #endif // ENABLE_AUTO_BED_LEVELING
  27. #include "ultralcd.h"
  28. #include "planner.h"
  29. #include "stepper.h"
  30. #include "temperature.h"
  31. #include "motion_control.h"
  32. #include "cardreader.h"
  33. #include "watchdog.h"
  34. #include "ConfigurationStore.h"
  35. #include "language.h"
  36. #include "pins_arduino.h"
  37. #ifdef BLINKM
  38. #include "BlinkM.h"
  39. #include "Wire.h"
  40. #endif
  41. #if NUM_SERVOS > 0
  42. #include "Servo.h"
  43. #endif
  44. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  45. #include <SPI.h>
  46. #endif
  47. #define VERSION_STRING "1.0.0"
  48. // look here for descriptions of gcodes: http://linuxcnc.org/handbook/gcode/g-code.html
  49. // http://objects.reprap.org/wiki/Mendel_User_Manual:_RepRapGCodes
  50. //Implemented Codes
  51. //-------------------
  52. // G0 -> G1
  53. // G1 - Coordinated Movement X Y Z E
  54. // G2 - CW ARC
  55. // G3 - CCW ARC
  56. // G4 - Dwell S<seconds> or P<milliseconds>
  57. // G10 - retract filament according to settings of M207
  58. // G11 - retract recover filament according to settings of M208
  59. // G28 - Home all Axis
  60. // G29 - Detailed Z-Probe, probes the bed at 3 points. You must de at the home position for this to work correctly.
  61. // G30 - Single Z Probe, probes bed at current XY location.
  62. // G90 - Use Absolute Coordinates
  63. // G91 - Use Relative Coordinates
  64. // G92 - Set current position to cordinates given
  65. // M Codes
  66. // M0 - Unconditional stop - Wait for user to press a button on the LCD (Only if ULTRA_LCD is enabled)
  67. // M1 - Same as M0
  68. // M17 - Enable/Power all stepper motors
  69. // M18 - Disable all stepper motors; same as M84
  70. // M20 - List SD card
  71. // M21 - Init SD card
  72. // M22 - Release SD card
  73. // M23 - Select SD file (M23 filename.g)
  74. // M24 - Start/resume SD print
  75. // M25 - Pause SD print
  76. // M26 - Set SD position in bytes (M26 S12345)
  77. // M27 - Report SD print status
  78. // M28 - Start SD write (M28 filename.g)
  79. // M29 - Stop SD write
  80. // M30 - Delete file from SD (M30 filename.g)
  81. // M31 - Output time since last M109 or SD card start to serial
  82. // M32 - Select file and start SD print (Can be used _while_ printing from SD card files):
  83. // syntax "M32 /path/filename#", or "M32 S<startpos bytes> !filename#"
  84. // Call gcode file : "M32 P !filename#" and return to caller file after finishing (simiarl to #include).
  85. // The '#' is necessary when calling from within sd files, as it stops buffer prereading
  86. // 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.
  87. // M80 - Turn on Power Supply
  88. // M81 - Turn off Power Supply
  89. // M82 - Set E codes absolute (default)
  90. // M83 - Set E codes relative while in Absolute Coordinates (G90) mode
  91. // M84 - Disable steppers until next move,
  92. // or use S<seconds> to specify an inactivity timeout, after which the steppers will be disabled. S0 to disable the timeout.
  93. // M85 - Set inactivity shutdown timer with parameter S<seconds>. To disable set zero (default)
  94. // M92 - Set axis_steps_per_unit - same syntax as G92
  95. // M104 - Set extruder target temp
  96. // M105 - Read current temp
  97. // M106 - Fan on
  98. // M107 - Fan off
  99. // M109 - Sxxx Wait for extruder current temp to reach target temp. Waits only when heating
  100. // Rxxx Wait for extruder current temp to reach target temp. Waits when heating and cooling
  101. // M114 - Output current position to serial port
  102. // M115 - Capabilities string
  103. // M117 - display message
  104. // M119 - Output Endstop status to serial port
  105. // M126 - Solenoid Air Valve Open (BariCUDA support by jmil)
  106. // M127 - Solenoid Air Valve Closed (BariCUDA vent to atmospheric pressure by jmil)
  107. // M128 - EtoP Open (BariCUDA EtoP = electricity to air pressure transducer by jmil)
  108. // M129 - EtoP Closed (BariCUDA EtoP = electricity to air pressure transducer by jmil)
  109. // M140 - Set bed target temp
  110. // M150 - Set BlinkM Colour Output R: Red<0-255> U(!): Green<0-255> B: Blue<0-255> over i2c, G for green does not work.
  111. // M190 - Sxxx Wait for bed current temp to reach target temp. Waits only when heating
  112. // Rxxx Wait for bed current temp to reach target temp. Waits when heating and cooling
  113. // M200 - Set filament diameter
  114. // M201 - Set max acceleration in units/s^2 for print moves (M201 X1000 Y1000)
  115. // M202 - Set max acceleration in units/s^2 for travel moves (M202 X1000 Y1000) Unused in Marlin!!
  116. // M203 - Set maximum feedrate that your machine can sustain (M203 X200 Y200 Z300 E10000) in mm/sec
  117. // 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
  118. // 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
  119. // M206 - set additional homeing offset
  120. // M207 - set retract length S[positive mm] F[feedrate mm/sec] Z[additional zlift/hop]
  121. // M208 - set recover=unretract length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  122. // 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.
  123. // M218 - set hotend offset (in mm): T<extruder_number> X<offset_on_X> Y<offset_on_Y>
  124. // M220 S<factor in percent>- set speed factor override percentage
  125. // M221 S<factor in percent>- set extrude factor override percentage
  126. // M240 - Trigger a camera to take a photograph
  127. // M250 - Set LCD contrast C<contrast value> (value 0..63)
  128. // M280 - set servo position absolute. P: servo index, S: angle or microseconds
  129. // M300 - Play beepsound S<frequency Hz> P<duration ms>
  130. // M301 - Set PID parameters P I and D
  131. // M302 - Allow cold extrudes, or set the minimum extrude S<temperature>.
  132. // M303 - PID relay autotune S<temperature> sets the target temperature. (default target temperature = 150C)
  133. // M304 - Set bed PID parameters P I and D
  134. // M400 - Finish all moves
  135. // M401 - Lower z-probe if present
  136. // M402 - Raise z-probe if present
  137. // M500 - stores paramters in EEPROM
  138. // M501 - reads parameters from EEPROM (if you need reset them after you changed them temporarily).
  139. // M502 - reverts to the default "factory settings". You still need to store them in EEPROM afterwards if you want to.
  140. // M503 - print the current settings (from memory not from eeprom)
  141. // M540 - Use S[0|1] to enable or disable the stop SD card print on endstop hit (requires ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED)
  142. // M600 - Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
  143. // M666 - set delta endstop adjustemnt
  144. // M605 - Set dual x-carriage movement mode: S<mode> [ X<duplication x-offset> R<duplication temp offset> ]
  145. // M907 - Set digital trimpot motor current using axis codes.
  146. // M908 - Control digital trimpot directly.
  147. // M350 - Set microstepping mode.
  148. // M351 - Toggle MS1 MS2 pins directly.
  149. // M928 - Start SD logging (M928 filename.g) - ended by M29
  150. // M999 - Restart after being stopped by error
  151. //Stepper Movement Variables
  152. //===========================================================================
  153. //=============================imported variables============================
  154. //===========================================================================
  155. //===========================================================================
  156. //=============================public variables=============================
  157. //===========================================================================
  158. #ifdef SDSUPPORT
  159. CardReader card;
  160. #endif
  161. float homing_feedrate[] = HOMING_FEEDRATE;
  162. bool axis_relative_modes[] = AXIS_RELATIVE_MODES;
  163. int feedmultiply=100; //100->1 200->2
  164. int saved_feedmultiply;
  165. int extrudemultiply=100; //100->1 200->2
  166. float current_position[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0 };
  167. float add_homeing[3]={0,0,0};
  168. #ifdef DELTA
  169. float endstop_adj[3]={0,0,0};
  170. #endif
  171. float min_pos[3] = { X_MIN_POS, Y_MIN_POS, Z_MIN_POS };
  172. float max_pos[3] = { X_MAX_POS, Y_MAX_POS, Z_MAX_POS };
  173. // Extruder offset
  174. #if EXTRUDERS > 1
  175. #ifndef DUAL_X_CARRIAGE
  176. #define NUM_EXTRUDER_OFFSETS 2 // only in XY plane
  177. #else
  178. #define NUM_EXTRUDER_OFFSETS 3 // supports offsets in XYZ plane
  179. #endif
  180. float extruder_offset[NUM_EXTRUDER_OFFSETS][EXTRUDERS] = {
  181. #if defined(EXTRUDER_OFFSET_X) && defined(EXTRUDER_OFFSET_Y)
  182. EXTRUDER_OFFSET_X, EXTRUDER_OFFSET_Y
  183. #endif
  184. };
  185. #endif
  186. uint8_t active_extruder = 0;
  187. int fanSpeed=0;
  188. #ifdef SERVO_ENDSTOPS
  189. int servo_endstops[] = SERVO_ENDSTOPS;
  190. int servo_endstop_angles[] = SERVO_ENDSTOP_ANGLES;
  191. #endif
  192. #ifdef BARICUDA
  193. int ValvePressure=0;
  194. int EtoPPressure=0;
  195. #endif
  196. #ifdef FWRETRACT
  197. bool autoretract_enabled=true;
  198. bool retracted=false;
  199. float retract_length=3, retract_feedrate=17*60, retract_zlift=0.8;
  200. float retract_recover_length=0, retract_recover_feedrate=8*60;
  201. #endif
  202. #ifdef ULTIPANEL
  203. bool powersupply = true;
  204. #endif
  205. #ifdef DELTA
  206. float delta[3] = {0.0, 0.0, 0.0};
  207. #endif
  208. //===========================================================================
  209. //=============================private variables=============================
  210. //===========================================================================
  211. const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
  212. static float destination[NUM_AXIS] = { 0.0, 0.0, 0.0, 0.0};
  213. static float offset[3] = {0.0, 0.0, 0.0};
  214. static bool home_all_axis = true;
  215. static float feedrate = 1500.0, next_feedrate, saved_feedrate;
  216. static long gcode_N, gcode_LastN, Stopped_gcode_LastN = 0;
  217. static bool relative_mode = false; //Determines Absolute or Relative Coordinates
  218. static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE];
  219. static bool fromsd[BUFSIZE];
  220. static int bufindr = 0;
  221. static int bufindw = 0;
  222. static int buflen = 0;
  223. //static int i = 0;
  224. static char serial_char;
  225. static int serial_count = 0;
  226. static boolean comment_mode = false;
  227. static char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
  228. const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
  229. //static float tt = 0;
  230. //static float bt = 0;
  231. //Inactivity shutdown variables
  232. static unsigned long previous_millis_cmd = 0;
  233. static unsigned long max_inactive_time = 0;
  234. static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000l;
  235. unsigned long starttime=0;
  236. unsigned long stoptime=0;
  237. static uint8_t tmp_extruder;
  238. bool Stopped=false;
  239. #if NUM_SERVOS > 0
  240. Servo servos[NUM_SERVOS];
  241. #endif
  242. bool CooldownNoWait = true;
  243. bool target_direction;
  244. //===========================================================================
  245. //=============================ROUTINES=============================
  246. //===========================================================================
  247. void get_arc_coordinates();
  248. bool setTargetedHotend(int code);
  249. void serial_echopair_P(const char *s_P, float v)
  250. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  251. void serial_echopair_P(const char *s_P, double v)
  252. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  253. void serial_echopair_P(const char *s_P, unsigned long v)
  254. { serialprintPGM(s_P); SERIAL_ECHO(v); }
  255. extern "C"{
  256. extern unsigned int __bss_end;
  257. extern unsigned int __heap_start;
  258. extern void *__brkval;
  259. int freeMemory() {
  260. int free_memory;
  261. if((int)__brkval == 0)
  262. free_memory = ((int)&free_memory) - ((int)&__bss_end);
  263. else
  264. free_memory = ((int)&free_memory) - ((int)__brkval);
  265. return free_memory;
  266. }
  267. }
  268. //adds an command to the main command buffer
  269. //thats really done in a non-safe way.
  270. //needs overworking someday
  271. void enquecommand(const char *cmd)
  272. {
  273. if(buflen < BUFSIZE)
  274. {
  275. //this is dangerous if a mixing of serial and this happsens
  276. strcpy(&(cmdbuffer[bufindw][0]),cmd);
  277. SERIAL_ECHO_START;
  278. SERIAL_ECHOPGM("enqueing \"");
  279. SERIAL_ECHO(cmdbuffer[bufindw]);
  280. SERIAL_ECHOLNPGM("\"");
  281. bufindw= (bufindw + 1)%BUFSIZE;
  282. buflen += 1;
  283. }
  284. }
  285. void enquecommand_P(const char *cmd)
  286. {
  287. if(buflen < BUFSIZE)
  288. {
  289. //this is dangerous if a mixing of serial and this happsens
  290. strcpy_P(&(cmdbuffer[bufindw][0]),cmd);
  291. SERIAL_ECHO_START;
  292. SERIAL_ECHOPGM("enqueing \"");
  293. SERIAL_ECHO(cmdbuffer[bufindw]);
  294. SERIAL_ECHOLNPGM("\"");
  295. bufindw= (bufindw + 1)%BUFSIZE;
  296. buflen += 1;
  297. }
  298. }
  299. void setup_killpin()
  300. {
  301. #if defined(KILL_PIN) && KILL_PIN > -1
  302. pinMode(KILL_PIN,INPUT);
  303. WRITE(KILL_PIN,HIGH);
  304. #endif
  305. }
  306. void setup_photpin()
  307. {
  308. #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
  309. SET_OUTPUT(PHOTOGRAPH_PIN);
  310. WRITE(PHOTOGRAPH_PIN, LOW);
  311. #endif
  312. }
  313. void setup_powerhold()
  314. {
  315. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  316. SET_OUTPUT(SUICIDE_PIN);
  317. WRITE(SUICIDE_PIN, HIGH);
  318. #endif
  319. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  320. SET_OUTPUT(PS_ON_PIN);
  321. #if defined(PS_DEFAULT_OFF)
  322. WRITE(PS_ON_PIN, PS_ON_ASLEEP);
  323. #else
  324. WRITE(PS_ON_PIN, PS_ON_AWAKE);
  325. #endif
  326. #endif
  327. }
  328. void suicide()
  329. {
  330. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  331. SET_OUTPUT(SUICIDE_PIN);
  332. WRITE(SUICIDE_PIN, LOW);
  333. #endif
  334. }
  335. void servo_init()
  336. {
  337. #if (NUM_SERVOS >= 1) && defined(SERVO0_PIN) && (SERVO0_PIN > -1)
  338. servos[0].attach(SERVO0_PIN);
  339. #endif
  340. #if (NUM_SERVOS >= 2) && defined(SERVO1_PIN) && (SERVO1_PIN > -1)
  341. servos[1].attach(SERVO1_PIN);
  342. #endif
  343. #if (NUM_SERVOS >= 3) && defined(SERVO2_PIN) && (SERVO2_PIN > -1)
  344. servos[2].attach(SERVO2_PIN);
  345. #endif
  346. #if (NUM_SERVOS >= 4) && defined(SERVO3_PIN) && (SERVO3_PIN > -1)
  347. servos[3].attach(SERVO3_PIN);
  348. #endif
  349. #if (NUM_SERVOS >= 5)
  350. #error "TODO: enter initalisation code for more servos"
  351. #endif
  352. // Set position of Servo Endstops that are defined
  353. #ifdef SERVO_ENDSTOPS
  354. for(int8_t i = 0; i < 3; i++)
  355. {
  356. if(servo_endstops[i] > -1) {
  357. servos[servo_endstops[i]].write(servo_endstop_angles[i * 2 + 1]);
  358. }
  359. }
  360. #endif
  361. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  362. delay(PROBE_SERVO_DEACTIVATION_DELAY);
  363. servos[servo_endstops[Z_AXIS]].detach();
  364. #endif
  365. }
  366. void setup()
  367. {
  368. setup_killpin();
  369. setup_powerhold();
  370. MYSERIAL.begin(BAUDRATE);
  371. SERIAL_PROTOCOLLNPGM("start");
  372. SERIAL_ECHO_START;
  373. // Check startup - does nothing if bootloader sets MCUSR to 0
  374. byte mcu = MCUSR;
  375. if(mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
  376. if(mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
  377. if(mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
  378. if(mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
  379. if(mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
  380. MCUSR=0;
  381. SERIAL_ECHOPGM(MSG_MARLIN);
  382. SERIAL_ECHOLNPGM(VERSION_STRING);
  383. #ifdef STRING_VERSION_CONFIG_H
  384. #ifdef STRING_CONFIG_H_AUTHOR
  385. SERIAL_ECHO_START;
  386. SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
  387. SERIAL_ECHOPGM(STRING_VERSION_CONFIG_H);
  388. SERIAL_ECHOPGM(MSG_AUTHOR);
  389. SERIAL_ECHOLNPGM(STRING_CONFIG_H_AUTHOR);
  390. SERIAL_ECHOPGM("Compiled: ");
  391. SERIAL_ECHOLNPGM(__DATE__);
  392. #endif
  393. #endif
  394. SERIAL_ECHO_START;
  395. SERIAL_ECHOPGM(MSG_FREE_MEMORY);
  396. SERIAL_ECHO(freeMemory());
  397. SERIAL_ECHOPGM(MSG_PLANNER_BUFFER_BYTES);
  398. SERIAL_ECHOLN((int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
  399. for(int8_t i = 0; i < BUFSIZE; i++)
  400. {
  401. fromsd[i] = false;
  402. }
  403. // loads data from EEPROM if available else uses defaults (and resets step acceleration rate)
  404. Config_RetrieveSettings();
  405. tp_init(); // Initialize temperature loop
  406. plan_init(); // Initialize planner;
  407. watchdog_init();
  408. st_init(); // Initialize stepper, this enables interrupts!
  409. setup_photpin();
  410. servo_init();
  411. lcd_init();
  412. _delay_ms(1000); // wait 1sec to display the splash screen
  413. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  414. SET_OUTPUT(CONTROLLERFAN_PIN); //Set pin used for driver cooling fan
  415. #endif
  416. }
  417. void loop()
  418. {
  419. if(buflen < (BUFSIZE-1))
  420. get_command();
  421. #ifdef SDSUPPORT
  422. card.checkautostart(false);
  423. #endif
  424. if(buflen)
  425. {
  426. #ifdef SDSUPPORT
  427. if(card.saving)
  428. {
  429. if(strstr_P(cmdbuffer[bufindr], PSTR("M29")) == NULL)
  430. {
  431. card.write_command(cmdbuffer[bufindr]);
  432. if(card.logging)
  433. {
  434. process_commands();
  435. }
  436. else
  437. {
  438. SERIAL_PROTOCOLLNPGM(MSG_OK);
  439. }
  440. }
  441. else
  442. {
  443. card.closefile();
  444. SERIAL_PROTOCOLLNPGM(MSG_FILE_SAVED);
  445. }
  446. }
  447. else
  448. {
  449. process_commands();
  450. }
  451. #else
  452. process_commands();
  453. #endif //SDSUPPORT
  454. buflen = (buflen-1);
  455. bufindr = (bufindr + 1)%BUFSIZE;
  456. }
  457. //check heater every n milliseconds
  458. manage_heater();
  459. manage_inactivity();
  460. checkHitEndstops();
  461. lcd_update();
  462. }
  463. void get_command()
  464. {
  465. while( MYSERIAL.available() > 0 && buflen < BUFSIZE) {
  466. serial_char = MYSERIAL.read();
  467. if(serial_char == '\n' ||
  468. serial_char == '\r' ||
  469. (serial_char == ':' && comment_mode == false) ||
  470. serial_count >= (MAX_CMD_SIZE - 1) )
  471. {
  472. if(!serial_count) { //if empty line
  473. comment_mode = false; //for new command
  474. return;
  475. }
  476. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  477. if(!comment_mode){
  478. comment_mode = false; //for new command
  479. fromsd[bufindw] = false;
  480. if(strchr(cmdbuffer[bufindw], 'N') != NULL)
  481. {
  482. strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
  483. gcode_N = (strtol(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL, 10));
  484. if(gcode_N != gcode_LastN+1 && (strstr_P(cmdbuffer[bufindw], PSTR("M110")) == NULL) ) {
  485. SERIAL_ERROR_START;
  486. SERIAL_ERRORPGM(MSG_ERR_LINE_NO);
  487. SERIAL_ERRORLN(gcode_LastN);
  488. //Serial.println(gcode_N);
  489. FlushSerialRequestResend();
  490. serial_count = 0;
  491. return;
  492. }
  493. if(strchr(cmdbuffer[bufindw], '*') != NULL)
  494. {
  495. byte checksum = 0;
  496. byte count = 0;
  497. while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
  498. strchr_pointer = strchr(cmdbuffer[bufindw], '*');
  499. if( (int)(strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)) != checksum) {
  500. SERIAL_ERROR_START;
  501. SERIAL_ERRORPGM(MSG_ERR_CHECKSUM_MISMATCH);
  502. SERIAL_ERRORLN(gcode_LastN);
  503. FlushSerialRequestResend();
  504. serial_count = 0;
  505. return;
  506. }
  507. //if no errors, continue parsing
  508. }
  509. else
  510. {
  511. SERIAL_ERROR_START;
  512. SERIAL_ERRORPGM(MSG_ERR_NO_CHECKSUM);
  513. SERIAL_ERRORLN(gcode_LastN);
  514. FlushSerialRequestResend();
  515. serial_count = 0;
  516. return;
  517. }
  518. gcode_LastN = gcode_N;
  519. //if no errors, continue parsing
  520. }
  521. else // if we don't receive 'N' but still see '*'
  522. {
  523. if((strchr(cmdbuffer[bufindw], '*') != NULL))
  524. {
  525. SERIAL_ERROR_START;
  526. SERIAL_ERRORPGM(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM);
  527. SERIAL_ERRORLN(gcode_LastN);
  528. serial_count = 0;
  529. return;
  530. }
  531. }
  532. if((strchr(cmdbuffer[bufindw], 'G') != NULL)){
  533. strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
  534. switch((int)((strtod(&cmdbuffer[bufindw][strchr_pointer - cmdbuffer[bufindw] + 1], NULL)))){
  535. case 0:
  536. case 1:
  537. case 2:
  538. case 3:
  539. if(Stopped == false) { // If printer is stopped by an error the G[0-3] codes are ignored.
  540. #ifdef SDSUPPORT
  541. if(card.saving)
  542. break;
  543. #endif //SDSUPPORT
  544. SERIAL_PROTOCOLLNPGM(MSG_OK);
  545. }
  546. else {
  547. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  548. LCD_MESSAGEPGM(MSG_STOPPED);
  549. }
  550. break;
  551. default:
  552. break;
  553. }
  554. }
  555. bufindw = (bufindw + 1)%BUFSIZE;
  556. buflen += 1;
  557. }
  558. serial_count = 0; //clear buffer
  559. }
  560. else
  561. {
  562. if(serial_char == ';') comment_mode = true;
  563. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  564. }
  565. }
  566. #ifdef SDSUPPORT
  567. if(!card.sdprinting || serial_count!=0){
  568. return;
  569. }
  570. //'#' stops reading from sd to the buffer prematurely, so procedural macro calls are possible
  571. // if it occures, stop_buffering is triggered and the buffer is ran dry.
  572. // this character _can_ occure in serial com, due to checksums. however, no checksums are used in sd printing
  573. static bool stop_buffering=false;
  574. if(buflen==0) stop_buffering=false;
  575. while( !card.eof() && buflen < BUFSIZE && !stop_buffering) {
  576. int16_t n=card.get();
  577. serial_char = (char)n;
  578. if(serial_char == '\n' ||
  579. serial_char == '\r' ||
  580. (serial_char == '#' && comment_mode == false) ||
  581. (serial_char == ':' && comment_mode == false) ||
  582. serial_count >= (MAX_CMD_SIZE - 1)||n==-1)
  583. {
  584. if(card.eof()){
  585. SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED);
  586. stoptime=millis();
  587. char time[30];
  588. unsigned long t=(stoptime-starttime)/1000;
  589. int hours, minutes;
  590. minutes=(t/60)%60;
  591. hours=t/60/60;
  592. sprintf_P(time, PSTR("%i hours %i minutes"),hours, minutes);
  593. SERIAL_ECHO_START;
  594. SERIAL_ECHOLN(time);
  595. lcd_setstatus(time);
  596. card.printingHasFinished();
  597. card.checkautostart(true);
  598. }
  599. if(serial_char=='#')
  600. stop_buffering=true;
  601. if(!serial_count)
  602. {
  603. comment_mode = false; //for new command
  604. return; //if empty line
  605. }
  606. cmdbuffer[bufindw][serial_count] = 0; //terminate string
  607. // if(!comment_mode){
  608. fromsd[bufindw] = true;
  609. buflen += 1;
  610. bufindw = (bufindw + 1)%BUFSIZE;
  611. // }
  612. comment_mode = false; //for new command
  613. serial_count = 0; //clear buffer
  614. }
  615. else
  616. {
  617. if(serial_char == ';') comment_mode = true;
  618. if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
  619. }
  620. }
  621. #endif //SDSUPPORT
  622. }
  623. float code_value()
  624. {
  625. return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL));
  626. }
  627. long code_value_long()
  628. {
  629. return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10));
  630. }
  631. bool code_seen(char code)
  632. {
  633. strchr_pointer = strchr(cmdbuffer[bufindr], code);
  634. return (strchr_pointer != NULL); //Return True if a character was found
  635. }
  636. #define DEFINE_PGM_READ_ANY(type, reader) \
  637. static inline type pgm_read_any(const type *p) \
  638. { return pgm_read_##reader##_near(p); }
  639. DEFINE_PGM_READ_ANY(float, float);
  640. DEFINE_PGM_READ_ANY(signed char, byte);
  641. #define XYZ_CONSTS_FROM_CONFIG(type, array, CONFIG) \
  642. static const PROGMEM type array##_P[3] = \
  643. { X_##CONFIG, Y_##CONFIG, Z_##CONFIG }; \
  644. static inline type array(int axis) \
  645. { return pgm_read_any(&array##_P[axis]); }
  646. XYZ_CONSTS_FROM_CONFIG(float, base_min_pos, MIN_POS);
  647. XYZ_CONSTS_FROM_CONFIG(float, base_max_pos, MAX_POS);
  648. XYZ_CONSTS_FROM_CONFIG(float, base_home_pos, HOME_POS);
  649. XYZ_CONSTS_FROM_CONFIG(float, max_length, MAX_LENGTH);
  650. XYZ_CONSTS_FROM_CONFIG(float, home_retract_mm, HOME_RETRACT_MM);
  651. XYZ_CONSTS_FROM_CONFIG(signed char, home_dir, HOME_DIR);
  652. #ifdef DUAL_X_CARRIAGE
  653. #if EXTRUDERS == 1 || defined(COREXY) \
  654. || !defined(X2_ENABLE_PIN) || !defined(X2_STEP_PIN) || !defined(X2_DIR_PIN) \
  655. || !defined(X2_HOME_POS) || !defined(X2_MIN_POS) || !defined(X2_MAX_POS) \
  656. || !defined(X_MAX_PIN) || X_MAX_PIN < 0
  657. #error "Missing or invalid definitions for DUAL_X_CARRIAGE mode."
  658. #endif
  659. #if X_HOME_DIR != -1 || X2_HOME_DIR != 1
  660. #error "Please use canonical x-carriage assignment" // the x-carriages are defined by their homing directions
  661. #endif
  662. #define DXC_FULL_CONTROL_MODE 0
  663. #define DXC_AUTO_PARK_MODE 1
  664. #define DXC_DUPLICATION_MODE 2
  665. static int dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  666. static float x_home_pos(int extruder) {
  667. if (extruder == 0)
  668. return base_home_pos(X_AXIS) + add_homeing[X_AXIS];
  669. else
  670. // In dual carriage mode the extruder offset provides an override of the
  671. // second X-carriage offset when homed - otherwise X2_HOME_POS is used.
  672. // This allow soft recalibration of the second extruder offset position without firmware reflash
  673. // (through the M218 command).
  674. return (extruder_offset[X_AXIS][1] > 0) ? extruder_offset[X_AXIS][1] : X2_HOME_POS;
  675. }
  676. static int x_home_dir(int extruder) {
  677. return (extruder == 0) ? X_HOME_DIR : X2_HOME_DIR;
  678. }
  679. static float inactive_extruder_x_pos = X2_MAX_POS; // used in mode 0 & 1
  680. static bool active_extruder_parked = false; // used in mode 1 & 2
  681. static float raised_parked_position[NUM_AXIS]; // used in mode 1
  682. static unsigned long delayed_move_time = 0; // used in mode 1
  683. static float duplicate_extruder_x_offset = DEFAULT_DUPLICATION_X_OFFSET; // used in mode 2
  684. static float duplicate_extruder_temp_offset = 0; // used in mode 2
  685. bool extruder_duplication_enabled = false; // used in mode 2
  686. #endif //DUAL_X_CARRIAGE
  687. static void axis_is_at_home(int axis) {
  688. #ifdef DUAL_X_CARRIAGE
  689. if (axis == X_AXIS) {
  690. if (active_extruder != 0) {
  691. current_position[X_AXIS] = x_home_pos(active_extruder);
  692. min_pos[X_AXIS] = X2_MIN_POS;
  693. max_pos[X_AXIS] = max(extruder_offset[X_AXIS][1], X2_MAX_POS);
  694. return;
  695. }
  696. else if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && active_extruder == 0) {
  697. current_position[X_AXIS] = base_home_pos(X_AXIS) + add_homeing[X_AXIS];
  698. min_pos[X_AXIS] = base_min_pos(X_AXIS) + add_homeing[X_AXIS];
  699. max_pos[X_AXIS] = min(base_max_pos(X_AXIS) + add_homeing[X_AXIS],
  700. max(extruder_offset[X_AXIS][1], X2_MAX_POS) - duplicate_extruder_x_offset);
  701. return;
  702. }
  703. }
  704. #endif
  705. current_position[axis] = base_home_pos(axis) + add_homeing[axis];
  706. min_pos[axis] = base_min_pos(axis) + add_homeing[axis];
  707. max_pos[axis] = base_max_pos(axis) + add_homeing[axis];
  708. }
  709. #ifdef ENABLE_AUTO_BED_LEVELING
  710. static void set_bed_level_equation(float z_at_xLeft_yFront, float z_at_xRight_yFront, float z_at_xLeft_yBack) {
  711. plan_bed_level_matrix.set_to_identity();
  712. vector_3 xLeftyFront = vector_3(LEFT_PROBE_BED_POSITION, FRONT_PROBE_BED_POSITION, z_at_xLeft_yFront);
  713. vector_3 xLeftyBack = vector_3(LEFT_PROBE_BED_POSITION, BACK_PROBE_BED_POSITION, z_at_xLeft_yBack);
  714. vector_3 xRightyFront = vector_3(RIGHT_PROBE_BED_POSITION, FRONT_PROBE_BED_POSITION, z_at_xRight_yFront);
  715. vector_3 xPositive = (xRightyFront - xLeftyFront).get_normal();
  716. vector_3 yPositive = (xLeftyBack - xLeftyFront).get_normal();
  717. vector_3 planeNormal = vector_3::cross(yPositive, xPositive).get_normal();
  718. //planeNormal.debug("planeNormal");
  719. //yPositive.debug("yPositive");
  720. matrix_3x3 bedLevel = matrix_3x3::create_look_at(planeNormal, yPositive);
  721. //bedLevel.debug("bedLevel");
  722. //plan_bed_level_matrix.debug("bed level before");
  723. //vector_3 uncorrected_position = plan_get_position_mm();
  724. //uncorrected_position.debug("position before");
  725. // and set our bed level equation to do the right thing
  726. plan_bed_level_matrix = matrix_3x3::create_inverse(bedLevel);
  727. //plan_bed_level_matrix.debug("bed level after");
  728. vector_3 corrected_position = plan_get_position();
  729. //corrected_position.debug("position after");
  730. current_position[X_AXIS] = corrected_position.x;
  731. current_position[Y_AXIS] = corrected_position.y;
  732. current_position[Z_AXIS] = corrected_position.z;
  733. // but the bed at 0 so we don't go below it.
  734. current_position[Z_AXIS] = -Z_PROBE_OFFSET_FROM_EXTRUDER;
  735. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  736. }
  737. static void run_z_probe() {
  738. plan_bed_level_matrix.set_to_identity();
  739. feedrate = homing_feedrate[Z_AXIS];
  740. // move down until you find the bed
  741. float zPosition = -10;
  742. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
  743. st_synchronize();
  744. // we have to let the planner know where we are right now as it is not where we said to go.
  745. zPosition = st_get_position_mm(Z_AXIS);
  746. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS]);
  747. // move up the retract distance
  748. zPosition += home_retract_mm(Z_AXIS);
  749. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
  750. st_synchronize();
  751. // move back down slowly to find bed
  752. feedrate = homing_feedrate[Z_AXIS]/4;
  753. zPosition -= home_retract_mm(Z_AXIS) * 2;
  754. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], zPosition, current_position[E_AXIS], feedrate/60, active_extruder);
  755. st_synchronize();
  756. current_position[Z_AXIS] = st_get_position_mm(Z_AXIS);
  757. // make sure the planner knows where we are as it may be a bit different than we last said to move to
  758. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  759. }
  760. static void do_blocking_move_to(float x, float y, float z) {
  761. float oldFeedRate = feedrate;
  762. feedrate = XY_TRAVEL_SPEED;
  763. current_position[X_AXIS] = x;
  764. current_position[Y_AXIS] = y;
  765. current_position[Z_AXIS] = z;
  766. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], feedrate/60, active_extruder);
  767. st_synchronize();
  768. feedrate = oldFeedRate;
  769. }
  770. static void do_blocking_move_relative(float offset_x, float offset_y, float offset_z) {
  771. do_blocking_move_to(current_position[X_AXIS] + offset_x, current_position[Y_AXIS] + offset_y, current_position[Z_AXIS] + offset_z);
  772. }
  773. static void setup_for_endstop_move() {
  774. saved_feedrate = feedrate;
  775. saved_feedmultiply = feedmultiply;
  776. feedmultiply = 100;
  777. previous_millis_cmd = millis();
  778. enable_endstops(true);
  779. }
  780. static void clean_up_after_endstop_move() {
  781. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  782. enable_endstops(false);
  783. #endif
  784. feedrate = saved_feedrate;
  785. feedmultiply = saved_feedmultiply;
  786. previous_millis_cmd = millis();
  787. }
  788. static void engage_z_probe() {
  789. // Engage Z Servo endstop if enabled
  790. #ifdef SERVO_ENDSTOPS
  791. if (servo_endstops[Z_AXIS] > -1) {
  792. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  793. servos[servo_endstops[Z_AXIS]].attach(0);
  794. #endif
  795. servos[servo_endstops[Z_AXIS]].write(servo_endstop_angles[Z_AXIS * 2]);
  796. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  797. delay(PROBE_SERVO_DEACTIVATION_DELAY);
  798. servos[servo_endstops[Z_AXIS]].detach();
  799. #endif
  800. }
  801. #endif
  802. }
  803. static void retract_z_probe() {
  804. // Retract Z Servo endstop if enabled
  805. #ifdef SERVO_ENDSTOPS
  806. if (servo_endstops[Z_AXIS] > -1) {
  807. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  808. servos[servo_endstops[Z_AXIS]].attach(0);
  809. #endif
  810. servos[servo_endstops[Z_AXIS]].write(servo_endstop_angles[Z_AXIS * 2 + 1]);
  811. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  812. delay(PROBE_SERVO_DEACTIVATION_DELAY);
  813. servos[servo_endstops[Z_AXIS]].detach();
  814. #endif
  815. }
  816. #endif
  817. }
  818. #endif // #ifdef ENABLE_AUTO_BED_LEVELING
  819. static void homeaxis(int axis) {
  820. #define HOMEAXIS_DO(LETTER) \
  821. ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))
  822. if (axis==X_AXIS ? HOMEAXIS_DO(X) :
  823. axis==Y_AXIS ? HOMEAXIS_DO(Y) :
  824. axis==Z_AXIS ? HOMEAXIS_DO(Z) :
  825. 0) {
  826. int axis_home_dir = home_dir(axis);
  827. #ifdef DUAL_X_CARRIAGE
  828. if (axis == X_AXIS)
  829. axis_home_dir = x_home_dir(active_extruder);
  830. #endif
  831. current_position[axis] = 0;
  832. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  833. // Engage Servo endstop if enabled
  834. #ifdef SERVO_ENDSTOPS
  835. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  836. if (axis==Z_AXIS) {
  837. #if defined (Z_RAISE_BEFORE_HOMING) && (Z_RAISE_BEFORE_HOMING > 0)
  838. destination[axis] = Z_RAISE_BEFORE_HOMING * axis_home_dir * (-1); // Set destination away from bed
  839. feedrate = max_feedrate[axis];
  840. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate, active_extruder);
  841. st_synchronize();
  842. #endif
  843. engage_z_probe();
  844. }
  845. else
  846. #endif
  847. if (servo_endstops[axis] > -1) {
  848. servos[servo_endstops[axis]].write(servo_endstop_angles[axis * 2]);
  849. }
  850. #endif
  851. destination[axis] = 1.5 * max_length(axis) * axis_home_dir;
  852. feedrate = homing_feedrate[axis];
  853. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  854. st_synchronize();
  855. current_position[axis] = 0;
  856. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  857. destination[axis] = -home_retract_mm(axis) * axis_home_dir;
  858. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  859. st_synchronize();
  860. destination[axis] = 2*home_retract_mm(axis) * axis_home_dir;
  861. #ifdef DELTA
  862. feedrate = homing_feedrate[axis]/10;
  863. #else
  864. feedrate = homing_feedrate[axis]/2 ;
  865. #endif
  866. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  867. st_synchronize();
  868. #ifdef DELTA
  869. // retrace by the amount specified in endstop_adj
  870. if (endstop_adj[axis] * axis_home_dir < 0) {
  871. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  872. destination[axis] = endstop_adj[axis];
  873. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  874. st_synchronize();
  875. }
  876. #endif
  877. axis_is_at_home(axis);
  878. destination[axis] = current_position[axis];
  879. feedrate = 0.0;
  880. endstops_hit_on_purpose();
  881. // Retract Servo endstop if enabled
  882. #ifdef SERVO_ENDSTOPS
  883. if (servo_endstops[axis] > -1) {
  884. servos[servo_endstops[axis]].write(servo_endstop_angles[axis * 2 + 1]);
  885. }
  886. #endif
  887. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  888. if (axis==Z_AXIS) retract_z_probe();
  889. #endif
  890. }
  891. }
  892. #define HOMEAXIS(LETTER) homeaxis(LETTER##_AXIS)
  893. void process_commands()
  894. {
  895. unsigned long codenum; //throw away variable
  896. char *starpos = NULL;
  897. #ifdef ENABLE_AUTO_BED_LEVELING
  898. float x_tmp, y_tmp, z_tmp, real_z;
  899. #endif
  900. if(code_seen('G'))
  901. {
  902. switch((int)code_value())
  903. {
  904. case 0: // G0 -> G1
  905. case 1: // G1
  906. if(Stopped == false) {
  907. get_coordinates(); // For X Y Z E F
  908. prepare_move();
  909. //ClearToSend();
  910. return;
  911. }
  912. //break;
  913. case 2: // G2 - CW ARC
  914. if(Stopped == false) {
  915. get_arc_coordinates();
  916. prepare_arc_move(true);
  917. return;
  918. }
  919. case 3: // G3 - CCW ARC
  920. if(Stopped == false) {
  921. get_arc_coordinates();
  922. prepare_arc_move(false);
  923. return;
  924. }
  925. case 4: // G4 dwell
  926. LCD_MESSAGEPGM(MSG_DWELL);
  927. codenum = 0;
  928. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  929. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  930. st_synchronize();
  931. codenum += millis(); // keep track of when we started waiting
  932. previous_millis_cmd = millis();
  933. while(millis() < codenum ){
  934. manage_heater();
  935. manage_inactivity();
  936. lcd_update();
  937. }
  938. break;
  939. #ifdef FWRETRACT
  940. case 10: // G10 retract
  941. if(!retracted)
  942. {
  943. destination[X_AXIS]=current_position[X_AXIS];
  944. destination[Y_AXIS]=current_position[Y_AXIS];
  945. destination[Z_AXIS]=current_position[Z_AXIS];
  946. current_position[Z_AXIS]+=-retract_zlift;
  947. destination[E_AXIS]=current_position[E_AXIS]-retract_length;
  948. feedrate=retract_feedrate;
  949. retracted=true;
  950. prepare_move();
  951. }
  952. break;
  953. case 11: // G11 retract_recover
  954. if(retracted)
  955. {
  956. destination[X_AXIS]=current_position[X_AXIS];
  957. destination[Y_AXIS]=current_position[Y_AXIS];
  958. destination[Z_AXIS]=current_position[Z_AXIS];
  959. current_position[Z_AXIS]+=retract_zlift;
  960. destination[E_AXIS]=current_position[E_AXIS]+retract_length+retract_recover_length;
  961. feedrate=retract_recover_feedrate;
  962. retracted=false;
  963. prepare_move();
  964. }
  965. break;
  966. #endif //FWRETRACT
  967. case 28: //G28 Home all Axis one at a time
  968. #ifdef ENABLE_AUTO_BED_LEVELING
  969. plan_bed_level_matrix.set_to_identity(); //Reset the plane ("erase" all leveling data)
  970. #endif //ENABLE_AUTO_BED_LEVELING
  971. saved_feedrate = feedrate;
  972. saved_feedmultiply = feedmultiply;
  973. feedmultiply = 100;
  974. previous_millis_cmd = millis();
  975. enable_endstops(true);
  976. for(int8_t i=0; i < NUM_AXIS; i++) {
  977. destination[i] = current_position[i];
  978. }
  979. feedrate = 0.0;
  980. #ifdef DELTA
  981. // A delta can only safely home all axis at the same time
  982. // all axis have to home at the same time
  983. // Move all carriages up together until the first endstop is hit.
  984. current_position[X_AXIS] = 0;
  985. current_position[Y_AXIS] = 0;
  986. current_position[Z_AXIS] = 0;
  987. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  988. destination[X_AXIS] = 3 * Z_MAX_LENGTH;
  989. destination[Y_AXIS] = 3 * Z_MAX_LENGTH;
  990. destination[Z_AXIS] = 3 * Z_MAX_LENGTH;
  991. feedrate = 1.732 * homing_feedrate[X_AXIS];
  992. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  993. st_synchronize();
  994. endstops_hit_on_purpose();
  995. current_position[X_AXIS] = destination[X_AXIS];
  996. current_position[Y_AXIS] = destination[Y_AXIS];
  997. current_position[Z_AXIS] = destination[Z_AXIS];
  998. // take care of back off and rehome now we are all at the top
  999. HOMEAXIS(X);
  1000. HOMEAXIS(Y);
  1001. HOMEAXIS(Z);
  1002. calculate_delta(current_position);
  1003. plan_set_position(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS], current_position[E_AXIS]);
  1004. #else // NOT DELTA
  1005. home_all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2])));
  1006. #if Z_HOME_DIR > 0 // If homing away from BED do Z first
  1007. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  1008. HOMEAXIS(Z);
  1009. }
  1010. #endif
  1011. #ifdef QUICK_HOME
  1012. if((home_all_axis)||( code_seen(axis_codes[X_AXIS]) && code_seen(axis_codes[Y_AXIS])) ) //first diagonal move
  1013. {
  1014. current_position[X_AXIS] = 0;current_position[Y_AXIS] = 0;
  1015. #ifndef DUAL_X_CARRIAGE
  1016. int x_axis_home_dir = home_dir(X_AXIS);
  1017. #else
  1018. int x_axis_home_dir = x_home_dir(active_extruder);
  1019. extruder_duplication_enabled = false;
  1020. #endif
  1021. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1022. destination[X_AXIS] = 1.5 * max_length(X_AXIS) * x_axis_home_dir;destination[Y_AXIS] = 1.5 * max_length(Y_AXIS) * home_dir(Y_AXIS);
  1023. feedrate = homing_feedrate[X_AXIS];
  1024. if(homing_feedrate[Y_AXIS]<feedrate)
  1025. feedrate =homing_feedrate[Y_AXIS];
  1026. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1027. st_synchronize();
  1028. axis_is_at_home(X_AXIS);
  1029. axis_is_at_home(Y_AXIS);
  1030. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1031. destination[X_AXIS] = current_position[X_AXIS];
  1032. destination[Y_AXIS] = current_position[Y_AXIS];
  1033. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  1034. feedrate = 0.0;
  1035. st_synchronize();
  1036. endstops_hit_on_purpose();
  1037. current_position[X_AXIS] = destination[X_AXIS];
  1038. current_position[Y_AXIS] = destination[Y_AXIS];
  1039. current_position[Z_AXIS] = destination[Z_AXIS];
  1040. }
  1041. #endif
  1042. if((home_all_axis) || (code_seen(axis_codes[X_AXIS])))
  1043. {
  1044. #ifdef DUAL_X_CARRIAGE
  1045. int tmp_extruder = active_extruder;
  1046. extruder_duplication_enabled = false;
  1047. active_extruder = !active_extruder;
  1048. HOMEAXIS(X);
  1049. inactive_extruder_x_pos = current_position[X_AXIS];
  1050. active_extruder = tmp_extruder;
  1051. HOMEAXIS(X);
  1052. // reset state used by the different modes
  1053. memcpy(raised_parked_position, current_position, sizeof(raised_parked_position));
  1054. delayed_move_time = 0;
  1055. active_extruder_parked = true;
  1056. #else
  1057. HOMEAXIS(X);
  1058. #endif
  1059. }
  1060. if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) {
  1061. HOMEAXIS(Y);
  1062. }
  1063. #if Z_HOME_DIR < 0 // If homing towards BED do Z last
  1064. if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) {
  1065. HOMEAXIS(Z);
  1066. }
  1067. #endif
  1068. if(code_seen(axis_codes[X_AXIS]))
  1069. {
  1070. if(code_value_long() != 0) {
  1071. current_position[X_AXIS]=code_value()+add_homeing[0];
  1072. }
  1073. }
  1074. if(code_seen(axis_codes[Y_AXIS])) {
  1075. if(code_value_long() != 0) {
  1076. current_position[Y_AXIS]=code_value()+add_homeing[1];
  1077. }
  1078. }
  1079. if(code_seen(axis_codes[Z_AXIS])) {
  1080. if(code_value_long() != 0) {
  1081. current_position[Z_AXIS]=code_value()+add_homeing[2];
  1082. }
  1083. }
  1084. #ifdef ENABLE_AUTO_BED_LEVELING
  1085. current_position[Z_AXIS] -= Z_PROBE_OFFSET_FROM_EXTRUDER; //Add Z_Probe offset (the distance is negative)
  1086. #endif
  1087. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1088. #endif // else DELTA
  1089. #ifdef ENDSTOPS_ONLY_FOR_HOMING
  1090. enable_endstops(false);
  1091. #endif
  1092. feedrate = saved_feedrate;
  1093. feedmultiply = saved_feedmultiply;
  1094. previous_millis_cmd = millis();
  1095. endstops_hit_on_purpose();
  1096. break;
  1097. #ifdef ENABLE_AUTO_BED_LEVELING
  1098. case 29: // G29 Detailed Z-Probe, probes the bed at 3 points.
  1099. {
  1100. #if Z_MIN_PIN == -1
  1101. #error "You must have a Z_MIN endstop in order to enable Auto Bed Leveling feature!!! Z_MIN_PIN must point to a valid hardware pin."
  1102. #endif
  1103. st_synchronize();
  1104. // make sure the bed_level_rotation_matrix is identity or the planner will get it incorectly
  1105. //vector_3 corrected_position = plan_get_position_mm();
  1106. //corrected_position.debug("position before G29");
  1107. plan_bed_level_matrix.set_to_identity();
  1108. vector_3 uncorrected_position = plan_get_position();
  1109. //uncorrected_position.debug("position durring G29");
  1110. current_position[X_AXIS] = uncorrected_position.x;
  1111. current_position[Y_AXIS] = uncorrected_position.y;
  1112. current_position[Z_AXIS] = uncorrected_position.z;
  1113. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1114. setup_for_endstop_move();
  1115. feedrate = homing_feedrate[Z_AXIS];
  1116. // prob 1
  1117. do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], Z_RAISE_BEFORE_PROBING);
  1118. do_blocking_move_to(LEFT_PROBE_BED_POSITION - X_PROBE_OFFSET_FROM_EXTRUDER, BACK_PROBE_BED_POSITION - Y_PROBE_OFFSET_FROM_EXTRUDER, current_position[Z_AXIS]);
  1119. engage_z_probe(); // Engage Z Servo endstop if available
  1120. run_z_probe();
  1121. float z_at_xLeft_yBack = current_position[Z_AXIS];
  1122. SERIAL_PROTOCOLPGM("Bed x: ");
  1123. SERIAL_PROTOCOL(LEFT_PROBE_BED_POSITION);
  1124. SERIAL_PROTOCOLPGM(" y: ");
  1125. SERIAL_PROTOCOL(BACK_PROBE_BED_POSITION);
  1126. SERIAL_PROTOCOLPGM(" z: ");
  1127. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  1128. SERIAL_PROTOCOLPGM("\n");
  1129. // prob 2
  1130. do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS);
  1131. do_blocking_move_to(LEFT_PROBE_BED_POSITION - X_PROBE_OFFSET_FROM_EXTRUDER, FRONT_PROBE_BED_POSITION - Y_PROBE_OFFSET_FROM_EXTRUDER, current_position[Z_AXIS]);
  1132. run_z_probe();
  1133. float z_at_xLeft_yFront = current_position[Z_AXIS];
  1134. SERIAL_PROTOCOLPGM("Bed x: ");
  1135. SERIAL_PROTOCOL(LEFT_PROBE_BED_POSITION);
  1136. SERIAL_PROTOCOLPGM(" y: ");
  1137. SERIAL_PROTOCOL(FRONT_PROBE_BED_POSITION);
  1138. SERIAL_PROTOCOLPGM(" z: ");
  1139. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  1140. SERIAL_PROTOCOLPGM("\n");
  1141. // prob 3
  1142. do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS);
  1143. // the current position will be updated by the blocking move so the head will not lower on this next call.
  1144. do_blocking_move_to(RIGHT_PROBE_BED_POSITION - X_PROBE_OFFSET_FROM_EXTRUDER, FRONT_PROBE_BED_POSITION - Y_PROBE_OFFSET_FROM_EXTRUDER, current_position[Z_AXIS]);
  1145. run_z_probe();
  1146. float z_at_xRight_yFront = current_position[Z_AXIS];
  1147. SERIAL_PROTOCOLPGM("Bed x: ");
  1148. SERIAL_PROTOCOL(RIGHT_PROBE_BED_POSITION);
  1149. SERIAL_PROTOCOLPGM(" y: ");
  1150. SERIAL_PROTOCOL(FRONT_PROBE_BED_POSITION);
  1151. SERIAL_PROTOCOLPGM(" z: ");
  1152. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  1153. SERIAL_PROTOCOLPGM("\n");
  1154. clean_up_after_endstop_move();
  1155. set_bed_level_equation(z_at_xLeft_yFront, z_at_xRight_yFront, z_at_xLeft_yBack);
  1156. retract_z_probe(); // Retract Z Servo endstop if available
  1157. st_synchronize();
  1158. // The following code correct the Z height difference from z-probe position and hotend tip position.
  1159. // The Z height on homing is measured by Z-Probe, but the probe is quite far from the hotend.
  1160. // When the bed is uneven, this height must be corrected.
  1161. real_z = float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]; //get the real Z (since the auto bed leveling is already correcting the plane)
  1162. x_tmp = current_position[X_AXIS] + X_PROBE_OFFSET_FROM_EXTRUDER;
  1163. y_tmp = current_position[Y_AXIS] + Y_PROBE_OFFSET_FROM_EXTRUDER;
  1164. z_tmp = current_position[Z_AXIS];
  1165. apply_rotation_xyz(plan_bed_level_matrix, x_tmp, y_tmp, z_tmp); //Apply the correction sending the probe offset
  1166. current_position[Z_AXIS] = z_tmp - real_z + current_position[Z_AXIS]; //The difference is added to current position and sent to planner.
  1167. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1168. }
  1169. break;
  1170. case 30: // G30 Single Z Probe
  1171. {
  1172. engage_z_probe(); // Engage Z Servo endstop if available
  1173. st_synchronize();
  1174. // TODO: make sure the bed_level_rotation_matrix is identity or the planner will get set incorectly
  1175. setup_for_endstop_move();
  1176. feedrate = homing_feedrate[Z_AXIS];
  1177. run_z_probe();
  1178. SERIAL_PROTOCOLPGM("Bed Position X: ");
  1179. SERIAL_PROTOCOL(current_position[X_AXIS]);
  1180. SERIAL_PROTOCOLPGM(" Y: ");
  1181. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  1182. SERIAL_PROTOCOLPGM(" Z: ");
  1183. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  1184. SERIAL_PROTOCOLPGM("\n");
  1185. clean_up_after_endstop_move();
  1186. retract_z_probe(); // Retract Z Servo endstop if available
  1187. }
  1188. break;
  1189. #endif // ENABLE_AUTO_BED_LEVELING
  1190. case 90: // G90
  1191. relative_mode = false;
  1192. break;
  1193. case 91: // G91
  1194. relative_mode = true;
  1195. break;
  1196. case 92: // G92
  1197. if(!code_seen(axis_codes[E_AXIS]))
  1198. st_synchronize();
  1199. for(int8_t i=0; i < NUM_AXIS; i++) {
  1200. if(code_seen(axis_codes[i])) {
  1201. if(i == E_AXIS) {
  1202. current_position[i] = code_value();
  1203. plan_set_e_position(current_position[E_AXIS]);
  1204. }
  1205. else {
  1206. current_position[i] = code_value()+add_homeing[i];
  1207. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  1208. }
  1209. }
  1210. }
  1211. break;
  1212. }
  1213. }
  1214. else if(code_seen('M'))
  1215. {
  1216. switch( (int)code_value() )
  1217. {
  1218. #ifdef ULTIPANEL
  1219. case 0: // M0 - Unconditional stop - Wait for user button press on LCD
  1220. case 1: // M1 - Conditional stop - Wait for user button press on LCD
  1221. {
  1222. LCD_MESSAGEPGM(MSG_USERWAIT);
  1223. codenum = 0;
  1224. if(code_seen('P')) codenum = code_value(); // milliseconds to wait
  1225. if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
  1226. st_synchronize();
  1227. previous_millis_cmd = millis();
  1228. if (codenum > 0){
  1229. codenum += millis(); // keep track of when we started waiting
  1230. while(millis() < codenum && !lcd_clicked()){
  1231. manage_heater();
  1232. manage_inactivity();
  1233. lcd_update();
  1234. }
  1235. }else{
  1236. while(!lcd_clicked()){
  1237. manage_heater();
  1238. manage_inactivity();
  1239. lcd_update();
  1240. }
  1241. }
  1242. LCD_MESSAGEPGM(MSG_RESUMING);
  1243. }
  1244. break;
  1245. #endif
  1246. case 17:
  1247. LCD_MESSAGEPGM(MSG_NO_MOVE);
  1248. enable_x();
  1249. enable_y();
  1250. enable_z();
  1251. enable_e0();
  1252. enable_e1();
  1253. enable_e2();
  1254. break;
  1255. #ifdef SDSUPPORT
  1256. case 20: // M20 - list SD card
  1257. SERIAL_PROTOCOLLNPGM(MSG_BEGIN_FILE_LIST);
  1258. card.ls();
  1259. SERIAL_PROTOCOLLNPGM(MSG_END_FILE_LIST);
  1260. break;
  1261. case 21: // M21 - init SD card
  1262. card.initsd();
  1263. break;
  1264. case 22: //M22 - release SD card
  1265. card.release();
  1266. break;
  1267. case 23: //M23 - Select file
  1268. starpos = (strchr(strchr_pointer + 4,'*'));
  1269. if(starpos!=NULL)
  1270. *(starpos-1)='\0';
  1271. card.openFile(strchr_pointer + 4,true);
  1272. break;
  1273. case 24: //M24 - Start SD print
  1274. card.startFileprint();
  1275. starttime=millis();
  1276. break;
  1277. case 25: //M25 - Pause SD print
  1278. card.pauseSDPrint();
  1279. break;
  1280. case 26: //M26 - Set SD index
  1281. if(card.cardOK && code_seen('S')) {
  1282. card.setIndex(code_value_long());
  1283. }
  1284. break;
  1285. case 27: //M27 - Get SD status
  1286. card.getStatus();
  1287. break;
  1288. case 28: //M28 - Start SD write
  1289. starpos = (strchr(strchr_pointer + 4,'*'));
  1290. if(starpos != NULL){
  1291. char* npos = strchr(cmdbuffer[bufindr], 'N');
  1292. strchr_pointer = strchr(npos,' ') + 1;
  1293. *(starpos-1) = '\0';
  1294. }
  1295. card.openFile(strchr_pointer+4,false);
  1296. break;
  1297. case 29: //M29 - Stop SD write
  1298. //processed in write to file routine above
  1299. //card,saving = false;
  1300. break;
  1301. case 30: //M30 <filename> Delete File
  1302. if (card.cardOK){
  1303. card.closefile();
  1304. starpos = (strchr(strchr_pointer + 4,'*'));
  1305. if(starpos != NULL){
  1306. char* npos = strchr(cmdbuffer[bufindr], 'N');
  1307. strchr_pointer = strchr(npos,' ') + 1;
  1308. *(starpos-1) = '\0';
  1309. }
  1310. card.removeFile(strchr_pointer + 4);
  1311. }
  1312. break;
  1313. case 32: //M32 - Select file and start SD print
  1314. {
  1315. if(card.sdprinting) {
  1316. st_synchronize();
  1317. }
  1318. starpos = (strchr(strchr_pointer + 4,'*'));
  1319. char* namestartpos = (strchr(strchr_pointer + 4,'!')); //find ! to indicate filename string start.
  1320. if(namestartpos==NULL)
  1321. {
  1322. namestartpos=strchr_pointer + 4; //default name position, 4 letters after the M
  1323. }
  1324. else
  1325. namestartpos++; //to skip the '!'
  1326. if(starpos!=NULL)
  1327. *(starpos-1)='\0';
  1328. bool call_procedure=(code_seen('P'));
  1329. if(strchr_pointer>namestartpos)
  1330. call_procedure=false; //false alert, 'P' found within filename
  1331. if( card.cardOK )
  1332. {
  1333. card.openFile(namestartpos,true,!call_procedure);
  1334. if(code_seen('S'))
  1335. if(strchr_pointer<namestartpos) //only if "S" is occuring _before_ the filename
  1336. card.setIndex(code_value_long());
  1337. card.startFileprint();
  1338. if(!call_procedure)
  1339. starttime=millis(); //procedure calls count as normal print time.
  1340. }
  1341. } break;
  1342. case 928: //M928 - Start SD write
  1343. starpos = (strchr(strchr_pointer + 5,'*'));
  1344. if(starpos != NULL){
  1345. char* npos = strchr(cmdbuffer[bufindr], 'N');
  1346. strchr_pointer = strchr(npos,' ') + 1;
  1347. *(starpos-1) = '\0';
  1348. }
  1349. card.openLogFile(strchr_pointer+5);
  1350. break;
  1351. #endif //SDSUPPORT
  1352. case 31: //M31 take time since the start of the SD print or an M109 command
  1353. {
  1354. stoptime=millis();
  1355. char time[30];
  1356. unsigned long t=(stoptime-starttime)/1000;
  1357. int sec,min;
  1358. min=t/60;
  1359. sec=t%60;
  1360. sprintf_P(time, PSTR("%i min, %i sec"), min, sec);
  1361. SERIAL_ECHO_START;
  1362. SERIAL_ECHOLN(time);
  1363. lcd_setstatus(time);
  1364. autotempShutdown();
  1365. }
  1366. break;
  1367. case 42: //M42 -Change pin status via gcode
  1368. if (code_seen('S'))
  1369. {
  1370. int pin_status = code_value();
  1371. int pin_number = LED_PIN;
  1372. if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
  1373. pin_number = code_value();
  1374. for(int8_t i = 0; i < (int8_t)sizeof(sensitive_pins); i++)
  1375. {
  1376. if (sensitive_pins[i] == pin_number)
  1377. {
  1378. pin_number = -1;
  1379. break;
  1380. }
  1381. }
  1382. #if defined(FAN_PIN) && FAN_PIN > -1
  1383. if (pin_number == FAN_PIN)
  1384. fanSpeed = pin_status;
  1385. #endif
  1386. if (pin_number > -1)
  1387. {
  1388. pinMode(pin_number, OUTPUT);
  1389. digitalWrite(pin_number, pin_status);
  1390. analogWrite(pin_number, pin_status);
  1391. }
  1392. }
  1393. break;
  1394. case 104: // M104
  1395. if(setTargetedHotend(104)){
  1396. break;
  1397. }
  1398. if (code_seen('S')) setTargetHotend(code_value(), tmp_extruder);
  1399. #ifdef DUAL_X_CARRIAGE
  1400. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && tmp_extruder == 0)
  1401. setTargetHotend1(code_value() == 0.0 ? 0.0 : code_value() + duplicate_extruder_temp_offset);
  1402. #endif
  1403. setWatch();
  1404. break;
  1405. case 140: // M140 set bed temp
  1406. if (code_seen('S')) setTargetBed(code_value());
  1407. break;
  1408. case 105 : // M105
  1409. if(setTargetedHotend(105)){
  1410. break;
  1411. }
  1412. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  1413. SERIAL_PROTOCOLPGM("ok T:");
  1414. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  1415. SERIAL_PROTOCOLPGM(" /");
  1416. SERIAL_PROTOCOL_F(degTargetHotend(tmp_extruder),1);
  1417. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  1418. SERIAL_PROTOCOLPGM(" B:");
  1419. SERIAL_PROTOCOL_F(degBed(),1);
  1420. SERIAL_PROTOCOLPGM(" /");
  1421. SERIAL_PROTOCOL_F(degTargetBed(),1);
  1422. #endif //TEMP_BED_PIN
  1423. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  1424. SERIAL_PROTOCOLPGM(" T");
  1425. SERIAL_PROTOCOL(cur_extruder);
  1426. SERIAL_PROTOCOLPGM(":");
  1427. SERIAL_PROTOCOL_F(degHotend(cur_extruder),1);
  1428. SERIAL_PROTOCOLPGM(" /");
  1429. SERIAL_PROTOCOL_F(degTargetHotend(cur_extruder),1);
  1430. }
  1431. #else
  1432. SERIAL_ERROR_START;
  1433. SERIAL_ERRORLNPGM(MSG_ERR_NO_THERMISTORS);
  1434. #endif
  1435. SERIAL_PROTOCOLPGM(" @:");
  1436. SERIAL_PROTOCOL(getHeaterPower(tmp_extruder));
  1437. SERIAL_PROTOCOLPGM(" B@:");
  1438. SERIAL_PROTOCOL(getHeaterPower(-1));
  1439. #ifdef SHOW_TEMP_ADC_VALUES
  1440. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  1441. SERIAL_PROTOCOLPGM(" ADC B:");
  1442. SERIAL_PROTOCOL_F(degBed(),1);
  1443. SERIAL_PROTOCOLPGM("C->");
  1444. SERIAL_PROTOCOL_F(rawBedTemp()/OVERSAMPLENR,0);
  1445. #endif
  1446. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  1447. SERIAL_PROTOCOLPGM(" T");
  1448. SERIAL_PROTOCOL(cur_extruder);
  1449. SERIAL_PROTOCOLPGM(":");
  1450. SERIAL_PROTOCOL_F(degHotend(cur_extruder),1);
  1451. SERIAL_PROTOCOLPGM("C->");
  1452. SERIAL_PROTOCOL_F(rawHotendTemp(cur_extruder)/OVERSAMPLENR,0);
  1453. }
  1454. #endif
  1455. SERIAL_PROTOCOLLN("");
  1456. return;
  1457. break;
  1458. case 109:
  1459. {// M109 - Wait for extruder heater to reach target.
  1460. if(setTargetedHotend(109)){
  1461. break;
  1462. }
  1463. LCD_MESSAGEPGM(MSG_HEATING);
  1464. #ifdef AUTOTEMP
  1465. autotemp_enabled=false;
  1466. #endif
  1467. if (code_seen('S')) {
  1468. setTargetHotend(code_value(), tmp_extruder);
  1469. #ifdef DUAL_X_CARRIAGE
  1470. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && tmp_extruder == 0)
  1471. setTargetHotend1(code_value() == 0.0 ? 0.0 : code_value() + duplicate_extruder_temp_offset);
  1472. #endif
  1473. CooldownNoWait = true;
  1474. } else if (code_seen('R')) {
  1475. setTargetHotend(code_value(), tmp_extruder);
  1476. #ifdef DUAL_X_CARRIAGE
  1477. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && tmp_extruder == 0)
  1478. setTargetHotend1(code_value() == 0.0 ? 0.0 : code_value() + duplicate_extruder_temp_offset);
  1479. #endif
  1480. CooldownNoWait = false;
  1481. }
  1482. #ifdef AUTOTEMP
  1483. if (code_seen('S')) autotemp_min=code_value();
  1484. if (code_seen('B')) autotemp_max=code_value();
  1485. if (code_seen('F'))
  1486. {
  1487. autotemp_factor=code_value();
  1488. autotemp_enabled=true;
  1489. }
  1490. #endif
  1491. setWatch();
  1492. codenum = millis();
  1493. /* See if we are heating up or cooling down */
  1494. target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling
  1495. #ifdef TEMP_RESIDENCY_TIME
  1496. long residencyStart;
  1497. residencyStart = -1;
  1498. /* continue to loop until we have reached the target temp
  1499. _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
  1500. while((residencyStart == -1) ||
  1501. (residencyStart >= 0 && (((unsigned int) (millis() - residencyStart)) < (TEMP_RESIDENCY_TIME * 1000UL))) ) {
  1502. #else
  1503. while ( target_direction ? (isHeatingHotend(tmp_extruder)) : (isCoolingHotend(tmp_extruder)&&(CooldownNoWait==false)) ) {
  1504. #endif //TEMP_RESIDENCY_TIME
  1505. if( (millis() - codenum) > 1000UL )
  1506. { //Print Temp Reading and remaining time every 1 second while heating up/cooling down
  1507. SERIAL_PROTOCOLPGM("T:");
  1508. SERIAL_PROTOCOL_F(degHotend(tmp_extruder),1);
  1509. SERIAL_PROTOCOLPGM(" E:");
  1510. SERIAL_PROTOCOL((int)tmp_extruder);
  1511. #ifdef TEMP_RESIDENCY_TIME
  1512. SERIAL_PROTOCOLPGM(" W:");
  1513. if(residencyStart > -1)
  1514. {
  1515. codenum = ((TEMP_RESIDENCY_TIME * 1000UL) - (millis() - residencyStart)) / 1000UL;
  1516. SERIAL_PROTOCOLLN( codenum );
  1517. }
  1518. else
  1519. {
  1520. SERIAL_PROTOCOLLN( "?" );
  1521. }
  1522. #else
  1523. SERIAL_PROTOCOLLN("");
  1524. #endif
  1525. codenum = millis();
  1526. }
  1527. manage_heater();
  1528. manage_inactivity();
  1529. lcd_update();
  1530. #ifdef TEMP_RESIDENCY_TIME
  1531. /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
  1532. or when current temp falls outside the hysteresis after target temp was reached */
  1533. if ((residencyStart == -1 && target_direction && (degHotend(tmp_extruder) >= (degTargetHotend(tmp_extruder)-TEMP_WINDOW))) ||
  1534. (residencyStart == -1 && !target_direction && (degHotend(tmp_extruder) <= (degTargetHotend(tmp_extruder)+TEMP_WINDOW))) ||
  1535. (residencyStart > -1 && labs(degHotend(tmp_extruder) - degTargetHotend(tmp_extruder)) > TEMP_HYSTERESIS) )
  1536. {
  1537. residencyStart = millis();
  1538. }
  1539. #endif //TEMP_RESIDENCY_TIME
  1540. }
  1541. LCD_MESSAGEPGM(MSG_HEATING_COMPLETE);
  1542. starttime=millis();
  1543. previous_millis_cmd = millis();
  1544. }
  1545. break;
  1546. case 190: // M190 - Wait for bed heater to reach target.
  1547. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  1548. LCD_MESSAGEPGM(MSG_BED_HEATING);
  1549. if (code_seen('S')) {
  1550. setTargetBed(code_value());
  1551. CooldownNoWait = true;
  1552. } else if (code_seen('R')) {
  1553. setTargetBed(code_value());
  1554. CooldownNoWait = false;
  1555. }
  1556. codenum = millis();
  1557. target_direction = isHeatingBed(); // true if heating, false if cooling
  1558. while ( target_direction ? (isHeatingBed()) : (isCoolingBed()&&(CooldownNoWait==false)) )
  1559. {
  1560. if(( millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
  1561. {
  1562. float tt=degHotend(active_extruder);
  1563. SERIAL_PROTOCOLPGM("T:");
  1564. SERIAL_PROTOCOL(tt);
  1565. SERIAL_PROTOCOLPGM(" E:");
  1566. SERIAL_PROTOCOL((int)active_extruder);
  1567. SERIAL_PROTOCOLPGM(" B:");
  1568. SERIAL_PROTOCOL_F(degBed(),1);
  1569. SERIAL_PROTOCOLLN("");
  1570. codenum = millis();
  1571. }
  1572. manage_heater();
  1573. manage_inactivity();
  1574. lcd_update();
  1575. }
  1576. LCD_MESSAGEPGM(MSG_BED_DONE);
  1577. previous_millis_cmd = millis();
  1578. #endif
  1579. break;
  1580. #if defined(FAN_PIN) && FAN_PIN > -1
  1581. case 106: //M106 Fan On
  1582. if (code_seen('S')){
  1583. fanSpeed=constrain(code_value(),0,255);
  1584. }
  1585. else {
  1586. fanSpeed=255;
  1587. }
  1588. break;
  1589. case 107: //M107 Fan Off
  1590. fanSpeed = 0;
  1591. break;
  1592. #endif //FAN_PIN
  1593. #ifdef BARICUDA
  1594. // PWM for HEATER_1_PIN
  1595. #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1
  1596. case 126: //M126 valve open
  1597. if (code_seen('S')){
  1598. ValvePressure=constrain(code_value(),0,255);
  1599. }
  1600. else {
  1601. ValvePressure=255;
  1602. }
  1603. break;
  1604. case 127: //M127 valve closed
  1605. ValvePressure = 0;
  1606. break;
  1607. #endif //HEATER_1_PIN
  1608. // PWM for HEATER_2_PIN
  1609. #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1
  1610. case 128: //M128 valve open
  1611. if (code_seen('S')){
  1612. EtoPPressure=constrain(code_value(),0,255);
  1613. }
  1614. else {
  1615. EtoPPressure=255;
  1616. }
  1617. break;
  1618. case 129: //M129 valve closed
  1619. EtoPPressure = 0;
  1620. break;
  1621. #endif //HEATER_2_PIN
  1622. #endif
  1623. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  1624. case 80: // M80 - Turn on Power Supply
  1625. SET_OUTPUT(PS_ON_PIN); //GND
  1626. WRITE(PS_ON_PIN, PS_ON_AWAKE);
  1627. #ifdef ULTIPANEL
  1628. powersupply = true;
  1629. LCD_MESSAGEPGM(WELCOME_MSG);
  1630. lcd_update();
  1631. #endif
  1632. break;
  1633. #endif
  1634. case 81: // M81 - Turn off Power Supply
  1635. disable_heater();
  1636. st_synchronize();
  1637. disable_e0();
  1638. disable_e1();
  1639. disable_e2();
  1640. finishAndDisableSteppers();
  1641. fanSpeed = 0;
  1642. delay(1000); // Wait a little before to switch off
  1643. #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
  1644. st_synchronize();
  1645. suicide();
  1646. #elif defined(PS_ON_PIN) && PS_ON_PIN > -1
  1647. SET_OUTPUT(PS_ON_PIN);
  1648. WRITE(PS_ON_PIN, PS_ON_ASLEEP);
  1649. #endif
  1650. #ifdef ULTIPANEL
  1651. powersupply = false;
  1652. LCD_MESSAGEPGM(MACHINE_NAME" "MSG_OFF".");
  1653. lcd_update();
  1654. #endif
  1655. break;
  1656. case 82:
  1657. axis_relative_modes[3] = false;
  1658. break;
  1659. case 83:
  1660. axis_relative_modes[3] = true;
  1661. break;
  1662. case 18: //compatibility
  1663. case 84: // M84
  1664. if(code_seen('S')){
  1665. stepper_inactive_time = code_value() * 1000;
  1666. }
  1667. else
  1668. {
  1669. bool all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2]))|| (code_seen(axis_codes[3])));
  1670. if(all_axis)
  1671. {
  1672. st_synchronize();
  1673. disable_e0();
  1674. disable_e1();
  1675. disable_e2();
  1676. finishAndDisableSteppers();
  1677. }
  1678. else
  1679. {
  1680. st_synchronize();
  1681. if(code_seen('X')) disable_x();
  1682. if(code_seen('Y')) disable_y();
  1683. if(code_seen('Z')) disable_z();
  1684. #if ((E0_ENABLE_PIN != X_ENABLE_PIN) && (E1_ENABLE_PIN != Y_ENABLE_PIN)) // Only enable on boards that have seperate ENABLE_PINS
  1685. if(code_seen('E')) {
  1686. disable_e0();
  1687. disable_e1();
  1688. disable_e2();
  1689. }
  1690. #endif
  1691. }
  1692. }
  1693. break;
  1694. case 85: // M85
  1695. code_seen('S');
  1696. max_inactive_time = code_value() * 1000;
  1697. break;
  1698. case 92: // M92
  1699. for(int8_t i=0; i < NUM_AXIS; i++)
  1700. {
  1701. if(code_seen(axis_codes[i]))
  1702. {
  1703. if(i == 3) { // E
  1704. float value = code_value();
  1705. if(value < 20.0) {
  1706. float factor = axis_steps_per_unit[i] / value; // increase e constants if M92 E14 is given for netfab.
  1707. max_e_jerk *= factor;
  1708. max_feedrate[i] *= factor;
  1709. axis_steps_per_sqr_second[i] *= factor;
  1710. }
  1711. axis_steps_per_unit[i] = value;
  1712. }
  1713. else {
  1714. axis_steps_per_unit[i] = code_value();
  1715. }
  1716. }
  1717. }
  1718. break;
  1719. case 115: // M115
  1720. SERIAL_PROTOCOLPGM(MSG_M115_REPORT);
  1721. break;
  1722. case 117: // M117 display message
  1723. starpos = (strchr(strchr_pointer + 5,'*'));
  1724. if(starpos!=NULL)
  1725. *(starpos-1)='\0';
  1726. lcd_setstatus(strchr_pointer + 5);
  1727. break;
  1728. case 114: // M114
  1729. SERIAL_PROTOCOLPGM("X:");
  1730. SERIAL_PROTOCOL(current_position[X_AXIS]);
  1731. SERIAL_PROTOCOLPGM("Y:");
  1732. SERIAL_PROTOCOL(current_position[Y_AXIS]);
  1733. SERIAL_PROTOCOLPGM("Z:");
  1734. SERIAL_PROTOCOL(current_position[Z_AXIS]);
  1735. SERIAL_PROTOCOLPGM("E:");
  1736. SERIAL_PROTOCOL(current_position[E_AXIS]);
  1737. SERIAL_PROTOCOLPGM(MSG_COUNT_X);
  1738. SERIAL_PROTOCOL(float(st_get_position(X_AXIS))/axis_steps_per_unit[X_AXIS]);
  1739. SERIAL_PROTOCOLPGM("Y:");
  1740. SERIAL_PROTOCOL(float(st_get_position(Y_AXIS))/axis_steps_per_unit[Y_AXIS]);
  1741. SERIAL_PROTOCOLPGM("Z:");
  1742. SERIAL_PROTOCOL(float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS]);
  1743. SERIAL_PROTOCOLLN("");
  1744. break;
  1745. case 120: // M120
  1746. enable_endstops(false) ;
  1747. break;
  1748. case 121: // M121
  1749. enable_endstops(true) ;
  1750. break;
  1751. case 119: // M119
  1752. SERIAL_PROTOCOLLN(MSG_M119_REPORT);
  1753. #if defined(X_MIN_PIN) && X_MIN_PIN > -1
  1754. SERIAL_PROTOCOLPGM(MSG_X_MIN);
  1755. SERIAL_PROTOCOLLN(((READ(X_MIN_PIN)^X_MIN_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1756. #endif
  1757. #if defined(X_MAX_PIN) && X_MAX_PIN > -1
  1758. SERIAL_PROTOCOLPGM(MSG_X_MAX);
  1759. SERIAL_PROTOCOLLN(((READ(X_MAX_PIN)^X_MAX_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1760. #endif
  1761. #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
  1762. SERIAL_PROTOCOLPGM(MSG_Y_MIN);
  1763. SERIAL_PROTOCOLLN(((READ(Y_MIN_PIN)^Y_MIN_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1764. #endif
  1765. #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
  1766. SERIAL_PROTOCOLPGM(MSG_Y_MAX);
  1767. SERIAL_PROTOCOLLN(((READ(Y_MAX_PIN)^Y_MAX_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1768. #endif
  1769. #if defined(Z_MIN_PIN) && Z_MIN_PIN > -1
  1770. SERIAL_PROTOCOLPGM(MSG_Z_MIN);
  1771. SERIAL_PROTOCOLLN(((READ(Z_MIN_PIN)^Z_MIN_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1772. #endif
  1773. #if defined(Z_MAX_PIN) && Z_MAX_PIN > -1
  1774. SERIAL_PROTOCOLPGM(MSG_Z_MAX);
  1775. SERIAL_PROTOCOLLN(((READ(Z_MAX_PIN)^Z_MAX_ENDSTOP_INVERTING)?MSG_ENDSTOP_HIT:MSG_ENDSTOP_OPEN));
  1776. #endif
  1777. break;
  1778. //TODO: update for all axis, use for loop
  1779. #ifdef BLINKM
  1780. case 150: // M150
  1781. {
  1782. byte red;
  1783. byte grn;
  1784. byte blu;
  1785. if(code_seen('R')) red = code_value();
  1786. if(code_seen('U')) grn = code_value();
  1787. if(code_seen('B')) blu = code_value();
  1788. SendColors(red,grn,blu);
  1789. }
  1790. break;
  1791. #endif //BLINKM
  1792. case 201: // M201
  1793. for(int8_t i=0; i < NUM_AXIS; i++)
  1794. {
  1795. if(code_seen(axis_codes[i]))
  1796. {
  1797. max_acceleration_units_per_sq_second[i] = code_value();
  1798. }
  1799. }
  1800. // 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)
  1801. reset_acceleration_rates();
  1802. break;
  1803. #if 0 // Not used for Sprinter/grbl gen6
  1804. case 202: // M202
  1805. for(int8_t i=0; i < NUM_AXIS; i++) {
  1806. if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
  1807. }
  1808. break;
  1809. #endif
  1810. case 203: // M203 max feedrate mm/sec
  1811. for(int8_t i=0; i < NUM_AXIS; i++) {
  1812. if(code_seen(axis_codes[i])) max_feedrate[i] = code_value();
  1813. }
  1814. break;
  1815. case 204: // M204 acclereration S normal moves T filmanent only moves
  1816. {
  1817. if(code_seen('S')) acceleration = code_value() ;
  1818. if(code_seen('T')) retract_acceleration = code_value() ;
  1819. }
  1820. break;
  1821. 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
  1822. {
  1823. if(code_seen('S')) minimumfeedrate = code_value();
  1824. if(code_seen('T')) mintravelfeedrate = code_value();
  1825. if(code_seen('B')) minsegmenttime = code_value() ;
  1826. if(code_seen('X')) max_xy_jerk = code_value() ;
  1827. if(code_seen('Z')) max_z_jerk = code_value() ;
  1828. if(code_seen('E')) max_e_jerk = code_value() ;
  1829. }
  1830. break;
  1831. case 206: // M206 additional homeing offset
  1832. for(int8_t i=0; i < 3; i++)
  1833. {
  1834. if(code_seen(axis_codes[i])) add_homeing[i] = code_value();
  1835. }
  1836. break;
  1837. #ifdef DELTA
  1838. case 666: // M666 set delta endstop adjustemnt
  1839. for(int8_t i=0; i < 3; i++)
  1840. {
  1841. if(code_seen(axis_codes[i])) endstop_adj[i] = code_value();
  1842. }
  1843. break;
  1844. #endif
  1845. #ifdef FWRETRACT
  1846. case 207: //M207 - set retract length S[positive mm] F[feedrate mm/sec] Z[additional zlift/hop]
  1847. {
  1848. if(code_seen('S'))
  1849. {
  1850. retract_length = code_value() ;
  1851. }
  1852. if(code_seen('F'))
  1853. {
  1854. retract_feedrate = code_value() ;
  1855. }
  1856. if(code_seen('Z'))
  1857. {
  1858. retract_zlift = code_value() ;
  1859. }
  1860. }break;
  1861. case 208: // M208 - set retract recover length S[positive mm surplus to the M207 S*] F[feedrate mm/sec]
  1862. {
  1863. if(code_seen('S'))
  1864. {
  1865. retract_recover_length = code_value() ;
  1866. }
  1867. if(code_seen('F'))
  1868. {
  1869. retract_recover_feedrate = code_value() ;
  1870. }
  1871. }break;
  1872. 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.
  1873. {
  1874. if(code_seen('S'))
  1875. {
  1876. int t= code_value() ;
  1877. switch(t)
  1878. {
  1879. case 0: autoretract_enabled=false;retracted=false;break;
  1880. case 1: autoretract_enabled=true;retracted=false;break;
  1881. default:
  1882. SERIAL_ECHO_START;
  1883. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  1884. SERIAL_ECHO(cmdbuffer[bufindr]);
  1885. SERIAL_ECHOLNPGM("\"");
  1886. }
  1887. }
  1888. }break;
  1889. #endif // FWRETRACT
  1890. #if EXTRUDERS > 1
  1891. case 218: // M218 - set hotend offset (in mm), T<extruder_number> X<offset_on_X> Y<offset_on_Y>
  1892. {
  1893. if(setTargetedHotend(218)){
  1894. break;
  1895. }
  1896. if(code_seen('X'))
  1897. {
  1898. extruder_offset[X_AXIS][tmp_extruder] = code_value();
  1899. }
  1900. if(code_seen('Y'))
  1901. {
  1902. extruder_offset[Y_AXIS][tmp_extruder] = code_value();
  1903. }
  1904. #ifdef DUAL_X_CARRIAGE
  1905. if(code_seen('Z'))
  1906. {
  1907. extruder_offset[Z_AXIS][tmp_extruder] = code_value();
  1908. }
  1909. #endif
  1910. SERIAL_ECHO_START;
  1911. SERIAL_ECHOPGM(MSG_HOTEND_OFFSET);
  1912. for(tmp_extruder = 0; tmp_extruder < EXTRUDERS; tmp_extruder++)
  1913. {
  1914. SERIAL_ECHO(" ");
  1915. SERIAL_ECHO(extruder_offset[X_AXIS][tmp_extruder]);
  1916. SERIAL_ECHO(",");
  1917. SERIAL_ECHO(extruder_offset[Y_AXIS][tmp_extruder]);
  1918. #ifdef DUAL_X_CARRIAGE
  1919. SERIAL_ECHO(",");
  1920. SERIAL_ECHO(extruder_offset[Z_AXIS][tmp_extruder]);
  1921. #endif
  1922. }
  1923. SERIAL_ECHOLN("");
  1924. }break;
  1925. #endif
  1926. case 220: // M220 S<factor in percent>- set speed factor override percentage
  1927. {
  1928. if(code_seen('S'))
  1929. {
  1930. feedmultiply = code_value() ;
  1931. }
  1932. }
  1933. break;
  1934. case 221: // M221 S<factor in percent>- set extrude factor override percentage
  1935. {
  1936. if(code_seen('S'))
  1937. {
  1938. extrudemultiply = code_value() ;
  1939. }
  1940. }
  1941. break;
  1942. #if NUM_SERVOS > 0
  1943. case 280: // M280 - set servo position absolute. P: servo index, S: angle or microseconds
  1944. {
  1945. int servo_index = -1;
  1946. int servo_position = 0;
  1947. if (code_seen('P'))
  1948. servo_index = code_value();
  1949. if (code_seen('S')) {
  1950. servo_position = code_value();
  1951. if ((servo_index >= 0) && (servo_index < NUM_SERVOS)) {
  1952. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  1953. servos[servo_index].attach(0);
  1954. #endif
  1955. servos[servo_index].write(servo_position);
  1956. #if defined (ENABLE_AUTO_BED_LEVELING) && (PROBE_SERVO_DEACTIVATION_DELAY > 0)
  1957. delay(PROBE_SERVO_DEACTIVATION_DELAY);
  1958. servos[servo_index].detach();
  1959. #endif
  1960. }
  1961. else {
  1962. SERIAL_ECHO_START;
  1963. SERIAL_ECHO("Servo ");
  1964. SERIAL_ECHO(servo_index);
  1965. SERIAL_ECHOLN(" out of range");
  1966. }
  1967. }
  1968. else if (servo_index >= 0) {
  1969. SERIAL_PROTOCOL(MSG_OK);
  1970. SERIAL_PROTOCOL(" Servo ");
  1971. SERIAL_PROTOCOL(servo_index);
  1972. SERIAL_PROTOCOL(": ");
  1973. SERIAL_PROTOCOL(servos[servo_index].read());
  1974. SERIAL_PROTOCOLLN("");
  1975. }
  1976. }
  1977. break;
  1978. #endif // NUM_SERVOS > 0
  1979. #if LARGE_FLASH == true && ( BEEPER > 0 || defined(ULTRALCD) )
  1980. case 300: // M300
  1981. {
  1982. int beepS = code_seen('S') ? code_value() : 110;
  1983. int beepP = code_seen('P') ? code_value() : 1000;
  1984. if (beepS > 0)
  1985. {
  1986. #if BEEPER > 0
  1987. tone(BEEPER, beepS);
  1988. delay(beepP);
  1989. noTone(BEEPER);
  1990. #elif defined(ULTRALCD)
  1991. lcd_buzz(beepS, beepP);
  1992. #endif
  1993. }
  1994. else
  1995. {
  1996. delay(beepP);
  1997. }
  1998. }
  1999. break;
  2000. #endif // M300
  2001. #ifdef PIDTEMP
  2002. case 301: // M301
  2003. {
  2004. if(code_seen('P')) Kp = code_value();
  2005. if(code_seen('I')) Ki = scalePID_i(code_value());
  2006. if(code_seen('D')) Kd = scalePID_d(code_value());
  2007. #ifdef PID_ADD_EXTRUSION_RATE
  2008. if(code_seen('C')) Kc = code_value();
  2009. #endif
  2010. updatePID();
  2011. SERIAL_PROTOCOL(MSG_OK);
  2012. SERIAL_PROTOCOL(" p:");
  2013. SERIAL_PROTOCOL(Kp);
  2014. SERIAL_PROTOCOL(" i:");
  2015. SERIAL_PROTOCOL(unscalePID_i(Ki));
  2016. SERIAL_PROTOCOL(" d:");
  2017. SERIAL_PROTOCOL(unscalePID_d(Kd));
  2018. #ifdef PID_ADD_EXTRUSION_RATE
  2019. SERIAL_PROTOCOL(" c:");
  2020. //Kc does not have scaling applied above, or in resetting defaults
  2021. SERIAL_PROTOCOL(Kc);
  2022. #endif
  2023. SERIAL_PROTOCOLLN("");
  2024. }
  2025. break;
  2026. #endif //PIDTEMP
  2027. #ifdef PIDTEMPBED
  2028. case 304: // M304
  2029. {
  2030. if(code_seen('P')) bedKp = code_value();
  2031. if(code_seen('I')) bedKi = scalePID_i(code_value());
  2032. if(code_seen('D')) bedKd = scalePID_d(code_value());
  2033. updatePID();
  2034. SERIAL_PROTOCOL(MSG_OK);
  2035. SERIAL_PROTOCOL(" p:");
  2036. SERIAL_PROTOCOL(bedKp);
  2037. SERIAL_PROTOCOL(" i:");
  2038. SERIAL_PROTOCOL(unscalePID_i(bedKi));
  2039. SERIAL_PROTOCOL(" d:");
  2040. SERIAL_PROTOCOL(unscalePID_d(bedKd));
  2041. SERIAL_PROTOCOLLN("");
  2042. }
  2043. break;
  2044. #endif //PIDTEMP
  2045. case 240: // M240 Triggers a camera by emulating a Canon RC-1 : http://www.doc-diy.net/photo/rc-1_hacked/
  2046. {
  2047. #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
  2048. const uint8_t NUM_PULSES=16;
  2049. const float PULSE_LENGTH=0.01524;
  2050. for(int i=0; i < NUM_PULSES; i++) {
  2051. WRITE(PHOTOGRAPH_PIN, HIGH);
  2052. _delay_ms(PULSE_LENGTH);
  2053. WRITE(PHOTOGRAPH_PIN, LOW);
  2054. _delay_ms(PULSE_LENGTH);
  2055. }
  2056. delay(7.33);
  2057. for(int i=0; i < NUM_PULSES; i++) {
  2058. WRITE(PHOTOGRAPH_PIN, HIGH);
  2059. _delay_ms(PULSE_LENGTH);
  2060. WRITE(PHOTOGRAPH_PIN, LOW);
  2061. _delay_ms(PULSE_LENGTH);
  2062. }
  2063. #endif
  2064. }
  2065. break;
  2066. #ifdef DOGLCD
  2067. case 250: // M250 Set LCD contrast value: C<value> (value 0..63)
  2068. {
  2069. if (code_seen('C')) {
  2070. lcd_setcontrast( ((int)code_value())&63 );
  2071. }
  2072. SERIAL_PROTOCOLPGM("lcd contrast value: ");
  2073. SERIAL_PROTOCOL(lcd_contrast);
  2074. SERIAL_PROTOCOLLN("");
  2075. }
  2076. break;
  2077. #endif
  2078. #ifdef PREVENT_DANGEROUS_EXTRUDE
  2079. case 302: // allow cold extrudes, or set the minimum extrude temperature
  2080. {
  2081. float temp = .0;
  2082. if (code_seen('S')) temp=code_value();
  2083. set_extrude_min_temp(temp);
  2084. }
  2085. break;
  2086. #endif
  2087. case 303: // M303 PID autotune
  2088. {
  2089. float temp = 150.0;
  2090. int e=0;
  2091. int c=5;
  2092. if (code_seen('E')) e=code_value();
  2093. if (e<0)
  2094. temp=70;
  2095. if (code_seen('S')) temp=code_value();
  2096. if (code_seen('C')) c=code_value();
  2097. PID_autotune(temp, e, c);
  2098. }
  2099. break;
  2100. case 400: // M400 finish all moves
  2101. {
  2102. st_synchronize();
  2103. }
  2104. break;
  2105. #if defined(ENABLE_AUTO_BED_LEVELING) && defined(SERVO_ENDSTOPS)
  2106. case 401:
  2107. {
  2108. engage_z_probe(); // Engage Z Servo endstop if available
  2109. }
  2110. break;
  2111. case 402:
  2112. {
  2113. retract_z_probe(); // Retract Z Servo endstop if enabled
  2114. }
  2115. break;
  2116. #endif
  2117. case 500: // M500 Store settings in EEPROM
  2118. {
  2119. Config_StoreSettings();
  2120. }
  2121. break;
  2122. case 501: // M501 Read settings from EEPROM
  2123. {
  2124. Config_RetrieveSettings();
  2125. }
  2126. break;
  2127. case 502: // M502 Revert to default settings
  2128. {
  2129. Config_ResetDefault();
  2130. }
  2131. break;
  2132. case 503: // M503 print settings currently in memory
  2133. {
  2134. Config_PrintSettings();
  2135. }
  2136. break;
  2137. #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
  2138. case 540:
  2139. {
  2140. if(code_seen('S')) abort_on_endstop_hit = code_value() > 0;
  2141. }
  2142. break;
  2143. #endif
  2144. #ifdef FILAMENTCHANGEENABLE
  2145. case 600: //Pause for filament change X[pos] Y[pos] Z[relative lift] E[initial retract] L[later retract distance for removal]
  2146. {
  2147. float target[4];
  2148. float lastpos[4];
  2149. target[X_AXIS]=current_position[X_AXIS];
  2150. target[Y_AXIS]=current_position[Y_AXIS];
  2151. target[Z_AXIS]=current_position[Z_AXIS];
  2152. target[E_AXIS]=current_position[E_AXIS];
  2153. lastpos[X_AXIS]=current_position[X_AXIS];
  2154. lastpos[Y_AXIS]=current_position[Y_AXIS];
  2155. lastpos[Z_AXIS]=current_position[Z_AXIS];
  2156. lastpos[E_AXIS]=current_position[E_AXIS];
  2157. //retract by E
  2158. if(code_seen('E'))
  2159. {
  2160. target[E_AXIS]+= code_value();
  2161. }
  2162. else
  2163. {
  2164. #ifdef FILAMENTCHANGE_FIRSTRETRACT
  2165. target[E_AXIS]+= FILAMENTCHANGE_FIRSTRETRACT ;
  2166. #endif
  2167. }
  2168. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder);
  2169. //lift Z
  2170. if(code_seen('Z'))
  2171. {
  2172. target[Z_AXIS]+= code_value();
  2173. }
  2174. else
  2175. {
  2176. #ifdef FILAMENTCHANGE_ZADD
  2177. target[Z_AXIS]+= FILAMENTCHANGE_ZADD ;
  2178. #endif
  2179. }
  2180. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder);
  2181. //move xy
  2182. if(code_seen('X'))
  2183. {
  2184. target[X_AXIS]+= code_value();
  2185. }
  2186. else
  2187. {
  2188. #ifdef FILAMENTCHANGE_XPOS
  2189. target[X_AXIS]= FILAMENTCHANGE_XPOS ;
  2190. #endif
  2191. }
  2192. if(code_seen('Y'))
  2193. {
  2194. target[Y_AXIS]= code_value();
  2195. }
  2196. else
  2197. {
  2198. #ifdef FILAMENTCHANGE_YPOS
  2199. target[Y_AXIS]= FILAMENTCHANGE_YPOS ;
  2200. #endif
  2201. }
  2202. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder);
  2203. if(code_seen('L'))
  2204. {
  2205. target[E_AXIS]+= code_value();
  2206. }
  2207. else
  2208. {
  2209. #ifdef FILAMENTCHANGE_FINALRETRACT
  2210. target[E_AXIS]+= FILAMENTCHANGE_FINALRETRACT ;
  2211. #endif
  2212. }
  2213. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder);
  2214. //finish moves
  2215. st_synchronize();
  2216. //disable extruder steppers so filament can be removed
  2217. disable_e0();
  2218. disable_e1();
  2219. disable_e2();
  2220. delay(100);
  2221. LCD_ALERTMESSAGEPGM(MSG_FILAMENTCHANGE);
  2222. uint8_t cnt=0;
  2223. while(!lcd_clicked()){
  2224. cnt++;
  2225. manage_heater();
  2226. manage_inactivity();
  2227. lcd_update();
  2228. if(cnt==0)
  2229. {
  2230. #if BEEPER > 0
  2231. SET_OUTPUT(BEEPER);
  2232. WRITE(BEEPER,HIGH);
  2233. delay(3);
  2234. WRITE(BEEPER,LOW);
  2235. delay(3);
  2236. #else
  2237. lcd_buzz(1000/6,100);
  2238. #endif
  2239. }
  2240. }
  2241. //return to normal
  2242. if(code_seen('L'))
  2243. {
  2244. target[E_AXIS]+= -code_value();
  2245. }
  2246. else
  2247. {
  2248. #ifdef FILAMENTCHANGE_FINALRETRACT
  2249. target[E_AXIS]+=(-1)*FILAMENTCHANGE_FINALRETRACT ;
  2250. #endif
  2251. }
  2252. current_position[E_AXIS]=target[E_AXIS]; //the long retract of L is compensated by manual filament feeding
  2253. plan_set_e_position(current_position[E_AXIS]);
  2254. plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder); //should do nothing
  2255. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], target[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder); //move xy back
  2256. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], target[E_AXIS], feedrate/60, active_extruder); //move z back
  2257. plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], lastpos[E_AXIS], feedrate/60, active_extruder); //final untretract
  2258. }
  2259. break;
  2260. #endif //FILAMENTCHANGEENABLE
  2261. #ifdef DUAL_X_CARRIAGE
  2262. case 605: // Set dual x-carriage movement mode:
  2263. // M605 S0: Full control mode. The slicer has full control over x-carriage movement
  2264. // M605 S1: Auto-park mode. The inactive head will auto park/unpark without slicer involvement
  2265. // M605 S2 [Xnnn] [Rmmm]: Duplication mode. The second extruder will duplicate the first with nnn
  2266. // millimeters x-offset and an optional differential hotend temperature of
  2267. // mmm degrees. E.g., with "M605 S2 X100 R2" the second extruder will duplicate
  2268. // the first with a spacing of 100mm in the x direction and 2 degrees hotter.
  2269. //
  2270. // Note: the X axis should be homed after changing dual x-carriage mode.
  2271. {
  2272. st_synchronize();
  2273. if (code_seen('S'))
  2274. dual_x_carriage_mode = code_value();
  2275. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE)
  2276. {
  2277. if (code_seen('X'))
  2278. duplicate_extruder_x_offset = max(code_value(),X2_MIN_POS - x_home_pos(0));
  2279. if (code_seen('R'))
  2280. duplicate_extruder_temp_offset = code_value();
  2281. SERIAL_ECHO_START;
  2282. SERIAL_ECHOPGM(MSG_HOTEND_OFFSET);
  2283. SERIAL_ECHO(" ");
  2284. SERIAL_ECHO(extruder_offset[X_AXIS][0]);
  2285. SERIAL_ECHO(",");
  2286. SERIAL_ECHO(extruder_offset[Y_AXIS][0]);
  2287. SERIAL_ECHO(" ");
  2288. SERIAL_ECHO(duplicate_extruder_x_offset);
  2289. SERIAL_ECHO(",");
  2290. SERIAL_ECHOLN(extruder_offset[Y_AXIS][1]);
  2291. }
  2292. else if (dual_x_carriage_mode != DXC_FULL_CONTROL_MODE && dual_x_carriage_mode != DXC_AUTO_PARK_MODE)
  2293. {
  2294. dual_x_carriage_mode = DEFAULT_DUAL_X_CARRIAGE_MODE;
  2295. }
  2296. active_extruder_parked = false;
  2297. extruder_duplication_enabled = false;
  2298. delayed_move_time = 0;
  2299. }
  2300. break;
  2301. #endif //DUAL_X_CARRIAGE
  2302. case 907: // M907 Set digital trimpot motor current using axis codes.
  2303. {
  2304. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  2305. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) digipot_current(i,code_value());
  2306. if(code_seen('B')) digipot_current(4,code_value());
  2307. if(code_seen('S')) for(int i=0;i<=4;i++) digipot_current(i,code_value());
  2308. #endif
  2309. }
  2310. break;
  2311. case 908: // M908 Control digital trimpot directly.
  2312. {
  2313. #if defined(DIGIPOTSS_PIN) && DIGIPOTSS_PIN > -1
  2314. uint8_t channel,current;
  2315. if(code_seen('P')) channel=code_value();
  2316. if(code_seen('S')) current=code_value();
  2317. digitalPotWrite(channel, current);
  2318. #endif
  2319. }
  2320. break;
  2321. case 350: // M350 Set microstepping mode. Warning: Steps per unit remains unchanged. S code sets stepping mode for all drivers.
  2322. {
  2323. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  2324. if(code_seen('S')) for(int i=0;i<=4;i++) microstep_mode(i,code_value());
  2325. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_mode(i,(uint8_t)code_value());
  2326. if(code_seen('B')) microstep_mode(4,code_value());
  2327. microstep_readings();
  2328. #endif
  2329. }
  2330. break;
  2331. case 351: // M351 Toggle MS1 MS2 pins directly, S# determines MS1 or MS2, X# sets the pin high/low.
  2332. {
  2333. #if defined(X_MS1_PIN) && X_MS1_PIN > -1
  2334. if(code_seen('S')) switch((int)code_value())
  2335. {
  2336. case 1:
  2337. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,code_value(),-1);
  2338. if(code_seen('B')) microstep_ms(4,code_value(),-1);
  2339. break;
  2340. case 2:
  2341. for(int i=0;i<NUM_AXIS;i++) if(code_seen(axis_codes[i])) microstep_ms(i,-1,code_value());
  2342. if(code_seen('B')) microstep_ms(4,-1,code_value());
  2343. break;
  2344. }
  2345. microstep_readings();
  2346. #endif
  2347. }
  2348. break;
  2349. case 999: // M999: Restart after being stopped
  2350. Stopped = false;
  2351. lcd_reset_alert_level();
  2352. gcode_LastN = Stopped_gcode_LastN;
  2353. FlushSerialRequestResend();
  2354. break;
  2355. }
  2356. }
  2357. else if(code_seen('T'))
  2358. {
  2359. tmp_extruder = code_value();
  2360. if(tmp_extruder >= EXTRUDERS) {
  2361. SERIAL_ECHO_START;
  2362. SERIAL_ECHO("T");
  2363. SERIAL_ECHO(tmp_extruder);
  2364. SERIAL_ECHOLN(MSG_INVALID_EXTRUDER);
  2365. }
  2366. else {
  2367. boolean make_move = false;
  2368. if(code_seen('F')) {
  2369. make_move = true;
  2370. next_feedrate = code_value();
  2371. if(next_feedrate > 0.0) {
  2372. feedrate = next_feedrate;
  2373. }
  2374. }
  2375. #if EXTRUDERS > 1
  2376. if(tmp_extruder != active_extruder) {
  2377. // Save current position to return to after applying extruder offset
  2378. memcpy(destination, current_position, sizeof(destination));
  2379. #ifdef DUAL_X_CARRIAGE
  2380. if (dual_x_carriage_mode == DXC_AUTO_PARK_MODE && Stopped == false &&
  2381. (delayed_move_time != 0 || current_position[X_AXIS] != x_home_pos(active_extruder)))
  2382. {
  2383. // Park old head: 1) raise 2) move to park position 3) lower
  2384. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS] + TOOLCHANGE_PARK_ZLIFT,
  2385. current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  2386. plan_buffer_line(x_home_pos(active_extruder), current_position[Y_AXIS], current_position[Z_AXIS] + TOOLCHANGE_PARK_ZLIFT,
  2387. current_position[E_AXIS], max_feedrate[X_AXIS], active_extruder);
  2388. plan_buffer_line(x_home_pos(active_extruder), current_position[Y_AXIS], current_position[Z_AXIS],
  2389. current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  2390. st_synchronize();
  2391. }
  2392. // apply Y & Z extruder offset (x offset is already used in determining home pos)
  2393. current_position[Y_AXIS] = current_position[Y_AXIS] -
  2394. extruder_offset[Y_AXIS][active_extruder] +
  2395. extruder_offset[Y_AXIS][tmp_extruder];
  2396. current_position[Z_AXIS] = current_position[Z_AXIS] -
  2397. extruder_offset[Z_AXIS][active_extruder] +
  2398. extruder_offset[Z_AXIS][tmp_extruder];
  2399. active_extruder = tmp_extruder;
  2400. // This function resets the max/min values - the current position may be overwritten below.
  2401. axis_is_at_home(X_AXIS);
  2402. if (dual_x_carriage_mode == DXC_FULL_CONTROL_MODE)
  2403. {
  2404. current_position[X_AXIS] = inactive_extruder_x_pos;
  2405. inactive_extruder_x_pos = destination[X_AXIS];
  2406. }
  2407. else if (dual_x_carriage_mode == DXC_DUPLICATION_MODE)
  2408. {
  2409. active_extruder_parked = (active_extruder == 0); // this triggers the second extruder to move into the duplication position
  2410. if (active_extruder == 0 || active_extruder_parked)
  2411. current_position[X_AXIS] = inactive_extruder_x_pos;
  2412. else
  2413. current_position[X_AXIS] = destination[X_AXIS] + duplicate_extruder_x_offset;
  2414. inactive_extruder_x_pos = destination[X_AXIS];
  2415. extruder_duplication_enabled = false;
  2416. }
  2417. else
  2418. {
  2419. // record raised toolhead position for use by unpark
  2420. memcpy(raised_parked_position, current_position, sizeof(raised_parked_position));
  2421. raised_parked_position[Z_AXIS] += TOOLCHANGE_UNPARK_ZLIFT;
  2422. active_extruder_parked = true;
  2423. delayed_move_time = 0;
  2424. }
  2425. #else
  2426. // Offset extruder (only by XY)
  2427. int i;
  2428. for(i = 0; i < 2; i++) {
  2429. current_position[i] = current_position[i] -
  2430. extruder_offset[i][active_extruder] +
  2431. extruder_offset[i][tmp_extruder];
  2432. }
  2433. // Set the new active extruder and position
  2434. active_extruder = tmp_extruder;
  2435. #endif //else DUAL_X_CARRIAGE
  2436. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2437. // Move to the old position if 'F' was in the parameters
  2438. if(make_move && Stopped == false) {
  2439. prepare_move();
  2440. }
  2441. }
  2442. #endif
  2443. SERIAL_ECHO_START;
  2444. SERIAL_ECHO(MSG_ACTIVE_EXTRUDER);
  2445. SERIAL_PROTOCOLLN((int)active_extruder);
  2446. }
  2447. }
  2448. else
  2449. {
  2450. SERIAL_ECHO_START;
  2451. SERIAL_ECHOPGM(MSG_UNKNOWN_COMMAND);
  2452. SERIAL_ECHO(cmdbuffer[bufindr]);
  2453. SERIAL_ECHOLNPGM("\"");
  2454. }
  2455. ClearToSend();
  2456. }
  2457. void FlushSerialRequestResend()
  2458. {
  2459. //char cmdbuffer[bufindr][100]="Resend:";
  2460. MYSERIAL.flush();
  2461. SERIAL_PROTOCOLPGM(MSG_RESEND);
  2462. SERIAL_PROTOCOLLN(gcode_LastN + 1);
  2463. ClearToSend();
  2464. }
  2465. void ClearToSend()
  2466. {
  2467. previous_millis_cmd = millis();
  2468. #ifdef SDSUPPORT
  2469. if(fromsd[bufindr])
  2470. return;
  2471. #endif //SDSUPPORT
  2472. SERIAL_PROTOCOLLNPGM(MSG_OK);
  2473. }
  2474. void get_coordinates()
  2475. {
  2476. bool seen[4]={false,false,false,false};
  2477. for(int8_t i=0; i < NUM_AXIS; i++) {
  2478. if(code_seen(axis_codes[i]))
  2479. {
  2480. destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
  2481. seen[i]=true;
  2482. }
  2483. else destination[i] = current_position[i]; //Are these else lines really needed?
  2484. }
  2485. if(code_seen('F')) {
  2486. next_feedrate = code_value();
  2487. if(next_feedrate > 0.0) feedrate = next_feedrate;
  2488. }
  2489. #ifdef FWRETRACT
  2490. if(autoretract_enabled)
  2491. if( !(seen[X_AXIS] || seen[Y_AXIS] || seen[Z_AXIS]) && seen[E_AXIS])
  2492. {
  2493. float echange=destination[E_AXIS]-current_position[E_AXIS];
  2494. if(echange<-MIN_RETRACT) //retract
  2495. {
  2496. if(!retracted)
  2497. {
  2498. destination[Z_AXIS]+=retract_zlift; //not sure why chaninging current_position negatively does not work.
  2499. //if slicer retracted by echange=-1mm and you want to retract 3mm, corrrectede=-2mm additionally
  2500. float correctede=-echange-retract_length;
  2501. //to generate the additional steps, not the destination is changed, but inversely the current position
  2502. current_position[E_AXIS]+=-correctede;
  2503. feedrate=retract_feedrate;
  2504. retracted=true;
  2505. }
  2506. }
  2507. else
  2508. if(echange>MIN_RETRACT) //retract_recover
  2509. {
  2510. if(retracted)
  2511. {
  2512. //current_position[Z_AXIS]+=-retract_zlift;
  2513. //if slicer retracted_recovered by echange=+1mm and you want to retract_recover 3mm, corrrectede=2mm additionally
  2514. float correctede=-echange+1*retract_length+retract_recover_length; //total unretract=retract_length+retract_recover_length[surplus]
  2515. current_position[E_AXIS]+=correctede; //to generate the additional steps, not the destination is changed, but inversely the current position
  2516. feedrate=retract_recover_feedrate;
  2517. retracted=false;
  2518. }
  2519. }
  2520. }
  2521. #endif //FWRETRACT
  2522. }
  2523. void get_arc_coordinates()
  2524. {
  2525. #ifdef SF_ARC_FIX
  2526. bool relative_mode_backup = relative_mode;
  2527. relative_mode = true;
  2528. #endif
  2529. get_coordinates();
  2530. #ifdef SF_ARC_FIX
  2531. relative_mode=relative_mode_backup;
  2532. #endif
  2533. if(code_seen('I')) {
  2534. offset[0] = code_value();
  2535. }
  2536. else {
  2537. offset[0] = 0.0;
  2538. }
  2539. if(code_seen('J')) {
  2540. offset[1] = code_value();
  2541. }
  2542. else {
  2543. offset[1] = 0.0;
  2544. }
  2545. }
  2546. void clamp_to_software_endstops(float target[3])
  2547. {
  2548. if (min_software_endstops) {
  2549. if (target[X_AXIS] < min_pos[X_AXIS]) target[X_AXIS] = min_pos[X_AXIS];
  2550. if (target[Y_AXIS] < min_pos[Y_AXIS]) target[Y_AXIS] = min_pos[Y_AXIS];
  2551. if (target[Z_AXIS] < min_pos[Z_AXIS]) target[Z_AXIS] = min_pos[Z_AXIS];
  2552. }
  2553. if (max_software_endstops) {
  2554. if (target[X_AXIS] > max_pos[X_AXIS]) target[X_AXIS] = max_pos[X_AXIS];
  2555. if (target[Y_AXIS] > max_pos[Y_AXIS]) target[Y_AXIS] = max_pos[Y_AXIS];
  2556. if (target[Z_AXIS] > max_pos[Z_AXIS]) target[Z_AXIS] = max_pos[Z_AXIS];
  2557. }
  2558. }
  2559. #ifdef DELTA
  2560. void calculate_delta(float cartesian[3])
  2561. {
  2562. delta[X_AXIS] = sqrt(DELTA_DIAGONAL_ROD_2
  2563. - sq(DELTA_TOWER1_X-cartesian[X_AXIS])
  2564. - sq(DELTA_TOWER1_Y-cartesian[Y_AXIS])
  2565. ) + cartesian[Z_AXIS];
  2566. delta[Y_AXIS] = sqrt(DELTA_DIAGONAL_ROD_2
  2567. - sq(DELTA_TOWER2_X-cartesian[X_AXIS])
  2568. - sq(DELTA_TOWER2_Y-cartesian[Y_AXIS])
  2569. ) + cartesian[Z_AXIS];
  2570. delta[Z_AXIS] = sqrt(DELTA_DIAGONAL_ROD_2
  2571. - sq(DELTA_TOWER3_X-cartesian[X_AXIS])
  2572. - sq(DELTA_TOWER3_Y-cartesian[Y_AXIS])
  2573. ) + cartesian[Z_AXIS];
  2574. /*
  2575. SERIAL_ECHOPGM("cartesian x="); SERIAL_ECHO(cartesian[X_AXIS]);
  2576. SERIAL_ECHOPGM(" y="); SERIAL_ECHO(cartesian[Y_AXIS]);
  2577. SERIAL_ECHOPGM(" z="); SERIAL_ECHOLN(cartesian[Z_AXIS]);
  2578. SERIAL_ECHOPGM("delta x="); SERIAL_ECHO(delta[X_AXIS]);
  2579. SERIAL_ECHOPGM(" y="); SERIAL_ECHO(delta[Y_AXIS]);
  2580. SERIAL_ECHOPGM(" z="); SERIAL_ECHOLN(delta[Z_AXIS]);
  2581. */
  2582. }
  2583. #endif
  2584. void prepare_move()
  2585. {
  2586. clamp_to_software_endstops(destination);
  2587. previous_millis_cmd = millis();
  2588. #ifdef DELTA
  2589. float difference[NUM_AXIS];
  2590. for (int8_t i=0; i < NUM_AXIS; i++) {
  2591. difference[i] = destination[i] - current_position[i];
  2592. }
  2593. float cartesian_mm = sqrt(sq(difference[X_AXIS]) +
  2594. sq(difference[Y_AXIS]) +
  2595. sq(difference[Z_AXIS]));
  2596. if (cartesian_mm < 0.000001) { cartesian_mm = abs(difference[E_AXIS]); }
  2597. if (cartesian_mm < 0.000001) { return; }
  2598. float seconds = 6000 * cartesian_mm / feedrate / feedmultiply;
  2599. int steps = max(1, int(DELTA_SEGMENTS_PER_SECOND * seconds));
  2600. // SERIAL_ECHOPGM("mm="); SERIAL_ECHO(cartesian_mm);
  2601. // SERIAL_ECHOPGM(" seconds="); SERIAL_ECHO(seconds);
  2602. // SERIAL_ECHOPGM(" steps="); SERIAL_ECHOLN(steps);
  2603. for (int s = 1; s <= steps; s++) {
  2604. float fraction = float(s) / float(steps);
  2605. for(int8_t i=0; i < NUM_AXIS; i++) {
  2606. destination[i] = current_position[i] + difference[i] * fraction;
  2607. }
  2608. calculate_delta(destination);
  2609. plan_buffer_line(delta[X_AXIS], delta[Y_AXIS], delta[Z_AXIS],
  2610. destination[E_AXIS], feedrate*feedmultiply/60/100.0,
  2611. active_extruder);
  2612. }
  2613. #else
  2614. #ifdef DUAL_X_CARRIAGE
  2615. if (active_extruder_parked)
  2616. {
  2617. if (dual_x_carriage_mode == DXC_DUPLICATION_MODE && active_extruder == 0)
  2618. {
  2619. // move duplicate extruder into correct duplication position.
  2620. plan_set_position(inactive_extruder_x_pos, current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2621. plan_buffer_line(current_position[X_AXIS] + duplicate_extruder_x_offset, current_position[Y_AXIS], current_position[Z_AXIS],
  2622. current_position[E_AXIS], max_feedrate[X_AXIS], 1);
  2623. plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
  2624. st_synchronize();
  2625. extruder_duplication_enabled = true;
  2626. active_extruder_parked = false;
  2627. }
  2628. else if (dual_x_carriage_mode == DXC_AUTO_PARK_MODE) // handle unparking of head
  2629. {
  2630. if (current_position[E_AXIS] == destination[E_AXIS])
  2631. {
  2632. // this is a travel move - skit it but keep track of current position (so that it can later
  2633. // be used as start of first non-travel move)
  2634. if (delayed_move_time != 0xFFFFFFFFUL)
  2635. {
  2636. memcpy(current_position, destination, sizeof(current_position));
  2637. if (destination[Z_AXIS] > raised_parked_position[Z_AXIS])
  2638. raised_parked_position[Z_AXIS] = destination[Z_AXIS];
  2639. delayed_move_time = millis();
  2640. return;
  2641. }
  2642. }
  2643. delayed_move_time = 0;
  2644. // unpark extruder: 1) raise, 2) move into starting XY position, 3) lower
  2645. plan_buffer_line(raised_parked_position[X_AXIS], raised_parked_position[Y_AXIS], raised_parked_position[Z_AXIS], current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  2646. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], raised_parked_position[Z_AXIS],
  2647. current_position[E_AXIS], min(max_feedrate[X_AXIS],max_feedrate[Y_AXIS]), active_extruder);
  2648. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  2649. current_position[E_AXIS], max_feedrate[Z_AXIS], active_extruder);
  2650. active_extruder_parked = false;
  2651. }
  2652. }
  2653. #endif //DUAL_X_CARRIAGE
  2654. // Do not use feedmultiply for E or Z only moves
  2655. if( (current_position[X_AXIS] == destination [X_AXIS]) && (current_position[Y_AXIS] == destination [Y_AXIS])) {
  2656. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate/60, active_extruder);
  2657. }
  2658. else {
  2659. plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60/100.0, active_extruder);
  2660. }
  2661. #endif //else DELTA
  2662. for(int8_t i=0; i < NUM_AXIS; i++) {
  2663. current_position[i] = destination[i];
  2664. }
  2665. }
  2666. void prepare_arc_move(char isclockwise) {
  2667. float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
  2668. // Trace the arc
  2669. mc_arc(current_position, destination, offset, X_AXIS, Y_AXIS, Z_AXIS, feedrate*feedmultiply/60/100.0, r, isclockwise, active_extruder);
  2670. // As far as the parser is concerned, the position is now == target. In reality the
  2671. // motion control system might still be processing the action and the real tool position
  2672. // in any intermediate location.
  2673. for(int8_t i=0; i < NUM_AXIS; i++) {
  2674. current_position[i] = destination[i];
  2675. }
  2676. previous_millis_cmd = millis();
  2677. }
  2678. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  2679. #if defined(FAN_PIN)
  2680. #if CONTROLLERFAN_PIN == FAN_PIN
  2681. #error "You cannot set CONTROLLERFAN_PIN equal to FAN_PIN"
  2682. #endif
  2683. #endif
  2684. unsigned long lastMotor = 0; //Save the time for when a motor was turned on last
  2685. unsigned long lastMotorCheck = 0;
  2686. void controllerFan()
  2687. {
  2688. if ((millis() - lastMotorCheck) >= 2500) //Not a time critical function, so we only check every 2500ms
  2689. {
  2690. lastMotorCheck = millis();
  2691. if(!READ(X_ENABLE_PIN) || !READ(Y_ENABLE_PIN) || !READ(Z_ENABLE_PIN)
  2692. #if EXTRUDERS > 2
  2693. || !READ(E2_ENABLE_PIN)
  2694. #endif
  2695. #if EXTRUDER > 1
  2696. #if defined(X2_ENABLE_PIN) && X2_ENABLE_PIN > -1
  2697. || !READ(X2_ENABLE_PIN)
  2698. #endif
  2699. || !READ(E1_ENABLE_PIN)
  2700. #endif
  2701. || !READ(E0_ENABLE_PIN)) //If any of the drivers are enabled...
  2702. {
  2703. lastMotor = millis(); //... set time to NOW so the fan will turn on
  2704. }
  2705. if ((millis() - lastMotor) >= (CONTROLLERFAN_SECS*1000UL) || lastMotor == 0) //If the last time any driver was enabled, is longer since than CONTROLLERSEC...
  2706. {
  2707. digitalWrite(CONTROLLERFAN_PIN, 0);
  2708. analogWrite(CONTROLLERFAN_PIN, 0);
  2709. }
  2710. else
  2711. {
  2712. // allows digital or PWM fan output to be used (see M42 handling)
  2713. digitalWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED);
  2714. analogWrite(CONTROLLERFAN_PIN, CONTROLLERFAN_SPEED);
  2715. }
  2716. }
  2717. }
  2718. #endif
  2719. #ifdef TEMP_STAT_LEDS
  2720. static bool blue_led = false;
  2721. static bool red_led = false;
  2722. static uint32_t stat_update = 0;
  2723. void handle_status_leds(void) {
  2724. float max_temp = 0.0;
  2725. if(millis() > stat_update) {
  2726. stat_update += 500; // Update every 0.5s
  2727. for (int8_t cur_extruder = 0; cur_extruder < EXTRUDERS; ++cur_extruder) {
  2728. max_temp = max(max_temp, degHotend(cur_extruder));
  2729. max_temp = max(max_temp, degTargetHotend(cur_extruder));
  2730. }
  2731. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  2732. max_temp = max(max_temp, degTargetBed());
  2733. max_temp = max(max_temp, degBed());
  2734. #endif
  2735. if((max_temp > 55.0) && (red_led == false)) {
  2736. digitalWrite(STAT_LED_RED, 1);
  2737. digitalWrite(STAT_LED_BLUE, 0);
  2738. red_led = true;
  2739. blue_led = false;
  2740. }
  2741. if((max_temp < 54.0) && (blue_led == false)) {
  2742. digitalWrite(STAT_LED_RED, 0);
  2743. digitalWrite(STAT_LED_BLUE, 1);
  2744. red_led = false;
  2745. blue_led = true;
  2746. }
  2747. }
  2748. }
  2749. #endif
  2750. void manage_inactivity()
  2751. {
  2752. if( (millis() - previous_millis_cmd) > max_inactive_time )
  2753. if(max_inactive_time)
  2754. kill();
  2755. if(stepper_inactive_time) {
  2756. if( (millis() - previous_millis_cmd) > stepper_inactive_time )
  2757. {
  2758. if(blocks_queued() == false) {
  2759. disable_x();
  2760. disable_y();
  2761. disable_z();
  2762. disable_e0();
  2763. disable_e1();
  2764. disable_e2();
  2765. }
  2766. }
  2767. }
  2768. #if defined(KILL_PIN) && KILL_PIN > -1
  2769. if( 0 == READ(KILL_PIN) )
  2770. kill();
  2771. #endif
  2772. #if defined(CONTROLLERFAN_PIN) && CONTROLLERFAN_PIN > -1
  2773. controllerFan(); //Check if fan should be turned on to cool stepper drivers down
  2774. #endif
  2775. #ifdef EXTRUDER_RUNOUT_PREVENT
  2776. if( (millis() - previous_millis_cmd) > EXTRUDER_RUNOUT_SECONDS*1000 )
  2777. if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
  2778. {
  2779. bool oldstatus=READ(E0_ENABLE_PIN);
  2780. enable_e0();
  2781. float oldepos=current_position[E_AXIS];
  2782. float oldedes=destination[E_AXIS];
  2783. plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS],
  2784. current_position[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS],
  2785. EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
  2786. current_position[E_AXIS]=oldepos;
  2787. destination[E_AXIS]=oldedes;
  2788. plan_set_e_position(oldepos);
  2789. previous_millis_cmd=millis();
  2790. st_synchronize();
  2791. WRITE(E0_ENABLE_PIN,oldstatus);
  2792. }
  2793. #endif
  2794. #if defined(DUAL_X_CARRIAGE)
  2795. // handle delayed move timeout
  2796. if (delayed_move_time != 0 && (millis() - delayed_move_time) > 1000 && Stopped == false)
  2797. {
  2798. // travel moves have been received so enact them
  2799. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  2800. memcpy(destination,current_position,sizeof(destination));
  2801. prepare_move();
  2802. }
  2803. #endif
  2804. #ifdef TEMP_STAT_LEDS
  2805. handle_status_leds();
  2806. #endif
  2807. check_axes_activity();
  2808. }
  2809. void kill()
  2810. {
  2811. cli(); // Stop interrupts
  2812. disable_heater();
  2813. disable_x();
  2814. disable_y();
  2815. disable_z();
  2816. disable_e0();
  2817. disable_e1();
  2818. disable_e2();
  2819. #if defined(PS_ON_PIN) && PS_ON_PIN > -1
  2820. pinMode(PS_ON_PIN,INPUT);
  2821. #endif
  2822. SERIAL_ERROR_START;
  2823. SERIAL_ERRORLNPGM(MSG_ERR_KILLED);
  2824. LCD_ALERTMESSAGEPGM(MSG_KILLED);
  2825. suicide();
  2826. while(1) { /* Intentionally left empty */ } // Wait for reset
  2827. }
  2828. void Stop()
  2829. {
  2830. disable_heater();
  2831. if(Stopped == false) {
  2832. Stopped = true;
  2833. Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
  2834. SERIAL_ERROR_START;
  2835. SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
  2836. LCD_MESSAGEPGM(MSG_STOPPED);
  2837. }
  2838. }
  2839. bool IsStopped() { return Stopped; };
  2840. #ifdef FAST_PWM_FAN
  2841. void setPwmFrequency(uint8_t pin, int val)
  2842. {
  2843. val &= 0x07;
  2844. switch(digitalPinToTimer(pin))
  2845. {
  2846. #if defined(TCCR0A)
  2847. case TIMER0A:
  2848. case TIMER0B:
  2849. // TCCR0B &= ~(_BV(CS00) | _BV(CS01) | _BV(CS02));
  2850. // TCCR0B |= val;
  2851. break;
  2852. #endif
  2853. #if defined(TCCR1A)
  2854. case TIMER1A:
  2855. case TIMER1B:
  2856. // TCCR1B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
  2857. // TCCR1B |= val;
  2858. break;
  2859. #endif
  2860. #if defined(TCCR2)
  2861. case TIMER2:
  2862. case TIMER2:
  2863. TCCR2 &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
  2864. TCCR2 |= val;
  2865. break;
  2866. #endif
  2867. #if defined(TCCR2A)
  2868. case TIMER2A:
  2869. case TIMER2B:
  2870. TCCR2B &= ~(_BV(CS20) | _BV(CS21) | _BV(CS22));
  2871. TCCR2B |= val;
  2872. break;
  2873. #endif
  2874. #if defined(TCCR3A)
  2875. case TIMER3A:
  2876. case TIMER3B:
  2877. case TIMER3C:
  2878. TCCR3B &= ~(_BV(CS30) | _BV(CS31) | _BV(CS32));
  2879. TCCR3B |= val;
  2880. break;
  2881. #endif
  2882. #if defined(TCCR4A)
  2883. case TIMER4A:
  2884. case TIMER4B:
  2885. case TIMER4C:
  2886. TCCR4B &= ~(_BV(CS40) | _BV(CS41) | _BV(CS42));
  2887. TCCR4B |= val;
  2888. break;
  2889. #endif
  2890. #if defined(TCCR5A)
  2891. case TIMER5A:
  2892. case TIMER5B:
  2893. case TIMER5C:
  2894. TCCR5B &= ~(_BV(CS50) | _BV(CS51) | _BV(CS52));
  2895. TCCR5B |= val;
  2896. break;
  2897. #endif
  2898. }
  2899. }
  2900. #endif //FAST_PWM_FAN
  2901. bool setTargetedHotend(int code){
  2902. tmp_extruder = active_extruder;
  2903. if(code_seen('T')) {
  2904. tmp_extruder = code_value();
  2905. if(tmp_extruder >= EXTRUDERS) {
  2906. SERIAL_ECHO_START;
  2907. switch(code){
  2908. case 104:
  2909. SERIAL_ECHO(MSG_M104_INVALID_EXTRUDER);
  2910. break;
  2911. case 105:
  2912. SERIAL_ECHO(MSG_M105_INVALID_EXTRUDER);
  2913. break;
  2914. case 109:
  2915. SERIAL_ECHO(MSG_M109_INVALID_EXTRUDER);
  2916. break;
  2917. case 218:
  2918. SERIAL_ECHO(MSG_M218_INVALID_EXTRUDER);
  2919. break;
  2920. }
  2921. SERIAL_ECHOLN(tmp_extruder);
  2922. return true;
  2923. }
  2924. }
  2925. return false;
  2926. }